Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import org.apache.unomi.api.Metadata;
import org.apache.unomi.api.actions.ActionType;
import org.apache.unomi.api.conditions.Condition;
import org.apache.unomi.api.conditions.ConditionType;
import org.apache.unomi.api.goals.Goal;
import org.apache.unomi.api.services.ConditionValidationService;
import org.apache.unomi.api.services.EventService;
import org.apache.unomi.api.services.RulesService;
import org.apache.unomi.api.services.SchedulerService;
Expand All @@ -36,6 +36,7 @@
import org.apache.unomi.services.impl.cache.MultiTypeCacheServiceImpl;
import org.apache.unomi.services.impl.definitions.DefinitionsServiceImpl;
import org.apache.unomi.services.impl.events.EventServiceImpl;
import org.apache.unomi.services.impl.rules.RulesServiceImpl;
import org.apache.unomi.services.impl.rules.TestActionExecutorDispatcher;
import org.apache.unomi.services.impl.scheduler.SchedulerServiceImpl;
import org.apache.unomi.tracing.api.TracerService;
Expand All @@ -52,6 +53,8 @@
import java.io.IOException;
import java.util.*;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class GoalsServiceImplTest {
Expand All @@ -66,6 +69,7 @@ public class GoalsServiceImplTest {
private EventServiceImpl eventService;
private SchedulerService schedulerService;
private MultiTypeCacheServiceImpl multiTypeCacheService;
private TestEventAdmin testEventAdmin;
@Mock
private BundleContext bundleContext;

Expand All @@ -74,6 +78,7 @@ public void setUp() throws IOException {

TracerService tracerService = TestHelper.createTracerService();
tenantService = new TestTenantService();
TestHelper.setupCommonTestData(tenantService);
tenantService.setCurrentTenantId("test-tenant");
ConditionEvaluatorDispatcher conditionEvaluatorDispatcher = TestConditionEvaluators.createDispatcher();
securityService = TestHelper.createSecurityService();
Expand All @@ -90,7 +95,7 @@ public void setUp() throws IOException {
TestHelper.createDefinitionServiceWithEventAdmin(persistenceService, bundleContext, schedulerService,
multiTypeCacheService, executionContextManager, tenantService);
definitionsService = servicePair.getKey();
TestEventAdmin testEventAdmin = servicePair.getValue();
testEventAdmin = servicePair.getValue();

// Inject definitionsService into the dispatcher
TestHelper.injectDefinitionsServiceIntoDispatcher(conditionEvaluatorDispatcher, definitionsService);
Expand Down Expand Up @@ -144,14 +149,21 @@ public Long getVersion() {

@AfterEach
public void tearDown() throws Exception {
// Shutdown TestEventAdmin if it exists (created via createDefinitionServiceWithEventAdmin)
// Note: TestEventAdmin is created internally by createDefinitionService, but we only have access
// to it when using createDefinitionServiceWithEventAdmin. For now, we rely on JVM cleanup.

// Stop scheduler service
if (schedulerService != null && schedulerService instanceof SchedulerServiceImpl) {
if (testEventAdmin != null) {
testEventAdmin.shutdown();
}
if (rulesService != null) {
((RulesServiceImpl) rulesService).preDestroy();
}
if (eventService != null && rulesService != null) {
eventService.removeEventListenerService((RulesServiceImpl) rulesService);
}
if (schedulerService instanceof SchedulerServiceImpl) {
((SchedulerServiceImpl) schedulerService).preDestroy();
}
if (executionContextManager != null) {
executionContextManager.setCurrentContext(null);
}

// Clear cache by clearing each tenant
if (multiTypeCacheService != null) {
Expand Down Expand Up @@ -181,6 +193,7 @@ public void tearDown() throws Exception {
eventService = null;
schedulerService = null;
multiTypeCacheService = null;
testEventAdmin = null;
bundleContext = null;
}

Expand Down Expand Up @@ -247,33 +260,37 @@ public void testSetGoalWithValidEvents() {

@Test
public void testSetGoalWithNestedConditions() {
// Create a goal with nested conditions
Goal goal = new Goal();
goal.setMetadata(new Metadata());
goal.getMetadata().setId("testGoal");
goal.getMetadata().setEnabled(true);

// Create parent condition
Condition parentCondition = new Condition();
parentCondition.setConditionTypeId("booleanCondition");
parentCondition.setConditionType(definitionsService.getConditionType("booleanCondition"));
parentCondition.setParameter("operator", "and");

// Create child condition
Condition childCondition = new Condition();
childCondition.setConditionTypeId("profilePropertyCondition");
childCondition.setConditionType(definitionsService.getConditionType("profilePropertyCondition"));
childCondition.setParameter("propertyName", "profileProperty");
childCondition.setParameter("comparisonOperator", "exists");

// Set up nested structure
List<Condition> subConditions = new ArrayList<>();
subConditions.add(childCondition);
parentCondition.setParameter("subConditions", subConditions);

goal.setStartEvent(parentCondition);

// Should not throw any exceptions
goalsService.setGoal(goal);
executionContextManager.executeAsSystem(() -> {
Goal goal = new Goal();
Metadata metadata = new Metadata();
metadata.setId("testGoal");
metadata.setEnabled(true);
goal.setMetadata(metadata);
goal.setItemId("testGoal");

ConditionType booleanConditionType = definitionsService.getConditionType("booleanCondition");
assertNotNull(booleanConditionType, "Boolean condition type should exist");
ConditionType profilePropertyConditionType = definitionsService.getConditionType("profilePropertyCondition");
assertNotNull(profilePropertyConditionType, "Profile property condition type should exist");

Condition parentCondition = new Condition();
parentCondition.setConditionTypeId("booleanCondition");
parentCondition.setConditionType(booleanConditionType);
parentCondition.setParameter("operator", "and");

Condition childCondition = new Condition();
childCondition.setConditionTypeId("profilePropertyCondition");
childCondition.setConditionType(profilePropertyConditionType);
childCondition.setParameter("propertyName", "profileProperty");
childCondition.setParameter("comparisonOperator", "exists");

List<Condition> subConditions = new ArrayList<>();
subConditions.add(childCondition);
parentCondition.setParameter("subConditions", subConditions);

goal.setStartEvent(parentCondition);
goalsService.setGoal(goal);
return null;
});
}
}
Loading