resource-registry/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/BasicTest.java

480 lines
19 KiB
Java
Raw Normal View History

/**
*
*/
package org.gcube.informationsystem.resourceregistry.instances.multicontext;
import java.util.HashMap;
2021-01-25 17:38:19 +01:00
import java.util.Map;
import java.util.UUID;
2021-01-25 17:38:19 +01:00
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.informationsystem.base.reference.IdentifiableElement;
2020-11-05 15:58:30 +01:00
import org.gcube.informationsystem.context.reference.entities.Context;
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.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.resourceregistry.ContextTest;
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;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAvailableInAnotherContextException;
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;
2020-01-27 17:07:37 +01:00
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
2021-03-02 16:55:42 +01:00
import org.gcube.informationsystem.resourceregistry.instances.ERManagementTest;
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.entities.FacetManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement;
2019-11-06 12:14:27 +01:00
import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement;
import org.gcube.informationsystem.utils.ElementMapper;
import org.gcube.resourcemanagement.model.impl.entities.facets.CPUFacetImpl;
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.CPUFacet;
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)
*/
2021-02-17 15:51:25 +01:00
public class BasicTest extends MultiContextTest {
private static Logger logger = LoggerFactory
.getLogger(BasicTest.class);
@Test
public void testDifferentScopes() throws Exception {
2020-11-05 17:03:05 +01:00
ContextTest.setContextByName(DEFAULT_TEST_SCOPE);
CPUFacet cpuFacet = new CPUFacetImpl();
cpuFacet.setClockSpeed("1 GHz");
cpuFacet.setModel("Opteron");
cpuFacet.setVendor("AMD");
FacetManagement facetManagement = new FacetManagement();
facetManagement.setJson(ElementMapper.marshal(cpuFacet));
facetManagement.setElementType(CPUFacet.NAME);
String json = facetManagement.create();
logger.debug("Created : {}", json);
CPUFacet createdCPUFacet = ElementMapper.unmarshal(CPUFacet.class, json);
UUID uuid = createdCPUFacet.getHeader().getUUID();
facetManagement = new FacetManagement();
facetManagement.setUUID(uuid);
2021-02-05 17:50:16 +01:00
String readJson = facetManagement.read().toString();
logger.debug("Read : {}", readJson);
/* ------------------------------------------------------------------ */
logger.debug("Switching to another scope");
ContextTest.setContextByName(PARENT_DEFAULT_TEST_SCOPE);
try {
facetManagement = new FacetManagement();
facetManagement.setUUID(uuid);
2021-02-05 17:50:16 +01:00
readJson = facetManagement.read().toString();
logger.debug("You should not be able to read Facet with UUID {}",
uuid);
throw new Exception(
"You should not be able to read Facet with UUID " + uuid);
} catch (FacetAvailableInAnotherContextException e) {
logger.debug("Good the facet created in the default context is not visible in an alternative context");
}
/* ---------------- entering hierarchic mode */
2020-10-27 15:12:11 +01:00
ContextUtility.getHierarchicalMode().set(true);
facetManagement = new FacetManagement();
facetManagement.setUUID(uuid);
2021-02-05 17:50:16 +01:00
readJson = facetManagement.read().toString();
logger.debug("You should be able to read it {}", readJson);
2020-10-27 15:12:11 +01:00
ContextUtility.getHierarchicalMode().set(false);
/* ---------------- leaving hierarchic mode */
cpuFacet.setAdditionalProperty("My", "Test");
try {
facetManagement = new FacetManagement();
facetManagement.setUUID(uuid);
facetManagement.setJson(ElementMapper.marshal(cpuFacet));
readJson = facetManagement.update();
logger.debug("You should not be able to update Facet with UUID {}",
uuid);
throw new Exception(
"You should not be able to read Facet with UUID " + uuid);
} catch (FacetAvailableInAnotherContextException e) {
logger.debug("Good the Facet created in the default context cannot be updated in an alternative context");
}
/* ------------------------------------------------------------------ */
logger.debug("Setting back default scope");
ContextTest.setContextByName(DEFAULT_TEST_SCOPE);
facetManagement = new FacetManagement();
facetManagement.setUUID(uuid);
facetManagement.setJson(ElementMapper.marshal(cpuFacet));
readJson = facetManagement.update();
logger.debug("Updated : {}", readJson);
facetManagement = new FacetManagement();
facetManagement.setUUID(uuid);
2021-02-05 17:50:16 +01:00
readJson = facetManagement.read().toString();
logger.debug("Read Updated : {}", readJson);
facetManagement = new FacetManagement();
facetManagement.setUUID(uuid);
boolean deleted = facetManagement.delete();
Assert.assertTrue(deleted);
}
@Test(expected = ResourceRegistryException.class)
public void testResourceWithOnlyOneFacet() throws Exception {
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.unpropagate);
propagationConstraint.setRemoveConstraint(RemoveConstraint.cascade);
IsIdentifiedBy<EService, Facet> isIdentifiedBy = new IsIdentifiedByImpl<EService, Facet>(
eService, softwareFacet, propagationConstraint);
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);
/*------------------------------------------------------------------------*/
try {
2021-01-28 22:53:10 +01:00
addToContextThenTestIfBehaveProperly(eService, true, ALTERNATIVE_TEST_SCOPE);
}finally {
UUID eServiceUUID = eService.getHeader().getUUID();
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(eServiceUUID);
boolean deleted = resourceManagement.delete();
Assert.assertTrue(deleted);
}
}
@Test
public void testCreateEServiceHostingNode() throws Exception {
2021-03-02 16:55:42 +01:00
EService eService = ERManagementTest.instantiateValidEService();
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);
Map<UUID, IdentifiableElement> eServiceInstances = new HashMap<>();
eServiceInstances.put(eService.getHeader().getUUID(), eService);
for(ConsistsOf<?,?> consistsOf : eService.getConsistsOf()) {
eServiceInstances.put(consistsOf.getHeader().getUUID(), consistsOf);
eServiceInstances.put(consistsOf.getTarget().getHeader().getUUID(), consistsOf.getTarget());
}
2021-03-02 16:55:42 +01:00
HostingNode hostingNode = ERManagementTest.instantiateValidHostinNode();
resourceManagement = new ResourceManagement();
resourceManagement.setElementType(HostingNode.NAME);
resourceManagement.setJson(ElementMapper.marshal(hostingNode));
String hnJson = resourceManagement.create();
logger.debug("Created : {}", hnJson);
hostingNode = ElementMapper.unmarshal(HostingNode.class, hnJson);
logger.debug("Unmarshalled {} {}", HostingNode.NAME, hostingNode);
UUID hostingNodeUUID = hostingNode.getHeader().getUUID();
Map<UUID, IdentifiableElement> hostingNodeInstances = new HashMap<>();
hostingNodeInstances.put(hostingNodeUUID, hostingNode);
for(ConsistsOf<?,?> consistsOf : hostingNode.getConsistsOf()) {
hostingNodeInstances.put(consistsOf.getHeader().getUUID(), consistsOf);
hostingNodeInstances.put(consistsOf.getTarget().getHeader().getUUID(), consistsOf.getTarget());
}
2021-01-28 22:53:10 +01:00
/* ------------------------------------------------------------------ */
2021-01-26 17:35:08 +01:00
2021-01-28 22:53:10 +01:00
String currentContextFulName = ContextTest.getCurrentContextFullName();
String targetContextFullName = ALTERNATIVE_TEST_SCOPE;
2021-01-28 22:53:10 +01:00
addToContextThenTestIfBehaveProperly(hostingNode, true, targetContextFullName);
2021-01-28 22:53:10 +01:00
logger.debug("Switching to the target context");
ContextTest.setContextByName(targetContextFullName);
UUID contextUUID = ContextUtility.getCurrentSecurityContext().getUUID();
try {
2021-01-26 17:35:08 +01:00
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(hostingNodeUUID);
resourceManagement.read();
logger.debug("You should not be able to read {} with UUID {} in {} with UUID {} (i.e., {})",
HostingNode.NAME, hostingNodeUUID.toString(), Context.NAME, contextUUID, ContextUtility.getCurrentContextFullName());
}catch (AvailableInAnotherContextException e) {
// OK
}
2021-01-25 17:38:19 +01:00
2021-01-28 22:53:10 +01:00
logger.debug("Switching back to the target context");
ContextTest.setContextByName(currentContextFulName);
addToContextThenTestIfBehaveProperly(hostingNode, false, targetContextFullName);
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(hostingNodeUUID);
2021-02-05 17:50:16 +01:00
String hnString = resourceManagement.read().toString();
HostingNode readHN = ElementMapper.unmarshal(HostingNode.class, hnString);
Assert.assertTrue(readHN.getHeader().getUUID().compareTo(hostingNodeUUID) == 0);
UUID eServiceUUID = eService.getHeader().getUUID();
try {
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(eServiceUUID);
resourceManagement.read();
} catch (ResourceAvailableInAnotherContextException e) {
logger.debug("Resource with {} Not Found as Expected",
eServiceUUID);
}
/* Commented because the behavior has been changed
try {
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(eServiceUUID);
resourceManagement.delete();
logger.debug("You should not be able to delete EService with UUID {}",
uuid);
throw new Exception(
"You should not be able to delete EService with UUID " + uuid);
} catch (ResourceAvailableInAnotherContextException e) {
logger.debug("Resource with {} Not Deleted as Expected",
eServiceUUID);
}
*/
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(hostingNodeUUID);
boolean deleted = resourceManagement.delete();
Assert.assertTrue(deleted);
/* ------------------------------------------------------------------ */
logger.debug("Setting back default scope");
ContextTest.setContextByName(DEFAULT_TEST_SCOPE);
2021-01-29 17:29:49 +01:00
try {
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(eServiceUUID);
deleted = resourceManagement.delete();
}catch (ResourceNotFoundException e) {
// OK
}
}
// @Test
public void addTest() throws ResourceNotFoundException,
ContextNotFoundException, ResourceRegistryException {
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setUUID(UUID.fromString(""));
resourceManagement.addToContext(ContextUtility.getCurrentSecurityContext().getUUID());
}
@Test
public void testAddAndRemoveFromContext() throws Exception {
/* Creating HostingNode */
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setElementType(HostingNode.NAME);
2021-02-23 11:51:37 +01:00
resourceManagement.setJson(SmartgearResourcesTest.HOSTING_NODE);
String hnJson = resourceManagement.create();
logger.debug("Created : {}", hnJson);
2021-02-23 11:51:37 +01:00
HostingNode hostingNode = ElementMapper.unmarshal(HostingNode.class, hnJson);
logger.debug("Unmarshalled {} {}", HostingNode.NAME, hostingNode);
UUID hnUUID = hostingNode.getHeader().getUUID();
/* Creating EService */
resourceManagement = new ResourceManagement();
resourceManagement.setElementType(EService.NAME);
2021-02-23 11:51:37 +01:00
resourceManagement.setJson(SmartgearResourcesTest.ESERVICE);
String eservicejson = resourceManagement.create();
logger.debug("Created : {}", eservicejson);
2021-02-23 11:51:37 +01:00
EService eService = ElementMapper.unmarshal(EService.class, eservicejson);
logger.debug("Unmarshalled {} {}", EService.NAME, eService);
UUID eServiceUUID = eService.getHeader().getUUID();
/* Creating Activates Relation */
PropagationConstraint propagationConstraint = new PropagationConstraintImpl();
propagationConstraint
.setRemoveConstraint(RemoveConstraint.cascade);
propagationConstraint
.setAddConstraint(AddConstraint.propagate);
Activates<HostingNode, EService> activates = new ActivatesImpl<>(hostingNode, eService,
propagationConstraint);
IsRelatedToManagement isRelatedToManagement = new IsRelatedToManagement();
isRelatedToManagement.setElementType(Activates.NAME);
String activatesJson = ElementMapper.marshal(activates);
isRelatedToManagement.setJson(activatesJson);
String createdActivatesJson = isRelatedToManagement.create();
logger.debug("Created : {}", createdActivatesJson);
@SuppressWarnings("unchecked")
Activates<HostingNode, EService> createdActivates = ElementMapper.unmarshal(Activates.class, createdActivatesJson);
UUID activatesUUID = createdActivates.getHeader().getUUID();
/* ------------------------------------------------------------------ */
/*
* resourceManagement = new ResourceManagement();
* resourceManagement.setUUID(hnUUID);
* resourceManagement.addToContext();
* resourceManagement = new ResourceManagement();
* resourceManagement.setUUID(eServiceUUID);
* resourceManagement.addToContext();
*
* This code is commented because the addToContext
* on relation enforce addToContext both on source
* and target
*
*/
isRelatedToManagement = new IsRelatedToManagement();
isRelatedToManagement.setUUID(activatesUUID);
2021-01-29 17:29:49 +01:00
UUID contextUUID = ContextUtility.getInstance().getSecurityContextByFullName(ALTERNATIVE_TEST_SCOPE).getUUID();
isRelatedToManagement.addToContext(contextUUID);
logger.debug("Switching to alternative scope");
ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE);
/* The addTocontext on the relation adds the source and target too.
* So that I MUST be able to read HostinNode and EService
*/
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(hnUUID);
resourceManagement.read();
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(eServiceUUID);
resourceManagement.read();
/* ------------------------------------------------------------------ */
logger.debug("Setting back default scope");
ContextTest.setContextByName(DEFAULT_TEST_SCOPE);
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(hnUUID);
Map<UUID,JsonNode> affectedInstances = resourceManagement.removeFromContext(ContextUtility.getCurrentSecurityContext().getUUID());
2021-01-29 17:29:49 +01:00
logger.debug("Remove from Context affects the following instances {}", affectedInstances.values());
// Assert.assertTrue(affectedInstaces);
/* The cascading MUST remove the relation and the target so that
* I MUST not be able to read Activates relation and EService
*/
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(eServiceUUID);
try {
resourceManagement.read();
String error = String.format("{} with UUID {} should not be visible.", EService.NAME, eServiceUUID);
logger.trace(error);
throw new Exception(error);
}catch (ResourceAvailableInAnotherContextException e) {
// OK
}
isRelatedToManagement = new IsRelatedToManagement();
isRelatedToManagement.setUUID(activatesUUID);
try {
isRelatedToManagement.read();
String error = String.format("{} with UUID {} should not be visible.", Activates.NAME, activatesUUID);
logger.trace(error);
throw new Exception(error);
}catch (RelationAvailableInAnotherContextException e) {
// OK
}
/* ------------------------------------------------------------------ */
logger.debug("Switching to alternative scope again");
ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE);
// TODO checks here
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(hnUUID);
boolean deleted = resourceManagement.delete();
Assert.assertTrue(deleted);
}
2020-11-05 15:58:30 +01:00
// @Test
public void testGetInstanceContexts() throws ObjectNotFound, Exception {
2020-11-05 15:58:30 +01:00
String type = "HostingNode";
String instanceId = "f0460614-9ffb-4ecd-bf52-d91e8d81d604";
@SuppressWarnings("rawtypes")
ElementManagement erManagement = ElementManagementUtility.getERManagement(type);
UUID uuid = null;
try {
uuid = UUID.fromString(instanceId);
} catch(Exception e) {
throw new ResourceRegistryException(e);
}
erManagement.setUUID(uuid);
String contexts = erManagement.getContexts();
logger.debug("{}", contexts);
}
2020-11-05 15:58:30 +01:00
}