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 8e41fd5..40a2c95 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 @@ -28,6 +28,7 @@ import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.base.reference.Element; import org.gcube.informationsystem.base.reference.IdentifiableElement; import org.gcube.informationsystem.model.reference.properties.Header; +import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; @@ -845,8 +846,8 @@ public abstract class ElementManagement { public String getContexts() throws NotFoundException, ContextException, ResourceRegistryException { try { ObjectMapper objectMapper = new ObjectMapper(); - ArrayNode arrayNode = getContextsAsArrayNode(objectMapper); - return objectMapper.writeValueAsString(arrayNode); + ObjectNode objectNode = getContextsAsObjectNode(objectMapper); + return objectMapper.writeValueAsString(objectNode); } catch(ResourceRegistryException e) { throw e; } catch (Exception e) { @@ -854,14 +855,16 @@ public abstract class ElementManagement { } } - public ArrayNode getContextsAsArrayNode(ObjectMapper objectMapper) throws NotFoundException, ContextException, ResourceRegistryException { + private ObjectNode getContextsAsObjectNode(ObjectMapper objectMapper) throws NotFoundException, ContextException, ResourceRegistryException { try { Set contexts = getContextsSet(); - ArrayNode arrayNode = objectMapper.createArrayNode(); + ContextCache contextCache = ContextCache.getInstance(); + ObjectNode objectNode = objectMapper.createObjectNode(); for(String contextUUID : contexts) { - arrayNode.add(contextUUID); + String contextFullName = contextCache.getContextFullNameByUUID(contextUUID); + objectNode.put(contextUUID, contextFullName); } - return arrayNode; + return objectNode; } catch(ResourceRegistryException e) { throw e; } catch (Exception e) { @@ -869,6 +872,11 @@ public abstract class ElementManagement { } } + public ObjectNode getContextsAsObjectNode() throws NotFoundException, ContextException, ResourceRegistryException { + ObjectMapper objectMapper = new ObjectMapper(); + return getContextsAsObjectNode(objectMapper); + } + public static String getClassProperty(JsonNode jsonNode) { if(jsonNode.has(Element.CLASS_PROPERTY)) { return jsonNode.get(Element.CLASS_PROPERTY).asText(); @@ -1100,13 +1108,12 @@ public abstract class ElementManagement { return (JsonNode) object; } - ObjectMapper objectMapper = new ObjectMapper(); if(key.compareTo(IdentifiableElement.HEADER_PROPERTY) == 0) { // Keeping the header HeaderOrient headerOrient = HeaderUtility.getHeaderOrient((ODocument) object); JsonNode headerJson = Utility.toJsonNode(headerOrient, false); if(ContextUtility.getIncludeInstanceContexts().get()) { - ((ObjectNode) headerJson).set(Header.__CONTEXTS, getContextsAsArrayNode(objectMapper)); + ((ObjectNode) headerJson).set(Header.__CONTEXTS, getContextsAsObjectNode()); } return headerJson; } @@ -1148,6 +1155,7 @@ public abstract class ElementManagement { if(object instanceof Collection) { Collection collection = (Collection) object; + ObjectMapper objectMapper = new ObjectMapper(); ArrayNode arrayNode = objectMapper.createArrayNode(); for(Object o : collection) { @@ -1163,6 +1171,7 @@ public abstract class ElementManagement { if(object instanceof Map) { @SuppressWarnings("unchecked") Map map = (Map) object; + ObjectMapper objectMapper = new ObjectMapper(); ObjectNode objectNode = objectMapper.createObjectNode(); for(String k : map.keySet()) { JsonNode obj = getPropertyForJson("PLACEHOLDER", map.get(k)); diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/AddToContextTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/AddToContextTest.java index 4810931..8be35d9 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/AddToContextTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/AddToContextTest.java @@ -1,7 +1,8 @@ package org.gcube.informationsystem.resourceregistry.instances.multicontext; import java.net.URI; -import java.util.List; +import java.util.HashSet; +import java.util.Map; import java.util.Set; import java.util.UUID; @@ -148,35 +149,21 @@ public class AddToContextTest extends MultiContextTest { return isCorrelatedTo; } - protected void checkNumerOfContext(Dataset datasetR1, int r1Number, Dataset datasetR2, int r2Number, Dataset datasetR3, int r3Number) throws Exception { + + protected void checkNumerOfContext(Dataset dataset, int expectedContextSize) throws Exception { setContextByName(GCUBE); ContextUtility.getHierarchicalMode().set(true); ContextUtility.getIncludeInstanceContexts().set(true); - Dataset r1 = ElementMapper.unmarshal(Dataset.class, getResourceManagement(datasetR1).read()); + String json = getResourceManagement(dataset).read(); + Dataset r1 = ElementMapper.unmarshal(Dataset.class, json); + @SuppressWarnings("unchecked") - List contextsR1 = (List) r1.getHeader().getAdditionalProperty(Header.__CONTEXTS); - Set contextsR1UUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(contextsR1); - Set contextsR1Fullname = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(contextsR1UUID); + Map contextsR1 = (Map) r1.getHeader().getAdditionalProperty(Header.__CONTEXTS); + Set contextsR1Fullname = new HashSet<>(contextsR1.values()); logger.info("Contexts of {} with UUID {} and Id {} are {}", Dataset.NAME, r1.getHeader().getUUID(), ((IdentifierFacet)r1.getIdentificationFacets().get(0)).getValue(), contextsR1Fullname); - Assert.assertTrue(contextsR1.size()==r1Number); - - Dataset r2 = ElementMapper.unmarshal(Dataset.class, getResourceManagement(datasetR2).read()); - @SuppressWarnings("unchecked") - List contextsR2 = (List) r2.getHeader().getAdditionalProperty(Header.__CONTEXTS); - Set contextsR2UUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(contextsR2); - Set contextsR2Fullname = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(contextsR2UUID); - logger.info("Contexts of {} with UUID {} and Id {} are {}", Dataset.NAME, r2.getHeader().getUUID(), ((IdentifierFacet)r2.getIdentificationFacets().get(0)).getValue(), contextsR2Fullname); - Assert.assertTrue(contextsR2.size()==r2Number); - - Dataset r3 = ElementMapper.unmarshal(Dataset.class, getResourceManagement(datasetR3).read()); - @SuppressWarnings("unchecked") - List contextsR3 = (List) r3.getHeader().getAdditionalProperty(Header.__CONTEXTS); - Set contextsR3UUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(contextsR3); - Set contextsR3Fullname = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(contextsR3UUID); - logger.info("Contexts of {} with UUID {} and Id {} are {}", Dataset.NAME, r3.getHeader().getUUID(), ((IdentifierFacet)r3.getIdentificationFacets().get(0)).getValue(), contextsR3Fullname); - Assert.assertTrue(contextsR3.size()==r3Number); + Assert.assertTrue(contextsR1.size()==expectedContextSize); ContextUtility.getHierarchicalMode().set(false); ContextUtility.getIncludeInstanceContexts().set(false); @@ -278,15 +265,21 @@ public class AddToContextTest extends MultiContextTest { logger.trace("As expected"); } - checkNumerOfContext(datasetR1, 3, datasetR2, 1, datasetR3, 1); + checkNumerOfContext(datasetR1, 3); + checkNumerOfContext(datasetR2, 1); + checkNumerOfContext(datasetR3, 1); setContextByName(DEVNEXT); addToContextThenTestIfBehaveProperly(datasetR1, false, NEXTNEXT); - checkNumerOfContext(datasetR1, 4, datasetR2, 2, datasetR3, 1); + checkNumerOfContext(datasetR1, 4); + checkNumerOfContext(datasetR2, 2); + checkNumerOfContext(datasetR3, 1); setContextByName(DEVSEC); addToContextThenTestIfBehaveProperly(datasetR1, false, DEVVRE); - checkNumerOfContext(datasetR1, 5, datasetR2, 2, datasetR3, 2); + checkNumerOfContext(datasetR1, 5); + checkNumerOfContext(datasetR2, 2); + checkNumerOfContext(datasetR3, 2); } catch (Exception e) { logger.error("", e); 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 index ceeb396..6a66ab7 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/ComplexTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/ComplexTest.java @@ -1,5 +1,6 @@ package org.gcube.informationsystem.resourceregistry.instances.multicontext; +import java.util.Map; import java.util.Set; import java.util.UUID; @@ -75,11 +76,10 @@ public class ComplexTest extends MultiContextTest { 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); + Map hostingNodeContextMap = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextMap(hostingNodeContexts); + Set hostingNodeContextUUID = hostingNodeContextMap.keySet(); + Assert.assertTrue(hostingNodeContextMap.size()==2); + logger.debug("Contexts of {} with UUID {} are {}", HostingNode.NAME, hostingNodeManagement.getUUID(), hostingNodeContextMap.values()); for(Facet facet : hostingNode.getFacets()) { FacetManagement facetManagement = new FacetManagement(); @@ -88,13 +88,12 @@ public class ComplexTest extends MultiContextTest { 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); + Map facetContextMap = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextMap(facetContexts); + Assert.assertTrue(facetContextMap.size()==2); + Set facetContextSet = facetContextMap.keySet(); + Assert.assertTrue(facetContextSet.containsAll(hostingNodeContextUUID)); + Assert.assertTrue(hostingNodeContextUUID.containsAll(facetContextSet)); + logger.debug("Contexts of {} with UUID {} are {}", typeName, facetManagement.getUUID(), facetContextMap.values()); } activatesManagement = new IsRelatedToManagement(); @@ -102,22 +101,22 @@ public class ComplexTest extends MultiContextTest { 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); + Map activatesContextMap = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextMap(isRelatedToContexts); + Set activatesContextsUUID = activatesContextMap.keySet(); 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); + Assert.assertFalse(activatesContextsUUID.containsAll(hostingNodeContextUUID)); + Assert.assertTrue(hostingNodeContextUUID.containsAll(activatesContextsUUID)); + logger.debug("Contexts of {} with UUID {} are {}", Activates.NAME, activatesManagement.getUUID(), activatesContextMap.values()); 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); + Map eServiceContextMap = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextMap(eServiceContexts); + Set eServiceContextsUUID = eServiceContextMap.keySet(); Assert.assertTrue(eServiceContextsUUID.size()==1); - Assert.assertFalse(eServiceContextsUUID.containsAll(hostingNodeContextsUUID)); - Assert.assertTrue(hostingNodeContextsUUID.containsAll(eServiceContextsUUID)); + Assert.assertFalse(eServiceContextsUUID.containsAll(hostingNodeContextUUID)); + Assert.assertTrue(hostingNodeContextUUID.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);