You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by jk...@apache.org on 2022/05/31 13:21:06 UTC

[unomi] branch master updated: UNOMI-580 : add endpoint to get schema + clean (#429)

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

jkevan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/unomi.git


The following commit(s) were added to refs/heads/master by this push:
     new 582867561 UNOMI-580 : add endpoint to get schema + clean (#429)
582867561 is described below

commit 582867561a799cee7858a7abf52cf1c4a75c7df0
Author: jsinovassin <58...@users.noreply.github.com>
AuthorDate: Tue May 31 15:21:02 2022 +0200

    UNOMI-580 : add endpoint to get schema + clean (#429)
    
    * UNOMI-580 : add endpoint to get schema + clean
    
    * feedbacks
    
    * getSchema return null instead of empty
---
 .../unomi/schema/rest/JsonSchemaEndPoint.java      |  39 ++++--
 .../org/apache/unomi/itests/ContextServletIT.java  |   2 -
 .../java/org/apache/unomi/itests/JSONSchemaIT.java | 154 ++++++++++++---------
 .../main/resources/etc/custom.system.properties    |   2 -
 4 files changed, 121 insertions(+), 76 deletions(-)

diff --git a/extensions/json-schema/rest/src/main/java/org/apache/unomi/schema/rest/JsonSchemaEndPoint.java b/extensions/json-schema/rest/src/main/java/org/apache/unomi/schema/rest/JsonSchemaEndPoint.java
index 6c9fb021d..bb178be0d 100644
--- a/extensions/json-schema/rest/src/main/java/org/apache/unomi/schema/rest/JsonSchemaEndPoint.java
+++ b/extensions/json-schema/rest/src/main/java/org/apache/unomi/schema/rest/JsonSchemaEndPoint.java
@@ -19,6 +19,7 @@ package org.apache.unomi.schema.rest;
 
 import org.apache.cxf.rs.security.cors.CrossOriginResourceSharing;
 import org.apache.unomi.rest.exception.InvalidRequestException;
+import org.apache.unomi.schema.api.JsonSchemaWrapper;
 import org.apache.unomi.schema.api.SchemaService;
 import org.osgi.service.component.annotations.Component;
 import org.osgi.service.component.annotations.Reference;
@@ -27,10 +28,13 @@ import org.slf4j.LoggerFactory;
 
 import javax.jws.WebMethod;
 import javax.jws.WebService;
-import javax.ws.rs.*;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
-import java.util.Base64;
 import java.util.Set;
 
 @WebService
@@ -65,6 +69,22 @@ public class JsonSchemaEndPoint {
         return schemaService.getInstalledJsonSchemaIds();
     }
 
+    /**
+     * Get a schema by it's id
+     *
+     * @param id of the schema
+     * @return Json schema matching the id
+     */
+    @POST
+    @Path("/query")
+    public String getSchema(String id) {
+        JsonSchemaWrapper schema = schemaService.getSchema(id);
+        if (schema != null) {
+            return schema.getSchema().replace("\n", "");
+        }
+        return null;
+    }
+
     /**
      * Save a JSON schema
      *
@@ -73,7 +93,7 @@ public class JsonSchemaEndPoint {
      */
     @POST
     @Path("/")
-    @Consumes({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON})
+    @Consumes({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON })
     @Produces(MediaType.APPLICATION_JSON)
     public Response save(String jsonSchema) {
         try {
@@ -85,14 +105,13 @@ public class JsonSchemaEndPoint {
     }
 
     /**
-     * Deletes a JSON schema.
-     * The id is a Base64 id as the id have is basically an URL
+     * Deletes a JSON schema by it's id.
      *
-     * @param base64JsonSchemaId the identifier of the JSON schema that we want to delete
+     * @param id the identifier of the JSON schema that we want to delete
      */
-    @DELETE
-    @Path("/{base64JsonSchemaId}")
-    public void remove(@PathParam("base64JsonSchemaId") String base64JsonSchemaId) {
-        schemaService.deleteSchema(new String(Base64.getDecoder().decode(base64JsonSchemaId)));
+    @POST
+    @Path("/delete")
+    public boolean remove(String id) {
+        return schemaService.deleteSchema(id);
     }
 }
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 90396ea14..fe1d1c91f 100644
--- a/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java
+++ b/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java
@@ -55,7 +55,6 @@ import java.time.ZoneId;
 import java.time.ZoneOffset;
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Base64;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
@@ -77,7 +76,6 @@ import static org.junit.Assert.assertTrue;
 @ExamReactorStrategy(PerSuite.class)
 public class ContextServletIT extends BaseIT {
     private final static String CONTEXT_URL = "/cxs/context.json";
-    private final static String JSONSCHEMA_URL = "/cxs/jsonSchema";
 
     private final static String THIRD_PARTY_HEADER_NAME = "X-Unomi-Peer";
     private final static String TEST_EVENT_TYPE = "testEventType";
diff --git a/itests/src/test/java/org/apache/unomi/itests/JSONSchemaIT.java b/itests/src/test/java/org/apache/unomi/itests/JSONSchemaIT.java
index 4cc9f15fe..2600a8912 100644
--- a/itests/src/test/java/org/apache/unomi/itests/JSONSchemaIT.java
+++ b/itests/src/test/java/org/apache/unomi/itests/JSONSchemaIT.java
@@ -18,7 +18,10 @@
 package org.apache.unomi.itests;
 
 import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
 import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.util.EntityUtils;
 import org.apache.unomi.api.Event;
 import org.apache.unomi.schema.api.JsonSchemaWrapper;
 import org.apache.unomi.schema.api.SchemaService;
@@ -30,14 +33,18 @@ 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.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import javax.inject.Inject;
 import java.io.IOException;
-import java.util.Base64;
 import java.util.List;
 import java.util.Objects;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 
 /**
  * Class to tests the JSON schema features
@@ -46,6 +53,7 @@ import static org.junit.Assert.*;
 @RunWith(PaxExam.class)
 @ExamReactorStrategy(PerSuite.class)
 public class JSONSchemaIT extends BaseIT {
+    private final static Logger LOGGER = LoggerFactory.getLogger(JSONSchemaIT.class);
     private final static String JSONSCHEMA_URL = "/cxs/jsonSchema";
     private static final int DEFAULT_TRYING_TIMEOUT = 2000;
     private static final int DEFAULT_TRYING_TRIES = 30;
@@ -74,54 +82,57 @@ public class JSONSchemaIT extends BaseIT {
     @Test
     public void testValidation_SaveDeleteSchemas() throws InterruptedException, IOException {
         // check that event is not valid at first
-        assertFalse(schemaService.isEventValid(resourceAsString("schemas/event-dummy-valid.json"), "dummy"));
+        assertFalse(schemaService
+                .isEventValid(resourceAsString("schemas/event-dummy-valid.json"), "dummy"));
 
         // Push schemas
         schemaService.saveSchema(resourceAsString("schemas/schema-dummy.json"));
         schemaService.saveSchema(resourceAsString("schemas/schema-dummy-properties.json"));
-        keepTrying("Event should be valid",
-                () -> schemaService.isEventValid(resourceAsString("schemas/event-dummy-valid.json"), "dummy"),
-                isValid -> isValid,
-                DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
+        keepTrying("Event should be valid", () -> schemaService
+                        .isEventValid(resourceAsString("schemas/event-dummy-valid.json"), "dummy"),
+                isValid -> isValid, DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
 
         // Test multiple invalid event:
         // unevaluated property at root:
-        assertFalse(schemaService.isEventValid(resourceAsString("schemas/event-dummy-invalid-1.json"), "dummy"));
+        assertFalse(schemaService.isEventValid(resourceAsString("schemas/event-dummy-invalid-1.json"),
+                "dummy"));
         // unevaluated property in properties:
-        assertFalse(schemaService.isEventValid(resourceAsString("schemas/event-dummy-invalid-2.json"), "dummy"));
+        assertFalse(schemaService.isEventValid(resourceAsString("schemas/event-dummy-invalid-2.json"),
+                "dummy"));
         // bad type number but should be string:
-        assertFalse(schemaService.isEventValid(resourceAsString("schemas/event-dummy-invalid-3.json"), "dummy"));
+        assertFalse(schemaService.isEventValid(resourceAsString("schemas/event-dummy-invalid-3.json"),
+                "dummy"));
 
         // remove one of the schema:
         assertTrue(schemaService.deleteSchema("https://vendor.test.com/schemas/json/events/dummy/properties/1-0-0"));
-        keepTrying("Event should be invalid since of the schema have been deleted",
-                () -> schemaService.isEventValid(resourceAsString("schemas/event-dummy-valid.json"), "dummy"),
-                isValid -> !isValid,
-                DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
+        keepTrying("Event should be invalid since of the schema have been deleted", () -> schemaService
+                        .isEventValid(resourceAsString("schemas/event-dummy-valid.json"), "dummy"),
+                isValid -> !isValid, DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
     }
 
     @Test
     public void testValidation_UpdateSchema() throws InterruptedException, IOException {
         // check that event is not valid at first
-        assertFalse(schemaService.isEventValid(resourceAsString("schemas/event-dummy-valid.json"), "dummy"));
+        assertFalse(schemaService
+                .isEventValid(resourceAsString("schemas/event-dummy-valid.json"), "dummy"));
 
         // Push schemas
         schemaService.saveSchema(resourceAsString("schemas/schema-dummy.json"));
         schemaService.saveSchema(resourceAsString("schemas/schema-dummy-properties.json"));
-        keepTrying("Event should be valid",
-                () -> schemaService.isEventValid(resourceAsString("schemas/event-dummy-valid.json"), "dummy"),
-                isValid -> isValid,
-                DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
+        keepTrying("Event should be valid", () -> schemaService
+                        .isEventValid(resourceAsString("schemas/event-dummy-valid.json"), "dummy"),
+                isValid -> isValid, DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
 
         // Test the invalid event, that use the new prop "invalidPropName" in properties:
-        assertFalse(schemaService.isEventValid(resourceAsString("schemas/event-dummy-invalid-2.json"), "dummy"));
+        assertFalse(schemaService.isEventValid(resourceAsString("schemas/event-dummy-invalid-2.json"),
+                "dummy"));
 
         // update the schema to allow "invalidPropName":
         schemaService.saveSchema(resourceAsString("schemas/schema-dummy-properties-updated.json"));
-        keepTrying("Event should be valid since of the schema have been updated",
-                () -> schemaService.isEventValid(resourceAsString("schemas/event-dummy-invalid-2.json"), "dummy"),
-                isValid -> isValid,
-                DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
+        keepTrying("Event should be valid since of the schema have been updated", () -> schemaService
+                        .isEventValid(resourceAsString("schemas/event-dummy-invalid-2.json"),
+                                "dummy"), isValid -> isValid, DEFAULT_TRYING_TIMEOUT,
+                DEFAULT_TRYING_TRIES);
     }
 
     @Test
@@ -129,28 +140,26 @@ public class JSONSchemaIT extends BaseIT {
         // Push base schemas
         schemaService.saveSchema(resourceAsString("schemas/schema-dummy.json"));
         schemaService.saveSchema(resourceAsString("schemas/schema-dummy-properties.json"));
-        keepTrying("Event should be valid",
-                () -> schemaService.isEventValid(resourceAsString("schemas/event-dummy-valid.json"), "dummy"),
-                isValid -> isValid,
-                DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
+        keepTrying("Event should be valid", () -> schemaService
+                        .isEventValid(resourceAsString("schemas/event-dummy-valid.json"), "dummy"),
+                isValid -> isValid, DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
 
         // check that extended event is not valid at first
-        assertFalse(schemaService.isEventValid(resourceAsString("schemas/event-dummy-extended.json"), "dummy"));
+        assertFalse(schemaService.isEventValid(resourceAsString("schemas/event-dummy-extended.json"),
+                "dummy"));
 
         // register both extensions (for root event and the properties level)
         schemaService.saveSchema(resourceAsString("schemas/schema-dummy-extension.json"));
         schemaService.saveSchema(resourceAsString("schemas/schema-dummy-properties-extension.json"));
-        keepTrying("Extended event should be valid since of the extensions have been deployed",
-                () -> schemaService.isEventValid(resourceAsString("schemas/event-dummy-extended.json"), "dummy"),
-                isValid -> isValid,
-                DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
+        keepTrying("Extended event should be valid since of the extensions have been deployed", () -> schemaService
+                        .isEventValid(resourceAsString("schemas/event-dummy-extended.json"), "dummy"),
+                isValid -> isValid, DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
 
         // delete one of the extension
         schemaService.deleteSchema("https://vendor.test.com/schemas/json/events/dummy/properties/extension/1-0-0");
-        keepTrying("Extended event should be invalid again, one necessary extension have been removed",
-                () -> schemaService.isEventValid(resourceAsString("schemas/event-dummy-extended.json"), "dummy"),
-                isValid -> !isValid,
-                DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
+        keepTrying("Extended event should be invalid again, one necessary extension have been removed", () -> schemaService
+                        .isEventValid(resourceAsString("schemas/event-dummy-extended.json"), "dummy"),
+                isValid -> !isValid, DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
     }
 
     @Test
@@ -158,61 +167,82 @@ public class JSONSchemaIT extends BaseIT {
         // Push base schemas
         schemaService.saveSchema(resourceAsString("schemas/schema-dummy.json"));
         schemaService.saveSchema(resourceAsString("schemas/schema-dummy-properties.json"));
-        keepTrying("Event should be valid",
-                () -> schemaService.isEventValid(resourceAsString("schemas/event-dummy-valid.json"), "dummy"),
-                isValid -> isValid,
-                DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
+        keepTrying("Event should be valid", () -> schemaService
+                        .isEventValid(resourceAsString("schemas/event-dummy-valid.json"), "dummy"),
+                isValid -> isValid, DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
 
         // check that extended event is not valid at first
-        assertFalse(schemaService.isEventValid(resourceAsString("schemas/event-dummy-extended.json"), "dummy"));
+        assertFalse(schemaService.isEventValid(resourceAsString("schemas/event-dummy-extended.json"),
+                "dummy"));
 
         // register both extensions (for root event and the properties level)
         schemaService.saveSchema(resourceAsString("schemas/schema-dummy-extension.json"));
         schemaService.saveSchema(resourceAsString("schemas/schema-dummy-properties-extension.json"));
-        keepTrying("Extended event should be valid since of the extensions have been deployed",
-                () -> schemaService.isEventValid(resourceAsString("schemas/event-dummy-extended.json"), "dummy"),
-                isValid -> isValid,
-                DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
+        keepTrying("Extended event should be valid since of the extensions have been deployed", () -> schemaService
+                        .isEventValid(resourceAsString("schemas/event-dummy-extended.json"), "dummy"),
+                isValid -> isValid, DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
 
         // check that extended event 2 is not valid due to usage of unevaluatedProperty not bring by schemas or extensions
-        assertFalse(schemaService.isEventValid(resourceAsString("schemas/event-dummy-extended-2.json"), "dummy"));
+        assertFalse(schemaService.isEventValid(resourceAsString("schemas/event-dummy-extended-2.json"),
+                "dummy"));
 
         // Update extensions to allow the extended event 2
         schemaService.saveSchema(resourceAsString("schemas/schema-dummy-properties-extension-2.json"));
-        keepTrying("Extended event 2 should be valid since of the extensions have been updated",
-                () -> schemaService.isEventValid(resourceAsString("schemas/event-dummy-extended-2.json"), "dummy"),
-                isValid -> isValid,
-                DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
+        keepTrying("Extended event 2 should be valid since of the extensions have been updated", () -> schemaService
+                        .isEventValid(resourceAsString("schemas/event-dummy-extended-2.json"),
+                                "dummy"), isValid -> isValid, DEFAULT_TRYING_TIMEOUT,
+                DEFAULT_TRYING_TRIES);
     }
 
     @Test
-    public void testEndPoint_GetInstalledJsonSchemas() throws InterruptedException {
+    public void testEndPoint_GetInstalledJsonSchemas() {
         List<String> jsonSchemas = get(JSONSCHEMA_URL, List.class);
         assertFalse("JSON schema list should not be empty, it should contain predefined Unomi schemas", jsonSchemas.isEmpty());
     }
 
     @Test
-    public void testEndPoint_SaveDelete() throws InterruptedException, IOException {
+    public void testEndPoint_GetJsonSchemasById() throws Exception {
+        // Push base schemas
+        schemaService.saveSchema(resourceAsString("schemas/schema-dummy.json"));
+        schemaService.saveSchema(resourceAsString("schemas/schema-dummy-properties.json"));
+
+        final String schemaId = "https://vendor.test.com/schemas/json/events/dummy/1-0-0";
+        final HttpPost request = new HttpPost(getFullUrl(JSONSCHEMA_URL + "/query"));
+
+        request.setEntity(new StringEntity(schemaId));
+
+        keepTrying("Should return a schema when calling the endpoint", () -> {
+            try (CloseableHttpResponse response = executeHttpRequest(request)) {
+                return EntityUtils.toString(response.getEntity());
+            } catch (IOException e) {
+                LOGGER.error("Failed to get the json schema with the id: {}", schemaId);
+            }
+            return "";
+        }, entity -> entity.contains("DummyEvent"), DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
+    }
+
+    @Test
+    public void testEndPoint_SaveDelete() throws Exception {
         assertNull(schemaService.getSchema("https://vendor.test.com/schemas/json/events/dummy/1-0-0"));
 
         // Post schema using REST call
-        try(CloseableHttpResponse response = post(JSONSCHEMA_URL, "schemas/schema-dummy.json", ContentType.TEXT_PLAIN)) {
+        try (CloseableHttpResponse response = post(JSONSCHEMA_URL, "schemas/schema-dummy.json", ContentType.TEXT_PLAIN)) {
             assertEquals("Invalid response code", 200, response.getStatusLine().getStatusCode());
         }
 
         // See schema is available
-        keepTrying("Schema should have been created", () -> schemaService.getSchema("https://vendor.test.com/schemas/json/events/dummy/1-0-0"),
-                Objects::nonNull, DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
+        keepTrying("Schema should have been created",
+                () -> schemaService.getSchema("https://vendor.test.com/schemas/json/events/dummy/1-0-0"), Objects::nonNull,
+                DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
 
         // Delete Schema using REST call
-        String encodedString = Base64.getEncoder()
-                .encodeToString("https://vendor.test.com/schemas/json/events/dummy/1-0-0".getBytes());
-        CloseableHttpResponse response = delete(JSONSCHEMA_URL + "/" + encodedString);
-        assertEquals("Invalid response code", 204, response.getStatusLine().getStatusCode());
+        final HttpPost request = new HttpPost(getFullUrl(JSONSCHEMA_URL + "/delete"));
+        request.setEntity(new StringEntity("https://vendor.test.com/schemas/json/events/dummy/1-0-0"));
+        CloseableHttpResponse response = executeHttpRequest(request);
+        assertEquals("Invalid response code", 200, response.getStatusLine().getStatusCode());
 
         waitForNullValue("Schema should have been deleted",
-                () -> schemaService.getSchema("https://vendor.test.com/schemas/json/events/dummy/1-0-0"),
-                DEFAULT_TRYING_TIMEOUT,
+                () -> schemaService.getSchema("https://vendor.test.com/schemas/json/events/dummy/1-0-0"), DEFAULT_TRYING_TIMEOUT,
                 DEFAULT_TRYING_TRIES);
     }
 
diff --git a/package/src/main/resources/etc/custom.system.properties b/package/src/main/resources/etc/custom.system.properties
index de6f4df86..30e993495 100644
--- a/package/src/main/resources/etc/custom.system.properties
+++ b/package/src/main/resources/etc/custom.system.properties
@@ -185,8 +185,6 @@ org.apache.unomi.rules.statistics.refresh.interval=${env:UNOMI_RULES_STATISTICS_
 org.apache.unomi.rules.optimizationActivated=${env:UNOMI_RULES_OPTIMIZATION_ACTIVATED:-true}
 # The number of threads to compose the pool size of the scheduler.
 org.apache.unomi.scheduler.thread.poolSize=${env:UNOMI_SCHEDULER_THREAD_POOL_SIZE:-5}
-# When performing json schema updates,
-services.json.schema.refresh.interval=${env:UNOMI_JSON_SCHEMA_REFRESH_INTERVAL:-1000}
 
 #######################################################################################################################
 ## Third Party server settings                                                                                       ##