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

822 lines
35 KiB
Java
Raw Normal View History

/**
*
*/
package org.gcube.informationsystem.resourceregistry.instances;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
2021-03-08 11:02:42 +01:00
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
2021-03-08 09:35:58 +01:00
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
2021-03-08 12:27:59 +01:00
import org.gcube.informationsystem.base.reference.Element;
2021-03-08 11:02:42 +01:00
import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.model.impl.properties.EncryptedImpl;
import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl;
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.properties.Encrypted;
2021-03-08 11:02:42 +01:00
import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint;
2021-03-08 11:02:42 +01:00
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.exceptions.query.InvalidQueryException;
2021-03-08 13:09:18 +01:00
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;
2019-11-06 12:14:27 +01:00
import org.gcube.informationsystem.resourceregistry.instances.model.relations.ConsistsOfManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement;
2021-03-08 11:02:42 +01:00
import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility;
import org.gcube.informationsystem.utils.ElementMapper;
import org.gcube.resourcemanagement.model.impl.entities.facets.AccessPointFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.CPUFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.EventFacetImpl;
2021-03-02 16:39:21 +01:00
import org.gcube.resourcemanagement.model.impl.entities.facets.IdentifierFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.LicenseFacetImpl;
2021-02-22 16:36:19 +01:00
import org.gcube.resourcemanagement.model.impl.entities.facets.MemoryFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.NetworkingFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.SoftwareFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.StateFacetImpl;
2021-03-02 16:39:21 +01:00
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.HostingNodeImpl;
import org.gcube.resourcemanagement.model.impl.properties.ValueSchemaImpl;
2021-02-22 16:36:19 +01:00
import org.gcube.resourcemanagement.model.impl.relations.consistsof.HasPersistentMemoryImpl;
import org.gcube.resourcemanagement.model.impl.relations.consistsof.HasVolatileMemoryImpl;
import org.gcube.resourcemanagement.model.impl.relations.consistsof.IsIdentifiedByImpl;
import org.gcube.resourcemanagement.model.impl.relations.isrelatedto.ActivatesImpl;
import org.gcube.resourcemanagement.model.reference.entities.facets.AccessPointFacet;
import org.gcube.resourcemanagement.model.reference.entities.facets.CPUFacet;
import org.gcube.resourcemanagement.model.reference.entities.facets.EventFacet;
2021-03-02 16:39:21 +01:00
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.LicenseFacet;
2021-02-22 16:36:19 +01:00
import org.gcube.resourcemanagement.model.reference.entities.facets.MemoryFacet;
import org.gcube.resourcemanagement.model.reference.entities.facets.MemoryFacet.MemoryUnit;
import org.gcube.resourcemanagement.model.reference.entities.facets.NetworkingFacet;
import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet;
import org.gcube.resourcemanagement.model.reference.entities.facets.StateFacet;
import org.gcube.resourcemanagement.model.reference.entities.resources.Configuration;
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.properties.ValueSchema;
2021-02-22 16:36:19 +01:00
import org.gcube.resourcemanagement.model.reference.relations.consistsof.HasPersistentMemory;
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.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 ERManagementTest extends ContextTest {
2021-03-02 16:55:42 +01:00
private static Logger logger = LoggerFactory.getLogger(ERManagementTest.class);
2021-03-08 09:35:58 +01:00
public static Configuration instantiateValidConfiguration() throws Exception {
Configuration configuration = new ConfigurationImpl();
IdentifierFacet identifierFacet = new IdentifierFacetImpl();
identifierFacet.setType(IdentificationType.STRING);
identifierFacet.setValue("MyID");
identifierFacet.setPersistent(false);
2021-03-02 16:55:42 +01:00
2021-03-08 09:35:58 +01:00
IsIdentifiedBy<Configuration, IdentifierFacet> isIdentifiedBy = new IsIdentifiedByImpl<Configuration, IdentifierFacet>(
configuration, identifierFacet);
configuration.addFacet(isIdentifiedBy);
return configuration;
}
2021-03-02 16:55:42 +01:00
public static EService instantiateValidEService() throws Exception {
EService eService = new EServiceImpl();
SoftwareFacet softwareFacet = new SoftwareFacetImpl();
softwareFacet.setGroup("InformationSystem");
softwareFacet.setName("resource-registry");
softwareFacet.setVersion("1.1.0");
IsIdentifiedBy<Resource, Facet> isIdentifiedBy = new IsIdentifiedByImpl<Resource, Facet>(eService,
softwareFacet, null);
eService.addFacet(isIdentifiedBy);
AccessPointFacet accessPointFacet = new AccessPointFacetImpl();
accessPointFacet.setEndpoint(new URI("https://localhost"));
accessPointFacet.setEntryName("port1");
eService.addFacet(accessPointFacet);
EventFacet eventFacet = new EventFacetImpl();
eventFacet.setDate(Calendar.getInstance().getTime());
ValueSchema event = new ValueSchemaImpl();
event.setSchema(new URI("Schema"));
event.setValue("Created");
eventFacet.setEvent(event);
eService.addFacet(eventFacet);
StateFacet stateFacet = new StateFacetImpl();
stateFacet.setValue("ready");
eService.addFacet(stateFacet);
LicenseFacet licenseFacet = new LicenseFacetImpl();
licenseFacet.setName("EUPL");
licenseFacet.setTextURL(
new URL("https://joinup.ec.europa.eu/community/eupl/og_page/european-union-public-licence-eupl-v11"));
eService.addFacet(licenseFacet);
return eService;
}
2021-03-08 12:27:59 +01:00
public static HostingNode instantiateValidHostingNode() throws Exception {
2021-03-02 16:55:42 +01:00
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> isIdentifiedBy = new IsIdentifiedByImpl<HostingNode, NetworkingFacet>(
hostingNode, networkingFacet);
hostingNode.addFacet(isIdentifiedBy);
CPUFacet cpuFacet = new CPUFacetImpl();
cpuFacet.setClockSpeed("1 GHz");
cpuFacet.setModel("Opteron");
cpuFacet.setVendor("AMD");
hostingNode.addFacet(cpuFacet);
MemoryFacet persistentMemoryFacet = new MemoryFacetImpl();
persistentMemoryFacet.setSize(1024);
persistentMemoryFacet.setUnit(MemoryUnit.GB);
persistentMemoryFacet.setUsed(512);
HasPersistentMemory<HostingNode, MemoryFacet> hasPersistentMemory = new HasPersistentMemoryImpl<HostingNode, MemoryFacet>(
hostingNode, persistentMemoryFacet);
hostingNode.addFacet(hasPersistentMemory);
MemoryFacet volatileMemoryFacet = new MemoryFacetImpl();
volatileMemoryFacet.setSize(8);
volatileMemoryFacet.setUnit(MemoryUnit.GB);
volatileMemoryFacet.setUsed(4);
HasVolatileMemory<HostingNode, MemoryFacet> hasVolatileMemory = new HasVolatileMemoryImpl<HostingNode, MemoryFacet>(
hostingNode, volatileMemoryFacet);
hostingNode.addFacet(hasVolatileMemory);
StateFacet hnStateFacet = new StateFacetImpl();
hnStateFacet.setValue("certified");
hostingNode.addFacet(hnStateFacet);
EventFacet hnEventFacet = new EventFacetImpl();
hnEventFacet.setDate(Calendar.getInstance().getTime());
ValueSchema hnEvent = new ValueSchemaImpl();
hnEvent.setSchema(new URI("Schema"));
hnEvent.setValue("Created");
hnEventFacet.setEvent(hnEvent);
hostingNode.addFacet(hnEventFacet);
return hostingNode;
}
2021-03-08 11:02:42 +01:00
public static void checkHeader(IdentifiableElement identifiableElement, IdentifiableElement createdIdentifiableElement) {
Header createdHeader = createdIdentifiableElement.getHeader();
Assert.assertTrue(createdHeader!=null);
UUID createdUUID = createdHeader.getUUID();
Assert.assertTrue(createdUUID!=null);
2021-03-08 11:20:18 +01:00
String createdBy = createdHeader.getCreatedBy();
Assert.assertTrue(createdBy!=null);
2021-03-08 11:02:42 +01:00
2021-03-08 11:20:18 +01:00
String lastUpdateBy = createdHeader.getLastUpdateBy();
Assert.assertTrue(lastUpdateBy!=null);
2021-03-08 11:02:42 +01:00
Date creationTime = createdHeader.getCreationTime();
Assert.assertTrue(creationTime!=null);
Date lastUpdateTime = createdHeader.getLastUpdateTime();
Assert.assertTrue(lastUpdateTime!=null);
2021-03-08 12:27:59 +01:00
Assert.assertTrue(lastUpdateTime.equals(creationTime) || lastUpdateTime.equals(lastUpdateTime));
2021-03-08 11:02:42 +01:00
Header header = identifiableElement.getHeader();
if(header!=null) {
if(header.getUUID()!=null) {
Assert.assertTrue(createdUUID.compareTo(header.getUUID())==0);
}
2021-03-08 11:20:18 +01:00
if(header.getCreatedBy()!=null) {
Assert.assertTrue(createdBy.compareTo(header.getCreatedBy())==0);
2021-03-08 11:02:42 +01:00
}else {
2021-03-08 11:20:18 +01:00
Assert.assertTrue(createdBy.compareTo(HeaderUtility.getUser())==0);
2021-03-08 11:02:42 +01:00
}
2021-03-08 11:20:18 +01:00
if(header.getLastUpdateBy()!=null) {
2021-03-08 12:27:59 +01:00
Assert.assertTrue(lastUpdateBy.compareTo(header.getLastUpdateBy())==0);
}else {
Assert.assertTrue(lastUpdateBy.compareTo(HeaderUtility.getUser())==0);
2021-03-08 11:02:42 +01:00
}
if(header.getCreationTime()!=null) {
2021-03-08 12:27:59 +01:00
Assert.assertTrue(creationTime.equals(header.getCreationTime()));
2021-03-08 11:02:42 +01:00
}
if(header.getLastUpdateTime()!=null) {
2021-03-08 12:27:59 +01:00
Assert.assertTrue(lastUpdateTime.after(header.getLastUpdateTime()));
2021-03-08 11:02:42 +01:00
}
}
}
2021-03-08 12:27:59 +01:00
public static void checkPropagationConstraint(PropagationConstraint propagationConstraint, PropagationConstraint gotPropagationConstraint) {
Assert.assertTrue(propagationConstraint.getAddConstraint()==gotPropagationConstraint.getAddConstraint());
Assert.assertTrue(propagationConstraint.getRemoveConstraint()==gotPropagationConstraint.getRemoveConstraint());
}
public static void checkConsistOf(ConsistsOf<? extends Resource, ? extends Facet> consistsOf, ConsistsOf<? extends Resource, ? extends Facet> gotConsistsOf) {
checkHeader(consistsOf, gotConsistsOf);
2021-03-08 11:02:42 +01:00
2021-03-08 12:27:59 +01:00
if(consistsOf.getPropagationConstraint()==null) {
PropagationConstraint propagationConstraint = gotConsistsOf.getPropagationConstraint();
Assert.assertTrue(propagationConstraint.getAddConstraint()==AddConstraint.propagate);
Assert.assertTrue(propagationConstraint.getRemoveConstraint()==RemoveConstraint.cascade);
checkPropagationConstraint(propagationConstraint, gotConsistsOf.getPropagationConstraint());
}else {
checkPropagationConstraint(consistsOf.getPropagationConstraint(), gotConsistsOf.getPropagationConstraint());
}
2021-03-08 11:02:42 +01:00
2021-03-08 12:27:59 +01:00
Map<String, Object> additionalProperties = new HashMap<>(consistsOf.getAdditionalProperties());
additionalProperties.remove(Element.SUPERCLASSES_PROPERTY);
Map<String, Object> gotAdditionalProperties = new HashMap<>(gotConsistsOf.getAdditionalProperties());
gotAdditionalProperties.remove(Element.SUPERCLASSES_PROPERTY);
Assert.assertTrue(additionalProperties.size()==gotAdditionalProperties.size());
for(String key : additionalProperties.keySet()) {
Assert.assertTrue(gotAdditionalProperties.containsKey(key));
Object additionalProperty = additionalProperties.get(key);
Object gotAdditionalProperty = gotAdditionalProperties.get(key);
Assert.assertTrue(additionalProperty.getClass() == gotAdditionalProperty.getClass());
Assert.assertTrue(additionalProperty.equals(gotAdditionalProperty));
}
}
public static void checkFacet(Facet facet, Facet gotFacet) throws Exception {
checkHeader(facet, gotFacet);
Class<? extends Facet> clz = facet.getClass();
Class<? extends Facet> gotClz = gotFacet.getClass();
Assert.assertTrue(clz==gotClz);
}
2021-03-08 13:09:18 +01:00
protected static <R extends Resource> void checkResource(R resource, R gotResource) throws Exception {
2021-03-08 12:27:59 +01:00
Assert.assertTrue(resource.getClass() == gotResource.getClass());
checkHeader(resource, gotResource);
2021-03-08 11:02:42 +01:00
List<ConsistsOf<? extends Resource, ? extends Facet>> resourceConsistsOf = resource.getConsistsOf();
2021-03-08 12:27:59 +01:00
List<ConsistsOf<? extends Resource, ? extends Facet>> gotResourceConsistsOf = gotResource.getConsistsOf();
Assert.assertTrue(resourceConsistsOf.size() == gotResourceConsistsOf.size());
2021-03-08 11:02:42 +01:00
for(ConsistsOf<? extends Resource, ? extends Facet> consistsOf : resourceConsistsOf) {
@SuppressWarnings("unchecked")
2021-03-08 12:27:59 +01:00
ConsistsOf<? extends Resource, ? extends Facet> gotConsistsOf = (ConsistsOf<? extends Resource, ? extends Facet>) gotResource.getConsistsOf(consistsOf.getClass(), consistsOf.getTarget().getClass()).get(0);
checkConsistOf(consistsOf, gotConsistsOf);
2021-03-08 11:02:42 +01:00
2021-03-08 12:27:59 +01:00
Facet facet = consistsOf.getTarget();
Facet gotFacet = gotConsistsOf.getTarget();
checkFacet(facet, gotFacet);
2021-03-08 11:02:42 +01:00
}
}
2021-03-08 13:09:18 +01:00
public static <R extends Resource> ResourceManagement getResourceManagement(R r) throws Exception {
2021-03-08 09:35:58 +01:00
ResourceManagement resourceManagement = new ResourceManagement();
2021-03-08 13:09:18 +01:00
resourceManagement.setElementType(Utility.getTypeName(r));
resourceManagement.setJson(ElementMapper.marshal(r));
if(r.getHeader()!=null && r.getHeader().getUUID()!=null) {
resourceManagement.setUUID(r.getHeader().getUUID());
}
return resourceManagement;
}
public static <R extends Resource> R createResource(R r) throws Exception {
ResourceManagement resourceManagement = getResourceManagement(r);
2021-03-08 09:35:58 +01:00
String json = resourceManagement.create();
2021-03-08 13:09:18 +01:00
@SuppressWarnings("unchecked")
R createdR = (R) ElementMapper.unmarshal(r.getClass(), json);
2021-03-08 11:02:42 +01:00
2021-03-08 13:09:18 +01:00
checkResource(r, createdR);
2021-03-08 12:27:59 +01:00
2021-03-08 13:09:18 +01:00
return createdR;
}
public static EService createEService() throws Exception {
EService eService = ERManagementTest.instantiateValidEService();
return createResource(eService);
2021-03-08 11:02:42 +01:00
}
2021-03-08 12:27:59 +01:00
public static HostingNode createHostingNode() throws Exception {
return createHostingNode(null);
}
public static HostingNode createHostingNode(EService eService) throws Exception {
HostingNode hostingNode = ERManagementTest.instantiateValidHostingNode();
if(eService!=null) {
PropagationConstraint propagationConstraint = new PropagationConstraintImpl();
propagationConstraint.setRemoveConstraint(RemoveConstraint.cascade);
Activates<HostingNode, EService> activates = new ActivatesImpl<HostingNode, EService>(hostingNode, eService,
propagationConstraint);
hostingNode.attachResource(activates);
}
2021-03-08 13:09:18 +01:00
return createResource(hostingNode);
2021-03-08 12:27:59 +01:00
}
public static Map<String, Resource> createHostingNodeAndEService() throws Exception {
Map<String, Resource> map = new HashMap<>();
EService eService = createEService();
map.put(EService.NAME, eService);
HostingNode hostingNode = createHostingNode(eService);
map.put(HostingNode.NAME, hostingNode);
return map;
}
2021-03-08 13:09:18 +01:00
public static <R extends Resource> void deleteResource(R r) throws Exception {
if(r!=null) {
ResourceManagement resourceManagement = getResourceManagement(r);
resourceManagement.delete();
}
}
2021-03-08 11:02:42 +01:00
@Test
public void testCreateEService() throws Exception {
EService eService = null;
try {
eService = createEService();
}finally {
2021-03-08 13:09:18 +01:00
deleteResource(eService);
2021-03-08 11:02:42 +01:00
}
2021-03-08 09:35:58 +01:00
}
/*
@Test
public void testReadResource() throws Exception {
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setUUID(UUID.fromString("26da57ee-33bd-4c4b-8aef-9206b61c329e"));
String read = resourceManagement.read().toString();
logger.debug(read);
2021-03-03 17:18:36 +01:00
}
2021-03-08 09:35:58 +01:00
*/
2021-03-03 17:18:36 +01:00
2021-03-08 09:35:58 +01:00
/*
@Test
2021-03-08 09:35:58 +01:00
public void testDeleteResource() throws Exception {
ResourceManagement resourceManagement = new ResourceManagement();
2021-03-08 09:35:58 +01:00
resourceManagement.setUUID(UUID.fromString("64635295-7ced-4931-a55f-40fc8199b280"));
boolean deleted = resourceManagement.delete();
Assert.assertTrue(deleted);
}
*/
2021-03-08 12:27:59 +01:00
@Test
public void testCreateHostingNode() throws Exception {
HostingNode hostingNode = null;
try {
hostingNode = createHostingNode();
}finally {
2021-03-08 13:09:18 +01:00
deleteResource(hostingNode);
2021-03-08 12:27:59 +01:00
}
}
2021-03-08 09:35:58 +01:00
@Test
public void testCreateHostingNodeAndEService() throws Exception {
Map<String, Resource> map = ERManagementTest.createHostingNodeAndEService();
2021-03-08 13:09:18 +01:00
deleteResource(map.get(EService.NAME));
deleteResource(map.get(HostingNode.NAME));
}
@Test
2021-03-08 09:35:58 +01:00
public void testCreateReadUpdateDeleteFacet() throws Exception {
2021-03-08 13:09:18 +01:00
EService eService = createEService();
2021-03-08 09:35:58 +01:00
2021-03-08 13:09:18 +01:00
try {
CPUFacet cpuFacet = new CPUFacetImpl();
cpuFacet.setClockSpeed("1 GHz");
cpuFacet.setModel("Opteron");
cpuFacet.setVendor("AMD");
ConsistsOf<EService, CPUFacet> consistsOf = new ConsistsOfImpl<EService, CPUFacet>(eService, cpuFacet);
ConsistsOfManagement consistsOfManagement = new ConsistsOfManagement();
consistsOfManagement.setElementType(Utility.getTypeName(consistsOf));
consistsOfManagement.setJson(ElementMapper.marshal(consistsOf));
String createdConsistsOfString = consistsOfManagement.create();
@SuppressWarnings("unchecked")
ConsistsOf<EService, CPUFacet> createdConsistsOf = ElementMapper.unmarshal(ConsistsOf.class, createdConsistsOfString);
CPUFacet createdCpuFacet = createdConsistsOf.getTarget();
Assert.assertTrue(cpuFacet.getClockSpeed().compareTo(createdCpuFacet.getClockSpeed()) == 0);
Assert.assertTrue(cpuFacet.getModel().compareTo(createdCpuFacet.getModel()) == 0);
Assert.assertTrue(cpuFacet.getVendor().compareTo(createdCpuFacet.getVendor()) == 0);
UUID uuid = createdCpuFacet.getHeader().getUUID();
FacetManagement facetManagement = new FacetManagement();
facetManagement.setElementType(Utility.getTypeName(createdCpuFacet));
facetManagement.setUUID(uuid);
String readJson = facetManagement.read().toString();
CPUFacet readCpuFacet = ElementMapper.unmarshal(CPUFacet.class, readJson);
logger.debug("Read:\nRaw Json : {}\nUnmarshalled : {}", readJson, readCpuFacet);
Assert.assertTrue(cpuFacet.getClockSpeed().compareTo(readCpuFacet.getClockSpeed()) == 0);
Assert.assertTrue(cpuFacet.getModel().compareTo(readCpuFacet.getModel()) == 0);
Assert.assertTrue(cpuFacet.getVendor().compareTo(readCpuFacet.getVendor()) == 0);
Assert.assertTrue(uuid.compareTo(readCpuFacet.getHeader().getUUID()) == 0);
String newVendor = "Intel";
String newClockSpeed = "2 GHz";
readCpuFacet.setVendor(newVendor);
readCpuFacet.setClockSpeed(newClockSpeed);
String additionPropertyKey = "My";
String additionPropertyValue = "Test";
readCpuFacet.setAdditionalProperty(additionPropertyKey, additionPropertyValue);
facetManagement = new FacetManagement();
facetManagement.setElementType(Utility.getTypeName(readCpuFacet));
facetManagement.setUUID(uuid);
facetManagement.setJson(ElementMapper.marshal(readCpuFacet));
String updatedJson = facetManagement.update();
CPUFacet updatedCpuFacet = ElementMapper.unmarshal(CPUFacet.class, updatedJson);
logger.debug("Updated:\nRaw Json : {}\nUnmarshalled : {}", updatedJson, updatedCpuFacet);
Assert.assertTrue(updatedCpuFacet.getClockSpeed().compareTo(newClockSpeed) == 0);
Assert.assertTrue(readCpuFacet.getModel().compareTo(updatedCpuFacet.getModel()) == 0);
Assert.assertTrue(updatedCpuFacet.getVendor().compareTo(newVendor) == 0);
Assert.assertTrue(((String) updatedCpuFacet.getAdditionalProperty(additionPropertyKey))
.compareTo((String) readCpuFacet.getAdditionalProperty(additionPropertyKey)) == 0);
Assert.assertTrue(uuid.compareTo(updatedCpuFacet.getHeader().getUUID()) == 0);
String user = AuthorizationProvider.instance.get().getClient().getId();
Assert.assertTrue(updatedCpuFacet.getHeader().getLastUpdateBy().compareTo(user) == 0);
facetManagement = new FacetManagement();
facetManagement.setElementType(Utility.getTypeName(updatedCpuFacet));
facetManagement.setUUID(uuid);
String readUpdatedJson = facetManagement.read().toString();
CPUFacet readUpdatedCpuFacet = ElementMapper.unmarshal(CPUFacet.class, readUpdatedJson);
logger.debug("Read Updated:\nRaw Json : {}\nUnmarshalled : {}", readUpdatedJson, readUpdatedCpuFacet);
Assert.assertTrue(updatedCpuFacet.getClockSpeed().compareTo(readUpdatedCpuFacet.getClockSpeed()) == 0);
Assert.assertTrue(updatedCpuFacet.getModel().compareTo(readUpdatedCpuFacet.getModel()) == 0);
Assert.assertTrue(updatedCpuFacet.getVendor().compareTo(readUpdatedCpuFacet.getVendor()) == 0);
Assert.assertTrue(((String) updatedCpuFacet.getAdditionalProperty(additionPropertyKey))
.compareTo((String) readUpdatedCpuFacet.getAdditionalProperty(additionPropertyKey)) == 0);
Assert.assertTrue(uuid.compareTo(updatedCpuFacet.getHeader().getUUID()) == 0);
facetManagement = new FacetManagement();
facetManagement.setElementType(Utility.getTypeName(readCpuFacet));
facetManagement.setUUID(uuid);
facetManagement.delete();
} finally {
deleteResource(eService);
}
}
2021-03-02 16:55:42 +01:00
@Test
public void testCreatePropertyTypeNotInSchema() throws Exception {
2021-03-03 17:18:36 +01:00
/*
* A facet cannot be created per se. Going to create a Configuration which does
* not impose any particular constraint except the IdentifierFact
*/
Configuration configuration = instantiateValidConfiguration();
AccessPointFacet accessPointFacet = new AccessPointFacetImpl();
accessPointFacet.setEndpoint(new URI("https://localhost"));
accessPointFacet.setEntryName("port1");
ValueSchema authorization = new ValueSchemaImpl();
authorization.setValue("pwd");
URI uri = new URI("https://www.gcube-system.org");
2020-12-21 23:09:57 +01:00
authorization.setSchema(uri);
accessPointFacet.setAuthorization(authorization);
2021-03-02 16:55:42 +01:00
2021-03-03 17:18:36 +01:00
String additionlaPropertyKey = "Test";
String additionlaPropertyValue = "MyTest";
accessPointFacet.setAdditionalProperty(additionlaPropertyKey, additionlaPropertyValue);
2021-03-02 16:55:42 +01:00
Encrypted encrypted = new EncryptedImpl();
2021-03-03 17:18:36 +01:00
String plainValue = "Encrypted";
String encryptedValue = EncryptedImpl.encrypt(plainValue);
encrypted.setEncryptedValue(encryptedValue);
2021-03-03 17:18:36 +01:00
String encryptedKey = "Enc";
accessPointFacet.setAdditionalProperty(encryptedKey, encrypted);
configuration.addFacet(accessPointFacet);
2021-03-02 16:55:42 +01:00
2021-03-03 17:18:36 +01:00
String marshalled = ElementMapper.marshal(configuration);
logger.debug(marshalled);
2021-03-02 16:55:42 +01:00
2021-03-08 13:09:18 +01:00
Configuration createdConfiguration = createResource(configuration);
2021-03-02 16:55:42 +01:00
2021-03-03 17:18:36 +01:00
AccessPointFacet apf = configuration.getFacets(AccessPointFacet.class).get(0);
Assert.assertTrue(apf.getAuthorization() instanceof ValueSchema);
2021-03-03 17:18:36 +01:00
Assert.assertTrue(apf.getAdditionalProperty(encryptedKey) instanceof Encrypted);
Encrypted enc = (Encrypted) apf.getAdditionalProperty(encryptedKey);
String encValue = enc.getEncryptedValue();
2021-03-02 16:55:42 +01:00
Assert.assertTrue(encValue.compareTo(encryptedValue) == 0);
String decryptedValue = EncryptedImpl.decrypt(encValue);
2021-03-03 17:18:36 +01:00
Assert.assertTrue(decryptedValue.compareTo(plainValue) == 0);
Assert.assertTrue(((String) apf.getAdditionalProperty(additionlaPropertyKey)).compareTo(additionlaPropertyValue) == 0);
2021-03-02 16:55:42 +01:00
2021-03-08 13:09:18 +01:00
deleteResource(createdConfiguration);
}
2021-03-02 16:55:42 +01:00
2021-03-03 17:18:36 +01:00
@Test
2021-03-02 16:55:42 +01:00
public void testGetAll() throws Exception {
Map<String, List<Resource>> resources = new HashMap<>();
2021-03-02 16:55:42 +01:00
final int MAX = 5;
int typeNumber = 0;
2021-03-02 16:55:42 +01:00
for (int i = 0; i < MAX; i++) {
2021-03-03 17:18:36 +01:00
Map<String, Resource> map = ERManagementTest.createHostingNodeAndEService();
2021-03-02 16:55:42 +01:00
if (typeNumber == 0) {
typeNumber = map.size();
}
2021-03-02 16:55:42 +01:00
for (String key : map.keySet()) {
if (!resources.containsKey(key)) {
resources.put(key, new ArrayList<Resource>());
}
resources.get(key).add(map.get(key));
}
}
2021-03-02 16:55:42 +01:00
/* Getting all instances of created specific Resources */
for (String key : resources.keySet()) {
ResourceManagement resourceManagement = (ResourceManagement) ElementManagementUtility.getERManagement(key);
String json = resourceManagement.all(false);
2021-03-02 16:55:42 +01:00
List<Resource> list = ElementMapper.unmarshalList(Resource.class, json);
logger.debug("{} are {} : {} ", key, list.size(), list);
2021-03-02 16:55:42 +01:00
Assert.assertTrue(list.size() == MAX);
}
2021-03-02 16:55:42 +01:00
/* Getting all Resources polymorphic and non polymorphic */
2021-03-02 16:55:42 +01:00
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);
2021-03-02 16:55:42 +01:00
Assert.assertTrue(list.size() == (MAX * typeNumber));
json = resourceManagement.all(false);
list = ElementMapper.unmarshalList(Resource.class, json);
2021-03-02 16:55:42 +01:00
Assert.assertTrue(list.size() == 0);
/* Getting all IsRelatedTo polymorphic and non polymorphic */
2021-03-02 16:55:42 +01:00
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);
2021-03-02 16:55:42 +01:00
Assert.assertTrue(resourcesList.size() == MAX);
json = isRelatedToManagement.all(false);
resourcesList = ElementMapper.unmarshalList(Resource.class, json);
2021-03-02 16:55:42 +01:00
Assert.assertTrue(resourcesList.size() == 0);
/* Getting all ConsistsOf polymorphic and non polymorphic */
2021-03-02 16:55:42 +01:00
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);
2021-03-02 16:55:42 +01:00
logger.debug("{} are {} : {} ", IsRelatedTo.NAME, consistsOfNonPolimorphicList.size(),
consistsOfNonPolimorphicList);
Assert.assertTrue(consistsOfPolimorphicList.size() >= consistsOfNonPolimorphicList.size());
/* Removing created Entity and Relation to have a clean DB */
2021-03-02 16:55:42 +01:00
List<Resource> resourceList = resources.get(HostingNode.NAME);
2021-03-02 16:55:42 +01:00
for (Resource r : resourceList) {
2021-03-08 13:09:18 +01:00
deleteResource(r);
}
}
2021-03-02 16:55:42 +01:00
@Test
2021-03-02 16:55:42 +01:00
public void testGetAllFrom() throws Exception {
2021-03-03 17:18:36 +01:00
Map<String, Resource> map = ERManagementTest.createHostingNodeAndEService();
2021-03-02 16:55:42 +01:00
EService eService = (EService) map.get(EService.NAME);
UUID eServiceUUID = eService.getHeader().getUUID();
2021-03-02 16:55:42 +01:00
HostingNode hostingNode = (HostingNode) map.get(HostingNode.NAME);
UUID hostingNodeUUID = hostingNode.getHeader().getUUID();
2021-03-02 16:55:42 +01:00
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setElementType(Service.NAME);
2021-03-02 16:55:42 +01:00
2021-01-28 22:53:10 +01:00
boolean includeRelation = false;
2021-03-02 16:55:42 +01:00
/* Getting Hosting Node */
2021-03-02 16:55:42 +01:00
String json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.BOTH, true,
null, includeRelation);
List<Resource> resourceList = ElementMapper.unmarshalList(Resource.class, json);
2021-03-02 16:55:42 +01:00
Assert.assertTrue(resourceList.size() == 1);
Resource resource = resourceList.get(0);
2021-03-02 16:55:42 +01:00
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);
2021-03-02 16:55:42 +01:00
Assert.assertTrue(resourceList.size() == 1);
resource = resourceList.get(0);
2021-03-02 16:55:42 +01:00
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);
2021-03-02 16:55:42 +01:00
Assert.assertTrue(resourceList.size() == 0);
json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.BOTH, false, null,
includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
2021-03-02 16:55:42 +01:00
Assert.assertTrue(resourceList.size() == 0);
json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.OUT, false, null,
includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
2021-03-02 16:55:42 +01:00
Assert.assertTrue(resourceList.size() == 0);
json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.IN, false, null,
includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
2021-03-02 16:55:42 +01:00
Assert.assertTrue(resourceList.size() == 0);
/* END Getting Hosting Node */
/* Getting EService */
2021-03-02 16:55:42 +01:00
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.BOTH, true,
null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
2021-03-02 16:55:42 +01:00
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);
2021-03-02 16:55:42 +01:00
Assert.assertTrue(resourceList.size() == 0);
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.IN, true, null,
includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
2021-03-02 16:55:42 +01:00
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);
2021-03-02 16:55:42 +01:00
Assert.assertTrue(resourceList.size() == 0);
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.OUT, false,
null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
2021-03-02 16:55:42 +01:00
Assert.assertTrue(resourceList.size() == 0);
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.IN, false, null,
includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
2021-03-02 16:55:42 +01:00
Assert.assertTrue(resourceList.size() == 0);
/* END Getting HostingNode */
2021-03-02 16:55:42 +01:00
Facet identificationFacet = eService.getIdentificationFacets().get(0);
UUID identificationFacetUUID = identificationFacet.getHeader().getUUID();
2021-03-02 16:55:42 +01:00
/* EService --ConsistsOf--> SoftwareFacet */
try {
2021-03-02 16:55:42 +01:00
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID,
ODirection.BOTH, true, null, includeRelation);
} catch (InvalidQueryException e) {
// Ok expected
}
2021-03-02 16:55:42 +01:00
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.OUT,
true, null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
2021-03-02 16:55:42 +01:00
Assert.assertTrue(resourceList.size() == 1);
resource = resourceList.get(0);
Facet targetIdentificationFacet = ((GCubeResource) resource).getIdentificationFacets().get(0);
2021-03-02 16:55:42 +01:00
Assert.assertTrue(resource.getHeader().getUUID().compareTo(eServiceUUID) == 0);
Assert.assertTrue(targetIdentificationFacet.getHeader().getUUID().compareTo(identificationFacetUUID) == 0);
try {
2021-03-02 16:55:42 +01:00
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.IN,
true, null, includeRelation);
} catch (InvalidQueryException e) {
// Ok expected
}
2021-03-02 16:55:42 +01:00
try {
2021-03-02 16:55:42 +01:00
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID,
ODirection.BOTH, false, null, includeRelation);
} catch (InvalidQueryException e) {
// Ok expected
}
2021-03-02 16:55:42 +01:00
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.OUT,
false, null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json);
2021-03-02 16:55:42 +01:00
Assert.assertTrue(resourceList.size() == 0);
try {
2021-03-02 16:55:42 +01:00
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.IN,
false, null, includeRelation);
} catch (InvalidQueryException e) {
// Ok expected
}
2021-03-02 16:55:42 +01:00
/* END EService --ConsistsOf--> SoftwareFacet */
/* Removing created Entity and Relation to have a clean DB */
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(hostingNode.getHeader().getUUID());
2021-03-05 15:14:51 +01:00
resourceManagement.delete();
}
2021-03-02 16:55:42 +01:00
2021-03-08 13:09:18 +01:00
// public static final String TEST_RESOURCE = "test-resource.json";
//
// // @Test
// public void testUpdateResourceFromFile()
// throws JsonParseException, JsonMappingException, IOException, ResourceRegistryException {
// File file = new File("src/test/resources/" + TEST_RESOURCE);
//
// logger.debug("{}", file.getAbsolutePath());
//
// FileInputStream fileInputStream = new FileInputStream(file);
// EService eService = ElementMapper.unmarshal(EService.class, fileInputStream);
//
// ResourceManagement resourceManagement = new ResourceManagement();
// resourceManagement.setUUID(eService.getHeader().getUUID());
// resourceManagement.setJson(ElementMapper.marshal(eService));
//
// resourceManagement.update();
//
// }
2021-03-02 16:55:42 +01:00
@Test
2021-03-08 13:09:18 +01:00
public void testUpdateFacetValue() throws Exception {
EService eService =null;
try {
eService = createEService();
final String newVersion = "1.2.0";
eService.getFacets(SoftwareFacet.class).get(0).setVersion(newVersion);
ResourceManagement resourceManagement = getResourceManagement(eService);
String json = resourceManagement.update();
EService updatedEService = ElementMapper.unmarshal(EService.class, json);
checkResource(eService, updatedEService);
Assert.assertTrue(updatedEService.getFacets(SoftwareFacet.class).get(0).getVersion().compareTo(newVersion) == 0);
}finally {
deleteResource(eService);
}
}
}