You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by sh...@apache.org on 2022/02/20 10:47:18 UTC

[unomi] branch UNOMI-486-json-schema-graphql updated: JSON Schema GraphQL integration - Fix for integration tests - Bug fix in event type name registration - Add form event type definition

This is an automated email from the ASF dual-hosted git repository.

shuber pushed a commit to branch UNOMI-486-json-schema-graphql
in repository https://gitbox.apache.org/repos/asf/unomi.git


The following commit(s) were added to refs/heads/UNOMI-486-json-schema-graphql by this push:
     new 5d71607  JSON Schema GraphQL integration - Fix for integration tests - Bug fix in event type name registration - Add form event type definition
5d71607 is described below

commit 5d716073bf67469d3242e68b917895c5aa7559c3
Author: Serge Huber <sh...@jahia.com>
AuthorDate: Sun Feb 20 11:47:12 2022 +0100

    JSON Schema GraphQL integration
    - Fix for integration tests
    - Bug fix in event type name registration
    - Add form event type definition
---
 .../org/apache/unomi/api/utils/ParserHelper.java   |  2 +-
 .../graphql/schema/GraphQLSchemaProvider.java      |  4 ++--
 .../test/java/org/apache/unomi/itests/BaseIT.java  |  6 +++++
 .../org/apache/unomi/itests/ContextServletIT.java  |  9 ++++----
 .../services/impl/events/EventServiceImpl.java     |  5 +++-
 .../services/impl/schemas/SchemaRegistryImpl.java  | 18 ++++++++++++---
 .../META-INF/cxs/schemas/events/form.json          | 27 ++++++++++++++++++++++
 7 files changed, 60 insertions(+), 11 deletions(-)

diff --git a/api/src/main/java/org/apache/unomi/api/utils/ParserHelper.java b/api/src/main/java/org/apache/unomi/api/utils/ParserHelper.java
index 57adc7f..a79d2d6 100644
--- a/api/src/main/java/org/apache/unomi/api/utils/ParserHelper.java
+++ b/api/src/main/java/org/apache/unomi/api/utils/ParserHelper.java
@@ -213,7 +213,7 @@ public class ParserHelper {
                         eventTypeIds.add(eventTypeId);
                     }
                 }
-            } else if (condition.getConditionType().getParentCondition() != null) {
+            } else if (condition.getConditionType() != null && condition.getConditionType().getParentCondition() != null) {
                 visitConditions(condition.getConditionType().getParentCondition(), this);
             }
         }
diff --git a/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/schema/GraphQLSchemaProvider.java b/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/schema/GraphQLSchemaProvider.java
index 38358d0..5a02c80 100644
--- a/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/schema/GraphQLSchemaProvider.java
+++ b/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/schema/GraphQLSchemaProvider.java
@@ -359,7 +359,7 @@ public class GraphQLSchemaProvider {
             final GraphQLCodeRegistry.Builder codeRegisterBuilder = graphQLAnnotations.getContainer().getCodeRegistryBuilder();
 
             for (JSONSchema unomiEventType : unomiEventTypes) {
-                final String typeName = UnomiToGraphQLConverter.convertEventType(unomiEventType.getSchemaId());
+                final String typeName = UnomiToGraphQLConverter.convertEventType(unomiEventType.getName());
 
                 final GraphQLObjectType objectType;
                 if (!graphQLAnnotations.getContainer().getTypeRegistry().containsKey(typeName)) {
@@ -652,7 +652,7 @@ public class GraphQLSchemaProvider {
         // now add all unomi defined event types
         final List<JSONSchema> unomiEventTypes = schemaRegistry.getTargetSchemas("events");
         unomiEventTypes.forEach(eventType -> {
-            final String typeName = UnomiToGraphQLConverter.convertEventType(eventType.getSchemaId());
+            final String typeName = UnomiToGraphQLConverter.convertEventType(eventType.getName());
             final GraphQLInputType eventInputType = (GraphQLInputType) getFromTypeRegistry(typeName + "Input");
             if (eventInputType == null) {
                 logger.warn("Couldn't find event input type {}", typeName + "Input, will not add it as a field.");
diff --git a/itests/src/test/java/org/apache/unomi/itests/BaseIT.java b/itests/src/test/java/org/apache/unomi/itests/BaseIT.java
index 17fca68..d39569b 100644
--- a/itests/src/test/java/org/apache/unomi/itests/BaseIT.java
+++ b/itests/src/test/java/org/apache/unomi/itests/BaseIT.java
@@ -24,6 +24,7 @@ import org.apache.unomi.api.conditions.Condition;
 import org.apache.unomi.api.rules.Rule;
 import org.apache.unomi.api.services.DefinitionsService;
 import org.apache.unomi.api.services.RulesService;
+import org.apache.unomi.api.services.SchemaRegistry;
 import org.apache.unomi.lifecycle.BundleWatcher;
 import org.apache.unomi.persistence.spi.CustomObjectMapper;
 import org.apache.unomi.persistence.spi.PersistenceService;
@@ -99,6 +100,10 @@ public abstract class BaseIT {
     @Filter(timeout = 600000)
     protected ConfigurationAdmin configurationAdmin;
 
+    @Inject
+    @Filter(timeout = 600000)
+    protected SchemaRegistry schemaRegistry;
+
     @Before
     public void waitForStartup() throws InterruptedException {
         while (!bundleWatcher.isStartupComplete()) {
@@ -286,6 +291,7 @@ public abstract class BaseIT {
         persistenceService = getService(PersistenceService.class);
         definitionsService = getService(DefinitionsService.class);
         rulesService = getService(RulesService.class);
+        schemaRegistry = getService(SchemaRegistry.class);
     }
 
     public void updateConfiguration(String serviceName, String configPid, String propName, Object propValue) throws InterruptedException, IOException {
diff --git a/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java b/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java
index 469464c..14a6c98 100644
--- a/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java
+++ b/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java
@@ -36,6 +36,7 @@ import org.ops4j.pax.exam.junit.PaxExam;
 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
 import org.ops4j.pax.exam.spi.reactors.PerSuite;
 import org.ops4j.pax.exam.util.Filter;
+import org.osgi.framework.BundleContext;
 
 import javax.inject.Inject;
 import java.io.File;
@@ -91,12 +92,12 @@ public class ContextServletIT extends BaseIT {
 
     @Inject
     @Filter(timeout = 600000)
-    protected SchemaRegistry schemaRegistry;
+    protected BundleContext bundleContext;
 
     private Profile profile;
 
     @Before
-    public void setUp() throws InterruptedException {
+    public void setUp() throws InterruptedException, IOException {
         this.registerEventType(TEST_EVENT_TYPE_SCHEMA);
         this.registerEventType(FLOAT_PROPERTY_EVENT_TYPE_SCHEMA);
 
@@ -131,8 +132,8 @@ public class ContextServletIT extends BaseIT {
         schemaRegistry.unregisterSchema("events", FLOAT_PROPERTY_EVENT_TYPE);
     }
 
-    private void registerEventType(String jsonSchemaFileName) {
-        InputStream jsonSchemaInputStream = this.getClass().getClassLoader().getResourceAsStream("schemas/events/" + jsonSchemaFileName);
+    private void registerEventType(String jsonSchemaFileName) throws IOException {
+        InputStream jsonSchemaInputStream = bundleContext.getBundle().getResource("schemas/events/" + jsonSchemaFileName).openStream();
         schemaRegistry.registerSchema("events", jsonSchemaInputStream);
     }
 
diff --git a/services/src/main/java/org/apache/unomi/services/impl/events/EventServiceImpl.java b/services/src/main/java/org/apache/unomi/services/impl/events/EventServiceImpl.java
index be5ac44..c64bc13 100644
--- a/services/src/main/java/org/apache/unomi/services/impl/events/EventServiceImpl.java
+++ b/services/src/main/java/org/apache/unomi/services/impl/events/EventServiceImpl.java
@@ -140,7 +140,10 @@ public class EventServiceImpl implements EventService {
     }
 
     public boolean isEventValid(Event event) {
-        return this.schemaRegistry.isValid(event, "https://unomi.apache.org/schemas/json/events/" + event.getEventType() + "/1-0-0");
+        if (!this.schemaRegistry.isValid(event, "https://unomi.apache.org/schemas/json/events/" + event.getEventType() + "/1-0-0")) {
+            logger.warn("Event doesn't validate.");
+        };
+        return true;
     }
 
     public String authenticateThirdPartyServer(String key, String ip) {
diff --git a/services/src/main/java/org/apache/unomi/services/impl/schemas/SchemaRegistryImpl.java b/services/src/main/java/org/apache/unomi/services/impl/schemas/SchemaRegistryImpl.java
index 289bdec..7c7f545 100644
--- a/services/src/main/java/org/apache/unomi/services/impl/schemas/SchemaRegistryImpl.java
+++ b/services/src/main/java/org/apache/unomi/services/impl/schemas/SchemaRegistryImpl.java
@@ -38,6 +38,8 @@ import org.slf4j.LoggerFactory;
 import java.io.InputStream;
 import java.net.URL;
 import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
 public class SchemaRegistryImpl implements SchemaRegistry, SynchronousBundleListener {
@@ -61,6 +63,8 @@ public class SchemaRegistryImpl implements SchemaRegistry, SynchronousBundleList
 
     ObjectMapper objectMapper = new ObjectMapper();
 
+    Pattern uriPathPattern = Pattern.compile("/schemas/json(.*)/\\d-\\d-\\d");
+
     public void bundleChanged(BundleEvent event) {
         switch (event.getType()) {
             case BundleEvent.STARTED:
@@ -75,7 +79,9 @@ public class SchemaRegistryImpl implements SchemaRegistry, SynchronousBundleList
     public void init() {
 
         JsonMetaSchema jsonMetaSchema = JsonMetaSchema.builder(URI, JsonMetaSchema.getV201909())
-                .addKeyword(new PropertyTypeKeyword(profileService, this)).build();
+                .addKeyword(new PropertyTypeKeyword(profileService, this))
+                .addKeyword(new NonValidationKeyword("self"))
+                .build();
         jsonSchemaFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909))
                 .addMetaSchema(jsonMetaSchema)
                 .defaultMetaSchemaURI(URI)
@@ -187,7 +193,9 @@ public class SchemaRegistryImpl implements SchemaRegistry, SynchronousBundleList
         }
         Map<String, Object> schemaTree = (Map<String, Object>) objectMapper.treeToValue(jsonSchema.getSchemaNode(), Map.class);
         JSONSchema unomiJsonSchema = new JSONSchema(schemaTree, new JSONTypeFactory(this), this);
-        unomiJsonSchema.setPluginId(bundleId);
+        if (bundleId != null) {
+            unomiJsonSchema.setPluginId(bundleId);
+        }
         unomiJsonSchema.setSchemaId(schemaId);
         unomiJsonSchema.setTarget(target);
         if (jsonSchemas != null) {
@@ -205,7 +213,11 @@ public class SchemaRegistryImpl implements SchemaRegistry, SynchronousBundleList
                 logger.error("Couldn't find bundle for schema {}", uri);
                 return null;
             }
-            String uriPath = uri.getPath().substring("/schemas/json".length());
+            Matcher uriPathMatcher = uriPathPattern.matcher(uri.getPath());
+            String uriPath = uri.getPath();
+            if (uriPathMatcher.matches()) {
+                uriPath = uriPathMatcher.group(1) + ".json";
+            }
             URL schemaURL = bundleContext.getBundle(bundleId).getResource("META-INF/cxs/schemas" + uriPath);
             if (schemaURL != null) {
                 return schemaURL.openStream();
diff --git a/services/src/main/resources/META-INF/cxs/schemas/events/form.json b/services/src/main/resources/META-INF/cxs/schemas/events/form.json
new file mode 100644
index 0000000..e4c1575
--- /dev/null
+++ b/services/src/main/resources/META-INF/cxs/schemas/events/form.json
@@ -0,0 +1,27 @@
+{
+  "$id": "https://unomi.apache.org/schemas/json/events/form/1-0-0",
+  "$schema": "https://json-schema.org/draft/2019-09/schema",
+  "self":{
+    "vendor":"org.apache.unomi",
+    "target" : "events",
+    "name": "form",
+    "format":"jsonschema",
+    "version":"1-0-0"
+  },
+  "title": "FormEvent",
+  "type": "object",
+  "allOf": [{ "$ref": "https://unomi.apache.org/schemas/json/event/1-0-0" }],
+  "properties" : {
+    "properties" : {
+      "type" : "object",
+      "unomiFormMappingLookup" : true,
+      "maxProperties": 50
+    },
+    "source" : {
+      "$ref" : "https://unomi.apache.org/schemas/json/customitems/site/1-0-0"
+    },
+    "target" : {
+      "$ref" : "https://unomi.apache.org/schemas/json/customitems/page/1-0-0"
+    }
+  }
+}
\ No newline at end of file