From a553b9cd8d7a5bf54d387c613f706b749bec3d83 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 17 Feb 2021 15:51:25 +0100 Subject: [PATCH] Reorganizing tests --- .../instances/multicontext/BasicTest.java | 550 +----------------- .../instances/multicontext/ComplexTest.java | 152 +++++ .../instances/multicontext/EnhancedTest.java | 270 +++++++++ .../multicontext/MultiContextTest.java | 235 ++++++++ .../types/SchemaManagementImplTest.java | 16 + 5 files changed, 674 insertions(+), 549 deletions(-) create mode 100644 src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/ComplexTest.java create mode 100644 src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/EnhancedTest.java create mode 100644 src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/MultiContextTest.java 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 fca8db1..2062a5d 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 @@ -3,39 +3,25 @@ */ package org.gcube.informationsystem.resourceregistry.instances.multicontext; -import java.io.IOException; 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.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.Entity; import org.gcube.informationsystem.model.reference.entities.Facet; -import org.gcube.informationsystem.model.reference.entities.Resource; -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.model.reference.relations.IsRelatedTo; -import org.gcube.informationsystem.model.reference.relations.Relation; import org.gcube.informationsystem.resourceregistry.ContextTest; -import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; -import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCacheRenewal; import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException; @@ -43,17 +29,13 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet. 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.api.utils.Utility; import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; -import org.gcube.informationsystem.resourceregistry.contexts.entities.ContextManagement; import org.gcube.informationsystem.resourceregistry.instances.SmartgearResourcesTest; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility; -import org.gcube.informationsystem.resourceregistry.instances.model.ERManagementUtility; 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; @@ -91,12 +73,10 @@ import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.orientechnologies.orient.core.record.ODirection; - /** * @author Luca Frosini (ISTI - CNR) */ -public class BasicTest extends ContextTest { +public class BasicTest extends MultiContextTest { private static Logger logger = LoggerFactory .getLogger(BasicTest.class); @@ -232,238 +212,6 @@ public class BasicTest extends ContextTest { } } - protected Map getRemovedExpectedInstances(Resource r) { - Map expected = new HashMap<>(); - expected.put(r.getHeader().getUUID(), r); - // TODO query - - for(IsRelatedTo isRelatedTo : r.getIsRelatedTo()) { - // TODO - if(isRelatedTo.getPropagationConstraint().getRemoveConstraint() == RemoveConstraint.cascade) { - expected.put(isRelatedTo.getHeader().getUUID(), isRelatedTo); - expected.putAll(getRemovedExpectedInstances(isRelatedTo.getTarget())); - - // TODO - } - - if(isRelatedTo.getPropagationConstraint().getRemoveConstraint() == RemoveConstraint.cascadeWhenOrphan) { - expected.put(isRelatedTo.getHeader().getUUID(), isRelatedTo); - expected.putAll(getRemovedExpectedInstances(isRelatedTo.getTarget())); - - // TODO - - // TODO - } - } - for(ConsistsOf consistsOf : r.getConsistsOf()) { - if(consistsOf.getPropagationConstraint().getRemoveConstraint() == RemoveConstraint.cascade || consistsOf.getPropagationConstraint().getRemoveConstraint() == RemoveConstraint.cascadeWhenOrphan ) { - expected.put(consistsOf.getHeader().getUUID(), consistsOf); - Facet facet = consistsOf.getTarget(); - expected.put(facet.getHeader().getUUID(), facet); - } - } - return expected; - } - - protected void removeFromContextThenTestIfBehaveProperly(Resource r, boolean dryRun) throws ResourceRegistryException { - Map expectedInstances = getRemovedExpectedInstances(r); - - ResourceManagement resourceManagement = new ResourceManagement(); - resourceManagement.setElementType(TypeMapper.getType(r.getClass())); - resourceManagement.setUUID(r.getHeader().getUUID()); - resourceManagement.setDryRunContextSharing(dryRun); - UUID contextUUID = ContextUtility.getCurrentSecurityContext().getUUID(); - Map affectedInstances = resourceManagement.removeFromContext(contextUUID); - - SortedSet expectedInstancesUUID = new TreeSet<>(expectedInstances.keySet()); - SortedSet affectedInstancesUUID = new TreeSet<>(affectedInstances.keySet()); - - logger.trace("Expected Instances are {} : {}", expectedInstancesUUID.size(), expectedInstancesUUID); - logger.trace("Affected Instances are {} : {}", affectedInstancesUUID.size(), affectedInstancesUUID); - - Assert.assertTrue(expectedInstancesUUID.size()==affectedInstancesUUID.size()); - - Assert.assertTrue(expectedInstancesUUID.containsAll(affectedInstancesUUID)); - Assert.assertTrue(affectedInstancesUUID.containsAll(expectedInstancesUUID)); - - - for(UUID uuid : affectedInstances.keySet()) { - - Element element = expectedInstances.get(uuid); - String expectedType = TypeMapper.getType(element.getClass()); - - JsonNode affectedJsonNode = affectedInstances.get(uuid); - String affectedType = affectedJsonNode.get(Element.CLASS_PROPERTY).asText(); - - Assert.assertTrue(affectedType.compareTo(expectedType)==0); - - } - - } - - interface RelationConstraint { - - public boolean mustBeAdded(Relation r); - - public boolean mustBeRemoved(Relation r); - - } - - protected Resource getAndAddIsRelatedTo(Resource r, RelationConstraint relationConstraint) throws ResourceRegistryException, Exception { - ResourceManagement resourceManagement = new ResourceManagement(); - resourceManagement.setElementType(Resource.NAME); - - String resourceType = TypeMapper.getType(r); - UUID resourceUUID = r.getHeader().getUUID(); - - // resourceManagement.setUUID(resourceUUID); - String ret = resourceManagement.query(IsRelatedTo.NAME, resourceType, resourceUUID, ODirection.IN, true, new HashMap<>(), true); - @SuppressWarnings("rawtypes") - List isRelatedToList = ElementMapper.unmarshalList(IsRelatedTo.class, ret); - for(@SuppressWarnings("rawtypes") IsRelatedTo isRelatedTo : isRelatedToList) { - @SuppressWarnings("unchecked") - boolean mustBeAdded = relationConstraint.mustBeAdded(isRelatedTo); - if(mustBeAdded) { - @SuppressWarnings("unchecked") - IsRelatedTo irt = (IsRelatedTo) isRelatedTo; - r.attachResource(irt); - } - } - return r; - } - - - - protected Map getAddedExpectedInstances(Resource r) throws ResourceRegistryException, Exception { - Map expected = new HashMap<>(); - expected.put(r.getHeader().getUUID(), r); - - RelationConstraint propagateConstraint = new RelationConstraint() { - - @Override - public boolean mustBeAdded(Relation r) { - return r.getPropagationConstraint().getAddConstraint() == AddConstraint.propagate; - } - - @Override - public boolean mustBeRemoved(Relation r) { - throw new UnsupportedOperationException(); - } - - }; - - r = getAndAddIsRelatedTo(r, propagateConstraint); - - for(IsRelatedTo isRelatedTo : r.getIsRelatedTo()) { - if(isRelatedTo.getPropagationConstraint().getAddConstraint() == AddConstraint.propagate) { - expected.put(isRelatedTo.getHeader().getUUID(), isRelatedTo); - expected.putAll(getAddedExpectedInstances(isRelatedTo.getTarget())); - } - } - for(ConsistsOf consistsOf : r.getConsistsOf()) { - if(consistsOf.getPropagationConstraint().getAddConstraint() == AddConstraint.propagate) { - expected.put(consistsOf.getHeader().getUUID(), consistsOf); - Facet facet = consistsOf.getTarget(); - expected.put(facet.getHeader().getUUID(), facet); - } - } - return expected; - } - - protected void addToContextThenTestIfBehaveProperly(Resource r, boolean dryRun, String targetContextFullName) throws ResourceRegistryException, Exception { - - Map expectedInstances = getAddedExpectedInstances(r); - - ResourceManagement resourceManagement = new ResourceManagement(); - resourceManagement.setElementType(TypeMapper.getType(r)); - resourceManagement.setUUID(r.getHeader().getUUID()); - resourceManagement.setDryRunContextSharing(dryRun); - UUID contextUUID = ContextUtility.getInstance().getSecurityContextByFullName(targetContextFullName).getUUID(); - Map affectedInstances = resourceManagement.addToContext(contextUUID); - - SortedSet expectedInstancesUUID = new TreeSet<>(expectedInstances.keySet()); - SortedSet affectedInstancesUUID = new TreeSet<>(affectedInstances.keySet()); - - logger.trace("Expected Instances are {} : {}", expectedInstancesUUID.size(), expectedInstancesUUID); - logger.trace("Affected Instances are {} : {}", affectedInstancesUUID.size(), affectedInstancesUUID); - - Assert.assertTrue(expectedInstancesUUID.size()==affectedInstancesUUID.size()); - - Assert.assertTrue(expectedInstancesUUID.containsAll(affectedInstancesUUID)); - Assert.assertTrue(affectedInstancesUUID.containsAll(expectedInstancesUUID)); - - - for(UUID uuid : affectedInstances.keySet()) { - - Element element = expectedInstances.get(uuid); - String expectedType = TypeMapper.getType(element.getClass()); - - JsonNode affectedJsonNode = affectedInstances.get(uuid); - String affectedType = affectedJsonNode.get(Element.CLASS_PROPERTY).asText(); - - Assert.assertTrue(affectedType.compareTo(expectedType)==0); - - } - - } - - @Test - public void testNoFollows() throws Exception { - ContextTest.setContextByName(DEFAULT_TEST_SCOPE); - EService eService = new EServiceImpl(); - - SoftwareFacet softwareFacet = new SoftwareFacetImpl(); - softwareFacet.setGroup("InformationSystem"); - softwareFacet.setName("resource-registry"); - softwareFacet.setVersion("1.1.0"); - PropagationConstraint propagationConstraint = new PropagationConstraintImpl(); - propagationConstraint.setAddConstraint(AddConstraint.propagate); - propagationConstraint.setRemoveConstraint(RemoveConstraint.cascade); - - IsIdentifiedBy isIdentifiedBy = new IsIdentifiedByImpl( - eService, softwareFacet, null); - eService.addFacet(isIdentifiedBy); - - 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); - - JsonNode jsonNode = ElementMapper.getObjectMapper().readTree(json); - - Map expectedInstances = new HashMap<>(); - expectedInstances.put(eService.getHeader().getUUID(), jsonNode); - - UUID contextUUID = ContextUtility.getInstance().getSecurityContextByFullName(ALTERNATIVE_TEST_SCOPE).getUUID(); - - try { - ERManagementUtility.addToContextNoPropagationConstraint(expectedInstances, contextUUID, true); - }catch (ResourceRegistryException e) { - logger.debug("As expected {}", e.getMessage()); - } - - @SuppressWarnings("unchecked") - IsIdentifiedBy gotIsIdentifiedBy = (IsIdentifiedBy) eService.getConsistsOf(IsIdentifiedBy.class).get(0); - - JsonNode gotIsIdentifiedByJsonNode = ElementMapper.getObjectMapper().readTree(ElementMapper.marshal(gotIsIdentifiedBy)); - expectedInstances.put(gotIsIdentifiedBy.getHeader().getUUID(), gotIsIdentifiedByJsonNode); - - try { - ERManagementUtility.addToContextNoPropagationConstraint(expectedInstances, contextUUID, true); - }catch (ResourceRegistryException e) { - // as expected - logger.debug("As expected {}", e.getMessage()); - }finally { - resourceManagement.delete(); - } - - } - - @Test public void testCreateEServiceHostingNode() throws Exception { EService eService = new EServiceImpl(); @@ -830,300 +578,4 @@ public class BasicTest extends ContextTest { } - protected ContextCacheRenewal contextCacheRenewal= new ContextCacheRenewal() { - - @Override - public List renew() throws ResourceRegistryException { - ContextManagement contextManagement = new ContextManagement(); - String contextsJsonString = contextManagement.allFromServer(false); - List contexts = null; - try { - contexts = ElementMapper.unmarshalList(contextsJsonString); - } catch (IOException e) { - logger.error("Unable to read context from server", e); - } - return contexts; - } - }; - - @Test - public void testGetInstancesContexts() throws ResourceRegistryException, Exception { - ContextTest.setContextByName(DEFAULT_TEST_SCOPE); - /* Creating HostingNode */ - ContextCache contextCache = ContextCache.getInstance(); - contextCache.setContextCacheRenewal(contextCacheRenewal); - - - ResourceManagement hostingNodeManagement = new ResourceManagement(); - hostingNodeManagement.setElementType(HostingNode.NAME); - hostingNodeManagement.setJson(SmartgearResourcesTest.HOSTING_NODE); - - String hnJson = hostingNodeManagement.create(); - logger.debug("Created : {}", hnJson); - HostingNode hostingNode = ElementMapper.unmarshal(HostingNode.class, hnJson); - logger.debug("Unmarshalled {} {}", HostingNode.NAME, hostingNode); - - /* Creating EService */ - - ResourceManagement eServiceManagement = new ResourceManagement(); - eServiceManagement.setElementType(EService.NAME); - eServiceManagement.setJson(SmartgearResourcesTest.ESERVICE); - - String eservicejson = eServiceManagement.create(); - logger.debug("Created : {}", eservicejson); - EService eService = ElementMapper.unmarshal(EService.class, eservicejson); - logger.debug("Unmarshalled {} {}", EService.NAME, eService); - - - /* Creating Activates Relation */ - - PropagationConstraint propagationConstraint = new PropagationConstraintImpl(); - propagationConstraint - .setRemoveConstraint(RemoveConstraint.cascade); - propagationConstraint - .setAddConstraint(AddConstraint.unpropagate); - - - Activates activates = new ActivatesImpl<>(hostingNode, eService, - propagationConstraint); - - IsRelatedToManagement activatesManagement = new IsRelatedToManagement(); - activatesManagement.setElementType(Activates.NAME); - String activatesJson = ElementMapper.marshal(activates); - activatesManagement.setJson(activatesJson); - String createdActivatesJson = activatesManagement.create(); - logger.debug("Created : {}", createdActivatesJson); - - @SuppressWarnings("unchecked") - Activates createdActivates = ElementMapper.unmarshal(Activates.class, createdActivatesJson); - logger.debug("UnMarshalled {}", createdActivates); - - /* ------------------------------------------------------------------ */ - - - - addToContextThenTestIfBehaveProperly(hostingNode, false, ALTERNATIVE_TEST_SCOPE); - - logger.debug("Switching to alternative scope"); - ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE); - - hostingNodeManagement = new ResourceManagement(); - hostingNodeManagement.setUUID(hostingNode.getHeader().getUUID()); - hostingNodeManagement.setElementType(Utility.getTypeName(hostingNode.getClass())); - - - String hostingNodeContexts = hostingNodeManagement.getContexts(); - logger.debug("Contexts of {} with UUID {} have the following UUID {}", HostingNode.NAME, hostingNodeManagement.getUUID(), hostingNodeContexts); - Set hostingNodeContextsUUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(hostingNodeContexts); - Assert.assertTrue(hostingNodeContextsUUID.size()==2); - Set hostingNodeContextFullNames = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(hostingNodeContextsUUID); - Assert.assertTrue(hostingNodeContextFullNames.size()==2); - logger.debug("Contexts of {} with UUID {} are {}", HostingNode.NAME, hostingNodeManagement.getUUID(), hostingNodeContextFullNames); - - for(Facet facet : hostingNode.getFacets()) { - FacetManagement facetManagement = new FacetManagement(); - facetManagement.setUUID(facet.getHeader().getUUID()); - String typeName = Utility.getTypeName(facet.getClass()); - facetManagement.setElementType(typeName); - String facetContexts = facetManagement.getContexts(); - logger.debug("Contexts of {} with UUID {} have the following UUID {}", typeName, facetManagement.getUUID(), facetContexts); - Set facetContextsUUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(facetContexts); - Assert.assertTrue(hostingNodeContextsUUID.size()==2); - Assert.assertTrue(facetContextsUUID.containsAll(hostingNodeContextsUUID)); - Assert.assertTrue(hostingNodeContextsUUID.containsAll(facetContextsUUID)); - Set facetContextFullNames = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(facetContextsUUID); - Assert.assertTrue(facetContextFullNames.size()==2); - logger.debug("Contexts of {} with UUID {} are {}", typeName, facetManagement.getUUID(), facetContextFullNames); - } - - activatesManagement = new IsRelatedToManagement(); - activatesManagement.setUUID(createdActivates.getHeader().getUUID()); - activatesManagement.setElementType(Utility.getTypeName(createdActivates.getClass())); - String isRelatedToContexts = activatesManagement.getContexts(); - logger.debug("Contexts of {} with UUID {} have the following UUID {}", Activates.NAME, activatesManagement.getUUID(), isRelatedToContexts); - Set activatesContextsUUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(isRelatedToContexts); - Assert.assertTrue(activatesContextsUUID.size()==1); - Assert.assertFalse(activatesContextsUUID.containsAll(hostingNodeContextsUUID)); - Assert.assertTrue(hostingNodeContextsUUID.containsAll(activatesContextsUUID)); - Set activatesContextFullNames = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(activatesContextsUUID); - Assert.assertTrue(activatesContextFullNames.size()==1); - logger.debug("Contexts of {} with UUID {} are {}", Activates.NAME, activatesManagement.getUUID(), activatesContextFullNames); - - - eServiceManagement = new ResourceManagement(); - eServiceManagement.setUUID(eService.getHeader().getUUID()); - eServiceManagement.setElementType(Utility.getTypeName(eService.getClass())); - String eServiceContexts = eServiceManagement.getContexts(); - logger.debug("Contexts of {} with UUID {} have the following UUID {}", EService.NAME, eServiceManagement.getUUID(), eServiceContexts); - Set eServiceContextsUUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(eServiceContexts); - Assert.assertTrue(eServiceContextsUUID.size()==1); - Assert.assertFalse(eServiceContextsUUID.containsAll(hostingNodeContextsUUID)); - Assert.assertTrue(hostingNodeContextsUUID.containsAll(eServiceContextsUUID)); - Set eServiceContextFullNames = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(eServiceContextsUUID); - Assert.assertTrue(eServiceContextFullNames.size()==1); - logger.debug("Contexts of {} with UUID {} are {}", EService.NAME, eServiceManagement.getUUID(), eServiceContextFullNames); - - boolean deleted = hostingNodeManagement.delete(); - Assert.assertTrue(deleted); - } - - - @Test - public void testGetContextsInInstances() throws ResourceRegistryException, Exception { - ContextTest.setContextByName(DEFAULT_TEST_SCOPE); - /* Creating HostingNode */ - ContextCache contextCache = ContextCache.getInstance(); - contextCache.setContextCacheRenewal(contextCacheRenewal); - - - ResourceManagement hostingNodeManagement = new ResourceManagement(); - hostingNodeManagement.setElementType(HostingNode.NAME); - hostingNodeManagement.setJson(SmartgearResourcesTest.HOSTING_NODE); - - String hnJson = hostingNodeManagement.create(); - logger.debug("Created : {}", hnJson); - HostingNode hostingNode = ElementMapper.unmarshal(HostingNode.class, hnJson); - logger.debug("Unmarshalled {} {}", HostingNode.NAME, hostingNode); - - /* Creating EService */ - - ResourceManagement eServiceManagement = new ResourceManagement(); - eServiceManagement.setElementType(EService.NAME); - eServiceManagement.setJson(SmartgearResourcesTest.ESERVICE); - - String eservicejson = eServiceManagement.create(); - logger.debug("Created : {}", eservicejson); - EService eService = ElementMapper.unmarshal(EService.class, eservicejson); - logger.debug("Unmarshalled {} {}", EService.NAME, eService); - - - /* Creating Activates Relation */ - - PropagationConstraint propagationConstraint = new PropagationConstraintImpl(); - propagationConstraint - .setRemoveConstraint(RemoveConstraint.cascade); - propagationConstraint - .setAddConstraint(AddConstraint.unpropagate); - - - Activates activates = new ActivatesImpl<>(hostingNode, eService, - propagationConstraint); - - IsRelatedToManagement activatesManagement = new IsRelatedToManagement(); - activatesManagement.setElementType(Activates.NAME); - String activatesJson = ElementMapper.marshal(activates); - activatesManagement.setJson(activatesJson); - String createdActivatesJson = activatesManagement.create(); - logger.debug("Created : {}", createdActivatesJson); - - @SuppressWarnings("unchecked") - Activates createdActivates = ElementMapper.unmarshal(Activates.class, createdActivatesJson); - logger.debug("UnMarshalled {}", createdActivates); - - /* ------------------------------------------------------------------ */ - - logger.debug("Switching to alternative scope"); - ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE); - - hostingNodeManagement = new ResourceManagement(); - hostingNodeManagement.setUUID(hostingNode.getHeader().getUUID()); - hostingNodeManagement.setElementType(Utility.getTypeName(hostingNode.getClass())); - hostingNodeManagement.addToContext(ContextUtility.getCurrentSecurityContext().getUUID()); - - ContextUtility.getIncludeInstanceContexts().set(true); - - String hostingNodeJson = hostingNodeManagement.read().toString(); - HostingNode hostingNodeRead = ElementMapper.unmarshal(HostingNode.class, hostingNodeJson); - logger.debug("Unmarshalled {} {}", HostingNode.NAME, hostingNodeRead); - - - @SuppressWarnings("unchecked") - Collection contextsCollection = (Collection) hostingNodeRead.getHeader().getAdditionalProperty(Header.__CONTEXTS); - Assert.assertTrue(contextsCollection.size()==2); - Set hostingNodeContextsUUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(contextsCollection); - Assert.assertTrue(hostingNodeContextsUUID.size()==2); - - - Set hostingNodeContextFullNames = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(hostingNodeContextsUUID); - Assert.assertTrue(hostingNodeContextFullNames.size()==2); - logger.debug("Contexts of {} with UUID {} are {}", HostingNode.NAME, hostingNodeManagement.getUUID(), hostingNodeContextFullNames); - - for(Facet facet : hostingNode.getFacets()) { - FacetManagement facetManagement = new FacetManagement(); - facetManagement.setUUID(facet.getHeader().getUUID()); - String typeName = Utility.getTypeName(facet.getClass()); - facetManagement.setElementType(typeName); - - String facetJson = facetManagement.read().toString(); - Facet facetRead = ElementMapper.unmarshal(Facet.class, facetJson); - logger.debug("Unmarshalled {} {}", Utility.getTypeName(facetRead.getClass()), facetRead); - @SuppressWarnings("unchecked") - Collection facetContextsCollection = (Collection) facetRead.getHeader().getAdditionalProperty(Header.__CONTEXTS); - Assert.assertTrue(contextsCollection.size()==2); - - Set facetContextsUUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(facetContextsCollection); - Assert.assertTrue(hostingNodeContextsUUID.size()==2); - Assert.assertTrue(facetContextsUUID.containsAll(hostingNodeContextsUUID)); - Assert.assertTrue(hostingNodeContextsUUID.containsAll(facetContextsUUID)); - Set facetContextFullNames = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(facetContextsUUID); - Assert.assertTrue(facetContextFullNames.size()==2); - logger.debug("Contexts of {} with UUID {} are {}", typeName, facetManagement.getUUID(), facetContextFullNames); - } - - activatesManagement = new IsRelatedToManagement(); - activatesManagement.setUUID(createdActivates.getHeader().getUUID()); - activatesManagement.setElementType(Utility.getTypeName(createdActivates.getClass())); - - try { - activatesManagement.read(); - String error = String.format("It should not be possible to read %s in %s. A %s should be raised instead", Activates.NAME, ALTERNATIVE_TEST_SCOPE, AvailableInAnotherContextException.class.getSimpleName()); - throw new Exception(error); - }catch (AvailableInAnotherContextException e) { - // This is what we expect - } - - // Restoring DEFAULT TEST SCOPE otherwise is not possible to read activates relation - // which is not available in ALTERNATIVE_TEST_SCOPE - ContextTest.setContextByName(DEFAULT_TEST_SCOPE); - activatesManagement = new IsRelatedToManagement(); - activatesManagement.setUUID(createdActivates.getHeader().getUUID()); - activatesManagement.setElementType(Utility.getTypeName(createdActivates.getClass())); - // reading again - String activatesReadJson = activatesManagement.read().toString(); - - @SuppressWarnings("unchecked") - Activates activatesRead = ElementMapper.unmarshal(Activates.class, activatesReadJson); - logger.debug("Unmarshalled {} {}", Utility.getTypeName(activatesRead.getClass()), activatesRead); - @SuppressWarnings("unchecked") - Collection activatesContextsCollection = (Collection) activatesRead.getHeader().getAdditionalProperty(Header.__CONTEXTS); - logger.debug("Contexts of {} with UUID {} have the following UUID {}", Activates.NAME, activatesManagement.getUUID(), activatesContextsCollection); - Set activatesContextsUUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(activatesContextsCollection); - Assert.assertTrue(activatesContextsUUID.size()==1); - Assert.assertFalse(activatesContextsUUID.containsAll(hostingNodeContextsUUID)); - Assert.assertTrue(hostingNodeContextsUUID.containsAll(activatesContextsUUID)); - Set activatesContextFullNames = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(activatesContextsUUID); - Assert.assertTrue(activatesContextFullNames.size()==1); - logger.debug("Contexts of {} with UUID {} are {}", Activates.NAME, activatesManagement.getUUID(), activatesContextFullNames); - - eServiceManagement = new ResourceManagement(); - eServiceManagement.setUUID(eService.getHeader().getUUID()); - eServiceManagement.setElementType(Utility.getTypeName(eService.getClass())); - String eServiceReadJson = eServiceManagement.read().toString(); - EService eServiceRead = ElementMapper.unmarshal(EService.class, eServiceReadJson); - logger.debug("Unmarshalled {} {}", Utility.getTypeName(eServiceRead.getClass()), eServiceRead); - @SuppressWarnings("unchecked") - Collection eServiceContextsCollection = (Collection) eServiceRead.getHeader().getAdditionalProperty(Header.__CONTEXTS); - logger.debug("Contexts of {} with UUID {} have the following UUID {}", EService.NAME, eServiceManagement.getUUID(), eServiceContextsCollection); - Set eServiceContextsUUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(eServiceContextsCollection); - Assert.assertTrue(eServiceContextsUUID.size()==1); - Assert.assertFalse(eServiceContextsUUID.containsAll(hostingNodeContextsUUID)); - Assert.assertTrue(hostingNodeContextsUUID.containsAll(eServiceContextsUUID)); - Set eServiceContextFullNames = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(eServiceContextsUUID); - Assert.assertTrue(eServiceContextFullNames.size()==1); - logger.debug("Contexts of {} with UUID {} are {}", EService.NAME, eServiceManagement.getUUID(), eServiceContextFullNames); - - boolean deleted = hostingNodeManagement.delete(); - Assert.assertTrue(deleted); - } - } diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/ComplexTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/ComplexTest.java new file mode 100644 index 0000000..5f14e41 --- /dev/null +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/ComplexTest.java @@ -0,0 +1,152 @@ +package org.gcube.informationsystem.resourceregistry.instances.multicontext; + +import java.util.Set; +import java.util.UUID; + +import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl; +import org.gcube.informationsystem.model.reference.entities.Facet; +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.resourceregistry.ContextTest; +import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.api.utils.Utility; +import org.gcube.informationsystem.resourceregistry.instances.SmartgearResourcesTest; +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.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; +import org.junit.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ComplexTest extends MultiContextTest { + + private static Logger logger = LoggerFactory.getLogger(ComplexTest.class); + + @Test + public void testGetInstancesContexts() throws ResourceRegistryException, Exception { + ContextTest.setContextByName(DEFAULT_TEST_SCOPE); + /* Creating HostingNode */ + ContextCache contextCache = ContextCache.getInstance(); + contextCache.setContextCacheRenewal(contextCacheRenewal); + + + ResourceManagement hostingNodeManagement = new ResourceManagement(); + hostingNodeManagement.setElementType(HostingNode.NAME); + hostingNodeManagement.setJson(SmartgearResourcesTest.HOSTING_NODE); + + String hnJson = hostingNodeManagement.create(); + logger.debug("Created : {}", hnJson); + HostingNode hostingNode = ElementMapper.unmarshal(HostingNode.class, hnJson); + logger.debug("Unmarshalled {} {}", HostingNode.NAME, hostingNode); + + /* Creating EService */ + + ResourceManagement eServiceManagement = new ResourceManagement(); + eServiceManagement.setElementType(EService.NAME); + eServiceManagement.setJson(SmartgearResourcesTest.ESERVICE); + + String eservicejson = eServiceManagement.create(); + logger.debug("Created : {}", eservicejson); + EService eService = ElementMapper.unmarshal(EService.class, eservicejson); + logger.debug("Unmarshalled {} {}", EService.NAME, eService); + + + /* Creating Activates Relation */ + + PropagationConstraint propagationConstraint = new PropagationConstraintImpl(); + propagationConstraint + .setRemoveConstraint(RemoveConstraint.cascade); + propagationConstraint + .setAddConstraint(AddConstraint.unpropagate); + + + Activates activates = new ActivatesImpl<>(hostingNode, eService, + propagationConstraint); + + IsRelatedToManagement activatesManagement = new IsRelatedToManagement(); + activatesManagement.setElementType(Activates.NAME); + String activatesJson = ElementMapper.marshal(activates); + activatesManagement.setJson(activatesJson); + String createdActivatesJson = activatesManagement.create(); + logger.debug("Created : {}", createdActivatesJson); + + @SuppressWarnings("unchecked") + Activates createdActivates = ElementMapper.unmarshal(Activates.class, createdActivatesJson); + logger.debug("UnMarshalled {}", createdActivates); + + /* ------------------------------------------------------------------ */ + + addToContextThenTestIfBehaveProperly(hostingNode, false, ALTERNATIVE_TEST_SCOPE); + + logger.debug("Switching to alternative scope"); + ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE); + + hostingNodeManagement = new ResourceManagement(); + hostingNodeManagement.setUUID(hostingNode.getHeader().getUUID()); + hostingNodeManagement.setElementType(Utility.getTypeName(hostingNode.getClass())); + + + String hostingNodeContexts = hostingNodeManagement.getContexts(); + logger.debug("Contexts of {} with UUID {} have the following UUID {}", HostingNode.NAME, hostingNodeManagement.getUUID(), hostingNodeContexts); + Set hostingNodeContextsUUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(hostingNodeContexts); + Assert.assertTrue(hostingNodeContextsUUID.size()==2); + Set hostingNodeContextFullNames = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(hostingNodeContextsUUID); + Assert.assertTrue(hostingNodeContextFullNames.size()==2); + logger.debug("Contexts of {} with UUID {} are {}", HostingNode.NAME, hostingNodeManagement.getUUID(), hostingNodeContextFullNames); + + for(Facet facet : hostingNode.getFacets()) { + FacetManagement facetManagement = new FacetManagement(); + facetManagement.setUUID(facet.getHeader().getUUID()); + String typeName = Utility.getTypeName(facet.getClass()); + facetManagement.setElementType(typeName); + String facetContexts = facetManagement.getContexts(); + logger.debug("Contexts of {} with UUID {} have the following UUID {}", typeName, facetManagement.getUUID(), facetContexts); + Set facetContextsUUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(facetContexts); + Assert.assertTrue(hostingNodeContextsUUID.size()==2); + Assert.assertTrue(facetContextsUUID.containsAll(hostingNodeContextsUUID)); + Assert.assertTrue(hostingNodeContextsUUID.containsAll(facetContextsUUID)); + Set facetContextFullNames = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(facetContextsUUID); + Assert.assertTrue(facetContextFullNames.size()==2); + logger.debug("Contexts of {} with UUID {} are {}", typeName, facetManagement.getUUID(), facetContextFullNames); + } + + activatesManagement = new IsRelatedToManagement(); + activatesManagement.setUUID(createdActivates.getHeader().getUUID()); + activatesManagement.setElementType(Utility.getTypeName(createdActivates.getClass())); + String isRelatedToContexts = activatesManagement.getContexts(); + logger.debug("Contexts of {} with UUID {} have the following UUID {}", Activates.NAME, activatesManagement.getUUID(), isRelatedToContexts); + Set activatesContextsUUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(isRelatedToContexts); + Assert.assertTrue(activatesContextsUUID.size()==1); + Assert.assertFalse(activatesContextsUUID.containsAll(hostingNodeContextsUUID)); + Assert.assertTrue(hostingNodeContextsUUID.containsAll(activatesContextsUUID)); + Set activatesContextFullNames = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(activatesContextsUUID); + Assert.assertTrue(activatesContextFullNames.size()==1); + logger.debug("Contexts of {} with UUID {} are {}", Activates.NAME, activatesManagement.getUUID(), activatesContextFullNames); + + + eServiceManagement = new ResourceManagement(); + eServiceManagement.setUUID(eService.getHeader().getUUID()); + eServiceManagement.setElementType(Utility.getTypeName(eService.getClass())); + String eServiceContexts = eServiceManagement.getContexts(); + logger.debug("Contexts of {} with UUID {} have the following UUID {}", EService.NAME, eServiceManagement.getUUID(), eServiceContexts); + Set eServiceContextsUUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(eServiceContexts); + Assert.assertTrue(eServiceContextsUUID.size()==1); + Assert.assertFalse(eServiceContextsUUID.containsAll(hostingNodeContextsUUID)); + Assert.assertTrue(hostingNodeContextsUUID.containsAll(eServiceContextsUUID)); + Set eServiceContextFullNames = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(eServiceContextsUUID); + Assert.assertTrue(eServiceContextFullNames.size()==1); + logger.debug("Contexts of {} with UUID {} are {}", EService.NAME, eServiceManagement.getUUID(), eServiceContextFullNames); + + boolean deleted = hostingNodeManagement.delete(); + Assert.assertTrue(deleted); + } + +} diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/EnhancedTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/EnhancedTest.java new file mode 100644 index 0000000..b702f3e --- /dev/null +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/EnhancedTest.java @@ -0,0 +1,270 @@ +/** + * + */ +package org.gcube.informationsystem.resourceregistry.instances.multicontext; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.UUID; + +import org.gcube.com.fasterxml.jackson.databind.JsonNode; +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.resourceregistry.ContextTest; +import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; +import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.api.utils.Utility; +import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; +import org.gcube.informationsystem.resourceregistry.instances.SmartgearResourcesTest; +import org.gcube.informationsystem.resourceregistry.instances.model.ERManagementUtility; +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.utils.ElementMapper; +import org.gcube.resourcemanagement.model.impl.entities.facets.SoftwareFacetImpl; +import org.gcube.resourcemanagement.model.impl.entities.resources.EServiceImpl; +import org.gcube.resourcemanagement.model.impl.relations.consistsof.IsIdentifiedByImpl; +import org.gcube.resourcemanagement.model.impl.relations.isrelatedto.ActivatesImpl; +import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet; +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.consistsof.IsIdentifiedBy; +import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Activates; +import org.junit.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class EnhancedTest extends MultiContextTest { + + private static Logger logger = LoggerFactory + .getLogger(EnhancedTest.class); + + @Test + public void testNoFollows() throws Exception { + ContextTest.setContextByName(DEFAULT_TEST_SCOPE); + EService eService = new EServiceImpl(); + + SoftwareFacet softwareFacet = new SoftwareFacetImpl(); + softwareFacet.setGroup("InformationSystem"); + softwareFacet.setName("resource-registry"); + softwareFacet.setVersion("1.1.0"); + PropagationConstraint propagationConstraint = new PropagationConstraintImpl(); + propagationConstraint.setAddConstraint(AddConstraint.propagate); + propagationConstraint.setRemoveConstraint(RemoveConstraint.cascade); + + IsIdentifiedBy isIdentifiedBy = new IsIdentifiedByImpl( + eService, softwareFacet, null); + eService.addFacet(isIdentifiedBy); + + 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); + + JsonNode jsonNode = ElementMapper.getObjectMapper().readTree(json); + + Map expectedInstances = new HashMap<>(); + expectedInstances.put(eService.getHeader().getUUID(), jsonNode); + + UUID contextUUID = ContextUtility.getInstance().getSecurityContextByFullName(ALTERNATIVE_TEST_SCOPE).getUUID(); + + try { + ERManagementUtility.addToContextNoPropagationConstraint(expectedInstances, contextUUID, true); + }catch (ResourceRegistryException e) { + logger.debug("As expected {}", e.getMessage()); + } + + @SuppressWarnings("unchecked") + IsIdentifiedBy gotIsIdentifiedBy = (IsIdentifiedBy) eService.getConsistsOf(IsIdentifiedBy.class).get(0); + + JsonNode gotIsIdentifiedByJsonNode = ElementMapper.getObjectMapper().readTree(ElementMapper.marshal(gotIsIdentifiedBy)); + expectedInstances.put(gotIsIdentifiedBy.getHeader().getUUID(), gotIsIdentifiedByJsonNode); + + try { + ERManagementUtility.addToContextNoPropagationConstraint(expectedInstances, contextUUID, true); + }catch (ResourceRegistryException e) { + // as expected + logger.debug("As expected {}", e.getMessage()); + }finally { + resourceManagement.delete(); + } + + } + + @Test + public void testGetContextsInInstances() throws ResourceRegistryException, Exception { + ContextTest.setContextByName(DEFAULT_TEST_SCOPE); + /* Creating HostingNode */ + ContextCache contextCache = ContextCache.getInstance(); + contextCache.setContextCacheRenewal(contextCacheRenewal); + + + ResourceManagement hostingNodeManagement = new ResourceManagement(); + hostingNodeManagement.setElementType(HostingNode.NAME); + hostingNodeManagement.setJson(SmartgearResourcesTest.HOSTING_NODE); + + String hnJson = hostingNodeManagement.create(); + logger.debug("Created : {}", hnJson); + HostingNode hostingNode = ElementMapper.unmarshal(HostingNode.class, hnJson); + logger.debug("Unmarshalled {} {}", HostingNode.NAME, hostingNode); + + /* Creating EService */ + + ResourceManagement eServiceManagement = new ResourceManagement(); + eServiceManagement.setElementType(EService.NAME); + eServiceManagement.setJson(SmartgearResourcesTest.ESERVICE); + + String eservicejson = eServiceManagement.create(); + logger.debug("Created : {}", eservicejson); + EService eService = ElementMapper.unmarshal(EService.class, eservicejson); + logger.debug("Unmarshalled {} {}", EService.NAME, eService); + + + /* Creating Activates Relation */ + + PropagationConstraint propagationConstraint = new PropagationConstraintImpl(); + propagationConstraint + .setRemoveConstraint(RemoveConstraint.cascade); + propagationConstraint + .setAddConstraint(AddConstraint.unpropagate); + + + Activates activates = new ActivatesImpl<>(hostingNode, eService, + propagationConstraint); + + IsRelatedToManagement activatesManagement = new IsRelatedToManagement(); + activatesManagement.setElementType(Activates.NAME); + String activatesJson = ElementMapper.marshal(activates); + activatesManagement.setJson(activatesJson); + String createdActivatesJson = activatesManagement.create(); + logger.debug("Created : {}", createdActivatesJson); + + @SuppressWarnings("unchecked") + Activates createdActivates = ElementMapper.unmarshal(Activates.class, createdActivatesJson); + logger.debug("UnMarshalled {}", createdActivates); + + /* ------------------------------------------------------------------ */ + + logger.debug("Switching to alternative scope"); + ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE); + + hostingNodeManagement = new ResourceManagement(); + hostingNodeManagement.setUUID(hostingNode.getHeader().getUUID()); + hostingNodeManagement.setElementType(Utility.getTypeName(hostingNode.getClass())); + hostingNodeManagement.addToContext(ContextUtility.getCurrentSecurityContext().getUUID()); + + ContextUtility.getIncludeInstanceContexts().set(true); + + hostingNodeManagement = new ResourceManagement(); + hostingNodeManagement.setUUID(hostingNode.getHeader().getUUID()); + hostingNodeManagement.setElementType(Utility.getTypeName(hostingNode.getClass())); + String hostingNodeJson = hostingNodeManagement.read().toString(); + HostingNode hostingNodeRead = ElementMapper.unmarshal(HostingNode.class, hostingNodeJson); + logger.debug("Unmarshalled {} {}", HostingNode.NAME, hostingNodeRead); + + + @SuppressWarnings("unchecked") + Collection contextsCollection = (Collection) hostingNodeRead.getHeader().getAdditionalProperty(Header.__CONTEXTS); + Assert.assertTrue(contextsCollection.size()==2); + Set hostingNodeContextsUUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(contextsCollection); + Assert.assertTrue(hostingNodeContextsUUID.size()==2); + + + Set hostingNodeContextFullNames = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(hostingNodeContextsUUID); + Assert.assertTrue(hostingNodeContextFullNames.size()==2); + logger.debug("Contexts of {} with UUID {} are {}", HostingNode.NAME, hostingNodeManagement.getUUID(), hostingNodeContextFullNames); + + for(Facet facet : hostingNode.getFacets()) { + FacetManagement facetManagement = new FacetManagement(); + facetManagement.setUUID(facet.getHeader().getUUID()); + String typeName = Utility.getTypeName(facet.getClass()); + facetManagement.setElementType(typeName); + + String facetJson = facetManagement.read().toString(); + Facet facetRead = ElementMapper.unmarshal(Facet.class, facetJson); + logger.debug("Unmarshalled {} {}", Utility.getTypeName(facetRead.getClass()), facetRead); + @SuppressWarnings("unchecked") + Collection facetContextsCollection = (Collection) facetRead.getHeader().getAdditionalProperty(Header.__CONTEXTS); + Assert.assertTrue(contextsCollection.size()==2); + + Set facetContextsUUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(facetContextsCollection); + Assert.assertTrue(hostingNodeContextsUUID.size()==2); + Assert.assertTrue(facetContextsUUID.containsAll(hostingNodeContextsUUID)); + Assert.assertTrue(hostingNodeContextsUUID.containsAll(facetContextsUUID)); + Set facetContextFullNames = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(facetContextsUUID); + Assert.assertTrue(facetContextFullNames.size()==2); + logger.debug("Contexts of {} with UUID {} are {}", typeName, facetManagement.getUUID(), facetContextFullNames); + } + + activatesManagement = new IsRelatedToManagement(); + activatesManagement.setUUID(createdActivates.getHeader().getUUID()); + activatesManagement.setElementType(Utility.getTypeName(createdActivates.getClass())); + + try { + activatesManagement.read(); + String error = String.format("It should not be possible to read %s in %s. A %s should be raised instead", Activates.NAME, ALTERNATIVE_TEST_SCOPE, AvailableInAnotherContextException.class.getSimpleName()); + throw new Exception(error); + }catch (AvailableInAnotherContextException e) { + // This is what we expect + } + + // Restoring DEFAULT TEST SCOPE otherwise is not possible to read activates relation + // which is not available in ALTERNATIVE_TEST_SCOPE + ContextTest.setContextByName(DEFAULT_TEST_SCOPE); + activatesManagement = new IsRelatedToManagement(); + activatesManagement.setUUID(createdActivates.getHeader().getUUID()); + activatesManagement.setElementType(Utility.getTypeName(createdActivates.getClass())); + // reading again + String activatesReadJson = activatesManagement.read().toString(); + + @SuppressWarnings("unchecked") + Activates activatesRead = ElementMapper.unmarshal(Activates.class, activatesReadJson); + logger.debug("Unmarshalled {} {}", Utility.getTypeName(activatesRead.getClass()), activatesRead); + @SuppressWarnings("unchecked") + Collection activatesContextsCollection = (Collection) activatesRead.getHeader().getAdditionalProperty(Header.__CONTEXTS); + logger.debug("Contexts of {} with UUID {} have the following UUID {}", Activates.NAME, activatesManagement.getUUID(), activatesContextsCollection); + Set activatesContextsUUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(activatesContextsCollection); + Assert.assertTrue(activatesContextsUUID.size()==1); + Assert.assertFalse(activatesContextsUUID.containsAll(hostingNodeContextsUUID)); + Assert.assertTrue(hostingNodeContextsUUID.containsAll(activatesContextsUUID)); + Set activatesContextFullNames = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(activatesContextsUUID); + Assert.assertTrue(activatesContextFullNames.size()==1); + logger.debug("Contexts of {} with UUID {} are {}", Activates.NAME, activatesManagement.getUUID(), activatesContextFullNames); + + eServiceManagement = new ResourceManagement(); + eServiceManagement.setUUID(eService.getHeader().getUUID()); + eServiceManagement.setElementType(Utility.getTypeName(eService.getClass())); + String eServiceReadJson = eServiceManagement.read().toString(); + EService eServiceRead = ElementMapper.unmarshal(EService.class, eServiceReadJson); + logger.debug("Unmarshalled {} {}", Utility.getTypeName(eServiceRead.getClass()), eServiceRead); + @SuppressWarnings("unchecked") + Collection eServiceContextsCollection = (Collection) eServiceRead.getHeader().getAdditionalProperty(Header.__CONTEXTS); + logger.debug("Contexts of {} with UUID {} have the following UUID {}", EService.NAME, eServiceManagement.getUUID(), eServiceContextsCollection); + Set eServiceContextsUUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(eServiceContextsCollection); + Assert.assertTrue(eServiceContextsUUID.size()==1); + Assert.assertFalse(eServiceContextsUUID.containsAll(hostingNodeContextsUUID)); + Assert.assertTrue(hostingNodeContextsUUID.containsAll(eServiceContextsUUID)); + Set eServiceContextFullNames = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(eServiceContextsUUID); + Assert.assertTrue(eServiceContextFullNames.size()==1); + logger.debug("Contexts of {} with UUID {} are {}", EService.NAME, eServiceManagement.getUUID(), eServiceContextFullNames); + + boolean deleted = hostingNodeManagement.delete(); + Assert.assertTrue(deleted); + } + +} 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 new file mode 100644 index 0000000..6e27e78 --- /dev/null +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/MultiContextTest.java @@ -0,0 +1,235 @@ +package org.gcube.informationsystem.resourceregistry.instances.multicontext; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.SortedSet; +import java.util.TreeSet; +import java.util.UUID; + +import org.gcube.com.fasterxml.jackson.databind.JsonNode; +import org.gcube.informationsystem.base.reference.Element; +import org.gcube.informationsystem.context.reference.entities.Context; +import org.gcube.informationsystem.model.reference.entities.Entity; +import org.gcube.informationsystem.model.reference.entities.Facet; +import org.gcube.informationsystem.model.reference.entities.Resource; +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.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.contexts.ContextUtility; +import org.gcube.informationsystem.resourceregistry.contexts.entities.ContextManagement; +import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement; +import org.gcube.informationsystem.types.TypeMapper; +import org.gcube.informationsystem.utils.ElementMapper; +import org.gcube.resourcemanagement.model.reference.entities.resources.EService; +import org.junit.Assert; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.orientechnologies.orient.core.record.ODirection; + +public class MultiContextTest extends ContextTest { + + private static Logger logger = LoggerFactory.getLogger(MultiContextTest.class); + + private Map getRemovedExpectedInstances(Resource r) { + Map expected = new HashMap<>(); + expected.put(r.getHeader().getUUID(), r); + // TODO query + + for (IsRelatedTo isRelatedTo : r.getIsRelatedTo()) { + // TODO + if (isRelatedTo.getPropagationConstraint().getRemoveConstraint() == RemoveConstraint.cascade) { + expected.put(isRelatedTo.getHeader().getUUID(), isRelatedTo); + expected.putAll(getRemovedExpectedInstances(isRelatedTo.getTarget())); + + // TODO + } + + if (isRelatedTo.getPropagationConstraint().getRemoveConstraint() == RemoveConstraint.cascadeWhenOrphan) { + expected.put(isRelatedTo.getHeader().getUUID(), isRelatedTo); + expected.putAll(getRemovedExpectedInstances(isRelatedTo.getTarget())); + + // TODO + + // TODO + } + } + for (ConsistsOf consistsOf : r.getConsistsOf()) { + if (consistsOf.getPropagationConstraint().getRemoveConstraint() == RemoveConstraint.cascade || consistsOf + .getPropagationConstraint().getRemoveConstraint() == RemoveConstraint.cascadeWhenOrphan) { + expected.put(consistsOf.getHeader().getUUID(), consistsOf); + Facet facet = consistsOf.getTarget(); + expected.put(facet.getHeader().getUUID(), facet); + } + } + return expected; + } + + @SuppressWarnings("unused") + private void removeFromContextThenTestIfBehaveProperly(Resource r, boolean dryRun) + throws ResourceRegistryException { + Map expectedInstances = getRemovedExpectedInstances(r); + + ResourceManagement resourceManagement = new ResourceManagement(); + resourceManagement.setElementType(TypeMapper.getType(r.getClass())); + resourceManagement.setUUID(r.getHeader().getUUID()); + resourceManagement.setDryRunContextSharing(dryRun); + UUID contextUUID = ContextUtility.getCurrentSecurityContext().getUUID(); + Map affectedInstances = resourceManagement.removeFromContext(contextUUID); + + SortedSet expectedInstancesUUID = new TreeSet<>(expectedInstances.keySet()); + SortedSet affectedInstancesUUID = new TreeSet<>(affectedInstances.keySet()); + + logger.trace("Expected Instances are {} : {}", expectedInstancesUUID.size(), expectedInstancesUUID); + logger.trace("Affected Instances are {} : {}", affectedInstancesUUID.size(), affectedInstancesUUID); + + Assert.assertTrue(expectedInstancesUUID.size() == affectedInstancesUUID.size()); + + Assert.assertTrue(expectedInstancesUUID.containsAll(affectedInstancesUUID)); + Assert.assertTrue(affectedInstancesUUID.containsAll(expectedInstancesUUID)); + + for (UUID uuid : affectedInstances.keySet()) { + + Element element = expectedInstances.get(uuid); + String expectedType = TypeMapper.getType(element.getClass()); + + JsonNode affectedJsonNode = affectedInstances.get(uuid); + String affectedType = affectedJsonNode.get(Element.CLASS_PROPERTY).asText(); + + Assert.assertTrue(affectedType.compareTo(expectedType) == 0); + + } + + } + + private interface RelationConstraint { + + public boolean mustBeAdded(Relation r); + + public boolean mustBeRemoved(Relation r); + + } + + protected Resource getAndAddIsRelatedTo(Resource r, RelationConstraint relationConstraint) + throws ResourceRegistryException, Exception { + ResourceManagement resourceManagement = new ResourceManagement(); + resourceManagement.setElementType(Resource.NAME); + + String resourceType = TypeMapper.getType(r); + UUID resourceUUID = r.getHeader().getUUID(); + + // resourceManagement.setUUID(resourceUUID); + String ret = resourceManagement.query(IsRelatedTo.NAME, resourceType, resourceUUID, ODirection.IN, true, + new HashMap<>(), true); + @SuppressWarnings("rawtypes") + List isRelatedToList = ElementMapper.unmarshalList(IsRelatedTo.class, ret); + for (@SuppressWarnings("rawtypes") + IsRelatedTo isRelatedTo : isRelatedToList) { + @SuppressWarnings("unchecked") + boolean mustBeAdded = relationConstraint.mustBeAdded(isRelatedTo); + if (mustBeAdded) { + @SuppressWarnings("unchecked") + IsRelatedTo irt = (IsRelatedTo) isRelatedTo; + r.attachResource(irt); + } + } + return r; + } + + protected Map getAddedExpectedInstances(Resource r) throws ResourceRegistryException, Exception { + Map expected = new HashMap<>(); + expected.put(r.getHeader().getUUID(), r); + + RelationConstraint propagateConstraint = new RelationConstraint() { + + @Override + public boolean mustBeAdded(Relation r) { + return r.getPropagationConstraint().getAddConstraint() == AddConstraint.propagate; + } + + @Override + public boolean mustBeRemoved(Relation r) { + throw new UnsupportedOperationException(); + } + + }; + + r = getAndAddIsRelatedTo(r, propagateConstraint); + + for (IsRelatedTo isRelatedTo : r.getIsRelatedTo()) { + if (isRelatedTo.getPropagationConstraint().getAddConstraint() == AddConstraint.propagate) { + expected.put(isRelatedTo.getHeader().getUUID(), isRelatedTo); + expected.putAll(getAddedExpectedInstances(isRelatedTo.getTarget())); + } + } + for (ConsistsOf consistsOf : r.getConsistsOf()) { + if (consistsOf.getPropagationConstraint().getAddConstraint() == AddConstraint.propagate) { + expected.put(consistsOf.getHeader().getUUID(), consistsOf); + Facet facet = consistsOf.getTarget(); + expected.put(facet.getHeader().getUUID(), facet); + } + } + return expected; + } + + protected void addToContextThenTestIfBehaveProperly(Resource r, boolean dryRun, String targetContextFullName) + throws ResourceRegistryException, Exception { + + Map expectedInstances = getAddedExpectedInstances(r); + + ResourceManagement resourceManagement = new ResourceManagement(); + resourceManagement.setElementType(TypeMapper.getType(r)); + resourceManagement.setUUID(r.getHeader().getUUID()); + resourceManagement.setDryRunContextSharing(dryRun); + UUID contextUUID = ContextUtility.getInstance().getSecurityContextByFullName(targetContextFullName).getUUID(); + Map affectedInstances = resourceManagement.addToContext(contextUUID); + + SortedSet expectedInstancesUUID = new TreeSet<>(expectedInstances.keySet()); + SortedSet affectedInstancesUUID = new TreeSet<>(affectedInstances.keySet()); + + logger.trace("Expected Instances are {} : {}", expectedInstancesUUID.size(), expectedInstancesUUID); + logger.trace("Affected Instances are {} : {}", affectedInstancesUUID.size(), affectedInstancesUUID); + + Assert.assertTrue(expectedInstancesUUID.size() == affectedInstancesUUID.size()); + + Assert.assertTrue(expectedInstancesUUID.containsAll(affectedInstancesUUID)); + Assert.assertTrue(affectedInstancesUUID.containsAll(expectedInstancesUUID)); + + for (UUID uuid : affectedInstances.keySet()) { + + Element element = expectedInstances.get(uuid); + String expectedType = TypeMapper.getType(element.getClass()); + + JsonNode affectedJsonNode = affectedInstances.get(uuid); + String affectedType = affectedJsonNode.get(Element.CLASS_PROPERTY).asText(); + + Assert.assertTrue(affectedType.compareTo(expectedType) == 0); + + } + + } + + protected ContextCacheRenewal contextCacheRenewal = new ContextCacheRenewal() { + + @Override + public List renew() throws ResourceRegistryException { + ContextManagement contextManagement = new ContextManagement(); + String contextsJsonString = contextManagement.allFromServer(false); + List contexts = null; + try { + contexts = ElementMapper.unmarshalList(contextsJsonString); + } catch (IOException e) { + logger.error("Unable to read context from server", e); + } + return contexts; + } + }; + +} diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/types/SchemaManagementImplTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/types/SchemaManagementImplTest.java index 34c2f2b..02fea39 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/types/SchemaManagementImplTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/types/SchemaManagementImplTest.java @@ -296,6 +296,22 @@ public class SchemaManagementImplTest extends ContextTest { Assert.assertTrue(typeManaged.getProperties().containsAll(type.getProperties())); } + @Test + public void deleteTestFacetType() throws Exception { + SchemaManagement schemaManagement = new SchemaManagement(); + String typeName = TestFacet.NAME; + schemaManagement.setTypeName(typeName); + try { + logger.info("Going to delete {}", typeName); + schemaManagement.delete(AccessType.FACET); + logger.info("Deleted {}", typeName); + } catch (SchemaNotFoundException e) { + // The test has been failed before creating the type + } catch (Exception e) { + throw e; + } + } + @Test public void createUpdateDeleteFacetType() throws Exception {