jkevan commented on a change in pull request #265: URL: https://github.com/apache/unomi/pull/265#discussion_r601660528
########## File path: plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/IncrementPropertyAction.java ########## @@ -0,0 +1,114 @@ +/* + * 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.plugins.baseplugin.actions; + +import org.apache.commons.beanutils.PropertyUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.unomi.api.*; +import org.apache.unomi.api.actions.Action; +import org.apache.unomi.api.actions.ActionExecutor; +import org.apache.unomi.api.services.EventService; +import org.apache.unomi.persistence.spi.PropertyHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.InvocationTargetException; +import java.util.Map; + +public class IncrementPropertyAction implements ActionExecutor { + private static final Logger logger = LoggerFactory.getLogger(IncrementPropertyAction.class.getName()); + + @Override + public int execute(final Action action, final Event event) { + boolean storeInSession = Boolean.TRUE.equals(action.getParameterValues().get("storeInSession")); + if (storeInSession && event.getSession() == null) { + return EventService.NO_CHANGE; + } + + String propertyName = (String) action.getParameterValues().get("propertyName"); + Profile profile = event.getProfile(); + Session session = event.getSession(); + + try { + Map<String, Object> properties = storeInSession ? session.getProperties() : profile.getProperties(); + Object propertyValue = getPropertyValue(action, event, propertyName, properties); + + if (PropertyHelper.setProperty(properties, propertyName, propertyValue, "alwaysSet")) { + return storeInSession ? EventService.SESSION_UPDATED : EventService.PROFILE_UPDATED; + } + } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { + logger.warn("Error resolving nested property of object. See debug log level for more information"); + if (logger.isDebugEnabled()) { + logger.error("Error resolving nested property of item: {}", storeInSession ? session : profile, e); + } + } + + return EventService.NO_CHANGE; + } + + private Object getPropertyValue(Action action, Event event, String propertyName, Map<String, Object> properties) + throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { + String propertyTarget = (String) action.getParameterValues().get("propertyTarget"); + String rootPropertyName = propertyName.split("\\.")[0]; + Object propertyValue = 1; + + Object propertyTargetValue = null; + + if (StringUtils.isNotEmpty(propertyTarget)) { + propertyTargetValue = PropertyUtils.getNestedProperty(((CustomItem) event.getTarget()).getProperties(), propertyTarget); + } + + if (propertyTargetValue != null) { + if (propertyTargetValue instanceof Integer) { + if (properties.containsKey(rootPropertyName)) { + propertyValue = (int) propertyTargetValue + (int) PropertyUtils.getNestedProperty(properties, propertyName); Review comment: Careful with cast: (int) PropertyUtils.getNestedProperty(properties, propertyName); - the PropertyUtils.getNestedProperty(properties, propertyName); can return null ? - the PropertyUtils.getNestedProperty(properties, propertyName); can return a String, if the prop exist and is a String ########## File path: plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/IncrementPropertyAction.java ########## @@ -0,0 +1,114 @@ +/* + * 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.plugins.baseplugin.actions; + +import org.apache.commons.beanutils.PropertyUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.unomi.api.*; +import org.apache.unomi.api.actions.Action; +import org.apache.unomi.api.actions.ActionExecutor; +import org.apache.unomi.api.services.EventService; +import org.apache.unomi.persistence.spi.PropertyHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.InvocationTargetException; +import java.util.Map; + +public class IncrementPropertyAction implements ActionExecutor { + private static final Logger logger = LoggerFactory.getLogger(IncrementPropertyAction.class.getName()); + + @Override + public int execute(final Action action, final Event event) { + boolean storeInSession = Boolean.TRUE.equals(action.getParameterValues().get("storeInSession")); + if (storeInSession && event.getSession() == null) { + return EventService.NO_CHANGE; + } + + String propertyName = (String) action.getParameterValues().get("propertyName"); + Profile profile = event.getProfile(); + Session session = event.getSession(); + + try { + Map<String, Object> properties = storeInSession ? session.getProperties() : profile.getProperties(); + Object propertyValue = getPropertyValue(action, event, propertyName, properties); + + if (PropertyHelper.setProperty(properties, propertyName, propertyValue, "alwaysSet")) { + return storeInSession ? EventService.SESSION_UPDATED : EventService.PROFILE_UPDATED; + } + } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { + logger.warn("Error resolving nested property of object. See debug log level for more information"); + if (logger.isDebugEnabled()) { + logger.error("Error resolving nested property of item: {}", storeInSession ? session : profile, e); + } + } + + return EventService.NO_CHANGE; + } + + private Object getPropertyValue(Action action, Event event, String propertyName, Map<String, Object> properties) + throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { + String propertyTarget = (String) action.getParameterValues().get("propertyTarget"); + String rootPropertyName = propertyName.split("\\.")[0]; + Object propertyValue = 1; + + Object propertyTargetValue = null; + + if (StringUtils.isNotEmpty(propertyTarget)) { + propertyTargetValue = PropertyUtils.getNestedProperty(((CustomItem) event.getTarget()).getProperties(), propertyTarget); + } + + if (propertyTargetValue != null) { + if (propertyTargetValue instanceof Integer) { + if (properties.containsKey(rootPropertyName)) { + propertyValue = (int) propertyTargetValue + (int) PropertyUtils.getNestedProperty(properties, propertyName); + } else { + propertyValue = propertyTargetValue; + } + } else if (propertyTargetValue instanceof Map) { + if (properties.containsKey(rootPropertyName)) { + Map<String, Object> nestedProperty = (Map<String, Object>) PropertyUtils.getNestedProperty(properties, propertyName); Review comment: We should check that PropertyUtils.getNestedProperty(properties, propertyName); is actually not null and it's a map, if not log an error and continue. ########## File path: itests/src/test/java/org/apache/unomi/itests/IncrementPropertyIT.java ########## @@ -0,0 +1,411 @@ +/* + * 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.*; +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.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 IncrementPropertyIT extends BaseIT { + private Profile profile; + private Rule rule; + private Event event; + + @Inject + @Filter(timeout = 600000) + protected ProfileService profileService; + + @Inject + @Filter(timeout = 600000) + protected EventService eventService; + + @Inject + @Filter(timeout = 600000) + protected RulesService rulesService; + + @Inject + @Filter(timeout = 600000) + protected DefinitionsService definitionsService; + + @Before + public void setup() throws Exception { + profile = createProfile(); + rule = new Rule(); + } + + @After + public void tearDown() { + rulesService.removeRule(rule.getItemId()); + profileService.delete(profile.getItemId(), false); + } + + @Test + public void testIncrementNotExistingProperty() throws InterruptedException { + int eventCode = buildAction("pageView.acme", null, null, null); + + if (eventCode == EventService.PROFILE_UPDATED) { + Profile updatedProfile = profileService.save(event.getProfile()); + refreshPersistence(); + + int value = ((Map<String, Integer>) updatedProfile.getProperty("pageView")).get("acme"); + Assert.assertEquals(1, value, 0.0); + } else { + throw new IllegalStateException("Profile was not updated"); Review comment: We should use Assert.fail("the message") instead of Exceptions when something goes wrong in the test. to be fixed on the other test cases too. ########## File path: plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/IncrementPropertyAction.java ########## @@ -0,0 +1,114 @@ +/* + * 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.plugins.baseplugin.actions; + +import org.apache.commons.beanutils.PropertyUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.unomi.api.*; +import org.apache.unomi.api.actions.Action; +import org.apache.unomi.api.actions.ActionExecutor; +import org.apache.unomi.api.services.EventService; +import org.apache.unomi.persistence.spi.PropertyHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.InvocationTargetException; +import java.util.Map; + +public class IncrementPropertyAction implements ActionExecutor { + private static final Logger logger = LoggerFactory.getLogger(IncrementPropertyAction.class.getName()); + + @Override + public int execute(final Action action, final Event event) { + boolean storeInSession = Boolean.TRUE.equals(action.getParameterValues().get("storeInSession")); + if (storeInSession && event.getSession() == null) { + return EventService.NO_CHANGE; + } + + String propertyName = (String) action.getParameterValues().get("propertyName"); + Profile profile = event.getProfile(); + Session session = event.getSession(); + + try { + Map<String, Object> properties = storeInSession ? session.getProperties() : profile.getProperties(); + Object propertyValue = getPropertyValue(action, event, propertyName, properties); + + if (PropertyHelper.setProperty(properties, propertyName, propertyValue, "alwaysSet")) { + return storeInSession ? EventService.SESSION_UPDATED : EventService.PROFILE_UPDATED; + } + } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { + logger.warn("Error resolving nested property of object. See debug log level for more information"); + if (logger.isDebugEnabled()) { + logger.error("Error resolving nested property of item: {}", storeInSession ? session : profile, e); + } + } + + return EventService.NO_CHANGE; + } + + private Object getPropertyValue(Action action, Event event, String propertyName, Map<String, Object> properties) + throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { + String propertyTarget = (String) action.getParameterValues().get("propertyTarget"); + String rootPropertyName = propertyName.split("\\.")[0]; + Object propertyValue = 1; + + Object propertyTargetValue = null; + + if (StringUtils.isNotEmpty(propertyTarget)) { + propertyTargetValue = PropertyUtils.getNestedProperty(((CustomItem) event.getTarget()).getProperties(), propertyTarget); + } + + if (propertyTargetValue != null) { + if (propertyTargetValue instanceof Integer) { + if (properties.containsKey(rootPropertyName)) { + propertyValue = (int) propertyTargetValue + (int) PropertyUtils.getNestedProperty(properties, propertyName); Review comment: I would suggest to test that the nestedProperty is not null and of type Integer. ########## File path: itests/src/test/java/org/apache/unomi/itests/IncrementPropertyIT.java ########## @@ -0,0 +1,411 @@ +/* + * 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.*; +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.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 IncrementPropertyIT extends BaseIT { + private Profile profile; + private Rule rule; + private Event event; + + @Inject + @Filter(timeout = 600000) + protected ProfileService profileService; + + @Inject + @Filter(timeout = 600000) + protected EventService eventService; + + @Inject + @Filter(timeout = 600000) + protected RulesService rulesService; + + @Inject + @Filter(timeout = 600000) + protected DefinitionsService definitionsService; + + @Before + public void setup() throws Exception { + profile = createProfile(); + rule = new Rule(); + } + + @After + public void tearDown() { + rulesService.removeRule(rule.getItemId()); + profileService.delete(profile.getItemId(), false); + } + + @Test + public void testIncrementNotExistingProperty() throws InterruptedException { + int eventCode = buildAction("pageView.acme", null, null, null); + + if (eventCode == EventService.PROFILE_UPDATED) { + Profile updatedProfile = profileService.save(event.getProfile()); + refreshPersistence(); Review comment: This is not necessary, you dont need to store the profile in test, just check that the profile from the event is correct. The save is done automatically by the ContextServlet and it is not required to insure the profile contains the correct values. You can remove this code here and also in the other test cases. ########## File path: itests/src/test/java/org/apache/unomi/itests/IncrementPropertyIT.java ########## @@ -0,0 +1,411 @@ +/* + * 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.*; +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.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 IncrementPropertyIT extends BaseIT { + private Profile profile; + private Rule rule; + private Event event; + + @Inject + @Filter(timeout = 600000) + protected ProfileService profileService; + + @Inject + @Filter(timeout = 600000) + protected EventService eventService; + + @Inject + @Filter(timeout = 600000) + protected RulesService rulesService; + + @Inject + @Filter(timeout = 600000) + protected DefinitionsService definitionsService; + + @Before + public void setup() throws Exception { + profile = createProfile(); + rule = new Rule(); + } + + @After + public void tearDown() { + rulesService.removeRule(rule.getItemId()); + profileService.delete(profile.getItemId(), false); + } + + @Test + public void testIncrementNotExistingProperty() throws InterruptedException { + int eventCode = buildAction("pageView.acme", null, null, null); + + if (eventCode == EventService.PROFILE_UPDATED) { + Profile updatedProfile = profileService.save(event.getProfile()); + refreshPersistence(); + + int value = ((Map<String, Integer>) updatedProfile.getProperty("pageView")).get("acme"); + Assert.assertEquals(1, value, 0.0); + } else { + throw new IllegalStateException("Profile was not updated"); + } + } + + @Test + public void testIncrementExistingProperty() throws InterruptedException { + Map<String, Object> properties = new HashMap<>(); + Map<String, Integer> propertyValue = new HashMap<>(); + propertyValue.put("acme", 49); + properties.put("pageView", propertyValue); + + int eventCode = buildAction("pageView.acme", null, properties, null); + + if (eventCode == EventService.PROFILE_UPDATED) { + Profile updatedProfile = profileService.save(event.getProfile()); + + refreshPersistence(); + + int value = ((Map<String, Integer>) updatedProfile.getProperty("pageView")).get("acme"); + Assert.assertEquals(50, value, 0.0); + } else { + throw new IllegalStateException("Profile was not updated"); + } + } + + @Test + public void testIncrementExistingPropertyWithExistingEventProperty() throws InterruptedException { + Map<String, Object> properties = new HashMap<>(); + Map<String, Integer> propertyValue = new HashMap<>(); + propertyValue.put("acme", 49); + properties.put("pageView", propertyValue); + + Map<String, Object> targetProperties = new HashMap<>(); + propertyValue = new HashMap<>(); + propertyValue.put("nasa", 19); + targetProperties.put("project", propertyValue); + + int eventCode = buildAction("pageView.acme", "project.nasa", properties, targetProperties); + + if (eventCode == EventService.PROFILE_UPDATED) { + Profile updatedProfile = profileService.save(event.getProfile()); + + refreshPersistence(); + + int value = ((Map<String, Integer>) updatedProfile.getProperty("pageView")).get("acme"); + Assert.assertEquals(68, value, 0.0); + } else { + throw new IllegalStateException("Profile was not updated"); + } + } + + @Test + public void testIncrementNotExistingObjectPropertyWithExistingEventObjectProperty() throws InterruptedException { + Map<String, Object> targetProperties = new HashMap<>(); + Map<String, Integer> propertyValue = new HashMap<>(); + propertyValue.put("acme", 49); + propertyValue.put("health", 18); + propertyValue.put("sport", 99); + targetProperties.put("pageView", propertyValue); + + int eventCode = buildAction("pageView", "pageView", null, targetProperties); + + if (eventCode == EventService.PROFILE_UPDATED) { + Profile updatedProfile = profileService.save(event.getProfile()); + + refreshPersistence(); + + Map<String, Integer> property = ((Map<String, Integer>) updatedProfile.getProperty("pageView")); + Assert.assertEquals(49, property.get("acme"), 0.0); + Assert.assertEquals(18, property.get("health"), 0.0); + Assert.assertEquals(99, property.get("sport"), 0.0); + } else { + throw new IllegalStateException("Profile was not updated"); + } + } + + @Test + public void testIncrementExistingObjectProperty() throws InterruptedException { + Map<String, Object> properties = new HashMap<>(); + Map<String, Integer> propertyValue = new HashMap<>(); + propertyValue.put("acme", 49); + propertyValue.put("nasa", 5); + properties.put("pageView", propertyValue); + + int eventCode = buildAction("pageView", null, properties, null); + + if (eventCode == EventService.PROFILE_UPDATED) { + Profile updatedProfile = profileService.save(event.getProfile()); + + refreshPersistence(); + + Map<String, Integer> property = ((Map<String, Integer>) updatedProfile.getProperty("pageView")); + Assert.assertEquals(50, property.get("acme"), 0.0); + Assert.assertEquals(6, property.get("nasa"), 0.0); + } else { + throw new IllegalStateException("Profile was not updated"); + } + } + + @Test + public void testIncrementExistingObjectPropertyWithExistingEventObjectProperty() throws InterruptedException { + Map<String, Object> properties = new HashMap<>(); + Map<String, Integer> propertyValue = new HashMap<>(); + propertyValue.put("acme", 49); + properties.put("pageView", propertyValue); + + Map<String, Object> targetProperties = new HashMap<>(); + Map<String, Integer> propertyValue1 = new HashMap<>(); + propertyValue1.put("acme", 31); + propertyValue1.put("health", 88); + propertyValue1.put("sport", 9); + targetProperties.put("pageView", propertyValue1); + + int eventCode = buildAction("pageView", "pageView", properties, targetProperties); + + if (eventCode == EventService.PROFILE_UPDATED) { + Profile updatedProfile = profileService.save(event.getProfile()); + + refreshPersistence(); + + Map<String, Integer> property = ((Map<String, Integer>) updatedProfile.getProperty("pageView")); + Assert.assertEquals(80, property.get("acme"), 0.0); + Assert.assertEquals(88, property.get("health"), 0.0); + Assert.assertEquals(9, property.get("sport"), 0.0); + } else { + throw new IllegalStateException("Profile was not updated"); + } + } + + @Test + public void testIncrementExistingPropertyNested() throws InterruptedException { + Map<String, Object> properties = new HashMap<>(); + Map<String, Object> properties1 = new HashMap<>(); + Map<String, Object> properties2 = new HashMap<>(); + Map<String, Integer> propertyValue = new HashMap<>(); + propertyValue.put("city", 13); + properties2.put("state", propertyValue); + properties1.put("country", properties2); + properties.put("continent", properties1); + + int eventCode = buildAction("continent.country.state.city", null, properties, null); + + if (eventCode == EventService.PROFILE_UPDATED) { + Profile updatedProfile = profileService.save(event.getProfile()); + + refreshPersistence(); + + Map<String, Integer> property = (Map<String, Integer>) ((Map<String, Object>) ((Map<String, Object>) updatedProfile.getProperty("continent")).get("country")).get("state"); + Assert.assertEquals(14, property.get("city"), 0.0); + } else { + throw new IllegalStateException("Profile was not updated"); + } + } + + @Test + public void testIncrementNotExistingPropertyNested() throws InterruptedException { + int eventCode = buildAction("continent.country.state.city", null, null, null); + + if (eventCode == EventService.PROFILE_UPDATED) { + Profile updatedProfile = profileService.save(event.getProfile()); + + refreshPersistence(); + + Map<String, Integer> property = (Map<String, Integer>) ((Map<String, Object>) ((Map<String, Object>) updatedProfile.getProperty("continent")).get("country")).get("state"); + Assert.assertEquals(1, property.get("city"), 0.0); + } else { + throw new IllegalStateException("Profile was not updated"); + } + } + + @Test + public void testIncrementExistingPropertyNestedWithExistingEventProperty() throws InterruptedException { + Map<String, Object> properties = new HashMap<>(); + Map<String, Object> properties1 = new HashMap<>(); + Map<String, Object> properties2 = new HashMap<>(); + Map<String, Integer> propertyValue = new HashMap<>(); + propertyValue.put("city", 13); + properties2.put("state", propertyValue); + properties1.put("country", properties2); + properties.put("continent", properties1); + + Map<String, Object> targetProperties = new HashMap<>(); + Map<String, Object> properties3 = new HashMap<>(); + Map<String, Object> propertyValue1 = new HashMap<>(); + propertyValue1.put("zone", 107); + properties3.put("mars", propertyValue1); + targetProperties.put("planet", properties3); + + int eventCode = buildAction("continent.country.state.city", "planet.mars.zone", properties, targetProperties); + + if (eventCode == EventService.PROFILE_UPDATED) { + Profile updatedProfile = profileService.save(event.getProfile()); + + refreshPersistence(); + + Map<String, Integer> property = (Map<String, Integer>) ((Map<String, Object>) ((Map<String, Object>) updatedProfile.getProperty("continent")).get("country")).get("state"); + Assert.assertEquals(120, property.get("city"), 0.0); + } else { + throw new IllegalStateException("Profile was not updated"); + } + } + + @Test + public void testIncrementObjectPropertyContainsStringValue() throws InterruptedException { + Map<String, Object> properties = new HashMap<>(); + Map<String, Object> propertyValue = new HashMap<>(); + propertyValue.put("books", 59); + propertyValue.put("chapters", 1001); + propertyValue.put("featured", "The forty rules"); + properties.put("library", propertyValue); + + int eventCode = buildAction("library", null, properties, null); + + if (eventCode == EventService.PROFILE_UPDATED) { + Profile updatedProfile = profileService.save(event.getProfile()); + + refreshPersistence(); + + Map<String, Object> property = ((Map<String, Object>) updatedProfile.getProperty("library")); + Assert.assertEquals(60, (int) property.get("books"), 0.0); + Assert.assertEquals(1002, (int) property.get("chapters"), 0.0); + Assert.assertEquals("The forty rules", property.get("featured")); + } else { + throw new IllegalStateException("Profile was not updated"); + } + } + + @Test + public void testIncrementObjectPropertyContainsStringValueWithExistingEventProperty() throws InterruptedException { + Map<String, Object> properties = new HashMap<>(); + Map<String, Object> propertyValue = new HashMap<>(); + propertyValue.put("books", 59); + propertyValue.put("chapters", 1001); + propertyValue.put("featured", "The forty rules"); + properties.put("library", propertyValue); + + Map<String, Object> targetProperties = new HashMap<>(); + Map<String, Object> properties1 = new HashMap<>(); + Map<String, Object> propertyValue1 = new HashMap<>(); + propertyValue1.put("books", 222); + propertyValue1.put("chapters", 2048); + propertyValue1.put("featured", "Bible"); + properties1.put("library", propertyValue1); + targetProperties.put("main", properties1); + + int eventCode = buildAction("library", "main.library", properties, targetProperties); + + if (eventCode == EventService.PROFILE_UPDATED) { + Profile updatedProfile = profileService.save(event.getProfile()); + + refreshPersistence(); + + Map<String, Object> property = ((Map<String, Object>) updatedProfile.getProperty("library")); + Assert.assertEquals(281, (int) property.get("books"), 0.0); + Assert.assertEquals(3049, (int) property.get("chapters"), 0.0); + Assert.assertEquals("The forty rules", property.get("featured")); + } else { + throw new IllegalStateException("Profile was not updated"); + } + } + + private void createRule(Action incrementPropertyAction) throws InterruptedException { + Condition condition = createCondition(); + Metadata metadata = createMetadata(); + + List<Action> actions = new ArrayList<>(); + actions.add(incrementPropertyAction); + + rule.setCondition(condition); + rule.setActions(actions); + rule.setMetadata(metadata); + rulesService.setRule(rule); + refreshPersistence(); + } + + private int buildAction(String propertyName, String propertyTargetName, Map<String, Object> properties, Map<String, Object> targetProperties) throws InterruptedException { Review comment: Would rename "buildActionAndSendEvent" -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
