You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by sh...@apache.org on 2021/07/09 13:06:26 UTC

[unomi] branch unomi-1.5.x updated: UNOMI-496 Fix NestedNulllException on increment property/interest integration tests. (#317)

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

shuber pushed a commit to branch unomi-1.5.x
in repository https://gitbox.apache.org/repos/asf/unomi.git


The following commit(s) were added to refs/heads/unomi-1.5.x by this push:
     new 9d74f53  UNOMI-496 Fix NestedNulllException on increment property/interest integration tests. (#317)
9d74f53 is described below

commit 9d74f53b832eb1ffb9eb31762225cc98b2657a9d
Author: Serge Huber <sh...@jahia.com>
AuthorDate: Fri Jul 9 15:03:53 2021 +0200

    UNOMI-496 Fix NestedNulllException on increment property/interest integration tests. (#317)
    
    (cherry picked from commit 27a607b446c3bffb43a49c240eba675b65511b71)
---
 .../apache/unomi/itests/IncrementInterestsIT.java  | 208 +++++++++++++++++++++
 .../apache/unomi/itests/IncrementPropertyIT.java   |  13 +-
 2 files changed, 220 insertions(+), 1 deletion(-)

diff --git a/itests/src/test/java/org/apache/unomi/itests/IncrementInterestsIT.java b/itests/src/test/java/org/apache/unomi/itests/IncrementInterestsIT.java
new file mode 100644
index 0000000..a0c3e92
--- /dev/null
+++ b/itests/src/test/java/org/apache/unomi/itests/IncrementInterestsIT.java
@@ -0,0 +1,208 @@
+/*
+ * 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 java.util.*;
+
+import javax.inject.Inject;
+
+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.Topic;
+import org.apache.unomi.api.actions.Action;
+import org.apache.unomi.api.conditions.Condition;
+import org.apache.unomi.api.rules.Rule;
+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.RulesService;
+import org.apache.unomi.api.services.TopicService;
+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 static org.apache.unomi.itests.BasicIT.ITEM_TYPE_PAGE;
+
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerSuite.class)
+public class IncrementInterestsIT extends BaseIT {
+    @Inject
+    @Filter(timeout = 600000)
+    protected ProfileService profileService;
+
+    @Inject
+    @Filter(timeout = 600000)
+    protected EventService eventService;
+
+    @Inject
+    @Filter(timeout = 600000)
+    protected TopicService topicService;
+
+    @Inject
+    @Filter(timeout = 600000)
+    protected RulesService rulesService;
+
+    @Inject
+    @Filter(timeout = 600000)
+    protected DefinitionsService definitionsService;
+
+    private Profile profile;
+    private Rule rule;
+    private Topic topic;
+
+    @Before
+    public void setup() throws Exception {
+        topic = createTopic("topicId");
+        profile = createProfile();
+        rule = new Rule(new Metadata(null, UUID.randomUUID().toString(), "IncrementInterestRule", "Test rule for IncrementInterestIT automated tests"));
+    }
+
+    @After
+    public void tearDown() {
+        rulesService.removeRule(rule.getItemId());
+        topicService.delete(topic.getItemId());
+        profileService.delete(profile.getItemId(), false);
+    }
+
+    @Test
+    public void test() throws InterruptedException {
+        Map<String, Double> interestsAsMap = new HashMap<>();
+        interestsAsMap.put(topic.getTopicId(), 50.0);
+        interestsAsMap.put("unknown", 10.0);
+
+        Event event = createEvent(profile, interestsAsMap);
+
+        int eventCode = eventService.send(event);
+
+        if (eventCode == EventService.PROFILE_UPDATED) {
+            Profile updatedProfile = profileService.save(event.getProfile());
+
+            Map<String, Double> interests = (Map<String, Double>) updatedProfile.getProperty("interests");
+
+            Assert.assertEquals(0.5, interests.get(topic.getTopicId()), 0.0);
+            Assert.assertFalse(interests.containsKey("unknown"));
+        } else {
+            Assert.fail("Profile was not updated");
+        }
+    }
+
+    @Test
+    public void testAction() throws InterruptedException {
+        Action incrementAction = new Action(definitionsService.getActionType("incrementInterestAction"));
+        incrementAction.setParameter("eventInterestProperty", "eventProperty::target.properties.interests");
+
+        Condition condition = new Condition(definitionsService.getConditionType("eventTypeCondition"));
+        condition.setParameter("eventTypeId", "view");
+
+        String itemId = UUID.randomUUID().toString();
+
+        Metadata metadata = new Metadata();
+        metadata.setId(itemId);
+        metadata.setName(itemId);
+        metadata.setDescription(itemId);
+        metadata.setEnabled(true);
+        metadata.setScope("systemscope");
+
+        rule.setCondition(condition);
+        List<Action> actions = new ArrayList<>();
+        actions.add(incrementAction);
+        rule.setActions(actions);
+        rule.setMetadata(metadata);
+
+        rulesService.setRule(rule);
+        refreshPersistence();
+
+        Map<String, Double> interestsAsMap = new HashMap<>();
+        interestsAsMap.put(topic.getTopicId(), 50.0);
+        interestsAsMap.put("unknown", 10.0);
+
+        Map<String, Object> properties = new HashMap<>();
+
+        Map<String,Object> pageInfo = new HashMap<>();
+        pageInfo.put("language", "en");
+        pageInfo.put("destinationURL", "https://www.acme.com/test-page.html");
+        pageInfo.put("referringURL", "https://unomi.apache.org");
+        pageInfo.put("pageID", "ITEM_ID_PAGE");
+        pageInfo.put("pagePath", "/test-page.html");
+        pageInfo.put("pageName", "Test page");
+
+        properties.put("interests", interestsAsMap);
+        properties.put("pageInfo", pageInfo);
+
+        CustomItem item = new CustomItem("page", ITEM_TYPE_PAGE);
+        item.setProperties(properties);
+
+        Event event = new Event("view", null, profile, null, null, item, new Date());
+        event.setPersistent(false);
+
+        int eventCode = eventService.send(event);
+
+        if (eventCode == EventService.PROFILE_UPDATED) {
+            Profile updatedProfile = profileService.save(event.getProfile());
+
+            Map<String, Double> interests = (Map<String, Double>) updatedProfile.getProperty("interests");
+
+            Assert.assertEquals(0.5, interests.get(topic.getTopicId()), 0.0);
+            Assert.assertFalse(interests.containsKey("unknown"));
+        } else {
+            throw new IllegalStateException("Profile was not updated");
+        }
+    }
+
+    private Event createEvent(Profile profile, Map<String, Double> interestsAsMap) {
+        Event event = new Event("incrementInterest", null, profile, null, null, profile, new Date());
+
+        event.setPersistent(false);
+        event.setProperty("interests", interestsAsMap);
+
+        return event;
+    }
+
+    private Topic createTopic(final String topicId) throws InterruptedException {
+        Topic topic = new Topic();
+
+        topic.setTopicId(topicId);
+        topic.setItemId(topicId);
+        topic.setName("topicName");
+        topic.setScope("scope");
+
+        topicService.save(topic);
+        refreshPersistence();
+
+        return topic;
+    }
+
+    private Profile createProfile() throws InterruptedException {
+        Profile profile = new Profile(UUID.randomUUID().toString());
+
+        profile.setProperty("firstName", "FirstName");
+        profile.setProperty("lastName", "LastName");
+
+        profileService.save(profile);
+        refreshPersistence();
+
+        return profile;
+    }
+}
diff --git a/itests/src/test/java/org/apache/unomi/itests/IncrementPropertyIT.java b/itests/src/test/java/org/apache/unomi/itests/IncrementPropertyIT.java
index 4488fb9..35999a0 100644
--- a/itests/src/test/java/org/apache/unomi/itests/IncrementPropertyIT.java
+++ b/itests/src/test/java/org/apache/unomi/itests/IncrementPropertyIT.java
@@ -385,7 +385,18 @@ public class IncrementPropertyIT extends BaseIT {
 
         CustomItem target = new CustomItem("ITEM_ID_PAGE", ITEM_TYPE_PAGE);
         target.setScope("acme-space");
-        if (targetProperties != null) target.setProperties(targetProperties);
+        if (targetProperties == null) {
+            targetProperties = new HashMap<>();
+        }
+        Map<String, Object> pageInfo = new HashMap<>();
+        pageInfo.put("language", "en");
+        pageInfo.put("destinationURL", "https://www.acme.com/test-page.html");
+        pageInfo.put("referringURL", "https://unomi.apache.org");
+        pageInfo.put("pageID", "ITEM_ID_PAGE");
+        pageInfo.put("pagePath", "/test-page.html");
+        pageInfo.put("pageName", "Test page");
+        targetProperties.put("pageInfo", pageInfo);
+        target.setProperties(targetProperties);
 
         event = new Event("view", null, profile, null, null, target, new Date());
         event.setPersistent(false);