package tudelft.healthpsychology.traumaontologies.answerstate; import static org.junit.Assert.assertEquals; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import org.junit.Test; import tudelft.healthpsychology.traumaontologies.questiontypes.TypedQuestion; import tudelft.utilities.tree.Tree; public class OntoPropAnswerStateTest { private final Tree, OntologyNode> tree = mock( Tree.class); @Test public void testOptionNonfinalOntState() { // if ontology not yet final, we should get ontology question OntologyAnswerState ontoState = mock(OntologyAnswerState.class); TypedQuestion options = mock(TypedQuestion.class); when(ontoState.getOptions(tree)).thenReturn(options); when(options.substitute(any())).thenReturn(options); OntoPropAnswerState state = new OntoPropAnswerState(ontoState, null); assertEquals(options, state.getOptions(tree)); } @Test(expected = NullPointerException.class) public void testOptionFinalOntStateyNullPropState() { // if ontology final and propstate=null, we should get NPE OntologyAnswerState ontoState = mock(OntologyAnswerState.class); when(ontoState.getOptions(tree)).thenReturn(null); OntoPropAnswerState state = new OntoPropAnswerState(ontoState, null); state.getOptions(tree); } @Test public void testOptionsFinalOntStateNonfinalPropState() { // if ontology final and propstate not, we should get propstate next OntologyAnswerState ontoState = mock(OntologyAnswerState.class); when(ontoState.getOptions(tree)).thenReturn(null); PropertiesAnswerState propState = mock(PropertiesAnswerState.class); TypedQuestion options = mock(TypedQuestion.class); when(options.substitute(any())).thenReturn(options); when(propState.getOptions(tree)).thenReturn(options); OntoPropAnswerState state = new OntoPropAnswerState(ontoState, propState); assertEquals(options, state.getOptions(tree)); } @Test public void testOptionsFinalOntStateFinalPropState() { // if ontology final and propstate not, we should get propstate next OntologyAnswerState ontoState = mock(OntologyAnswerState.class); when(ontoState.getOptions(tree)).thenReturn(null); PropertiesAnswerState propState = mock(PropertiesAnswerState.class); when(propState.getOptions(tree)).thenReturn(null); OntoPropAnswerState state = new OntoPropAnswerState(ontoState, propState); assertEquals(null, state.getOptions(tree)); } @Test public void testWithCollega() { OntologyAnswerState ontoState = mock(OntologyAnswerState.class); OntologyNode node = mock(OntologyNode.class); when(node.toString()).thenReturn("persoon"); when(ontoState.getNode()).thenReturn("persoon"); PropertiesAnswerState propState = mock(PropertiesAnswerState.class); TypedQuestion options = mock(TypedQuestion.class); when(options.fits(any())).thenReturn(true); when(ontoState.getOptions(tree)).thenReturn(options); OntologyAnswerState newOntoState = mock(OntologyAnswerState.class); when(ontoState.with(any(String.class), any())).thenReturn(newOntoState); when(newOntoState.getOptions(tree)).thenReturn(null); OntologyNode collega = mock(OntologyNode.class); when(collega.toString()).thenReturn("collega"); when(newOntoState.getNode()).thenReturn("collega"); OntoPropAnswerState state = new OntoPropAnswerState(ontoState, propState); OntoPropAnswerState newstate = state.with("Collega", tree); assertEquals("collega", newstate.getOntologyAnswer().getNode().toString()); } /** * Test that after the ontology is completed, the state of the propState is * set correctly. */ @Test public void testOntoStateCompletion() { OntologyAnswerState ontoState = mock(OntologyAnswerState.class); OntologyNode node = mock(OntologyNode.class); when(node.toString()).thenReturn("persoon"); when(ontoState.getNode()).thenReturn("persoon"); TypedQuestion options = mock(TypedQuestion.class); when(options.fits(any())).thenReturn(true); when(ontoState.getOptions(tree)).thenReturn(options); PropertiesAnswerState propState = null; // new ontostate is final OntologyAnswerState newOntoState = mock(OntologyAnswerState.class); when(ontoState.with(any(String.class), any())).thenReturn(newOntoState); when(newOntoState.getOptions(tree)).thenReturn(null); OntologyNode collega = mock(OntologyNode.class); when(collega.toString()).thenReturn("collega"); when(newOntoState.getNode()).thenReturn("collega"); Collection collegavalues = new HashSet<>(); Property collegaprop = mock(Property.class); when(collegaprop.toString()).thenReturn("collegaprop"); collegavalues.add(collegaprop); when(collega.getAttribute()).thenReturn(collegavalues); OntoPropAnswerState state = new OntoPropAnswerState(ontoState, propState); OntoPropAnswerState newstate = state.with("Collega", tree); assertEquals("collega", newstate.getOntologyAnswer().getNode().toString()); assertEquals("collega", newstate.getPropertyAnswer().getNode().toString()); } /** * Test that when the ontologystate is completed, the propState is merged * correctly with existing answers. */ @Test public void testOntoStateCompletionWithExistingPropAnswers() { OntologyAnswerState ontoState = mock(OntologyAnswerState.class); OntologyNode node = mock(OntologyNode.class); when(node.toString()).thenReturn("persoon"); when(ontoState.getNode()).thenReturn("persoon"); TypedQuestion options = mock(TypedQuestion.class); when(options.fits(any())).thenReturn(true); when(ontoState.getOptions(tree)).thenReturn(options); PropertiesAnswerState propState = mock(PropertiesAnswerState.class); TypedQuestion propOptions = mock(TypedQuestion.class); when(propState.getOptions(tree)).thenReturn(propOptions); OntologyNode propnode = mock(OntologyNode.class); Collection alreadyvalues = new HashSet<>(); Property prop1 = mock(Property.class); when(prop1.toString()).thenReturn("property1"); alreadyvalues.add(prop1); when(propnode.getAttribute()).thenReturn(alreadyvalues); when(propState.getNode()).thenReturn("propnode"); Map alreadymap = new HashMap<>(); alreadymap.put(prop1, "user's answer 1"); when(propState.getAnswers()).thenReturn(alreadymap); // new ontostate is final OntologyAnswerState newOntoState = mock(OntologyAnswerState.class); when(ontoState.with(any(String.class), any())).thenReturn(newOntoState); when(newOntoState.getOptions(tree)).thenReturn(null); // newOntoState, final and reached using ontoState.with() OntologyNode collega = mock(OntologyNode.class); when(collega.toString()).thenReturn("collega"); Collection collegavalues = new HashSet<>(); Property collegaprop = mock(Property.class); when(collegaprop.toString()).thenReturn("collegaprop"); collegavalues.add(collegaprop); when(collega.getAttribute()).thenReturn(collegavalues); when(newOntoState.getNode()).thenReturn("collega"); // finally simulate the step OntoPropAnswerState state = new OntoPropAnswerState(ontoState, propState); OntoPropAnswerState newstate = state.with("Collega", tree); assertEquals("collega", newstate.getOntologyAnswer().getNode().toString()); // was existing answer merged? assertEquals(1, newstate.getPropertyAnswer().getAnswers().size()); // following too difficult to test? // assertEquals(1, newstate.getOptions(tree)); } }