283 lines
11 KiB
Java
283 lines
11 KiB
Java
package org.gcube.informationsystem.resourceregistry.instances.multicontext;
|
|
|
|
import java.net.URI;
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
|
|
import org.gcube.informationsystem.model.impl.properties.HeaderImpl;
|
|
import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl;
|
|
import org.gcube.informationsystem.model.reference.properties.Header;
|
|
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint;
|
|
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.AddConstraint;
|
|
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.RemoveConstraint;
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException;
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
|
|
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.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.ContactFacetImpl;
|
|
import org.gcube.resourcemanagement.model.impl.entities.facets.CoverageFacetImpl;
|
|
import org.gcube.resourcemanagement.model.impl.entities.facets.IdentifierFacetImpl;
|
|
import org.gcube.resourcemanagement.model.impl.entities.resources.DatasetImpl;
|
|
import org.gcube.resourcemanagement.model.impl.properties.ValueSchemaImpl;
|
|
import org.gcube.resourcemanagement.model.impl.relations.consistsof.HasOwnerImpl;
|
|
import org.gcube.resourcemanagement.model.impl.relations.consistsof.HasSpatialCoverageImpl;
|
|
import org.gcube.resourcemanagement.model.impl.relations.consistsof.IsIdentifiedByImpl;
|
|
import org.gcube.resourcemanagement.model.impl.relations.isrelatedto.IsCorrelatedToImpl;
|
|
import org.gcube.resourcemanagement.model.reference.entities.facets.ContactFacet;
|
|
import org.gcube.resourcemanagement.model.reference.entities.facets.CoverageFacet;
|
|
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.SoftwareFacet;
|
|
import org.gcube.resourcemanagement.model.reference.entities.resources.Dataset;
|
|
import org.gcube.resourcemanagement.model.reference.entities.resources.EService;
|
|
import org.gcube.resourcemanagement.model.reference.properties.ValueSchema;
|
|
import org.gcube.resourcemanagement.model.reference.relations.consistsof.HasCoverage;
|
|
import org.gcube.resourcemanagement.model.reference.relations.consistsof.HasOwner;
|
|
import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy;
|
|
import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.IsCorrelatedTo;
|
|
import org.junit.Assert;
|
|
import org.junit.Test;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
public class AddToContextTest extends MultiContextTest {
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(AddToContextTest.class);
|
|
|
|
@Test
|
|
public void testCreateEServiceAndRemoveFromContextRequiredFacet() throws Exception {
|
|
EService eService = ERManagementTest.createEService();
|
|
|
|
@SuppressWarnings("unchecked")
|
|
IsIdentifiedBy<EService, SoftwareFacet> isIdentifiedBy = (IsIdentifiedBy<EService, SoftwareFacet>) eService.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) {
|
|
ERManagementTest.deleteResource(eService);
|
|
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) {
|
|
ERManagementTest.deleteResource(eService);
|
|
throw e;
|
|
}
|
|
|
|
ERManagementTest.deleteResource(eService);
|
|
|
|
}
|
|
|
|
protected Dataset createDataset(String id, String uuidString) throws Exception {
|
|
Dataset dataset = new DatasetImpl();
|
|
dataset.setHeader(new HeaderImpl(UUID.fromString(uuidString)));
|
|
|
|
IdentifierFacet identifierFacet = new IdentifierFacetImpl();
|
|
identifierFacet.setValue(id);
|
|
identifierFacet.setType(IdentificationType.STRING);
|
|
identifierFacet.setPersistent(false);
|
|
IsIdentifiedBy<Dataset, IdentifierFacet> isIdentifiedBy = new IsIdentifiedByImpl<>(dataset, identifierFacet);
|
|
dataset.addFacet(isIdentifiedBy);
|
|
|
|
ContactFacet contactFacet = new ContactFacetImpl();
|
|
contactFacet.setTitle("Dr.");
|
|
contactFacet.setName("Luca");
|
|
contactFacet.setSurname("Frosini");
|
|
contactFacet.setEMail("luca.frosini@d4science.org");
|
|
HasOwner<Dataset, ContactFacet> hasOwner = new HasOwnerImpl<>(dataset, contactFacet);
|
|
dataset.addFacet(hasOwner);
|
|
|
|
CoverageFacet coverageFacet = new CoverageFacetImpl();
|
|
ValueSchema coverage = new ValueSchemaImpl();
|
|
coverage.setValue("Test");
|
|
coverage.setSchema(new URI("String"));
|
|
coverageFacet.setCoverage(coverage);
|
|
HasCoverage<Dataset, CoverageFacet> hasCoverage = new HasSpatialCoverageImpl<Dataset, CoverageFacet>(dataset, coverageFacet);
|
|
dataset.addFacet(hasCoverage);
|
|
|
|
try {
|
|
deleteResource(dataset);
|
|
}catch (NotFoundException e) {
|
|
// OK
|
|
logger.trace("OK");
|
|
}
|
|
|
|
return createResource(dataset);
|
|
}
|
|
|
|
protected PropagationConstraint getPropagationConstraint() {
|
|
PropagationConstraint propagationConstraint = new PropagationConstraintImpl();
|
|
propagationConstraint.setAddConstraint(AddConstraint.propagate);
|
|
propagationConstraint.setRemoveConstraint(RemoveConstraint.cascade);
|
|
return propagationConstraint;
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
protected IsCorrelatedTo<Dataset, Dataset> createIsCorrelatedTo(Dataset source, Dataset target) throws Exception {
|
|
PropagationConstraint propagationConstraint = getPropagationConstraint();
|
|
IsCorrelatedTo<Dataset, Dataset> isCorrelatedTo = new IsCorrelatedToImpl<Dataset, Dataset>(source, target, propagationConstraint);
|
|
|
|
IsRelatedToManagement isRelatedToManagement = getIsRelatedToManagement(isCorrelatedTo);
|
|
String json = isRelatedToManagement.create();
|
|
|
|
isCorrelatedTo = ElementMapper.unmarshal(isCorrelatedTo.getClass(), json);
|
|
|
|
return isCorrelatedTo;
|
|
}
|
|
|
|
@Test
|
|
public void testAddToContextFromDifferentSourceContext() throws Exception {
|
|
setContextByName(GCUBE);
|
|
|
|
Dataset datasetR1 = createDataset("R1", "da111111-dada-1111-1111-111111111111");
|
|
Dataset datasetR2 = null;
|
|
Dataset datasetR3 = null;
|
|
try {
|
|
addToContextThenTestIfBehaveProperly(datasetR1, false, DEVNEXT);
|
|
addToContextThenTestIfBehaveProperly(datasetR1, false, DEVSEC);
|
|
|
|
setContextByName(DEVNEXT);
|
|
datasetR2 = createDataset("R2", "da222222-dada-2222-2222-222222222222");
|
|
IsCorrelatedTo<Dataset, Dataset> isCorrelatedToR1R2 = createIsCorrelatedTo(datasetR1, datasetR2);
|
|
|
|
getResourceManagement(datasetR1).exists();
|
|
getIsRelatedToManagement(isCorrelatedToR1R2).exists();
|
|
getResourceManagement(datasetR2).exists();
|
|
|
|
|
|
setContextByName(DEVSEC);
|
|
datasetR3 = createDataset("R3", "da333333-dada-3333-3333-333333333333");
|
|
IsCorrelatedTo<Dataset, Dataset> isCorrelatedToR1R3 = createIsCorrelatedTo(datasetR1, datasetR3);
|
|
|
|
getResourceManagement(datasetR1).exists();
|
|
getIsRelatedToManagement(isCorrelatedToR1R3).exists();
|
|
getResourceManagement(datasetR3).exists();
|
|
|
|
|
|
try {
|
|
getIsRelatedToManagement(isCorrelatedToR1R2).exists();
|
|
}catch (AvailableInAnotherContextException e) {
|
|
// OK
|
|
logger.trace("As expected");
|
|
}
|
|
|
|
try {
|
|
getResourceManagement(datasetR2).exists();
|
|
}catch (AvailableInAnotherContextException e) {
|
|
// OK
|
|
logger.trace("As expected");
|
|
}
|
|
|
|
|
|
|
|
setContextByName(DEVNEXT);
|
|
try {
|
|
getIsRelatedToManagement(isCorrelatedToR1R3).exists();
|
|
}catch (AvailableInAnotherContextException e) {
|
|
// OK
|
|
logger.trace("As expected");
|
|
}
|
|
|
|
try {
|
|
getResourceManagement(datasetR3).exists();
|
|
}catch (AvailableInAnotherContextException e) {
|
|
// OK
|
|
logger.trace("As expected");
|
|
}
|
|
|
|
|
|
|
|
setContextByName(GCUBE);
|
|
getResourceManagement(datasetR1).exists();
|
|
try {
|
|
getIsRelatedToManagement(isCorrelatedToR1R2).exists();
|
|
}catch (AvailableInAnotherContextException e) {
|
|
// OK
|
|
logger.trace("As expected");
|
|
}
|
|
|
|
try {
|
|
getResourceManagement(datasetR2).exists();
|
|
}catch (AvailableInAnotherContextException e) {
|
|
// OK
|
|
logger.trace("As expected");
|
|
}
|
|
|
|
try {
|
|
getIsRelatedToManagement(isCorrelatedToR1R3).exists();
|
|
}catch (AvailableInAnotherContextException e) {
|
|
// OK
|
|
logger.trace("As expected");
|
|
}
|
|
|
|
try {
|
|
getResourceManagement(datasetR3).exists();
|
|
}catch (AvailableInAnotherContextException e) {
|
|
// OK
|
|
logger.trace("As expected");
|
|
}
|
|
|
|
ContextUtility.getHierarchicalMode().set(true);
|
|
ContextUtility.getIncludeInstanceContexts().set(true);
|
|
|
|
Dataset r1 = ElementMapper.unmarshal(Dataset.class, getResourceManagement(datasetR1).read());
|
|
@SuppressWarnings("unchecked")
|
|
List<String> contextsR1 = (List<String>) r1.getHeader().getAdditionalProperty(Header.__CONTEXTS);
|
|
Assert.assertTrue(contextsR1.size()==3);
|
|
|
|
Dataset r2 = ElementMapper.unmarshal(Dataset.class, getResourceManagement(datasetR2).read());
|
|
@SuppressWarnings("unchecked")
|
|
List<String> contextsR2 = (List<String>) r2.getHeader().getAdditionalProperty(Header.__CONTEXTS);
|
|
Assert.assertTrue(contextsR2.size()==1);
|
|
|
|
Dataset r3 = ElementMapper.unmarshal(Dataset.class, getResourceManagement(datasetR3).read());
|
|
@SuppressWarnings("unchecked")
|
|
List<String> contextsR3 = (List<String>) r3.getHeader().getAdditionalProperty(Header.__CONTEXTS);
|
|
Assert.assertTrue(contextsR3.size()==1);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
logger.error("", e);
|
|
throw e;
|
|
}finally {
|
|
ContextUtility.getHierarchicalMode().set(false);
|
|
ContextUtility.getIncludeInstanceContexts().set(false);
|
|
|
|
setContextByName(GCUBE);
|
|
deleteResource(datasetR1);
|
|
|
|
if(datasetR2!=null) {
|
|
setContextByName(DEVNEXT);
|
|
deleteResource(datasetR2);
|
|
}
|
|
|
|
if(datasetR3!=null) {
|
|
setContextByName(DEVSEC);
|
|
deleteResource(datasetR3);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|