Reorganizing tests

This commit is contained in:
Luca Frosini 2021-02-17 15:51:25 +01:00
parent 2597379ad4
commit a553b9cd8d
5 changed files with 674 additions and 549 deletions

View File

@ -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<UUID, Element> getRemovedExpectedInstances(Resource r) {
Map<UUID, Element> expected = new HashMap<>();
expected.put(r.getHeader().getUUID(), r);
// TODO query
for(IsRelatedTo<? extends Resource, ? extends Resource> 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<? extends Resource, ? extends Facet> 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<UUID, Element> 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<UUID, JsonNode> affectedInstances = resourceManagement.removeFromContext(contextUUID);
SortedSet<UUID> expectedInstancesUUID = new TreeSet<>(expectedInstances.keySet());
SortedSet<UUID> 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<Resource, Entity> r);
public boolean mustBeRemoved(Relation<Resource, Entity> 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<IsRelatedTo> isRelatedToList = ElementMapper.unmarshalList(IsRelatedTo.class, ret);
for(@SuppressWarnings("rawtypes") IsRelatedTo isRelatedTo : isRelatedToList) {
@SuppressWarnings("unchecked")
boolean mustBeAdded = relationConstraint.mustBeAdded(isRelatedTo);
if(mustBeAdded) {
@SuppressWarnings("unchecked")
IsRelatedTo<EService, Resource> irt = (IsRelatedTo<EService, Resource>) isRelatedTo;
r.attachResource(irt);
}
}
return r;
}
protected Map<UUID, Element> getAddedExpectedInstances(Resource r) throws ResourceRegistryException, Exception {
Map<UUID, Element> expected = new HashMap<>();
expected.put(r.getHeader().getUUID(), r);
RelationConstraint propagateConstraint = new RelationConstraint() {
@Override
public boolean mustBeAdded(Relation<Resource, Entity> r) {
return r.getPropagationConstraint().getAddConstraint() == AddConstraint.propagate;
}
@Override
public boolean mustBeRemoved(Relation<Resource, Entity> r) {
throw new UnsupportedOperationException();
}
};
r = getAndAddIsRelatedTo(r, propagateConstraint);
for(IsRelatedTo<? extends Resource, ? extends Resource> isRelatedTo : r.getIsRelatedTo()) {
if(isRelatedTo.getPropagationConstraint().getAddConstraint() == AddConstraint.propagate) {
expected.put(isRelatedTo.getHeader().getUUID(), isRelatedTo);
expected.putAll(getAddedExpectedInstances(isRelatedTo.getTarget()));
}
}
for(ConsistsOf<? extends Resource, ? extends Facet> 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<UUID, Element> 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<UUID, JsonNode> affectedInstances = resourceManagement.addToContext(contextUUID);
SortedSet<UUID> expectedInstancesUUID = new TreeSet<>(expectedInstances.keySet());
SortedSet<UUID> 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<EService, Facet> isIdentifiedBy = new IsIdentifiedByImpl<EService, Facet>(
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<UUID, JsonNode> 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<EService, Facet> gotIsIdentifiedBy = (IsIdentifiedBy<EService, Facet>) 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<Context> renew() throws ResourceRegistryException {
ContextManagement contextManagement = new ContextManagement();
String contextsJsonString = contextManagement.allFromServer(false);
List<Context> 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<HostingNode, EService> 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<HostingNode, EService> 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<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);
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<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);
}
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<UUID> 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<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);
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<UUID> 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<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);
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<HostingNode, EService> 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<HostingNode, EService> 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<String> contextsCollection = (Collection<String>) hostingNodeRead.getHeader().getAdditionalProperty(Header.__CONTEXTS);
Assert.assertTrue(contextsCollection.size()==2);
Set<UUID> hostingNodeContextsUUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(contextsCollection);
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);
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<String> facetContextsCollection = (Collection<String>) facetRead.getHeader().getAdditionalProperty(Header.__CONTEXTS);
Assert.assertTrue(contextsCollection.size()==2);
Set<UUID> 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<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);
}
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<HostingNode, EService> activatesRead = ElementMapper.unmarshal(Activates.class, activatesReadJson);
logger.debug("Unmarshalled {} {}", Utility.getTypeName(activatesRead.getClass()), activatesRead);
@SuppressWarnings("unchecked")
Collection<String> activatesContextsCollection = (Collection<String>) activatesRead.getHeader().getAdditionalProperty(Header.__CONTEXTS);
logger.debug("Contexts of {} with UUID {} have the following UUID {}", Activates.NAME, activatesManagement.getUUID(), activatesContextsCollection);
Set<UUID> 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<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);
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<String> eServiceContextsCollection = (Collection<String>) eServiceRead.getHeader().getAdditionalProperty(Header.__CONTEXTS);
logger.debug("Contexts of {} with UUID {} have the following UUID {}", EService.NAME, eServiceManagement.getUUID(), eServiceContextsCollection);
Set<UUID> 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<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);
boolean deleted = hostingNodeManagement.delete();
Assert.assertTrue(deleted);
}
}

View File

@ -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<HostingNode, EService> 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<HostingNode, EService> 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<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);
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<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);
}
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<UUID> 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<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);
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<UUID> 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<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);
boolean deleted = hostingNodeManagement.delete();
Assert.assertTrue(deleted);
}
}

View File

@ -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<EService, Facet> isIdentifiedBy = new IsIdentifiedByImpl<EService, Facet>(
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<UUID, JsonNode> 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<EService, Facet> gotIsIdentifiedBy = (IsIdentifiedBy<EService, Facet>) 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<HostingNode, EService> 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<HostingNode, EService> 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<String> contextsCollection = (Collection<String>) hostingNodeRead.getHeader().getAdditionalProperty(Header.__CONTEXTS);
Assert.assertTrue(contextsCollection.size()==2);
Set<UUID> hostingNodeContextsUUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(contextsCollection);
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);
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<String> facetContextsCollection = (Collection<String>) facetRead.getHeader().getAdditionalProperty(Header.__CONTEXTS);
Assert.assertTrue(contextsCollection.size()==2);
Set<UUID> 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<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);
}
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<HostingNode, EService> activatesRead = ElementMapper.unmarshal(Activates.class, activatesReadJson);
logger.debug("Unmarshalled {} {}", Utility.getTypeName(activatesRead.getClass()), activatesRead);
@SuppressWarnings("unchecked")
Collection<String> activatesContextsCollection = (Collection<String>) activatesRead.getHeader().getAdditionalProperty(Header.__CONTEXTS);
logger.debug("Contexts of {} with UUID {} have the following UUID {}", Activates.NAME, activatesManagement.getUUID(), activatesContextsCollection);
Set<UUID> 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<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);
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<String> eServiceContextsCollection = (Collection<String>) eServiceRead.getHeader().getAdditionalProperty(Header.__CONTEXTS);
logger.debug("Contexts of {} with UUID {} have the following UUID {}", EService.NAME, eServiceManagement.getUUID(), eServiceContextsCollection);
Set<UUID> 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<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);
boolean deleted = hostingNodeManagement.delete();
Assert.assertTrue(deleted);
}
}

View File

@ -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<UUID, Element> getRemovedExpectedInstances(Resource r) {
Map<UUID, Element> expected = new HashMap<>();
expected.put(r.getHeader().getUUID(), r);
// TODO query
for (IsRelatedTo<? extends Resource, ? extends Resource> 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<? extends Resource, ? extends Facet> 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<UUID, Element> 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<UUID, JsonNode> affectedInstances = resourceManagement.removeFromContext(contextUUID);
SortedSet<UUID> expectedInstancesUUID = new TreeSet<>(expectedInstances.keySet());
SortedSet<UUID> 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<Resource, Entity> r);
public boolean mustBeRemoved(Relation<Resource, Entity> 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<IsRelatedTo> isRelatedToList = ElementMapper.unmarshalList(IsRelatedTo.class, ret);
for (@SuppressWarnings("rawtypes")
IsRelatedTo isRelatedTo : isRelatedToList) {
@SuppressWarnings("unchecked")
boolean mustBeAdded = relationConstraint.mustBeAdded(isRelatedTo);
if (mustBeAdded) {
@SuppressWarnings("unchecked")
IsRelatedTo<EService, Resource> irt = (IsRelatedTo<EService, Resource>) isRelatedTo;
r.attachResource(irt);
}
}
return r;
}
protected Map<UUID, Element> getAddedExpectedInstances(Resource r) throws ResourceRegistryException, Exception {
Map<UUID, Element> expected = new HashMap<>();
expected.put(r.getHeader().getUUID(), r);
RelationConstraint propagateConstraint = new RelationConstraint() {
@Override
public boolean mustBeAdded(Relation<Resource, Entity> r) {
return r.getPropagationConstraint().getAddConstraint() == AddConstraint.propagate;
}
@Override
public boolean mustBeRemoved(Relation<Resource, Entity> r) {
throw new UnsupportedOperationException();
}
};
r = getAndAddIsRelatedTo(r, propagateConstraint);
for (IsRelatedTo<? extends Resource, ? extends Resource> isRelatedTo : r.getIsRelatedTo()) {
if (isRelatedTo.getPropagationConstraint().getAddConstraint() == AddConstraint.propagate) {
expected.put(isRelatedTo.getHeader().getUUID(), isRelatedTo);
expected.putAll(getAddedExpectedInstances(isRelatedTo.getTarget()));
}
}
for (ConsistsOf<? extends Resource, ? extends Facet> 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<UUID, Element> 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<UUID, JsonNode> affectedInstances = resourceManagement.addToContext(contextUUID);
SortedSet<UUID> expectedInstancesUUID = new TreeSet<>(expectedInstances.keySet());
SortedSet<UUID> 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<Context> renew() throws ResourceRegistryException {
ContextManagement contextManagement = new ContextManagement();
String contextsJsonString = contextManagement.allFromServer(false);
List<Context> contexts = null;
try {
contexts = ElementMapper.unmarshalList(contextsJsonString);
} catch (IOException e) {
logger.error("Unable to read context from server", e);
}
return contexts;
}
};
}

View File

@ -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 {