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: Test coverage update #10

Merged
merged 1 commit into from
Nov 26, 2024
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 @@ -103,7 +103,13 @@ public Mono<List<String>> getTppEnabledList(String fiscalCode) {
.filter(tpp -> tpp.getValue().getTppState())
.map(Map.Entry::getKey)
.toList())
.doOnSuccess(tppIdList -> log.info("EMD][CITIZEN][FIND-CITIZEN-CONSENTS-ENABLED] Consents found: {}", (tppIdList.size())));
.doOnSuccess(tppIdList -> {
if (tppIdList != null) {
log.info("EMD][CITIZEN][FIND-CITIZEN-CONSENTS-ENABLED] Consents found: {}", (tppIdList.size()));
} else {
log.info("EMD][CITIZEN][FIND-CITIZEN-CONSENTS-ENABLED] No consents found.");
}
});
}

@Override
Expand All @@ -128,7 +134,13 @@ public Mono<CitizenConsentDTO> getCitizenConsentsListEnabled(String fiscalCode)

return mapperToDTO.map(citizenConsent);
})
.doOnSuccess(citizenConsent -> log.info("EMD][CITIZEN][FIND-CITIZEN-CONSENTS-ENABLED] Consents found: {}", citizenConsent.getConsents().size()));
.doOnSuccess(citizenConsent -> {
if (citizenConsent != null && citizenConsent.getConsents() != null) {
log.info("EMD][CITIZEN][FIND-CITIZEN-CONSENTS-ENABLED] Consents found: {}", citizenConsent.getConsents().size());
} else {
log.info("EMD][CITIZEN][FIND-CITIZEN-CONSENTS-ENABLED] No consents found.");
}
});


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,41 @@ void getAllFiscalCode_NoChannelsEnabled() {
Assertions.assertEquals("NO CHANNELS ENABLED", resultResponse);
});
}

@Test
void getCitizenConsentsListEnabled_ShouldReturnCitizenConsent() {
CitizenConsentDTO mockConsent = CitizenConsentDTOFaker.mockInstance(true);
Mockito.when(citizenService.getCitizenConsentsListEnabled(FISCAL_CODE))
.thenReturn(Mono.just(mockConsent));

webClient.get()
.uri("/emd/citizen/list/{fiscalCode}/enabled", FISCAL_CODE)
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isOk()
.expectBody(CitizenConsentDTO.class)
.value(response -> {
assert response != null;
Assertions.assertEquals(response, mockConsent);
});

}

@Test
void getCitizenEnabled_ShouldReturnListOfCitizens() {
String tppId = "TPP123";
List<CitizenConsentDTO> mockConsents = List.of(CitizenConsentDTOFaker.mockInstance(true));
Mockito.when(citizenService.getCitizenEnabled(tppId))
.thenReturn(Mono.just(mockConsents));

webClient.get()
.uri("/emd/citizen/{tppId}", tppId)
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isOk()
.expectBodyList(CitizenConsentDTO.class)
.value(response -> Assertions.assertEquals(1, response.size()));

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

Expand Down Expand Up @@ -263,4 +264,69 @@ void get_Ok() {
assertEquals(CITIZEN_CONSENT.getFiscalCode(), response.getFiscalCode());
}

@Test
void getCitizenConsentsListEnabled_Ok() {
Map<String, ConsentDetails> consents = new HashMap<>();
consents.put("Tpp1", ConsentDetails.builder().tppState(true).tcDate(LocalDateTime.now()).build());
consents.put("Tpp2", ConsentDetails.builder().tppState(false).tcDate(LocalDateTime.now()).build());

CitizenConsent citizenConsent = CitizenConsent.builder()
.fiscalCode(FISCAL_CODE)
.consents(consents)
.build();

CitizenConsentDTO expectedDTO = dtoMapper.map(citizenConsent);
expectedDTO.getConsents().remove("Tpp2"); // Filter out disabled TPP

when(citizenRepository.findByFiscalCode(FISCAL_CODE)).thenReturn(Mono.just(citizenConsent));

CitizenConsentDTO response = citizenService.getCitizenConsentsListEnabled(FISCAL_CODE).block();

assertNotNull(response);
assertEquals(1, response.getConsents().size());
assertTrue(response.getConsents().containsKey("Tpp1"));
assertFalse(response.getConsents().containsKey("Tpp2"));
}

@Test
void getCitizenConsentsListEnabled_Empty() {
when(citizenRepository.findByFiscalCode(FISCAL_CODE)).thenReturn(Mono.empty());

CitizenConsentDTO response = citizenService.getCitizenConsentsListEnabled(FISCAL_CODE).block();

assertNull(response);
}

@Test
void getCitizenEnabled_Ok() {
CitizenConsent citizenConsent1 = CitizenConsent.builder()
.fiscalCode("FiscalCode1")
.consents(Map.of(TPP_ID, ConsentDetails.builder().tppState(true).tcDate(LocalDateTime.now()).build()))
.build();

CitizenConsent citizenConsent2 = CitizenConsent.builder()
.fiscalCode("FiscalCode2")
.consents(Map.of(TPP_ID, ConsentDetails.builder().tppState(true).tcDate(LocalDateTime.now()).build()))
.build();

when(citizenRepository.findByTppIdEnabled(TPP_ID)).thenReturn(Flux.just(citizenConsent1, citizenConsent2));

List<CitizenConsentDTO> response = citizenService.getCitizenEnabled(TPP_ID).block();

assertNotNull(response);
assertEquals(2, response.size());
assertEquals("FiscalCode1", response.get(0).getFiscalCode());
assertEquals("FiscalCode2", response.get(1).getFiscalCode());
}

@Test
void getCitizenEnabled_Empty() {
when(citizenRepository.findByTppIdEnabled(TPP_ID)).thenReturn(Flux.empty());

List<CitizenConsentDTO> response = citizenService.getCitizenEnabled(TPP_ID).block();

assertNotNull(response);
assertTrue(response.isEmpty());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;

Expand Down Expand Up @@ -55,10 +55,6 @@ void handleExistingConsent_ConsentAlreadyExists() {

when(dtoMapper.map(existingConsent)).thenReturn(CITIZEN_CONSENT_DTO);

when(citizenRepository.findAll()).thenReturn(Flux.just(CitizenConsentFaker.mockInstance(true)));

doNothing().when(bloomFilterService).add(anyString());

CitizenConsentDTO result = validationService.handleExistingConsent(existingConsent, tppId, CITIZEN_CONSENT).block();

assertNotNull(result);
Expand All @@ -76,10 +72,6 @@ void handleExistingConsent_NewConsentForTpp() {
when(citizenRepository.save(existingConsent)).thenReturn(Mono.just(existingConsent));
when(dtoMapper.map(existingConsent)).thenReturn(CITIZEN_CONSENT_DTO);

when(citizenRepository.findAll()).thenReturn(Flux.just(CitizenConsentFaker.mockInstance(true)));

doNothing().when(bloomFilterService).add(anyString());

CitizenConsentDTO result = validationService.handleExistingConsent(existingConsent, activeTppDTO.getTppId(), CITIZEN_CONSENT).block();

assertNotNull(result);
Expand All @@ -103,10 +95,6 @@ void validateTppAndSaveConsent_TppValidAndActive() {
when(citizenRepository.save(CITIZEN_CONSENT)).thenReturn(Mono.just(CITIZEN_CONSENT));
when(dtoMapper.map(CITIZEN_CONSENT)).thenReturn(CITIZEN_CONSENT_DTO);

when(citizenRepository.findAll()).thenReturn(Flux.just(CitizenConsentFaker.mockInstance(true)));

doNothing().when(bloomFilterService).add(anyString());

CitizenConsentDTO result = validationService.validateTppAndSaveConsent(fiscalCode, activeTppDTO.getTppId(), CITIZEN_CONSENT).block();

assertNotNull(result);
Expand Down
Loading