resource-registry-publisher/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/InvalidOperationTest.java

195 lines
8.7 KiB
Java

package org.gcube.informationsystem.resourceregistry.publisher;
import java.util.Map;
import java.util.UUID;
import org.gcube.informationsystem.model.impl.properties.HeaderImpl;
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.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entities.resource.ResourceAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaViolationException;
import org.gcube.informationsystem.serialization.ElementMapper;
import org.gcube.resourcemanagement.model.impl.entities.facets.CPUFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.SimpleFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.SoftwareFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.resources.HostingNodeImpl;
import org.gcube.resourcemanagement.model.impl.entities.resources.RunningPluginImpl;
import org.gcube.resourcemanagement.model.impl.relations.consistsof.IsIdentifiedByImpl;
import org.gcube.resourcemanagement.model.reference.entities.facets.CPUFacet;
import org.gcube.resourcemanagement.model.reference.entities.facets.SimpleFacet;
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.EService;
import org.gcube.resourcemanagement.model.reference.entities.resources.HostingNode;
import org.gcube.resourcemanagement.model.reference.entities.resources.RunningPlugin;
import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class InvalidOperationTest extends ERManagementTest {
private static Logger logger = LoggerFactory.getLogger(InvalidOperationTest.class);
public static final String ACTIVATES = "{\"propagationConstraint\":{\"@class\":\"PropagationConstraint\",\"add\":\"propagate\",\"remove\":\"cascade\"},\"@class\":\"Activates\",\"source\":{\"header\":{\"@class\":\"Header\",\"uuid\":\"CONFIGURATION_UUID\"},\"@class\":\"Configuration\"},\"target\":{\"header\":{\"@class\":\"Header\",\"uuid\":\"ESERVICE_UUID\"},\"@class\":\"EService\"}}";
public static final String ACTOR = "{\"@class\":\"Actor\",\"header\":null,\"consistsOf\":[{\"@class\":\"IsIdentifiedBy\",\"header\":null,\"propagationConstraint\":{\"@class\":\"PropagationConstraint\",\"remove\":\"cascadeWhenOrphan\",\"add\":\"propagate\"},\"source\":{\"@class\":\"Actor\",\"header\":null},\"target\":{\"@class\":\"ContactFacet\",\"header\":null,\"title\":\"Dr.\",\"name\":\"Frosini\",\"middleName\":null,\"surname\":null,\"eMail\":\"luca.frosini@isti.cnr.it\"}}],\"isRelatedTo\":[]}";
@Test(expected = SchemaViolationException.class)
public void createInvalidIsRealtedTo() throws Exception {
Configuration configuration = createConfiguration();
EService eService = createEService();
try {
/*
* Trying to create a relation activates between a Configuration and EService
* The creation MUST fails raising SchemaViolationException because the
* Activates relation is between two Service isntaces
*
* The only way to try to create it is using static string because Java classes
* already deny to create and instance of Activates
*
* Here we want to test how the service behave if a client does not behave properly
*/
String json = ACTIVATES.replace("CONFIGURATION_UUID", configuration.getHeader().getUUID().toString());
json = json.replace("ESERVICE_UUID", eService.getHeader().getUUID().toString());
resourceRegistryPublisher.create(json);
}finally {
resourceRegistryPublisher.delete(configuration);
resourceRegistryPublisher.delete(eService);
}
}
@Test(expected = ResourceAlreadyPresentException.class)
public void testRecreate() throws Exception {
EService eService = createEService();
try {
resourceRegistryPublisher.create(eService);
}finally {
resourceRegistryPublisher.delete(eService);
}
}
@Test(expected = SchemaViolationException.class)
public void testCreateStandAloneFacet() throws Exception {
CPUFacet cpuFacet = new CPUFacetImpl();
cpuFacet.setClockSpeed("1 GHz");
cpuFacet.setModel("Opteron");
cpuFacet.setVendor("AMD");
resourceRegistryPublisher.create(cpuFacet);
}
@Test(expected = SchemaViolationException.class)
public void testCreateInvalidRunningPlugin() throws Exception {
RunningPlugin runningPlugin = new RunningPluginImpl();
SoftwareFacet softwareFacet = new SoftwareFacetImpl();
softwareFacet.setGroup("information-system");
softwareFacet.setName("is-exporter-se-plugin");
softwareFacet.setVersion("1.0.0");
IsIdentifiedBy<RunningPlugin, SoftwareFacet> isIdentifiedBy = new IsIdentifiedByImpl<>(runningPlugin, softwareFacet);
runningPlugin.addFacet(isIdentifiedBy);
resourceRegistryPublisher.create(runningPlugin);
}
@Test(expected = ResourceRegistryException.class)
public void testCreateAnEntityDifferentFromDeclared() throws Exception {
EService eService = ERManagementTest.instantiateValidEService();
resourceRegistryPublisher.create(eService);
}
@Test(expected = SchemaViolationException.class)
public void testCreateAbstractEntity() throws Exception {
resourceRegistryPublisher.create(ACTOR);
}
@Test(expected = SchemaViolationException.class)
public void testCreateHostingNodeAndEServiceWithSharedFacet() throws Exception {
ERManagementTest erManagementTest = new ERManagementTest();
Map<String, Resource> map = erManagementTest.createHostingNodeAndEService();
EService eService = (EService) map.get(EService.NAME);
HostingNode hostingNode = (HostingNode) map.get(HostingNode.NAME);
try {
Facet shared = hostingNode.getConsistsOf().get(0).getTarget();
ConsistsOf<EService, Facet> consistsOf = new ConsistsOfImpl<>(eService, shared);
consistsOf = resourceRegistryPublisher.create(consistsOf);
logger.debug("Created : {}", consistsOf);
} finally {
resourceRegistryPublisher.delete(eService);
resourceRegistryPublisher.delete(hostingNode);
}
}
@Test(expected = SchemaViolationException.class)
public void testCreateEServiceAndDeleteRequiredConsistsOf() throws Exception {
EService eService = null;
try {
ERManagementTest erManagementTest = new ERManagementTest();
eService = erManagementTest.createEService();
@SuppressWarnings("unchecked")
IsIdentifiedBy<EService, SoftwareFacet> isIdentifiedBy = (IsIdentifiedBy<EService, SoftwareFacet>) eService.getConsistsOf(IsIdentifiedBy.class).get(0);
resourceRegistryPublisher.delete(isIdentifiedBy);
}finally {
resourceRegistryPublisher.delete(eService);
}
}
@Test(expected = SchemaViolationException.class)
public void testCreateEServiceAndDeleteRequiredFacet() throws Exception {
ERManagementTest erManagementTest = new ERManagementTest();
EService eService = erManagementTest.createEService();
@SuppressWarnings("unchecked")
IsIdentifiedBy<EService, SoftwareFacet> isIdentifiedBy = (IsIdentifiedBy<EService, SoftwareFacet>) eService.getConsistsOf(IsIdentifiedBy.class).get(0);
SoftwareFacet softwareFacet = isIdentifiedBy.getTarget();
try {
resourceRegistryPublisher.delete(softwareFacet);
}finally {
resourceRegistryPublisher.delete(eService);
}
}
@Test(expected = SchemaViolationException.class)
public void testCreateConsistsOfBeetweenResources() throws Exception {
ERManagementTest erManagementTest = new ERManagementTest();
Map<String, Resource> map = erManagementTest.createHostingNodeAndEService();
UUID hostingNodeUUID = map.get(HostingNode.NAME).getHeader().getUUID();
UUID eServiceUUID = map.get(EService.NAME).getHeader().getUUID();
HostingNode hostingNode = new HostingNodeImpl();
hostingNode.setHeader(new HeaderImpl(hostingNodeUUID));
SimpleFacet fakeEServiceAsSimpleFacet = new SimpleFacetImpl();
fakeEServiceAsSimpleFacet.setHeader(new HeaderImpl(eServiceUUID));
ConsistsOf<HostingNode, SimpleFacet> consistsOf = new ConsistsOfImpl<HostingNode, SimpleFacet>(hostingNode, fakeEServiceAsSimpleFacet);
try {
String json = ElementMapper.marshal(consistsOf);
json = json.replaceAll(SimpleFacet.NAME, EService.NAME);
resourceRegistryPublisher.create(json);
throw new Exception("A ConsistsOf has been created between two resoures. This should not happen");
} finally {
resourceRegistryPublisher.delete(map.get(EService.NAME));
resourceRegistryPublisher.delete(map.get(HostingNode.NAME));
}
}
}