Changed the code which uses the contexts included in header or

explicitly requested for an instance
This commit is contained in:
Luca Frosini 2021-10-28 10:50:46 +02:00
parent 8a66cb622a
commit fe1dafd250
3 changed files with 56 additions and 55 deletions

View File

@ -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<El extends OElement, T extends Type> {
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<El extends OElement, T extends Type> {
}
}
public ArrayNode getContextsAsArrayNode(ObjectMapper objectMapper) throws NotFoundException, ContextException, ResourceRegistryException {
private ObjectNode getContextsAsObjectNode(ObjectMapper objectMapper) throws NotFoundException, ContextException, ResourceRegistryException {
try {
Set<String> 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<El extends OElement, T extends Type> {
}
}
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<El extends OElement, T extends Type> {
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<El extends OElement, T extends Type> {
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<El extends OElement, T extends Type> {
if(object instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) object;
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode objectNode = objectMapper.createObjectNode();
for(String k : map.keySet()) {
JsonNode obj = getPropertyForJson("PLACEHOLDER", map.get(k));

View File

@ -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<String> contextsR1 = (List<String>) r1.getHeader().getAdditionalProperty(Header.__CONTEXTS);
Set<UUID> contextsR1UUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(contextsR1);
Set<String> contextsR1Fullname = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(contextsR1UUID);
Map<String,String> contextsR1 = (Map<String, String>) r1.getHeader().getAdditionalProperty(Header.__CONTEXTS);
Set<String> 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<String> contextsR2 = (List<String>) r2.getHeader().getAdditionalProperty(Header.__CONTEXTS);
Set<UUID> contextsR2UUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(contextsR2);
Set<String> 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<String> contextsR3 = (List<String>) r3.getHeader().getAdditionalProperty(Header.__CONTEXTS);
Set<UUID> contextsR3UUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(contextsR3);
Set<String> 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);

View File

@ -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<UUID> hostingNodeContextsUUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(hostingNodeContexts);
Assert.assertTrue(hostingNodeContextsUUID.size()==2);
Set<String> 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<UUID, String> hostingNodeContextMap = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextMap(hostingNodeContexts);
Set<UUID> 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<UUID> 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<String> 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<UUID, String> facetContextMap = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextMap(facetContexts);
Assert.assertTrue(facetContextMap.size()==2);
Set<UUID> 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<UUID> activatesContextsUUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(isRelatedToContexts);
Map<UUID, String> activatesContextMap = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextMap(isRelatedToContexts);
Set<UUID> activatesContextsUUID = activatesContextMap.keySet();
Assert.assertTrue(activatesContextsUUID.size()==1);
Assert.assertFalse(activatesContextsUUID.containsAll(hostingNodeContextsUUID));
Assert.assertTrue(hostingNodeContextsUUID.containsAll(activatesContextsUUID));
Set<String> 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<UUID> eServiceContextsUUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(eServiceContexts);
Map<UUID,String> eServiceContextMap = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextMap(eServiceContexts);
Set<UUID> 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<String> 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);