Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: IDP-2192 kieContainer ready logic simplified #238

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
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 @@ -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
Loading