You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by GitBox <gi...@apache.org> on 2022/03/14 13:58:51 UTC

[GitHub] [unomi] sergehuber commented on a change in pull request #393: UNOMI-555 : add possiblity to store jsonSchema

sergehuber commented on a change in pull request #393:
URL: https://github.com/apache/unomi/pull/393#discussion_r825812853



##########
File path: api/src/main/java/org/apache/unomi/api/schema/UnomiJSONSchema.java
##########
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.unomi.api.schema;
+
+import org.apache.unomi.api.Metadata;
+import org.apache.unomi.api.MetadataItem;
+
+/**
+ * Object which represents a JSON schema stored in Elasticsearch

Review comment:
       Could we say instead `stored in the persistence service` ? I prefer we don't have dependencies to ES in the API.

##########
File path: api/src/main/java/org/apache/unomi/api/services/SchemaRegistry.java
##########
@@ -17,21 +17,93 @@
 
 package org.apache.unomi.api.services;
 
+import org.apache.unomi.api.Metadata;
+import org.apache.unomi.api.PartialList;
 import org.apache.unomi.api.schema.json.JSONSchema;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.List;
 
+/**
+ *

Review comment:
       Description is missing, please add it.

##########
File path: itests/src/test/java/org/apache/unomi/itests/JSONSchemaIT.java
##########
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package org.apache.unomi.itests;
+
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.entity.ContentType;
+import org.apache.unomi.api.Metadata;
+import org.apache.unomi.api.schema.UnomiJSONSchema;
+import org.apache.unomi.api.services.SchemaRegistry;
+import org.apache.unomi.persistence.spi.PersistenceService;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+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 javax.inject.Inject;
+import java.io.IOException;
+import java.util.Base64;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Class to tests the JSON schema features
+ */
+
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerSuite.class)
+public class JSONSchemaIT extends BaseIT {
+    private final static String JSONSCHEMA_URL = "/cxs/jsonSchema";
+    private static final int DEFAULT_TRYING_TIMEOUT = 2000;
+    private static final int DEFAULT_TRYING_TRIES = 30;
+
+    @Inject
+    @Filter(timeout = 600000)
+    protected SchemaRegistry schemaRegistry;
+
+    @Inject
+    @Filter(timeout = 600000)
+    protected PersistenceService persistenceService;
+
+    @Before
+    public void setUp() throws InterruptedException {
+        keepTrying("Couldn't find json schema endpoint",
+                () -> get(JSONSCHEMA_URL, List.class), Objects::nonNull,
+                DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
+    }
+
+    @After
+    public void tearDown() {
+        schemaRegistry.deleteSchema("https://unomi.apache.org/schemas/json/events/test-event-type/1-0-0");
+    }
+
+    @Test
+    public void testGetJsonSchemasMetadatas() throws InterruptedException {
+        List jsonSchemas = get(JSONSCHEMA_URL, List.class);
+        assertTrue("JSON schema list should be empty", jsonSchemas.isEmpty());
+
+        post(JSONSCHEMA_URL, "schemas/events/test-event-type.json", ContentType.create("text/plain"));
+
+        refreshPersistence();
+        Thread.sleep(2000); //Making sure jsonSchema is updated in DB
+        jsonSchemas = get(JSONSCHEMA_URL, List.class);
+        assertFalse("JSON schema list should not be empty", jsonSchemas.isEmpty());
+        assertEquals("JSON schema list should not be empty", 1, jsonSchemas.size());
+    }
+
+    @Test
+    public void testSaveNewValidJSONSchema() throws InterruptedException {
+
+        assertTrue("JSON schema list should be empty", persistenceService.getAllItems(UnomiJSONSchema.class).isEmpty());
+
+        CloseableHttpResponse response = post(JSONSCHEMA_URL, "schemas/events/test-event-type.json", ContentType.create("text/plain"));
+
+        assertEquals("Invalid response code", 200, response.getStatusLine().getStatusCode());
+        refreshPersistence();
+        Thread.sleep(2000); //Making sure jsonSchema is updated in DB
+        assertFalse("JSON schema list should not be empty", persistenceService.getAllItems(UnomiJSONSchema.class).isEmpty());
+    }
+
+    @Test
+    public void testDeleteJSONSchema() throws IOException, InterruptedException {
+        assertTrue("JSON schema list should be empty", persistenceService.getAllItems(UnomiJSONSchema.class).isEmpty());
+
+        post(JSONSCHEMA_URL, "schemas/events/test-event-type.json", ContentType.create("text/plain"));
+
+        refreshPersistence();
+        Thread.sleep(2000); //Making sure jsonSchema is updated in DB

Review comment:
       More sleeping :)

##########
File path: itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java
##########
@@ -128,13 +139,21 @@ public void tearDown() {
         profileService.delete(profile.getItemId(), false);
         segmentService.removeSegmentDefinition(SEGMENT_ID, false);
 
-        schemaRegistry.unregisterSchema("events", TEST_EVENT_TYPE);
-        schemaRegistry.unregisterSchema("events", FLOAT_PROPERTY_EVENT_TYPE);
+        String encodedString = Base64.getEncoder()
+                .encodeToString("https://unomi.apache.org/schemas/json/events/test-event-type/1-0-0".getBytes());
+        delete(JSONSCHEMA_URL + "/" + encodedString);
+
+        encodedString = Base64.getEncoder()
+                .encodeToString("https://unomi.apache.org/schemas/json/events/float-property-type/1-0-0".getBytes());
+        delete(JSONSCHEMA_URL + "/" + encodedString);
+
+        encodedString = Base64.getEncoder()
+                .encodeToString("https://unomi.apache.org/schemas/json/events/float-property-type/1-0-0".getBytes());
+        delete(JSONSCHEMA_URL + "/" + encodedString);
     }
 
-    private void registerEventType(String jsonSchemaFileName) throws IOException {
-        InputStream jsonSchemaInputStream = bundleContext.getBundle().getResource("schemas/events/" + jsonSchemaFileName).openStream();
-        schemaRegistry.registerSchema("events", jsonSchemaInputStream);
+    private void registerEventType(String jsonSchemaFileName) {
+        post(JSONSCHEMA_URL, "schemas/events/" + jsonSchemaFileName, ContentType.create("text/plain"));

Review comment:
       Also while we are at it I think we should replace all the ContentType.create calls with the constants: ContentType.APPLICATION_JSON and ContentType.TEXT_PLAIN

##########
File path: itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java
##########
@@ -128,13 +139,21 @@ public void tearDown() {
         profileService.delete(profile.getItemId(), false);
         segmentService.removeSegmentDefinition(SEGMENT_ID, false);
 
-        schemaRegistry.unregisterSchema("events", TEST_EVENT_TYPE);
-        schemaRegistry.unregisterSchema("events", FLOAT_PROPERTY_EVENT_TYPE);
+        String encodedString = Base64.getEncoder()
+                .encodeToString("https://unomi.apache.org/schemas/json/events/test-event-type/1-0-0".getBytes());
+        delete(JSONSCHEMA_URL + "/" + encodedString);
+
+        encodedString = Base64.getEncoder()
+                .encodeToString("https://unomi.apache.org/schemas/json/events/float-property-type/1-0-0".getBytes());
+        delete(JSONSCHEMA_URL + "/" + encodedString);
+
+        encodedString = Base64.getEncoder()
+                .encodeToString("https://unomi.apache.org/schemas/json/events/float-property-type/1-0-0".getBytes());
+        delete(JSONSCHEMA_URL + "/" + encodedString);
     }
 
-    private void registerEventType(String jsonSchemaFileName) throws IOException {
-        InputStream jsonSchemaInputStream = bundleContext.getBundle().getResource("schemas/events/" + jsonSchemaFileName).openStream();
-        schemaRegistry.registerSchema("events", jsonSchemaInputStream);
+    private void registerEventType(String jsonSchemaFileName) {
+        post(JSONSCHEMA_URL, "schemas/events/" + jsonSchemaFileName, ContentType.create("text/plain"));

Review comment:
       I understand why you use text/plain but I'm wondering if it couldn't work with JSON ? I find It weird to send JSON in text/plain

##########
File path: itests/src/test/java/org/apache/unomi/itests/JSONSchemaIT.java
##########
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package org.apache.unomi.itests;
+
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.entity.ContentType;
+import org.apache.unomi.api.Metadata;
+import org.apache.unomi.api.schema.UnomiJSONSchema;
+import org.apache.unomi.api.services.SchemaRegistry;
+import org.apache.unomi.persistence.spi.PersistenceService;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+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 javax.inject.Inject;
+import java.io.IOException;
+import java.util.Base64;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Class to tests the JSON schema features
+ */
+
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerSuite.class)
+public class JSONSchemaIT extends BaseIT {
+    private final static String JSONSCHEMA_URL = "/cxs/jsonSchema";
+    private static final int DEFAULT_TRYING_TIMEOUT = 2000;
+    private static final int DEFAULT_TRYING_TRIES = 30;
+
+    @Inject
+    @Filter(timeout = 600000)
+    protected SchemaRegistry schemaRegistry;
+
+    @Inject
+    @Filter(timeout = 600000)
+    protected PersistenceService persistenceService;
+
+    @Before
+    public void setUp() throws InterruptedException {
+        keepTrying("Couldn't find json schema endpoint",
+                () -> get(JSONSCHEMA_URL, List.class), Objects::nonNull,
+                DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
+    }
+
+    @After
+    public void tearDown() {
+        schemaRegistry.deleteSchema("https://unomi.apache.org/schemas/json/events/test-event-type/1-0-0");
+    }
+
+    @Test
+    public void testGetJsonSchemasMetadatas() throws InterruptedException {
+        List jsonSchemas = get(JSONSCHEMA_URL, List.class);
+        assertTrue("JSON schema list should be empty", jsonSchemas.isEmpty());
+
+        post(JSONSCHEMA_URL, "schemas/events/test-event-type.json", ContentType.create("text/plain"));
+
+        refreshPersistence();
+        Thread.sleep(2000); //Making sure jsonSchema is updated in DB
+        jsonSchemas = get(JSONSCHEMA_URL, List.class);
+        assertFalse("JSON schema list should not be empty", jsonSchemas.isEmpty());
+        assertEquals("JSON schema list should not be empty", 1, jsonSchemas.size());
+    }
+
+    @Test
+    public void testSaveNewValidJSONSchema() throws InterruptedException {
+
+        assertTrue("JSON schema list should be empty", persistenceService.getAllItems(UnomiJSONSchema.class).isEmpty());
+
+        CloseableHttpResponse response = post(JSONSCHEMA_URL, "schemas/events/test-event-type.json", ContentType.create("text/plain"));
+
+        assertEquals("Invalid response code", 200, response.getStatusLine().getStatusCode());
+        refreshPersistence();
+        Thread.sleep(2000); //Making sure jsonSchema is updated in DB

Review comment:
       Same here :)

##########
File path: itests/src/test/java/org/apache/unomi/itests/JSONSchemaIT.java
##########
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package org.apache.unomi.itests;
+
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.entity.ContentType;
+import org.apache.unomi.api.Metadata;
+import org.apache.unomi.api.schema.UnomiJSONSchema;
+import org.apache.unomi.api.services.SchemaRegistry;
+import org.apache.unomi.persistence.spi.PersistenceService;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+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 javax.inject.Inject;
+import java.io.IOException;
+import java.util.Base64;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Class to tests the JSON schema features
+ */
+
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerSuite.class)
+public class JSONSchemaIT extends BaseIT {
+    private final static String JSONSCHEMA_URL = "/cxs/jsonSchema";
+    private static final int DEFAULT_TRYING_TIMEOUT = 2000;
+    private static final int DEFAULT_TRYING_TRIES = 30;
+
+    @Inject
+    @Filter(timeout = 600000)
+    protected SchemaRegistry schemaRegistry;
+
+    @Inject
+    @Filter(timeout = 600000)
+    protected PersistenceService persistenceService;
+
+    @Before
+    public void setUp() throws InterruptedException {
+        keepTrying("Couldn't find json schema endpoint",
+                () -> get(JSONSCHEMA_URL, List.class), Objects::nonNull,
+                DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
+    }
+
+    @After
+    public void tearDown() {
+        schemaRegistry.deleteSchema("https://unomi.apache.org/schemas/json/events/test-event-type/1-0-0");
+    }
+
+    @Test
+    public void testGetJsonSchemasMetadatas() throws InterruptedException {
+        List jsonSchemas = get(JSONSCHEMA_URL, List.class);
+        assertTrue("JSON schema list should be empty", jsonSchemas.isEmpty());
+
+        post(JSONSCHEMA_URL, "schemas/events/test-event-type.json", ContentType.create("text/plain"));
+
+        refreshPersistence();
+        Thread.sleep(2000); //Making sure jsonSchema is updated in DB

Review comment:
       Please don't use Thread.sleep here but replace with a keepTrying. We are trying to remove sleeps not add more :)

##########
File path: itests/src/test/java/org/apache/unomi/itests/JSONSchemaIT.java
##########
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package org.apache.unomi.itests;
+
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.entity.ContentType;
+import org.apache.unomi.api.Metadata;
+import org.apache.unomi.api.schema.UnomiJSONSchema;
+import org.apache.unomi.api.services.SchemaRegistry;
+import org.apache.unomi.persistence.spi.PersistenceService;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+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 javax.inject.Inject;
+import java.io.IOException;
+import java.util.Base64;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Class to tests the JSON schema features
+ */
+
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerSuite.class)
+public class JSONSchemaIT extends BaseIT {
+    private final static String JSONSCHEMA_URL = "/cxs/jsonSchema";
+    private static final int DEFAULT_TRYING_TIMEOUT = 2000;
+    private static final int DEFAULT_TRYING_TRIES = 30;
+
+    @Inject
+    @Filter(timeout = 600000)
+    protected SchemaRegistry schemaRegistry;
+
+    @Inject
+    @Filter(timeout = 600000)
+    protected PersistenceService persistenceService;
+
+    @Before
+    public void setUp() throws InterruptedException {
+        keepTrying("Couldn't find json schema endpoint",
+                () -> get(JSONSCHEMA_URL, List.class), Objects::nonNull,
+                DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
+    }
+
+    @After
+    public void tearDown() {
+        schemaRegistry.deleteSchema("https://unomi.apache.org/schemas/json/events/test-event-type/1-0-0");
+    }
+
+    @Test
+    public void testGetJsonSchemasMetadatas() throws InterruptedException {
+        List jsonSchemas = get(JSONSCHEMA_URL, List.class);
+        assertTrue("JSON schema list should be empty", jsonSchemas.isEmpty());
+
+        post(JSONSCHEMA_URL, "schemas/events/test-event-type.json", ContentType.create("text/plain"));
+
+        refreshPersistence();
+        Thread.sleep(2000); //Making sure jsonSchema is updated in DB
+        jsonSchemas = get(JSONSCHEMA_URL, List.class);
+        assertFalse("JSON schema list should not be empty", jsonSchemas.isEmpty());
+        assertEquals("JSON schema list should not be empty", 1, jsonSchemas.size());
+    }
+
+    @Test
+    public void testSaveNewValidJSONSchema() throws InterruptedException {
+
+        assertTrue("JSON schema list should be empty", persistenceService.getAllItems(UnomiJSONSchema.class).isEmpty());
+
+        CloseableHttpResponse response = post(JSONSCHEMA_URL, "schemas/events/test-event-type.json", ContentType.create("text/plain"));
+
+        assertEquals("Invalid response code", 200, response.getStatusLine().getStatusCode());
+        refreshPersistence();
+        Thread.sleep(2000); //Making sure jsonSchema is updated in DB
+        assertFalse("JSON schema list should not be empty", persistenceService.getAllItems(UnomiJSONSchema.class).isEmpty());
+    }
+
+    @Test
+    public void testDeleteJSONSchema() throws IOException, InterruptedException {
+        assertTrue("JSON schema list should be empty", persistenceService.getAllItems(UnomiJSONSchema.class).isEmpty());
+
+        post(JSONSCHEMA_URL, "schemas/events/test-event-type.json", ContentType.create("text/plain"));
+
+        refreshPersistence();
+        Thread.sleep(2000); //Making sure jsonSchema is updated in DB
+
+        String encodedString = Base64.getEncoder()
+                .encodeToString("https://unomi.apache.org/schemas/json/events/test-event-type/1-0-0".getBytes());
+        CloseableHttpResponse response = delete(JSONSCHEMA_URL + "/" + encodedString);
+        assertEquals("Invalid response code", 204, response.getStatusLine().getStatusCode());
+
+        refreshPersistence();
+        Thread.sleep(2000); //Making sure jsonSchema is updated in DB

Review comment:
       And here




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@unomi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org