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

jsinovassinnaik pushed a commit to branch UNOMI-817
in repository https://gitbox.apache.org/repos/asf/unomi.git

commit 63e88a2159cdea459f7b3c866f85294893b90058
Author: jsinovassin <jsinovassinn...@jahia.com>
AuthorDate: Wed Apr 24 10:56:52 2024 +0200

    fix tests
---
 .../java/org/apache/unomi/itests/SegmentIT.java    | 10 ++---
 .../actions/SetEventOccurenceCountAction.java      | 10 +++--
 .../PastEventConditionESQueryBuilder.java          | 45 ++++++++++++++++------
 .../rest/endpoints/SegmentServiceEndPoint.java     |  2 +
 4 files changed, 47 insertions(+), 20 deletions(-)

diff --git a/itests/src/test/java/org/apache/unomi/itests/SegmentIT.java 
b/itests/src/test/java/org/apache/unomi/itests/SegmentIT.java
index 3b3aa9314..59375b8c9 100644
--- a/itests/src/test/java/org/apache/unomi/itests/SegmentIT.java
+++ b/itests/src/test/java/org/apache/unomi/itests/SegmentIT.java
@@ -547,9 +547,8 @@ public class SegmentIT extends BaseIT {
 
         // insure the profile is engaged;
         try {
-            Assert.assertTrue("Profile should have 2 events in the scoring",
-                    (Long) ((Map) 
testEvent.getProfile().getSystemProperties().get("pastEvents"))
-                            
.get(pastEventCondition.getParameterValues().get("generatedPropertyKey")) == 2);
+            Map<String, Object> pastEvent = ((List<Map<String, 
Object>>)testEvent.getProfile().getSystemProperties().get("pastEvents")).stream().filter(profilePastEvent
 -> 
profilePastEvent.get("key").equals(pastEventCondition.getParameterValues().get("generatedPropertyKey"))).findFirst().get();
+            Assert.assertEquals("Profile should have 2 events in the scoring", 
2, (long) pastEvent.get("count"));
             Assert.assertTrue("Profile is engaged", 
testEvent.getProfile().getScores().containsKey("past-event-scoring-test")
                     && 
testEvent.getProfile().getScores().get("past-event-scoring-test") == 50);
         } catch (Exception e) {
@@ -562,8 +561,9 @@ public class SegmentIT extends BaseIT {
         // insure the profile is still engaged after recalculate;
         keepTrying("Profile should have 2 events in the scoring", () -> 
profileService.load("test_profile_id"), updatedProfile -> {
             try {
-                boolean eventCounted = (Integer) ((Map) 
updatedProfile.getSystemProperties().get("pastEvents"))
-                        
.get(pastEventCondition.getParameterValues().get("generatedPropertyKey")) == 2;
+                Map<String, Object> pastEvent = ((List<Map<String, 
Object>>)updatedProfile.getSystemProperties().get("pastEvents")).stream().filter(profilePastEvent
 -> 
profilePastEvent.get("key").equals(pastEventCondition.getParameterValues().get("generatedPropertyKey"))).findFirst().get();
+
+                boolean eventCounted = (Integer) pastEvent.get("count") == 2;
                 boolean profileEngaged = 
updatedProfile.getScores().containsKey("past-event-scoring-test")
                         && 
updatedProfile.getScores().get("past-event-scoring-test") == 50;
                 return eventCounted && profileEngaged;
diff --git 
a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/SetEventOccurenceCountAction.java
 
b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/SetEventOccurenceCountAction.java
index 2b195227f..e70922d04 100644
--- 
a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/SetEventOccurenceCountAction.java
+++ 
b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/SetEventOccurenceCountAction.java
@@ -30,6 +30,7 @@ import java.time.Duration;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
 import java.util.*;
+import java.util.stream.Collectors;
 
 import javax.xml.bind.DatatypeConverter;
 
@@ -128,11 +129,12 @@ public class SetEventOccurenceCountAction implements 
ActionExecutor {
     }
 
     private boolean updatePastEvents(Event event, String generatedPropertyKey, 
long count) {
-        ArrayList<Map<String, Object>> pastEvents =  (ArrayList<Map<String, 
Object>>) event.getProfile().getSystemProperties().get("pastEvents");
-        if (pastEvents == null) {
-            pastEvents = new ArrayList<>();
+        ArrayList<Map<String, Object>> pastEvents = new ArrayList<>();
+        ArrayList<Map<String, Object>> existingPastEvents = 
(ArrayList<Map<String, Object>>) 
event.getProfile().getSystemProperties().get("pastEvents");
+        if (existingPastEvents != null) {
+            pastEvents.addAll(existingPastEvents.stream().filter(pastEvent -> 
!pastEvent.get("key").equals(generatedPropertyKey)).collect(Collectors.toList()));
         }
-        pastEvents.removeIf(pastEvent -> 
pastEvent.get("key").equals(generatedPropertyKey));
+
         Map<String, Object> pastEvent = new HashMap<>();
         pastEvent.put("key", generatedPropertyKey);
         pastEvent.put("count", count);
diff --git 
a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/PastEventConditionESQueryBuilder.java
 
b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/PastEventConditionESQueryBuilder.java
index 28e8c1939..772be5f71 100644
--- 
a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/PastEventConditionESQueryBuilder.java
+++ 
b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/PastEventConditionESQueryBuilder.java
@@ -138,10 +138,7 @@ public class PastEventConditionESQueryBuilder implements 
ConditionESQueryBuilder
 
         Condition subConditionCount = new 
Condition(definitionsService.getConditionType("profilePropertyCondition"));
 
-        Condition subConditionKey = new 
Condition(definitionsService.getConditionType("profilePropertyCondition"));
-        subConditionKey.setParameter("propertyName", 
"systemProperties.pastEvents.key");
-        subConditionKey.setParameter("comparisonOperator", "equals");
-        subConditionKey.setParameter("propertyValue", generatedPropertyKey);
+        Condition subConditionKey = 
getKeyEqualsCondition(generatedPropertyKey);
 
         ConditionType profilePropertyConditionType = 
definitionsService.getConditionType("profilePropertyCondition");
         if (eventsOccurred) {
@@ -154,12 +151,26 @@ public class PastEventConditionESQueryBuilder implements 
ConditionESQueryBuilder
             booleanCondition.setParameter("subConditions", 
Arrays.asList(subConditionCount, subConditionKey));
 
             countCondition.setParameter("subCondition", booleanCondition);
+            return countCondition;
+
         } else {
-            Condition keyMissing = new Condition(profilePropertyConditionType);
-            keyMissing.setParameter("propertyName", 
"systemProperties.pastEvents.key");
-            keyMissing.setParameter("comparisonOperator", "missing");
-            keyMissing.setParameter("propertyValue", generatedPropertyKey);
 
+            // 1. Key not present in profile
+            Condition keyNestedCondition = new Condition();
+            
keyNestedCondition.setConditionType(definitionsService.getConditionType("nestedCondition"));
+            keyNestedCondition.setParameter("path", 
"systemProperties.pastEvents");
+
+            Condition keyEquals = new Condition(profilePropertyConditionType);
+            keyEquals.setParameter("propertyName", 
"systemProperties.pastEvents.key");
+            keyEquals.setParameter("comparisonOperator", "equals");
+            keyEquals.setParameter("propertyValue", generatedPropertyKey);
+
+            keyNestedCondition.setParameter("subCondition", keyEquals);
+
+            Condition mustNotExist = new 
Condition(definitionsService.getConditionType("notCondition"));
+            mustNotExist.setParameter("subCondition", keyNestedCondition);
+
+            // 2. Key present in profile but value equals to 0
             Condition counterZero = new 
Condition(profilePropertyConditionType);
             counterZero.setParameter("propertyName", 
"systemProperties.pastEvents.count");
             counterZero.setParameter("comparisonOperator", "equals");
@@ -169,14 +180,26 @@ public class PastEventConditionESQueryBuilder implements 
ConditionESQueryBuilder
             keyExistsAndCounterZero.setParameter("operator", "and");
             keyExistsAndCounterZero.setParameter("subConditions", 
Arrays.asList(subConditionKey, counterZero));
 
+            Condition nestedKeyExistsAndCounterZero = new Condition();
+            
nestedKeyExistsAndCounterZero.setConditionType(definitionsService.getConditionType("nestedCondition"));
+            nestedKeyExistsAndCounterZero.setParameter("path", 
"systemProperties.pastEvents");
+            nestedKeyExistsAndCounterZero.setParameter("subCondition", 
keyExistsAndCounterZero);
+
             Condition counterCondition = new Condition();
             
counterCondition.setConditionType(definitionsService.getConditionType("booleanCondition"));
             counterCondition.setParameter("operator", "or");
-            counterCondition.setParameter("subConditions", 
Arrays.asList(keyMissing, keyExistsAndCounterZero));
+            counterCondition.setParameter("subConditions", 
Arrays.asList(mustNotExist, nestedKeyExistsAndCounterZero));
 
-            countCondition.setParameter("subCondition", counterCondition);
+            return counterCondition;
         }
-        return countCondition;
+    }
+
+    private Condition getKeyEqualsCondition(String generatedPropertyKey) {
+        Condition subConditionKey = new 
Condition(definitionsService.getConditionType("profilePropertyCondition"));
+        subConditionKey.setParameter("propertyName", 
"systemProperties.pastEvents.key");
+        subConditionKey.setParameter("comparisonOperator", "equals");
+        subConditionKey.setParameter("propertyValue", generatedPropertyKey);
+        return subConditionKey;
     }
 
     private Set<String> getProfileIdsMatchingEventCount(Condition 
eventCondition, int minimumEventCount, int maximumEventCount) {
diff --git 
a/rest/src/main/java/org/apache/unomi/rest/endpoints/SegmentServiceEndPoint.java
 
b/rest/src/main/java/org/apache/unomi/rest/endpoints/SegmentServiceEndPoint.java
index 3cd547598..fbf854ecb 100644
--- 
a/rest/src/main/java/org/apache/unomi/rest/endpoints/SegmentServiceEndPoint.java
+++ 
b/rest/src/main/java/org/apache/unomi/rest/endpoints/SegmentServiceEndPoint.java
@@ -171,6 +171,8 @@ public class SegmentServiceEndPoint {
     @GET
     @Path("/{segmentID}")
     public Segment getSegmentDefinition(@PathParam("segmentID") String 
segmentId) {
+
+        segmentService.recalculatePastEventConditions();
         return segmentService.getSegmentDefinition(segmentId);
     }
 

Reply via email to