From 119ae14dfa31fc8684fe22b79ecaf593633ec0e9 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 2 May 2023 14:48:38 +0200 Subject: [PATCH] Metadata are included only if requested by the client --- .../contexts/ContextUtility.java | 27 ---- .../contexts/security/SecurityContext.java | 5 +- .../instances/base/ElementManagement.java | 22 ++- .../model/entities/EntityManagement.java | 3 + .../resourceregistry/queries/QueryImpl.java | 1 + .../queries/json/JsonQuery.java | 2 + .../requests/RequestInfo.java | 148 ++++++++++++++++++ .../requests/RequestUtility.java | 21 +++ .../resourceregistry/rest/Access.java | 26 +-- .../resourceregistry/rest/BaseRest.java | 56 +------ .../resourceregistry/rest/ContextManager.java | 3 +- .../rest/InstancesManager.java | 12 +- .../resourceregistry/rest/SharingManager.java | 4 +- .../resourceregistry/rest/TypeManager.java | 4 +- .../instances/ERManagementTest.java | 106 +++++++++++-- .../instances/InvalidOperationTest.java | 38 ++--- .../multicontext/AddToContextTest.java | 26 +-- .../instances/multicontext/BasicTest.java | 37 +++-- .../instances/multicontext/ComplexTest.java | 13 +- .../multicontext/MultiContextTest.java | 10 +- .../resourceregistry/queries/QueryTest.java | 14 +- 21 files changed, 384 insertions(+), 194 deletions(-) create mode 100644 src/main/java/org/gcube/informationsystem/resourceregistry/requests/RequestInfo.java create mode 100644 src/main/java/org/gcube/informationsystem/resourceregistry/requests/RequestUtility.java diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ContextUtility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ContextUtility.java index 0323d35..cb2f80b 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ContextUtility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ContextUtility.java @@ -51,33 +51,6 @@ public class ContextUtility { contexts = new HashMap<>(); } - private static final InheritableThreadLocal hierarchicalMode = new InheritableThreadLocal() { - - @Override - protected Boolean initialValue() { - return false; - } - - }; - - public static InheritableThreadLocal getHierarchicalMode() { - return hierarchicalMode; - } - - - private static final InheritableThreadLocal includeInstanceContexts = new InheritableThreadLocal() { - - @Override - protected Boolean initialValue() { - return false; - } - - }; - - public static InheritableThreadLocal getIncludeInstanceContexts() { - return includeInstanceContexts; - } - public static String getCurrentContextFullName() { return SecretManagerProvider.instance.get().getContext(); } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/security/SecurityContext.java b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/security/SecurityContext.java index cd6d64f..0bb0021 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/security/SecurityContext.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/security/SecurityContext.java @@ -19,6 +19,7 @@ import org.gcube.informationsystem.contexts.reference.entities.Context; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment; +import org.gcube.informationsystem.resourceregistry.requests.RequestUtility; import org.gcube.informationsystem.resourceregistry.utils.OrientDBUtility; import org.gcube.informationsystem.utils.UUIDManager; import org.slf4j.Logger; @@ -93,7 +94,7 @@ public class SecurityContext { protected Set children; protected boolean isHierarchicalMode() { - return hierarchical && ContextUtility.getHierarchicalMode().get(); + return hierarchical && RequestUtility.getRequestInfo().get().isHierarchicalMode(); } public void setParentSecurityContext(SecurityContext parentSecurityContext) { @@ -419,7 +420,7 @@ public class SecurityContext { @Override public Boolean call() throws Exception { - ContextUtility.getHierarchicalMode().set(false); + RequestUtility.getRequestInfo().get().setHierarchicalMode(false); ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal(); ODatabaseDocument oDatabaseDocument = null; diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java index 9a8e534..6b0c081 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java @@ -46,6 +46,8 @@ import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityCo import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode; import org.gcube.informationsystem.resourceregistry.instances.base.properties.PropertyElementManagement; import org.gcube.informationsystem.resourceregistry.instances.model.Operation; +import org.gcube.informationsystem.resourceregistry.requests.RequestInfo; +import org.gcube.informationsystem.resourceregistry.requests.RequestUtility; import org.gcube.informationsystem.resourceregistry.types.CachedType; import org.gcube.informationsystem.resourceregistry.types.TypesCache; import org.gcube.informationsystem.resourceregistry.utils.MetadataOrient; @@ -178,7 +180,7 @@ public abstract class ElementManagement { this.dryRun = dryRun; } - protected void setAsEntryPoint() { + public void setAsEntryPoint() { this.entryPoint = true; } @@ -401,14 +403,14 @@ public abstract class ElementManagement { /* Add first these key to provide an order in Json */ + RequestInfo requestInfo = RequestUtility.getRequestInfo().get(); + List keysToAddFirst = new ArrayList<>(); keysToAddFirst.add(Element.TYPE_PROPERTY); keysToAddFirst.add(Element.SUPERTYPES_PROPERTY); keysToAddFirst.add(IdentifiableElement.ID_PROPERTY); keysToAddFirst.add(IdentifiableElement.METADATA_PROPERTY); - if(ContextUtility.getIncludeInstanceContexts().get()) { - keysToAddFirst.add(ERElement.CONTEXTS_PROPERTY); - } + keysToAddFirst.add(ERElement.CONTEXTS_PROPERTY); keysToAddFirst.add(Relation.PROPAGATION_CONSTRAINT_PROPERTY); for(String key : keysToAddFirst) { @@ -423,8 +425,18 @@ public abstract class ElementManagement { objectNode.replace(Element.SUPERTYPES_PROPERTY, arrayNode); break; + case IdentifiableElement.METADATA_PROPERTY: + if(requestInfo.includeMeta()) { + if(requestInfo.allMeta() || entryPoint) { + analizeProperty(element, key, objectNode); + } + } + break; + case ERElement.CONTEXTS_PROPERTY: - objectNode.replace(ERElement.CONTEXTS_PROPERTY, getContextsAsObjectNode()); + if(requestInfo.includeContexts()) { + objectNode.replace(ERElement.CONTEXTS_PROPERTY, getContextsAsObjectNode()); + } break; default: diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/EntityManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/EntityManagement.java index 0fd030b..a55fa76 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/EntityManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/EntityManagement.java @@ -501,6 +501,7 @@ public abstract class EntityManagement EntityManagement entityManagement = ElementManagementUtility.getEntityManagement(getWorkingContext(), oDatabaseDocument, (OVertex) vertex); try { + entityManagement.setAsEntryPoint(); JsonNode jsonNode = entityManagement.serializeAsJsonNode(); arrayNode.add(jsonNode); } catch(ResourceRegistryException e) { @@ -689,6 +690,7 @@ public abstract class EntityManagement jsonNode = entityManagement.serializeAsJsonNode(); } */ + entityManagement.setAsEntryPoint(); JsonNode node = entityManagement.serializeAsJsonNode(); arrayNode.add(node); @@ -798,6 +800,7 @@ public abstract class EntityManagement continue; } } + entityManagement.setAsEntryPoint(); JsonNode jsonNode = entityManagement.serializeAsJsonNode(); arrayNode.add(jsonNode); } catch(ResourceRegistryException e) { diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/queries/QueryImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/queries/QueryImpl.java index db71b0d..3bdab1a 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/queries/QueryImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/queries/QueryImpl.java @@ -61,6 +61,7 @@ public class QueryImpl implements Query { OElement element = ElementManagementUtility.getElementFromOptional(oResult.getElement()); ElementManagement erManagement = ElementManagementUtility.getERManagement(securityContext, oDatabaseDocument, element); + erManagement.setAsEntryPoint(); jsonNode = erManagement.serializeAsJsonNode(); } arrayNode.add(jsonNode); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/queries/json/JsonQuery.java b/src/main/java/org/gcube/informationsystem/resourceregistry/queries/json/JsonQuery.java index 415c0ab..077d0d4 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/queries/json/JsonQuery.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/queries/json/JsonQuery.java @@ -142,6 +142,7 @@ public class JsonQuery { String gotType = erManagement.getTypeName(); if(requestedType.compareTo(gotType)==0) { + erManagement.setAsEntryPoint(); jsonNodeResult = erManagement.serializeAsJsonNode(); arrayNode.add(jsonNodeResult); continue; @@ -149,6 +150,7 @@ public class JsonQuery { CachedType cachedType = TypesCache.getInstance().getCachedType(gotType); if(cachedType.getSuperTypes().contains(requestedType)) { + erManagement.setAsEntryPoint(); jsonNodeResult = erManagement.serializeAsJsonNode(); arrayNode.add(jsonNodeResult); continue; diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/requests/RequestInfo.java b/src/main/java/org/gcube/informationsystem/resourceregistry/requests/RequestInfo.java new file mode 100644 index 0000000..a42515e --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/requests/RequestInfo.java @@ -0,0 +1,148 @@ +package org.gcube.informationsystem.resourceregistry.requests; + +import java.util.List; + +import javax.ws.rs.core.UriInfo; + +import org.gcube.informationsystem.base.reference.IdentifiableElement; +import org.gcube.informationsystem.model.reference.properties.Metadata; +import org.gcube.informationsystem.resourceregistry.api.rest.InstancePath; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class RequestInfo { + + protected static Logger logger = LoggerFactory.getLogger(RequestInfo.class); + + protected UriInfo uriInfo; + + /** + * Track if the request requested to include {@link Metadata} + */ + protected boolean includeMeta; + + /** + * Track if the request requested to include {@link Metadata} in all + * {@link IdentifiableElement} or just in the root instance + */ + protected boolean allMeta; + + + /** + * Track if hierarchicalMode has been requested + */ + protected boolean hierarchicalMode; + + /** + * Track if the request requested to include contexts + */ + protected boolean includeContexts; + + public RequestInfo() { + this.includeMeta = false; + this.allMeta = false; + this.hierarchicalMode = false; + this.includeContexts = false; + } + + /** + * Set the parameter if the user is allowed otherwise the default is maintained + * @param queryParameterKey requested query parameter + * @param bool the value to set + * @return the value of variable corresponding the request parameter independetly if + * the value has been set. + */ + protected boolean setIfAllowed(String queryParameterKey, boolean bool) { + switch (queryParameterKey) { + case InstancePath.INCLUDE_META_QUERY_PARAMETER: + // TODO check is the user has the role to request such parameter + includeMeta = bool; + return includeMeta; + + case InstancePath.INCLUDE_META_IN_ALL_INSTANCES_QUERY_PARAMETER: + // TODO check is the user has the role to request such parameter + allMeta = bool; + return allMeta; + + case InstancePath.INCLUDE_CONTEXTS_QUERY_PARAMETER: + // TODO check is the user has the role to request such parameter + includeContexts = bool; + return includeContexts; + + case InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER: + // TODO check is the user has the role to request such parameter + hierarchicalMode = bool; + return hierarchicalMode; + + default: + break; + } + return false; + } + + public void checkQueryParameter(String queryParameterKey) { + try { + List queryParameterList = uriInfo.getQueryParameters().get(queryParameterKey); + if(queryParameterList!=null && queryParameterList.size()>0) { + String booleanString = queryParameterList.get(0); + boolean bool = Boolean.valueOf(booleanString); + setIfAllowed(queryParameterKey, bool); + } + }catch (Throwable t) { + logger.warn("Unable to properly set the Hierarchical Mode is set", t.getMessage()); + } + } + + public void setUriInfo(UriInfo uriInfo) { + this.uriInfo = uriInfo; + } + + public void checkAllQueryParameters() { + checkIncludeQueryParameters(); + checkQueryParameter(InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER); + } + + public void checkIncludeQueryParameters() { + checkQueryParameter(InstancePath.INCLUDE_META_QUERY_PARAMETER); + checkQueryParameter(InstancePath.INCLUDE_META_IN_ALL_INSTANCES_QUERY_PARAMETER); + checkQueryParameter(InstancePath.INCLUDE_CONTEXTS_QUERY_PARAMETER); + } + + public boolean includeMeta() { + return includeMeta; + } + + public void setIncludeMeta(boolean includeMeta) { + this.includeMeta = includeMeta; + } + + public boolean allMeta() { + return allMeta; + } + + public void setAllMeta(boolean allMeta) { + this.allMeta = allMeta; + } + + public boolean isHierarchicalMode() { + return hierarchicalMode; + } + + public void setHierarchicalMode(boolean hierarchicalMode) { + this.hierarchicalMode = hierarchicalMode; + } + + public boolean includeContexts() { + return includeContexts; + } + + public void setIncludeContexts(boolean includeContexts) { + this.includeContexts = includeContexts; + } + + + +} diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/requests/RequestUtility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/requests/RequestUtility.java new file mode 100644 index 0000000..e08f857 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/requests/RequestUtility.java @@ -0,0 +1,21 @@ +package org.gcube.informationsystem.resourceregistry.requests; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class RequestUtility { + + private static final InheritableThreadLocal requestInfo = new InheritableThreadLocal() { + + @Override + protected RequestInfo initialValue() { + return new RequestInfo(); + } + + }; + + public static InheritableThreadLocal getRequestInfo() { + return requestInfo; + } + +} diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/Access.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/Access.java index 2e49828..9686238 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/Access.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/Access.java @@ -40,6 +40,8 @@ import org.gcube.informationsystem.resourceregistry.instances.model.entities.Res import org.gcube.informationsystem.resourceregistry.queries.Query; import org.gcube.informationsystem.resourceregistry.queries.QueryImpl; import org.gcube.informationsystem.resourceregistry.queries.json.JsonQuery; +import org.gcube.informationsystem.resourceregistry.requests.RequestInfo; +import org.gcube.informationsystem.resourceregistry.requests.RequestUtility; import org.gcube.informationsystem.resourceregistry.types.TypeManagement; import org.gcube.informationsystem.types.TypeMapper; import org.gcube.informationsystem.types.reference.Type; @@ -131,8 +133,7 @@ public class Access extends BaseRest { logger.info("Requested all {}instances of {}", polymorphic ? InstancePath.POLYMORPHIC_QUERY_PARAMETER + " " : "", type); CalledMethodProvider.instance.set("listInstances"); - checkHierarchicalMode(); - checkIncludeInstancesContexts(); + RequestUtility.getRequestInfo().get().checkAllQueryParameters(); ElementManagement erManagement = ElementManagementUtility.getERManagement(type); return erManagement.all(polymorphic); @@ -151,8 +152,7 @@ public class Access extends BaseRest { logger.info("Requested to check if {} with id {} exists", type, uuid); CalledMethodProvider.instance.set("existInstance"); - checkHierarchicalMode(); - checkIncludeInstancesContexts(); + RequestUtility.getRequestInfo().get().checkQueryParameter(InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER); ElementManagement erManagement = ElementManagementUtility.getERManagement(type); @@ -188,8 +188,7 @@ public class Access extends BaseRest { logger.info("Requested to read {} with id {}", type, uuid); CalledMethodProvider.instance.set("readInstance"); - checkHierarchicalMode(); - checkIncludeInstancesContexts(); + RequestUtility.getRequestInfo().get().checkAllQueryParameters(); @SuppressWarnings("rawtypes") ElementManagement erManagement = ElementManagementUtility.getERManagement(type); @@ -245,8 +244,13 @@ public class Access extends BaseRest { logger.info("Requested query (Raw {}):\n{}", raw, query); CalledMethodProvider.instance.set("graphQuery"); - checkHierarchicalMode(); - checkIncludeInstancesContexts(); + RequestInfo requestInfo = RequestUtility.getRequestInfo().get(); + if(raw) { + // TODO Check if the role allow to request raw data + requestInfo.checkQueryParameter(InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER); + }else { + requestInfo.checkAllQueryParameters(); + } Query queryManager = new QueryImpl(); return queryManager.query(query, raw); @@ -299,8 +303,7 @@ public class Access extends BaseRest { logger.info("Requested json query \n{}", jsonQuery); CalledMethodProvider.instance.set("jsonQuery"); - checkHierarchicalMode(); - checkIncludeInstancesContexts(); + RequestUtility.getRequestInfo().get().checkAllQueryParameters(); JsonQuery jsonQueryManager = new JsonQuery(); jsonQueryManager.setJsonQuery(jsonQuery); @@ -350,8 +353,7 @@ public class Access extends BaseRest { CalledMethodProvider.instance.set("query"); - checkHierarchicalMode(); - checkIncludeInstancesContexts(); + RequestUtility.getRequestInfo().get().checkAllQueryParameters(); ElementManagement erManagement = ElementManagementUtility.getERManagement(resourcetype); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/BaseRest.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/BaseRest.java index eb93995..b37ca35 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/BaseRest.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/BaseRest.java @@ -1,12 +1,10 @@ package org.gcube.informationsystem.resourceregistry.rest; -import java.util.List; - import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; -import org.gcube.informationsystem.resourceregistry.api.rest.InstancePath; -import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; +import org.gcube.informationsystem.resourceregistry.requests.RequestInfo; +import org.gcube.informationsystem.resourceregistry.requests.RequestUtility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -17,56 +15,16 @@ public class BaseRest { protected Logger logger = LoggerFactory.getLogger(this.getClass()); - public BaseRest() { - ContextUtility.getHierarchicalMode().set(false); - ContextUtility.getIncludeInstanceContexts().set(false); - } - @Context protected UriInfo uriInfo; - protected boolean isRequesterAllowedToPerformHierarchicalRequests() { - // TODO check is the user has the role to query in hierarchic mode - return true; + public BaseRest() { + RequestInfo requestInfo = new RequestInfo(); + requestInfo.setUriInfo(uriInfo); + + RequestUtility.getRequestInfo().set(requestInfo); } - protected void checkHierarchicalMode() { - try { - List hierarchicalQueryParameterList = uriInfo.getQueryParameters().get(InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER); - if(hierarchicalQueryParameterList!=null && hierarchicalQueryParameterList.size()>0) { - String hierarchicalBooleanString = hierarchicalQueryParameterList.get(0); - boolean hierarchical = Boolean.valueOf(hierarchicalBooleanString); - boolean h = hierarchical && isRequesterAllowedToPerformHierarchicalRequests(); - if(h) { - logger.info("The request was performed in hierarchical mode and the requester is allowed. Going to set hierarchical mode."); - ContextUtility.getHierarchicalMode().set(h); - } - } - }catch (Throwable t) { - logger.warn("Unable to properly set the Hierarchical Mode is set", t.getMessage()); - } - } - protected boolean isRequesterAllowedToRequestInstancesContexts() { - // TODO check is the user has the role to query in get instance Contexts - return true; - } - - protected void checkIncludeInstancesContexts() { - try { - List includeContextsQueryParameterList = uriInfo.getQueryParameters().get(InstancePath.INCLUDE_CONTEXTS_QUERY_PARAMETER); - if(includeContextsQueryParameterList!=null && includeContextsQueryParameterList.size()>0) { - String includeContextsBooleanString = includeContextsQueryParameterList.get(0); - boolean includeContexts = Boolean.valueOf(includeContextsBooleanString); - boolean i = includeContexts && isRequesterAllowedToRequestInstancesContexts(); - if(i) { - logger.info("The requet to include the contexts is allowed."); - ContextUtility.getIncludeInstanceContexts().set(true); - } - } - }catch (Throwable t) { - logger.warn("Unable to properly set the Hierarchical Mode is set", t.getMessage()); - } - } } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ContextManager.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ContextManager.java index f29a055..be5b378 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ContextManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ContextManager.java @@ -38,8 +38,7 @@ public class ContextManager { private static Logger logger = LoggerFactory.getLogger(ContextManager.class); public ContextManager() { - ContextUtility.getHierarchicalMode().set(false); - ContextUtility.getIncludeInstanceContexts().set(false); + } /* diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java index 626f49a..826136e 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java @@ -27,6 +27,7 @@ import org.gcube.informationsystem.resourceregistry.api.rest.InstancePath; import org.gcube.informationsystem.resourceregistry.api.rest.SharingPath; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility; +import org.gcube.informationsystem.resourceregistry.requests.RequestUtility; /** * @author Luca Frosini (ISTI - CNR) @@ -55,8 +56,7 @@ public class InstancesManager extends BaseRest { logger.info("Requested all {}instances of {}", polymorphic ? InstancePath.POLYMORPHIC_QUERY_PARAMETER + " " : "", type); CalledMethodProvider.instance.set("listInstances"); - checkHierarchicalMode(); - checkIncludeInstancesContexts(); + RequestUtility.getRequestInfo().get().checkAllQueryParameters(); ElementManagement erManagement = ElementManagementUtility.getERManagement(type); return erManagement.all(polymorphic); @@ -76,8 +76,7 @@ public class InstancesManager extends BaseRest { logger.info("Requested to check if {} with id {} exists", type, uuid); CalledMethodProvider.instance.set("existInstance"); - checkHierarchicalMode(); - checkIncludeInstancesContexts(); + RequestUtility.getRequestInfo().get().checkQueryParameter(InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER); @SuppressWarnings("rawtypes") ElementManagement erManagement = ElementManagementUtility.getERManagement(type); @@ -114,8 +113,7 @@ public class InstancesManager extends BaseRest { logger.info("Requested to read {} with id {}", type, uuid); CalledMethodProvider.instance.set("readInstance"); - checkHierarchicalMode(); - checkIncludeInstancesContexts(); + RequestUtility.getRequestInfo().get().checkAllQueryParameters(); ElementManagement erManagement = ElementManagementUtility.getERManagement(type); erManagement.setElementType(type); @@ -140,6 +138,8 @@ public class InstancesManager extends BaseRest { logger.trace("Requested to update/create {} with id {} with json {}", type, uuid, json); CalledMethodProvider.instance.set("updateInstance"); + RequestUtility.getRequestInfo().get().checkIncludeQueryParameters(); + @SuppressWarnings("rawtypes") ElementManagement erManagement = ElementManagementUtility.getERManagement(type); erManagement.setUUID(UUID.fromString(uuid)); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManager.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManager.java index 3d94372..945a78f 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManager.java @@ -24,7 +24,6 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.entities.reso import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaViolationException; import org.gcube.informationsystem.resourceregistry.api.rest.SharingPath; import org.gcube.informationsystem.resourceregistry.api.rest.SharingPath.SharingOperation; -import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility; import org.gcube.informationsystem.resourceregistry.instances.model.ERManagement; @@ -42,8 +41,7 @@ public class SharingManager { private static Logger logger = LoggerFactory.getLogger(SharingManager.class); public SharingManager() { - ContextUtility.getHierarchicalMode().set(false); - ContextUtility.getIncludeInstanceContexts().set(false); + } protected String serializeAffectedInstaces(ObjectMapper objectMapper, Map affectedInstances) throws ResourceRegistryException { diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/TypeManager.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/TypeManager.java index cfe8c0f..e6007c8 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/TypeManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/TypeManager.java @@ -20,7 +20,6 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegis import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException; import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException; import org.gcube.informationsystem.resourceregistry.api.rest.TypePath; -import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; import org.gcube.informationsystem.resourceregistry.types.TypeManagement; import org.gcube.informationsystem.types.TypeMapper; import org.gcube.informationsystem.types.reference.Type; @@ -38,8 +37,7 @@ public class TypeManager { public static final String TYPE_PATH_PARAMETER = "TYPE_NAME"; public TypeManager() { - ContextUtility.getHierarchicalMode().set(false); - ContextUtility.getIncludeInstanceContexts().set(false); + } /* 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 87cf800..7042a93 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/ERManagementTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/ERManagementTest.java @@ -34,6 +34,8 @@ import org.gcube.informationsystem.resourceregistry.instances.model.entities.Fac import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement; import org.gcube.informationsystem.resourceregistry.instances.model.relations.ConsistsOfManagement; import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement; +import org.gcube.informationsystem.resourceregistry.requests.RequestInfo; +import org.gcube.informationsystem.resourceregistry.requests.RequestUtility; import org.gcube.informationsystem.resourceregistry.utils.MetadataUtility; import org.gcube.informationsystem.serialization.ElementMapper; import org.gcube.resourcemanagement.model.impl.entities.facets.AccessPointFacetImpl; @@ -73,6 +75,7 @@ import org.gcube.resourcemanagement.model.reference.relations.consistsof.HasVola import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy; import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Activates; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -107,6 +110,12 @@ public class ERManagementTest extends ContextTest { // } // } + @Before + public void before() throws Exception { + RequestInfo requestInfo = RequestUtility.getRequestInfo().get(); + requestInfo.setIncludeMeta(true); + requestInfo.setAllMeta(true); + } public static SoftwareFacet getSoftwareFacet() { SoftwareFacet softwareFacet = new SoftwareFacetImpl(); @@ -257,7 +266,7 @@ public class ERManagementTest extends ContextTest { } if(metadata.getLastUpdateTime()!=null) { - Assert.assertTrue(lastUpdateTime.after(metadata.getLastUpdateTime())); + Assert.assertTrue(lastUpdateTime.after(metadata.getLastUpdateTime()) || lastUpdateTime.compareTo(metadata.getLastUpdateTime())==0); } } @@ -325,7 +334,39 @@ public class ERManagementTest extends ContextTest { } - public static ResourceManagement getResourceManagement(R r) throws Exception { + + protected static void checkResourceRootMetaOnly(R resource, R gotResource) throws Exception { + Assert.assertTrue(resource.getClass() == gotResource.getClass()); + checkUUIDAndMetadata(resource, gotResource); + + List> resourceConsistsOf = resource.getConsistsOf(); + List> gotResourceConsistsOf = gotResource.getConsistsOf(); + Assert.assertTrue(resourceConsistsOf.size() == gotResourceConsistsOf.size()); + + for(ConsistsOf gotConsistsOf : gotResourceConsistsOf) { + Assert.assertNull(gotConsistsOf.getMetadata()); + Assert.assertNull(gotConsistsOf.getTarget().getMetadata()); + } + + } + + + protected static void checkResourceNoMeta(R resource, R gotResource) throws Exception { + Assert.assertTrue(resource.getClass() == gotResource.getClass()); + Assert.assertNull(gotResource.getMetadata()); + + List> resourceConsistsOf = resource.getConsistsOf(); + List> gotResourceConsistsOf = gotResource.getConsistsOf(); + Assert.assertTrue(resourceConsistsOf.size() == gotResourceConsistsOf.size()); + + for(ConsistsOf gotConsistsOf : gotResourceConsistsOf) { + Assert.assertNull(gotConsistsOf.getMetadata()); + Assert.assertNull(gotConsistsOf.getTarget().getMetadata()); + } + + } + + public ResourceManagement getResourceManagement(R r) throws Exception { ResourceManagement resourceManagement = new ResourceManagement(); resourceManagement.setElementType(r.getTypeName()); resourceManagement.setJson(ElementMapper.marshal(r)); @@ -335,7 +376,7 @@ public class ERManagementTest extends ContextTest { return resourceManagement; } - public static IsRelatedToManagement getIsRelatedToManagement(IsRelatedTo isRelatedTo) throws Exception { + public IsRelatedToManagement getIsRelatedToManagement(IsRelatedTo isRelatedTo) throws Exception { IsRelatedToManagement isRelatedToManagement = new IsRelatedToManagement(); isRelatedToManagement.setElementType(isRelatedTo.getTypeName()); isRelatedToManagement.setJson(ElementMapper.marshal(isRelatedTo)); @@ -347,7 +388,7 @@ public class ERManagementTest extends ContextTest { - public static R createResource(R r) throws Exception { + public R createResource(R r) throws Exception { ResourceManagement resourceManagement = getResourceManagement(r); String json = resourceManagement.create(); @@ -363,20 +404,20 @@ public class ERManagementTest extends ContextTest { return createdR; } - public static EService createEService() throws Exception { + public EService createEService() throws Exception { EService eService = ERManagementTest.instantiateValidEService(); return createResource(eService); } - public static HostingNode createHostingNode() throws Exception { + public HostingNode createHostingNode() throws Exception { return createHostingNode(null); } - public static HostingNode createHostingNode(EService eService) throws Exception { + public HostingNode createHostingNode(EService eService) throws Exception { return createHostingNode(eService, RemoveConstraint.cascade, DeleteConstraint.cascade); } - public static HostingNode createHostingNode(EService eService, RemoveConstraint removeConstraint, DeleteConstraint deleteConstraint) throws Exception { + public HostingNode createHostingNode(EService eService, RemoveConstraint removeConstraint, DeleteConstraint deleteConstraint) throws Exception { HostingNode hostingNode = ERManagementTest.instantiateValidHostingNode(); if(eService!=null) { PropagationConstraint propagationConstraint = new PropagationConstraintImpl(); @@ -389,12 +430,12 @@ public class ERManagementTest extends ContextTest { return createResource(hostingNode); } - public static Configuration createConfiguration() throws Exception { + public Configuration createConfiguration() throws Exception { Configuration configuration = ERManagementTest.instantiateValidConfiguration(); return createResource(configuration); } - public static Map createHostingNodeAndEService() throws Exception { + public Map createHostingNodeAndEService() throws Exception { Map map = new HashMap<>(); EService eService = createEService(); @@ -406,7 +447,7 @@ public class ERManagementTest extends ContextTest { return map; } - public static void deleteResource(R r) throws Exception { + public void deleteResource(R r) throws Exception { if(r!=null) { ResourceManagement resourceManagement = getResourceManagement(r); resourceManagement.delete(); @@ -425,13 +466,46 @@ public class ERManagementTest extends ContextTest { } + @Test + public void testCreateAndReadEService() throws Exception { + EService eService = null; + try { + eService = createEService(); + RequestInfo requestInfo = RequestUtility.getRequestInfo().get(); + + requestInfo.setIncludeMeta(true); + requestInfo.setAllMeta(true); + EService readEService = (EService) readResource(eService.getID()); + checkResource(eService, readEService); + + requestInfo.setIncludeMeta(true); + requestInfo.setAllMeta(false); + readEService = (EService) readResource(eService.getID()); + checkResourceRootMetaOnly(eService, readEService); + + requestInfo.setIncludeMeta(false); + readEService = (EService) readResource(eService.getID()); + checkResourceNoMeta(eService, readEService); + + }finally { + deleteResource(eService); + } + + } + + + protected Resource readResource(UUID uuid) throws Exception { + ResourceManagement resourceManagement = new ResourceManagement(); + resourceManagement.setUUID(uuid); + String json = resourceManagement.read().toString(); + logger.debug(json); + return ElementMapper.unmarshal(Resource.class, json); + } + /* @Test public void testReadResource() throws Exception { - ResourceManagement resourceManagement = new ResourceManagement(); - resourceManagement.setUUID(UUID.fromString("26da57ee-33bd-4c4b-8aef-9206b61c329e")); - String read = resourceManagement.read().toString(); - logger.debug(read); + readResource(UUID.fromString("26da57ee-33bd-4c4b-8aef-9206b61c329e")); } */ @@ -458,7 +532,7 @@ public class ERManagementTest extends ContextTest { @Test public void testCreateHostingNodeAndEService() throws Exception { - Map map = ERManagementTest.createHostingNodeAndEService(); + Map map = createHostingNodeAndEService(); deleteResource(map.get(HostingNode.NAME)); } diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/InvalidOperationTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/InvalidOperationTest.java index 193d652..fbb8903 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/InvalidOperationTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/InvalidOperationTest.java @@ -46,8 +46,8 @@ public class InvalidOperationTest extends ERManagementTest { @Test(expected = SchemaViolationException.class) public void createInvalidIsRealtedTo() throws Exception { - Configuration configuration = ERManagementTest.createConfiguration(); - EService eService = ERManagementTest.createEService(); + Configuration configuration = createConfiguration(); + EService eService = createEService(); try { /* * Trying to create a relation activates between a Configuration and EService @@ -66,18 +66,18 @@ public class InvalidOperationTest extends ERManagementTest { isRelatedToManagement.setJson(json); isRelatedToManagement.create(); }finally { - ERManagementTest.deleteResource(configuration); - ERManagementTest.deleteResource(eService); + deleteResource(configuration); + deleteResource(eService); } } @Test(expected = ResourceAlreadyPresentException.class) public void testRecreate() throws Exception { - EService eService = ERManagementTest.createEService(); + EService eService = createEService(); try { - ERManagementTest.createResource(eService); + createResource(eService); }finally { - ERManagementTest.deleteResource(eService); + deleteResource(eService); } } @@ -111,12 +111,12 @@ public class InvalidOperationTest extends ERManagementTest { IsIdentifiedBy isIdentifiedBy = new IsIdentifiedByImpl<>(runningPlugin, softwareFacet); runningPlugin.addFacet(isIdentifiedBy); - ERManagementTest.createResource(runningPlugin); + createResource(runningPlugin); } @Test(expected = ResourceRegistryException.class) public void testCreateAnEntityDifferentFromDeclared() throws Exception { - EService eService = ERManagementTest.instantiateValidEService(); + EService eService = instantiateValidEService(); ResourceManagement resourceManagement = new ResourceManagement(); resourceManagement.setElementType(Service.NAME); resourceManagement.setJson(ElementMapper.marshal(eService)); @@ -133,7 +133,7 @@ public class InvalidOperationTest extends ERManagementTest { @Test(expected = SchemaViolationException.class) public void testCreateHostingNodeAndEServiceWithSharedFacet() throws Exception { - Map map = ERManagementTest.createHostingNodeAndEService(); + Map map = createHostingNodeAndEService(); EService eService = (EService) map.get(EService.NAME); HostingNode hostingNode = (HostingNode) map.get(HostingNode.NAME); try { @@ -150,8 +150,8 @@ public class InvalidOperationTest extends ERManagementTest { logger.debug("Created : {}", json); } finally { - ERManagementTest.deleteResource(eService); - ERManagementTest.deleteResource(hostingNode); + deleteResource(eService); + deleteResource(hostingNode); } } @@ -160,7 +160,7 @@ public class InvalidOperationTest extends ERManagementTest { public void testCreateEServiceAndDeleteRequiredConsistsOf() throws Exception { EService eService = null; try { - eService = ERManagementTest.createEService(); + eService = createEService(); @SuppressWarnings("unchecked") IsIdentifiedBy isIdentifiedBy = (IsIdentifiedBy) eService.getConsistsOf(IsIdentifiedBy.class).get(0); @@ -170,13 +170,13 @@ public class InvalidOperationTest extends ERManagementTest { consistsOfManagement.setUUID(isIdentifiedBy.getID()); consistsOfManagement.delete(); }finally { - ERManagementTest.deleteResource(eService); + deleteResource(eService); } } @Test(expected = SchemaViolationException.class) public void testCreateEServiceAndDeleteRequiredFacet() throws Exception { - EService eService = ERManagementTest.createEService(); + EService eService = createEService(); @SuppressWarnings("unchecked") IsIdentifiedBy isIdentifiedBy = (IsIdentifiedBy) eService.getConsistsOf(IsIdentifiedBy.class).get(0); @@ -192,14 +192,14 @@ public class InvalidOperationTest extends ERManagementTest { try { facetManagement.delete(); }finally { - ERManagementTest.deleteResource(eService); + deleteResource(eService); } } @Test(expected = SchemaViolationException.class) public void testCreateConsistsOfBeetweenResources() throws Exception { - Map map = ERManagementTest.createHostingNodeAndEService(); + Map map = createHostingNodeAndEService(); UUID hostingNodeUUID = map.get(HostingNode.NAME).getID(); UUID eServiceUUID = map.get(EService.NAME).getID(); @@ -223,8 +223,8 @@ public class InvalidOperationTest extends ERManagementTest { consistsOfManagement.create(); throw new Exception("A ConsistsOf has been created between two resoures. This should not happen"); } finally { - ERManagementTest.deleteResource(map.get(EService.NAME)); - ERManagementTest.deleteResource(map.get(HostingNode.NAME)); + deleteResource(map.get(EService.NAME)); + deleteResource(map.get(HostingNode.NAME)); } } diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/AddToContextTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/AddToContextTest.java index 7c5f21f..d213d6f 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/AddToContextTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/AddToContextTest.java @@ -15,10 +15,10 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundExcep import org.gcube.informationsystem.resourceregistry.api.exceptions.types.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.resourceregistry.requests.RequestUtility; import org.gcube.informationsystem.serialization.ElementMapper; import org.gcube.resourcemanagement.model.impl.entities.facets.ContactFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.facets.CoverageFacetImpl; @@ -52,7 +52,7 @@ public class AddToContextTest extends MultiContextTest { @Test public void testCreateEServiceAndRemoveFromContextRequiredFacet() throws Exception { - EService eService = ERManagementTest.createEService(); + EService eService = createEService(); @SuppressWarnings("unchecked") IsIdentifiedBy isIdentifiedBy = (IsIdentifiedBy) eService.getConsistsOf(IsIdentifiedBy.class).get(0); @@ -66,7 +66,7 @@ public class AddToContextTest extends MultiContextTest { }catch (SchemaViolationException e) { // As expected }catch (Exception e) { - ERManagementTest.deleteResource(eService); + deleteResource(eService); throw e; } @@ -81,11 +81,11 @@ public class AddToContextTest extends MultiContextTest { }catch (SchemaViolationException e) { // As expected }catch (Exception e) { - ERManagementTest.deleteResource(eService); + deleteResource(eService); throw e; } - ERManagementTest.deleteResource(eService); + deleteResource(eService); } @@ -151,8 +151,8 @@ public class AddToContextTest extends MultiContextTest { protected void checkNumerOfContext(Dataset dataset, int expectedContextSize) throws Exception { setContextByName(GCUBE); - ContextUtility.getHierarchicalMode().set(true); - ContextUtility.getIncludeInstanceContexts().set(true); + RequestUtility.getRequestInfo().get().setHierarchicalMode(true); + RequestUtility.getRequestInfo().get().setIncludeContexts(true); String json = getResourceManagement(dataset).read(); logger.trace("Resource with contexts in HierarchicalMode from server is {}", json); @@ -164,8 +164,9 @@ public class AddToContextTest extends MultiContextTest { logger.info("Contexts of {} with UUID {} and Id {} are {}", Dataset.NAME, r1.getID(), ((IdentifierFacet)r1.getIdentificationFacets().get(0)).getValue(), contextsR1Fullname); Assert.assertTrue(contextsR1.size()==expectedContextSize); - ContextUtility.getHierarchicalMode().set(false); - ContextUtility.getIncludeInstanceContexts().set(false); + RequestUtility.getRequestInfo().get().setHierarchicalMode(false); + RequestUtility.getRequestInfo().get().setIncludeContexts(false); + } @Test @@ -284,8 +285,9 @@ public class AddToContextTest extends MultiContextTest { logger.error("", e); throw e; }finally { - ContextUtility.getHierarchicalMode().set(false); - ContextUtility.getIncludeInstanceContexts().set(false); + + RequestUtility.getRequestInfo().get().setHierarchicalMode(false); + RequestUtility.getRequestInfo().get().setIncludeContexts(false); setContextByName(GCUBE); deleteResource(datasetR1); @@ -308,7 +310,7 @@ public class AddToContextTest extends MultiContextTest { // ContextCache contextCache = ContextCache.getInstance(); // HostingNode hostingNode = new HostingNodeImpl(); // hostingNode.setUUID(UUID.fromString("a87bb07e-5320-4fd8-a48d-bf3cc55756c4")); -// ResourceManagement resourceManagement = ERManagementTest.getResourceManagement(hostingNode); +// ResourceManagement resourceManagement = getResourceManagement(hostingNode); // resourceManagement.setDryRun(false); // UUID contextUUID = ContextUtility.getInstance().getSecurityContextByFullName("/gcube/devsec").getUUID(); // resourceManagement.addToContext(contextUUID); diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/BasicTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/BasicTest.java index 824ceba..90c409a 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/BasicTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/BasicTest.java @@ -18,7 +18,6 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.entities.reso import org.gcube.informationsystem.resourceregistry.api.exceptions.entities.resource.ResourceNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.relations.RelationAvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.relations.isrelatedto.IsRelatedToNotFoundException; -import org.gcube.informationsystem.resourceregistry.instances.ERManagementTest; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility; import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement; import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement; @@ -134,13 +133,13 @@ public class BasicTest extends MultiContextTest { @Test public void testAddResourceToContext() throws Exception { - EService eService = ERManagementTest.createEService(); + EService eService = createEService(); try { addToContextThenTestIfBehaveProperly(eService, ALTERNATIVE_TEST_SCOPE); } catch (Exception e) { throw e; } finally { - ERManagementTest.deleteResource(eService); + deleteResource(eService); } } @@ -148,7 +147,7 @@ public class BasicTest extends MultiContextTest { @Test public void testCreateEServiceHostingNode() throws Exception { - EService eService = ERManagementTest.createEService(); + EService eService = createEService(); Map eServiceInstances = new HashMap<>(); eServiceInstances.put(eService.getID(), eService); @@ -157,7 +156,7 @@ public class BasicTest extends MultiContextTest { eServiceInstances.put(consistsOf.getTarget().getID(), consistsOf.getTarget()); } - HostingNode hostingNode = ERManagementTest.createHostingNode(eService); + HostingNode hostingNode = createHostingNode(eService); UUID hostingNodeUUID = hostingNode.getID(); Map hostingNodeInstances = new HashMap<>(); @@ -179,7 +178,7 @@ public class BasicTest extends MultiContextTest { addToContextThenTestIfBehaveProperly(hostingNode, targetContextFullName); - ERManagementTest.deleteResource(hostingNode); + deleteResource(hostingNode); rm = (ResourceManagement) ElementManagementUtility.getERManagement(Resource.NAME); all = rm.all(true); @@ -203,10 +202,10 @@ public class BasicTest extends MultiContextTest { } /* Creating EService */ - EService eService = ERManagementTest.createEService(); + EService eService = createEService(); /* Creating HostingNode */ - HostingNode hostingNode = ERManagementTest.createHostingNode(eService, removeConstraint, DeleteConstraint.cascade); + HostingNode hostingNode = createHostingNode(eService, removeConstraint, DeleteConstraint.cascade); @SuppressWarnings("unchecked") Activates activates = (Activates) getOutcomingIsRelatedTo(hostingNode).get(0); @@ -217,7 +216,7 @@ public class BasicTest extends MultiContextTest { // Adding Activated to ALTERNATIVE_TEST_SCOPE - IsRelatedToManagement isRelatedToManagement = ERManagementTest.getIsRelatedToManagement(activates); + IsRelatedToManagement isRelatedToManagement = getIsRelatedToManagement(activates); addToContextThenTestIfBehaveProperly(activates, ALTERNATIVE_TEST_SCOPE); /* ------------------------------------------------------------------ */ @@ -233,7 +232,7 @@ public class BasicTest extends MultiContextTest { * I MUST not be able to read Activates relation and EService */ - ResourceManagement resourceManagement = ERManagementTest.getResourceManagement(hostingNode); + ResourceManagement resourceManagement = getResourceManagement(hostingNode); try { resourceManagement.read(); String error = String.format("{} with UUID {} should not be visible.", HostingNode.NAME, hostingNode.getID()); @@ -243,7 +242,7 @@ public class BasicTest extends MultiContextTest { // OK } - isRelatedToManagement = ERManagementTest.getIsRelatedToManagement(activates); + isRelatedToManagement = getIsRelatedToManagement(activates); try { isRelatedToManagement.read(); String error = String.format("{} with UUID {} should not be visible.", Activates.NAME, activates.getID()); @@ -253,7 +252,7 @@ public class BasicTest extends MultiContextTest { // OK } - resourceManagement = ERManagementTest.getResourceManagement(eService); + resourceManagement = getResourceManagement(eService); try { resourceManagement.read(); String error = String.format("{} with UUID {} should not be visible.", EService.NAME, eService.getID()); @@ -268,22 +267,22 @@ public class BasicTest extends MultiContextTest { // The Instances MUST be still available in ALTERNATIVE_TEST_SCOPE ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE); - resourceManagement = ERManagementTest.getResourceManagement(hostingNode); + resourceManagement = getResourceManagement(hostingNode); resourceManagement.read(); - isRelatedToManagement = ERManagementTest.getIsRelatedToManagement(activates); + isRelatedToManagement = getIsRelatedToManagement(activates); isRelatedToManagement.read(); - resourceManagement = ERManagementTest.getResourceManagement(eService); + resourceManagement = getResourceManagement(eService); resourceManagement.read(); } finally { // Removing HostingNode MUST delete Activates and EService due to propagationConstraint - ResourceManagement resourceManagement = ERManagementTest.getResourceManagement(hostingNode); + ResourceManagement resourceManagement = getResourceManagement(hostingNode); resourceManagement.delete(); - resourceManagement = ERManagementTest.getResourceManagement(hostingNode); + resourceManagement = getResourceManagement(hostingNode); try { resourceManagement.read(); @@ -294,7 +293,7 @@ public class BasicTest extends MultiContextTest { // OK } - IsRelatedToManagement isRelatedToManagement = ERManagementTest.getIsRelatedToManagement(activates); + IsRelatedToManagement isRelatedToManagement = getIsRelatedToManagement(activates); try { isRelatedToManagement.read(); String error = String.format("{} with UUID {} should not be found.", Activates.NAME, activates.getID()); @@ -304,7 +303,7 @@ public class BasicTest extends MultiContextTest { // OK } - resourceManagement = ERManagementTest.getResourceManagement(eService); + resourceManagement = getResourceManagement(eService); try { resourceManagement.read(); String error = String.format("{} with UUID {} should not be found.", EService.NAME, eService.getID()); diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/ComplexTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/ComplexTest.java index b92c6c3..0ef3a62 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/ComplexTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/ComplexTest.java @@ -12,7 +12,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.instances.ERManagementTest; import org.gcube.informationsystem.resourceregistry.instances.model.entities.FacetManagement; import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement; import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement; @@ -39,10 +38,10 @@ public class ComplexTest extends MultiContextTest { contextCache.setContextCacheRenewal(contextCacheRenewal); /* Creating HostingNode */ - HostingNode hostingNode = ERManagementTest.createHostingNode(); + HostingNode hostingNode = createHostingNode(); /* Creating EService */ - EService eService = ERManagementTest.createEService(); + EService eService = createEService(); /* Creating Activates Relation */ @@ -72,7 +71,7 @@ public class ComplexTest extends MultiContextTest { logger.debug("Switching to alternative scope"); ContextTest.setContextByName(NEXTNEXT); - ResourceManagement hostingNodeManagement = ERManagementTest.getResourceManagement(hostingNode); + ResourceManagement hostingNodeManagement = getResourceManagement(hostingNode); String hostingNodeContexts = hostingNodeManagement.getContexts(); logger.debug("Contexts of {} with UUID {} have the following UUID {}", HostingNode.NAME, hostingNodeManagement.getUUID(), hostingNodeContexts); @@ -109,7 +108,7 @@ public class ComplexTest extends MultiContextTest { logger.debug("Contexts of {} with UUID {} are {}", Activates.NAME, activatesManagement.getUUID(), activatesContextMap.values()); - ResourceManagement eServiceManagement = ERManagementTest.getResourceManagement(eService); + ResourceManagement eServiceManagement = getResourceManagement(eService); String eServiceContexts = eServiceManagement.getContexts(); logger.debug("Contexts of {} with UUID {} have the following UUID {}", EService.NAME, eServiceManagement.getUUID(), eServiceContexts); Map eServiceContextMap = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextMap(eServiceContexts); @@ -121,11 +120,11 @@ public class ComplexTest extends MultiContextTest { Assert.assertTrue(eServiceContextFullNames.size()==1); logger.debug("Contexts of {} with UUID {} are {}", EService.NAME, eServiceManagement.getUUID(), eServiceContextFullNames); - ERManagementTest.deleteResource(hostingNode); + deleteResource(hostingNode); try { ContextTest.setContextByName(DEVNEXT); - ERManagementTest.deleteResource(eService); + deleteResource(eService); } catch (Exception e) { logger.error("",e); } diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/MultiContextTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/MultiContextTest.java index 77a0593..a6e14e4 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/MultiContextTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/MultiContextTest.java @@ -83,7 +83,7 @@ public class MultiContextTest extends ERManagementTest { // Must be investigated cause infinite recursion to Jackson // Map expectedInstances = getRemovedExpectedInstances(r); - ResourceManagement resourceManagement = ERManagementTest.getResourceManagement(r); + ResourceManagement resourceManagement = getResourceManagement(r); resourceManagement.setDryRun(dryRun); UUID contextUUID = ContextUtility.getCurrentSecurityContext().getUUID(); resourceManagement.removeFromContext(contextUUID); @@ -179,7 +179,7 @@ public class MultiContextTest extends ERManagementTest { protected Map getExpectedInstancesAddToContext(Resource resource) throws ResourceRegistryException, Exception { - String json = ERManagementTest.getResourceManagement(resource).read(); + String json = getResourceManagement(resource).read(); Resource r = ElementMapper.unmarshal(resource.getClass(), json); Map expected = new HashMap<>(); @@ -222,7 +222,7 @@ public class MultiContextTest extends ERManagementTest { // Map expectedInstances = getExpectedInstancesAddToContext(r); - ResourceManagement resourceManagement = ERManagementTest.getResourceManagement(r); + ResourceManagement resourceManagement = getResourceManagement(r); resourceManagement.setDryRun(dryRun); UUID contextUUID = ContextUtility.getInstance().getSecurityContextByFullName(targetContextFullName).getUUID(); resourceManagement.addToContext(contextUUID); @@ -233,7 +233,7 @@ public class MultiContextTest extends ERManagementTest { if(!dryRun) { String currentContext = SecretManagerProvider.instance.get().getContext(); ContextTest.setContextByName(targetContextFullName); - resourceManagement = ERManagementTest.getResourceManagement(r); + resourceManagement = getResourceManagement(r); String json = resourceManagement.read(); Resource resource = ElementMapper.unmarshal(r.getClass(), json); Assert.assertTrue(resource.getClass() == r.getClass()); @@ -281,7 +281,7 @@ public class MultiContextTest extends ERManagementTest { // expectedInstances.putAll(getExpectedInstancesAddToContext(isRelatedTo.getTarget())); // expectedInstances.put(isRelatedTo.getUUID(), isRelatedTo); - IsRelatedToManagement isRelatedToManagement = ERManagementTest.getIsRelatedToManagement(isRelatedTo); + IsRelatedToManagement isRelatedToManagement = getIsRelatedToManagement(isRelatedTo); isRelatedToManagement.setDryRun(dryRun); UUID contextUUID = ContextUtility.getInstance().getSecurityContextByFullName(targetContextFullName).getUUID(); isRelatedToManagement.addToContext(contextUUID); diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/queries/QueryTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/queries/QueryTest.java index 54a8d93..921ed49 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/queries/QueryTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/queries/QueryTest.java @@ -76,7 +76,7 @@ public class QueryTest extends ERManagementTest { int typeNumber = 0; for (int i = 0; i < MAX; i++) { - Map map = ERManagementTest.createHostingNodeAndEService(); + Map map = createHostingNodeAndEService(); if (typeNumber == 0) { typeNumber = map.size(); } @@ -150,14 +150,14 @@ public class QueryTest extends ERManagementTest { List resourceList = resources.get(HostingNode.NAME); for (Resource r : resourceList) { - ERManagementTest.deleteResource(r); + deleteResource(r); } } } @Test public void testGetAllFrom() throws Exception { - Map map = ERManagementTest.createHostingNodeAndEService(); + Map map = createHostingNodeAndEService(); EService eService = (EService) map.get(EService.NAME); HostingNode hostingNode = (HostingNode) map.get(HostingNode.NAME); @@ -280,8 +280,8 @@ public class QueryTest extends ERManagementTest { /* END EService --ConsistsOf--> SoftwareFacet */ } finally { - ERManagementTest.deleteResource(eService); - ERManagementTest.deleteResource(hostingNode); + deleteResource(eService); + deleteResource(hostingNode); } } @@ -330,7 +330,7 @@ public class QueryTest extends ERManagementTest { simplePropertyFacet.setValue("test"); configuration.addFacet(simplePropertyFacet); - return ERManagementTest.createResource(configuration); + return createResource(configuration); } @@ -448,7 +448,7 @@ public class QueryTest extends ERManagementTest { } } finally { for(Configuration configuration : createdConfigurations) { - ERManagementTest.deleteResource(configuration); + deleteResource(configuration); } }