Reorganizing tests

This commit is contained in:
Luca Frosini 2021-03-08 09:56:23 +01:00
parent 5fc6288a65
commit d0fe1fc2ec
4 changed files with 149 additions and 118 deletions

View File

@ -1,52 +0,0 @@
package org.gcube.informationsystem.resourceregistry.instances;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ServiceLoader;
import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.types.TypeMapper;
import org.gcube.informationsystem.utils.discovery.ElementSpecilizationDiscovery;
import org.gcube.informationsystem.utils.discovery.RegistrationProvider;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class DiscoveryTest {
private static Logger logger = LoggerFactory.getLogger(DiscoveryTest.class);
@Test
public void discover() throws Exception {
ServiceLoader<? extends RegistrationProvider> regsitrationProviders = ServiceLoader
.load(RegistrationProvider.class);
List<Package> packages = new ArrayList<>();
for(RegistrationProvider registrationProvider : regsitrationProviders) {
packages.addAll(registrationProvider.getPackagesToRegister());
}
Package[] packagesArray = packages.stream().toArray(Package[]::new);
ElementSpecilizationDiscovery<Resource> resourceDiscovery = new ElementSpecilizationDiscovery<>(Resource.class);
Arrays.stream(packagesArray).forEach(p -> resourceDiscovery.addPackage(p));
resourceDiscovery.discover();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
boolean first = true;
for (Class<? extends Resource> resource : resourceDiscovery.getDiscovered()) {
if(first) {
first = false;
}else {
stringBuilder.append(",");
}
stringBuilder.append(TypeMapper.serializeType(resource));
}
stringBuilder.append("]");
logger.debug(stringBuilder.toString());
}
}

View File

@ -11,7 +11,6 @@ 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.schema.SchemaViolationException;
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
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;
@ -84,7 +83,6 @@ public class InvalidInstancesTest extends ContextTest {
@Test(expected = SchemaViolationException.class)
public void testCreateStandAloneFacet() throws Exception {
CPUFacet cpuFacet = new CPUFacetImpl();
cpuFacet.setClockSpeed("1 GHz");
cpuFacet.setModel("Opteron");
@ -166,7 +164,6 @@ public class InvalidInstancesTest extends ContextTest {
}
@Test(expected = SchemaViolationException.class)
public void testCreateEServiceAndDeleteRequiredConsistsOf() throws Exception {
EService eService = ERManagementTest.instantiateValidEService();
@ -184,12 +181,6 @@ public class InvalidInstancesTest extends ContextTest {
try {
consistsOfManagement.delete();
throw new Exception("You should not be able to delete a mandatory ConsistsOf");
}catch (SchemaViolationException e) {
// As expected
throw e;
}catch (Exception e) {
throw e;
}finally {
resourceManagement.delete();
}
@ -217,63 +208,12 @@ public class InvalidInstancesTest extends ContextTest {
try {
facetManagement.delete();
throw new Exception("You should not be able to delete a mandatory Facet");
}catch (SchemaViolationException e) {
// As expected
throw e;
}catch (Exception e) {
throw e;
}finally {
resourceManagement.delete();
}
}
@Test
public void testCreateEServiceAndRemoveFromContextRequiredFacet() throws Exception {
EService eService = ERManagementTest.instantiateValidEService();
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setElementType(EService.NAME);
resourceManagement.setJson(ElementMapper.marshal(eService));
String createEServiceString = resourceManagement.create();
EService createEService = ElementMapper.unmarshal(EService.class, createEServiceString);
@SuppressWarnings("unchecked")
IsIdentifiedBy<EService, SoftwareFacet> isIdentifiedBy = (IsIdentifiedBy<EService, SoftwareFacet>) createEService.getConsistsOf(IsIdentifiedBy.class).get(0);
ConsistsOfManagement consistsOfManagement = new ConsistsOfManagement();
consistsOfManagement.setElementType(IsIdentifiedBy.NAME);
consistsOfManagement.setUUID(isIdentifiedBy.getHeader().getUUID());
try {
consistsOfManagement.removeFromContext(ContextUtility.getCurrentSecurityContext().getUUID());
throw new Exception("You should not be able to delete a mandatory ConsistsOf");
}catch (SchemaViolationException e) {
// As expected
}catch (Exception e) {
resourceManagement.delete();
throw e;
}
SoftwareFacet softwareFacet = isIdentifiedBy.getTarget();
FacetManagement facetManagement = new FacetManagement();
facetManagement.setElementType(SoftwareFacet.NAME);
facetManagement.setUUID(softwareFacet.getHeader().getUUID());
try {
facetManagement.removeFromContext(ContextUtility.getCurrentSecurityContext().getUUID());
throw new Exception("You should not be able to delete a mandatory Facet");
}catch (SchemaViolationException e) {
// As expected
}catch (Exception e) {
resourceManagement.delete();
throw e;
}
resourceManagement.delete();
}
@Test(expected = SchemaViolationException.class)
public void testCreateConsistsOfBeetweenResources() throws Exception {
Map<String, Resource> map = ERManagementTest.createHostingNodeAndEService();
@ -299,12 +239,6 @@ public class InvalidInstancesTest extends ContextTest {
consistsOfManagement.create();
throw new Exception("A ConsistsOf has been created between two resoures. This should not happen");
} catch (ResourceRegistryException e) {
logger.error("Sounds good. A {} cannot be created between two resources", ConsistsOf.NAME, e);
throw e;
} catch (Exception e) {
throw e;
} finally {
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setUUID(eServiceUUID);

View File

@ -0,0 +1,61 @@
package org.gcube.informationsystem.resourceregistry.instances.multicontext;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException;
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
import org.gcube.informationsystem.resourceregistry.instances.ERManagementTest;
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.utils.ElementMapper;
import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet;
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 {
@Test
public void testCreateEServiceAndRemoveFromContextRequiredFacet() throws Exception {
EService eService = ERManagementTest.instantiateValidEService();
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setElementType(EService.NAME);
resourceManagement.setJson(ElementMapper.marshal(eService));
String createEServiceString = resourceManagement.create();
EService createEService = ElementMapper.unmarshal(EService.class, createEServiceString);
@SuppressWarnings("unchecked")
IsIdentifiedBy<EService, SoftwareFacet> isIdentifiedBy = (IsIdentifiedBy<EService, SoftwareFacet>) createEService.getConsistsOf(IsIdentifiedBy.class).get(0);
ConsistsOfManagement consistsOfManagement = new ConsistsOfManagement();
consistsOfManagement.setElementType(IsIdentifiedBy.NAME);
consistsOfManagement.setUUID(isIdentifiedBy.getHeader().getUUID());
try {
consistsOfManagement.removeFromContext(ContextUtility.getCurrentSecurityContext().getUUID());
throw new Exception("You should not be able to delete a mandatory ConsistsOf");
}catch (SchemaViolationException e) {
// As expected
}catch (Exception e) {
resourceManagement.delete();
throw e;
}
SoftwareFacet softwareFacet = isIdentifiedBy.getTarget();
FacetManagement facetManagement = new FacetManagement();
facetManagement.setElementType(SoftwareFacet.NAME);
facetManagement.setUUID(softwareFacet.getHeader().getUUID());
try {
facetManagement.removeFromContext(ContextUtility.getCurrentSecurityContext().getUUID());
throw new Exception("You should not be able to delete a mandatory Facet");
}catch (SchemaViolationException e) {
// As expected
}catch (Exception e) {
resourceManagement.delete();
throw e;
}
resourceManagement.delete();
}
}

View File

@ -0,0 +1,88 @@
package org.gcube.informationsystem.resourceregistry.types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ServiceLoader;
import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.types.TypeMapper;
import org.gcube.informationsystem.utils.discovery.ElementSpecilizationDiscovery;
import org.gcube.informationsystem.utils.discovery.RegistrationProvider;
import org.gcube.resourcemanagement.model.reference.entities.resources.Actor;
import org.gcube.resourcemanagement.model.reference.entities.resources.ConcreteDataset;
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.Dataset;
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.LegalBody;
import org.gcube.resourcemanagement.model.reference.entities.resources.Person;
import org.gcube.resourcemanagement.model.reference.entities.resources.Plugin;
import org.gcube.resourcemanagement.model.reference.entities.resources.RunningPlugin;
import org.gcube.resourcemanagement.model.reference.entities.resources.Schema;
import org.gcube.resourcemanagement.model.reference.entities.resources.Service;
import org.gcube.resourcemanagement.model.reference.entities.resources.Site;
import org.gcube.resourcemanagement.model.reference.entities.resources.Software;
import org.gcube.resourcemanagement.model.reference.entities.resources.VirtualMachine;
import org.gcube.resourcemanagement.model.reference.entities.resources.VirtualService;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class DiscoveryTest {
private static Logger logger = LoggerFactory.getLogger(DiscoveryTest.class);
@Test
public void discover() throws Exception {
ServiceLoader<? extends RegistrationProvider> regsitrationProviders = ServiceLoader
.load(RegistrationProvider.class);
List<Package> packages = new ArrayList<>();
for(RegistrationProvider registrationProvider : regsitrationProviders) {
packages.addAll(registrationProvider.getPackagesToRegister());
}
Package[] packagesArray = packages.stream().toArray(Package[]::new);
ElementSpecilizationDiscovery<Resource> resourceDiscovery = new ElementSpecilizationDiscovery<>(Resource.class);
Arrays.stream(packagesArray).forEach(p -> resourceDiscovery.addPackage(p));
resourceDiscovery.discover();
List<Class<? extends Resource>> expected = new ArrayList<>();
expected.add(Resource.class);
expected.add(Actor.class);
expected.add(ConcreteDataset.class);
expected.add(Configuration.class);
expected.add(ConfigurationTemplate.class);
expected.add(Dataset.class);
expected.add(EService.class);
expected.add(GCubeResource.class);
expected.add(HostingNode.class);
expected.add(LegalBody.class);
expected.add(Person.class);
expected.add(Plugin.class);
expected.add(RunningPlugin.class);
expected.add(Schema.class);
expected.add(Service.class);
expected.add(Site.class);
expected.add(Software.class);
expected.add(VirtualMachine.class);
expected.add(VirtualService.class);
List<Class<? extends Resource>> found = resourceDiscovery.getDiscovered();
for (Class<? extends Resource> resource : found) {
String typeDefinition = TypeMapper.serializeType(resource);
logger.debug(typeDefinition);
}
Assert.assertTrue(expected.containsAll(found));
Assert.assertTrue(found.containsAll(expected));
}
}