From afbc42eb8b32a4387ba3aef44b5c0e34a754c1ea Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 26 Jan 2021 16:52:02 +0100 Subject: [PATCH] Added dryRun --- .../contexts/ContextUtility.java | 2 +- .../instances/base/ElementManagement.java | 8 +- .../model/entities/EntityManagement.java | 19 +- .../model/relations/RelationManagement.java | 61 ++- .../resourceregistry/rest/SharingManager.java | 13 +- .../instances/multicontext/BasicTest.java | 391 ++++++++++++++++-- 6 files changed, 427 insertions(+), 67 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ContextUtility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ContextUtility.java index 5496f3e..fd1e73b 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ContextUtility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ContextUtility.java @@ -81,7 +81,7 @@ public class ContextUtility { return includeInstanceContexts; } - private static String getCurrentContextFullName() { + public static String getCurrentContextFullName() { String token = SecurityTokenProvider.instance.get(); AuthorizationEntry authorizationEntry = null; try { 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 59c3e33..917909f 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 @@ -773,12 +773,12 @@ public abstract class ElementManagement { SecurityContext targetSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(contextUUID); - Map affecteElements = internalAddToContext(targetSecurityContext); + Map affectedInstances = internalAddToContext(targetSecurityContext); oDatabaseDocument.commit(); logger.info("{} with UUID {} successfully added to Context with UUID {}", elementType, uuid, contextUUID); - return affecteElements; + return affectedInstances; } catch(ResourceRegistryException e) { logger.error("Unable to add {} with UUID {} to Context with UUID {}", elementType, uuid, contextUUID); if(oDatabaseDocument != null) { @@ -812,12 +812,12 @@ public abstract class ElementManagement { SecurityContext targetSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(contextUUID); - Map affecteElements = internalRemoveFromContext(targetSecurityContext); + Map affectedInstances = internalRemoveFromContext(targetSecurityContext); oDatabaseDocument.commit(); logger.info("{} with UUID {} successfully removed from Context with UUID {}", elementType, uuid, contextUUID); - return affecteElements; + return affectedInstances; } catch(ResourceRegistryException e) { logger.error("Unable to remove {} with UUID {} from Context with UUID {}", elementType, uuid, contextUUID); if(oDatabaseDocument != null) { diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/EntityManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/EntityManagement.java index e095d1c..d5e453b 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/EntityManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/EntityManagement.java @@ -196,10 +196,13 @@ public abstract class EntityManagement extends EntityEl Map affectedInstances = new HashMap<>(); - targetSecurityContext.addElement(getElement(), oDatabaseDocument); + if(!dryRunContextSharing) { + targetSecurityContext.addElement(getElement(), oDatabaseDocument); + } + /* * DO NOT UNCOMMENT - * the instance is added internalAddToContext() function after + * the instance is added in internalAddToContext() function after * the update of Header metadata i.e. modifiedBy, lastUpdateTime * affectedInstances.put(uuid, serializeSelfOnly()); */ @@ -207,8 +210,8 @@ public abstract class EntityManagement extends EntityEl Iterable edges = getElement().getEdges(ODirection.OUT); for(OEdge edge : edges) { - @SuppressWarnings("rawtypes") - RelationManagement relationManagement = getRelationManagement(edge); + RelationManagement relationManagement = getRelationManagement(edge); + relationManagement.setDryRunContextSharing(dryRunContextSharing); affectedInstances.putAll(relationManagement.internalAddToContext(targetSecurityContext)); } @@ -224,12 +227,14 @@ public abstract class EntityManagement extends EntityEl Iterable edges = getElement().getEdges(ODirection.OUT); for(OEdge edge : edges) { - @SuppressWarnings("rawtypes") - RelationManagement relationManagement = getRelationManagement(edge); + RelationManagement relationManagement = getRelationManagement(edge); + relationManagement.setDryRunContextSharing(dryRunContextSharing); affectedInstances.putAll(relationManagement.internalRemoveFromContext(targetSecurityContext)); } - targetSecurityContext.removeElement(getElement(), oDatabaseDocument); + if(!dryRunContextSharing) { + targetSecurityContext.removeElement(getElement(), oDatabaseDocument); + } /* * DO NOT UNCOMMENT * the instance is added internalAddToContext() function after diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/RelationManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/RelationManagement.java index 0819374..4f00baa 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/RelationManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/RelationManagement.java @@ -30,6 +30,7 @@ import org.gcube.informationsystem.resourceregistry.instances.base.relations.Rel import org.gcube.informationsystem.resourceregistry.instances.model.entities.EntityManagement; import org.gcube.informationsystem.resourceregistry.instances.model.entities.FacetManagement; import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement; +import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility; import org.gcube.informationsystem.resourceregistry.utils.PropagationConstraintOrient; import org.gcube.informationsystem.resourceregistry.utils.Utility; import org.gcube.informationsystem.utils.ElementMapper; @@ -71,6 +72,8 @@ public abstract class RelationManagement internalAddToContext(SecurityContext targetSecurityContext) + throws ContextException, ResourceRegistryException { + try { + Map affectedInstances = reallyAddToContext(targetSecurityContext); + if(propagationConstraint.getAddConstraint()==PropagationConstraint.AddConstraint.propagate) { + if(!dryRunContextSharing) { + HeaderUtility.updateModifiedByAndLastUpdate(element); + element.save(); + } + affectedInstances.put(uuid, serializeSelfOnly()); + } + return affectedInstances; + } catch(ResourceRegistryException e) { + throw e; + } catch(Exception e) { + throw new ResourceRegistryException( + "Error Adding " + elementType + " to " + targetSecurityContext.toString(), e.getCause()); + } + } + @Override protected Map reallyAddToContext(SecurityContext targetSecurityContext) throws ContextException, ResourceRegistryException { @@ -279,7 +303,7 @@ public abstract class RelationManagement affectedInstances = new HashMap<>(); /* Adding source to Context */ - affectedInstances.putAll(getSourceEntityManagement().internalAddToContext(targetSecurityContext)); + ResourceManagement resourceManagement = getSourceEntityManagement(); + resourceManagement.setDryRunContextSharing(dryRunContextSharing); + affectedInstances.putAll(resourceManagement.internalAddToContext(targetSecurityContext)); /* Adding target to Context */ + T targetEntityManagement = getTargetEntityManagement(); + targetEntityManagement.setDryRunContextSharing(dryRunContextSharing); affectedInstances.putAll(getTargetEntityManagement().internalAddToContext(targetSecurityContext)); - targetSecurityContext.addElement(getElement(), oDatabaseDocument); + if(!dryRunContextSharing) { + targetSecurityContext.addElement(getElement(), oDatabaseDocument); + } affectedInstances.put(uuid, serializeSelfOnly()); return affectedInstances; @@ -354,7 +388,7 @@ public abstract class RelationManagement iterable = target.getEdges(ODirection.IN); Iterator iterator = iterable.iterator(); @@ -412,7 +451,7 @@ public abstract class RelationManagement elementManagement = ElementManagementUtility.getERManagement(type); elementManagement.setUUID(UUID.fromString(instanceId)); elementManagement.setDryRunContextSharing(dryRun); - ArrayNode ret = null; if(operation == SharingOperation.ADD) { affectedInstances.putAll(elementManagement.addToContext(UUID.fromString(contextId))); }else { @@ -181,17 +179,18 @@ public class SharingManager { CalledMethodProvider.instance.set(calledMethod.toString()); - @SuppressWarnings("rawtypes") - ElementManagement elementManagement = ElementManagementUtility.getERManagement(type); + ElementManagement elementManagement = ElementManagementUtility.getERManagement(type); elementManagement.setUUID(UUID.fromString(instanceId)); elementManagement.setDryRunContextSharing(dryRun); + UUID contextUUID = UUID.fromString(contextId); + ObjectMapper objectMapper = new ObjectMapper(); Map affectedInstances = null; if(operation == SharingOperation.ADD) { - affectedInstances = elementManagement.addToContext(UUID.fromString(contextId)); + affectedInstances = elementManagement.addToContext(contextUUID); }else { - affectedInstances = elementManagement.removeFromContext(UUID.fromString(contextId)); + affectedInstances = elementManagement.removeFromContext(contextUUID); } try { 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 8c30698..7523580 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,21 +8,27 @@ import java.net.URI; import java.net.URL; import java.util.Calendar; import java.util.Collection; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; import java.util.UUID; import org.gcube.com.fasterxml.jackson.databind.JsonNode; -import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode; import org.gcube.common.authorization.client.exceptions.ObjectNotFound; +import org.gcube.informationsystem.base.reference.Element; +import org.gcube.informationsystem.base.reference.IdentifiableElement; import org.gcube.informationsystem.context.reference.entities.Context; +import org.gcube.informationsystem.model.impl.properties.HeaderImpl; import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl; import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.properties.Header; 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.contexts.ContextCache; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCacheRenewal; @@ -42,6 +48,7 @@ import org.gcube.informationsystem.resourceregistry.instances.base.ElementManage 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.IsRelatedToManagement; +import org.gcube.informationsystem.types.TypeMapper; import org.gcube.informationsystem.utils.ElementMapper; import org.gcube.resourcemanagement.model.impl.entities.facets.AccessPointFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.facets.CPUFacetImpl; @@ -179,10 +186,15 @@ public class BasicTest extends ContextTest { Assert.assertTrue(deleted); } + // TODO test add to context to a resource with only one facet and add propagation constraint to unpropagate. + // TODO test remove from context to a facet which is the only facet that characterise a Resource. + + @Test - public void testCreateEServiceHostingNode() throws Exception { + public void addRemoveDryRunTest() throws Exception { EService eService = new EServiceImpl(); + // I add only one facet to have the possibility to define the tests easier SoftwareFacet softwareFacet = new SoftwareFacetImpl(); softwareFacet.setGroup("InformationSystem"); softwareFacet.setName("resource-registry"); @@ -190,31 +202,7 @@ public class BasicTest extends ContextTest { IsIdentifiedBy isIdentifiedBy = new IsIdentifiedByImpl( eService, softwareFacet, null); eService.addFacet(isIdentifiedBy); - - AccessPointFacet accessPointFacet = new AccessPointFacetImpl(); - accessPointFacet.setEndpoint(new URI("http://localhost")); - accessPointFacet.setEntryName("port1"); - eService.addFacet(accessPointFacet); - - EventFacet eventFacet = new EventFacetImpl(); - eventFacet.setDate(Calendar.getInstance().getTime()); - ValueSchema event = new ValueSchemaImpl(); - event.setValue("Created"); - eventFacet.setEvent(event); - eService.addFacet(eventFacet); - - StateFacet stateFacet = new StateFacetImpl(); - stateFacet.setValue("ready"); - eService.addFacet(stateFacet); - - LicenseFacet licenseFacet = new LicenseFacetImpl(); - licenseFacet.setName("EUPL"); - licenseFacet - .setTextURL(new URL( - "https://joinup.ec.europa.eu/community/eupl/og_page/european-union-public-licence-eupl-v11")); - eService.addFacet(licenseFacet); - - + ResourceManagement resourceManagement = new ResourceManagement(); resourceManagement.setElementType(EService.NAME); resourceManagement.setJson(ElementMapper.marshal(eService)); @@ -223,6 +211,14 @@ public class BasicTest extends ContextTest { logger.debug("Created : {}", json); eService = ElementMapper.unmarshal(EService.class, json); logger.debug("Unmarshalled {} {}", EService.NAME, eService); + + Map eServiceInstances = new HashMap<>(); + eServiceInstances.put(eService.getHeader().getUUID(), eService); + for(ConsistsOf consistsOf : eService.getConsistsOf()) { + eServiceInstances.put(consistsOf.getHeader().getUUID(), consistsOf); + eServiceInstances.put(consistsOf.getTarget().getHeader().getUUID(), consistsOf.getTarget()); + } + HostingNode hostingNode = new HostingNodeImpl(); @@ -261,9 +257,11 @@ public class BasicTest extends ContextTest { HasPersistentMemory hasPersistentMemory = new HasPersistentMemoryImpl( hostingNode, disk, null); hostingNode.addFacet(hasPersistentMemory); - + Activates activates = new ActivatesImpl( hostingNode, eService, null); + UUID activatedUUID = UUID.randomUUID(); + activates.setHeader(new HeaderImpl(activatedUUID)); hostingNode.attachResource(activates); resourceManagement = new ResourceManagement(); @@ -274,7 +272,30 @@ public class BasicTest extends ContextTest { logger.debug("Created : {}", hnJson); hostingNode = ElementMapper.unmarshal(HostingNode.class, hnJson); logger.debug("Unmarshalled {} {}", HostingNode.NAME, hostingNode); - UUID uuid = hostingNode.getHeader().getUUID(); + UUID hostingNodeUUID = hostingNode.getHeader().getUUID(); + + + Map hostingNodeInstances = new HashMap<>(); + hostingNodeInstances.put(hostingNodeUUID, hostingNode); + + for(ConsistsOf consistsOf : hostingNode.getConsistsOf()) { + hostingNodeInstances.put(consistsOf.getHeader().getUUID(), consistsOf); + hostingNodeInstances.put(consistsOf.getTarget().getHeader().getUUID(), consistsOf.getTarget()); + } + + IsRelatedToManagement isRelatedToManagement = new IsRelatedToManagement(); + isRelatedToManagement.setElementType(Activates.NAME); + isRelatedToManagement.setUUID(activatedUUID); + String activatesString = isRelatedToManagement.read(); + activates = ElementMapper.unmarshal(Activates.class, activatesString); + + Map createdInstances = new HashMap<>(); + createdInstances.putAll(hostingNodeInstances); + /* These should not be added due to default propagation constraint for IsRelatedTo relation + createdInstances.put(activatedUUID, activates); + createdInstances.putAll(eServiceInstances); + */ + Assert.assertTrue(createdInstances.size()==9); /* ------------------------------------------------------------------ */ @@ -282,18 +303,69 @@ public class BasicTest extends ContextTest { ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE); resourceManagement = new ResourceManagement(); - resourceManagement.setUUID(uuid); + resourceManagement.setUUID(hostingNodeUUID); - Map affectedInstaces = resourceManagement.addToContext(ContextUtility.getCurrentSecurityContext().getUUID()); + resourceManagement.setDryRunContextSharing(true); + UUID contextUUID = ContextUtility.getCurrentSecurityContext().getUUID(); + Map testAffectedInstances = resourceManagement.addToContext(contextUUID); - Assert.assertTrue(affectedInstaces); + resourceManagement = new ResourceManagement(); + resourceManagement.setUUID(hostingNodeUUID); + + try { + resourceManagement.read(); + logger.debug("You should not be able to read {} with UUID {} in {} with UUID {} (i.e., {})", + HostingNode.NAME, hostingNodeUUID.toString(), Context.NAME, contextUUID, ContextUtility.getCurrentContextFullName()); + }catch (AvailableInAnotherContextException e) { + // OK + } + + + resourceManagement.setDryRunContextSharing(false); + Map affectedInstances = resourceManagement.addToContext(contextUUID); + logger.debug("Add to Context afffects the following instances {}", affectedInstances.values()); + + SortedSet testAffectedInstancesUUID = new TreeSet<>(testAffectedInstances.keySet()); + SortedSet affectedInstancesUUID = new TreeSet<>(affectedInstances.keySet()); + SortedSet createdInstancesUUID = new TreeSet<>(createdInstances.keySet()); + + logger.debug("Created Instances where {}", createdInstancesUUID); + logger.debug("Test Affected Instances where {}", testAffectedInstancesUUID); + logger.debug("Affected Instances are {}", affectedInstancesUUID); + + Assert.assertTrue(testAffectedInstancesUUID.size()==affectedInstancesUUID.size()); + Assert.assertTrue(createdInstancesUUID.size()==affectedInstancesUUID.size()); + + Assert.assertTrue(createdInstancesUUID.containsAll(testAffectedInstancesUUID)); + Assert.assertTrue(testAffectedInstancesUUID.containsAll(createdInstancesUUID)); + + Assert.assertTrue(createdInstancesUUID.containsAll(affectedInstancesUUID)); + Assert.assertTrue(affectedInstancesUUID.containsAll(createdInstancesUUID)); + + Assert.assertTrue(testAffectedInstancesUUID.containsAll(affectedInstancesUUID)); + Assert.assertTrue(affectedInstancesUUID.containsAll(testAffectedInstancesUUID)); + + + for(UUID uuid : affectedInstances.keySet()) { + JsonNode testAffectedJsonNode = testAffectedInstances.get(uuid); + String testAffectedType = testAffectedJsonNode.get(Element.CLASS_PROPERTY).asText(); + + JsonNode affectedJsonNode = affectedInstances.get(uuid); + String affectedType = affectedJsonNode.get(Element.CLASS_PROPERTY).asText(); + + IdentifiableElement identifiableElement = createdInstances.get(uuid); + String createdType = TypeMapper.getType(identifiableElement.getClass()); + + Assert.assertTrue(affectedType.compareTo(testAffectedType)==0); + Assert.assertTrue(affectedType.compareTo(createdType)==0); + } resourceManagement = new ResourceManagement(); - resourceManagement.setUUID(uuid); + resourceManagement.setUUID(hostingNodeUUID); String hnString = resourceManagement.read(); HostingNode readHN = ElementMapper.unmarshal(HostingNode.class, hnString); - Assert.assertTrue(readHN.getHeader().getUUID().compareTo(uuid) == 0); + Assert.assertTrue(readHN.getHeader().getUUID().compareTo(hostingNodeUUID) == 0); UUID eServiceUUID = eService.getHeader().getUUID(); try { @@ -321,7 +393,250 @@ public class BasicTest extends ContextTest { */ resourceManagement = new ResourceManagement(); - resourceManagement.setUUID(uuid); + resourceManagement.setUUID(hostingNodeUUID); + boolean deleted = resourceManagement.delete(); + Assert.assertTrue(deleted); + + /* ------------------------------------------------------------------ */ + + logger.debug("Setting back default scope"); + ContextTest.setContextByName(DEFAULT_TEST_SCOPE); + + resourceManagement = new ResourceManagement(); + resourceManagement.setUUID(eServiceUUID); + deleted = resourceManagement.delete(); + Assert.assertTrue(deleted); + + } + + + + @Test + public void testCreateEServiceHostingNode() throws Exception { + EService eService = new EServiceImpl(); + + SoftwareFacet softwareFacet = new SoftwareFacetImpl(); + softwareFacet.setGroup("InformationSystem"); + softwareFacet.setName("resource-registry"); + softwareFacet.setVersion("1.1.0"); + IsIdentifiedBy isIdentifiedBy = new IsIdentifiedByImpl( + eService, softwareFacet, null); + eService.addFacet(isIdentifiedBy); + + AccessPointFacet accessPointFacet = new AccessPointFacetImpl(); + accessPointFacet.setEndpoint(new URI("http://localhost")); + accessPointFacet.setEntryName("port1"); + eService.addFacet(accessPointFacet); + + EventFacet eventFacet = new EventFacetImpl(); + eventFacet.setDate(Calendar.getInstance().getTime()); + ValueSchema event = new ValueSchemaImpl(); + event.setValue("Created"); + eventFacet.setEvent(event); + eService.addFacet(eventFacet); + + StateFacet stateFacet = new StateFacetImpl(); + stateFacet.setValue("ready"); + eService.addFacet(stateFacet); + + LicenseFacet licenseFacet = new LicenseFacetImpl(); + licenseFacet.setName("EUPL"); + licenseFacet + .setTextURL(new URL( + "https://joinup.ec.europa.eu/community/eupl/og_page/european-union-public-licence-eupl-v11")); + eService.addFacet(licenseFacet); + + ResourceManagement resourceManagement = new ResourceManagement(); + resourceManagement.setElementType(EService.NAME); + resourceManagement.setJson(ElementMapper.marshal(eService)); + + String json = resourceManagement.create(); + logger.debug("Created : {}", json); + eService = ElementMapper.unmarshal(EService.class, json); + logger.debug("Unmarshalled {} {}", EService.NAME, eService); + + Map eServiceInstances = new HashMap<>(); + eServiceInstances.put(eService.getHeader().getUUID(), eService); + for(ConsistsOf consistsOf : eService.getConsistsOf()) { + eServiceInstances.put(consistsOf.getHeader().getUUID(), consistsOf); + eServiceInstances.put(consistsOf.getTarget().getHeader().getUUID(), consistsOf.getTarget()); + } + + + + HostingNode hostingNode = new HostingNodeImpl(); + + NetworkingFacet networkingFacet = new NetworkingFacetImpl(); + networkingFacet.setIPAddress("146.48.87.183"); + networkingFacet.setHostName("pc-frosini.isti.cnr.it"); + networkingFacet.setDomainName("isti.cnr.it"); + networkingFacet.setMask("255.255.248.0"); + networkingFacet.setBroadcastAddress("146.48.87.255"); + + IsIdentifiedBy isIdentifiedByHNNF = new IsIdentifiedByImpl( + hostingNode, networkingFacet, null); + hostingNode.addFacet(isIdentifiedByHNNF); + + CPUFacet cpuFacet = new CPUFacetImpl(); + cpuFacet.setClockSpeed("1 GHz"); + cpuFacet.setModel("Opteron"); + cpuFacet.setVendor("AMD"); + hostingNode.addFacet(cpuFacet); + + MemoryFacet ram = new MemoryFacetImpl(); + ram.setSize(8); + ram.setUnit(MemoryUnit.GB); + ram.setUsed(2); + + HasVolatileMemory hasVolatileMemory = new HasVolatileMemoryImpl( + hostingNode, ram, null); + hostingNode.addFacet(hasVolatileMemory); + + MemoryFacet disk = new MemoryFacetImpl(); + disk.setSize(256); + disk.setUnit(MemoryUnit.GB); + disk.setUsed(120); + + HasPersistentMemory hasPersistentMemory = new HasPersistentMemoryImpl( + hostingNode, disk, null); + hostingNode.addFacet(hasPersistentMemory); + + Activates activates = new ActivatesImpl( + hostingNode, eService, null); + UUID activatedUUID = UUID.randomUUID(); + activates.setHeader(new HeaderImpl(activatedUUID)); + hostingNode.attachResource(activates); + + resourceManagement = new ResourceManagement(); + resourceManagement.setElementType(HostingNode.NAME); + resourceManagement.setJson(ElementMapper.marshal(hostingNode)); + + String hnJson = resourceManagement.create(); + logger.debug("Created : {}", hnJson); + hostingNode = ElementMapper.unmarshal(HostingNode.class, hnJson); + logger.debug("Unmarshalled {} {}", HostingNode.NAME, hostingNode); + UUID hostingNodeUUID = hostingNode.getHeader().getUUID(); + + + Map hostingNodeInstances = new HashMap<>(); + hostingNodeInstances.put(hostingNodeUUID, hostingNode); + + for(ConsistsOf consistsOf : hostingNode.getConsistsOf()) { + hostingNodeInstances.put(consistsOf.getHeader().getUUID(), consistsOf); + hostingNodeInstances.put(consistsOf.getTarget().getHeader().getUUID(), consistsOf.getTarget()); + } + + IsRelatedToManagement isRelatedToManagement = new IsRelatedToManagement(); + isRelatedToManagement.setElementType(Activates.NAME); + isRelatedToManagement.setUUID(activatedUUID); + String activatesString = isRelatedToManagement.read(); + activates = ElementMapper.unmarshal(Activates.class, activatesString); + + Map createdInstances = new HashMap<>(); + createdInstances.putAll(hostingNodeInstances); + /* These should not be added due to default propagation constraint for IsRelatedTo relation + createdInstances.put(activatedUUID, activates); + createdInstances.putAll(eServiceInstances); + */ + Assert.assertTrue(createdInstances.size()==9); + + /* ------------------------------------------------------------------ */ + + logger.debug("Switching to another scope"); + ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE); + + resourceManagement = new ResourceManagement(); + resourceManagement.setUUID(hostingNodeUUID); + + resourceManagement.setDryRunContextSharing(true); + UUID contextUUID = ContextUtility.getCurrentSecurityContext().getUUID(); + Map testAffectedInstances = resourceManagement.addToContext(contextUUID); + + resourceManagement = new ResourceManagement(); + resourceManagement.setUUID(hostingNodeUUID); + + try { + resourceManagement.read(); + logger.debug("You should not be able to read {} with UUID {} in {} with UUID {} (i.e., {})", + HostingNode.NAME, hostingNodeUUID.toString(), Context.NAME, contextUUID, ContextUtility.getCurrentContextFullName()); + }catch (AvailableInAnotherContextException e) { + // OK + } + + + resourceManagement.setDryRunContextSharing(false); + Map affectedInstances = resourceManagement.addToContext(contextUUID); + logger.debug("Add to Context afffects the following instances {}", affectedInstances.values()); + + SortedSet testAffectedInstancesUUID = new TreeSet<>(testAffectedInstances.keySet()); + SortedSet affectedInstancesUUID = new TreeSet<>(affectedInstances.keySet()); + SortedSet createdInstancesUUID = new TreeSet<>(createdInstances.keySet()); + + logger.debug("Created Instances where {}", createdInstancesUUID); + logger.debug("Test Affected Instances where {}", testAffectedInstancesUUID); + logger.debug("Affected Instances are {}", affectedInstancesUUID); + + Assert.assertTrue(testAffectedInstancesUUID.size()==affectedInstancesUUID.size()); + Assert.assertTrue(createdInstancesUUID.size()==affectedInstancesUUID.size()); + + Assert.assertTrue(createdInstancesUUID.containsAll(testAffectedInstancesUUID)); + Assert.assertTrue(testAffectedInstancesUUID.containsAll(createdInstancesUUID)); + + Assert.assertTrue(createdInstancesUUID.containsAll(affectedInstancesUUID)); + Assert.assertTrue(affectedInstancesUUID.containsAll(createdInstancesUUID)); + + Assert.assertTrue(testAffectedInstancesUUID.containsAll(affectedInstancesUUID)); + Assert.assertTrue(affectedInstancesUUID.containsAll(testAffectedInstancesUUID)); + + + for(UUID uuid : affectedInstances.keySet()) { + JsonNode testAffectedJsonNode = testAffectedInstances.get(uuid); + String testAffectedType = testAffectedJsonNode.get(Element.CLASS_PROPERTY).asText(); + + JsonNode affectedJsonNode = affectedInstances.get(uuid); + String affectedType = affectedJsonNode.get(Element.CLASS_PROPERTY).asText(); + + IdentifiableElement identifiableElement = createdInstances.get(uuid); + String createdType = TypeMapper.getType(identifiableElement.getClass()); + + Assert.assertTrue(affectedType.compareTo(testAffectedType)==0); + Assert.assertTrue(affectedType.compareTo(createdType)==0); + } + + resourceManagement = new ResourceManagement(); + resourceManagement.setUUID(hostingNodeUUID); + + String hnString = resourceManagement.read(); + HostingNode readHN = ElementMapper.unmarshal(HostingNode.class, hnString); + Assert.assertTrue(readHN.getHeader().getUUID().compareTo(hostingNodeUUID) == 0); + + UUID eServiceUUID = eService.getHeader().getUUID(); + try { + resourceManagement = new ResourceManagement(); + resourceManagement.setUUID(eServiceUUID); + resourceManagement.read(); + } catch (ResourceAvailableInAnotherContextException e) { + logger.debug("Resource with {} Not Found as Expected", + eServiceUUID); + } + + /* Commented because the behavior has been changed + try { + resourceManagement = new ResourceManagement(); + resourceManagement.setUUID(eServiceUUID); + resourceManagement.delete(); + logger.debug("You should not be able to delete EService with UUID {}", + uuid); + throw new Exception( + "You should not be able to delete EService with UUID " + uuid); + } catch (ResourceAvailableInAnotherContextException e) { + logger.debug("Resource with {} Not Deleted as Expected", + eServiceUUID); + } + */ + + resourceManagement = new ResourceManagement(); + resourceManagement.setUUID(hostingNodeUUID); boolean deleted = resourceManagement.delete(); Assert.assertTrue(deleted); @@ -443,8 +758,10 @@ public class BasicTest extends ContextTest { resourceManagement = new ResourceManagement(); resourceManagement.setUUID(hnUUID); - Map affectedInstaces = resourceManagement.removeFromContext(ContextUtility.getCurrentSecurityContext().getUUID()); - Assert.assertTrue(affectedInstaces); + Map affectedInstances = resourceManagement.removeFromContext(ContextUtility.getCurrentSecurityContext().getUUID()); + logger.debug("Remove from Context afffects 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