package org.gcube.informationsystem.resourceregistry.instances; 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.entity.resource.ResourceAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException; import org.gcube.informationsystem.resourceregistry.api.utils.Utility; 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; import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement; import org.gcube.informationsystem.utils.ElementMapper; import org.gcube.resourcemanagement.model.impl.entities.facets.CPUFacetImpl; import org.gcube.resourcemanagement.model.impl.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.Actor; 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.entities.resources.Service; import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy; import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Activates; 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 = ERManagementTest.createConfiguration(); EService eService = ERManagementTest.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 */ IsRelatedToManagement isRelatedToManagement = new IsRelatedToManagement(); isRelatedToManagement.setElementType(Activates.NAME); String json = ACTIVATES.replace("CONFIGURATION_UUID", configuration.getHeader().getUUID().toString()); json = json.replace("ESERVICE_UUID", eService.getHeader().getUUID().toString()); isRelatedToManagement.setJson(json); isRelatedToManagement.create(); }finally { ERManagementTest.deleteResource(configuration); ERManagementTest.deleteResource(eService); } } @Test(expected = ResourceAlreadyPresentException.class) public void testRecreate() throws Exception { EService eService = ERManagementTest.createEService(); try { ERManagementTest.createResource(eService); }finally { ERManagementTest.deleteResource(eService); } } @Test(expected = SchemaViolationException.class) public void testCreateStandAloneFacet() throws Exception { CPUFacet cpuFacet = new CPUFacetImpl(); cpuFacet.setClockSpeed("1 GHz"); cpuFacet.setModel("Opteron"); cpuFacet.setVendor("AMD"); FacetManagement facetManagement = new FacetManagement(); facetManagement.setElementType(CPUFacet.NAME); String json = ElementMapper.marshal(cpuFacet); logger.debug("{}", json); facetManagement.setJson(json); /* A facet cannot be created per se */ facetManagement.create(); } @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 isIdentifiedBy = new IsIdentifiedByImpl<>(runningPlugin, softwareFacet); runningPlugin.addFacet(isIdentifiedBy); ERManagementTest.createResource(runningPlugin); } @Test(expected = ResourceRegistryException.class) public void testCreateAnEntityDifferentFromDeclared() throws Exception { EService eService = ERManagementTest.instantiateValidEService(); ResourceManagement resourceManagement = new ResourceManagement(); resourceManagement.setElementType(Service.NAME); resourceManagement.setJson(ElementMapper.marshal(eService)); resourceManagement.create(); } @Test(expected = SchemaViolationException.class) public void testCreateAbstractEntity() throws Exception { ResourceManagement resourceManagement = new ResourceManagement(); resourceManagement.setElementType(Actor.NAME); resourceManagement.setJson(ACTOR); resourceManagement.create(); } @Test(expected = SchemaViolationException.class) public void testCreateHostingNodeAndEServiceWithSharedFacet() throws Exception { Map 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(); ConsistsOfManagement consistsOfManagement = new ConsistsOfManagement(); consistsOfManagement.setElementType(ConsistsOf.NAME); consistsOfManagement.setJson("{}"); ConsistsOf consistsOf = new ConsistsOfImpl<>(eService, shared); String consistsOfJsonString = ElementMapper.marshal(consistsOf); consistsOfManagement.setJson(consistsOfJsonString); String json = consistsOfManagement.create(); logger.debug("Created : {}", json); } finally { ERManagementTest.deleteResource(eService); ERManagementTest.deleteResource(hostingNode); } } @Test(expected = SchemaViolationException.class) public void testCreateEServiceAndDeleteRequiredConsistsOf() throws Exception { EService eService = null; try { eService = ERManagementTest.createEService(); @SuppressWarnings("unchecked") IsIdentifiedBy isIdentifiedBy = (IsIdentifiedBy) eService.getConsistsOf(IsIdentifiedBy.class).get(0); ConsistsOfManagement consistsOfManagement = new ConsistsOfManagement(); consistsOfManagement.setElementType(Utility.getTypeName(isIdentifiedBy)); consistsOfManagement.setUUID(isIdentifiedBy.getHeader().getUUID()); consistsOfManagement.delete(); }finally { ERManagementTest.deleteResource(eService); } } @Test(expected = SchemaViolationException.class) public void testCreateEServiceAndDeleteRequiredFacet() throws Exception { EService eService = ERManagementTest.createEService(); @SuppressWarnings("unchecked") IsIdentifiedBy isIdentifiedBy = (IsIdentifiedBy) eService.getConsistsOf(IsIdentifiedBy.class).get(0); ConsistsOfManagement consistsOfManagement = new ConsistsOfManagement(); consistsOfManagement.setElementType(IsIdentifiedBy.NAME); consistsOfManagement.setUUID(isIdentifiedBy.getHeader().getUUID()); SoftwareFacet softwareFacet = isIdentifiedBy.getTarget(); FacetManagement facetManagement = new FacetManagement(); facetManagement.setElementType(SoftwareFacet.NAME); facetManagement.setUUID(softwareFacet.getHeader().getUUID()); try { facetManagement.delete(); }finally { ERManagementTest.deleteResource(eService); } } @Test(expected = SchemaViolationException.class) public void testCreateConsistsOfBeetweenResources() throws Exception { Map 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 consistsOf = new ConsistsOfImpl(hostingNode, fakeEServiceAsSimpleFacet); try { ConsistsOfManagement consistsOfManagement = new ConsistsOfManagement(); consistsOfManagement.setElementType(ConsistsOf.NAME); String json = ElementMapper.marshal(consistsOf); json = json.replaceAll(SimpleFacet.NAME, EService.NAME); consistsOfManagement.setJson(json); consistsOfManagement.create(); throw new Exception("A ConsistsOf has been created between two resoures. This should not happen"); } finally { ERManagementTest.deleteResource(map.get(EService.NAME)); ERManagementTest.deleteResource(map.get(HostingNode.NAME)); } } }