source: TraumaOntologies/src/test/java/tudelft/healthpsychology/traumaontologies/answerstate/PropertiesAnswerStateTest.java

Last change on this file was 5, checked in by Bart Vastenhouw, 5 years ago

Intermediate update

File size: 3.1 KB
Line 
1package tudelft.healthpsychology.traumaontologies.answerstate;
2
3import static org.junit.Assert.assertEquals;
4import static org.mockito.Mockito.mock;
5import static org.mockito.Mockito.when;
6
7import java.io.IOException;
8import java.util.Arrays;
9import java.util.Collection;
10import java.util.Collections;
11import java.util.HashMap;
12import java.util.Map;
13
14import org.junit.Before;
15import org.junit.Test;
16
17import com.fasterxml.jackson.core.JsonProcessingException;
18import com.fasterxml.jackson.databind.ObjectMapper;
19
20import tudelft.healthpsychology.traumaontologies.owltree.OwlProperty;
21import tudelft.healthpsychology.traumaontologies.questiontypes.TypedQuestion;
22import tudelft.utilities.tree.Tree;
23
24public class PropertiesAnswerStateTest {
25 private static final String SERIALIZED = "{\"PropertiesAnswerState\":{\"nodelabel\":\"node\",\"answers\":[{\"prop\":{\"OwlProperty\":{\"range\":\"string\",\"comment\":\"comment1\",\"id\":\"id1\"}},\"answer\":\"answer1\"}]}}";
26 // "{\"PropertiesAnswerState\":{\"nodelabel\":\"node\",\"answers\":{}}}";
27 private final Tree<String, Collection<Property>, OntologyNode> tree = mock(
28 Tree.class);
29 private OntologyNode node;
30 private final ObjectMapper jackson = new ObjectMapper();
31
32 @Before
33 public void before() {
34 node = mock(OntologyNode.class);
35 when(tree.get("node")).thenReturn(node);
36
37 }
38
39 @Test
40 public void getOptionsTest() {
41 // check state with no properties
42 when(node.getAttribute()).thenReturn(Collections.emptyList());
43 PropertiesAnswerState state = new PropertiesAnswerState("node");
44 assertEquals(null, state.getOptions(tree));
45 }
46
47 @Test
48 public void getOptionsTest1() {
49 // check state with a property, not yet answered
50 Property prop1 = mock(Property.class);
51 TypedQuestion questiontype1 = mock(TypedQuestion.class);
52 when(prop1.getQuestionType()).thenReturn(questiontype1);
53 when(node.getAttribute()).thenReturn(Arrays.asList(prop1));
54 PropertiesAnswerState state = new PropertiesAnswerState("node");
55 assertEquals(questiontype1, state.getOptions(tree));
56 }
57
58 @Test
59 public void getOptionsTest2() {
60 // check state with a property, not yet answered
61 Property prop1 = mock(Property.class);
62 TypedQuestion questiontype1 = mock(TypedQuestion.class);
63 when(prop1.getQuestionType()).thenReturn(questiontype1);
64 when(node.getAttribute()).thenReturn(Arrays.asList(prop1));
65 PropertiesAnswerState state = new PropertiesAnswerState("node");
66 assertEquals(questiontype1, state.getOptions(tree));
67 }
68
69 @Test
70 public void testSerialize() throws JsonProcessingException {
71 Map<Property, String> answers = new HashMap<>();
72 answers.put(new OwlProperty("string", "comment1", "id1"), "answer1");
73 PropertiesAnswerState state = new PropertiesAnswerState("node",
74 answers);
75 assertEquals(SERIALIZED, jackson.writeValueAsString(state));
76 }
77
78 @Test
79 public void testDeserialize() throws IOException {
80 Map<Property, String> answers = new HashMap<>();
81 answers.put(new OwlProperty("string", "comment1", "id1"), "answer1");
82 PropertiesAnswerState state = new PropertiesAnswerState("node",
83 answers);
84 assertEquals(state, jackson.readValue(SERIALIZED, AnswerState.class));
85 }
86
87}
Note: See TracBrowser for help on using the repository browser.