UNOMI-398: Implement interests increments integration tests (#225)
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 0ae2feb..da0e715 100644
--- a/itests/src/test/java/org/apache/unomi/itests/BasicIT.java
+++ b/itests/src/test/java/org/apache/unomi/itests/BasicIT.java
@@ -73,7 +73,7 @@
private static final String ITEM_ID_SITE = "/test/site";
private static final String ITEM_TYPE_VISITOR = "VISITOR";
private static final String ITEM_ID_PAGE_1 = "/test/site/page1";
- private static final String ITEM_TYPE_PAGE = "page";
+ protected static final String ITEM_TYPE_PAGE = "page";
private static final String FIRST_NAME = "firstName";
private static final String LAST_NAME = "lastName";
diff --git a/itests/src/test/java/org/apache/unomi/itests/IncrementInterestsIT.java b/itests/src/test/java/org/apache/unomi/itests/IncrementInterestsIT.java
index 9ebaa79..ba8ae25 100644
--- a/itests/src/test/java/org/apache/unomi/itests/IncrementInterestsIT.java
+++ b/itests/src/test/java/org/apache/unomi/itests/IncrementInterestsIT.java
@@ -24,11 +24,18 @@
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.Assert;
import org.junit.Test;
@@ -38,6 +45,10 @@
import org.ops4j.pax.exam.spi.reactors.PerSuite;
import org.ops4j.pax.exam.util.Filter;
+import com.sun.tools.javac.util.List;
+
+import static org.apache.unomi.itests.BasicIT.ITEM_TYPE_PAGE;
+
@RunWith(PaxExam.class)
@ExamReactorStrategy(PerSuite.class)
public class IncrementInterestsIT
@@ -56,6 +67,14 @@
@Filter(timeout = 600000)
protected TopicService topicService;
+ @Inject
+ @Filter(timeout = 600000)
+ protected RulesService rulesService;
+
+ @Inject
+ @Filter(timeout = 600000)
+ protected DefinitionsService definitionsService;
+
@Test
@SuppressWarnings("unchecked")
public void test()
@@ -85,6 +104,10 @@
Assert.assertEquals( 0.5, interests.get( topic.getTopicId() ), 0.0 );
Assert.assertFalse( interests.containsKey( "unknown" ) );
}
+ else
+ {
+ throw new IllegalStateException( "Profile was not updated" );
+ }
}
finally
{
@@ -93,6 +116,83 @@
}
}
+ @Test
+ @SuppressWarnings("unchecked")
+ public void testAction()
+ throws InterruptedException
+ {
+ final Topic topic = createTopic( "topicId" );
+ final Profile profile = createProfile();
+
+ final Action incrementAction = new Action( definitionsService.getActionType( "incrementInterestAction" ) );
+ incrementAction.setParameter( "eventInterestProperty", "eventProperty::target.properties.interests" );
+
+ final Condition condition = new Condition( definitionsService.getConditionType( "eventTypeCondition" ) );
+ condition.setParameter( "eventTypeId", "view" );
+
+ final String itemId = UUID.randomUUID().toString();
+
+ final Metadata metadata = new Metadata();
+ metadata.setId( itemId );
+ metadata.setName( itemId );
+ metadata.setDescription( itemId );
+ metadata.setEnabled( true );
+ metadata.setScope( "systemscope" );
+
+ final Rule rule = new Rule();
+
+ rule.setCondition( condition );
+ rule.setActions( List.of( incrementAction ) );
+ rule.setMetadata( metadata );
+
+ rulesService.setRule( rule );
+
+ keepTrying( "Failed waiting for the creation of the rule for the IncrementInterestsIT test",
+ () -> rulesService.getRule( rule.getItemId() ), Objects::nonNull, 1000, 100 );
+
+ final Map<String, Double> interestsAsMap = new HashMap<>();
+ interestsAsMap.put( topic.getTopicId(), 50.0 );
+ interestsAsMap.put( "unknown", 10.0 );
+
+ final Map<String, Object> properties = new HashMap<>();
+
+ properties.put( "interests", interestsAsMap );
+
+ final CustomItem item = new CustomItem( "page", ITEM_TYPE_PAGE );
+ item.setProperties( properties );
+
+ final Event event = new Event( "view", null, profile, null, null, item, new Date() );
+ event.setPersistent( false );
+
+ try
+ {
+ int eventCode = eventService.send( event );
+
+ if ( eventCode == EventService.PROFILE_UPDATED )
+ {
+ Profile updatedProfile = profileService.save( event.getProfile() );
+
+ refreshPersistence();
+
+ 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" );
+ }
+ }
+ finally
+ {
+ rulesService.removeRule( rule.getItemId() );
+
+ topicService.delete( topic.getItemId() );
+ profileService.delete( profile.getItemId(), false );
+ }
+ }
+
private Event createEvent( Profile profile, Map<String, Double> interestsAsMap )
{
final Event event = new Event( "incrementInterest", null, profile, null, null, profile, new Date() );
diff --git a/plugins/baseplugin/src/main/resources/META-INF/cxs/actions/incrementInterest.json b/plugins/baseplugin/src/main/resources/META-INF/cxs/actions/incrementInterest.json
index a328eb0..0a036e0 100644
--- a/plugins/baseplugin/src/main/resources/META-INF/cxs/actions/incrementInterest.json
+++ b/plugins/baseplugin/src/main/resources/META-INF/cxs/actions/incrementInterest.json
@@ -12,7 +12,7 @@
"parameters": [
{
"id": "eventInterestProperty",
- "type": "properties",
+ "type": "string",
"multivalued": false
}
]
diff --git a/plugins/baseplugin/src/main/resources/META-INF/cxs/rules/incrementInterest.json b/plugins/baseplugin/src/main/resources/META-INF/cxs/rules/incrementInterest.json
index ad002f3..f289aa3 100644
--- a/plugins/baseplugin/src/main/resources/META-INF/cxs/rules/incrementInterest.json
+++ b/plugins/baseplugin/src/main/resources/META-INF/cxs/rules/incrementInterest.json
@@ -14,7 +14,6 @@
{
"type": "incrementInterestAction",
"parameterValues": {
- "eventInterestProperty": "target.properties.interests"
}
}
]