From 6f94126de3ef109af89bdb27631f067b0f224853 Mon Sep 17 00:00:00 2001 From: "antonio.torre" Date: Thu, 14 Dec 2023 09:26:14 +0100 Subject: [PATCH 1/2] IDP-2192 kieContainer ready logic simplified --- .../evaluate/RuleEngineServiceImpl.java | 46 +++++++++---------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/src/main/java/it/gov/pagopa/admissibility/service/onboarding/evaluate/RuleEngineServiceImpl.java b/src/main/java/it/gov/pagopa/admissibility/service/onboarding/evaluate/RuleEngineServiceImpl.java index 499e91d4..5e1c8a24 100644 --- a/src/main/java/it/gov/pagopa/admissibility/service/onboarding/evaluate/RuleEngineServiceImpl.java +++ b/src/main/java/it/gov/pagopa/admissibility/service/onboarding/evaluate/RuleEngineServiceImpl.java @@ -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> 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> 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 be involved + onboardingContextHolderService.getBeneficiaryRulesKieInitiativeIds() // the initiative is inside the container drools + .contains(initiative.getInitiativeId()); } } From 187abc8d3a17abca4e68dc98ce30e742ebf4ccd3 Mon Sep 17 00:00:00 2001 From: "antonio.torre" Date: Thu, 14 Dec 2023 09:49:41 +0100 Subject: [PATCH 2/2] IDP-2192 kieContainer ready logic simplified --- .../service/onboarding/evaluate/RuleEngineServiceImpl.java | 2 +- .../onboarding/evaluate/RuleEngineServiceImplTest.java | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/java/it/gov/pagopa/admissibility/service/onboarding/evaluate/RuleEngineServiceImpl.java b/src/main/java/it/gov/pagopa/admissibility/service/onboarding/evaluate/RuleEngineServiceImpl.java index 5e1c8a24..ce1064c0 100644 --- a/src/main/java/it/gov/pagopa/admissibility/service/onboarding/evaluate/RuleEngineServiceImpl.java +++ b/src/main/java/it/gov/pagopa/admissibility/service/onboarding/evaluate/RuleEngineServiceImpl.java @@ -71,7 +71,7 @@ public EvaluationDTO applyRules(OnboardingDTO onboardingRequest, InitiativeConfi } private boolean checkIfKieBaseContainerIsReady(InitiativeConfig initiative) { - return !CollectionUtils.isEmpty(initiative.getAutomatedCriteria()) && // the drools container is supposed to be involved + 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()); } diff --git a/src/test/java/it/gov/pagopa/admissibility/service/onboarding/evaluate/RuleEngineServiceImplTest.java b/src/test/java/it/gov/pagopa/admissibility/service/onboarding/evaluate/RuleEngineServiceImplTest.java index fd45329c..7982d120 100644 --- a/src/test/java/it/gov/pagopa/admissibility/service/onboarding/evaluate/RuleEngineServiceImplTest.java +++ b/src/test/java/it/gov/pagopa/admissibility/service/onboarding/evaluate/RuleEngineServiceImplTest.java @@ -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())); @@ -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());