From a684479858340c3e368ee40821c17cb7da7db858 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 2 Mar 2021 16:39:21 +0100 Subject: [PATCH] Fixing and improving tests --- .../instances/ERManagementTest.java | 73 ++++++++++++++++--- 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/ERManagementTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/ERManagementTest.java index 37746e2..37c4d2e 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/ERManagementTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/ERManagementTest.java @@ -43,12 +43,14 @@ 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; +import org.gcube.resourcemanagement.model.impl.entities.facets.IdentifierFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.facets.LicenseFacetImpl; 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.SimpleFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.facets.SoftwareFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.facets.StateFacetImpl; +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.entities.resources.RunningPluginImpl; @@ -61,6 +63,8 @@ import org.gcube.resourcemanagement.model.reference.entities.facets.AccessPointF import org.gcube.resourcemanagement.model.reference.entities.facets.CPUFacet; import org.gcube.resourcemanagement.model.reference.entities.facets.ContactFacet; import org.gcube.resourcemanagement.model.reference.entities.facets.EventFacet; +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; import org.gcube.resourcemanagement.model.reference.entities.facets.MemoryFacet; import org.gcube.resourcemanagement.model.reference.entities.facets.MemoryFacet.MemoryUnit; @@ -95,16 +99,13 @@ public class ERManagementTest extends ContextTest { private static Logger logger = LoggerFactory .getLogger(ERManagementTest.class); - @Test - public void testCreateFacetWithEncrypted() throws Exception { + @Test(expected = SchemaViolationException.class) + public void testCreateFacet() throws Exception { + CPUFacet cpuFacet = new CPUFacetImpl(); cpuFacet.setClockSpeed("1 GHz"); cpuFacet.setModel("Opteron"); cpuFacet.setVendor("AMD"); - Encrypted encrypted = new EncryptedImpl(); - String encryptedValue = EncryptedImpl.encrypt("plain-value"); - encrypted.setEncryptedValue(encryptedValue); - cpuFacet.setAdditionalProperty("test", encrypted); FacetManagement facetManagement = new FacetManagement(); facetManagement.setElementType(CPUFacet.NAME); @@ -113,13 +114,61 @@ public class ERManagementTest extends ContextTest { facetManagement.setJson(json); /* A facet cannot be created per se */ - String cpuFacetJson = facetManagement.create(); - CPUFacet createdCpuFacet = ElementMapper.unmarshal(CPUFacet.class, - cpuFacetJson); - logger.debug("Created:\nRaw Json : {}\nUnmarshalled : {}", - cpuFacetJson, createdCpuFacet); + facetManagement.create(); + } + + + @Test + public void testCreateFacetWithEncrypted() throws Exception { + /* + * A facet cannot be created per se. Going to create a Configuration which + * does not impose any particular constraint except the IdentifierFact + */ - facetManagement.delete(); + Configuration configuration = new ConfigurationImpl(); + + IdentifierFacet identifierFacet = new IdentifierFacetImpl(); + identifierFacet.setType(IdentificationType.STRING); + identifierFacet.setValue("MyID"); + identifierFacet.setPersistent(false); + + IsIdentifiedBy isIdentifiedBy = new IsIdentifiedByImpl(configuration, identifierFacet); + configuration.addFacet(isIdentifiedBy); + + + CPUFacet cpuFacet = new CPUFacetImpl(); + cpuFacet.setClockSpeed("1 GHz"); + cpuFacet.setModel("Opteron"); + cpuFacet.setVendor("AMD"); + Encrypted encrypted = new EncryptedImpl(); + String plainValue = "plain-value"; + String encryptedValue = EncryptedImpl.encrypt(plainValue); + encrypted.setEncryptedValue(encryptedValue); + cpuFacet.setAdditionalProperty("test", encrypted); + configuration.addFacet(cpuFacet); + + ResourceManagement resourceManagement = new ResourceManagement(); + resourceManagement.setElementType(Configuration.NAME); + String json = ElementMapper.marshal(configuration); + logger.debug("{}", json); + resourceManagement.setJson(json); + + /* A facet cannot be created per se */ + String configurationJsonString = resourceManagement.create(); + + + Configuration createdConfiguration = ElementMapper.unmarshal(Configuration.class, + configurationJsonString); + logger.debug("Created:\nRaw Json : {}\nUnmarshalled : {}", + configurationJsonString, createdConfiguration); + + CPUFacet readCpuFacet = configuration.getFacets(CPUFacet.class).get(0); + String gotEncryptedValue = ((Encrypted) readCpuFacet.getAdditionalProperty("test")).getEncryptedValue(); + Assert.assertTrue(gotEncryptedValue.compareTo(encryptedValue)==0); + String gotPlainValue = EncryptedImpl.decrypt(gotEncryptedValue); + Assert.assertTrue(gotPlainValue.compareTo(plainValue)==0); + + resourceManagement.delete(); }