You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by jk...@apache.org on 2021/02/24 14:21:07 UTC
[unomi] branch unomi-1.5.x updated (d2eeee1 -> a07779e)
This is an automated email from the ASF dual-hosted git repository.
jkevan pushed a change to branch unomi-1.5.x
in repository https://gitbox.apache.org/repos/asf/unomi.git.
from d2eeee1 UNOMI-420: provide IT for mergeProfileOnPropertyAction and forceEvent… (#255)
new 40fa8a3 UNOMI-419 add IT to cover condition validation in segment (#254)
new a07779e UNOMI-419: fix SegmentsIT test to wait on full startup (#256)
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../exceptions/BadSegmentConditionException.java | 12 ++--
.../java/org/apache/unomi/itests/SegmentIT.java | 75 +++++++++++++++++++++-
.../ElasticSearchPersistenceServiceImpl.java | 10 +--
.../unomi/persistence/spi/PersistenceService.java | 4 +-
.../services/impl/segments/SegmentServiceImpl.java | 7 +-
5 files changed, 91 insertions(+), 17 deletions(-)
copy extensions/web-tracker/javascript/src/integrations.js => api/src/main/java/org/apache/unomi/api/exceptions/BadSegmentConditionException.java (81%)
[unomi] 02/02: UNOMI-419: fix SegmentsIT test to wait on full
startup (#256)
Posted by jk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
jkevan pushed a commit to branch unomi-1.5.x
in repository https://gitbox.apache.org/repos/asf/unomi.git
commit a07779e07ff8e99e21d0fbbbbd05d3acce246d8c
Author: kevan Jahanshahi <ke...@jahia.com>
AuthorDate: Wed Feb 24 15:15:21 2021 +0100
UNOMI-419: fix SegmentsIT test to wait on full startup (#256)
---
.../src/test/java/org/apache/unomi/itests/SegmentIT.java | 15 +++++++++++++++
1 file changed, 15 insertions(+)
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 b0b8dd7..2b63ed8 100644
--- a/itests/src/test/java/org/apache/unomi/itests/SegmentIT.java
+++ b/itests/src/test/java/org/apache/unomi/itests/SegmentIT.java
@@ -22,7 +22,9 @@ import org.apache.unomi.api.conditions.Condition;
import org.apache.unomi.api.segments.Segment;
import org.apache.unomi.api.services.SegmentService;
import org.apache.unomi.api.exceptions.BadSegmentConditionException;
+import org.apache.unomi.lifecycle.BundleWatcher;
import org.junit.Assert;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.junit.PaxExam;
@@ -45,6 +47,19 @@ public class SegmentIT extends BaseIT {
@Filter(timeout = 600000)
protected SegmentService segmentService;
+
+ @Inject @Filter(timeout = 600000)
+ protected BundleWatcher bundleWatcher;
+
+ @Before
+ public void setUp() throws InterruptedException {
+ while (!bundleWatcher.isStartupComplete()) {
+ LOGGER.info("Waiting for startup to complete...");
+ Thread.sleep(1000);
+ }
+ removeItems(Segment.class);
+ }
+
@Test
public void testSegments() {
Assert.assertNotNull("Segment service should be available", segmentService);
[unomi] 01/02: UNOMI-419 add IT to cover condition validation in
segment (#254)
Posted by jk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
jkevan pushed a commit to branch unomi-1.5.x
in repository https://gitbox.apache.org/repos/asf/unomi.git
commit 40fa8a3a04f00b93d96431dc17d76e447cab5a41
Author: MT BENTERKI <be...@gmail.com>
AuthorDate: Wed Feb 24 10:16:40 2021 +0100
UNOMI-419 add IT to cover condition validation in segment (#254)
* UNOMI-419 fix GraphQLListIT.java
* UNOMI-419 add IT to cover condition validation
* UNOMI-419 Add IT for bad parameter type (string instead integer)
Co-authored-by: Kevan <ke...@jahia.com>
---
.../exceptions/BadSegmentConditionException.java | 24 +++++++++
.../java/org/apache/unomi/itests/SegmentIT.java | 60 +++++++++++++++++++++-
.../ElasticSearchPersistenceServiceImpl.java | 10 ++--
.../unomi/persistence/spi/PersistenceService.java | 4 +-
.../services/impl/segments/SegmentServiceImpl.java | 7 ++-
5 files changed, 93 insertions(+), 12 deletions(-)
diff --git a/api/src/main/java/org/apache/unomi/api/exceptions/BadSegmentConditionException.java b/api/src/main/java/org/apache/unomi/api/exceptions/BadSegmentConditionException.java
new file mode 100644
index 0000000..bed7131
--- /dev/null
+++ b/api/src/main/java/org/apache/unomi/api/exceptions/BadSegmentConditionException.java
@@ -0,0 +1,24 @@
+/*
+ * 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.exceptions;
+
+public class BadSegmentConditionException extends RuntimeException {
+ public BadSegmentConditionException() {
+ super();
+ }
+}
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 ce8bd17..b0b8dd7 100644
--- a/itests/src/test/java/org/apache/unomi/itests/SegmentIT.java
+++ b/itests/src/test/java/org/apache/unomi/itests/SegmentIT.java
@@ -18,7 +18,10 @@
package org.apache.unomi.itests;
import org.apache.unomi.api.Metadata;
+import org.apache.unomi.api.conditions.Condition;
+import org.apache.unomi.api.segments.Segment;
import org.apache.unomi.api.services.SegmentService;
+import org.apache.unomi.api.exceptions.BadSegmentConditionException;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -36,8 +39,10 @@ import java.util.List;
@ExamReactorStrategy(PerSuite.class)
public class SegmentIT extends BaseIT {
private final static Logger LOGGER = LoggerFactory.getLogger(SegmentIT.class);
+ private final static String SEGMENT_ID = "test-segment-id-2";
- @Inject @Filter(timeout = 600000)
+ @Inject
+ @Filter(timeout = 600000)
protected SegmentService segmentService;
@Test
@@ -47,4 +52,57 @@ public class SegmentIT extends BaseIT {
Assert.assertEquals("Segment metadata list should be empty", 0, segmentMetadatas.size());
LOGGER.info("Retrieved " + segmentMetadatas.size() + " segment metadata entries");
}
+
+ @Test(expected = BadSegmentConditionException.class)
+ public void testSegmentWithNullCondition() {
+ Metadata segmentMetadata = new Metadata(SEGMENT_ID);
+ Segment segment = new Segment();
+ segment.setMetadata(segmentMetadata);
+ segment.setCondition(null);
+
+ segmentService.setSegmentDefinition(segment);
+ }
+
+ @Test(expected = BadSegmentConditionException.class)
+ public void testSegmentWithInValidCondition() {
+ Metadata segmentMetadata = new Metadata(SEGMENT_ID);
+ Segment segment = new Segment();
+ segment.setMetadata(segmentMetadata);
+ Condition condition = new Condition();
+ condition.setParameter("param", "param value");
+ condition.setConditionTypeId("fakeConditionId");
+ segment.setCondition(condition);
+
+ segmentService.setSegmentDefinition(segment);
+ }
+
+ @Test(expected = BadSegmentConditionException.class)
+ public void testSegmentWithInvalidConditionParameterTypes() {
+ 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", "10");
+ Condition pastEventEventCondition = new Condition(definitionsService.getConditionType("eventTypeCondition"));
+ pastEventEventCondition.setParameter("eventTypeId", "test-event-type");
+ segmentCondition.setParameter("eventCondition", pastEventEventCondition);
+ segment.setCondition(segmentCondition);
+ segmentService.setSegmentDefinition(segment);
+ }
+
+ @Test
+ public void testSegmentWithValidCondition() {
+ 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", 10);
+ Condition pastEventEventCondition = new Condition(definitionsService.getConditionType("eventTypeCondition"));
+ pastEventEventCondition.setParameter("eventTypeId", "test-event-type");
+ segmentCondition.setParameter("eventCondition", pastEventEventCondition);
+ segment.setCondition(segmentCondition);
+ segmentService.setSegmentDefinition(segment);
+
+ segmentService.removeSegmentDefinition(SEGMENT_ID, false);
+ }
}
diff --git a/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ElasticSearchPersistenceServiceImpl.java b/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ElasticSearchPersistenceServiceImpl.java
index 9b75bdf..5932cf8 100644
--- a/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ElasticSearchPersistenceServiceImpl.java
+++ b/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ElasticSearchPersistenceServiceImpl.java
@@ -1456,14 +1456,14 @@ public class ElasticSearchPersistenceServiceImpl implements PersistenceService,
}
@Override
- public boolean isValidCondition(Condition query, Item item) {
+ public boolean isValidCondition(Condition condition, Item item) {
try {
- conditionEvaluatorDispatcher.eval(query, item);
- QueryBuilder builder = QueryBuilders.boolQuery()
+ conditionEvaluatorDispatcher.eval(condition, item);
+ QueryBuilders.boolQuery()
.must(QueryBuilders.idsQuery().addIds(item.getItemId()))
- .must(conditionESQueryBuilderDispatcher.buildFilter(query));
+ .must(conditionESQueryBuilderDispatcher.buildFilter(condition));
} catch (Exception e) {
- logger.error("failed at setSegmentDefinition, condition={}", query.toString(), e);
+ logger.error("Failed to validate condition, condition={}", condition, e);
return false;
}
return true;
diff --git a/persistence-spi/src/main/java/org/apache/unomi/persistence/spi/PersistenceService.java b/persistence-spi/src/main/java/org/apache/unomi/persistence/spi/PersistenceService.java
index 18d53c3..77128b5 100644
--- a/persistence-spi/src/main/java/org/apache/unomi/persistence/spi/PersistenceService.java
+++ b/persistence-spi/src/main/java/org/apache/unomi/persistence/spi/PersistenceService.java
@@ -312,11 +312,11 @@ public interface PersistenceService {
/**
* validates if a condition throws exception at query build.
*
- * @param query the condition we're testing the specified item against
+ * @param condition the condition we're testing the specified item against
* @param item the item we're checking against the specified condition
* @return {@code true} if the item satisfies the condition, {@code false} otherwise
*/
- boolean isValidCondition(Condition query, Item item);
+ boolean isValidCondition(Condition condition, Item item);
/**
* Same as {@code query(fieldName, fieldValue, sortBy, clazz, 0, -1).getList()}
*
diff --git a/services/src/main/java/org/apache/unomi/services/impl/segments/SegmentServiceImpl.java b/services/src/main/java/org/apache/unomi/services/impl/segments/SegmentServiceImpl.java
index 363395d..7d8c2ce 100644
--- a/services/src/main/java/org/apache/unomi/services/impl/segments/SegmentServiceImpl.java
+++ b/services/src/main/java/org/apache/unomi/services/impl/segments/SegmentServiceImpl.java
@@ -39,6 +39,7 @@ import org.apache.unomi.persistence.spi.CustomObjectMapper;
import org.apache.unomi.persistence.spi.aggregate.TermsAggregate;
import org.apache.unomi.services.impl.AbstractServiceImpl;
import org.apache.unomi.services.impl.ParserHelper;
+import org.apache.unomi.api.exceptions.BadSegmentConditionException;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
@@ -257,8 +258,9 @@ public class SegmentServiceImpl extends AbstractServiceImpl implements SegmentSe
public void setSegmentDefinition(Segment segment) {
ParserHelper.resolveConditionType(definitionsService, segment.getCondition());
- if (persistenceService.isValidCondition(segment.getCondition(), new Profile()) == false)
+ if (!persistenceService.isValidCondition(segment.getCondition(), new Profile())) {
throw new BadSegmentConditionException();
+ }
if (segment.getMetadata().isEnabled() && !segment.getMetadata().isMissingPlugins()) {
updateAutoGeneratedRules(segment.getMetadata(), segment.getCondition());
@@ -1142,7 +1144,4 @@ public class SegmentServiceImpl extends AbstractServiceImpl implements SegmentSe
public void setTaskExecutionPeriod(long taskExecutionPeriod) {
this.taskExecutionPeriod = taskExecutionPeriod;
}
-
- private class BadSegmentConditionException extends RuntimeException {
- }
}