package tudelft.healthpsychology.traumaontologies.answerstate; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.io.File; import java.util.Arrays; import java.util.Collection; import java.util.LinkedList; import java.util.List; import org.junit.Before; import org.junit.Test; import org.semanticweb.owlapi.apibinding.OWLManager; import org.semanticweb.owlapi.model.OWLOntology; import org.semanticweb.owlapi.model.OWLOntologyCreationException; import org.semanticweb.owlapi.model.OWLOntologyManager; import tudelft.healthpsychology.traumaontologies.owltree.OwlOntologyNode; import tudelft.healthpsychology.traumaontologies.owltree.OwlTreeReasoner; import tudelft.healthpsychology.traumaontologies.questiontypes.TypedQuestion; import tudelft.utilities.junit.GeneralTests; import tudelft.utilities.tree.Tree; public class OntologyAnswerStateTest extends GeneralTests { private final static OntologyNode persoon = ontonode("persoon"); private final static OntologyNode familie = ontonode("familie"); private final static OntologyNode ouder = ontonode("ouder"); private final static OntologyNode grootouder = ontonode("grootouder"); private final static OntologyNode vriend = ontonode("vriend"); private final static OntologyNode collega = ontonode("collega"); private final static OntologyNode vader = ontonode("vader"); private final static OntologyNode moeder = ontonode("moeder"); private final static OntologyNode opa = ontonode("opa"); private final static OntologyNode oma = ontonode("oma"); private final Tree, OntologyNode> tree = mock( Tree.class); private final static OntologyAnswerState state1 = new OntologyAnswerState( "persoon", 1, false, false, "hij"); private final static OntologyAnswerState state1a = new OntologyAnswerState( "persoon", 1, false, false, "hij"); private final static OntologyAnswerState state2 = new OntologyAnswerState( "familie", 1, false, false, "hij"); private final static OntologyAnswerState state3 = new OntologyAnswerState( "persoon", 2, false, false, "hij"); private final static OntologyAnswerState state4 = new OntologyAnswerState( "persoon", 1, true, false, "hij"); private final static OntologyAnswerState state5 = new OntologyAnswerState( "persoon", 1, false, true, "hij"); private final static OntologyAnswerState state6 = new OntologyAnswerState( "persoon", 1, false, false, "zij"); @Before public void before() { // when-doReturn is raising compilation troubles for no reason... // make crazy large set, so that it will not be the max depth. List largesublist = new LinkedList(); for (int n = 0; n < 20; n++) largesublist.add(ontonode("test" + n)); when(tree.get("persoon")).thenReturn(persoon); when(tree.get("familie")).thenReturn(familie); when(tree.get("ouder")).thenReturn(ouder); when(tree.get("grootouder")).thenReturn(grootouder); when(tree.get("vriend")).thenReturn(vriend); when(tree.get("collega")).thenReturn(collega); when(tree.get("vader")).thenReturn(vader); when(tree.get("moeder")).thenReturn(moeder); when(tree.get("opa")).thenReturn(opa); when(tree.get("oma")).thenReturn(oma); // The persoon hierarchy doReturn(Arrays.asList(familie, vriend, collega)).when(persoon) .getChildren(1); doReturn(Arrays.asList(ouder, grootouder, vriend, collega)) .when(persoon).getChildren(2); doReturn(Arrays.asList(vader, moeder, opa, oma, vriend, collega)) .when(persoon).getChildren(3); doReturn(largesublist).when(persoon).getChildren(4); // the familie hierarchy doReturn(Arrays.asList(ouder, grootouder)).when(familie).getChildren(); doReturn(Arrays.asList(ouder, grootouder)).when(familie).getChildren(1); doReturn(Arrays.asList(vader, moeder, opa, oma)).when(familie) .getChildren(2); doReturn(largesublist).when(oma).getChildren(); doReturn(largesublist).when(familie).getChildren(3); when(familie.getParent()).thenReturn(persoon); } @Override public List> getGeneralTestData() { return Arrays.asList(Arrays.asList(state1, state1a), Arrays.asList(state2), Arrays.asList(state3), Arrays.asList(state4), Arrays.asList(state5), Arrays.asList(state6)); } @Override public List getGeneralTestStrings() { return Arrays.asList( "OntologyAnswerState\\[.*persoon,false,1,false,hij\\]", "OntologyAnswerState\\[.*familie,false,1,false,hij\\]", "OntologyAnswerState\\[.*persoon,false,2,false,hij\\]", "OntologyAnswerState\\[.*persoon,true,1,false,hij\\]", "OntologyAnswerState\\[.*persoon,false,1,true,hij\\]", "OntologyAnswerState\\[.*persoon,false,1,false,zij\\]"); } @Test public void smokeTest() { new OntologyAnswerState(persoon, null); } @Test public void depthPersoonTest() { OntologyAnswerState state = new OntologyAnswerState(persoon, null); assertEquals(3, state.getDepth()); } @Test public void depthFamilieTest() { OntologyAnswerState state = new OntologyAnswerState(familie, null); assertEquals(2, state.getDepth()); } @Test public void getOptionsTest() { OntologyAnswerState state = new OntologyAnswerState(persoon, null); TypedQuestion options = state.getOptions(tree); System.out.println("options=" + options); } @Test public void withtNullWithoutParentTest() { OntologyAnswerState state = new OntologyAnswerState(persoon, null); OntologyAnswerState newstate = state.with((OntologyNode) null, tree); assertEquals("persoon", newstate.getNode()); assertEquals(state.getDepth() - 1, newstate.getDepth()); } @Test public void withNullWithParentTest() { OntologyAnswerState state = new OntologyAnswerState(familie, null); int zoom = state.getDepth(); OntologyAnswerState newstate = state.with((OntologyNode) null, tree); assertNotNull("new state should be nonfinal as familie has parent", newstate.getOptions(tree)); // we should stick with familie node but zoom out 1. assertEquals("familie", newstate.getNode()); assertEquals(zoom - 1, newstate.getDepth()); } @Test public void withLeafTest() { OntologyAnswerState state = new OntologyAnswerState(familie, null); OntologyAnswerState newstate = state.with("vader", tree); assertNull("new state should be final as vader is leaf", newstate.getOptions(tree)); } @Test public void withNonLeafTest() { OntologyAnswerState state = new OntologyAnswerState(familie, null); OntologyAnswerState newstate = state.with("oma", tree); assertNotNull("new state should be nonfinal as oma is not a leaf", newstate.getOptions(tree)); } @Test public void withOtherTest() { OntologyAnswerState state = new OntologyAnswerState(familie, null); OntologyAnswerState spystate = spy(state); spystate.with("anders", tree); verify(spystate, times(1)).with((OntologyNode) null, tree); } @Test public void withVaderTest() { OntologyAnswerState state = new OntologyAnswerState(familie, null); OntologyAnswerState spystate = spy(state); spystate.with("vader", tree); verify(spystate, times(1)).with(vader, tree); } @Test(expected = IllegalArgumentException.class) public void withNonsenseTest() { OntologyAnswerState state = new OntologyAnswerState(familie, null); state.with("nonsense", tree); } @Test public void testParkState() throws OWLOntologyCreationException { OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); OWLOntology ontology = manager.loadOntologyFromOntologyDocument( new File("src/main/resources/Child Sexual Abuse.owl")); OwlTreeReasoner reas = new OwlTreeReasoner(ontology); OwlOntologyNode park = new OwlOntologyNode("Park", reas); OntologyAnswerState state = new OntologyAnswerState("park", 2, false, false, "it"); } private static OntologyNode ontonode(String label) { OntologyNode m = mock(OntologyNode.class); when(m.getLabel()).thenReturn(label); when(m.toString()).thenReturn("mock ontonode " + label); return m; } }