Skip to content

Commit

Permalink
Plural datatype property support - testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Dec 9, 2016
1 parent 9f27c39 commit b3403ff
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.Map;
import java.util.Set;

@OWLClass(iri = Vocabulary.cOwlClassB)
@OWLClass(iri = Vocabulary.c_OwlClassB)
public class OWLClassB {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,44 @@
import cz.cvut.kbss.jopa.model.annotations.Id;
import cz.cvut.kbss.jopa.model.annotations.OWLClass;
import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty;
import cz.cvut.kbss.jopa.test.environment.Generators;

import java.util.Date;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
* Contains a generated string URI and data property attributes of primitive wrapper types
* - boolean, int, long, double and date.
*
* @author ledvima1
*/
@OWLClass(iri = "http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassM")
@OWLClass(iri = Vocabulary.c_OwlClassM)
public class OWLClassM {

@Id(generated = true)
private String key;

@OWLDataProperty(iri = "http://krizik.felk.cvut.cz/ontologies/jopa/attributes#m-booleanAttribute")
@OWLDataProperty(iri = Vocabulary.p_m_booleanAttribute)
private Boolean booleanAttribute;

@OWLDataProperty(iri = "http://krizik.felk.cvut.cz/ontologies/jopa/attributes#m-intAttribute")
@OWLDataProperty(iri = Vocabulary.p_m_intAttribute)
private Integer intAttribute;

@OWLDataProperty(iri = "http://krizik.felk.cvut.cz/ontologies/jopa/attributes#m-longAttribute")
@OWLDataProperty(iri = Vocabulary.p_m_longAttribute)
private Long longAttribute;

@OWLDataProperty(iri = "http://krizik.felk.cvut.cz/ontologies/jopa/attributes#m-doubleAttribute")
@OWLDataProperty(iri = Vocabulary.p_m_doubleAttribute)
private Double doubleAttribute;

@OWLDataProperty(iri = "http://krizik.felk.cvut.cz/ontologies/jopa/attributes#m-dateAttribute")
@OWLDataProperty(iri = Vocabulary.p_m_dateAttribute)
private Date dateAttribute;

@OWLDataProperty(iri = "http://krizik.felk.cvut.cz/ontologies/jopa/attributes#m-enumAttribute")
@OWLDataProperty(iri = Vocabulary.p_m_enumAttribute)
private Severity enumAttribute;

@OWLDataProperty(iri = Vocabulary.p_m_IntegerSet)
private Set<Integer> integerSet;

public enum Severity {
LOW, MEDIUM, HIGH
}
Expand Down Expand Up @@ -110,6 +115,14 @@ public void setEnumAttribute(Severity enumAttribute) {
this.enumAttribute = enumAttribute;
}

public Set<Integer> getIntegerSet() {
return integerSet;
}

public void setIntegerSet(Set<Integer> integerSet) {
this.integerSet = integerSet;
}

@Override
public String toString() {
return "OWLCLassM{" +
Expand All @@ -119,6 +132,7 @@ public String toString() {
", longAttribute=" + longAttribute +
", doubleAttribute=" + doubleAttribute +
", enumAttribute=" + enumAttribute +
", integerSet=" + integerSet +
'}';
}

Expand All @@ -132,5 +146,6 @@ public void initializeTestValues(boolean includingKey) {
this.doubleAttribute = 3.14D;
this.dateAttribute = new Date();
this.enumAttribute = Severity.MEDIUM;
this.integerSet = IntStream.generate(Generators::randomInt).limit(10).boxed().collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,22 @@

public class Vocabulary {

public static final String cOwlClassB = "http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassB";
private static final String ATTRIBUTE_BASE = "http://krizik.felk.cvut.cz/ontologies/jopa/attributes#";
private static final String CLASS_BASE = "http://krizik.felk.cvut.cz/ontologies/jopa/entities#";

public static final String pAStringAttribute = "http://krizik.felk.cvut.cz/ontologies/jopa/attributes#A-stringAttribute";
public static final String pBStringAttribute = "http://krizik.felk.cvut.cz/ontologies/jopa/attributes#B-stringAttribute";
public static final String c_OwlClassB = CLASS_BASE + "OWLClassB";
public static final String c_OwlClassM = CLASS_BASE + "OWLClassM";

public static final String p_m_booleanAttribute = ATTRIBUTE_BASE + "m-booleanAttribute";
public static final String p_m_intAttribute = ATTRIBUTE_BASE + "m-intAttribute";
public static final String p_m_longAttribute = ATTRIBUTE_BASE + "m-longAttribute";
public static final String p_m_doubleAttribute = ATTRIBUTE_BASE + "m-doubleAttribute";
public static final String p_m_dateAttribute = ATTRIBUTE_BASE + "m-dateAttribute";
public static final String p_m_enumAttribute = ATTRIBUTE_BASE + "m-enumAttribute";
public static final String p_m_IntegerSet = ATTRIBUTE_BASE + "m-pluralIntAttribute";

public static final String pAStringAttribute = ATTRIBUTE_BASE + "A-stringAttribute";
public static final String pBStringAttribute = ATTRIBUTE_BASE + "B-stringAttribute";

private Vocabulary() {
throw new AssertionError();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* Copyright (C) 2016 Czech Technical University in Prague
*
* <p>
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
Expand Down Expand Up @@ -177,6 +177,10 @@ public static Set<URL> createUrls() {
}).collect(Collectors.toSet());
}

public static int randomInt() {
return RANDOM.nextInt();
}

public static int randomInt(int max) {
return RANDOM.nextInt(max);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* Copyright (C) 2016 Czech Technical University in Prague
*
* <p>
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
Expand Down Expand Up @@ -500,4 +500,18 @@ public void testPersistEntityWithMappedSuperclass() {
assertEquals(entityA.getStringAttribute(), result.getOwlClassA().getStringAttribute());
assertEquals(entityA.getTypes(), result.getOwlClassA().getTypes());
}

@Test
public void persistEntityWithDatatypePropertyCollectionPersistsAllValues() {
assertFalse(entityM.getIntegerSet().isEmpty());
this.em = getEntityManager("PersistEntityWithDatatypePropertyCollection", false);
em.getTransaction().begin();
em.persist(entityM);
em.getTransaction().commit();

assertNotNull(entityM.getKey());
final OWLClassM result = em.find(OWLClassM.class, entityM.getKey());
assertNotNull(result);
assertEquals(entityM.getIntegerSet(), result.getIntegerSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -517,4 +517,44 @@ public void removeEntityTwiceInOneTransactionRemovesIt() {

assertNull(em.find(OWLClassA.class, entityA.getUri()));
}

@Test
public void settingDatatypeCollectionToNullRemovesAllValues() {
this.em = getEntityManager("settingDatatypeCollectionToNullRemovesAllValues", true);
persist(entityM);

em.getTransaction().begin();
final OWLClassM toClear = em.find(OWLClassM.class, entityM.getKey());
toClear.setIntegerSet(null);
em.getTransaction().commit();

final OWLClassM result = em.find(OWLClassM.class, entityM.getKey());
assertNull(result.getIntegerSet());
verifyDatatypePropertiesRemoved();
}

private void verifyDatatypePropertiesRemoved() {
for (Integer value : entityM.getIntegerSet()) {
assertFalse(em.createNativeQuery("ASK { ?x ?p ?v . }", Boolean.class)
.setParameter("x", URI.create(entityM.getKey()))
.setParameter("p", URI.create(Vocabulary.p_m_IntegerSet)).setParameter("v", value)
.getSingleResult());
}
}

@Test
public void clearingDatatypeCollectionRemovesAllValues() {
this.em = getEntityManager("clearingDatatypeCollectionRemovesAllValues", true);
persist(entityM);

em.getTransaction().begin();
final OWLClassM toClear = em.find(OWLClassM.class, entityM.getKey());
toClear.getIntegerSet().clear();
em.getTransaction().commit();

final OWLClassM result = em.find(OWLClassM.class, entityM.getKey());
// Could be the cached variant, which contains empty collection, or loaded from ontology, which contains null
assertTrue(result.getIntegerSet() == null || result.getIntegerSet().isEmpty());
verifyDatatypePropertiesRemoved();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* Copyright (C) 2016 Czech Technical University in Prague
*
* <p>
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
Expand All @@ -27,6 +27,7 @@
import java.net.URI;
import java.net.URL;
import java.util.*;
import java.util.stream.IntStream;

import static org.hamcrest.core.Is.isA;
import static org.junit.Assert.*;
Expand Down Expand Up @@ -943,4 +944,41 @@ public void settingNonEmptyFieldInMappedSuperclassThrowsICViolationOnMerge() {
em.merge(entityQ);
em.getTransaction().commit();
}

@Test
public void addingValuesToDatatypePropertyCollectionAddsThemIntoRepository() {
this.em = getEntityManager("addingValuesToDatatypePropertyCollectionAddsThemIntoRepository", false);
persist(entityM);

IntStream.generate(Generators::randomInt).limit(7).forEach(entityM.getIntegerSet()::add);
em.getTransaction().begin();
em.merge(entityM);
em.getTransaction().commit();

final OWLClassM result = em.find(OWLClassM.class, entityM.getKey());
assertEquals(entityM.getIntegerSet(), result.getIntegerSet());
}

@Test
public void removingValuesFromDatatypePropertyCollectionRemovesThemFromRepository() {
this.em = getEntityManager("removingValuesFromDatatypePropertyCollectionRemovesThemFromRepository", false);
persist(entityM);

final Iterator<Integer> it = entityM.getIntegerSet().iterator();
while (it.hasNext()) {
it.next();
if (Generators.randomBoolean()) {
it.remove();
}
}
// Make sure there is at least one element
entityM.getIntegerSet().add(Generators.randomInt());

em.getTransaction().begin();
em.merge(entityM);
em.getTransaction().commit();

final OWLClassM result = em.find(OWLClassM.class, entityM.getKey());
assertEquals(entityM.getIntegerSet(), result.getIntegerSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private void initAxiomsForOWLClassB(OWLClassB entityB, NamedResource subject, As
throws OntoDriverException {
final List<Axiom<?>> axioms = new ArrayList<>();
final Axiom<?> classAssertion = new AxiomImpl<>(subject, Assertion.createClassAssertion(false),
new Value<>(NamedResource.create(Vocabulary.cOwlClassB)));
new Value<>(NamedResource.create(Vocabulary.c_OwlClassB)));
axioms.add(classAssertion);
axioms.add(new AxiomImpl<>(subject, stringAss, new Value<>(entityB.getStringAttribute())));
final AxiomDescriptor desc = new AxiomDescriptor(subject);
Expand Down

0 comments on commit b3403ff

Please sign in to comment.