Improved tests and assertion
This commit is contained in:
parent
406035dd40
commit
376fbd1df6
|
@ -24,11 +24,13 @@ 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.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.resourceregistry.ContextTest;
|
||||
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
|
||||
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCacheRenewal;
|
||||
|
@ -189,226 +191,59 @@ public class BasicTest extends ContextTest {
|
|||
// TODO test add to context to a resource with only one facet and add propagation constraint to unpropagate.
|
||||
// TODO test remove from context to a facet which is the only facet that characterise a Resource.
|
||||
|
||||
protected Map<UUID, Element> getAddedExpectedInstances(Resource r) {
|
||||
Map<UUID, Element> expected = new HashMap<>();
|
||||
expected.put(r.getHeader().getUUID(), r);
|
||||
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;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addRemoveDryRunTest() throws Exception {
|
||||
EService eService = new EServiceImpl();
|
||||
|
||||
// I add only one facet to have the possibility to define the tests easier
|
||||
SoftwareFacet softwareFacet = new SoftwareFacetImpl();
|
||||
softwareFacet.setGroup("InformationSystem");
|
||||
softwareFacet.setName("resource-registry");
|
||||
softwareFacet.setVersion("1.1.0");
|
||||
IsIdentifiedBy<EService, Facet> isIdentifiedBy = new IsIdentifiedByImpl<EService, Facet>(
|
||||
eService, softwareFacet, null);
|
||||
eService.addFacet(isIdentifiedBy);
|
||||
|
||||
protected void addToContextThenTestIfBehaveProperly(Resource r, boolean dryRun) throws ResourceRegistryException {
|
||||
Map<UUID, Element> expectedInstances = getAddedExpectedInstances(r);
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
|
||||
HostingNode hostingNode = new HostingNodeImpl();
|
||||
|
||||
NetworkingFacet networkingFacet = new NetworkingFacetImpl();
|
||||
networkingFacet.setIPAddress("146.48.87.183");
|
||||
networkingFacet.setHostName("pc-frosini.isti.cnr.it");
|
||||
networkingFacet.setDomainName("isti.cnr.it");
|
||||
networkingFacet.setMask("255.255.248.0");
|
||||
networkingFacet.setBroadcastAddress("146.48.87.255");
|
||||
|
||||
IsIdentifiedBy<HostingNode, NetworkingFacet> isIdentifiedByHNNF = new IsIdentifiedByImpl<HostingNode, NetworkingFacet>(
|
||||
hostingNode, networkingFacet, null);
|
||||
hostingNode.addFacet(isIdentifiedByHNNF);
|
||||
|
||||
CPUFacet cpuFacet = new CPUFacetImpl();
|
||||
cpuFacet.setClockSpeed("1 GHz");
|
||||
cpuFacet.setModel("Opteron");
|
||||
cpuFacet.setVendor("AMD");
|
||||
hostingNode.addFacet(cpuFacet);
|
||||
|
||||
MemoryFacet ram = new MemoryFacetImpl();
|
||||
ram.setSize(8);
|
||||
ram.setUnit(MemoryUnit.GB);
|
||||
ram.setUsed(2);
|
||||
|
||||
HasVolatileMemory<HostingNode, MemoryFacet> hasVolatileMemory = new HasVolatileMemoryImpl<HostingNode, MemoryFacet>(
|
||||
hostingNode, ram, null);
|
||||
hostingNode.addFacet(hasVolatileMemory);
|
||||
|
||||
MemoryFacet disk = new MemoryFacetImpl();
|
||||
disk.setSize(256);
|
||||
disk.setUnit(MemoryUnit.GB);
|
||||
disk.setUsed(120);
|
||||
|
||||
HasPersistentMemory<HostingNode, MemoryFacet> hasPersistentMemory = new HasPersistentMemoryImpl<HostingNode, MemoryFacet>(
|
||||
hostingNode, disk, null);
|
||||
hostingNode.addFacet(hasPersistentMemory);
|
||||
|
||||
Activates<HostingNode, EService> activates = new ActivatesImpl<HostingNode, EService>(
|
||||
hostingNode, eService, null);
|
||||
UUID activatedUUID = UUID.randomUUID();
|
||||
activates.setHeader(new HeaderImpl(activatedUUID));
|
||||
hostingNode.attachResource(activates);
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
IsRelatedToManagement isRelatedToManagement = new IsRelatedToManagement();
|
||||
isRelatedToManagement.setElementType(Activates.NAME);
|
||||
isRelatedToManagement.setUUID(activatedUUID);
|
||||
String activatesString = isRelatedToManagement.read();
|
||||
activates = ElementMapper.unmarshal(Activates.class, activatesString);
|
||||
|
||||
Map<UUID, IdentifiableElement> createdInstances = new HashMap<>();
|
||||
createdInstances.putAll(hostingNodeInstances);
|
||||
/* These should not be added due to default propagation constraint for IsRelatedTo relation
|
||||
createdInstances.put(activatedUUID, activates);
|
||||
createdInstances.putAll(eServiceInstances);
|
||||
*/
|
||||
Assert.assertTrue(createdInstances.size()==9);
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
logger.debug("Switching to another scope");
|
||||
ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE);
|
||||
|
||||
resourceManagement = new ResourceManagement();
|
||||
resourceManagement.setUUID(hostingNodeUUID);
|
||||
|
||||
resourceManagement.setDryRunContextSharing(true);
|
||||
resourceManagement.setUUID(r.getHeader().getUUID());
|
||||
resourceManagement.setDryRunContextSharing(dryRun);
|
||||
UUID contextUUID = ContextUtility.getCurrentSecurityContext().getUUID();
|
||||
Map<UUID, JsonNode> testAffectedInstances = resourceManagement.addToContext(contextUUID);
|
||||
|
||||
resourceManagement = new ResourceManagement();
|
||||
resourceManagement.setUUID(hostingNodeUUID);
|
||||
|
||||
try {
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
resourceManagement.setDryRunContextSharing(false);
|
||||
Map<UUID, JsonNode> affectedInstances = resourceManagement.addToContext(contextUUID);
|
||||
logger.debug("Add to Context afffects the following instances {}", affectedInstances.values());
|
||||
|
||||
SortedSet<UUID> testAffectedInstancesUUID = new TreeSet<>(testAffectedInstances.keySet());
|
||||
SortedSet<UUID> expectedInstancesUUID = new TreeSet<>(expectedInstances.keySet());
|
||||
SortedSet<UUID> affectedInstancesUUID = new TreeSet<>(affectedInstances.keySet());
|
||||
SortedSet<UUID> createdInstancesUUID = new TreeSet<>(createdInstances.keySet());
|
||||
|
||||
logger.debug("Created Instances where {}", createdInstancesUUID);
|
||||
logger.debug("Test Affected Instances where {}", testAffectedInstancesUUID);
|
||||
logger.debug("Affected Instances are {}", affectedInstancesUUID);
|
||||
logger.trace("Expected Instances are {} : {}", expectedInstancesUUID.size(), expectedInstancesUUID);
|
||||
logger.trace("Affected Instances are {} : {}", affectedInstancesUUID.size(), affectedInstancesUUID);
|
||||
|
||||
Assert.assertTrue(testAffectedInstancesUUID.size()==affectedInstancesUUID.size());
|
||||
Assert.assertTrue(createdInstancesUUID.size()==affectedInstancesUUID.size());
|
||||
Assert.assertTrue(expectedInstancesUUID.size()==affectedInstancesUUID.size());
|
||||
|
||||
Assert.assertTrue(createdInstancesUUID.containsAll(testAffectedInstancesUUID));
|
||||
Assert.assertTrue(testAffectedInstancesUUID.containsAll(createdInstancesUUID));
|
||||
|
||||
Assert.assertTrue(createdInstancesUUID.containsAll(affectedInstancesUUID));
|
||||
Assert.assertTrue(affectedInstancesUUID.containsAll(createdInstancesUUID));
|
||||
|
||||
Assert.assertTrue(testAffectedInstancesUUID.containsAll(affectedInstancesUUID));
|
||||
Assert.assertTrue(affectedInstancesUUID.containsAll(testAffectedInstancesUUID));
|
||||
Assert.assertTrue(expectedInstancesUUID.containsAll(affectedInstancesUUID));
|
||||
Assert.assertTrue(affectedInstancesUUID.containsAll(expectedInstancesUUID));
|
||||
|
||||
|
||||
for(UUID uuid : affectedInstances.keySet()) {
|
||||
JsonNode testAffectedJsonNode = testAffectedInstances.get(uuid);
|
||||
String testAffectedType = testAffectedJsonNode.get(Element.CLASS_PROPERTY).asText();
|
||||
|
||||
Element element = expectedInstances.get(uuid);
|
||||
String expectedType = TypeMapper.getType(element.getClass());
|
||||
|
||||
JsonNode affectedJsonNode = affectedInstances.get(uuid);
|
||||
String affectedType = affectedJsonNode.get(Element.CLASS_PROPERTY).asText();
|
||||
|
||||
IdentifiableElement identifiableElement = createdInstances.get(uuid);
|
||||
String createdType = TypeMapper.getType(identifiableElement.getClass());
|
||||
Assert.assertTrue(affectedType.compareTo(expectedType)==0);
|
||||
|
||||
Assert.assertTrue(affectedType.compareTo(testAffectedType)==0);
|
||||
Assert.assertTrue(affectedType.compareTo(createdType)==0);
|
||||
}
|
||||
|
||||
resourceManagement = new ResourceManagement();
|
||||
resourceManagement.setUUID(hostingNodeUUID);
|
||||
|
||||
String hnString = resourceManagement.read();
|
||||
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);
|
||||
|
||||
resourceManagement = new ResourceManagement();
|
||||
resourceManagement.setUUID(eServiceUUID);
|
||||
deleted = resourceManagement.delete();
|
||||
Assert.assertTrue(deleted);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
|
@ -530,32 +365,27 @@ public class BasicTest extends ContextTest {
|
|||
isRelatedToManagement.setElementType(Activates.NAME);
|
||||
isRelatedToManagement.setUUID(activatedUUID);
|
||||
String activatesString = isRelatedToManagement.read();
|
||||
activates = ElementMapper.unmarshal(Activates.class, activatesString);
|
||||
|
||||
Map<UUID, IdentifiableElement> createdInstances = new HashMap<>();
|
||||
createdInstances.putAll(hostingNodeInstances);
|
||||
/* These should not be added due to default propagation constraint for IsRelatedTo relation
|
||||
createdInstances.put(activatedUUID, activates);
|
||||
createdInstances.putAll(eServiceInstances);
|
||||
*/
|
||||
Assert.assertTrue(createdInstances.size()==9);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Activates<HostingNode, EService> unmarshalledActivates = ElementMapper.unmarshal(Activates.class, activatesString);
|
||||
// An intermediate variable has been used to suppress warning here and not to the whole test function
|
||||
activates = unmarshalledActivates;
|
||||
|
||||
hostingNode.attachResource(activates);
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
logger.debug("Switching to another scope");
|
||||
ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE);
|
||||
|
||||
resourceManagement = new ResourceManagement();
|
||||
resourceManagement.setUUID(hostingNodeUUID);
|
||||
addToContextThenTestIfBehaveProperly(hostingNode, true);
|
||||
|
||||
resourceManagement.setDryRunContextSharing(true);
|
||||
UUID contextUUID = ContextUtility.getCurrentSecurityContext().getUUID();
|
||||
Map<UUID, JsonNode> testAffectedInstances = resourceManagement.addToContext(contextUUID);
|
||||
|
||||
resourceManagement = new ResourceManagement();
|
||||
resourceManagement.setUUID(hostingNodeUUID);
|
||||
|
||||
try {
|
||||
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());
|
||||
|
@ -563,49 +393,10 @@ public class BasicTest extends ContextTest {
|
|||
// OK
|
||||
}
|
||||
|
||||
|
||||
resourceManagement.setDryRunContextSharing(false);
|
||||
Map<UUID, JsonNode> affectedInstances = resourceManagement.addToContext(contextUUID);
|
||||
logger.debug("Add to Context afffects the following instances {}", affectedInstances.values());
|
||||
|
||||
SortedSet<UUID> testAffectedInstancesUUID = new TreeSet<>(testAffectedInstances.keySet());
|
||||
SortedSet<UUID> affectedInstancesUUID = new TreeSet<>(affectedInstances.keySet());
|
||||
SortedSet<UUID> createdInstancesUUID = new TreeSet<>(createdInstances.keySet());
|
||||
|
||||
logger.debug("Created Instances where {}", createdInstancesUUID);
|
||||
logger.debug("Test Affected Instances where {}", testAffectedInstancesUUID);
|
||||
logger.debug("Affected Instances are {}", affectedInstancesUUID);
|
||||
|
||||
Assert.assertTrue(testAffectedInstancesUUID.size()==affectedInstancesUUID.size());
|
||||
Assert.assertTrue(createdInstancesUUID.size()==affectedInstancesUUID.size());
|
||||
|
||||
Assert.assertTrue(createdInstancesUUID.containsAll(testAffectedInstancesUUID));
|
||||
Assert.assertTrue(testAffectedInstancesUUID.containsAll(createdInstancesUUID));
|
||||
|
||||
Assert.assertTrue(createdInstancesUUID.containsAll(affectedInstancesUUID));
|
||||
Assert.assertTrue(affectedInstancesUUID.containsAll(createdInstancesUUID));
|
||||
|
||||
Assert.assertTrue(testAffectedInstancesUUID.containsAll(affectedInstancesUUID));
|
||||
Assert.assertTrue(affectedInstancesUUID.containsAll(testAffectedInstancesUUID));
|
||||
|
||||
|
||||
for(UUID uuid : affectedInstances.keySet()) {
|
||||
JsonNode testAffectedJsonNode = testAffectedInstances.get(uuid);
|
||||
String testAffectedType = testAffectedJsonNode.get(Element.CLASS_PROPERTY).asText();
|
||||
|
||||
JsonNode affectedJsonNode = affectedInstances.get(uuid);
|
||||
String affectedType = affectedJsonNode.get(Element.CLASS_PROPERTY).asText();
|
||||
|
||||
IdentifiableElement identifiableElement = createdInstances.get(uuid);
|
||||
String createdType = TypeMapper.getType(identifiableElement.getClass());
|
||||
|
||||
Assert.assertTrue(affectedType.compareTo(testAffectedType)==0);
|
||||
Assert.assertTrue(affectedType.compareTo(createdType)==0);
|
||||
}
|
||||
addToContextThenTestIfBehaveProperly(hostingNode, false);
|
||||
|
||||
resourceManagement = new ResourceManagement();
|
||||
resourceManagement.setUUID(hostingNodeUUID);
|
||||
|
||||
String hnString = resourceManagement.read();
|
||||
HostingNode readHN = ElementMapper.unmarshal(HostingNode.class, hnString);
|
||||
Assert.assertTrue(readHN.getHeader().getUUID().compareTo(hostingNodeUUID) == 0);
|
||||
|
|
Loading…
Reference in New Issue