This is an automated email from the ASF dual-hosted git repository.
shuber 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 5df3113 Unomi-336 fix past event count using custom timestamp (#160)
5df3113 is described below
commit 5df31138f4f1657addd7b4584b2b669f60fd693b
Author: nlevitsky <[email protected]>
AuthorDate: Wed May 20 17:43:34 2020 +0300
Unomi-336 fix past event count using custom timestamp (#160)
* fix(SetEventOccurenceCountAction.java) - check the event's timestamp
before increase the pastEvent counter by 1
* fix(SetEventOccurenceCountAction.java) - change comment to Serge
suggestion
* rename(UpdateEventFromContextServletIT) - rename test class to
"ContextServletIT"
* improve(ContextServletIT) - refactor existing tests
* improve(ContextServletIT) - delete indexes' CONTENT instead of deleting
the indexes
* feat(ContextServletIT) - remove sessions between tests
* improve(TestUtils) - change "executeContextJSONRequest" function to static
* feat(ContextServletIT) - add unit-tests for past-event-segment
calculation when adding events with custom timestamp
Co-authored-by: nlevitsky <Pitzek1305>
---
.../test/java/org/apache/unomi/itests/AllITs.java | 2 +-
.../test/java/org/apache/unomi/itests/BasicIT.java | 3 +-
.../org/apache/unomi/itests/ContextServletIT.java | 298 +++++++++++++++++++++
.../java/org/apache/unomi/itests/SecurityIT.java | 3 +-
.../java/org/apache/unomi/itests/TestUtils.java | 35 ++-
.../itests/UpdateEventFromContextServletIT.java | 165 ------------
.../actions/SetEventOccurenceCountAction.java | 21 +-
7 files changed, 353 insertions(+), 174 deletions(-)
diff --git a/itests/src/test/java/org/apache/unomi/itests/AllITs.java
b/itests/src/test/java/org/apache/unomi/itests/AllITs.java
index c9ee4ff..73245e7 100644
--- a/itests/src/test/java/org/apache/unomi/itests/AllITs.java
+++ b/itests/src/test/java/org/apache/unomi/itests/AllITs.java
@@ -41,7 +41,7 @@ import org.junit.runners.Suite.SuiteClasses;
PropertiesUpdateActionIT.class,
ModifyConsentIT.class,
PatchIT.class,
- UpdateEventFromContextServletIT.class,
+ ContextServletIT.class,
SecurityIT.class
})
public class AllITs {
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 1a8e7e7..0ae2feb 100644
--- a/itests/src/test/java/org/apache/unomi/itests/BasicIT.java
+++ b/itests/src/test/java/org/apache/unomi/itests/BasicIT.java
@@ -58,7 +58,6 @@ public class BasicIT extends BaseIT {
private final static Logger LOGGER =
LoggerFactory.getLogger(BasicIT.class);
private ObjectMapper objectMapper = new ObjectMapper();
- private TestUtils testUtils = new TestUtils();
private static final String SESSION_ID_0 =
"aa3b04bd-8f4d-4a07-8e96-d33ffa04d3d0";
private static final String SESSION_ID_1 =
"aa3b04bd-8f4d-4a07-8e96-d33ffa04d3d1";
@@ -305,7 +304,7 @@ public class BasicIT extends BaseIT {
}
private TestUtils.RequestResponse executeContextJSONRequest(HttpPost
request, String sessionId) throws IOException {
- return testUtils.executeContextJSONRequest(request, sessionId);
+ return TestUtils.executeContextJSONRequest(request, sessionId);
}
private void checkVisitor1ResponseProperties(Map<String, Object>
profileProperties) {
diff --git a/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java
b/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java
new file mode 100644
index 0000000..926627b
--- /dev/null
+++ b/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java
@@ -0,0 +1,298 @@
+/*
+ * 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 com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.unomi.api.*;
+import org.apache.unomi.api.conditions.Condition;
+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.SegmentService;
+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.net.URI;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.Arrays;
+import java.util.Date;
+
+import static org.hamcrest.core.IsCollectionContaining.hasItem;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+
+
+/**
+ * Created by Ron Barabash on 5/4/2020.
+ */
+
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerSuite.class)
+public class ContextServletIT extends BaseIT {
+ private final static String CONTEXT_URL = "/context.json";
+ private final static String THIRD_PARTY_HEADER_NAME = "X-Unomi-Peer";
+ private final static String SEGMENT_EVENT_TYPE = "test-event-type";
+ private final static String SEGMENT_ID = "test-segment-id";
+ private final static int SEGMENT_NUMBER_OF_DAYS = 30;
+
+ private ObjectMapper objectMapper = new ObjectMapper();
+
+ @Inject
+ @Filter(timeout = 600000)
+ protected EventService eventService;
+
+ @Inject
+ @Filter(timeout = 600000)
+ protected PersistenceService persistenceService;
+
+ @Inject
+ @Filter(timeout = 600000)
+ protected ProfileService profileService;
+
+ @Inject
+ @Filter(timeout = 600000)
+ protected DefinitionsService definitionsService;
+
+ @Inject
+ @Filter(timeout = 600000)
+ protected SegmentService segmentService;
+
+ @Before
+ public void setUp() throws InterruptedException {
+ //Create a past-event segment
+ Metadata segmentMetadata = new Metadata(SEGMENT_ID);
+ Segment segment = new Segment(segmentMetadata);
+ Condition segmentCondition = new
Condition(definitionsService.getConditionType("pastEventCondition"));
+ segmentCondition.setParameter("minimumEventCount",2);
+
segmentCondition.setParameter("numberOfDays",SEGMENT_NUMBER_OF_DAYS);
+ Condition pastEventEventCondition = new
Condition(definitionsService.getConditionType("eventTypeCondition"));
+
pastEventEventCondition.setParameter("eventTypeId",SEGMENT_EVENT_TYPE);
+
segmentCondition.setParameter("eventCondition",pastEventEventCondition);
+ segment.setCondition(segmentCondition);
+ segmentService.setSegmentDefinition(segment);
+ Thread.sleep(2000);
+ }
+
+ @After
+ public void tearDown() {
+ TestUtils.removeAllEvents(definitionsService,
persistenceService);
+ TestUtils.removeAllSessions(definitionsService,
persistenceService);
+ TestUtils.removeAllProfiles(definitionsService,
persistenceService);
+ segmentService.removeSegmentDefinition(SEGMENT_ID,false);
+ persistenceService.refresh();
+ }
+
+ @Test
+ public void testUpdateEventFromContextAuthorizedThirdParty_Success()
throws IOException, InterruptedException {
+ //Arrange
+ String eventId = "test-event-id1";
+ String profileId = "test-profile-id";
+ String sessionId = "test-session-id";
+ String scope = "test-scope";
+ String eventTypeOriginal = "test-event-type-original";
+ String eventTypeUpdated = "test-event-type-updated";
+ Profile profile = new Profile(profileId);
+ Session session = new Session(sessionId, profile, new Date(),
scope);
+ Event event = new Event(eventId, eventTypeOriginal, session,
profile, scope, null, null, new Date());
+ profileService.save(profile);
+ this.eventService.send(event);
+ Thread.sleep(2000);
+ event.setEventType(eventTypeUpdated); //change the event so we
can see the update effect
+
+ //Act
+ ContextRequest contextRequest = new ContextRequest();
+ contextRequest.setSessionId(session.getItemId());
+ contextRequest.setEvents(Arrays.asList(event));
+ HttpPost request = new HttpPost(URL + CONTEXT_URL);
+ request.addHeader(THIRD_PARTY_HEADER_NAME, UNOMI_KEY);
+ request.setEntity(new
StringEntity(objectMapper.writeValueAsString(contextRequest),
ContentType.create("application/json")));
+ TestUtils.executeContextJSONRequest(request, sessionId);
+ Thread.sleep(2000); //Making sure event is updated in DB
+
+ //Assert
+ event = this.eventService.getEvent(eventId);
+ assertEquals(2, event.getVersion().longValue());
+ assertEquals(eventTypeUpdated,event.getEventType());
+ }
+
+ @Test
+ public void testUpdateEventFromContextUnAuthorizedThirdParty_Fail()
throws IOException, InterruptedException {
+ //Arrange
+ String eventId = "test-event-id2";
+ String profileId = "test-profile-id";
+ String sessionId = "test-session-id";
+ String scope = "test-scope";
+ String eventTypeOriginal = "test-event-type-original";
+ String eventTypeUpdated = "test-event-type-updated";
+ Profile profile = new Profile(profileId);
+ Session session = new Session(sessionId, profile, new Date(),
scope);
+ Event event = new Event(eventId, eventTypeOriginal, session,
profile, scope, null, null, new Date());
+ profileService.save(profile);
+ this.eventService.send(event);
+ Thread.sleep(2000);
+ event.setEventType(eventTypeUpdated); //change the event so we
can see the update effect
+
+ //Act
+ ContextRequest contextRequest = new ContextRequest();
+ contextRequest.setSessionId(session.getItemId());
+ contextRequest.setEvents(Arrays.asList(event));
+ HttpPost request = new HttpPost(URL + CONTEXT_URL);
+ request.setEntity(new
StringEntity(objectMapper.writeValueAsString(contextRequest),
ContentType.create("application/json")));
+ TestUtils.executeContextJSONRequest(request, sessionId);
+ Thread.sleep(2000); //Making sure event is updated in DB
+
+ //Assert
+ event = this.eventService.getEvent(eventId);
+ assertEquals(1, event.getVersion().longValue());
+ assertEquals(eventTypeOriginal,event.getEventType());
+ }
+
+
+ @Test
+ public void
testUpdateEventFromContextAuthorizedThirdPartyNoItemID_Fail() throws
IOException, InterruptedException {
+ //Arrange
+ String eventId = "test-event-id3";
+ String profileId = "test-profile-id";
+ String sessionId = "test-session-id";
+ String scope = "test-scope";
+ String eventTypeOriginal = "test-event-type-original";
+ String eventTypeUpdated = "test-event-type-updated";
+ Profile profile = new Profile(profileId);
+ Session session = new Session(sessionId, profile, new Date(),
scope);
+ Event event = new Event(eventId, eventTypeOriginal, session,
profile, scope, null, null, new Date());
+ profileService.save(profile);
+ this.eventService.send(event);
+ Thread.sleep(2000);
+ event.setEventType(eventTypeUpdated); //change the event so we
can see the update effect
+
+ //Act
+ ContextRequest contextRequest = new ContextRequest();
+ contextRequest.setSessionId(session.getItemId());
+ contextRequest.setEvents(Arrays.asList(event));
+ HttpPost request = new HttpPost(URL + CONTEXT_URL);
+ request.setEntity(new
StringEntity(objectMapper.writeValueAsString(contextRequest),
ContentType.create("application/json")));
+ TestUtils.executeContextJSONRequest(request, sessionId);
+ Thread.sleep(2000); //Making sure event is updated in DB
+
+ //Assert
+ event = this.eventService.getEvent(eventId);
+ assertEquals(1, event.getVersion().longValue());
+ assertEquals(eventTypeOriginal,event.getEventType());
+ }
+
+ @Test
+ public void
testCreateEventsWithNoTimestampParam_profileAddedToSegment() throws IOException
{
+ //Arrange
+ String sessionId = "test-session-id";
+ String scope = "test-scope";
+ Event event = new Event();
+ event.setEventType(SEGMENT_EVENT_TYPE);
+ event.setScope(scope);
+
+ //Act
+ ContextRequest contextRequest = new ContextRequest();
+ contextRequest.setSessionId(sessionId);
+ contextRequest.setRequireSegments(true);
+ contextRequest.setEvents(Arrays.asList(event));
+ HttpPost request = new HttpPost(URL + CONTEXT_URL);
+ request.setEntity(new
StringEntity(objectMapper.writeValueAsString(contextRequest),
ContentType.create("application/json")));
+ String cookieHeaderValue =
TestUtils.executeContextJSONRequest(request, sessionId).getCookieHeaderValue();
+ //Add the context-profile-id cookie to the second event
+ request.addHeader("Cookie", cookieHeaderValue);
+ ContextResponse response =
(TestUtils.executeContextJSONRequest(request, sessionId)).getContextResponse();
//second event
+
+ //Assert
+ assertEquals(1, response.getProfileSegments().size());
+ assertThat(response.getProfileSegments(),hasItem(SEGMENT_ID));
+ }
+
+ @Test
+ public void
testCreateEventWithTimestampParam_pastEvent_profileIsNotAddedToSegment() throws
IOException {
+ //Arrange
+ String sessionId = "test-session-id";
+ String scope = "test-scope";
+ Event event = new Event();
+ event.setEventType(SEGMENT_EVENT_TYPE);
+ event.setScope(scope);
+ String regularURI = URL + CONTEXT_URL;
+ long oldTimestamp =
LocalDateTime.now(ZoneId.of("UTC")).minusDays(SEGMENT_NUMBER_OF_DAYS +
1).toInstant(ZoneOffset.UTC).toEpochMilli();
+ String customTimestampURI = regularURI + "?timestamp=" +
oldTimestamp;
+
+ //Act
+ ContextRequest contextRequest = new ContextRequest();
+ contextRequest.setSessionId(sessionId);
+ contextRequest.setRequireSegments(true);
+ contextRequest.setEvents(Arrays.asList(event));
+ HttpPost request = new HttpPost(regularURI);
+ request.setEntity(new
StringEntity(objectMapper.writeValueAsString(contextRequest),
ContentType.create("application/json")));
+ //The first event is with a default timestamp (now)
+ String cookieHeaderValue =
TestUtils.executeContextJSONRequest(request, sessionId).getCookieHeaderValue();
+ //The second event is with a customized timestamp
+ request.setURI(URI.create(customTimestampURI));
+ request.addHeader("Cookie", cookieHeaderValue);
+ ContextResponse response =
(TestUtils.executeContextJSONRequest(request, sessionId)).getContextResponse();
//second event
+
+ //Assert
+ assertEquals(0,response.getProfileSegments().size());
+ }
+
+ @Test
+ public void
testCreateEventWithTimestampParam_futureEvent_profileIsNotAddedToSegment()
throws IOException {
+ //Arrange
+ String sessionId = "test-session-id";
+ String scope = "test-scope";
+ Event event = new Event();
+ event.setEventType(SEGMENT_EVENT_TYPE);
+ event.setScope(scope);
+ String regularURI = URL + CONTEXT_URL;
+ long futureTimestamp =
LocalDateTime.now(ZoneId.of("UTC")).plusDays(1).toInstant(ZoneOffset.UTC).toEpochMilli();
+ String customTimestampURI = regularURI + "?timestamp=" +
futureTimestamp;
+
+ //Act
+ ContextRequest contextRequest = new ContextRequest();
+ contextRequest.setSessionId(sessionId);
+ contextRequest.setRequireSegments(true);
+ contextRequest.setEvents(Arrays.asList(event));
+ HttpPost request = new HttpPost(regularURI);
+ request.setEntity(new
StringEntity(objectMapper.writeValueAsString(contextRequest),
ContentType.create("application/json")));
+ //The first event is with a default timestamp (now)
+ String cookieHeaderValue =
TestUtils.executeContextJSONRequest(request, sessionId).getCookieHeaderValue();
+ //The second event is with a customized timestamp
+ request.setURI(URI.create(customTimestampURI));
+ request.addHeader("Cookie", cookieHeaderValue);
+ ContextResponse response =
(TestUtils.executeContextJSONRequest(request, sessionId)).getContextResponse();
//second event
+
+ //Assert
+ assertEquals(0,response.getProfileSegments().size());
+ }
+}
diff --git a/itests/src/test/java/org/apache/unomi/itests/SecurityIT.java
b/itests/src/test/java/org/apache/unomi/itests/SecurityIT.java
index c2d5a3f..b90e953 100644
--- a/itests/src/test/java/org/apache/unomi/itests/SecurityIT.java
+++ b/itests/src/test/java/org/apache/unomi/itests/SecurityIT.java
@@ -54,7 +54,6 @@ public class SecurityIT extends BaseIT {
private static final String SESSION_ID = "vuln-session-id";
private ObjectMapper objectMapper;
- private TestUtils testUtils = new TestUtils();
@Inject
@Filter(timeout = 600000)
@@ -114,7 +113,7 @@ public class SecurityIT extends BaseIT {
}
private TestUtils.RequestResponse executeContextJSONRequest(HttpPost
request, String sessionId) throws IOException {
- return testUtils.executeContextJSONRequest(request, sessionId);
+ return TestUtils.executeContextJSONRequest(request, sessionId);
}
diff --git a/itests/src/test/java/org/apache/unomi/itests/TestUtils.java
b/itests/src/test/java/org/apache/unomi/itests/TestUtils.java
index 066adbd..eae0252 100644
--- a/itests/src/test/java/org/apache/unomi/itests/TestUtils.java
+++ b/itests/src/test/java/org/apache/unomi/itests/TestUtils.java
@@ -25,7 +25,13 @@ import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.unomi.api.ContextResponse;
+import org.apache.unomi.api.Event;
+import org.apache.unomi.api.Profile;
+import org.apache.unomi.api.Session;
+import org.apache.unomi.api.conditions.Condition;
+import org.apache.unomi.api.services.DefinitionsService;
import org.apache.unomi.persistence.spi.CustomObjectMapper;
+import org.apache.unomi.persistence.spi.PersistenceService;
import org.junit.Assert;
import java.io.IOException;
@@ -52,7 +58,7 @@ public class TestUtils {
return null;
}
- public RequestResponse executeContextJSONRequest(HttpPost request,
String sessionId) throws IOException {
+ public static RequestResponse executeContextJSONRequest(HttpPost
request, String sessionId) throws IOException {
try (CloseableHttpResponse response =
HttpClientBuilder.create().build().execute(request)) {
// validate mimeType
String mimeType =
ContentType.getOrDefault(response.getEntity()).getMimeType();
@@ -73,6 +79,33 @@ public class TestUtils {
}
}
+ public static boolean removeAllProfiles(DefinitionsService
definitionsService, PersistenceService persistenceService) {
+ Condition condition = new
Condition(definitionsService.getConditionType("profilePropertyCondition"));
+ condition.setParameter("propertyName","itemType");
+ condition.setParameter("comparisonOperator","equals");
+ condition.setParameter("propertyValue","profile");
+
+ return persistenceService.removeByQuery(condition,
Profile.class);
+ }
+
+ public static boolean removeAllEvents(DefinitionsService
definitionsService, PersistenceService persistenceService) {
+ Condition condition = new
Condition(definitionsService.getConditionType("eventPropertyCondition"));
+ condition.setParameter("propertyName","itemType");
+ condition.setParameter("comparisonOperator","equals");
+ condition.setParameter("propertyValue","event");
+
+ return persistenceService.removeByQuery(condition, Event.class);
+ }
+
+ public static boolean removeAllSessions(DefinitionsService
definitionsService, PersistenceService persistenceService) {
+ Condition condition = new
Condition(definitionsService.getConditionType("sessionPropertyCondition"));
+ condition.setParameter("propertyName","itemType");
+ condition.setParameter("comparisonOperator","equals");
+ condition.setParameter("propertyValue","session");
+
+ return persistenceService.removeByQuery(condition,
Session.class);
+ }
+
public static class RequestResponse {
private ContextResponse contextResponse;
private String cookieHeaderValue;
diff --git
a/itests/src/test/java/org/apache/unomi/itests/UpdateEventFromContextServletIT.java
b/itests/src/test/java/org/apache/unomi/itests/UpdateEventFromContextServletIT.java
deleted file mode 100644
index 20da2bc..0000000
---
a/itests/src/test/java/org/apache/unomi/itests/UpdateEventFromContextServletIT.java
+++ /dev/null
@@ -1,165 +0,0 @@
-/*
- * 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 com.fasterxml.jackson.databind.ObjectMapper;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.entity.ContentType;
-import org.apache.http.entity.StringEntity;
-import org.apache.unomi.api.ContextRequest;
-import org.apache.unomi.api.Event;
-import org.apache.unomi.api.Profile;
-import org.apache.unomi.api.Session;
-import org.apache.unomi.api.services.EventService;
-import org.apache.unomi.api.services.ProfileService;
-import org.apache.unomi.persistence.spi.PersistenceService;
-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;
-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.Collections;
-import java.util.Date;
-
-
-/**
- * Created by Ron Barabash on 5/4/2020.
- */
-
-@RunWith(PaxExam.class)
-@ExamReactorStrategy(PerSuite.class)
-public class UpdateEventFromContextServletIT extends BaseIT {
- private final static String TEST_SESSION_ID = "test-session-id";
- private final static String TEST_EVENT_ID = "test-event-id";
- private final static String TEST_SCOPE = "test-scope";
- private final static String TEST_PROFILE_ID = "test-profile-id";
- private final static String EVENT_TYPE = "view";
- private final static String THIRD_PARTY_HEADER_NAME = "X-Unomi-Peer";
- private Profile profile = new Profile(TEST_PROFILE_ID);
- private Session session = new Session(TEST_SESSION_ID, profile, new
Date(), TEST_SCOPE);
- private ObjectMapper objectMapper = new ObjectMapper();
- private TestUtils testUtils = new TestUtils();
-
- @Inject
- @Filter(timeout = 600000)
- protected EventService eventService;
- @Inject
- @Filter(timeout = 600000)
- protected PersistenceService persistenceService;
- @Inject
- @Filter(timeout = 600000)
- protected ProfileService profileService;
-
- @Before
- public void setUp() throws InterruptedException {
- Event pageViewEvent = new Event(TEST_EVENT_ID, EVENT_TYPE,
session, profile, TEST_SCOPE, null, null, new Date());
-
- profileService.save(profile);
- this.eventService.send(pageViewEvent);
-
- Thread.sleep(2000);
- }
-
- @After
- public void tearDown() {
- //Using remove index due to document version is still
persistent after deletion as referenced here
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html#delete-versioning
- this.persistenceService.removeIndex("event-date-*");
- this.profileService.delete(TEST_PROFILE_ID, false);
- this.persistenceService.refresh();
- }
-
- @Test
- public void testUpdateEventFromContextAuthorizedThirdParty_Success()
throws IOException, InterruptedException {
- Event event = this.eventService.getEvent(TEST_EVENT_ID);
- Assert.assertEquals(new Long(1), event.getVersion());
- Profile profile = profileService.load(TEST_PROFILE_ID);
- Event pageViewEvent = new Event(TEST_EVENT_ID, EVENT_TYPE,
session, profile, TEST_SCOPE, null, null, new Date());
-
- ContextRequest contextRequest = new ContextRequest();
- contextRequest.setSessionId(session.getItemId());
-
contextRequest.setEvents(Collections.singletonList(pageViewEvent));
-
- HttpPost request = new HttpPost(URL + "/context.json");
- request.addHeader(THIRD_PARTY_HEADER_NAME, UNOMI_KEY);
- request.setEntity(new
StringEntity(objectMapper.writeValueAsString(contextRequest),
- ContentType.create("application/json")));
-
- //Making sure Unomi is up and running
- Thread.sleep(5000);
- this.testUtils.executeContextJSONRequest(request,
TEST_SESSION_ID);
-
- //Making sure event is updated in DB
- Thread.sleep(2000);
- event = this.eventService.getEvent(TEST_EVENT_ID);
- Assert.assertEquals(new Long(2), event.getVersion());
- }
-
- @Test
- public void testUpdateEventFromContextUnAuthorizedThirdParty_Fail()
throws IOException, InterruptedException {
- Event event = this.eventService.getEvent(TEST_EVENT_ID);
- Assert.assertEquals(new Long(1), event.getVersion());
- Profile profile = profileService.load(TEST_PROFILE_ID);
- Event pageViewEvent = new Event(TEST_EVENT_ID, EVENT_TYPE,
session, profile, TEST_SCOPE, null, null, new Date());
- ContextRequest contextRequest = new ContextRequest();
- contextRequest.setSessionId(session.getItemId());
-
contextRequest.setEvents(Collections.singletonList(pageViewEvent));
- HttpPost request = new HttpPost(URL + "/context.json");
- request.setEntity(new
StringEntity(objectMapper.writeValueAsString(contextRequest),
- ContentType.create("application/json")));
-
- //Making sure Unomi is up and running
- Thread.sleep(5000);
- this.testUtils.executeContextJSONRequest(request,
TEST_SESSION_ID);
-
- //Making sure event is updated in DB
- Thread.sleep(2000);
- event = this.eventService.getEvent(TEST_EVENT_ID);
- Assert.assertEquals(new Long(1), event.getVersion());
- }
-
-
- @Test
- public void
testUpdateEventFromContextAuthorizedThirdPartyNoItemID_Fail() throws
IOException, InterruptedException {
- Event event = this.eventService.getEvent(TEST_EVENT_ID);
- Assert.assertEquals(new Long(1), event.getVersion());
- Profile profile = profileService.load(TEST_PROFILE_ID);
- Event pageViewEvent = new Event(EVENT_TYPE, session, profile,
TEST_SCOPE, null, null, new Date());
- ContextRequest contextRequest = new ContextRequest();
- contextRequest.setSessionId(session.getItemId());
-
contextRequest.setEvents(Collections.singletonList(pageViewEvent));
- HttpPost request = new HttpPost(URL + "/context.json");
- request.setEntity(new
StringEntity(objectMapper.writeValueAsString(contextRequest),
- ContentType.create("application/json")));
-
- //Making sure Unomi is up and running
- Thread.sleep(5000);
- this.testUtils.executeContextJSONRequest(request,
TEST_SESSION_ID);
-
- //Making sure event is updated in DB
- Thread.sleep(2000);
- event = this.eventService.getEvent(TEST_EVENT_ID);
- Assert.assertEquals(new Long(1), event.getVersion());
- }
-}
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 cd1b5e3..3c4e6f2 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
@@ -25,7 +25,12 @@ import org.apache.unomi.api.services.DefinitionsService;
import org.apache.unomi.api.services.EventService;
import org.apache.unomi.persistence.spi.PersistenceService;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
import java.util.ArrayList;
+import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -60,13 +65,14 @@ public class SetEventOccurenceCountAction implements
ActionExecutor {
c.setParameter("propertyValue", event.getProfileId());
conditions.add(c);
+ int numberOfDays = 0;
if (pastEventCondition.getParameter("numberOfDays") != null) {
- int i = (Integer) pastEventCondition.getParameter("numberOfDays");
+ numberOfDays = (Integer)
pastEventCondition.getParameter("numberOfDays");
Condition timeCondition = new
Condition(definitionsService.getConditionType("eventPropertyCondition"));
timeCondition.setParameter("propertyName", "timeStamp");
timeCondition.setParameter("comparisonOperator", "greaterThan");
- timeCondition.setParameter("propertyValueDateExpr", "now-" + i +
"d");
+ timeCondition.setParameter("propertyValueDateExpr", "now-" +
numberOfDays + "d");
conditions.add(timeCondition);
}
@@ -80,7 +86,16 @@ public class SetEventOccurenceCountAction implements
ActionExecutor {
pastEvents = new LinkedHashMap<>();
event.getProfile().getSystemProperties().put("pastEvents",
pastEvents);
}
- pastEvents.put((String)
pastEventCondition.getParameter("generatedPropertyKey"), count + 1);
+
+ //Only increase the counter by 1 if the current event is in the
now-numberOfDays range
+ LocalDateTime now = LocalDateTime.now(ZoneId.of("UTC"));
+ LocalDateTime eventTime =
LocalDateTime.ofInstant(event.getTimeStamp().toInstant(),ZoneId.of("UTC"));
+ long daysDiff = Duration.between(eventTime,now).toDays();
+ if (daysDiff >= 0 && daysDiff <= numberOfDays) {
+ count++;
+ }
+
+ pastEvents.put((String)
pastEventCondition.getParameter("generatedPropertyKey"), count);
return EventService.PROFILE_UPDATED;
}