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 85953aa0d693a1068d60e71df547a5b94c125713 Author: jsinovassin <jsinovassinn...@jahia.com> AuthorDate: Wed Apr 24 10:56:52 2024 +0200 fix tests --- .../java/org/apache/unomi/itests/SegmentIT.java | 5 +-- .../PastEventConditionESQueryBuilder.java | 45 ++++++++++++++++------ .../rest/endpoints/SegmentServiceEndPoint.java | 2 + 3 files changed, 38 insertions(+), 14 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..e48bbc1ed 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) { 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); }