From ac54fe6694350736cdd383493371501d273b0ba8 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 2 Feb 2023 14:26:59 +0100 Subject: [PATCH] Ported changes from master --- CHANGELOG.md | 10 ++-- .../instances/base/ElementManagement.java | 16 ++++- .../base/relations/JsonQueryIsRelatedTo.java | 58 ++++++++++++++----- .../instances/ERManagementTest.java | 34 +++++------ .../queries/JsonQueryTest.java | 17 ++++-- src/test/resources/queries/query8.json | 29 ++++++++++ src/test/resources/queries/query8.query | 1 + src/test/resources/queries/query9.json | 36 ++++++++++++ src/test/resources/queries/query9.query | 1 + 9 files changed, 161 insertions(+), 41 deletions(-) create mode 100644 src/test/resources/queries/query8.json create mode 100644 src/test/resources/queries/query8.query create mode 100644 src/test/resources/queries/query9.json create mode 100644 src/test/resources/queries/query9.query diff --git a/CHANGELOG.md b/CHANGELOG.md index 88b4f1f..04fc213 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,13 +5,13 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [v5.0.0-SNAPSHOT] - Switched to gcube-smartgears-bom 3.0.0 - -## [v4.1.1] - - Fixed bug on JSONQuery for Facets which does not have any properties to match [#24237] +- Fixed bug on JSONQuery for IsRelatedTo relations indicating both source and target resources [#24264] +- Fixed bug on returned boolean values as string [#24240] +- Enabled array properties [#24225] + - -## [v4.1.0-SNAPSHOT] +## [v4.1.0] - Relation source-target instance types are validated against Relation schema [#7355] - The instances are validated with the defined schema [#18216] 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 2a5ccef..4a473b2 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 @@ -20,6 +20,7 @@ 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.com.fasterxml.jackson.databind.node.BooleanNode; import org.gcube.com.fasterxml.jackson.databind.node.JsonNodeType; import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode; import org.gcube.com.fasterxml.jackson.databind.node.TextNode; @@ -893,7 +894,16 @@ public abstract class ElementManagement { return PropertyElementManagement.getPropertyDocument(value); case ARRAY: - break; + /* + * Due to bug https://github.com/orientechnologies/orientdb/issues/7354 + * we should not support ArrayList + */ + List list = new ArrayList<>(); + ArrayNode arrayNode = (ArrayNode) value; + for(JsonNode node : arrayNode) { + list.add(getObjectFromJsonNode(node)); + } + return list; case BINARY: break; @@ -1194,6 +1204,10 @@ public abstract class ElementManagement { return objectNode; } + if(object instanceof Boolean) { + return BooleanNode.valueOf((Boolean) object); + } + return new TextNode(object.toString()); } catch(Exception e) { diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/queries/json/base/relations/JsonQueryIsRelatedTo.java b/src/main/java/org/gcube/informationsystem/resourceregistry/queries/json/base/relations/JsonQueryIsRelatedTo.java index 7b48b5c..a800e6c 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/queries/json/base/relations/JsonQueryIsRelatedTo.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/queries/json/base/relations/JsonQueryIsRelatedTo.java @@ -3,7 +3,7 @@ package org.gcube.informationsystem.resourceregistry.queries.json.base.relations import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.base.reference.Direction; -import org.gcube.informationsystem.model.reference.relations.ConsistsOf; +import org.gcube.informationsystem.model.reference.relations.IsRelatedTo; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException; @@ -32,7 +32,19 @@ public class JsonQueryIsRelatedTo extends JsonQueryRelation { StringBuffer buffer = new StringBuffer(); int size = jsonNode.size(); - if(size > 2) { + + // Remove @class from size + --size; + + if(jsonNode.has(IsRelatedTo.SOURCE_PROPERTY)) { + --size; + } + + if(jsonNode.has(IsRelatedTo.TARGET_PROPERTY)) { + --size; + } + + if(size > 0) { buffer.append("SELECT FROM "); if(entryPoint) { buffer.append(type); @@ -60,8 +72,8 @@ public class JsonQueryIsRelatedTo extends JsonQueryRelation { stringBuffer = buffer; - // Size 2 means that only '@class' and 'target'/'source' properties are present - if(size > 2) { + // Size 0 means that only '@class' and 'target'/'source' properties are present + if(size > 0) { if(!entryPoint) { stringBuffer.append(" )"); // Close ) SELECT } @@ -75,8 +87,8 @@ public class JsonQueryIsRelatedTo extends JsonQueryRelation { @Override public StringBuffer analize(StringBuffer stringBuffer) throws SchemaException, ResourceRegistryException { - JsonNode sourceJsonNode = jsonNode.get(ConsistsOf.SOURCE_PROPERTY); - JsonNode targetJsonNode = jsonNode.get(ConsistsOf.TARGET_PROPERTY); + JsonNode sourceJsonNode = jsonNode.get(IsRelatedTo.SOURCE_PROPERTY); + JsonNode targetJsonNode = jsonNode.get(IsRelatedTo.TARGET_PROPERTY); JsonNode resourceJsonNode = null; @@ -96,11 +108,13 @@ public class JsonQueryIsRelatedTo extends JsonQueryRelation { stringBuffer = jsonQueryResource.analize(stringBuffer); StringBuffer buffer = new StringBuffer(); - buffer.append("TRAVERSE "); - buffer.append(direction.opposite().name().toLowerCase()); - buffer.append("V(\""); - buffer.append(requestedResourceType); - buffer.append("\") FROM ( "); + if(requestedResourceType!=null) { + buffer.append("TRAVERSE "); + buffer.append(direction.opposite().name().toLowerCase()); + buffer.append("V(\""); + buffer.append(requestedResourceType); + buffer.append("\") FROM ( "); + } buffer.append("TRAVERSE "); buffer.append(direction.name().toLowerCase()); buffer.append("E(\""); @@ -108,13 +122,29 @@ public class JsonQueryIsRelatedTo extends JsonQueryRelation { buffer.append("\") FROM ( "); buffer.append(stringBuffer); buffer.append(")"); - buffer.append(")"); + if(requestedResourceType!=null) { + buffer.append(")"); + } stringBuffer = buffer; + if(sourceJsonNode!=null && targetJsonNode!=null) { + // Target has still to be analised + + jsonQueryResource = new JsonQueryResource(targetJsonNode); + jsonQueryResource.setDirection(Direction.IN); + jsonQueryResource.setEntryPoint(false); + stringBuffer = jsonQueryResource.analize(stringBuffer); + + boolean entryPointOldValue = entryPoint; + // It is no more and entry point for the function traverseThisEdge + entryPoint = false; + stringBuffer = traverseThisEdge(stringBuffer); + // Restoring entryPoint indication + entryPoint = entryPointOldValue; + } + return stringBuffer; } - - } 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 37293e5..fd8b3e6 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/ERManagementTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/ERManagementTest.java @@ -95,23 +95,23 @@ public class ERManagementTest extends ContextTest { public static final String VERSION = "1.0.0"; public static final String NEW_VERSION = "2.0.0"; - @Before - @After - public void cleanInstances() throws Exception { - // Clean the environment first to avoid error if a previous tests fails without cleaning the env - ResourceManagement rm = (ResourceManagement) ElementManagementUtility.getERManagement(Resource.NAME); - String all = rm.all(true); - List allResources = ElementMapper.unmarshalList(Resource.class, all); - for(Resource r : allResources) { - try { - ERManagementTest.deleteResource(r); - }catch (ResourceNotFoundException e) { - // A resource could be already deleted deleting another resource giving the propagation constraint - }catch (ODatabaseException e) { - // could occur - } - } - } +// @Before +// @After +// public void cleanInstances() throws Exception { +// // Clean the environment first to avoid error if a previous tests fails without cleaning the env +// ResourceManagement rm = (ResourceManagement) ElementManagementUtility.getERManagement(Resource.NAME); +// String all = rm.all(true); +// List allResources = ElementMapper.unmarshalList(Resource.class, all); +// for(Resource r : allResources) { +// try { +// ERManagementTest.deleteResource(r); +// }catch (ResourceNotFoundException e) { +// // A resource could be already deleted deleting another resource giving the propagation constraint +// }catch (ODatabaseException e) { +// // could occur +// } +// } +// } public static SoftwareFacet getSoftwareFacet() { diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/queries/JsonQueryTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/queries/JsonQueryTest.java index b074f6c..608fae3 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/queries/JsonQueryTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/queries/JsonQueryTest.java @@ -3,6 +3,7 @@ package org.gcube.informationsystem.resourceregistry.queries; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; +import java.io.FilenameFilter; import java.net.URL; import org.gcube.com.fasterxml.jackson.databind.JsonNode; @@ -34,8 +35,16 @@ public class JsonQueryTest extends ContextTest { File queriesDirectory = getQueriesDirectory(); - for(int i=1; i<7; i++) { - File jsonQueryFile = new File(queriesDirectory, "query" + i + ".json"); + FilenameFilter filenameFilter = new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + return name.endsWith(".json"); + } + }; + + for(File jsonQueryFile : queriesDirectory.listFiles(filenameFilter)) { + logger.info("Going to read JSON query frtm file {}", jsonQueryFile.getAbsolutePath()); + ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(jsonQueryFile); logger.info("Going to test the following JSON query {}", jsonNode.toString()); @@ -47,7 +56,7 @@ public class JsonQueryTest extends ContextTest { logger.info("Created Query from JSON: {}", createdStringBuffer.toString()); StringBuffer expectedStringBuffer = new StringBuffer(); - File expectedQueryFile = new File(queriesDirectory, "query" + i + ".query"); + File expectedQueryFile = new File(queriesDirectory, jsonQueryFile.getName().replace("json", "query")); try(BufferedReader br = new BufferedReader(new FileReader(expectedQueryFile))) { for(String line; (line = br.readLine()) != null; ) { expectedStringBuffer.append(line); @@ -66,7 +75,7 @@ public class JsonQueryTest extends ContextTest { @Test public void testSingleCreateQuery() throws Exception { File queriesDirectory = getQueriesDirectory(); - File jsonQueryFile = new File(queriesDirectory, "query7.json"); + File jsonQueryFile = new File(queriesDirectory, "query9.json"); ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(jsonQueryFile); logger.info("Going to test the following JSON query {}", jsonNode.toString()); diff --git a/src/test/resources/queries/query8.json b/src/test/resources/queries/query8.json new file mode 100644 index 0000000..367ffc8 --- /dev/null +++ b/src/test/resources/queries/query8.json @@ -0,0 +1,29 @@ +{ + "@class": "CallsFor", + "source": { + "@class": "EService", + "consistsOf": [ + { + "@class": "IsIdentifiedBy", + "target": { + "@class": "SoftwareFacet", + "group": "org.gcube.data-catalogue", + "name": "gcat" + } + } + ] + }, + "target":{ + "@class": "VirtualService", + "consistsOf": [ + { + "@class": "IsIdentifiedBy", + "target": { + "@class": "SoftwareFacet", + "group": "org.gcube.data-catalogue", + "name": "catalogue-virtual-service" + } + } + ] + } +} \ No newline at end of file diff --git a/src/test/resources/queries/query8.query b/src/test/resources/queries/query8.query new file mode 100644 index 0000000..6720b28 --- /dev/null +++ b/src/test/resources/queries/query8.query @@ -0,0 +1 @@ +TRAVERSE inE("CallsFor") FROM ( TRAVERSE outV("VirtualService") FROM ( TRAVERSE inE("IsIdentifiedBy") FROM ( SELECT FROM ( TRAVERSE inV("SoftwareFacet") FROM ( TRAVERSE outE("IsIdentifiedBy") FROM ( TRAVERSE inV("VirtualService") FROM ( TRAVERSE outE("CallsFor") FROM ( TRAVERSE outV("EService") FROM ( TRAVERSE inE("IsIdentifiedBy") FROM ( SELECT FROM ( TRAVERSE inV("SoftwareFacet") FROM ( TRAVERSE outE("IsIdentifiedBy") FROM ( TRAVERSE outV("EService") FROM ( SELECT FROM CallsFor)))) WHERE group = "org.gcube.data-catalogue" AND name = "gcat"))))))) WHERE group = "org.gcube.data-catalogue" AND name = "catalogue-virtual-service"))) \ No newline at end of file diff --git a/src/test/resources/queries/query9.json b/src/test/resources/queries/query9.json new file mode 100644 index 0000000..5ee9d9b --- /dev/null +++ b/src/test/resources/queries/query9.json @@ -0,0 +1,36 @@ +{ + "@class": "SimpleFacet", + "_in": { + "@class": "ConsistsOf", + "source": { + "@class": "Configuration", + "consistsOf": [ + { + "@class": "IsIdentifiedBy", + "target": { + "@class": "IdentifierFacet", + "value": "gcat-configuration" + } + } + ], + "isRelatedTo": [ + { + "@class": "IsCustomizedBy", + "source": { + "@class": "VirtualService", + "consistsOf": [ + { + "@class": "IsIdentifiedBy", + "target": { + "@class": "SoftwareFacet", + "group": "org.gcube.data-catalogue", + "name": "catalogue-virtual-service" + } + } + ] + } + } + ] + } + } +} \ No newline at end of file diff --git a/src/test/resources/queries/query9.query b/src/test/resources/queries/query9.query new file mode 100644 index 0000000..b723d4b --- /dev/null +++ b/src/test/resources/queries/query9.query @@ -0,0 +1 @@ +SELECT FROM ( TRAVERSE inV("SimpleFacet") FROM ( TRAVERSE outE("ConsistsOf") FROM ( TRAVERSE outV("Configuration") FROM ( TRAVERSE inE("IsIdentifiedBy") FROM ( SELECT FROM ( TRAVERSE inV("IdentifierFacet") FROM ( TRAVERSE outE("IsIdentifiedBy") FROM ( TRAVERSE inV("Configuration") FROM ( TRAVERSE outE("IsCustomizedBy") FROM ( TRAVERSE outV("VirtualService") FROM ( TRAVERSE inE("IsIdentifiedBy") FROM ( SELECT FROM ( TRAVERSE inV("SoftwareFacet") FROM ( TRAVERSE outE("IsIdentifiedBy") FROM ( TRAVERSE outV("VirtualService") FROM ( SELECT FROM IsCustomizedBy)))) WHERE group = "org.gcube.data-catalogue" AND name = "catalogue-virtual-service"))))))) WHERE value = "gcat-configuration"))))) \ No newline at end of file