1 | package tudelft.healthpsychology.traumaontologies.answerstate;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertNotNull;
|
---|
5 | import static org.junit.Assert.assertNull;
|
---|
6 | import static org.mockito.Mockito.doReturn;
|
---|
7 | import static org.mockito.Mockito.mock;
|
---|
8 | import static org.mockito.Mockito.spy;
|
---|
9 | import static org.mockito.Mockito.times;
|
---|
10 | import static org.mockito.Mockito.verify;
|
---|
11 | import static org.mockito.Mockito.when;
|
---|
12 |
|
---|
13 | import java.io.File;
|
---|
14 | import java.util.Arrays;
|
---|
15 | import java.util.Collection;
|
---|
16 | import java.util.LinkedList;
|
---|
17 | import java.util.List;
|
---|
18 |
|
---|
19 | import org.junit.Before;
|
---|
20 | import org.junit.Test;
|
---|
21 | import org.semanticweb.owlapi.apibinding.OWLManager;
|
---|
22 | import org.semanticweb.owlapi.model.OWLOntology;
|
---|
23 | import org.semanticweb.owlapi.model.OWLOntologyCreationException;
|
---|
24 | import org.semanticweb.owlapi.model.OWLOntologyManager;
|
---|
25 |
|
---|
26 | import tudelft.healthpsychology.traumaontologies.owltree.OwlOntologyNode;
|
---|
27 | import tudelft.healthpsychology.traumaontologies.owltree.OwlTreeReasoner;
|
---|
28 | import tudelft.healthpsychology.traumaontologies.questiontypes.TypedQuestion;
|
---|
29 | import tudelft.utilities.junit.GeneralTests;
|
---|
30 | import tudelft.utilities.tree.Tree;
|
---|
31 |
|
---|
32 | public class OntologyAnswerStateTest extends GeneralTests<OntologyAnswerState> {
|
---|
33 |
|
---|
34 | private final static OntologyNode persoon = ontonode("persoon");
|
---|
35 | private final static OntologyNode familie = ontonode("familie");
|
---|
36 | private final static OntologyNode ouder = ontonode("ouder");
|
---|
37 | private final static OntologyNode grootouder = ontonode("grootouder");
|
---|
38 | private final static OntologyNode vriend = ontonode("vriend");
|
---|
39 | private final static OntologyNode collega = ontonode("collega");
|
---|
40 | private final static OntologyNode vader = ontonode("vader");
|
---|
41 | private final static OntologyNode moeder = ontonode("moeder");
|
---|
42 | private final static OntologyNode opa = ontonode("opa");
|
---|
43 | private final static OntologyNode oma = ontonode("oma");
|
---|
44 | private final Tree<String, Collection<Property>, OntologyNode> tree = mock(
|
---|
45 | Tree.class);
|
---|
46 |
|
---|
47 | private final static OntologyAnswerState state1 = new OntologyAnswerState(
|
---|
48 | "persoon", 1, false, false, "hij");
|
---|
49 | private final static OntologyAnswerState state1a = new OntologyAnswerState(
|
---|
50 | "persoon", 1, false, false, "hij");
|
---|
51 |
|
---|
52 | private final static OntologyAnswerState state2 = new OntologyAnswerState(
|
---|
53 | "familie", 1, false, false, "hij");
|
---|
54 | private final static OntologyAnswerState state3 = new OntologyAnswerState(
|
---|
55 | "persoon", 2, false, false, "hij");
|
---|
56 | private final static OntologyAnswerState state4 = new OntologyAnswerState(
|
---|
57 | "persoon", 1, true, false, "hij");
|
---|
58 |
|
---|
59 | private final static OntologyAnswerState state5 = new OntologyAnswerState(
|
---|
60 | "persoon", 1, false, true, "hij");
|
---|
61 | private final static OntologyAnswerState state6 = new OntologyAnswerState(
|
---|
62 | "persoon", 1, false, false, "zij");
|
---|
63 |
|
---|
64 | @Before
|
---|
65 | public void before() {
|
---|
66 | // when-doReturn is raising compilation troubles for no reason...
|
---|
67 |
|
---|
68 | // make crazy large set, so that it will not be the max depth.
|
---|
69 | List<OntologyNode> largesublist = new LinkedList<OntologyNode>();
|
---|
70 | for (int n = 0; n < 20; n++)
|
---|
71 | largesublist.add(ontonode("test" + n));
|
---|
72 |
|
---|
73 | when(tree.get("persoon")).thenReturn(persoon);
|
---|
74 | when(tree.get("familie")).thenReturn(familie);
|
---|
75 | when(tree.get("ouder")).thenReturn(ouder);
|
---|
76 | when(tree.get("grootouder")).thenReturn(grootouder);
|
---|
77 | when(tree.get("vriend")).thenReturn(vriend);
|
---|
78 | when(tree.get("collega")).thenReturn(collega);
|
---|
79 | when(tree.get("vader")).thenReturn(vader);
|
---|
80 | when(tree.get("moeder")).thenReturn(moeder);
|
---|
81 | when(tree.get("opa")).thenReturn(opa);
|
---|
82 | when(tree.get("oma")).thenReturn(oma);
|
---|
83 |
|
---|
84 | // The persoon hierarchy
|
---|
85 | doReturn(Arrays.asList(familie, vriend, collega)).when(persoon)
|
---|
86 | .getChildren(1);
|
---|
87 | doReturn(Arrays.asList(ouder, grootouder, vriend, collega))
|
---|
88 | .when(persoon).getChildren(2);
|
---|
89 | doReturn(Arrays.asList(vader, moeder, opa, oma, vriend, collega))
|
---|
90 | .when(persoon).getChildren(3);
|
---|
91 | doReturn(largesublist).when(persoon).getChildren(4);
|
---|
92 |
|
---|
93 | // the familie hierarchy
|
---|
94 | doReturn(Arrays.asList(ouder, grootouder)).when(familie).getChildren();
|
---|
95 | doReturn(Arrays.asList(ouder, grootouder)).when(familie).getChildren(1);
|
---|
96 | doReturn(Arrays.asList(vader, moeder, opa, oma)).when(familie)
|
---|
97 | .getChildren(2);
|
---|
98 | doReturn(largesublist).when(oma).getChildren();
|
---|
99 | doReturn(largesublist).when(familie).getChildren(3);
|
---|
100 | when(familie.getParent()).thenReturn(persoon);
|
---|
101 |
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | public List<List<OntologyAnswerState>> getGeneralTestData() {
|
---|
106 | return Arrays.asList(Arrays.asList(state1, state1a),
|
---|
107 | Arrays.asList(state2), Arrays.asList(state3),
|
---|
108 | Arrays.asList(state4), Arrays.asList(state5),
|
---|
109 | Arrays.asList(state6));
|
---|
110 | }
|
---|
111 |
|
---|
112 | @Override
|
---|
113 | public List<String> getGeneralTestStrings() {
|
---|
114 | return Arrays.asList(
|
---|
115 | "OntologyAnswerState\\[.*persoon,false,1,false,hij\\]",
|
---|
116 | "OntologyAnswerState\\[.*familie,false,1,false,hij\\]",
|
---|
117 | "OntologyAnswerState\\[.*persoon,false,2,false,hij\\]",
|
---|
118 | "OntologyAnswerState\\[.*persoon,true,1,false,hij\\]",
|
---|
119 | "OntologyAnswerState\\[.*persoon,false,1,true,hij\\]",
|
---|
120 | "OntologyAnswerState\\[.*persoon,false,1,false,zij\\]");
|
---|
121 | }
|
---|
122 |
|
---|
123 | @Test
|
---|
124 | public void smokeTest() {
|
---|
125 | new OntologyAnswerState(persoon, null);
|
---|
126 | }
|
---|
127 |
|
---|
128 | @Test
|
---|
129 | public void depthPersoonTest() {
|
---|
130 | OntologyAnswerState state = new OntologyAnswerState(persoon, null);
|
---|
131 | assertEquals(3, state.getDepth());
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Test
|
---|
135 | public void depthFamilieTest() {
|
---|
136 | OntologyAnswerState state = new OntologyAnswerState(familie, null);
|
---|
137 | assertEquals(2, state.getDepth());
|
---|
138 | }
|
---|
139 |
|
---|
140 | @Test
|
---|
141 | public void getOptionsTest() {
|
---|
142 | OntologyAnswerState state = new OntologyAnswerState(persoon, null);
|
---|
143 | TypedQuestion options = state.getOptions(tree);
|
---|
144 | System.out.println("options=" + options);
|
---|
145 |
|
---|
146 | }
|
---|
147 |
|
---|
148 | @Test
|
---|
149 | public void withtNullWithoutParentTest() {
|
---|
150 | OntologyAnswerState state = new OntologyAnswerState(persoon, null);
|
---|
151 | OntologyAnswerState newstate = state.with((OntologyNode) null, tree);
|
---|
152 | assertEquals("persoon", newstate.getNode());
|
---|
153 | assertEquals(state.getDepth() - 1, newstate.getDepth());
|
---|
154 | }
|
---|
155 |
|
---|
156 | @Test
|
---|
157 | public void withNullWithParentTest() {
|
---|
158 | OntologyAnswerState state = new OntologyAnswerState(familie, null);
|
---|
159 | int zoom = state.getDepth();
|
---|
160 | OntologyAnswerState newstate = state.with((OntologyNode) null, tree);
|
---|
161 | assertNotNull("new state should be nonfinal as familie has parent",
|
---|
162 | newstate.getOptions(tree));
|
---|
163 | // we should stick with familie node but zoom out 1.
|
---|
164 | assertEquals("familie", newstate.getNode());
|
---|
165 | assertEquals(zoom - 1, newstate.getDepth());
|
---|
166 | }
|
---|
167 |
|
---|
168 | @Test
|
---|
169 | public void withLeafTest() {
|
---|
170 | OntologyAnswerState state = new OntologyAnswerState(familie, null);
|
---|
171 | OntologyAnswerState newstate = state.with("vader", tree);
|
---|
172 | assertNull("new state should be final as vader is leaf",
|
---|
173 | newstate.getOptions(tree));
|
---|
174 | }
|
---|
175 |
|
---|
176 | @Test
|
---|
177 | public void withNonLeafTest() {
|
---|
178 | OntologyAnswerState state = new OntologyAnswerState(familie, null);
|
---|
179 | OntologyAnswerState newstate = state.with("oma", tree);
|
---|
180 | assertNotNull("new state should be nonfinal as oma is not a leaf",
|
---|
181 | newstate.getOptions(tree));
|
---|
182 | }
|
---|
183 |
|
---|
184 | @Test
|
---|
185 | public void withOtherTest() {
|
---|
186 | OntologyAnswerState state = new OntologyAnswerState(familie, null);
|
---|
187 | OntologyAnswerState spystate = spy(state);
|
---|
188 | spystate.with("anders", tree);
|
---|
189 | verify(spystate, times(1)).with((OntologyNode) null, tree);
|
---|
190 | }
|
---|
191 |
|
---|
192 | @Test
|
---|
193 | public void withVaderTest() {
|
---|
194 | OntologyAnswerState state = new OntologyAnswerState(familie, null);
|
---|
195 | OntologyAnswerState spystate = spy(state);
|
---|
196 | spystate.with("vader", tree);
|
---|
197 | verify(spystate, times(1)).with(vader, tree);
|
---|
198 | }
|
---|
199 |
|
---|
200 | @Test(expected = IllegalArgumentException.class)
|
---|
201 | public void withNonsenseTest() {
|
---|
202 | OntologyAnswerState state = new OntologyAnswerState(familie, null);
|
---|
203 | state.with("nonsense", tree);
|
---|
204 | }
|
---|
205 |
|
---|
206 | @Test
|
---|
207 | public void testParkState() throws OWLOntologyCreationException {
|
---|
208 | OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
|
---|
209 | OWLOntology ontology = manager.loadOntologyFromOntologyDocument(
|
---|
210 | new File("src/main/resources/Child Sexual Abuse.owl"));
|
---|
211 | OwlTreeReasoner reas = new OwlTreeReasoner(ontology);
|
---|
212 | OwlOntologyNode park = new OwlOntologyNode("Park", reas);
|
---|
213 |
|
---|
214 | OntologyAnswerState state = new OntologyAnswerState("park", 2, false,
|
---|
215 | false, "it");
|
---|
216 |
|
---|
217 | }
|
---|
218 |
|
---|
219 | private static OntologyNode ontonode(String label) {
|
---|
220 | OntologyNode m = mock(OntologyNode.class);
|
---|
221 | when(m.getLabel()).thenReturn(label);
|
---|
222 | when(m.toString()).thenReturn("mock ontonode " + label);
|
---|
223 | return m;
|
---|
224 | }
|
---|
225 |
|
---|
226 | }
|
---|