Skip to content

Commit

Permalink
fix: IDP-2192 kieContainer ready logic simplified (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioT90 authored Dec 14, 2023
1 parent 85339b4 commit 42f4d01
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,40 +41,38 @@ public RuleEngineServiceImpl(OnboardingContextHolderService onboardingContextHol
public EvaluationDTO applyRules(OnboardingDTO onboardingRequest, InitiativeConfig initiative) {
log.trace("[ONBOARDING_REQUEST] [RULE_ENGINE] evaluating rules of user {} into initiative {}", onboardingRequest.getUserId(), onboardingRequest.getInitiativeId());

StatelessKieSession statelessKieSession = onboardingContextHolderService.getBeneficiaryRulesKieBase().newStatelessKieSession();

OnboardingDroolsDTO req = onboarding2OnboardingDroolsMapper.apply(onboardingRequest);

@SuppressWarnings("unchecked")
List<Command<?>> cmds = Arrays.asList(
CommandFactory.newInsert(req),
CommandFactory.newInsert(criteriaCodeService),
new AgendaGroupSetFocusCommand(req.getInitiativeId())
);

long before = System.currentTimeMillis();
statelessKieSession.execute(CommandFactory.newBatchExecution(cmds));
if(checkIfKieBaseContainerIsReady(initiative)) {
StatelessKieSession statelessKieSession = onboardingContextHolderService.getBeneficiaryRulesKieBase().newStatelessKieSession();

checkIfContainerWasReady(req, initiative);
@SuppressWarnings("unchecked")
List<Command<?>> cmds = Arrays.asList(
CommandFactory.newInsert(req),
CommandFactory.newInsert(criteriaCodeService),
new AgendaGroupSetFocusCommand(req.getInitiativeId())
);

PerformanceLogger.logTiming("ONBOARDING_RULE_ENGINE", before, "resulted into rejections %s".formatted(req.getOnboardingRejectionReasons()));

log.trace("[ONBOARDING_REQUEST] [RULE_ENGINE] Send message prepared: {}", req);

return onboarding2EvaluationMapper.apply(req, initiative, req.getOnboardingRejectionReasons());
}
long before = System.currentTimeMillis();
statelessKieSession.execute(CommandFactory.newBatchExecution(cmds));

private void checkIfContainerWasReady(OnboardingDroolsDTO req, InitiativeConfig initiative) {
if (req.getOnboardingRejectionReasons().isEmpty() && // there is not rejection reason
!CollectionUtils.isEmpty(initiative.getAutomatedCriteria()) && // the drools container is supposed to be involved
!onboardingContextHolderService.getBeneficiaryRulesKieInitiativeIds() // the initiative was not inside the container drools
.contains(initiative.getInitiativeId())
) {
PerformanceLogger.logTiming("ONBOARDING_RULE_ENGINE", before, "resulted into rejections %s".formatted(req.getOnboardingRejectionReasons()));
} else {
req.getOnboardingRejectionReasons().add(new OnboardingRejectionReason(
OnboardingRejectionReason.OnboardingRejectionReasonType.TECHNICAL_ERROR,
OnboardingConstants.REJECTION_REASON_RULE_ENGINE_NOT_READY,
null, null, null
));
}

log.trace("[ONBOARDING_REQUEST] [RULE_ENGINE] Send message prepared: {}", req);

return onboarding2EvaluationMapper.apply(req, initiative, req.getOnboardingRejectionReasons());
}

private boolean checkIfKieBaseContainerIsReady(InitiativeConfig initiative) {
return CollectionUtils.isEmpty(initiative.getAutomatedCriteria()) || // the drools container is supposed to not be involved
onboardingContextHolderService.getBeneficiaryRulesKieInitiativeIds() // the initiative is inside the container drools
.contains(initiative.getInitiativeId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,12 @@ void applyRules() {
initiativeConfig.setOrganizationId("ORGANIZATIONID");

Mockito.when(onboardingContextHolderServiceMock.getBeneficiaryRulesKieBase())
.thenReturn(buildContainer(onboardingDTO.getInitiativeId()));
.thenReturn(buildContainer(initiativeId));

// When
EvaluationDTO result = ruleEngineService.applyRules(onboardingDTO, initiativeConfig);

// Then
Mockito.verify(onboardingContextHolderServiceMock).getBeneficiaryRulesKieBase();

Assertions.assertInstanceOf(EvaluationCompletedDTO.class, result);
Assertions.assertNotNull(result.getAdmissibilityCheckDate());
Assertions.assertFalse(result.getAdmissibilityCheckDate().isAfter(LocalDateTime.now()));
Expand Down Expand Up @@ -129,8 +127,6 @@ void testNotRules_kieBasedInitiatives(){
initiativeConfig.setInitiativeId(initiativeId);
initiativeConfig.setAutomatedCriteria(List.of(AutomatedCriteriaDTO.builder().build()));

Mockito.when(onboardingContextHolderServiceMock.getBeneficiaryRulesKieBase())
.thenReturn(buildContainer(INITIATIVEID));
Mockito.when(onboardingContextHolderServiceMock.getBeneficiaryRulesKieInitiativeIds())
.thenReturn(Collections.emptySet());

Expand Down

0 comments on commit 42f4d01

Please sign in to comment.