Fixing tests

This commit is contained in:
Luca Frosini 2021-03-05 18:21:25 +01:00
parent d7ed728c81
commit 56265d35bc
2 changed files with 255 additions and 266 deletions

View File

@ -1,34 +1,25 @@
package org.gcube.informationsystem.resourceregistry.instances.model.entity;
import java.util.Map;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.resourceregistry.ContextTest;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.utils.Utility;
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
import org.gcube.informationsystem.resourceregistry.instances.ERManagementTest;
import org.gcube.informationsystem.resourceregistry.instances.model.entities.FacetManagement;
import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility;
import org.gcube.informationsystem.utils.ElementMapper;
import org.gcube.resourcemanagement.model.impl.entities.facets.SoftwareFacetImpl;
import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
/**
* @author lucafrosini
*
*/
public class FacetManagementTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(ERManagementTest.class);
// private static Logger logger = LoggerFactory.getLogger(ERManagementTest.class);
public static final String GROUP = "InformationSystem";
public static final String NAME = "resource-registry";
@ -71,249 +62,249 @@ public class FacetManagementTest extends ContextTest {
public static void checkAssertion(Facet facet, UUID uuid, boolean create) {
checkHeader(facet, uuid, create);
}
protected <F extends Facet> F create(F facet) throws Exception {
FacetManagement facetManagement = new FacetManagement();
String facetType = Utility.getTypeName(facet);
facetManagement.setElementType(facetType);
facetManagement.setJson(ElementMapper.marshal(facet));
String json = facetManagement.create();
logger.debug("Created : {}", json);
@SuppressWarnings("unchecked")
F createdFacet = (F) ElementMapper.unmarshal(facet.getClass(), json);
logger.debug("Unmarshalled {}", createdFacet);
UUID uuid = null;
if(facet.getHeader() != null) {
uuid = facet.getHeader().getUUID();
}
checkAssertion(createdFacet, uuid, true);
return createdFacet;
}
protected <F extends Facet> F update(F facet) throws Exception {
FacetManagement facetManagement = new FacetManagement();
String facetType = Utility.getTypeName(facet);
facetManagement.setElementType(facetType);
facetManagement.setJson(ElementMapper.marshal(facet));
String json = facetManagement.update();
logger.debug("Updated : {}", json);
@SuppressWarnings("unchecked")
F updatedFacet = (F) ElementMapper.unmarshal(facet.getClass(), json);
logger.debug("Unmarshalled {}", updatedFacet);
UUID uuid = facet.getHeader().getUUID();
checkAssertion(updatedFacet, uuid, false);
return updatedFacet;
}
protected <F extends Facet> F read(F facet) throws Exception {
UUID uuid = facet.getHeader().getUUID();
FacetManagement facetManagement = new FacetManagement();
String facetType = Utility.getTypeName(facet);
facetManagement.setElementType(facetType);
facetManagement.setUUID(uuid);
String json = facetManagement.read().toString();
logger.debug("Read : {}", json);
@SuppressWarnings("unchecked")
F readFacet = (F) ElementMapper.unmarshal(facet.getClass(), json);
logger.debug("Unmarshalled {}", readFacet);
checkAssertion(readFacet, uuid, false);
return readFacet;
}
protected <F extends Facet> void delete(F facet) throws Exception {
FacetManagement facetManagement = new FacetManagement();
String facetType = Utility.getTypeName(facet);
facetManagement.setElementType(facetType);
facetManagement.setUUID(facet.getHeader().getUUID());
facetManagement.delete();
try {
read(facet);
} catch(FacetNotFoundException e) {
logger.info("Facet not found as expected");
}
}
protected <F extends Facet> Map<UUID,JsonNode> addToContext(F facet) throws Exception {
FacetManagement facetManagement = new FacetManagement();
String facetType = Utility.getTypeName(facet);
facetManagement.setElementType(facetType);
facetManagement.setUUID(facet.getHeader().getUUID());
facetManagement.addToContext(ContextUtility.getCurrentSecurityContext().getUUID());
Map<UUID,JsonNode> affectedInstances = facetManagement.getAffectedInstances();
Assert.assertTrue(affectedInstances.containsKey(facet.getHeader().getUUID()));
Assert.assertTrue(affectedInstances.size()==1);
return affectedInstances;
}
protected <F extends Facet> Map<UUID,JsonNode> removeFromContext(F facet) throws Exception {
FacetManagement facetManagement = new FacetManagement();
String facetType = Utility.getTypeName(facet);
facetManagement.setElementType(facetType);
facetManagement.setUUID(facet.getHeader().getUUID());
facetManagement.removeFromContext(ContextUtility.getCurrentSecurityContext().getUUID());
Map<UUID,JsonNode> affectedInstances = facetManagement.getAffectedInstances();
Assert.assertTrue(affectedInstances.containsKey(facet.getHeader().getUUID()));
Assert.assertTrue(affectedInstances.size()==1);
return affectedInstances;
}
interface ActionFunction<F extends Facet> {
void call(F facet) throws Exception;
}
protected <F extends Facet, C extends Exception, E extends Exception> void assertThrow(F facet, Class<C> c,
ActionFunction<F> action) throws Exception {
try {
action.call(facet);
throw new RuntimeException("Expected " + c.getName());
} catch(Exception e) {
if(c.isAssignableFrom(e.getClass())) {
logger.debug("As expected {} has been thrown", c.getName());
return;
}
throw e;
}
}
@Test
public void createUpdateReadDelete() throws Exception {
SoftwareFacet softwareFacet = getSoftwareFacet();
/* Testing Create */
softwareFacet = create(softwareFacet);
checkSoftwareFacetAssertion(softwareFacet, VERSION);
/* Testing Update */
softwareFacet.setVersion(NEW_VERSION);
softwareFacet = update(softwareFacet);
checkSoftwareFacetAssertion(softwareFacet, NEW_VERSION);
/* Testing Read */
softwareFacet = read(softwareFacet);
checkSoftwareFacetAssertion(softwareFacet, NEW_VERSION);
assertThrow(softwareFacet, FacetAlreadyPresentException.class, (SoftwareFacet s) -> {
create(s);
});
/* Testing Delete */
delete(softwareFacet);
/* Testing new Create to check creation with provided UUID */
softwareFacet = create(softwareFacet);
checkSoftwareFacetAssertion(softwareFacet, NEW_VERSION);
delete(softwareFacet);
}
@Test
public void testHierarchy() throws Exception {
/* Setting scope /gcube/devNext/NextNext */
ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE);
SoftwareFacet softwareFacet = getSoftwareFacet();
/* Testing Create */
softwareFacet = create(softwareFacet);
checkSoftwareFacetAssertion(softwareFacet, VERSION);
softwareFacet = update(softwareFacet);
checkSoftwareFacetAssertion(softwareFacet, VERSION);
/* Testing Read */
softwareFacet = read(softwareFacet);
checkSoftwareFacetAssertion(softwareFacet, VERSION);
/* Setting (parent) scope /gcube/devNext */
ContextTest.setContextByName(DEFAULT_TEST_SCOPE);
assertThrow(softwareFacet, FacetAvailableInAnotherContextException.class, (SoftwareFacet s) -> {
read(s);
});
/* Entering hierarchical mode */
ContextUtility.getHierarchicalMode().set(true);
softwareFacet = read(softwareFacet);
checkSoftwareFacetAssertion(softwareFacet, VERSION);
/* Setting (parent of parent) scope /gcube */
ContextTest.setContextByName(PARENT_DEFAULT_TEST_SCOPE);
softwareFacet = read(softwareFacet);
checkSoftwareFacetAssertion(softwareFacet, VERSION);
/* Leaving hierarchical mode */
ContextUtility.getHierarchicalMode().set(false);
assertThrow(softwareFacet, FacetAvailableInAnotherContextException.class, (SoftwareFacet s) -> {
read(s);
});
/* Adding to /gcube. The context are now /gcube and /gcube/devNext/NextNext */
addToContext(softwareFacet);
softwareFacet = read(softwareFacet);
checkSoftwareFacetAssertion(softwareFacet, VERSION);
softwareFacet.setVersion(NEW_VERSION);
softwareFacet = update(softwareFacet);
checkSoftwareFacetAssertion(softwareFacet, NEW_VERSION);
/* Restoring scope /gcube/devNext/NextNext */
ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE);
read(softwareFacet);
checkSoftwareFacetAssertion(softwareFacet, NEW_VERSION);
/* Removing from /gcube/devNext/NextNext. The context is now /gcube */
removeFromContext(softwareFacet);
assertThrow(softwareFacet, FacetAvailableInAnotherContextException.class, (SoftwareFacet s) -> {
read(s);
});
/* Setting (parent) scope /gcube/devNext */
ContextTest.setContextByName(DEFAULT_TEST_SCOPE);
assertThrow(softwareFacet, FacetAvailableInAnotherContextException.class, (SoftwareFacet s) -> {
read(s);
});
/* Entering hierarchical mode */
ContextUtility.getHierarchicalMode().set(true);
assertThrow(softwareFacet, FacetAvailableInAnotherContextException.class, (SoftwareFacet s) -> {
read(s);
});
/* Setting (parent of parent) scope /gcube */
ContextTest.setContextByName(PARENT_DEFAULT_TEST_SCOPE);
// The facet must be readable in hierarchic mode in /gcube because the context
// has been explicitly added
read(softwareFacet);
checkSoftwareFacetAssertion(softwareFacet, NEW_VERSION);
/* Leaving hierarchical mode */
ContextUtility.getHierarchicalMode().set(false);
read(softwareFacet);
checkSoftwareFacetAssertion(softwareFacet, NEW_VERSION);
delete(softwareFacet);
}
//
// protected <F extends Facet> F create(F facet) throws Exception {
// FacetManagement facetManagement = new FacetManagement();
// String facetType = Utility.getTypeName(facet);
// facetManagement.setElementType(facetType);
// facetManagement.setJson(ElementMapper.marshal(facet));
//
// String json = facetManagement.create();
// logger.debug("Created : {}", json);
// @SuppressWarnings("unchecked")
// F createdFacet = (F) ElementMapper.unmarshal(facet.getClass(), json);
// logger.debug("Unmarshalled {}", createdFacet);
//
// UUID uuid = null;
// if(facet.getHeader() != null) {
// uuid = facet.getHeader().getUUID();
// }
// checkAssertion(createdFacet, uuid, true);
// return createdFacet;
// }
//
// protected <F extends Facet> F update(F facet) throws Exception {
// FacetManagement facetManagement = new FacetManagement();
// String facetType = Utility.getTypeName(facet);
// facetManagement.setElementType(facetType);
// facetManagement.setJson(ElementMapper.marshal(facet));
//
// String json = facetManagement.update();
// logger.debug("Updated : {}", json);
// @SuppressWarnings("unchecked")
// F updatedFacet = (F) ElementMapper.unmarshal(facet.getClass(), json);
// logger.debug("Unmarshalled {}", updatedFacet);
//
// UUID uuid = facet.getHeader().getUUID();
// checkAssertion(updatedFacet, uuid, false);
//
// return updatedFacet;
// }
//
// protected <F extends Facet> F read(F facet) throws Exception {
// UUID uuid = facet.getHeader().getUUID();
//
// FacetManagement facetManagement = new FacetManagement();
// String facetType = Utility.getTypeName(facet);
// facetManagement.setElementType(facetType);
// facetManagement.setUUID(uuid);
//
// String json = facetManagement.read().toString();
// logger.debug("Read : {}", json);
// @SuppressWarnings("unchecked")
// F readFacet = (F) ElementMapper.unmarshal(facet.getClass(), json);
// logger.debug("Unmarshalled {}", readFacet);
//
// checkAssertion(readFacet, uuid, false);
//
// return readFacet;
// }
//
// protected <F extends Facet> void delete(F facet) throws Exception {
// FacetManagement facetManagement = new FacetManagement();
// String facetType = Utility.getTypeName(facet);
// facetManagement.setElementType(facetType);
// facetManagement.setUUID(facet.getHeader().getUUID());
//
// facetManagement.delete();
//
// try {
// read(facet);
// } catch(FacetNotFoundException e) {
// logger.info("Facet not found as expected");
// }
// }
//
// protected <F extends Facet> Map<UUID,JsonNode> addToContext(F facet) throws Exception {
// FacetManagement facetManagement = new FacetManagement();
// String facetType = Utility.getTypeName(facet);
// facetManagement.setElementType(facetType);
// facetManagement.setUUID(facet.getHeader().getUUID());
//
// facetManagement.addToContext(ContextUtility.getCurrentSecurityContext().getUUID());
// Map<UUID,JsonNode> affectedInstances = facetManagement.getAffectedInstances();
// Assert.assertTrue(affectedInstances.containsKey(facet.getHeader().getUUID()));
// Assert.assertTrue(affectedInstances.size()==1);
//
// return affectedInstances;
// }
//
// protected <F extends Facet> Map<UUID,JsonNode> removeFromContext(F facet) throws Exception {
// FacetManagement facetManagement = new FacetManagement();
// String facetType = Utility.getTypeName(facet);
// facetManagement.setElementType(facetType);
// facetManagement.setUUID(facet.getHeader().getUUID());
//
// facetManagement.removeFromContext(ContextUtility.getCurrentSecurityContext().getUUID());
// Map<UUID,JsonNode> affectedInstances = facetManagement.getAffectedInstances();
// Assert.assertTrue(affectedInstances.containsKey(facet.getHeader().getUUID()));
// Assert.assertTrue(affectedInstances.size()==1);
//
// return affectedInstances;
// }
//
// interface ActionFunction<F extends Facet> {
// void call(F facet) throws Exception;
// }
//
// protected <F extends Facet, C extends Exception, E extends Exception> void assertThrow(F facet, Class<C> c,
// ActionFunction<F> action) throws Exception {
// try {
// action.call(facet);
// throw new RuntimeException("Expected " + c.getName());
// } catch(Exception e) {
// if(c.isAssignableFrom(e.getClass())) {
// logger.debug("As expected {} has been thrown", c.getName());
// return;
// }
// throw e;
// }
// }
//
// @Test
// public void createUpdateReadDelete() throws Exception {
// SoftwareFacet softwareFacet = getSoftwareFacet();
//
// /* Testing Create */
// softwareFacet = create(softwareFacet);
// checkSoftwareFacetAssertion(softwareFacet, VERSION);
//
// /* Testing Update */
// softwareFacet.setVersion(NEW_VERSION);
// softwareFacet = update(softwareFacet);
// checkSoftwareFacetAssertion(softwareFacet, NEW_VERSION);
//
// /* Testing Read */
// softwareFacet = read(softwareFacet);
// checkSoftwareFacetAssertion(softwareFacet, NEW_VERSION);
//
// assertThrow(softwareFacet, FacetAlreadyPresentException.class, (SoftwareFacet s) -> {
// create(s);
// });
//
// /* Testing Delete */
// delete(softwareFacet);
//
// /* Testing new Create to check creation with provided UUID */
// softwareFacet = create(softwareFacet);
// checkSoftwareFacetAssertion(softwareFacet, NEW_VERSION);
//
// delete(softwareFacet);
//
// }
//
// @Test
// public void testHierarchy() throws Exception {
// /* Setting scope /gcube/devNext/NextNext */
// ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE);
//
// SoftwareFacet softwareFacet = getSoftwareFacet();
//
// /* Testing Create */
// softwareFacet = create(softwareFacet);
// checkSoftwareFacetAssertion(softwareFacet, VERSION);
//
// softwareFacet = update(softwareFacet);
// checkSoftwareFacetAssertion(softwareFacet, VERSION);
//
// /* Testing Read */
// softwareFacet = read(softwareFacet);
// checkSoftwareFacetAssertion(softwareFacet, VERSION);
//
// /* Setting (parent) scope /gcube/devNext */
// ContextTest.setContextByName(DEFAULT_TEST_SCOPE);
//
// assertThrow(softwareFacet, FacetAvailableInAnotherContextException.class, (SoftwareFacet s) -> {
// read(s);
// });
//
// /* Entering hierarchical mode */
// ContextUtility.getHierarchicalMode().set(true);
//
// softwareFacet = read(softwareFacet);
// checkSoftwareFacetAssertion(softwareFacet, VERSION);
//
// /* Setting (parent of parent) scope /gcube */
// ContextTest.setContextByName(PARENT_DEFAULT_TEST_SCOPE);
//
// softwareFacet = read(softwareFacet);
// checkSoftwareFacetAssertion(softwareFacet, VERSION);
//
// /* Leaving hierarchical mode */
// ContextUtility.getHierarchicalMode().set(false);
//
// assertThrow(softwareFacet, FacetAvailableInAnotherContextException.class, (SoftwareFacet s) -> {
// read(s);
// });
//
// /* Adding to /gcube. The context are now /gcube and /gcube/devNext/NextNext */
// addToContext(softwareFacet);
// softwareFacet = read(softwareFacet);
// checkSoftwareFacetAssertion(softwareFacet, VERSION);
//
// softwareFacet.setVersion(NEW_VERSION);
// softwareFacet = update(softwareFacet);
// checkSoftwareFacetAssertion(softwareFacet, NEW_VERSION);
//
// /* Restoring scope /gcube/devNext/NextNext */
// ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE);
// read(softwareFacet);
// checkSoftwareFacetAssertion(softwareFacet, NEW_VERSION);
//
// /* Removing from /gcube/devNext/NextNext. The context is now /gcube */
// removeFromContext(softwareFacet);
//
// assertThrow(softwareFacet, FacetAvailableInAnotherContextException.class, (SoftwareFacet s) -> {
// read(s);
// });
//
// /* Setting (parent) scope /gcube/devNext */
// ContextTest.setContextByName(DEFAULT_TEST_SCOPE);
// assertThrow(softwareFacet, FacetAvailableInAnotherContextException.class, (SoftwareFacet s) -> {
// read(s);
// });
//
// /* Entering hierarchical mode */
// ContextUtility.getHierarchicalMode().set(true);
//
// assertThrow(softwareFacet, FacetAvailableInAnotherContextException.class, (SoftwareFacet s) -> {
// read(s);
// });
//
// /* Setting (parent of parent) scope /gcube */
// ContextTest.setContextByName(PARENT_DEFAULT_TEST_SCOPE);
// // The facet must be readable in hierarchic mode in /gcube because the context
// // has been explicitly added
// read(softwareFacet);
// checkSoftwareFacetAssertion(softwareFacet, NEW_VERSION);
//
// /* Leaving hierarchical mode */
// ContextUtility.getHierarchicalMode().set(false);
//
// read(softwareFacet);
// checkSoftwareFacetAssertion(softwareFacet, NEW_VERSION);
//
// delete(softwareFacet);
// }
}

View File

@ -20,7 +20,6 @@ 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;
@ -29,13 +28,10 @@ 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;
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.relations.isrelatedto.ActivatesImpl;
import org.gcube.resourcemanagement.model.reference.entities.facets.CPUFacet;
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;
@ -52,6 +48,7 @@ public class BasicTest extends MultiContextTest {
private static Logger logger = LoggerFactory
.getLogger(BasicTest.class);
/*
@Test
public void testDifferentScopes() throws Exception {
ContextTest.setContextByName(DEFAULT_TEST_SCOPE);
@ -77,7 +74,7 @@ public class BasicTest extends MultiContextTest {
String readJson = facetManagement.read().toString();
logger.debug("Read : {}", readJson);
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ * /
logger.debug("Switching to another scope");
ContextTest.setContextByName(PARENT_DEFAULT_TEST_SCOPE);
@ -93,7 +90,7 @@ public class BasicTest extends MultiContextTest {
logger.debug("Good the facet created in the default context is not visible in an alternative context");
}
/* ---------------- entering hierarchic mode */
/* ---------------- entering hierarchic mode * /
ContextUtility.getHierarchicalMode().set(true);
@ -104,7 +101,7 @@ public class BasicTest extends MultiContextTest {
ContextUtility.getHierarchicalMode().set(false);
/* ---------------- leaving hierarchic mode */
/* ---------------- leaving hierarchic mode * /
cpuFacet.setAdditionalProperty("My", "Test");
@ -121,7 +118,7 @@ public class BasicTest extends MultiContextTest {
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);
@ -142,6 +139,7 @@ public class BasicTest extends MultiContextTest {
facetManagement.delete();
}
*/
@Test
public void testResource() throws Exception {