Instances test clean the environment before and after each test

This commit is contained in:
Luca Frosini 2021-03-09 12:58:46 +01:00
parent abd1584901
commit ce3d021845
9 changed files with 469 additions and 522 deletions

View File

@ -28,7 +28,9 @@ import org.gcube.informationsystem.model.reference.properties.PropagationConstra
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.exceptions.entity.resource.ResourceNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.utils.Utility;
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.ConsistsOfManagement;
@ -71,7 +73,9 @@ import org.gcube.resourcemanagement.model.reference.relations.consistsof.HasPers
import org.gcube.resourcemanagement.model.reference.relations.consistsof.HasVolatileMemory;
import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy;
import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Activates;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -88,6 +92,23 @@ public class ERManagementTest extends ContextTest {
public static final String VERSION = "1.0.0";
public static final String NEW_VERSION = "2.0.0";
@Before
@After
public void cleanInstances() throws Exception {
// Clean the environment first to avoid error if a previous tests fails without cleaning the env
ResourceManagement rm = (ResourceManagement) ElementManagementUtility.getERManagement(Resource.NAME);
String all = rm.all(true);
List<Resource> allResources = ElementMapper.unmarshalList(Resource.class, all);
for(Resource r : allResources) {
try {
ERManagementTest.deleteResource(r);
}catch (ResourceNotFoundException e) {
// A resource could be already deleted deleting another resource giving the propagation constraint
}
}
}
public static SoftwareFacet getSoftwareFacet() {
SoftwareFacet softwareFacet = new SoftwareFacetImpl();
softwareFacet.setGroup(GROUP);

View File

@ -4,7 +4,6 @@ import java.security.Key;
import org.gcube.informationsystem.model.impl.properties.EncryptedImpl;
import org.gcube.informationsystem.model.reference.properties.Encrypted;
import org.gcube.informationsystem.resourceregistry.ContextTest;
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement;
import org.gcube.informationsystem.utils.ElementMapper;
@ -17,7 +16,7 @@ import org.junit.Test;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class EncryptionTest extends ContextTest {
public class EncryptionTest extends ERManagementTest {
public static final String PLAIN_VALUE = "plain-value";

View File

@ -8,7 +8,6 @@ import org.gcube.informationsystem.model.impl.relations.ConsistsOfImpl;
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.resourceregistry.ContextTest;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException;
@ -39,7 +38,7 @@ import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class InvalidOperationTest extends ContextTest {
public class InvalidOperationTest extends ERManagementTest {
private static Logger logger = LoggerFactory.getLogger(InvalidOperationTest.class);

View File

@ -1,506 +0,0 @@
package org.gcube.informationsystem.resourceregistry.instances;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.entities.Resource;
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.exceptions.entity.resource.ResourceNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException;
import org.gcube.informationsystem.resourceregistry.api.utils.Utility;
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement;
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility;
import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.relations.ConsistsOfManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement;
import org.gcube.informationsystem.utils.ElementMapper;
import org.gcube.resourcemanagement.model.impl.entities.facets.IdentifierFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.SimplePropertyFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.resources.ConfigurationImpl;
import org.gcube.resourcemanagement.model.impl.relations.consistsof.IsIdentifiedByImpl;
import org.gcube.resourcemanagement.model.reference.entities.facets.IdentifierFacet;
import org.gcube.resourcemanagement.model.reference.entities.facets.IdentifierFacet.IdentificationType;
import org.gcube.resourcemanagement.model.reference.entities.facets.SimplePropertyFacet;
import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet;
import org.gcube.resourcemanagement.model.reference.entities.resources.Configuration;
import org.gcube.resourcemanagement.model.reference.entities.resources.ConfigurationTemplate;
import org.gcube.resourcemanagement.model.reference.entities.resources.EService;
import org.gcube.resourcemanagement.model.reference.entities.resources.GCubeResource;
import org.gcube.resourcemanagement.model.reference.entities.resources.HostingNode;
import org.gcube.resourcemanagement.model.reference.entities.resources.Service;
import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy;
import org.junit.Assert;
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 QueryTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(QueryTest.class);
private static final String EXTRA = "EXTRA";
@Test
public void testGetAll() throws Exception {
// Clean the environment first to avoid error if a previous tests fails without cleaning the env
ResourceManagement rm = (ResourceManagement) ElementManagementUtility.getERManagement(Resource.NAME);
String all = rm.all(true);
List<Resource> allResources = ElementMapper.unmarshalList(Resource.class, all);
for(Resource r : allResources) {
try {
ERManagementTest.deleteResource(r);
}catch (ResourceNotFoundException e) {
// A resource could be already deleted deleting another resource giving the propagation constraint
}
}
Map<String, List<Resource>> resources = new HashMap<>();
try {
final int MAX = 5;
int typeNumber = 0;
for (int i = 0; i < MAX; i++) {
Map<String, Resource> map = ERManagementTest.createHostingNodeAndEService();
if (typeNumber == 0) {
typeNumber = map.size();
}
for (String key : map.keySet()) {
if (!resources.containsKey(key)) {
resources.put(key, new ArrayList<Resource>());
}
resources.get(key).add(map.get(key));
}
}
/* Getting all instances of created specific Resources */
for (String key : resources.keySet()) {
ResourceManagement resourceManagement = (ResourceManagement) ElementManagementUtility.getERManagement(key);
String json = resourceManagement.all(false);
List<Resource> list = ElementMapper.unmarshalList(Resource.class, json);
logger.debug("{} are {} : {} ", key, list.size(), list);
Assert.assertTrue(list.size() == MAX);
}
/* Getting all Resources polymorphic and non polymorphic */
ResourceManagement resourceManagement = (ResourceManagement) ElementManagementUtility
.getERManagement(Resource.NAME);
String json = resourceManagement.all(true);
List<Resource> list = ElementMapper.unmarshalList(Resource.class, json);
logger.debug("{} are {} : {} ", Resource.NAME, list.size(), list);
Assert.assertTrue(list.size() == (MAX * typeNumber));
json = resourceManagement.all(false);
list = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(list.size() == 0);
/* Getting all IsRelatedTo polymorphic and non polymorphic */
IsRelatedToManagement isRelatedToManagement = (IsRelatedToManagement) ElementManagementUtility
.getERManagement(IsRelatedTo.NAME);
json = isRelatedToManagement.all(true);
List<Resource> resourcesList = ElementMapper.unmarshalList(Resource.class, json);
logger.debug("{} are {} : {} ", IsRelatedTo.NAME, resourcesList.size(), resourcesList);
Assert.assertTrue(resourcesList.size() == MAX);
json = isRelatedToManagement.all(false);
resourcesList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourcesList.size() == 0);
/* Getting all ConsistsOf polymorphic and non polymorphic */
ConsistsOfManagement consistsOfManagement = (ConsistsOfManagement) ElementManagementUtility
.getERManagement(ConsistsOf.NAME);
json = consistsOfManagement.all(true);
List<Resource> consistsOfPolimorphicList = ElementMapper.unmarshalList(Resource.class, json);
logger.debug("{} are {} : {} ", IsRelatedTo.NAME, consistsOfPolimorphicList.size(), consistsOfPolimorphicList);
json = consistsOfManagement.all(false);
List<Resource> consistsOfNonPolimorphicList = ElementMapper.unmarshalList(Resource.class, json);
logger.debug("{} are {} : {} ", IsRelatedTo.NAME, consistsOfNonPolimorphicList.size(),
consistsOfNonPolimorphicList);
Assert.assertTrue(consistsOfPolimorphicList.size() >= consistsOfNonPolimorphicList.size());
} finally {
/* Removing created Entity and Relation to have a clean DB */
List<Resource> resourceList = resources.get(HostingNode.NAME);
for (Resource r : resourceList) {
ERManagementTest.deleteResource(r);
}
}
}
@Test
public void testGetAllFrom() throws Exception {
// Clean the environment first to avoid error if a previous tests fails without cleaning the env
ResourceManagement rm = (ResourceManagement) ElementManagementUtility.getERManagement(Resource.NAME);
String all = rm.all(true);
List<Resource> allResources = ElementMapper.unmarshalList(Resource.class, all);
for(Resource r : allResources) {
try {
ERManagementTest.deleteResource(r);
}catch (ResourceNotFoundException e) {
// A resource could be already deleted deleting another resource giving the propagation constraint
}
}
Map<String, Resource> map = ERManagementTest.createHostingNodeAndEService();
EService eService = (EService) map.get(EService.NAME);
HostingNode hostingNode = (HostingNode) map.get(HostingNode.NAME);
try {
UUID eServiceUUID = eService.getHeader().getUUID();
UUID hostingNodeUUID = hostingNode.getHeader().getUUID();
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setElementType(Service.NAME);
boolean includeRelation = false;
/* Getting Hosting Node */
String json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.BOTH, true,
null, includeRelation);
List<Resource> resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 1);
Resource resource = resourceList.get(0);
Assert.assertTrue(resource.getHeader().getUUID().compareTo(hostingNodeUUID) == 0);
json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.OUT, true, null,
includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 1);
resource = resourceList.get(0);
Assert.assertTrue(resource.getHeader().getUUID().compareTo(hostingNodeUUID) == 0);
json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.IN, true, null,
includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 0);
json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.BOTH, false, null,
includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 0);
json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.OUT, false, null,
includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 0);
json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.IN, false, null,
includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 0);
/* END Getting Hosting Node */
/* Getting EService */
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.BOTH, true,
null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 1);
Assert.assertTrue(resourceList.get(0).getHeader().getUUID().compareTo(eServiceUUID) == 0);
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.OUT, true, null,
includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 0);
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.IN, true, null,
includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 1);
Assert.assertTrue(resourceList.get(0).getHeader().getUUID().compareTo(eServiceUUID) == 0);
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.BOTH, false,
null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 0);
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.OUT, false,
null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 0);
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.IN, false, null,
includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 0);
/* END Getting HostingNode */
Facet identificationFacet = eService.getIdentificationFacets().get(0);
UUID identificationFacetUUID = identificationFacet.getHeader().getUUID();
/* EService --ConsistsOf--> SoftwareFacet */
try {
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID,
ODirection.BOTH, true, null, includeRelation);
} catch (InvalidQueryException e) {
// Ok expected
}
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.OUT,
true, null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 1);
resource = resourceList.get(0);
Facet targetIdentificationFacet = ((GCubeResource) resource).getIdentificationFacets().get(0);
Assert.assertTrue(resource.getHeader().getUUID().compareTo(eServiceUUID) == 0);
Assert.assertTrue(targetIdentificationFacet.getHeader().getUUID().compareTo(identificationFacetUUID) == 0);
try {
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.IN,
true, null, includeRelation);
throw new Exception("Expected InvalidQueryException");
} catch (InvalidQueryException e) {
// Ok expected
}
try {
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID,
ODirection.BOTH, false, null, includeRelation);
throw new Exception("Expected InvalidQueryException");
} catch (InvalidQueryException e) {
// Ok expected
}
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.OUT,
false, null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 0);
try {
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.IN,
false, null, includeRelation);
throw new Exception("Expected InvalidQueryException");
} catch (InvalidQueryException e) {
// Ok expected
}
/* END EService --ConsistsOf--> SoftwareFacet */
} finally {
ERManagementTest.deleteResource(eService);
ERManagementTest.deleteResource(hostingNode);
}
}
@Test
public void testQuery() throws Exception {
// Clean the environment first to avoid error if a previous tests fails without cleaning the env
ResourceManagement rm = (ResourceManagement) ElementManagementUtility.getERManagement(Resource.NAME);
String all = rm.all(true);
List<Resource> allResources = ElementMapper.unmarshalList(Resource.class, all);
for(Resource r : allResources) {
try {
ERManagementTest.deleteResource(r);
}catch (ResourceNotFoundException e) {
// A resource could be already deleted deleting another resource giving the propagation constraint
}
}
String relationType = ConsistsOf.NAME;
String facetType = SoftwareFacet.NAME;
Map<String, String> constraint = new HashMap<>();
constraint.put(SoftwareFacet.GROUP_PROPERTY, "Gis");
constraint.put(SoftwareFacet.NAME_PROPERTY, "Thredds");
String type = Service.NAME;
@SuppressWarnings("rawtypes")
ElementManagement erManagement = ElementManagementUtility.getERManagement(type);
boolean includeRelation = false;
if (erManagement instanceof ResourceManagement) {
boolean[] booleans = new boolean[] {true, false};
for(boolean bool : booleans) {
String ret = ((ResourceManagement) erManagement).query(relationType, facetType, null, ODirection.OUT, bool, constraint, includeRelation);
logger.debug("Result of query for {}polymorphic {} --{}--> {} with constaint {} is {}", bool ? "" : "NOT ",
type, relationType, facetType, constraint, ret);
}
}
}
protected Configuration createConfiguration(String name, Object value) throws Exception {
Configuration configuration = new ConfigurationImpl();
IdentifierFacet identifierFacet = new IdentifierFacetImpl();
identifierFacet.setValue(name);
identifierFacet.setType(IdentificationType.STRING);
if(value!=null) {
identifierFacet.setAdditionalProperty(EXTRA, value);
}
IsIdentifiedBy<Configuration, IdentifierFacet> isIdentifiedBy = new IsIdentifiedByImpl<Configuration, IdentifierFacet>(configuration, identifierFacet);
configuration.addFacet(isIdentifiedBy);
SimplePropertyFacet simplePropertyFacet = new SimplePropertyFacetImpl();
simplePropertyFacet.setName("test");
simplePropertyFacet.setValue("test");
configuration.addFacet(simplePropertyFacet);
return ERManagementTest.createResource(configuration);
}
@Test
public void getAllResourcesHavingFacet() throws ObjectNotFound, Exception {
// Clean the environment first to avoid error if a previous tests fails without cleaning the env
ResourceManagement rm = (ResourceManagement) ElementManagementUtility.getERManagement(Resource.NAME);
String all = rm.all(true);
List<Resource> allResources = ElementMapper.unmarshalList(Resource.class, all);
for(Resource r : allResources) {
try {
ERManagementTest.deleteResource(r);
}catch (ResourceNotFoundException e) {
// A resource could be already deleted deleting another resource giving the propagation constraint
}
}
String[] names = new String[]{"MyTest", "MyTest", "Aux", "MyID", "MyID"};
Object[] additionalProperties = new Object[] {5, 5.0, false, "test", null};
List<Configuration> createdConfigurations = new ArrayList<>();
for(int i=0; i<names.length; i++) {
createdConfigurations.add(createConfiguration(names[i], additionalProperties[i]));
}
try {
String parentResourceType = Utility.getTypeName(ConfigurationTemplate.class);
String resourceType = Utility.getTypeName(Configuration.class);
String relationType = Utility.getTypeName(IsIdentifiedBy.class);
String referenceType = Utility.getTypeName(IdentifierFacet.class);
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setElementType(resourceType);
ResourceManagement parentResourceManagement = new ResourceManagement();
parentResourceManagement.setElementType(parentResourceType);
ODirection directionEnum = ODirection.OUT;
UUID refereceUUID = null;
Boolean[] polymorphics = new Boolean[] {true, false};
int expectedSize = 1;
Set<UUID> expectedUUIDFor2Results = new HashSet<>();
expectedUUIDFor2Results.add(createdConfigurations.get(3).getHeader().getUUID());
expectedUUIDFor2Results.add(createdConfigurations.get(4).getHeader().getUUID());
for(int i=0; i<names.length; i++) {
expectedSize = 1;
if(i==4) {
expectedSize = 2;
}
Configuration expected = createdConfigurations.get(i);
UUID expectedUUID = expected.getHeader().getUUID();
boolean includeRelation = false;
for(Boolean polymorphic : polymorphics) {
Map<String,String> constraint = new HashMap<>();
constraint.put(IdentifierFacet.VALUE_PROPERTY, names[i]);
if(additionalProperties[i]!=null) {
constraint.put(EXTRA, additionalProperties[i].toString());
}
logger.debug("Going to query for {}polymorphic {} --{}--> {} with constaints {}", polymorphic ? "" : "NOT ",
resourceType, relationType, referenceType, constraint);
String ret = resourceManagement.query(relationType, referenceType, refereceUUID, directionEnum,
polymorphic, constraint, includeRelation);
List<Configuration> list = ElementMapper.unmarshalList(Configuration.class, ret);
Assert.assertTrue(list.size()==expectedSize);
if(expectedSize==1) {
Configuration configuration = list.get(0);
UUID gotUUID = configuration.getHeader().getUUID();
Assert.assertTrue(gotUUID.compareTo(expectedUUID)==0);
}else {
Set<UUID> gotUUIDs = new HashSet<>();
for(Configuration c : list) {
gotUUIDs.add(c.getHeader().getUUID());
}
Assert.assertTrue(gotUUIDs.containsAll(expectedUUIDFor2Results));
Assert.assertTrue(expectedUUIDFor2Results.containsAll(gotUUIDs));
}
if(polymorphic) {
logger.debug("Going to query for polymorphic {} --{}--> {} with constaints {}",
parentResourceType, relationType, referenceType, constraint);
String retPolimorphic = parentResourceManagement.query(relationType, referenceType, refereceUUID, directionEnum,
polymorphic, constraint, includeRelation);
List<ConfigurationTemplate> listPolimorphic = ElementMapper.unmarshalList(ConfigurationTemplate.class, retPolimorphic);
Assert.assertTrue(listPolimorphic.size()==expectedSize);
if(expectedSize==1) {
ConfigurationTemplate configurationTemplate = listPolimorphic.get(0);
Assert.assertTrue(configurationTemplate instanceof Configuration);
UUID gotPolimorphicUUID = configurationTemplate.getHeader().getUUID();
Assert.assertTrue(gotPolimorphicUUID.compareTo(expectedUUID)==0);
}else {
Set<UUID> gotUUIDs = new HashSet<>();
for(ConfigurationTemplate c : listPolimorphic) {
Assert.assertTrue(c instanceof Configuration);
gotUUIDs.add(c.getHeader().getUUID());
}
Assert.assertTrue(gotUUIDs.containsAll(expectedUUIDFor2Results));
Assert.assertTrue(expectedUUIDFor2Results.containsAll(gotUUIDs));
}
}
}
}
} finally {
for(Configuration configuration : createdConfigurations) {
ERManagementTest.deleteResource(configuration);
}
}
}
}

View File

@ -42,7 +42,6 @@ import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint;
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.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement;
import org.gcube.informationsystem.utils.ElementMapper;
@ -79,7 +78,7 @@ import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class SmartgearResourcesTest extends ContextTest {
public class SmartgearResourcesTest extends ERManagementTest {
private static Logger logger = LoggerFactory
.getLogger(SmartgearResourcesTest.class);

View File

@ -10,7 +10,7 @@ import org.gcube.resourcemanagement.model.reference.entities.resources.EService;
import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy;
import org.junit.Test;
public class AddToContextTest {
public class AddToContextTest extends MultiContextTest {
@Test
public void testCreateEServiceAndRemoveFromContextRequiredFacet() throws Exception {

View File

@ -36,7 +36,7 @@ import org.slf4j.LoggerFactory;
import com.orientechnologies.orient.core.record.ODirection;
public class MultiContextTest extends ContextTest {
public class MultiContextTest extends ERManagementTest {
private static Logger logger = LoggerFactory.getLogger(MultiContextTest.class);

View File

@ -1,24 +1,61 @@
/**
*
*/
package org.gcube.informationsystem.resourceregistry.query;
import org.gcube.informationsystem.resourceregistry.ContextTest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException;
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
import org.gcube.informationsystem.resourceregistry.api.utils.Utility;
import org.gcube.informationsystem.resourceregistry.instances.ERManagementTest;
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement;
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility;
import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.relations.ConsistsOfManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement;
import org.gcube.informationsystem.utils.ElementMapper;
import org.gcube.resourcemanagement.model.impl.entities.facets.IdentifierFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.SimplePropertyFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.resources.ConfigurationImpl;
import org.gcube.resourcemanagement.model.impl.relations.consistsof.IsIdentifiedByImpl;
import org.gcube.resourcemanagement.model.reference.entities.facets.IdentifierFacet;
import org.gcube.resourcemanagement.model.reference.entities.facets.IdentifierFacet.IdentificationType;
import org.gcube.resourcemanagement.model.reference.entities.facets.SimplePropertyFacet;
import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet;
import org.gcube.resourcemanagement.model.reference.entities.resources.Configuration;
import org.gcube.resourcemanagement.model.reference.entities.resources.ConfigurationTemplate;
import org.gcube.resourcemanagement.model.reference.entities.resources.EService;
import org.gcube.resourcemanagement.model.reference.entities.resources.GCubeResource;
import org.gcube.resourcemanagement.model.reference.entities.resources.HostingNode;
import org.gcube.resourcemanagement.model.reference.entities.resources.Service;
import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy;
import org.junit.Assert;
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 QueryTest extends ContextTest {
public class QueryTest extends ERManagementTest {
private static Logger logger = LoggerFactory.getLogger(QueryTest.class);
private static final String EXTRA = "EXTRA";
@Test
public void testQuery() throws InvalidQueryException{
public void testRawQuery() throws InvalidQueryException {
QueryImpl queryImpl = new QueryImpl();
String query = "select from SoftwareFacet";
@ -31,4 +68,402 @@ public class QueryTest extends ContextTest {
logger.debug(ret);
}
@Test
public void testGetAll() throws Exception {
Map<String, List<Resource>> resources = new HashMap<>();
try {
final int MAX = 5;
int typeNumber = 0;
for (int i = 0; i < MAX; i++) {
Map<String, Resource> map = ERManagementTest.createHostingNodeAndEService();
if (typeNumber == 0) {
typeNumber = map.size();
}
for (String key : map.keySet()) {
if (!resources.containsKey(key)) {
resources.put(key, new ArrayList<Resource>());
}
resources.get(key).add(map.get(key));
}
}
/* Getting all instances of created specific Resources */
for (String key : resources.keySet()) {
ResourceManagement resourceManagement = (ResourceManagement) ElementManagementUtility.getERManagement(key);
String json = resourceManagement.all(false);
List<Resource> list = ElementMapper.unmarshalList(Resource.class, json);
logger.debug("{} are {} : {} ", key, list.size(), list);
Assert.assertTrue(list.size() == MAX);
}
/* Getting all Resources polymorphic and non polymorphic */
ResourceManagement resourceManagement = (ResourceManagement) ElementManagementUtility
.getERManagement(Resource.NAME);
String json = resourceManagement.all(true);
List<Resource> list = ElementMapper.unmarshalList(Resource.class, json);
logger.debug("{} are {} : {} ", Resource.NAME, list.size(), list);
Assert.assertTrue(list.size() == (MAX * typeNumber));
json = resourceManagement.all(false);
list = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(list.size() == 0);
/* Getting all IsRelatedTo polymorphic and non polymorphic */
IsRelatedToManagement isRelatedToManagement = (IsRelatedToManagement) ElementManagementUtility
.getERManagement(IsRelatedTo.NAME);
json = isRelatedToManagement.all(true);
List<Resource> resourcesList = ElementMapper.unmarshalList(Resource.class, json);
logger.debug("{} are {} : {} ", IsRelatedTo.NAME, resourcesList.size(), resourcesList);
Assert.assertTrue(resourcesList.size() == MAX);
json = isRelatedToManagement.all(false);
resourcesList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourcesList.size() == 0);
/* Getting all ConsistsOf polymorphic and non polymorphic */
ConsistsOfManagement consistsOfManagement = (ConsistsOfManagement) ElementManagementUtility
.getERManagement(ConsistsOf.NAME);
json = consistsOfManagement.all(true);
List<Resource> consistsOfPolimorphicList = ElementMapper.unmarshalList(Resource.class, json);
logger.debug("{} are {} : {} ", IsRelatedTo.NAME, consistsOfPolimorphicList.size(), consistsOfPolimorphicList);
json = consistsOfManagement.all(false);
List<Resource> consistsOfNonPolimorphicList = ElementMapper.unmarshalList(Resource.class, json);
logger.debug("{} are {} : {} ", IsRelatedTo.NAME, consistsOfNonPolimorphicList.size(),
consistsOfNonPolimorphicList);
Assert.assertTrue(consistsOfPolimorphicList.size() >= consistsOfNonPolimorphicList.size());
} finally {
/* Removing created Entity and Relation to have a clean DB */
List<Resource> resourceList = resources.get(HostingNode.NAME);
for (Resource r : resourceList) {
ERManagementTest.deleteResource(r);
}
}
}
@Test
public void testGetAllFrom() throws Exception {
Map<String, Resource> map = ERManagementTest.createHostingNodeAndEService();
EService eService = (EService) map.get(EService.NAME);
HostingNode hostingNode = (HostingNode) map.get(HostingNode.NAME);
try {
UUID eServiceUUID = eService.getHeader().getUUID();
UUID hostingNodeUUID = hostingNode.getHeader().getUUID();
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setElementType(Service.NAME);
boolean includeRelation = false;
/* Getting Hosting Node */
String json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.BOTH, true,
null, includeRelation);
List<Resource> resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 1);
Resource resource = resourceList.get(0);
Assert.assertTrue(resource.getHeader().getUUID().compareTo(hostingNodeUUID) == 0);
json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.OUT, true, null,
includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 1);
resource = resourceList.get(0);
Assert.assertTrue(resource.getHeader().getUUID().compareTo(hostingNodeUUID) == 0);
json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.IN, true, null,
includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 0);
json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.BOTH, false, null,
includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 0);
json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.OUT, false, null,
includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 0);
json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.IN, false, null,
includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 0);
/* END Getting Hosting Node */
/* Getting EService */
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.BOTH, true,
null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 1);
Assert.assertTrue(resourceList.get(0).getHeader().getUUID().compareTo(eServiceUUID) == 0);
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.OUT, true, null,
includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 0);
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.IN, true, null,
includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 1);
Assert.assertTrue(resourceList.get(0).getHeader().getUUID().compareTo(eServiceUUID) == 0);
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.BOTH, false,
null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 0);
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.OUT, false,
null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 0);
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.IN, false, null,
includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 0);
/* END Getting HostingNode */
Facet identificationFacet = eService.getIdentificationFacets().get(0);
UUID identificationFacetUUID = identificationFacet.getHeader().getUUID();
/* EService --ConsistsOf--> SoftwareFacet */
try {
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID,
ODirection.BOTH, true, null, includeRelation);
} catch (InvalidQueryException e) {
// Ok expected
}
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.OUT,
true, null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 1);
resource = resourceList.get(0);
Facet targetIdentificationFacet = ((GCubeResource) resource).getIdentificationFacets().get(0);
Assert.assertTrue(resource.getHeader().getUUID().compareTo(eServiceUUID) == 0);
Assert.assertTrue(targetIdentificationFacet.getHeader().getUUID().compareTo(identificationFacetUUID) == 0);
try {
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.IN,
true, null, includeRelation);
throw new Exception("Expected InvalidQueryException");
} catch (InvalidQueryException e) {
// Ok expected
}
try {
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID,
ODirection.BOTH, false, null, includeRelation);
throw new Exception("Expected InvalidQueryException");
} catch (InvalidQueryException e) {
// Ok expected
}
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.OUT,
false, null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size() == 0);
try {
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.IN,
false, null, includeRelation);
throw new Exception("Expected InvalidQueryException");
} catch (InvalidQueryException e) {
// Ok expected
}
/* END EService --ConsistsOf--> SoftwareFacet */
} finally {
ERManagementTest.deleteResource(eService);
ERManagementTest.deleteResource(hostingNode);
}
}
@Test
public void testQuery() throws Exception {
String relationType = ConsistsOf.NAME;
String facetType = SoftwareFacet.NAME;
Map<String, String> constraint = new HashMap<>();
constraint.put(SoftwareFacet.GROUP_PROPERTY, "Gis");
constraint.put(SoftwareFacet.NAME_PROPERTY, "Thredds");
String type = Service.NAME;
@SuppressWarnings("rawtypes")
ElementManagement erManagement = ElementManagementUtility.getERManagement(type);
boolean includeRelation = false;
if (erManagement instanceof ResourceManagement) {
boolean[] booleans = new boolean[] {true, false};
for(boolean bool : booleans) {
String ret = ((ResourceManagement) erManagement).query(relationType, facetType, null, ODirection.OUT, bool, constraint, includeRelation);
logger.debug("Result of query for {}polymorphic {} --{}--> {} with constaint {} is {}", bool ? "" : "NOT ",
type, relationType, facetType, constraint, ret);
}
}
}
protected Configuration createConfiguration(String name, Object value) throws Exception {
Configuration configuration = new ConfigurationImpl();
IdentifierFacet identifierFacet = new IdentifierFacetImpl();
identifierFacet.setValue(name);
identifierFacet.setType(IdentificationType.STRING);
if(value!=null) {
identifierFacet.setAdditionalProperty(EXTRA, value);
}
IsIdentifiedBy<Configuration, IdentifierFacet> isIdentifiedBy = new IsIdentifiedByImpl<Configuration, IdentifierFacet>(configuration, identifierFacet);
configuration.addFacet(isIdentifiedBy);
SimplePropertyFacet simplePropertyFacet = new SimplePropertyFacetImpl();
simplePropertyFacet.setName("test");
simplePropertyFacet.setValue("test");
configuration.addFacet(simplePropertyFacet);
return ERManagementTest.createResource(configuration);
}
@Test
public void getAllResourcesHavingFacet() throws ObjectNotFound, Exception {
String[] names = new String[]{"MyTest", "MyTest", "Aux", "MyID", "MyID"};
Object[] additionalProperties = new Object[] {5, 5.0, false, "test", null};
List<Configuration> createdConfigurations = new ArrayList<>();
for(int i=0; i<names.length; i++) {
createdConfigurations.add(createConfiguration(names[i], additionalProperties[i]));
}
try {
String parentResourceType = Utility.getTypeName(ConfigurationTemplate.class);
String resourceType = Utility.getTypeName(Configuration.class);
String relationType = Utility.getTypeName(IsIdentifiedBy.class);
String referenceType = Utility.getTypeName(IdentifierFacet.class);
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setElementType(resourceType);
ResourceManagement parentResourceManagement = new ResourceManagement();
parentResourceManagement.setElementType(parentResourceType);
ODirection directionEnum = ODirection.OUT;
UUID refereceUUID = null;
Boolean[] polymorphics = new Boolean[] {true, false};
int expectedSize = 1;
Set<UUID> expectedUUIDFor2Results = new HashSet<>();
expectedUUIDFor2Results.add(createdConfigurations.get(3).getHeader().getUUID());
expectedUUIDFor2Results.add(createdConfigurations.get(4).getHeader().getUUID());
for(int i=0; i<names.length; i++) {
expectedSize = 1;
if(i==4) {
expectedSize = 2;
}
Configuration expected = createdConfigurations.get(i);
UUID expectedUUID = expected.getHeader().getUUID();
boolean includeRelation = false;
for(Boolean polymorphic : polymorphics) {
Map<String,String> constraint = new HashMap<>();
constraint.put(IdentifierFacet.VALUE_PROPERTY, names[i]);
if(additionalProperties[i]!=null) {
constraint.put(EXTRA, additionalProperties[i].toString());
}
logger.debug("Going to query for {}polymorphic {} --{}--> {} with constaints {}", polymorphic ? "" : "NOT ",
resourceType, relationType, referenceType, constraint);
String ret = resourceManagement.query(relationType, referenceType, refereceUUID, directionEnum,
polymorphic, constraint, includeRelation);
List<Configuration> list = ElementMapper.unmarshalList(Configuration.class, ret);
Assert.assertTrue(list.size()==expectedSize);
if(expectedSize==1) {
Configuration configuration = list.get(0);
UUID gotUUID = configuration.getHeader().getUUID();
Assert.assertTrue(gotUUID.compareTo(expectedUUID)==0);
}else {
Set<UUID> gotUUIDs = new HashSet<>();
for(Configuration c : list) {
gotUUIDs.add(c.getHeader().getUUID());
}
Assert.assertTrue(gotUUIDs.containsAll(expectedUUIDFor2Results));
Assert.assertTrue(expectedUUIDFor2Results.containsAll(gotUUIDs));
}
if(polymorphic) {
logger.debug("Going to query for polymorphic {} --{}--> {} with constaints {}",
parentResourceType, relationType, referenceType, constraint);
String retPolimorphic = parentResourceManagement.query(relationType, referenceType, refereceUUID, directionEnum,
polymorphic, constraint, includeRelation);
List<ConfigurationTemplate> listPolimorphic = ElementMapper.unmarshalList(ConfigurationTemplate.class, retPolimorphic);
Assert.assertTrue(listPolimorphic.size()==expectedSize);
if(expectedSize==1) {
ConfigurationTemplate configurationTemplate = listPolimorphic.get(0);
Assert.assertTrue(configurationTemplate instanceof Configuration);
UUID gotPolimorphicUUID = configurationTemplate.getHeader().getUUID();
Assert.assertTrue(gotPolimorphicUUID.compareTo(expectedUUID)==0);
}else {
Set<UUID> gotUUIDs = new HashSet<>();
for(ConfigurationTemplate c : listPolimorphic) {
Assert.assertTrue(c instanceof Configuration);
gotUUIDs.add(c.getHeader().getUUID());
}
Assert.assertTrue(gotUUIDs.containsAll(expectedUUIDFor2Results));
Assert.assertTrue(expectedUUIDFor2Results.containsAll(gotUUIDs));
}
}
}
}
} finally {
for(Configuration configuration : createdConfigurations) {
ERManagementTest.deleteResource(configuration);
}
}
}
}

View File

@ -4,7 +4,7 @@ import java.util.UUID;
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.instances.multicontext.MultiContextTest;
import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility;
import org.gcube.resourcemanagement.model.impl.entities.facets.SoftwareFacetImpl;
import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet;
@ -17,7 +17,7 @@ import org.junit.Assert;
* @author lucafrosini
*
*/
public class FacetManagementTest extends ContextTest {
public class FacetManagementTest extends MultiContextTest {
// private static Logger logger = LoggerFactory.getLogger(ERManagementTest.class);