All the refactored code has been tested with existing JUnit tests

This commit is contained in:
Luca Frosini 2021-10-22 19:26:36 +02:00
parent 6e985eec65
commit 90a25ee7e7
17 changed files with 159 additions and 57 deletions

View File

@ -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;
}

View File

@ -112,8 +112,7 @@ public class ContextManagement extends EntityElementManagement<Context, EntityTy
@Override
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
if (workingContext == null) {
workingContext = ContextUtility.getInstance()
.getSecurityContextByUUID(ContextSecurityContext.CONTEXT_SECURITY_CONTEXT_UUID);
workingContext = ContextSecurityContext.getInstance();
}
return workingContext;
}
@ -149,13 +148,14 @@ public class ContextManagement extends EntityElementManagement<Context, EntityTy
select.append("\"");
select.append(QueryLogicalOperator.AND.getLogicalOperator());
select.append(Context.HEADER_PROPERTY);
select.append(".");
select.append(Header.UUID_PROPERTY);
select.append(QueryConditionalOperator.NE.getConditionalOperator());
select.append("\"");
select.append(parentContext.uuid);
select.append("\"");
errorMessage.append("A root ");
errorMessage.append("A ");
errorMessage.append(Context.NAME);
errorMessage.append(" with ");
errorMessage.append(this.getName());

View File

@ -14,7 +14,6 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegis
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isparentof.IsParentOfAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isparentof.IsParentOfNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException;
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
import org.gcube.informationsystem.resourceregistry.contexts.entities.ContextManagement;
import org.gcube.informationsystem.resourceregistry.contexts.security.ContextSecurityContext;
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
@ -51,8 +50,7 @@ public class IsParentOfManagement extends RelationElementManagement<ContextManag
@Override
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
if (workingContext == null) {
workingContext = ContextUtility.getInstance()
.getSecurityContextByUUID(ContextSecurityContext.CONTEXT_SECURITY_CONTEXT_UUID);
workingContext = ContextSecurityContext.getInstance();
}
return workingContext;
}

View File

@ -3,6 +3,7 @@ package org.gcube.informationsystem.resourceregistry.contexts.security;
import java.util.UUID;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -18,15 +19,26 @@ public class AdminSecurityContext extends SecurityContext {
private static Logger logger = LoggerFactory.getLogger(SecurityContext.class);
public static final String ADMIN_SECURITY_CONTEXT;
public static final UUID ADMIN_SECURITY_CONTEXT_UUID;
private static final String ADMIN_SECURITY_CONTEXT;
private static final UUID ADMIN_SECURITY_CONTEXT_UUID;
static {
ADMIN_SECURITY_CONTEXT = "00000000-0000-0000-0000-000000000000";
ADMIN_SECURITY_CONTEXT_UUID = UUID.fromString(ADMIN_SECURITY_CONTEXT);
}
public AdminSecurityContext() throws ResourceRegistryException {
private static AdminSecurityContext instance;
public static AdminSecurityContext getInstance() throws ResourceRegistryException {
if(instance==null) {
instance = new AdminSecurityContext();
ContextUtility contextUtility = ContextUtility.getInstance();
contextUtility.addSecurityContext(ADMIN_SECURITY_CONTEXT, instance);
}
return instance;
}
private AdminSecurityContext() throws ResourceRegistryException {
super(ADMIN_SECURITY_CONTEXT_UUID, false);
}

View File

@ -3,6 +3,7 @@ package org.gcube.informationsystem.resourceregistry.contexts.security;
import java.util.UUID;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -16,15 +17,26 @@ public class ContextSecurityContext extends SecurityContext {
private static Logger logger = LoggerFactory.getLogger(SecurityContext.class);
public static final String CONTEXT_SECURITY_CONTEXT;
public static final UUID CONTEXT_SECURITY_CONTEXT_UUID;
private static final String CONTEXT_SECURITY_CONTEXT;
private static final UUID CONTEXT_SECURITY_CONTEXT_UUID;
static {
CONTEXT_SECURITY_CONTEXT = "ffffffff-ffff-ffff-ffff-ffffffffffff";
CONTEXT_SECURITY_CONTEXT_UUID = UUID.fromString(CONTEXT_SECURITY_CONTEXT);
}
public ContextSecurityContext() throws ResourceRegistryException {
private static ContextSecurityContext instance;
public static ContextSecurityContext getInstance() throws ResourceRegistryException {
if(instance==null) {
instance = new ContextSecurityContext();
ContextUtility contextUtility = ContextUtility.getInstance();
contextUtility.addSecurityContext(CONTEXT_SECURITY_CONTEXT, instance);
}
return instance;
}
private ContextSecurityContext() throws ResourceRegistryException {
super(CONTEXT_SECURITY_CONTEXT_UUID, false);
}

View File

@ -3,6 +3,7 @@ package org.gcube.informationsystem.resourceregistry.contexts.security;
import java.util.UUID;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -16,15 +17,26 @@ public class QueryTemplatesSecurityContext extends SecurityContext {
private static Logger logger = LoggerFactory.getLogger(SecurityContext.class);
public static final String QUERY_TEMPLATES_SECURITY_CONTEXT;
public static final UUID QUERY_TEMPLATES_SECURITY_CONTEXT_UUID;
private static final String QUERY_TEMPLATES_SECURITY_CONTEXT;
private static final UUID QUERY_TEMPLATES_SECURITY_CONTEXT_UUID;
static {
QUERY_TEMPLATES_SECURITY_CONTEXT = "dddddddd-dddd-dddd-dddd-dddddddddddd";
QUERY_TEMPLATES_SECURITY_CONTEXT_UUID = UUID.fromString(QUERY_TEMPLATES_SECURITY_CONTEXT);
}
public QueryTemplatesSecurityContext() throws ResourceRegistryException {
private static QueryTemplatesSecurityContext instance;
public static QueryTemplatesSecurityContext getInstance() throws ResourceRegistryException {
if(instance==null) {
instance = new QueryTemplatesSecurityContext();
ContextUtility contextUtility = ContextUtility.getInstance();
contextUtility.addSecurityContext(QUERY_TEMPLATES_SECURITY_CONTEXT, instance);
}
return instance;
}
private QueryTemplatesSecurityContext() throws ResourceRegistryException {
super(QUERY_TEMPLATES_SECURITY_CONTEXT_UUID, false);
}

View File

@ -3,6 +3,7 @@ package org.gcube.informationsystem.resourceregistry.contexts.security;
import java.util.UUID;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -16,16 +17,26 @@ public class SchemaSecurityContext extends SecurityContext {
private static Logger logger = LoggerFactory.getLogger(SecurityContext.class);
// Used to persist Schemas
public static final String SCHEMA_SECURITY_CONTEXT;
public static final UUID SCHEMA_SECURITY_CONTEXT_UUID;
private static final String SCHEMA_SECURITY_CONTEXT;
private static final UUID SCHEMA_SECURITY_CONTEXT_UUID;
static {
SCHEMA_SECURITY_CONTEXT = "eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee";
SCHEMA_SECURITY_CONTEXT_UUID = UUID.fromString(SCHEMA_SECURITY_CONTEXT);
}
public SchemaSecurityContext() throws ResourceRegistryException {
private static SchemaSecurityContext instance;
public static SchemaSecurityContext getInstance() throws ResourceRegistryException {
if(instance==null) {
instance = new SchemaSecurityContext();
ContextUtility contextUtility = ContextUtility.getInstance();
contextUtility.addSecurityContext(SCHEMA_SECURITY_CONTEXT, instance);
}
return instance;
}
private SchemaSecurityContext() throws ResourceRegistryException {
super(SCHEMA_SECURITY_CONTEXT_UUID, false);
}

View File

@ -19,7 +19,6 @@ import org.gcube.informationsystem.base.reference.properties.PropertyElement;
import org.gcube.informationsystem.base.reference.relations.RelationElement;
import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.model.reference.properties.Property;
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
import org.gcube.informationsystem.resourceregistry.contexts.security.AdminSecurityContext;
import org.gcube.informationsystem.resourceregistry.contexts.security.ContextSecurityContext;
import org.gcube.informationsystem.resourceregistry.contexts.security.QueryTemplatesSecurityContext;
@ -183,19 +182,11 @@ public class DatabaseEnvironment {
try {
boolean created = initGraphDB();
ContextUtility contextUtility = ContextUtility.getInstance();
AdminSecurityContext adminSecurityContext = AdminSecurityContext.getInstance();
AdminSecurityContext adminSecurityContext = new AdminSecurityContext();
contextUtility.addSecurityContext(adminSecurityContext.getUUID().toString(), adminSecurityContext);
QueryTemplatesSecurityContext queryTemplatesSecurityContext = new QueryTemplatesSecurityContext();
contextUtility.addSecurityContext(queryTemplatesSecurityContext.getUUID().toString(), queryTemplatesSecurityContext);
ContextSecurityContext contextSecurityContext = new ContextSecurityContext();
contextUtility.addSecurityContext(contextSecurityContext.getUUID().toString(), contextSecurityContext);
SchemaSecurityContext schemaSecurityContext = new SchemaSecurityContext();
contextUtility.addSecurityContext(schemaSecurityContext.getUUID().toString(), schemaSecurityContext);
QueryTemplatesSecurityContext queryTemplatesSecurityContext = QueryTemplatesSecurityContext.getInstance();
SchemaSecurityContext schemaSecurityContext = SchemaSecurityContext.getInstance();
ContextSecurityContext contextSecurityContext = ContextSecurityContext.getInstance();
if(created) {
ODatabasePool pool = new ODatabasePool(DatabaseEnvironment.DB_URI, CHANGED_ADMIN_USERNAME,
@ -206,9 +197,10 @@ public class DatabaseEnvironment {
oDatabaseDocument.close();
pool.close();
queryTemplatesSecurityContext.create();
schemaSecurityContext.create();
contextSecurityContext.create();
schemaSecurityContext.create();
List<Class<? extends Element>> definitionToBeCreated = new ArrayList<>();
definitionToBeCreated.add(PropertyElement.class);

View File

@ -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<Context, En
@Override
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
if (workingContext == null) {
workingContext = ContextUtility.getInstance()
.getSecurityContextByUUID(QueryTemplatesSecurityContext.QUERY_TEMPLATES_SECURITY_CONTEXT_UUID);
workingContext = QueryTemplatesSecurityContext.getInstance();
}
return workingContext;
}
@ -125,8 +127,9 @@ public class QueryTemplateManagement extends EntityElementManagement<Context, En
@Override
protected OVertex reallyUpdate() throws NotFoundException, ResourceRegistryException {
try {
tryTemplate();
OVertex queryTemplate = getElement();
queryTemplate = (OVertex) updateProperties(oClass, queryTemplate, jsonNode, ignoreKeys, ignoreStartWithKeys);
return getElement();
} catch(ResourceRegistryException e) {
throw e;
@ -139,8 +142,7 @@ public class QueryTemplateManagement extends EntityElementManagement<Context, En
@Override
protected void reallyDelete() throws NotFoundException, ResourceRegistryException {
// TODO Auto-generated method stub
getElement().delete();
}
@Override
@ -155,8 +157,25 @@ public class QueryTemplateManagement extends EntityElementManagement<Context, En
@Override
public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
// TODO Auto-generated method stub
return null;
ObjectMapper objectMapper = new ObjectMapper();
ArrayNode arrayNode = objectMapper.createArrayNode();
Iterable<ODocument> 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);
}
}
}

View File

@ -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<E extends EntityType> exten
@Override
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
if (workingContext == null) {
workingContext = ContextUtility.getInstance()
.getSecurityContextByUUID(SchemaSecurityContext.SCHEMA_SECURITY_CONTEXT_UUID);
workingContext = SchemaSecurityContext.getInstance();
}
return workingContext;
}

View File

@ -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<OElement
@Override
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
if(workingContext == null) {
workingContext = ContextUtility.getInstance()
.getSecurityContextByUUID(SchemaSecurityContext.SCHEMA_SECURITY_CONTEXT_UUID);
workingContext = SchemaSecurityContext.getInstance();
}
return workingContext;
}

View File

@ -15,7 +15,6 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.Rela
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;
@ -61,8 +60,7 @@ public abstract class RelationTypeDefinitionManagement<T extends EntityTypeDefin
@Override
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
if (workingContext == null) {
this.workingContext = ContextUtility.getInstance()
.getSecurityContextByUUID(SchemaSecurityContext.SCHEMA_SECURITY_CONTEXT_UUID);
this.workingContext = SchemaSecurityContext.getInstance();
}
return workingContext;
}

View File

@ -84,9 +84,7 @@ public class ContextManagementTest extends ContextTest {
}
protected void roleUserAssertions(UUID uuid, UUID oldParentUUID, boolean deleted) throws ResourceRegistryException {
ContextSecurityContext contextSecurityContext = new ContextSecurityContext();
ContextUtility.getInstance().addSecurityContext(contextSecurityContext.getUUID().toString(),
contextSecurityContext);
ContextSecurityContext contextSecurityContext = ContextSecurityContext.getInstance();
ODatabaseDocument oDatabaseDocument = contextSecurityContext.getDatabaseDocument(PermissionMode.READER);
OSecurity oSecurity = oDatabaseDocument.getMetadata().getSecurity();

View File

@ -34,7 +34,7 @@ public class JsonQueryTest extends ContextTest {
File queriesDirectory = getQueriesDirectory();
for(int i=1; i<6; i++) {
for(int i=1; i<7; i++) {
File jsonQueryFile = new File(queriesDirectory, "query" + i + ".json");
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonQueryFile);

View File

@ -0,0 +1,31 @@
package org.gcube.informationsystem.resourceregistry.query.templates;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class QueryTemplateManagementTest {
private static Logger logger = LoggerFactory.getLogger(QueryTemplateManagementTest.class);
//@Test
public void testCreate() throws ResourceRegistryException {
QueryTemplateManagement queryTemplateManagement = new QueryTemplateManagement();
queryTemplateManagement.setName("Test");
queryTemplateManagement.setJson("");
String created = queryTemplateManagement.create();
logger.debug(created);
}
@Test
public void testUpdate() {
}
@Test
public void testDelete() {
}
}

View File

@ -0,0 +1,23 @@
{
"@class": "EService",
"consistsOf": [
{
"@class": "ConsistsOf",
"propagationConstraint" : {
"add": "propagate"
},
"target": {
"@class": "StateFacet",
"value": "down"
}
},
{
"@class": "IsIdentifiedBy",
"target": {
"@class": "SoftwareFacet",
"name": "data-transfer-service",
"group": "DataTransfer"
}
}
]
}

View File

@ -0,0 +1 @@
TRAVERSE outV("EService") FROM ( TRAVERSE inE("IsIdentifiedBy") FROM ( SELECT FROM ( TRAVERSE inV("SoftwareFacet") FROM ( TRAVERSE outE("IsIdentifiedBy") FROM ( TRAVERSE outV("EService") FROM ( SELECT FROM ( TRAVERSE inE("ConsistsOf") FROM ( SELECT FROM StateFacet WHERE value = "down")) WHERE propagationConstraint.add = "propagate")))) WHERE name = "data-transfer-service" AND group = "DataTransfer"))