Fixing and improving tests

This commit is contained in:
Luca Frosini 2021-03-02 16:39:21 +01:00
parent 951aa750e1
commit a684479858
1 changed files with 61 additions and 12 deletions

View File

@ -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<Configuration, IdentifierFacet> isIdentifiedBy = new IsIdentifiedByImpl<Configuration, IdentifierFacet>(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();
}