diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java index bec8e5d..5ed2ff4 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java @@ -513,14 +513,14 @@ public abstract class ElementManagement { element = internalCreate(); } + sanityCheck(null); + oDatabaseDocument.commit(); if(update) { setReload(true); } - sanityCheck(null); - // TODO Notify to subscriptionNotification return serializeAsJsonNode().toString(); @@ -556,10 +556,10 @@ public abstract class ElementManagement { element = internalCreate(); - oDatabaseDocument.commit(); - sanityCheck(null); + oDatabaseDocument.commit(); + // TODO Notify to subscriptionNotification return serializeAsJsonNode().toString(); @@ -621,12 +621,12 @@ public abstract class ElementManagement { element = internalUpdate(); + sanityCheck(null); + oDatabaseDocument.commit(); setReload(true); - sanityCheck(null); - // TODO Notify to subscriptionNotification return serializeAsJsonNode().toString(); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/ResourceManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/ResourceManagement.java index f174f10..8fec800 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/ResourceManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/ResourceManagement.java @@ -338,9 +338,11 @@ public class ResourceManagement extends EntityManagement { ResourceType resourceType = (ResourceType) cachedType.getType(); List linkedEntities = resourceType.getFacets(); - for(LinkedEntity linkedEntity : linkedEntities) { - if(linkedEntity.getMin()>0 || linkedEntity.getMax()>0) { - constraints.add(linkedEntity); + if(linkedEntities!=null) { + for(LinkedEntity linkedEntity : linkedEntities) { + if(linkedEntity.getMin()>0 || (linkedEntity.getMax()!=null && linkedEntity.getMax()>0)) { + constraints.add(linkedEntity); + } } } @@ -352,9 +354,11 @@ public class ResourceManagement extends EntityManagement { ResourceType resourceSuperType = (ResourceType) cachedSuperType.getType(); List linkedEnt = resourceSuperType.getFacets(); - for(LinkedEntity linkedEntity : linkedEnt) { - if(linkedEntity.getMin()>0 || linkedEntity.getMax()>0) { - constraints.add(linkedEntity); + if(linkedEnt!=null) { + for(LinkedEntity linkedEntity : linkedEnt) { + if(linkedEntity.getMin()>0 || (linkedEntity.getMax()!=null && linkedEntity.getMax()>0)) { + constraints.add(linkedEntity); + } } } } @@ -377,6 +381,9 @@ public class ResourceManagement extends EntityManagement { stringBuffer.append(". Found "); stringBuffer.append(occurrence); stringBuffer.append(" instances"); + stringBuffer.append(". The constraint has been defined by "); + stringBuffer.append(linkedEntity.getSource()); + stringBuffer.append(" type."); return stringBuffer.toString(); } @@ -415,7 +422,7 @@ public class ResourceManagement extends EntityManagement { @Override public void sanityCheck(Map expectedInstances) throws SchemaViolationException, ResourceRegistryException { - JsonNode resourceInstance = serializeAsJsonNode().deepCopy(); + JsonNode resourceInstance = createCompleteJsonNode(); TypesCache typesCache = TypesCache.getInstance(); @@ -489,8 +496,8 @@ public class ResourceManagement extends EntityManagement { throw new SchemaViolationException(message); } - int max = linkedEntity.getMax(); - if(max>0 && satisfiedTimes>max) { + Integer max = linkedEntity.getMax(); + if((max!=null && max>0) && satisfiedTimes>max) { String message = constraintNotSatisfiedErrorMessage(linkedEntity, satisfiedTimes); throw new SchemaViolationException(message); } diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/ERManagementTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/ERManagementTest.java index dbb9640..939bcd5 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/ERManagementTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/ERManagementTest.java @@ -34,6 +34,7 @@ import org.gcube.informationsystem.resourceregistry.ContextTest; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility; import org.gcube.informationsystem.resourceregistry.instances.model.entities.FacetManagement; import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement; @@ -167,7 +168,7 @@ public class ERManagementTest extends ContextTest { Assert.assertTrue(deleted); } - @Test + @Test(expected = SchemaViolationException.class) public void testCreateRunningPlugin() throws Exception { RunningPlugin runningPlugin = new RunningPluginImpl(); diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml index fd988d8..49e0744 100644 --- a/src/test/resources/logback-test.xml +++ b/src/test/resources/logback-test.xml @@ -14,7 +14,8 @@ - + +