Added test for prepared query
This commit is contained in:
parent
2bc948b4f4
commit
7327f61370
|
@ -1,20 +1,33 @@
|
||||||
package org.gcube.informationsystem.resourceregistry.instances.model.entity;
|
package org.gcube.informationsystem.resourceregistry.instances.model.entity;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
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.Facet;
|
||||||
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
|
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
|
||||||
import org.gcube.informationsystem.resourceregistry.ContextTest;
|
import org.gcube.informationsystem.resourceregistry.ContextTest;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||||
|
import org.gcube.informationsystem.resourceregistry.api.utils.Utility;
|
||||||
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement;
|
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement;
|
||||||
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility;
|
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility;
|
||||||
import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement;
|
import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement;
|
||||||
import org.gcube.informationsystem.utils.ElementMapper;
|
import org.gcube.informationsystem.utils.ElementMapper;
|
||||||
|
import org.gcube.resourcemanagement.model.impl.entities.facets.IdentifierFacetImpl;
|
||||||
|
import org.gcube.resourcemanagement.model.impl.entities.resources.ConfigurationImpl;
|
||||||
import org.gcube.resourcemanagement.model.impl.entities.resources.EServiceImpl;
|
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.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.SoftwareFacet;
|
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.EService;
|
||||||
import org.gcube.resourcemanagement.model.reference.entities.resources.Service;
|
import org.gcube.resourcemanagement.model.reference.entities.resources.Service;
|
||||||
import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy;
|
import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy;
|
||||||
|
@ -113,4 +126,152 @@ public class ResourceManagementTest extends ContextTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final String EXTRA = "EXTRA";
|
||||||
|
|
||||||
|
protected Configuration createConfiguration(String name, Object value) throws ResourceRegistryException, IOException {
|
||||||
|
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);
|
||||||
|
|
||||||
|
ResourceManagement resourceManagement = new ResourceManagement();
|
||||||
|
resourceManagement.setElementType(Configuration.NAME);
|
||||||
|
resourceManagement.setJson(ElementMapper.marshal(configuration));
|
||||||
|
String json = resourceManagement.create();
|
||||||
|
logger.trace("Created {}", json);
|
||||||
|
configuration = ElementMapper.unmarshal(Configuration.class, json);
|
||||||
|
|
||||||
|
return 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]));
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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);
|
||||||
|
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));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(Configuration configuration : createdConfigurations) {
|
||||||
|
ResourceManagement rManagement = new ResourceManagement();
|
||||||
|
rManagement.setElementType(resourceType);
|
||||||
|
rManagement.setUUID(configuration.getHeader().getUUID());
|
||||||
|
rManagement.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,51 +0,0 @@
|
||||||
package org.gcube.informationsystem.resourceregistry.rest;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
|
||||||
import org.gcube.informationsystem.resourceregistry.ContextTest;
|
|
||||||
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.resourcemanagement.model.reference.entities.facets.IdentifierFacet;
|
|
||||||
import org.gcube.resourcemanagement.model.reference.entities.resources.Configuration;
|
|
||||||
import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import com.orientechnologies.orient.core.record.ODirection;
|
|
||||||
|
|
||||||
public class AccessTest {
|
|
||||||
|
|
||||||
private static Logger logger = LoggerFactory.getLogger(AccessTest.class);
|
|
||||||
|
|
||||||
// @Test
|
|
||||||
public void simulatGetAllResourcesHavingFacet() throws ObjectNotFound, Exception {
|
|
||||||
ContextTest.setContextByName("/gcube/devNext");
|
|
||||||
|
|
||||||
UUID refereceUUID = null;
|
|
||||||
ODirection directionEnum = ODirection.OUT;
|
|
||||||
Boolean polymorphic = false;
|
|
||||||
|
|
||||||
String resourceType = Utility.getTypeName(Configuration.class);
|
|
||||||
String relationType = Utility.getTypeName(IsIdentifiedBy.class);
|
|
||||||
String referenceType = Utility.getTypeName(IdentifierFacet.class);
|
|
||||||
|
|
||||||
Map<String,String> constraint = new HashMap<>();
|
|
||||||
constraint.put("value", "Aux");
|
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("rawtypes")
|
|
||||||
ElementManagement erManagement = ElementManagementUtility.getERManagement(resourceType);
|
|
||||||
|
|
||||||
String ret = ((ResourceManagement) erManagement).query(relationType, referenceType, refereceUUID, directionEnum,
|
|
||||||
polymorphic, constraint);
|
|
||||||
|
|
||||||
logger.debug(ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue