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

jsinovassinnaik pushed a commit to branch UNOMI-598-validate-events-with-scope
in repository https://gitbox.apache.org/repos/asf/unomi.git

commit e40feaeb59169a41681f9cf1d494a2cd5d6d0bfc
Author: jsinovassin <[email protected]>
AuthorDate: Tue Jun 28 17:39:39 2022 +0200

    feedbacks
---
 .../apache/unomi/schema/keyword/ScopeKeyword.java  |  2 +-
 .../unomi/schema/keyword/ScopeValidator.java       |  2 +-
 .../META-INF/cxs/schemas/events/event.json         |  2 +-
 .../cxs/schemas/items/consent/consent.json         |  2 +-
 .../resources/META-INF/cxs/schemas/items/item.json |  2 +-
 .../test/java/org/apache/unomi/itests/BasicIT.java | 27 ++++++++++++++++++-
 .../org/apache/unomi/itests/ContextServletIT.java  | 31 +++++++++++++++++-----
 .../org/apache/unomi/itests/InputValidationIT.java | 25 +++++++++++++++++
 .../resources/schemas/event-flattened-valid.json   | 20 +++++++-------
 .../resources/schemas/schema-dummy-properties.json |  2 +-
 10 files changed, 92 insertions(+), 23 deletions(-)

diff --git 
a/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/keyword/ScopeKeyword.java
 
b/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/keyword/ScopeKeyword.java
index ebe15ce30..471a475ab 100644
--- 
a/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/keyword/ScopeKeyword.java
+++ 
b/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/keyword/ScopeKeyword.java
@@ -28,7 +28,7 @@ public class ScopeKeyword extends AbstractKeyword {
     private ScopeService scopeService;
 
     public ScopeKeyword(ScopeService scopeService) {
-        super("scope");
+        super("validateScope");
         this.scopeService = scopeService;
     }
 
diff --git 
a/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/keyword/ScopeValidator.java
 
b/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/keyword/ScopeValidator.java
index 2f41d9b28..63bc65b29 100644
--- 
a/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/keyword/ScopeValidator.java
+++ 
b/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/keyword/ScopeValidator.java
@@ -34,7 +34,7 @@ public class ScopeValidator extends AbstractJsonValidator {
     private ScopeService scopeService;
 
     public ScopeValidator(ScopeService scopeService) {
-        super("scope");
+        super("validateScope");
         this.scopeService = scopeService;
     }
 
diff --git 
a/extensions/json-schema/services/src/main/resources/META-INF/cxs/schemas/events/event.json
 
b/extensions/json-schema/services/src/main/resources/META-INF/cxs/schemas/events/event.json
index 3e0dcf346..b4673a760 100644
--- 
a/extensions/json-schema/services/src/main/resources/META-INF/cxs/schemas/events/event.json
+++ 
b/extensions/json-schema/services/src/main/resources/META-INF/cxs/schemas/events/event.json
@@ -25,7 +25,7 @@
     },
     "scope" : {
       "type" : [ "string"],
-      "scope":  true,
+      "validateScope": true,
       "pattern" : "^(\\w|[-_@\\.]){0,60}$"
     },
     "sourceId" : {
diff --git 
a/extensions/json-schema/services/src/main/resources/META-INF/cxs/schemas/items/consent/consent.json
 
b/extensions/json-schema/services/src/main/resources/META-INF/cxs/schemas/items/consent/consent.json
index 5e35b89a0..40b09d64d 100644
--- 
a/extensions/json-schema/services/src/main/resources/META-INF/cxs/schemas/items/consent/consent.json
+++ 
b/extensions/json-schema/services/src/main/resources/META-INF/cxs/schemas/items/consent/consent.json
@@ -12,7 +12,7 @@
   "properties" : {
     "scope" : {
       "type" : "string",
-      "scope":  true
+      "validateScope" : true
     },
     "typeIdentifier" : {
       "type" : "string"
diff --git 
a/extensions/json-schema/services/src/main/resources/META-INF/cxs/schemas/items/item.json
 
b/extensions/json-schema/services/src/main/resources/META-INF/cxs/schemas/items/item.json
index 0c6e3d91e..d4a9f7772 100644
--- 
a/extensions/json-schema/services/src/main/resources/META-INF/cxs/schemas/items/item.json
+++ 
b/extensions/json-schema/services/src/main/resources/META-INF/cxs/schemas/items/item.json
@@ -21,7 +21,7 @@
     },
     "scope" : {
       "type" : ["null","string"],
-      "scope":  true,
+      "validateScope": true,
       "description" : "The item's scope"
     },
     "version" : {
diff --git a/itests/src/test/java/org/apache/unomi/itests/BasicIT.java 
b/itests/src/test/java/org/apache/unomi/itests/BasicIT.java
index 941072cbd..6bbde1f95 100644
--- a/itests/src/test/java/org/apache/unomi/itests/BasicIT.java
+++ b/itests/src/test/java/org/apache/unomi/itests/BasicIT.java
@@ -28,14 +28,19 @@ import org.apache.http.util.EntityUtils;
 import org.apache.unomi.api.ContextRequest;
 import org.apache.unomi.api.CustomItem;
 import org.apache.unomi.api.Event;
+import org.apache.unomi.api.Metadata;
 import org.apache.unomi.api.Profile;
+import org.apache.unomi.api.Scope;
 import org.apache.unomi.api.conditions.ConditionType;
 import org.apache.unomi.api.rules.Rule;
 import org.apache.unomi.api.services.DefinitionsService;
 import org.apache.unomi.api.services.ProfileService;
+import org.apache.unomi.api.services.ScopeService;
 import org.apache.unomi.itests.tools.httpclient.HttpClientThatWaitsForUnomi;
 import org.apache.unomi.persistence.spi.CustomObjectMapper;
+import org.junit.After;
 import org.junit.Assert;
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.ops4j.pax.exam.junit.PaxExam;
@@ -64,7 +69,6 @@ public class BasicIT extends BaseIT {
     private static final String SESSION_ID_4 = 
"aa3b04bd-8f4d-4a07-8e96-d33ffa04d3d4";
 
     private static final String EVENT_TYPE_LOGIN = "login";
-    private static final String EVENT_TYPE_LOGIN_SCHEMA = 
"schemas/events/login.json";
     private static final String EVENT_TYPE_VIEW = "view";
     private static final String TEST_SCOPE = "testScope";
 
@@ -93,6 +97,27 @@ public class BasicIT extends BaseIT {
     @Inject @Filter(timeout = 600000)
     protected DefinitionsService definitionsService;
 
+    @Inject @Filter(timeout = 600000)
+    protected ScopeService scopeService;
+
+    @Before
+    public void setUp() throws InterruptedException {
+        Scope scope = new Scope();
+        scope.setItemId(TEST_SCOPE);
+        Metadata metadata = new Metadata();
+        metadata.setName("Test scope");
+        metadata.setId(TEST_SCOPE);
+        scope.setMetadata(metadata);
+        scopeService.save(scope);
+        keepTrying("Scope test-scope not found in the required time", () -> 
scopeService.getScope(TEST_SCOPE),
+                Objects::nonNull, DEFAULT_TRYING_TIMEOUT, 
DEFAULT_TRYING_TRIES);
+    }
+    @After
+    public void tearDown() {
+        scopeService.delete(TEST_SCOPE);
+    }
+
+
     @Test
     public void testContextJS() throws IOException {
         LOGGER.info("Start test testContextJS");
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 fe1d1c91f..4ee8aa9d2 100644
--- a/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java
+++ b/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java
@@ -26,6 +26,7 @@ import org.apache.unomi.api.ContextResponse;
 import org.apache.unomi.api.Event;
 import org.apache.unomi.api.Metadata;
 import org.apache.unomi.api.Profile;
+import org.apache.unomi.api.Scope;
 import org.apache.unomi.api.Session;
 import org.apache.unomi.api.conditions.Condition;
 import org.apache.unomi.api.segments.Scoring;
@@ -33,6 +34,7 @@ import org.apache.unomi.api.segments.Segment;
 import org.apache.unomi.api.services.DefinitionsService;
 import org.apache.unomi.api.services.EventService;
 import org.apache.unomi.api.services.ProfileService;
+import org.apache.unomi.api.services.ScopeService;
 import org.apache.unomi.api.services.SegmentService;
 import org.apache.unomi.persistence.spi.CustomObjectMapper;
 import org.apache.unomi.persistence.spi.PersistenceService;
@@ -89,6 +91,7 @@ public class ContextServletIT extends BaseIT {
 
     private static final int DEFAULT_TRYING_TIMEOUT = 2000;
     private static final int DEFAULT_TRYING_TRIES = 30;
+    public static final String TEST_SCOPE = "test-scope";
 
     @Inject
     @Filter(timeout = 600000)
@@ -114,6 +117,10 @@ public class ContextServletIT extends BaseIT {
     @Filter(timeout = 600000)
     protected SchemaService schemaService;
 
+    @Inject
+    @Filter(timeout = 600000)
+    protected ScopeService scopeService;
+
     private Profile profile;
 
     @Before
@@ -145,6 +152,16 @@ public class ContextServletIT extends BaseIT {
                 (schemaIds) -> 
(schemaIds.contains("https://unomi.apache.org/schemas/json/events/floatPropertyType/1-0-0";)
 &&
                         
schemaIds.contains("https://unomi.apache.org/schemas/json/events/testEventType/1-0-0";)),
                 DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
+
+        Scope scope = new Scope();
+        scope.setItemId(TEST_SCOPE);
+        Metadata metadata = new Metadata();
+        metadata.setName("Test scope");
+        metadata.setId(TEST_SCOPE);
+        scope.setMetadata(metadata);
+        scopeService.save(scope);
+        keepTrying("Scope test-scope not found in the required time", () -> 
scopeService.getScope(TEST_SCOPE),
+                Objects::nonNull, DEFAULT_TRYING_TIMEOUT, 
DEFAULT_TRYING_TRIES);
     }
 
     @After
@@ -163,6 +180,8 @@ public class ContextServletIT extends BaseIT {
                 (schemaIds) -> 
(!schemaIds.contains("https://unomi.apache.org/schemas/json/events/floatPropertyType/1-0-0";)
 &&
                         
!schemaIds.contains("https://unomi.apache.org/schemas/json/events/testEventType/1-0-0";)),
                 DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
+
+        scopeService.delete(TEST_SCOPE);
     }
 
     @Test
@@ -170,7 +189,7 @@ public class ContextServletIT extends BaseIT {
         //Arrange
         String eventId = "test-event-id-" + System.currentTimeMillis();
         String sessionId = "test-session-id";
-        String scope = "test-scope";
+        String scope = TEST_SCOPE;
         String eventTypeOriginal = "test-event-type-original";
         Profile profile = new Profile(TEST_PROFILE_ID);
         Session session = new Session(sessionId, profile, new Date(), scope);
@@ -208,7 +227,7 @@ public class ContextServletIT extends BaseIT {
         //Arrange
         String eventId = "test-event-id-" + System.currentTimeMillis();
         String sessionId = "test-session-id";
-        String scope = "test-scope";
+        String scope = TEST_SCOPE;
         String eventTypeOriginal = "test-event-type-original";
         String eventTypeUpdated = TEST_EVENT_TYPE;
         Profile profile = new Profile(TEST_PROFILE_ID);
@@ -246,7 +265,7 @@ public class ContextServletIT extends BaseIT {
         //Arrange
         String eventId = "test-event-id-" + System.currentTimeMillis();
         String sessionId = "test-session-id";
-        String scope = "test-scope";
+        String scope = TEST_SCOPE;
         String eventTypeOriginal = "test-event-type-original";
         String eventTypeUpdated = TEST_EVENT_TYPE;
         Session session = new Session(sessionId, profile, new Date(), scope);
@@ -278,7 +297,7 @@ public class ContextServletIT extends BaseIT {
     public void testCreateEventsWithNoTimestampParam_profileAddedToSegment() 
throws IOException, InterruptedException {
         //Arrange
         String sessionId = "test-session-id";
-        String scope = "test-scope";
+        String scope = TEST_SCOPE;
         Event event = new Event();
         event.setEventType(TEST_EVENT_TYPE);
         event.setScope(scope);
@@ -310,7 +329,7 @@ public class ContextServletIT extends BaseIT {
     public void 
testCreateEventWithTimestampParam_pastEvent_profileIsNotAddedToSegment() throws 
IOException, InterruptedException {
         //Arrange
         String sessionId = "test-session-id";
-        String scope = "test-scope";
+        String scope = TEST_SCOPE;
         Event event = new Event();
         event.setEventType(TEST_EVENT_TYPE);
         event.setScope(scope);
@@ -343,7 +362,7 @@ public class ContextServletIT extends BaseIT {
     public void 
testCreateEventWithTimestampParam_futureEvent_profileIsNotAddedToSegment() 
throws IOException, InterruptedException {
         //Arrange
         String sessionId = "test-session-id";
-        String scope = "test-scope";
+        String scope = TEST_SCOPE;
         Event event = new Event();
         event.setEventType(TEST_EVENT_TYPE);
         event.setScope(scope);
diff --git 
a/itests/src/test/java/org/apache/unomi/itests/InputValidationIT.java 
b/itests/src/test/java/org/apache/unomi/itests/InputValidationIT.java
index 0723d58c2..5bda5bc09 100644
--- a/itests/src/test/java/org/apache/unomi/itests/InputValidationIT.java
+++ b/itests/src/test/java/org/apache/unomi/itests/InputValidationIT.java
@@ -24,6 +24,9 @@ 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.api.Metadata;
+import org.apache.unomi.api.Scope;
+import org.apache.unomi.api.services.ScopeService;
 import org.apache.unomi.itests.tools.httpclient.HttpClientThatWaitsForUnomi;
 import org.apache.unomi.schema.api.JsonSchemaWrapper;
 import org.apache.unomi.schema.api.SchemaService;
@@ -39,7 +42,9 @@ import java.io.IOException;
 import java.net.URLEncoder;
 import java.nio.charset.StandardCharsets;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
@@ -59,6 +64,26 @@ public class InputValidationIT extends BaseIT {
     @Filter(timeout = 600000)
     protected SchemaService schemaService;
 
+    @Inject
+    @Filter(timeout = 600000)
+    protected ScopeService scopeService;
+
+    @Before
+    public void setUp() {
+        Scope scope = new Scope();
+        scope.setItemId("dummy_scope");
+        Metadata metadata = new Metadata();
+        metadata.setName("Dummy scope");
+        metadata.setId("dummy_scope");
+        scope.setMetadata(metadata);
+        scopeService.save(scope);
+    }
+
+    @After
+    public void tearDown() throws InterruptedException {
+        removeItems(Scope.class);
+    }
+
     @Test
     public void test_param_EventsCollectorRequestNotNull() throws IOException {
         doPOSTRequestTest(EVENT_COLLECTOR_URL, null, null, 400, 
ERROR_MESSAGE_INVALID_DATA_RECEIVED);
diff --git a/itests/src/test/resources/schemas/event-flattened-valid.json 
b/itests/src/test/resources/schemas/event-flattened-valid.json
index e83d0101f..7c8e380b6 100644
--- a/itests/src/test/resources/schemas/event-flattened-valid.json
+++ b/itests/src/test/resources/schemas/event-flattened-valid.json
@@ -1,13 +1,13 @@
 {
-  "eventType":"flattened",
-  "scope":"dummy_scope",
-  "flattenedProperties": {
-    "interests": {
-      "cars": 15,
-      "football": 59
+    "eventType": "flattened",
+    "scope": "dummy_scope",
+    "flattenedProperties": {
+        "interests": {
+            "cars": 15,
+            "football": 59
+        }
+    },
+    "properties": {
+        "marker": "###EVENT_MARKER###"
     }
-  },
-  "properties": {
-    "marker": "###EVENT_MARKER###"
-  }
 }
diff --git a/itests/src/test/resources/schemas/schema-dummy-properties.json 
b/itests/src/test/resources/schemas/schema-dummy-properties.json
index 194bf8d00..e497791ee 100644
--- a/itests/src/test/resources/schemas/schema-dummy-properties.json
+++ b/itests/src/test/resources/schemas/schema-dummy-properties.json
@@ -15,7 +15,7 @@
     },
     "scope": {
       "type": "string",
-      "scope": true
+      "validateScope": true
     },
     "path": {
       "type": "string",

Reply via email to