From 43fdf5e237e87c6ccd74099992f6f342845e7f3f Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 9 Mar 2021 09:51:32 +0100 Subject: [PATCH] Adding tests and assertion --- .../instances/ERManagementTest.java | 20 +- .../instances/multicontext/BasicTest.java | 206 ++++++++++-------- .../multicontext/MultiContextTest.java | 107 +++++++-- 3 files changed, 227 insertions(+), 106 deletions(-) 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 48a2548..b57e8ce 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/ERManagementTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/ERManagementTest.java @@ -26,11 +26,13 @@ import org.gcube.informationsystem.model.reference.properties.PropagationConstra import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.AddConstraint; import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.RemoveConstraint; import org.gcube.informationsystem.model.reference.relations.ConsistsOf; +import org.gcube.informationsystem.model.reference.relations.IsRelatedTo; import org.gcube.informationsystem.resourceregistry.ContextTest; import org.gcube.informationsystem.resourceregistry.api.utils.Utility; import org.gcube.informationsystem.resourceregistry.instances.model.entities.FacetManagement; import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement; import org.gcube.informationsystem.resourceregistry.instances.model.relations.ConsistsOfManagement; +import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement; import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility; import org.gcube.informationsystem.utils.ElementMapper; import org.gcube.resourcemanagement.model.impl.entities.facets.AccessPointFacetImpl; @@ -317,6 +319,18 @@ public class ERManagementTest extends ContextTest { } return resourceManagement; } + + public static IsRelatedToManagement getIsRelatedToManagement(IsRelatedTo isRelatedTo) throws Exception { + IsRelatedToManagement isRelatedToManagement = new IsRelatedToManagement(); + isRelatedToManagement.setElementType(Utility.getTypeName(isRelatedTo)); + isRelatedToManagement.setJson(ElementMapper.marshal(isRelatedTo)); + if(isRelatedTo.getHeader()!=null && isRelatedTo.getHeader().getUUID()!=null) { + isRelatedToManagement.setUUID(isRelatedTo.getHeader().getUUID()); + } + return isRelatedToManagement; + } + + public static R createResource(R r) throws Exception { ResourceManagement resourceManagement = getResourceManagement(r); @@ -340,10 +354,14 @@ public class ERManagementTest extends ContextTest { } public static HostingNode createHostingNode(EService eService) throws Exception { + return createHostingNode(eService, RemoveConstraint.cascade); + } + + public static HostingNode createHostingNode(EService eService, RemoveConstraint removeConstraint) throws Exception { HostingNode hostingNode = ERManagementTest.instantiateValidHostingNode(); if(eService!=null) { PropagationConstraint propagationConstraint = new PropagationConstraintImpl(); - propagationConstraint.setRemoveConstraint(RemoveConstraint.cascade); + propagationConstraint.setRemoveConstraint(removeConstraint); Activates activates = new ActivatesImpl(hostingNode, eService, propagationConstraint); hostingNode.attachResource(activates); diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/BasicTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/BasicTest.java index c3e8db3..6d6bb1a 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/BasicTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/BasicTest.java @@ -8,24 +8,20 @@ import java.util.List; import java.util.Map; import java.util.UUID; -import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.informationsystem.base.reference.IdentifiableElement; -import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl; import org.gcube.informationsystem.model.reference.entities.Resource; -import org.gcube.informationsystem.model.reference.properties.PropagationConstraint; -import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.AddConstraint; import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.RemoveConstraint; import org.gcube.informationsystem.model.reference.relations.ConsistsOf; import org.gcube.informationsystem.resourceregistry.ContextTest; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAvailableInAnotherContextException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationAvailableInAnotherContextException; -import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; +import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isrelatedto.IsRelatedToNotFoundException; import org.gcube.informationsystem.resourceregistry.instances.ERManagementTest; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility; import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement; import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement; import org.gcube.informationsystem.utils.ElementMapper; -import org.gcube.resourcemanagement.model.impl.relations.isrelatedto.ActivatesImpl; import org.gcube.resourcemanagement.model.reference.entities.resources.EService; import org.gcube.resourcemanagement.model.reference.entities.resources.HostingNode; import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Activates; @@ -187,103 +183,133 @@ public class BasicTest extends MultiContextTest { allResources = ElementMapper.unmarshalList(Resource.class, all); Assert.assertTrue(allResources.size()==0); } - + @Test public void testAddAndRemoveFromContext() throws Exception { - /* Creating HostingNode */ - HostingNode hostingNode = ERManagementTest.createHostingNode(); + testAddAndRemoveFromContext(RemoveConstraint.cascade); + //testAddAndRemoveFromContext(RemoveConstraint.cascadeWhenOrphan); + } + + /* + * The code does not support RemoveConstraint.keep + */ + protected void testAddAndRemoveFromContext(RemoveConstraint removeConstraint) throws Exception { + if(removeConstraint == RemoveConstraint.keep) { + throw new Exception("This test is not designed to test a RemoveConstraint.keep"); + } /* Creating EService */ EService eService = ERManagementTest.createEService(); - /* Creating Activates Relation */ - PropagationConstraint propagationConstraint = new PropagationConstraintImpl(); - propagationConstraint - .setRemoveConstraint(RemoveConstraint.cascade); - propagationConstraint - .setAddConstraint(AddConstraint.propagate); + /* Creating HostingNode */ + HostingNode hostingNode = ERManagementTest.createHostingNode(eService, removeConstraint); - - Activates activates = new ActivatesImpl<>(hostingNode, eService, - propagationConstraint); - - IsRelatedToManagement isRelatedToManagement = new IsRelatedToManagement(); - isRelatedToManagement.setElementType(Activates.NAME); - String activatesJson = ElementMapper.marshal(activates); - isRelatedToManagement.setJson(activatesJson); - String createdActivatesJson = isRelatedToManagement.create(); - - @SuppressWarnings("unchecked") - Activates createdActivates = ElementMapper.unmarshal(Activates.class, createdActivatesJson); - UUID activatesUUID = createdActivates.getHeader().getUUID(); + Activates activates = (Activates) getOutcomingIsRelatedTo(hostingNode).get(0); - - /* ------------------------------------------------------------------ */ - - isRelatedToManagement = new IsRelatedToManagement(); - isRelatedToManagement.setUUID(activatesUUID); - UUID contextUUID = ContextUtility.getInstance().getSecurityContextByFullName(ALTERNATIVE_TEST_SCOPE).getUUID(); - isRelatedToManagement.addToContext(contextUUID); - - logger.debug("Switching to alternative scope"); - ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE); - - /* The addTocontext on the relation adds the source and target too. - * So that I MUST be able to read HostinNode and EService - */ - - ResourceManagement resourceManagement = ERManagementTest.getResourceManagement(hostingNode); - resourceManagement.read(); - - resourceManagement = ERManagementTest.getResourceManagement(eService); - resourceManagement.read(); - - /* ------------------------------------------------------------------ */ - logger.debug("Setting back default scope"); - ContextTest.setContextByName(DEFAULT_TEST_SCOPE); - - resourceManagement = ERManagementTest.getResourceManagement(hostingNode); - resourceManagement.removeFromContext(ContextUtility.getCurrentSecurityContext().getUUID()); - - Map affectedInstances = resourceManagement.getAffectedInstances(); - logger.debug("Remove from Context affects the following instances {}", affectedInstances.values()); - - // Assert.assertTrue(affectedInstaces); - - /* The cascading MUST remove the relation and the target so that - * I MUST not be able to read Activates relation and EService - */ - - resourceManagement = ERManagementTest.getResourceManagement(eService); try { + + /* ------------------------------------------------------------------ */ + + // Adding Activated to ALTERNATIVE_TEST_SCOPE + + IsRelatedToManagement isRelatedToManagement = ERManagementTest.getIsRelatedToManagement(activates); + addToContextThenTestIfBehaveProperly(activates, ALTERNATIVE_TEST_SCOPE); + + /* ------------------------------------------------------------------ */ + + + // Removing HostingNode from initial context + ContextTest.setContextByName(DEFAULT_TEST_SCOPE); + + removeFromContextThenTestIfBehaveProperly(hostingNode); + + /* + * The cascading MUST remove the relation and the target so that + * I MUST not be able to read Activates relation and EService + */ + + ResourceManagement resourceManagement = ERManagementTest.getResourceManagement(hostingNode); + try { + resourceManagement.read(); + String error = String.format("{} with UUID {} should not be visible.", HostingNode.NAME, hostingNode.getHeader().getUUID()); + logger.trace(error); + throw new Exception(error); + }catch (ResourceAvailableInAnotherContextException e) { + // OK + } + + isRelatedToManagement = ERManagementTest.getIsRelatedToManagement(activates); + try { + isRelatedToManagement.read(); + String error = String.format("{} with UUID {} should not be visible.", Activates.NAME, activates.getHeader().getUUID()); + logger.trace(error); + throw new Exception(error); + }catch (RelationAvailableInAnotherContextException e) { + // OK + } + + resourceManagement = ERManagementTest.getResourceManagement(eService); + try { + resourceManagement.read(); + String error = String.format("{} with UUID {} should not be visible.", EService.NAME, eService.getHeader().getUUID()); + logger.trace(error); + throw new Exception(error); + }catch (ResourceAvailableInAnotherContextException e) { + // OK + } + + /* ------------------------------------------------------------------ */ + + // The Instances MUSt still be available in ALTERNATIVE_TEST_SCOPE + ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE); + + resourceManagement = ERManagementTest.getResourceManagement(hostingNode); resourceManagement.read(); - String error = String.format("{} with UUID {} should not be visible.", EService.NAME, eService.getHeader().getUUID()); - logger.trace(error); - throw new Exception(error); - }catch (ResourceAvailableInAnotherContextException e) { - // OK - } - - isRelatedToManagement = new IsRelatedToManagement(); - isRelatedToManagement.setUUID(activatesUUID); - try { + + isRelatedToManagement = ERManagementTest.getIsRelatedToManagement(activates); isRelatedToManagement.read(); - String error = String.format("{} with UUID {} should not be visible.", Activates.NAME, activatesUUID); - logger.trace(error); - throw new Exception(error); - }catch (RelationAvailableInAnotherContextException e) { - // OK + + resourceManagement = ERManagementTest.getResourceManagement(eService); + resourceManagement.read(); + + } finally { + + // Removing HostingNode MUST delete Activates and EService due to propagationConstraint + ResourceManagement resourceManagement = ERManagementTest.getResourceManagement(hostingNode); + resourceManagement.delete(); + + resourceManagement = ERManagementTest.getResourceManagement(hostingNode); + + try { + resourceManagement.read(); + String error = String.format("{} with UUID {} should not be found.", HostingNode.NAME, hostingNode.getHeader().getUUID()); + logger.trace(error); + throw new Exception(error); + }catch (ResourceNotFoundException e) { + // OK + } + + IsRelatedToManagement isRelatedToManagement = ERManagementTest.getIsRelatedToManagement(activates); + try { + isRelatedToManagement.read(); + String error = String.format("{} with UUID {} should not be found.", Activates.NAME, activates.getHeader().getUUID()); + logger.trace(error); + throw new Exception(error); + }catch (IsRelatedToNotFoundException e) { + // OK + } + + resourceManagement = ERManagementTest.getResourceManagement(eService); + try { + resourceManagement.read(); + String error = String.format("{} with UUID {} should not be found.", EService.NAME, eService.getHeader().getUUID()); + logger.trace(error); + throw new Exception(error); + }catch (ResourceNotFoundException e) { + // OK + } } - - - /* ------------------------------------------------------------------ */ - logger.debug("Switching to alternative scope again"); - ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE); - // TODO checks here - - resourceManagement = ERManagementTest.getResourceManagement(hostingNode); - resourceManagement.delete(); } } diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/MultiContextTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/MultiContextTest.java index 9fc7b17..edff451 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/MultiContextTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/MultiContextTest.java @@ -21,10 +21,13 @@ import org.gcube.informationsystem.model.reference.relations.Relation; import org.gcube.informationsystem.resourceregistry.ContextTest; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCacheRenewal; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAvailableInAnotherContextException; +import org.gcube.informationsystem.resourceregistry.api.utils.Utility; import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; import org.gcube.informationsystem.resourceregistry.contexts.entities.ContextManagement; import org.gcube.informationsystem.resourceregistry.instances.ERManagementTest; import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement; +import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement; import org.gcube.informationsystem.types.TypeMapper; import org.gcube.informationsystem.utils.ElementMapper; import org.junit.Assert; @@ -54,6 +57,7 @@ public class MultiContextTest extends ContextTest { if (isRelatedTo.getPropagationConstraint().getRemoveConstraint() == RemoveConstraint.cascadeWhenOrphan) { expected.put(isRelatedTo.getHeader().getUUID(), isRelatedTo); + @SuppressWarnings("rawtypes") List incoming = getIncomingIsRelatedTo(r); if(incoming.size()==1) { Assert.assertTrue(incoming.get(0).getHeader().getUUID().compareTo(isRelatedTo.getHeader().getUUID())==0); @@ -104,11 +108,27 @@ public class MultiContextTest extends ContextTest { String affectedType = affectedJsonNode.get(Element.CLASS_PROPERTY).asText(); Assert.assertTrue(affectedType.compareTo(expectedType) == 0); - + } + + if(!dryRun) { + resourceManagement = ERManagementTest.getResourceManagement(r); + try { + resourceManagement.read(); + String error = String.format("{} with UUID {} should not be visible.", Utility.getTypeName(r), r.getHeader().getUUID()); + logger.trace(error); + throw new Exception(error); + }catch (ResourceAvailableInAnotherContextException e) { + // OK + } } } + protected void removeFromContextThenTestIfBehaveProperly(Resource r) throws Exception { + removeFromContextThenTestIfBehaveProperly(r, true); + removeFromContextThenTestIfBehaveProperly(r, false); + } + private interface RelationConstraintEvaluator { public boolean mustBeAdded(Relation r); @@ -156,7 +176,11 @@ public class MultiContextTest extends ContextTest { return r; } - protected Map getExpectedInstancesAddToContext(Resource r) throws ResourceRegistryException, Exception { + protected Map getExpectedInstancesAddToContext(Resource resource) throws ResourceRegistryException, Exception { + + String json = ERManagementTest.getResourceManagement(resource).read(); + Resource r = ElementMapper.unmarshal(resource.getClass(), json); + Map expected = new HashMap<>(); expected.put(r.getHeader().getUUID(), r); @@ -202,7 +226,23 @@ public class MultiContextTest extends ContextTest { UUID contextUUID = ContextUtility.getInstance().getSecurityContextByFullName(targetContextFullName).getUUID(); resourceManagement.addToContext(contextUUID); Map affectedInstances = resourceManagement.getAffectedInstances(); - + + checkAffectedInstances(expectedInstances, affectedInstances); + + if(!dryRun) { + String currentContext = ContextTest.getCurrentContextFullName(); + ContextTest.setContextByName(targetContextFullName); + resourceManagement = ERManagementTest.getResourceManagement(r); + String json = resourceManagement.read(); + Resource resource = ElementMapper.unmarshal(r.getClass(), json); + Assert.assertTrue(resource.getClass() == r.getClass()); + Assert.assertTrue(resource.getHeader().getUUID().compareTo(r.getHeader().getUUID())==0); + ContextTest.setContextByName(currentContext); + } + } + + protected void checkAffectedInstances(Map expectedInstances, Map affectedInstances) { + SortedSet expectedInstancesUUID = new TreeSet<>(expectedInstances.keySet()); SortedSet affectedInstancesUUID = new TreeSet<>(affectedInstances.keySet()); @@ -226,24 +266,61 @@ public class MultiContextTest extends ContextTest { } - if(!dryRun) { - String currentContext = ContextTest.getCurrentContextFullName(); - ContextTest.setContextByName(targetContextFullName); - resourceManagement = ERManagementTest.getResourceManagement(r); - String json = resourceManagement.read(); - Resource resource = ElementMapper.unmarshal(r.getClass(), json); - Assert.assertTrue(resource.getClass() == r.getClass()); - Assert.assertTrue(resource.getHeader().getUUID().compareTo(r.getHeader().getUUID())==0); - ContextTest.setContextByName(currentContext); - } - } protected void addToContextThenTestIfBehaveProperly(Resource r, String targetContextFullName) throws Exception { addToContextThenTestIfBehaveProperly(r, true, targetContextFullName); addToContextThenTestIfBehaveProperly(r, false, targetContextFullName); } - + + protected void addToContextThenTestIfBehaveProperly(IsRelatedTo isRelatedTo, boolean dryRun, String targetContextFullName) + throws ResourceRegistryException, Exception { + Map expectedInstances = getExpectedInstancesAddToContext(isRelatedTo.getSource()); + expectedInstances.putAll(getExpectedInstancesAddToContext(isRelatedTo.getTarget())); + expectedInstances.put(isRelatedTo.getHeader().getUUID(), isRelatedTo); + + IsRelatedToManagement isRelatedToManagement = ERManagementTest.getIsRelatedToManagement(isRelatedTo); + isRelatedToManagement.setDryRun(dryRun); + UUID contextUUID = ContextUtility.getInstance().getSecurityContextByFullName(targetContextFullName).getUUID(); + isRelatedToManagement.addToContext(contextUUID); + Map affectedInstances = isRelatedToManagement.getAffectedInstances(); + + + checkAffectedInstances(expectedInstances, affectedInstances); + + if(!dryRun) { + String currentContext = ContextTest.getCurrentContextFullName(); + ContextTest.setContextByName(targetContextFullName); + isRelatedToManagement = ERManagementTest.getIsRelatedToManagement(isRelatedTo); + String json = isRelatedToManagement.read(); + @SuppressWarnings("unchecked") + IsRelatedTo irt = ElementMapper.unmarshal(isRelatedTo.getClass(), json); + Assert.assertTrue(isRelatedTo.getClass() == irt.getClass()); + Assert.assertTrue(isRelatedTo.getHeader().getUUID().compareTo(irt.getHeader().getUUID())==0); + + Resource source = irt.getSource(); + ResourceManagement resourceManagement = ERManagementTest.getResourceManagement(source); + json = resourceManagement.read(); + Resource resource = ElementMapper.unmarshal(source.getClass(), json); + Assert.assertTrue(resource.getClass() == source.getClass()); + Assert.assertTrue(resource.getHeader().getUUID().compareTo(source.getHeader().getUUID())==0); + + Resource target = irt.getTarget(); + resourceManagement = ERManagementTest.getResourceManagement(target); + json = resourceManagement.read(); + resource = ElementMapper.unmarshal(target.getClass(), json); + Assert.assertTrue(resource.getClass() == target.getClass()); + Assert.assertTrue(resource.getHeader().getUUID().compareTo(target.getHeader().getUUID())==0); + + ContextTest.setContextByName(currentContext); + } + } + + protected void addToContextThenTestIfBehaveProperly(IsRelatedTo isRelatedTo, String targetContextFullName) throws Exception { + addToContextThenTestIfBehaveProperly(isRelatedTo, true, targetContextFullName); + addToContextThenTestIfBehaveProperly(isRelatedTo, false, targetContextFullName); + } + protected ContextCacheRenewal contextCacheRenewal = new ContextCacheRenewal() { @Override