From 90a25ee7e7dc07d369ef5232e416b8be1ab9da66 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 22 Oct 2021 19:26:36 +0200 Subject: [PATCH] All the refactored code has been tested with existing JUnit tests --- .../contexts/ContextUtility.java | 3 +- .../contexts/entities/ContextManagement.java | 6 +-- .../relations/IsParentOfManagement.java | 4 +- .../security/AdminSecurityContext.java | 18 +++++++-- .../security/ContextSecurityContext.java | 18 +++++++-- .../QueryTemplatesSecurityContext.java | 18 +++++++-- .../security/SchemaSecurityContext.java | 19 ++++++++-- .../dbinitialization/DatabaseEnvironment.java | 20 +++------- .../templates/QueryTemplateManagement.java | 37 ++++++++++++++----- .../EntityTypeDefinitionManagement.java | 4 +- .../PropertyTypeDefinitionManagement.java | 4 +- .../RelationTypeDefinitionManagement.java | 4 +- .../contexts/ContextManagementTest.java | 4 +- .../resourceregistry/query/JsonQueryTest.java | 2 +- .../QueryTemplateManagementTest.java | 31 ++++++++++++++++ src/test/resources/queries/query6.json | 23 ++++++++++++ src/test/resources/queries/query6.query | 1 + 17 files changed, 159 insertions(+), 57 deletions(-) create mode 100644 src/test/java/org/gcube/informationsystem/resourceregistry/query/templates/QueryTemplateManagementTest.java create mode 100644 src/test/resources/queries/query6.json create mode 100644 src/test/resources/queries/query6.query 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 873f748..c1ec576 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ContextUtility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ContextUtility.java @@ -103,8 +103,7 @@ public class ContextUtility { } public static AdminSecurityContext getAdminSecurityContext() throws ResourceRegistryException { - AdminSecurityContext adminSecurityContext = (AdminSecurityContext) ContextUtility.getInstance() - .getSecurityContextByUUID(AdminSecurityContext.ADMIN_SECURITY_CONTEXT_UUID); + AdminSecurityContext adminSecurityContext = AdminSecurityContext.getInstance(); return adminSecurityContext; } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/entities/ContextManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/entities/ContextManagement.java index 9a570e5..d94162b 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/entities/ContextManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/entities/ContextManagement.java @@ -112,8 +112,7 @@ public class ContextManagement extends EntityElementManagement> definitionToBeCreated = new ArrayList<>(); definitionToBeCreated.add(PropertyElement.class); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/query/templates/QueryTemplateManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/query/templates/QueryTemplateManagement.java index 526be48..101db97 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/query/templates/QueryTemplateManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/query/templates/QueryTemplateManagement.java @@ -2,25 +2,28 @@ package org.gcube.informationsystem.resourceregistry.query.templates; import java.util.HashMap; +import org.gcube.com.fasterxml.jackson.core.JsonProcessingException; import org.gcube.com.fasterxml.jackson.databind.JsonNode; +import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; +import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode; import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.context.reference.entities.Context; import org.gcube.informationsystem.query.templates.reference.entities.QueryTemplate; -import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException; -import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; import org.gcube.informationsystem.resourceregistry.contexts.security.QueryTemplatesSecurityContext; import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext; import org.gcube.informationsystem.resourceregistry.instances.base.entities.EntityElementManagement; +import org.gcube.informationsystem.resourceregistry.utils.Utility; import org.gcube.informationsystem.types.reference.entities.EntityType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.orientechnologies.orient.core.db.document.ODatabaseDocument; import com.orientechnologies.orient.core.record.OVertex; +import com.orientechnologies.orient.core.record.impl.ODocument; import com.orientechnologies.orient.core.sql.executor.OResultSet; /** @@ -63,8 +66,7 @@ public class QueryTemplateManagement extends EntityElementManagement iterable = oDatabaseDocument.browseClass(typeName, polymorphic); + for (ODocument vertex : iterable) { + QueryTemplateManagement queryTemplateManagement = new QueryTemplateManagement(); + queryTemplateManagement.setElement((OVertex) vertex); + try { + JsonNode jsonObject = queryTemplateManagement.serializeAsJsonNode(); + arrayNode.add(jsonObject); + } catch (ResourceRegistryException e) { + logger.error("Unable to correctly serialize {}. It will be excluded from results. {}", + vertex.toString(), Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE); + } + } + try { + return objectMapper.writeValueAsString(arrayNode); + } catch (JsonProcessingException e) { + throw new ResourceRegistryException(e); + } } } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/types/entities/EntityTypeDefinitionManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/types/entities/EntityTypeDefinitionManagement.java index 242e41c..5405fdb 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/types/entities/EntityTypeDefinitionManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/types/entities/EntityTypeDefinitionManagement.java @@ -13,7 +13,6 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.Entity import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException; -import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; import org.gcube.informationsystem.resourceregistry.contexts.security.SchemaSecurityContext; import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility; @@ -50,8 +49,7 @@ public abstract class EntityTypeDefinitionManagement exten @Override protected SecurityContext getWorkingContext() throws ResourceRegistryException { if (workingContext == null) { - workingContext = ContextUtility.getInstance() - .getSecurityContextByUUID(SchemaSecurityContext.SCHEMA_SECURITY_CONTEXT_UUID); + workingContext = SchemaSecurityContext.getInstance(); } return workingContext; } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/types/properties/PropertyTypeDefinitionManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/types/properties/PropertyTypeDefinitionManagement.java index 019b16f..dc99032 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/types/properties/PropertyTypeDefinitionManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/types/properties/PropertyTypeDefinitionManagement.java @@ -12,7 +12,6 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegis import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException; -import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; import org.gcube.informationsystem.resourceregistry.contexts.security.SchemaSecurityContext; import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement; @@ -57,8 +56,7 @@ public class PropertyTypeDefinitionManagement extends ElementManagement