package tudelft.healthpsychology.traumaontologies.owltree; 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.io.IOException; import java.util.Arrays; import java.util.List; import org.junit.Before; import org.junit.Test; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLDataPropertyDomainAxiom; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import tudelft.healthpsychology.traumaontologies.answerstate.Property; import tudelft.healthpsychology.traumaontologies.questiontypes.TypedQuestion; import tudelft.utilities.junit.GeneralTests; import uk.ac.manchester.cs.owl.owlapi.OWLDataPropertyImpl; /** * NOTICE this is a junit test so we do NOT test if * {@link OWLDataPropertyDomainAxiom} properly implements equals. */ public class OwlPropertyTest extends GeneralTests { private static final String STRING = "http://www.w3.org/2001/XMLSchema#string"; private OwlProperty prop1, prop1a, prop2, prop3; private final OWLDataPropertyDomainAxiom dp1 = mock( OWLDataPropertyDomainAxiom.class); private final OWLDataPropertyDomainAxiom dp2 = mock( OWLDataPropertyDomainAxiom.class); private final OWLDataPropertyDomainAxiom dp3 = mock( OWLDataPropertyDomainAxiom.class); private final OwlTreeReasoner r = mock(OwlTreeReasoner.class); private final ObjectMapper jackson = new ObjectMapper(); private final String SERIALIZED = "{\"OwlProperty\":{\"range\":\"range\",\"comment\":\"comment\",\"id\":\"node\"}}"; @Before public void before() { when(r.getRange(any())).thenReturn(STRING); OWLDataPropertyImpl odpi = mockDataPropImpl("id1"); when(dp1.getProperty()).thenReturn(odpi); odpi = mockDataPropImpl("id2"); when(dp2.getProperty()).thenReturn(odpi); odpi = mockDataPropImpl("id3"); when(dp3.getProperty()).thenReturn(odpi); when(r.getComment(dp1)).thenReturn("comment1"); when(r.getComment(dp2)).thenReturn("comment2"); when(r.getComment(dp3)).thenReturn("comment3"); prop1 = new OwlProperty(dp1, r); prop1a = new OwlProperty(dp1, r); prop2 = new OwlProperty(dp2, r); prop3 = new OwlProperty(dp3, r); } private OWLDataPropertyImpl mockDataPropImpl(String id) { OWLDataPropertyImpl odp1 = mock(OWLDataPropertyImpl.class); when(odp1.getIRI()).thenReturn(IRI.create(id)); return odp1; } @Override public List> getGeneralTestData() { return Arrays.asList(Arrays.asList(prop1, prop1a), Arrays.asList(prop2), Arrays.asList(prop3)); } @Override public List getGeneralTestStrings() { return Arrays.asList("OWLProperty\\[id1,comment1\\]", "OWLProperty\\[id2,comment2\\]", "OWLProperty\\[id3,comment3\\]"); } @Test public void getAnswerTypeTest() { OwlProperty prop = new OwlProperty(dp1, r); TypedQuestion atype = prop.getQuestionType(); assertEquals("comment1", atype.getQuestion()); } @Test public void testSerialize() throws JsonProcessingException { OwlProperty prop = new OwlProperty("range", "comment", "node"); assertEquals(SERIALIZED, jackson.writeValueAsString(prop)); } @Test public void testDeserialize() throws IOException { OwlProperty prop = new OwlProperty("range", "comment", "node"); assertEquals(prop, jackson.readValue(SERIALIZED, Property.class)); } }