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.ERManagementTest; 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(DEVNEXT); ContextCache contextCache = ContextCache.getInstance(); contextCache.setContextCacheRenewal(contextCacheRenewal); /* Creating HostingNode */ HostingNode hostingNode = ERManagementTest.createHostingNode(); /* Creating EService */ EService eService = ERManagementTest.createEService(); /* 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, NEXTNEXT); logger.debug("Switching to alternative scope"); ContextTest.setContextByName(NEXTNEXT); ResourceManagement hostingNodeManagement = ERManagementTest.getResourceManagement(hostingNode); 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); ResourceManagement eServiceManagement = ERManagementTest.getResourceManagement(eService); 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); ERManagementTest.deleteResource(hostingNode); try { ContextTest.setContextByName(DEVNEXT); ERManagementTest.deleteResource(eService); } catch (Exception e) { logger.error("",e); } } }