1 | package tudelft.healthpsychology.traumaontologies.answerstate;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.mockito.Mockito.mock;
|
---|
5 | import static org.mockito.Mockito.when;
|
---|
6 |
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.Map;
|
---|
13 |
|
---|
14 | import org.junit.Before;
|
---|
15 | import org.junit.Test;
|
---|
16 |
|
---|
17 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
18 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
19 |
|
---|
20 | import tudelft.healthpsychology.traumaontologies.owltree.OwlProperty;
|
---|
21 | import tudelft.healthpsychology.traumaontologies.questiontypes.TypedQuestion;
|
---|
22 | import tudelft.utilities.tree.Tree;
|
---|
23 |
|
---|
24 | public 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 | }
|
---|