314 lines
14 KiB
Java
314 lines
14 KiB
Java
package org.gcube.informationsystem.resourceregistry.instances.multicontext;
|
|
|
|
import java.net.URI;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
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.contexts.entities.ContextManagement;
|
|
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, String uuid) throws Exception {
|
|
PropagationConstraint propagationConstraint = getPropagationConstraint();
|
|
IsCorrelatedTo<Dataset, Dataset> isCorrelatedTo = new IsCorrelatedToImpl<Dataset, Dataset>(source, target, propagationConstraint);
|
|
isCorrelatedTo.setHeader(new HeaderImpl(UUID.fromString(uuid)));
|
|
|
|
IsRelatedToManagement isRelatedToManagement = getIsRelatedToManagement(isCorrelatedTo);
|
|
String json = isRelatedToManagement.create();
|
|
|
|
isCorrelatedTo = ElementMapper.unmarshal(isCorrelatedTo.getClass(), json);
|
|
|
|
return isCorrelatedTo;
|
|
}
|
|
|
|
protected void checkNumerOfContext(Dataset datasetR1, int r1Number, Dataset datasetR2, int r2Number, Dataset datasetR3, int r3Number) throws Exception {
|
|
setContextByName(GCUBE);
|
|
|
|
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);
|
|
Set<UUID> contextsR1UUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(contextsR1);
|
|
Set<String> contextsR1Fullname = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(contextsR1UUID);
|
|
logger.info("Contexts of {} with UUID {} and Id {} are {}", Dataset.NAME, r1.getHeader().getUUID(), ((IdentifierFacet)r1.getIdentificationFacets().get(0)).getValue(), contextsR1Fullname);
|
|
Assert.assertTrue(contextsR1.size()==r1Number);
|
|
|
|
Dataset r2 = ElementMapper.unmarshal(Dataset.class, getResourceManagement(datasetR2).read());
|
|
@SuppressWarnings("unchecked")
|
|
List<String> contextsR2 = (List<String>) r2.getHeader().getAdditionalProperty(Header.__CONTEXTS);
|
|
Set<UUID> contextsR2UUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(contextsR2);
|
|
Set<String> contextsR2Fullname = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(contextsR2UUID);
|
|
logger.info("Contexts of {} with UUID {} and Id {} are {}", Dataset.NAME, r2.getHeader().getUUID(), ((IdentifierFacet)r2.getIdentificationFacets().get(0)).getValue(), contextsR2Fullname);
|
|
Assert.assertTrue(contextsR2.size()==r2Number);
|
|
|
|
Dataset r3 = ElementMapper.unmarshal(Dataset.class, getResourceManagement(datasetR3).read());
|
|
@SuppressWarnings("unchecked")
|
|
List<String> contextsR3 = (List<String>) r3.getHeader().getAdditionalProperty(Header.__CONTEXTS);
|
|
Set<UUID> contextsR3UUID = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextUUIDSet(contextsR3);
|
|
Set<String> contextsR3Fullname = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextFullNameSet(contextsR3UUID);
|
|
logger.info("Contexts of {} with UUID {} and Id {} are {}", Dataset.NAME, r3.getHeader().getUUID(), ((IdentifierFacet)r3.getIdentificationFacets().get(0)).getValue(), contextsR3Fullname);
|
|
Assert.assertTrue(contextsR3.size()==r3Number);
|
|
|
|
ContextUtility.getHierarchicalMode().set(false);
|
|
ContextUtility.getIncludeInstanceContexts().set(false);
|
|
}
|
|
|
|
@Test
|
|
public void testAddToContextFromDifferentSourceContext() throws Exception {
|
|
setContextByName(GCUBE);
|
|
|
|
/* Needed to init context cache */
|
|
ContextManagement contextManagement = new ContextManagement();
|
|
contextManagement.all(false);
|
|
|
|
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, "da111111-1111-aaaa-2222-222222222222");
|
|
|
|
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, "da111111-1111-aaaa-3333-333333333333");
|
|
|
|
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");
|
|
}
|
|
|
|
checkNumerOfContext(datasetR1, 3, datasetR2, 1, datasetR3, 1);
|
|
|
|
setContextByName(DEVNEXT);
|
|
addToContextThenTestIfBehaveProperly(datasetR1, false, NEXTNEXT);
|
|
checkNumerOfContext(datasetR1, 4, datasetR2, 2, datasetR3, 1);
|
|
|
|
setContextByName(DEVSEC);
|
|
addToContextThenTestIfBehaveProperly(datasetR1, false, DEVVRE);
|
|
checkNumerOfContext(datasetR1, 5, datasetR2, 2, datasetR3, 2);
|
|
|
|
} 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);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|