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

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

Intermediate update

File size: 7.4 KB
Line 
1package tudelft.healthpsychology.traumaontologies.answerstate;
2
3import static org.junit.Assert.assertEquals;
4import static org.mockito.Matchers.any;
5import static org.mockito.Mockito.mock;
6import static org.mockito.Mockito.when;
7
8import java.util.Collection;
9import java.util.HashMap;
10import java.util.HashSet;
11import java.util.Map;
12
13import org.junit.Test;
14
15import tudelft.healthpsychology.traumaontologies.questiontypes.TypedQuestion;
16import tudelft.utilities.tree.Tree;
17
18public class OntoPropAnswerStateTest {
19 private final Tree<String, Collection<Property>, OntologyNode> tree = mock(
20 Tree.class);
21
22 @Test
23 public void testOptionNonfinalOntState() {
24 // if ontology not yet final, we should get ontology question
25 OntologyAnswerState ontoState = mock(OntologyAnswerState.class);
26 TypedQuestion options = mock(TypedQuestion.class);
27 when(ontoState.getOptions(tree)).thenReturn(options);
28 when(options.substitute(any())).thenReturn(options);
29
30 OntoPropAnswerState state = new OntoPropAnswerState(ontoState, null);
31
32 assertEquals(options, state.getOptions(tree));
33 }
34
35 @Test(expected = NullPointerException.class)
36 public void testOptionFinalOntStateyNullPropState() {
37 // if ontology final and propstate=null, we should get NPE
38 OntologyAnswerState ontoState = mock(OntologyAnswerState.class);
39 when(ontoState.getOptions(tree)).thenReturn(null);
40 OntoPropAnswerState state = new OntoPropAnswerState(ontoState, null);
41
42 state.getOptions(tree);
43 }
44
45 @Test
46 public void testOptionsFinalOntStateNonfinalPropState() {
47 // if ontology final and propstate not, we should get propstate next
48 OntologyAnswerState ontoState = mock(OntologyAnswerState.class);
49 when(ontoState.getOptions(tree)).thenReturn(null);
50 PropertiesAnswerState propState = mock(PropertiesAnswerState.class);
51 TypedQuestion options = mock(TypedQuestion.class);
52 when(options.substitute(any())).thenReturn(options);
53
54 when(propState.getOptions(tree)).thenReturn(options);
55
56 OntoPropAnswerState state = new OntoPropAnswerState(ontoState,
57 propState);
58
59 assertEquals(options, state.getOptions(tree));
60 }
61
62 @Test
63 public void testOptionsFinalOntStateFinalPropState() {
64 // if ontology final and propstate not, we should get propstate next
65 OntologyAnswerState ontoState = mock(OntologyAnswerState.class);
66 when(ontoState.getOptions(tree)).thenReturn(null);
67 PropertiesAnswerState propState = mock(PropertiesAnswerState.class);
68 when(propState.getOptions(tree)).thenReturn(null);
69
70 OntoPropAnswerState state = new OntoPropAnswerState(ontoState,
71 propState);
72 assertEquals(null, state.getOptions(tree));
73 }
74
75 @Test
76 public void testWithCollega() {
77 OntologyAnswerState ontoState = mock(OntologyAnswerState.class);
78 OntologyNode node = mock(OntologyNode.class);
79 when(node.toString()).thenReturn("persoon");
80 when(ontoState.getNode()).thenReturn("persoon");
81
82 PropertiesAnswerState propState = mock(PropertiesAnswerState.class);
83
84 TypedQuestion options = mock(TypedQuestion.class);
85 when(options.fits(any())).thenReturn(true);
86 when(ontoState.getOptions(tree)).thenReturn(options);
87
88 OntologyAnswerState newOntoState = mock(OntologyAnswerState.class);
89 when(ontoState.with(any(String.class), any())).thenReturn(newOntoState);
90
91 when(newOntoState.getOptions(tree)).thenReturn(null);
92 OntologyNode collega = mock(OntologyNode.class);
93 when(collega.toString()).thenReturn("collega");
94 when(newOntoState.getNode()).thenReturn("collega");
95
96 OntoPropAnswerState state = new OntoPropAnswerState(ontoState,
97 propState);
98 OntoPropAnswerState newstate = state.with("Collega", tree);
99
100 assertEquals("collega",
101 newstate.getOntologyAnswer().getNode().toString());
102 }
103
104 /**
105 * Test that after the ontology is completed, the state of the propState is
106 * set correctly.
107 */
108 @Test
109 public void testOntoStateCompletion() {
110 OntologyAnswerState ontoState = mock(OntologyAnswerState.class);
111 OntologyNode node = mock(OntologyNode.class);
112 when(node.toString()).thenReturn("persoon");
113 when(ontoState.getNode()).thenReturn("persoon");
114 TypedQuestion options = mock(TypedQuestion.class);
115 when(options.fits(any())).thenReturn(true);
116 when(ontoState.getOptions(tree)).thenReturn(options);
117
118 PropertiesAnswerState propState = null;
119
120 // new ontostate is final
121 OntologyAnswerState newOntoState = mock(OntologyAnswerState.class);
122 when(ontoState.with(any(String.class), any())).thenReturn(newOntoState);
123 when(newOntoState.getOptions(tree)).thenReturn(null);
124 OntologyNode collega = mock(OntologyNode.class);
125 when(collega.toString()).thenReturn("collega");
126 when(newOntoState.getNode()).thenReturn("collega");
127 Collection<Property> collegavalues = new HashSet<>();
128 Property collegaprop = mock(Property.class);
129 when(collegaprop.toString()).thenReturn("collegaprop");
130 collegavalues.add(collegaprop);
131 when(collega.getAttribute()).thenReturn(collegavalues);
132
133 OntoPropAnswerState state = new OntoPropAnswerState(ontoState,
134 propState);
135 OntoPropAnswerState newstate = state.with("Collega", tree);
136
137 assertEquals("collega",
138 newstate.getOntologyAnswer().getNode().toString());
139 assertEquals("collega",
140 newstate.getPropertyAnswer().getNode().toString());
141 }
142
143 /**
144 * Test that when the ontologystate is completed, the propState is merged
145 * correctly with existing answers.
146 */
147 @Test
148 public void testOntoStateCompletionWithExistingPropAnswers() {
149 OntologyAnswerState ontoState = mock(OntologyAnswerState.class);
150 OntologyNode node = mock(OntologyNode.class);
151 when(node.toString()).thenReturn("persoon");
152 when(ontoState.getNode()).thenReturn("persoon");
153 TypedQuestion options = mock(TypedQuestion.class);
154 when(options.fits(any())).thenReturn(true);
155 when(ontoState.getOptions(tree)).thenReturn(options);
156
157 PropertiesAnswerState propState = mock(PropertiesAnswerState.class);
158 TypedQuestion propOptions = mock(TypedQuestion.class);
159 when(propState.getOptions(tree)).thenReturn(propOptions);
160 OntologyNode propnode = mock(OntologyNode.class);
161 Collection<Property> alreadyvalues = new HashSet<>();
162 Property prop1 = mock(Property.class);
163 when(prop1.toString()).thenReturn("property1");
164 alreadyvalues.add(prop1);
165 when(propnode.getAttribute()).thenReturn(alreadyvalues);
166 when(propState.getNode()).thenReturn("propnode");
167 Map<Property, String> alreadymap = new HashMap<>();
168 alreadymap.put(prop1, "user's answer 1");
169 when(propState.getAnswers()).thenReturn(alreadymap);
170
171 // new ontostate is final
172 OntologyAnswerState newOntoState = mock(OntologyAnswerState.class);
173 when(ontoState.with(any(String.class), any())).thenReturn(newOntoState);
174 when(newOntoState.getOptions(tree)).thenReturn(null);
175
176 // newOntoState, final and reached using ontoState.with()
177 OntologyNode collega = mock(OntologyNode.class);
178 when(collega.toString()).thenReturn("collega");
179 Collection<Property> collegavalues = new HashSet<>();
180 Property collegaprop = mock(Property.class);
181 when(collegaprop.toString()).thenReturn("collegaprop");
182 collegavalues.add(collegaprop);
183 when(collega.getAttribute()).thenReturn(collegavalues);
184 when(newOntoState.getNode()).thenReturn("collega");
185
186 // finally simulate the step
187 OntoPropAnswerState state = new OntoPropAnswerState(ontoState,
188 propState);
189 OntoPropAnswerState newstate = state.with("Collega", tree);
190
191 assertEquals("collega",
192 newstate.getOntologyAnswer().getNode().toString());
193 // was existing answer merged?
194 assertEquals(1, newstate.getPropertyAnswer().getAnswers().size());
195 // following too difficult to test?
196 // assertEquals(1, newstate.getOptions(tree));
197 }
198
199}
Note: See TracBrowser for help on using the repository browser.