Add to context add only the ERElement in the source context #12218
This commit is contained in:
parent
78d1f71aa6
commit
f17106e42e
|
@ -10,8 +10,10 @@ import org.gcube.common.authorization.library.AuthorizationEntry;
|
|||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.common.scope.impl.ScopeBean;
|
||||
import org.gcube.informationsystem.base.reference.IdentifiableElement;
|
||||
import org.gcube.informationsystem.context.reference.entities.Context;
|
||||
import org.gcube.informationsystem.context.reference.relations.IsParentOf;
|
||||
import org.gcube.informationsystem.model.reference.properties.Header;
|
||||
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
|
||||
|
@ -29,6 +31,7 @@ import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
|
|||
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
|
||||
import com.orientechnologies.orient.core.record.ODirection;
|
||||
import com.orientechnologies.orient.core.record.OVertex;
|
||||
import com.orientechnologies.orient.core.record.impl.ODocument;
|
||||
import com.orientechnologies.orient.core.sql.executor.OResult;
|
||||
import com.orientechnologies.orient.core.sql.executor.OResultSet;
|
||||
|
||||
|
@ -115,6 +118,8 @@ public class ContextUtility {
|
|||
}
|
||||
|
||||
public synchronized SecurityContext getSecurityContextByFullName(String fullName) throws ContextException {
|
||||
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
|
||||
ODatabaseDocument oDatabaseDocument = null;
|
||||
try {
|
||||
SecurityContext securityContext = null;
|
||||
|
||||
|
@ -128,14 +133,18 @@ public class ContextUtility {
|
|||
if(securityContext==null) {
|
||||
logger.trace("{} for {} is not in cache. Going to get it", SecurityContext.class.getSimpleName(),
|
||||
fullName);
|
||||
oDatabaseDocument = getAdminSecurityContext().getDatabaseDocument(PermissionMode.READER);
|
||||
|
||||
OVertex contextVertex = getContextVertexByFullName(fullName);
|
||||
OVertex contextVertex = getContextVertexByFullName(oDatabaseDocument, fullName);
|
||||
|
||||
uuid = Utility.getUUID(contextVertex);
|
||||
ODocument oDocument = contextVertex.getProperty(IdentifiableElement.HEADER_PROPERTY);
|
||||
|
||||
uuid = UUID.fromString(oDocument.getProperty(Header.UUID_PROPERTY));
|
||||
|
||||
securityContext = getSecurityContextByUUID(uuid, contextVertex);
|
||||
|
||||
addSecurityContext(fullName, securityContext);
|
||||
|
||||
}
|
||||
|
||||
return securityContext;
|
||||
|
@ -144,6 +153,14 @@ public class ContextUtility {
|
|||
throw e;
|
||||
} catch(Exception e) {
|
||||
throw new ContextException("Unable to retrieve Context UUID from current Context", e);
|
||||
} finally {
|
||||
if(oDatabaseDocument!=null) {
|
||||
oDatabaseDocument.close();
|
||||
}
|
||||
|
||||
if(current!=null) {
|
||||
current.activateOnCurrentThread();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -163,12 +180,21 @@ public class ContextUtility {
|
|||
|
||||
private OVertex getContextVertexByUUID(UUID uuid) throws ResourceRegistryException {
|
||||
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
|
||||
OVertex oVertex = Utility.getElementByUUID(getAdminSecurityContext().getDatabaseDocument(PermissionMode.READER), Context.NAME, uuid,
|
||||
ODatabaseDocument oDatabaseDocument = null;
|
||||
try {
|
||||
oDatabaseDocument = getAdminSecurityContext().getDatabaseDocument(PermissionMode.READER);
|
||||
OVertex oVertex = Utility.getElementByUUID(oDatabaseDocument, Context.NAME, uuid,
|
||||
OVertex.class);
|
||||
return oVertex;
|
||||
} finally {
|
||||
if(oDatabaseDocument!=null) {
|
||||
oDatabaseDocument.close();
|
||||
}
|
||||
|
||||
if(current!=null) {
|
||||
current.activateOnCurrentThread();
|
||||
}
|
||||
return oVertex;
|
||||
}
|
||||
}
|
||||
|
||||
private SecurityContext getSecurityContextByUUID(UUID uuid, OVertex contextVertex) throws ResourceRegistryException {
|
||||
|
@ -198,14 +224,14 @@ public class ContextUtility {
|
|||
return securityContext;
|
||||
}
|
||||
|
||||
/*
|
||||
protected UUID getContextUUIDFromFullName(String fullName) throws ResourceRegistryException {
|
||||
OVertex contextVertex = getContextVertexByFullName(fullName);
|
||||
return Utility.getUUID(contextVertex);
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
private OVertex getContextVertexByFullName(String fullName) throws ResourceRegistryException {
|
||||
|
||||
private OVertex getContextVertexByFullName(ODatabaseDocument oDatabaseDocument, String fullName) throws ResourceRegistryException {
|
||||
logger.trace("Going to get {} {} with full name '{}'", Context.NAME, OVertex.class.getSimpleName(), fullName);
|
||||
|
||||
ScopeBean scopeBean = new ScopeBean(fullName);
|
||||
|
@ -216,9 +242,7 @@ public class ContextUtility {
|
|||
Map<String, String> map = new HashMap<>();
|
||||
map.put("name", name);
|
||||
|
||||
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
|
||||
|
||||
OResultSet resultSet = getAdminSecurityContext().getDatabaseDocument(PermissionMode.READER).query(select, map);
|
||||
OResultSet resultSet = oDatabaseDocument.query(select, map);
|
||||
|
||||
if(resultSet == null || !resultSet.hasNext()) {
|
||||
throw new ContextNotFoundException("Error retrieving context with name " + fullName);
|
||||
|
@ -234,10 +258,6 @@ public class ContextUtility {
|
|||
+ "but required the one with path" + fullName + ". Please Reimplement the query");
|
||||
}
|
||||
|
||||
if(current!=null) {
|
||||
current.activateOnCurrentThread();
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
|
|
|
@ -308,13 +308,20 @@ public class SecurityContext {
|
|||
|
||||
public void addElement(OElement element) throws ResourceRegistryException {
|
||||
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
|
||||
ODatabaseDocument adminDatabaseDocument = getAdminDatabaseDocument();
|
||||
adminDatabaseDocument.activateOnCurrentThread();
|
||||
ODatabaseDocument adminDatabaseDocument = null;
|
||||
try {
|
||||
adminDatabaseDocument = getAdminDatabaseDocument();
|
||||
addElement(element, adminDatabaseDocument);
|
||||
}finally {
|
||||
if(adminDatabaseDocument!=null) {
|
||||
adminDatabaseDocument.close();
|
||||
}
|
||||
|
||||
if(current!=null) {
|
||||
current.activateOnCurrentThread();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void allow(OSecurity oSecurity, ODocument oDocument, boolean hierarchic) {
|
||||
String writerRoleName = getSecurityRoleOrUserName(PermissionMode.WRITER, SecurityType.ROLE, hierarchic);
|
||||
|
@ -325,18 +332,26 @@ public class SecurityContext {
|
|||
|
||||
public boolean isElementInContext(final OElement element) throws ResourceRegistryException {
|
||||
ORID orid = element.getIdentity();
|
||||
|
||||
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
|
||||
ODatabaseDocument contextODatabaseDocument = getDatabaseDocument(PermissionMode.READER);
|
||||
contextODatabaseDocument.activateOnCurrentThread();
|
||||
ODatabaseDocument contextODatabaseDocument = null;
|
||||
|
||||
try {
|
||||
contextODatabaseDocument = getDatabaseDocument(PermissionMode.READER);
|
||||
|
||||
ORecord oRecord = contextODatabaseDocument.getRecord(orid);
|
||||
logger.trace("{}", oRecord);
|
||||
return true;
|
||||
|
||||
} finally {
|
||||
if(contextODatabaseDocument!=null) {
|
||||
contextODatabaseDocument.close();
|
||||
}
|
||||
|
||||
if(current!=null) {
|
||||
current.activateOnCurrentThread();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void addElement(OElement element, ODatabaseDocument oDatabaseDocument) {
|
||||
|
@ -352,13 +367,20 @@ public class SecurityContext {
|
|||
|
||||
public void removeElement(OElement element) throws ResourceRegistryException {
|
||||
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
|
||||
ODatabaseDocument adminDatabaseDocument = getAdminDatabaseDocument();
|
||||
adminDatabaseDocument.activateOnCurrentThread();
|
||||
ODatabaseDocument adminDatabaseDocument = null;
|
||||
try {
|
||||
adminDatabaseDocument = getAdminDatabaseDocument();
|
||||
removeElement(element, adminDatabaseDocument);
|
||||
}finally {
|
||||
if(adminDatabaseDocument!=null) {
|
||||
adminDatabaseDocument.close();
|
||||
}
|
||||
|
||||
if(current!=null) {
|
||||
current.activateOnCurrentThread();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void deny(OSecurity oSecurity, ODocument oDocument, boolean hierarchical) {
|
||||
// The element could be created in such a context so the writerUser for the
|
||||
|
@ -395,8 +417,11 @@ public class SecurityContext {
|
|||
@Override
|
||||
public Boolean call() throws Exception {
|
||||
ContextUtility.getHierarchicalMode().set(false);
|
||||
ODatabaseDocument oDatabaseDocument = getDatabaseDocument(PermissionMode.READER);
|
||||
|
||||
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
|
||||
ODatabaseDocument oDatabaseDocument = null;
|
||||
try {
|
||||
oDatabaseDocument = getDatabaseDocument(PermissionMode.READER);
|
||||
oDatabaseDocument.activateOnCurrentThread();
|
||||
ORecord element = oDatabaseDocument.getRecord(oDocument.getIdentity());
|
||||
if(element == null) {
|
||||
|
@ -406,8 +431,14 @@ public class SecurityContext {
|
|||
} catch(Exception e) {
|
||||
return false;
|
||||
} finally {
|
||||
if(oDatabaseDocument!=null) {
|
||||
oDatabaseDocument.close();
|
||||
}
|
||||
|
||||
if(current!=null) {
|
||||
current.activateOnCurrentThread();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -422,18 +453,23 @@ public class SecurityContext {
|
|||
|
||||
public void create() throws ResourceRegistryException {
|
||||
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
|
||||
|
||||
ODatabaseDocument adminDatabaseDocument = getAdminDatabaseDocument();
|
||||
adminDatabaseDocument.activateOnCurrentThread();
|
||||
ODatabaseDocument adminDatabaseDocument = null;
|
||||
try {
|
||||
adminDatabaseDocument = getAdminDatabaseDocument();
|
||||
|
||||
create(adminDatabaseDocument);
|
||||
|
||||
adminDatabaseDocument.commit();
|
||||
} finally {
|
||||
if(adminDatabaseDocument!=null) {
|
||||
adminDatabaseDocument.close();
|
||||
}
|
||||
|
||||
if(current!=null) {
|
||||
current.activateOnCurrentThread();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected ORole addExtraRules(ORole role, PermissionMode permissionMode) {
|
||||
return role;
|
||||
|
@ -518,18 +554,25 @@ public class SecurityContext {
|
|||
|
||||
public void delete() throws ResourceRegistryException {
|
||||
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
|
||||
ODatabaseDocument adminDatabaseDocument = getAdminDatabaseDocument();
|
||||
adminDatabaseDocument.activateOnCurrentThread();
|
||||
ODatabaseDocument adminDatabaseDocument = null;
|
||||
try {
|
||||
adminDatabaseDocument = getAdminDatabaseDocument();
|
||||
|
||||
delete(adminDatabaseDocument);
|
||||
|
||||
adminDatabaseDocument.commit();
|
||||
} finally {
|
||||
if(adminDatabaseDocument!=null) {
|
||||
adminDatabaseDocument.close();
|
||||
}
|
||||
|
||||
if(current!=null) {
|
||||
current.activateOnCurrentThread();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected void removeChildrenHRolesFromParents(OSecurity oSecurity) {
|
||||
Set<SecurityContext> parents = getAllParents();
|
||||
Set<SecurityContext> allChildren = getAllChildren();
|
||||
|
|
|
@ -776,7 +776,8 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
|
|||
logger.debug("Going to delete {} with UUID {}", accessType.getName(), uuid);
|
||||
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
|
||||
try {
|
||||
oDatabaseDocument = ContextUtility.getAdminSecurityContext().getDatabaseDocument(PermissionMode.WRITER);
|
||||
// oDatabaseDocument = ContextUtility.getAdminSecurityContext().getDatabaseDocument(PermissionMode.WRITER);
|
||||
oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.WRITER);
|
||||
oDatabaseDocument.begin();
|
||||
setAsEntryPoint();
|
||||
|
||||
|
@ -1224,6 +1225,14 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
|
|||
// https://www.orientdb.com/docs/last/java/Graph-Schema-Property.html#using-constraints
|
||||
// Going to validate them here
|
||||
|
||||
if(operation.isSafe()) {
|
||||
/*
|
||||
* The sanity check is not required for a safe operation.
|
||||
*/
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Set<PropertyDefinition> definedProperties = getAllProperties();
|
||||
|
||||
if(definedProperties==null) {
|
||||
|
|
|
@ -113,12 +113,11 @@ public class FacetManagement extends EntityManagement<Facet, FacetType> {
|
|||
if(!entryPoint) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(operation.isSafe()) {
|
||||
/* You should not be here.
|
||||
* The sanity check should not be triggered for a safety operation.
|
||||
* Anyway, using this code as guard.
|
||||
/*
|
||||
* The sanity check is not required for a safe operation.
|
||||
*/
|
||||
logger.warn("sanityCheck should not be triggered for a safe method (i.e. {}). It is not an error but it slow down the performace. Please contact the developer", operation.toString());
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -161,6 +160,7 @@ public class FacetManagement extends EntityManagement<Facet, FacetType> {
|
|||
}
|
||||
|
||||
resourceManagement.setOperation(operation);
|
||||
|
||||
resourceManagement.sanityCheck();
|
||||
|
||||
}catch (ResourceRegistryException e) {
|
||||
|
|
|
@ -23,6 +23,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resour
|
|||
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException;
|
||||
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
|
||||
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode;
|
||||
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility;
|
||||
import org.gcube.informationsystem.resourceregistry.instances.model.Operation;
|
||||
|
@ -35,6 +36,7 @@ import org.gcube.informationsystem.resourceregistry.utils.Utility;
|
|||
import org.gcube.informationsystem.types.reference.entities.ResourceType;
|
||||
import org.gcube.informationsystem.types.reference.properties.LinkedEntity;
|
||||
|
||||
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
|
||||
import com.orientechnologies.orient.core.metadata.schema.OClass;
|
||||
import com.orientechnologies.orient.core.record.ODirection;
|
||||
import com.orientechnologies.orient.core.record.OEdge;
|
||||
|
@ -304,10 +306,12 @@ public class ResourceManagement extends EntityManagement<Resource, ResourceType>
|
|||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String all(boolean polymorphic) throws ResourceRegistryException {
|
||||
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
|
||||
try {
|
||||
oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.READER);
|
||||
|
||||
return reallyGetAll(polymorphic);
|
||||
} catch(ResourceRegistryException e) {
|
||||
throw e;
|
||||
|
@ -317,6 +321,9 @@ public class ResourceManagement extends EntityManagement<Resource, ResourceType>
|
|||
if(oDatabaseDocument != null) {
|
||||
oDatabaseDocument.close();
|
||||
}
|
||||
if(current!=null) {
|
||||
current.activateOnCurrentThread();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -404,6 +411,13 @@ public class ResourceManagement extends EntityManagement<Resource, ResourceType>
|
|||
*/
|
||||
@Override
|
||||
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
|
||||
if(operation.isSafe()) {
|
||||
/*
|
||||
* The sanity check is not required for a safe operation.
|
||||
*/
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// In case of a resource is deleted due to cascade effect is look like is the entry point
|
||||
// of the operation and the sanity check is not required. The Resource and all its facets are deleted.
|
||||
|
|
|
@ -94,11 +94,9 @@ public class ConsistsOfManagement extends RelationManagement<FacetManagement, Fa
|
|||
}
|
||||
|
||||
if(operation.isSafe()) {
|
||||
/* You should not be here.
|
||||
* The sanity check should not be triggered for a safety operation.
|
||||
* Anyway, using this code as guard.
|
||||
/*
|
||||
* The sanity check is not required for a safe operation.
|
||||
*/
|
||||
logger.warn("sanityCheck should not be triggered for a safe method (i.e. {}). It is not an error but it slow down the performace. Please contact the developer", operation.toString());
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -99,8 +99,7 @@ public class IsRelatedToManagement extends RelationManagement<ResourceManagement
|
|||
|
||||
@Override
|
||||
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
super.sanityCheck();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -36,6 +36,12 @@ public class ContextTest {
|
|||
|
||||
public static final String DEFAULT_TEST_SCOPE_ANOTHER_USER;
|
||||
|
||||
public static final String GCUBE;
|
||||
public static final String DEVNEXT;
|
||||
public static final String NEXTNEXT;
|
||||
public static final String DEVSEC;
|
||||
public static final String DEVVRE;
|
||||
|
||||
static {
|
||||
properties = new Properties();
|
||||
InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME);
|
||||
|
@ -51,6 +57,11 @@ public class ContextTest {
|
|||
// DEFAULT_TEST_SCOPE_NAME = PARENT_DEFAULT_TEST_SCOPE + "/preprod";
|
||||
// ALTERNATIVE_TEST_SCOPE = DEFAULT_TEST_SCOPE_NAME + "/preVRE";
|
||||
|
||||
GCUBE = "/gcube";
|
||||
DEVNEXT = GCUBE + "/devNext";
|
||||
NEXTNEXT = DEVNEXT + "/NextNext";
|
||||
DEVSEC = GCUBE + "/devsec";
|
||||
DEVVRE = DEVSEC + "/devVRE";
|
||||
|
||||
PARENT_DEFAULT_TEST_SCOPE = "/gcube";
|
||||
DEFAULT_TEST_SCOPE = PARENT_DEFAULT_TEST_SCOPE + "/devNext";
|
||||
|
|
|
@ -81,6 +81,8 @@ import org.junit.Test;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.orientechnologies.orient.core.exception.ODatabaseException;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
|
@ -105,6 +107,8 @@ public class ERManagementTest extends ContextTest {
|
|||
ERManagementTest.deleteResource(r);
|
||||
}catch (ResourceNotFoundException e) {
|
||||
// A resource could be already deleted deleting another resource giving the propagation constraint
|
||||
}catch (ODatabaseException e) {
|
||||
// could occur
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -165,7 +169,7 @@ public class ERManagementTest extends ContextTest {
|
|||
LicenseFacet licenseFacet = new LicenseFacetImpl();
|
||||
licenseFacet.setName("EUPL");
|
||||
licenseFacet.setTextURL(
|
||||
new URL("https://joinup.ec.europa.eu/community/eupl/og_page/european-union-public-licence-eupl-v11"));
|
||||
new URL("https://joinup.ec.europa.eu/community/etestAddToContextFromDifferentSourceContextupl/og_page/european-union-public-licence-eupl-v11"));
|
||||
eService.addFacet(licenseFacet);
|
||||
|
||||
return eService;
|
||||
|
|
|
@ -1,17 +1,54 @@
|
|||
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();
|
||||
|
@ -51,4 +88,195 @@ public class AddToContextTest extends MultiContextTest {
|
|||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,6 @@ import org.gcube.informationsystem.model.reference.properties.PropagationConstra
|
|||
import org.gcube.informationsystem.resourceregistry.ContextTest;
|
||||
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.utils.Utility;
|
||||
import org.gcube.informationsystem.resourceregistry.instances.ERManagementTest;
|
||||
import org.gcube.informationsystem.resourceregistry.instances.model.entities.FacetManagement;
|
||||
|
@ -33,7 +32,7 @@ public class ComplexTest extends MultiContextTest {
|
|||
|
||||
@Test
|
||||
public void testGetInstancesContexts() throws ResourceRegistryException, Exception {
|
||||
ContextTest.setContextByName(DEFAULT_TEST_SCOPE);
|
||||
ContextTest.setContextByName(DEVNEXT);
|
||||
|
||||
ContextCache contextCache = ContextCache.getInstance();
|
||||
contextCache.setContextCacheRenewal(contextCacheRenewal);
|
||||
|
@ -47,10 +46,8 @@ public class ComplexTest extends MultiContextTest {
|
|||
|
||||
/* Creating Activates Relation */
|
||||
PropagationConstraint propagationConstraint = new PropagationConstraintImpl();
|
||||
propagationConstraint
|
||||
.setRemoveConstraint(RemoveConstraint.cascade);
|
||||
propagationConstraint
|
||||
.setAddConstraint(AddConstraint.unpropagate);
|
||||
propagationConstraint.setRemoveConstraint(RemoveConstraint.cascade);
|
||||
propagationConstraint.setAddConstraint(AddConstraint.unpropagate);
|
||||
|
||||
|
||||
Activates<HostingNode, EService> activates = new ActivatesImpl<>(hostingNode, eService,
|
||||
|
@ -69,10 +66,10 @@ public class ComplexTest extends MultiContextTest {
|
|||
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
addToContextThenTestIfBehaveProperly(hostingNode, false, ALTERNATIVE_TEST_SCOPE);
|
||||
addToContextThenTestIfBehaveProperly(hostingNode, false, NEXTNEXT);
|
||||
|
||||
logger.debug("Switching to alternative scope");
|
||||
ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE);
|
||||
ContextTest.setContextByName(NEXTNEXT);
|
||||
|
||||
ResourceManagement hostingNodeManagement = ERManagementTest.getResourceManagement(hostingNode);
|
||||
|
||||
|
@ -128,10 +125,10 @@ public class ComplexTest extends MultiContextTest {
|
|||
ERManagementTest.deleteResource(hostingNode);
|
||||
|
||||
try {
|
||||
ContextTest.setContextByName(DEVNEXT);
|
||||
ERManagementTest.deleteResource(eService);
|
||||
throw new Exception(EService.NAME + " should be already deleted giving the cascade");
|
||||
}catch (ResourceNotFoundException e) {
|
||||
// As expected
|
||||
} catch (Exception e) {
|
||||
logger.error("",e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue