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/02/08 17:17:17 UTC

[unomi] 11/15: UNOMI-422 improve PropertiesUpdateActionIT (#241)

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

commit b5b6a3b3c6e2ccefc54b14192b46dd476ec90db0
Author: MT BENTERKI <be...@gmail.com>
AuthorDate: Wed Feb 3 10:40:04 2021 +0100

    UNOMI-422 improve PropertiesUpdateActionIT (#241)
    
    (cherry picked from commit 40cac9bb99698fc35c2de5877e00c12c9671fe40)
---
 .../unomi/itests/PropertiesUpdateActionIT.java     | 132 +++++++++++++++++++--
 1 file changed, 124 insertions(+), 8 deletions(-)

diff --git a/itests/src/test/java/org/apache/unomi/itests/PropertiesUpdateActionIT.java b/itests/src/test/java/org/apache/unomi/itests/PropertiesUpdateActionIT.java
index 1e64b91..f1b6304 100644
--- a/itests/src/test/java/org/apache/unomi/itests/PropertiesUpdateActionIT.java
+++ b/itests/src/test/java/org/apache/unomi/itests/PropertiesUpdateActionIT.java
@@ -35,9 +35,7 @@ import org.slf4j.LoggerFactory;
 
 import javax.inject.Inject;
 import java.io.IOException;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
 
 /**
  * Created by amidani on 12/10/2017.
@@ -57,7 +55,7 @@ public class PropertiesUpdateActionIT extends BaseIT {
     protected EventService eventService;
 
     @Before
-    public void setUp() throws IOException {
+    public void setUp() throws IOException, InterruptedException {
         Profile profile = new Profile();
         profile.setItemId(PROFILE_TEST_ID);
         profileService.save(profile);
@@ -67,6 +65,8 @@ public class PropertiesUpdateActionIT extends BaseIT {
         profileTarget.setItemId(PROFILE_TARGET_TEST_ID);
         profileService.save(profileTarget);
         LOGGER.info("Profile saved with ID [{}].", profileTarget.getItemId());
+
+        refreshPersistence();
     }
 
     @Test
@@ -81,16 +81,19 @@ public class PropertiesUpdateActionIT extends BaseIT {
         propertyToUpdate.put("properties.firstName", "UPDATED FIRST NAME CURRENT PROFILE");
 
         updateProperties.setProperty(UpdatePropertiesAction.PROPS_TO_UPDATE, propertyToUpdate);
+        updateProperties.setProperty(UpdatePropertiesAction.TARGET_ID_KEY, PROFILE_TARGET_TEST_ID);
+        updateProperties.setProperty(UpdatePropertiesAction.TARGET_TYPE_KEY, "profile");
+
         int changes = eventService.send(updateProperties);
 
         LOGGER.info("Changes of the event : {}", changes);
 
         Assert.assertTrue(changes > 0);
+        Assert.assertEquals("UPDATED FIRST NAME CURRENT PROFILE", profile.getProperty("firstName"));
     }
 
     @Test
     public void testUpdateProperties_NotCurrentProfile() {
-
         Profile profile = profileService.load(PROFILE_TARGET_TEST_ID);
         Profile profileToUpdate = profileService.load(PROFILE_TEST_ID);
         Assert.assertNull(profileToUpdate.getProperty("firstName"));
@@ -104,12 +107,125 @@ public class PropertiesUpdateActionIT extends BaseIT {
         updateProperties.setProperty(UpdatePropertiesAction.PROPS_TO_UPDATE, propertyToUpdate);
         updateProperties.setProperty(UpdatePropertiesAction.TARGET_ID_KEY, PROFILE_TEST_ID);
         updateProperties.setProperty(UpdatePropertiesAction.TARGET_TYPE_KEY, "profile");
-        int changes = eventService.send(updateProperties);
-
-        LOGGER.info("Changes of the event : {}", changes);
+        eventService.send(updateProperties);
 
         profileToUpdate = profileService.load(PROFILE_TEST_ID);
         Assert.assertEquals("UPDATED FIRST NAME", profileToUpdate.getProperty("firstName"));
+    }
+
+    @Test
+    public void testUpdateProperties_CurrentProfile_PROPS_TO_ADD() throws InterruptedException {
+        Profile profile = profileService.load(PROFILE_TEST_ID);
+
+        Event updateProperties = new Event("updateProperties", null, profile, null, null, profile, new Date());
+        updateProperties.setPersistent(false);
+
+        Map<String, Object> propertyToAdd = new HashMap<>();
+        propertyToAdd.put("properties.prop1", "New property 1");
+        propertyToAdd.put("properties.prop2", "New property 2");
+        propertyToAdd.put("properties.prop3", "New property 3");
+
+        updateProperties.setProperty(UpdatePropertiesAction.PROPS_TO_ADD, propertyToAdd);
+        updateProperties.setProperty(UpdatePropertiesAction.TARGET_ID_KEY, PROFILE_TEST_ID);
+        updateProperties.setProperty(UpdatePropertiesAction.TARGET_TYPE_KEY, "profile");
+        eventService.send(updateProperties);
+        profileService.save(profile);
+        refreshPersistence();
+
+        profile = profileService.load(PROFILE_TEST_ID);
+        Assert.assertEquals("New property 1", profile.getProperty("prop1"));
+        Assert.assertEquals("New property 2", profile.getProperty("prop2"));
+        Assert.assertEquals("New property 3", profile.getProperty("prop3"));
+    }
+
+    @Test
+    public void testUpdateProperties_CurrentProfile_PROPS_TO_ADD_TO_SET() throws InterruptedException {
+        Profile profile = profileService.load(PROFILE_TEST_ID);
+        Event updateProperties = new Event("updateProperties", null, profile, null, null, profile, new Date());
+        updateProperties.setPersistent(false);
+
+        Map<String, Object> propertyToAddToSet = new HashMap<>();
+        propertyToAddToSet.put("properties.prop1", "New property 1");
+        propertyToAddToSet.put("properties.prop2", "New property 2");
+        propertyToAddToSet.put("properties.prop3", "New property 3");
+
+        updateProperties.setProperty(UpdatePropertiesAction.PROPS_TO_ADD, propertyToAddToSet);
+        updateProperties.setProperty(UpdatePropertiesAction.TARGET_ID_KEY, PROFILE_TEST_ID);
+        updateProperties.setProperty(UpdatePropertiesAction.TARGET_TYPE_KEY, "profile");
+        eventService.send(updateProperties);
+        profileService.save(profile);
+        refreshPersistence();
+
+        profile = profileService.load(PROFILE_TEST_ID);
+        Assert.assertEquals("New property 1", profile.getProperty("prop1"));
+        Assert.assertEquals("New property 2", profile.getProperty("prop2"));
+        Assert.assertEquals("New property 3", profile.getProperty("prop3"));
+
+        // Add set and check
+        propertyToAddToSet = new HashMap<>();
+        propertyToAddToSet.put("properties.prop1", "New property 1 bis");
+        propertyToAddToSet.put("properties.prop3", "New property 3 bis");
+
+        updateProperties = new Event("updateProperties", null, profile, null, null, profile, new Date());
+        updateProperties.setPersistent(false);
+        updateProperties.setProperty(UpdatePropertiesAction.PROPS_TO_ADD_TO_SET, propertyToAddToSet);
+        updateProperties.setProperty(UpdatePropertiesAction.TARGET_ID_KEY, PROFILE_TEST_ID);
+        updateProperties.setProperty(UpdatePropertiesAction.TARGET_TYPE_KEY, "profile");
+        eventService.send(updateProperties);
+        profileService.save(profile);
+        refreshPersistence();
+
+        profile = profileService.load(PROFILE_TEST_ID);
+        Assert.assertEquals(2, ((List<String>) profile.getProperty("prop1")).size());
+        Assert.assertEquals(2, ((List<String>) profile.getProperty("prop3")).size());
+        Assert.assertEquals("New property 1", ((List<String>) profile.getProperty("prop1")).get(1));
+        Assert.assertEquals("New property 2", profile.getProperty("prop2"));
+        Assert.assertEquals("New property 3 bis", ((List<String>) profile.getProperty("prop3")).get(0));
+    }
+
+    @Test
+    public void testUpdateProperties_CurrentProfile_PROPS_TO_DELETE() throws InterruptedException {
+        Profile profile = profileService.load(PROFILE_TEST_ID);
+        Event updateProperties = new Event("updateProperties", null, profile, null, null, profile, new Date());
+        updateProperties.setPersistent(false);
+
+        Map<String, Object> propertyToAdd = new HashMap<>();
+        propertyToAdd.put("properties.prop1", "New property 1");
+        propertyToAdd.put("properties.prop1bis", "New property 1 bis");
+        propertyToAdd.put("properties.prop2", "New property 2");
+        propertyToAdd.put("properties.prop3", "New property 3");
+
+        updateProperties.setProperty(UpdatePropertiesAction.PROPS_TO_ADD, propertyToAdd);
+        updateProperties.setProperty(UpdatePropertiesAction.TARGET_ID_KEY, PROFILE_TEST_ID);
+        updateProperties.setProperty(UpdatePropertiesAction.TARGET_TYPE_KEY, "profile");
+        eventService.send(updateProperties);
+        profileService.save(profile);
+        refreshPersistence();
+
+        profile = profileService.load(PROFILE_TEST_ID);
+        Assert.assertEquals("New property 1", profile.getProperty("prop1"));
+        Assert.assertEquals("New property 1 bis", profile.getProperty("prop1bis"));
+        Assert.assertEquals("New property 2", profile.getProperty("prop2"));
+        Assert.assertEquals("New property 3", profile.getProperty("prop3"));
+
+        // Delete property and check
+        List<String> propertyToDelete = new ArrayList<>();
+        propertyToDelete.add("properties.prop1bis");
+
+        updateProperties = new Event("updateProperties", null, profile, null, null, profile, new Date());
+        updateProperties.setPersistent(false);
+        updateProperties.setProperty(UpdatePropertiesAction.PROPS_TO_DELETE, propertyToDelete);
+        updateProperties.setProperty(UpdatePropertiesAction.TARGET_ID_KEY, PROFILE_TEST_ID);
+        updateProperties.setProperty(UpdatePropertiesAction.TARGET_TYPE_KEY, "profile");
+
+        eventService.send(updateProperties);
+        profileService.save(profile);
+        refreshPersistence();
 
+        profile = profileService.load(PROFILE_TEST_ID);
+        Assert.assertNull(profile.getProperty("prop1bis"));
+        Assert.assertEquals("New property 1", profile.getProperty("prop1"));
+        Assert.assertEquals("New property 2", profile.getProperty("prop2"));
+        Assert.assertEquals("New property 3", profile.getProperty("prop3"));
     }
 }