Skip to content

Commit

Permalink
chore: logging drools rules deleted by command (#239)
Browse files Browse the repository at this point in the history
* logging drools rules deleted by command

* logging drools rules deleted by command

* logging drools rules deleted by command
  • Loading branch information
antonioT90 authored Dec 14, 2023
1 parent 42f4d01 commit eb20c3b
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package it.gov.pagopa.admissibility.connector.repository;

import it.gov.pagopa.admissibility.model.DroolsRule;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import it.gov.pagopa.common.reactive.mongo.ReactiveMongoRepositoryExt;

/**
* it will handle the persistence of {@link it.gov.pagopa.admissibility.model.DroolsRule} entity*/
public interface DroolsRuleRepository extends ReactiveMongoRepository<DroolsRule, String> {
public interface DroolsRuleRepository extends ReactiveMongoRepositoryExt<DroolsRule, String> {
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package it.gov.pagopa.admissibility.connector.repository;

import it.gov.pagopa.admissibility.model.InitiativeCounters;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import it.gov.pagopa.common.reactive.mongo.ReactiveMongoRepositoryExt;

/**
* it will handle the persistence of {@link InitiativeCounters} entity*/
public interface InitiativeCountersRepository extends ReactiveMongoRepository<InitiativeCounters, String>, InitiativeCountersReservationOpsRepository {
public interface InitiativeCountersRepository extends ReactiveMongoRepositoryExt<InitiativeCounters, String>, InitiativeCountersReservationOpsRepository {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.time.LocalDateTime;

@Slf4j
@Data
@AllArgsConstructor
Expand All @@ -23,4 +25,5 @@ public class DroolsRule {
private String rule;
private String ruleVersion;
private InitiativeConfig initiativeConfig;
private LocalDateTime updateDate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.time.LocalDateTime;
import java.util.stream.Collectors;

@Service
Expand Down Expand Up @@ -49,6 +50,7 @@ public DroolsRule apply(Initiative2BuildDTO initiative) {
out.setId(initiative.getInitiativeId());
out.setName(initiative.getInitiativeName());
out.setRuleVersion("20230404");
out.setUpdateDate(LocalDateTime.now());

out.setRule("""
package %s;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,21 @@ private Mono<?> execAndLogTiming(String deleteFlowName, String initiativeId, Mon
}

private Mono<Void> deleteDroolsRule(String initiativeId) {
return droolsRuleRepository.deleteById(initiativeId)
.doOnSuccess(d -> {
log.info("[DELETE_INITIATIVE] Deleted initiative {} from collection: beneficiary_rule", initiativeId);
return droolsRuleRepository.removeById(initiativeId)
.doOnNext(d -> {
log.info("[DELETE_INITIATIVE] Deleted {} initiative {} from collection: beneficiary_rule", d.getDeletedCount(), initiativeId);
auditUtilities.logDeletedDroolsRule(initiativeId);
})
.then();
}

private Mono<Void> deleteInitiativeCounters(String initiativeId) {
return initiativeCountersRepository.deleteById(initiativeId)
.doOnSuccess(i -> {
log.info("[DELETE_INITIATIVE] Deleted initiative {} from collection: initiative_counters", initiativeId);
return initiativeCountersRepository.removeById(initiativeId)
.doOnNext(i -> {
log.info("[DELETE_INITIATIVE] Deleted {} initiative {} from collection: initiative_counters", i.getDeletedCount(), initiativeId);
auditUtilities.logDeletedInitiativeCounters(initiativeId);
});
})
.then();
}

private Mono<Void> deleteOnboardingFamilies(String initiativeId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ private void acceptNewKieBase(KieBase newKieBase) {
preLoadKieBase(newKieBase);
this.kieBase = newKieBase;
this.kieInitiatives = readKieInitiatives(newKieBase);
log.info("[BENEFICIARY_RULE_CONTAINER_LOAD] Rule engine rules loaded: {}", kieInitiatives);
}

private Set<String> readKieInitiatives(KieBase kieBase) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package it.gov.pagopa.common.reactive.mongo;

import com.mongodb.client.result.DeleteResult;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.data.repository.NoRepositoryBean;
import reactor.core.publisher.Mono;

@NoRepositoryBean
public interface ReactiveMongoRepositoryExt<T, ID> extends ReactiveMongoRepository<T, ID> {
Mono<DeleteResult> removeById(ID id);
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package it.gov.pagopa.common.reactive.mongo;

import com.mongodb.client.result.DeleteResult;
import lombok.NonNull;
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.mongodb.repository.support.SimpleReactiveMongoRepository;
import org.springframework.util.Assert;
import reactor.core.publisher.Mono;

import java.io.Serializable;

public class ReactiveMongoRepositoryImpl<E, I extends Serializable> extends SimpleReactiveMongoRepository<E, I> {
import static org.springframework.data.mongodb.core.query.Criteria.where;

public class ReactiveMongoRepositoryImpl<E, I extends Serializable> extends SimpleReactiveMongoRepository<E, I> implements ReactiveMongoRepositoryExt<E, I> {

private final ReactiveMongoOperations mongoOperations;
private final MongoEntityInformation<E, I> entityInformation;
Expand All @@ -29,4 +33,22 @@ public ReactiveMongoRepositoryImpl(MongoEntityInformation<E, I> entityInformatio
entityInformation.getJavaType(), entityInformation.getCollectionName()).singleOrEmpty();
}

@Override
public Mono<DeleteResult> removeById(I id) {
Assert.notNull(id, "The given id must not be null");

return mongoOperations
.remove(getIdQuery(id), entityInformation.getJavaType(), entityInformation.getCollectionName());
}

@SuppressWarnings("squid:S2177") // suppressing overriding private super method
private Query getIdQuery(Object id) {
return new Query(getIdCriteria(id));
}

@SuppressWarnings("squid:S2177") // suppressing overriding private super method
private Criteria getIdCriteria(Object id) {
return where(entityInformation.getIdAttribute()).is(id);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ void test() {
checkRepositories();
checkErrorsPublished(notValidMessages, maxWaitingMs, errorUseCases);

Mockito.verify(droolsRuleRepositorySpy).findAll();

System.out.printf("""
************************
Time spent to send %d (%d + %d) messages (from start): %d millis
Expand Down Expand Up @@ -188,7 +186,7 @@ protected Pattern getErrorUseCaseIdPatternMatch() {
errorUseCases.add(Pair.of(
() -> {
Mockito.doThrow(new MongoException("Command error dummy"))
.when(droolsRuleRepositorySpy).deleteById(errorInitiativeId);
.when(droolsRuleRepositorySpy).removeById(errorInitiativeId);
return commandOperationErrorString;
},
errorMessage -> checkErrorMessageHeaders(errorMessage, "[ADMISSIBILITY_COMMANDS] An error occurred evaluating commands", commandOperationErrorString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private void checkResult(DroolsRule result, Initiative2BuildDTO dto) {
.isLogoPresent(Boolean.TRUE)
.beneficiaryType(InitiativeGeneralDTO.BeneficiaryTypeEnum.PF)
.build());

expected.setUpdateDate(result.getUpdateDate());
expected.setRuleVersion("20230404");

Assertions.assertEquals(expected, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -61,7 +62,8 @@ void configureMocks() {
.automatedCriteriaCodes(List.of("CODE"))
.initiativeBudget(i.getGeneral().getBudget())
.beneficiaryInitiativeBudget(i.getGeneral().getBeneficiaryBudget())
.build());
.build(),
LocalDateTime.now());
});
Mockito.when(droolsRuleRepositoryMock.save(Mockito.any())).thenAnswer(invocation -> Mono.just(invocation.getArgument(0)));
Mockito.when(kieContainerBuilderServiceMock.buildAll()).thenReturn(Mono.just(newKieBaseBuiltMock));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.gov.pagopa.admissibility.service.commands.operations;

import com.mongodb.MongoException;
import com.mongodb.client.result.DeleteResult;
import it.gov.pagopa.admissibility.connector.repository.DroolsRuleRepository;
import it.gov.pagopa.admissibility.connector.repository.InitiativeCountersRepository;
import it.gov.pagopa.admissibility.connector.repository.OnboardingFamiliesRepository;
Expand Down Expand Up @@ -43,11 +44,11 @@ void executeOK() {
String initiativeId = "INITIATIVEID";
String familyid = "FAMILYID";

Mockito.when(droolsRuleRepositoryMock.deleteById(initiativeId))
.thenReturn(Mono.just(Mockito.mock(Void.class)));
Mockito.when(droolsRuleRepositoryMock.removeById(initiativeId))
.thenReturn(Mono.just(Mockito.mock(DeleteResult.class)));

Mockito.when(initiativeCountersRepositoryMock.deleteById(initiativeId))
.thenReturn(Mono.just(Mockito.mock(Void.class)));
Mockito.when(initiativeCountersRepositoryMock.removeById(initiativeId))
.thenReturn(Mono.just(Mockito.mock(DeleteResult.class)));

Family family = Family.builder()
.familyId(familyid)
Expand All @@ -66,8 +67,8 @@ void executeOK() {

Assertions.assertNotNull(result);

Mockito.verify(droolsRuleRepositoryMock, Mockito.times(1)).deleteById(Mockito.anyString());
Mockito.verify(initiativeCountersRepositoryMock, Mockito.times(1)).deleteById(Mockito.anyString());
Mockito.verify(droolsRuleRepositoryMock, Mockito.times(1)).removeById(Mockito.anyString());
Mockito.verify(initiativeCountersRepositoryMock, Mockito.times(1)).removeById(Mockito.anyString());
Mockito.verify(onboardingFamiliesRepositoryMock, Mockito.times(1)).findByInitiativeIdWithBatch(Mockito.anyString(),Mockito.anyInt());
Mockito.verify(onboardingFamiliesRepositoryMock, Mockito.times(1)).deleteById(Mockito.anyString());

Expand All @@ -76,14 +77,14 @@ void executeOK() {
@Test
void executeError() {
String initiativeId = "INITIATIVEID";
Mockito.when(droolsRuleRepositoryMock.deleteById(initiativeId))
Mockito.when(droolsRuleRepositoryMock.removeById(initiativeId))
.thenThrow(new MongoException("DUMMY_EXCEPTION"));

try{
deleteInitiativeService.execute(initiativeId).block();
Assertions.fail();
}catch (Throwable t){
Assertions.assertTrue(t instanceof MongoException);
}catch (MongoException t){
// Do Nothing
}
}
}

0 comments on commit eb20c3b

Please sign in to comment.