From c351aadb7aa0f9f934b5b83a1617045dc2c3d1ba Mon Sep 17 00:00:00 2001 From: PhilippFruh Date: Mon, 25 Jul 2022 16:11:31 +0200 Subject: [PATCH] test: add tests for AppointmentService --- .../appointment/AppointmentService.java | 18 ++- .../appointment/AppointmentServiceTest.java | 150 ++++++++++++++++++ 2 files changed, 164 insertions(+), 4 deletions(-) create mode 100644 src/test/java/de/caritas/cob/userservice/api/service/appointment/AppointmentServiceTest.java diff --git a/src/main/java/de/caritas/cob/userservice/api/service/appointment/AppointmentService.java b/src/main/java/de/caritas/cob/userservice/api/service/appointment/AppointmentService.java index 1a0bf1401..3f5eb86ee 100644 --- a/src/main/java/de/caritas/cob/userservice/api/service/appointment/AppointmentService.java +++ b/src/main/java/de/caritas/cob/userservice/api/service/appointment/AppointmentService.java @@ -48,8 +48,7 @@ public void createConsultant(ConsultantAdminResponseDTO consultantAdminResponseD } if (consultantAdminResponseDTO != null) { - ObjectMapper mapper = new ObjectMapper() - .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + ObjectMapper mapper = getObjectMapper(false); addTechnicalUserHeaders(this.appointmentConsultantApi.getApiClient()); try { de.caritas.cob.userservice.appointmentservice.generated.web.model.ConsultantDTO consultant = @@ -68,8 +67,7 @@ public void updateConsultant(ConsultantAdminResponseDTO consultantAdminResponseD } if (consultantAdminResponseDTO != null) { - ObjectMapper mapper = new ObjectMapper() - .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + ObjectMapper mapper = getObjectMapper(false); addTechnicalUserHeaders(this.appointmentConsultantApi.getApiClient()); try { de.caritas.cob.userservice.appointmentservice.generated.web.model.ConsultantDTO consultant = @@ -82,6 +80,18 @@ public void updateConsultant(ConsultantAdminResponseDTO consultantAdminResponseD } } + /** + * ObjectMapper provider method for injecting Mocks while testing + * + * @param failOnUnknownProperties DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES + * @return ObjectMapper + */ + protected ObjectMapper getObjectMapper(boolean failOnUnknownProperties) { + ObjectMapper mapper = new ObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, failOnUnknownProperties); + return mapper; + } + public void deleteConsultant(String consultantId) { if (!appointmentFeatureEnabled) { return; diff --git a/src/test/java/de/caritas/cob/userservice/api/service/appointment/AppointmentServiceTest.java b/src/test/java/de/caritas/cob/userservice/api/service/appointment/AppointmentServiceTest.java new file mode 100644 index 000000000..07c8fb656 --- /dev/null +++ b/src/test/java/de/caritas/cob/userservice/api/service/appointment/AppointmentServiceTest.java @@ -0,0 +1,150 @@ +package de.caritas.cob.userservice.api.service.appointment; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.nullable; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.springframework.test.util.ReflectionTestUtils.setField; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import de.caritas.cob.userservice.api.adapters.keycloak.dto.KeycloakLoginResponseDTO; +import de.caritas.cob.userservice.api.adapters.web.dto.ConsultantAdminResponseDTO; +import de.caritas.cob.userservice.api.port.out.IdentityClient; +import de.caritas.cob.userservice.api.service.httpheader.SecurityHeaderSupplier; +import de.caritas.cob.userservice.api.service.httpheader.TenantHeaderSupplier; +import de.caritas.cob.userservice.appointmentservice.generated.web.AgencyApi; +import de.caritas.cob.userservice.appointmentservice.generated.web.ConsultantApi; +import de.caritas.cob.userservice.appointmentservice.generated.web.model.ConsultantDTO; +import java.util.LinkedList; +import org.junit.Before; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentMatchers; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; +import org.slf4j.Logger; + +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) // To allow "UnnecessaryStubbing" to keep tests clean +class AppointmentServiceTest { + + private static final String FIELD_NAME_APPOINTMENTS_ENABLED = "appointmentFeatureEnabled"; + + @Spy + @InjectMocks + AppointmentService appointmentService; + + @Mock + ConsultantApi appointmentConsultantApi; + @Mock + AgencyApi appointmentAgencyApi; + @Mock + SecurityHeaderSupplier securityHeaderSupplier; + @Mock + TenantHeaderSupplier tenantHeaderSupplier; + @Mock + IdentityClient identityClient; + + @Mock + Logger log; + + @Mock + ConsultantDTO consultantDTO; + + @Mock + ConsultantAdminResponseDTO consultantAdminResponseDTO; + + @Mock + KeycloakLoginResponseDTO keycloakLoginResponseDTO; + + @Mock + org.springframework.http.HttpHeaders httpHeaders; + + @Mock + ObjectMapper objectMapper; + + @Before + public void setup() { + } + + @BeforeEach + public void beforeEach() throws JsonProcessingException { + when(identityClient.loginUser(any(), any())).thenReturn(keycloakLoginResponseDTO); + when(identityClient.loginUser(any(), any())).thenReturn(keycloakLoginResponseDTO); + when(securityHeaderSupplier.getKeycloakAndCsrfHttpHeaders(any())).thenReturn(httpHeaders); + when(consultantDTO.getId()).thenReturn("testId"); + when(objectMapper.readValue(nullable(String.class), + ArgumentMatchers.>any())).thenReturn(consultantDTO); + when(appointmentService.getObjectMapper(anyBoolean())).thenReturn(objectMapper); + } + + @Test + void createConsultant_Should_NotCallAppointmentService_WhenAppointmentsIsDisabled() { + setField(appointmentService, FIELD_NAME_APPOINTMENTS_ENABLED, false); + appointmentService.createConsultant(consultantAdminResponseDTO); + verify(appointmentConsultantApi, never()).createConsultant(any()); + verify(appointmentConsultantApi, never()).createConsultantWithHttpInfo(any()); + } + + @Test + void updateConsultant_Should_NotCallAppointmentService_WhenAppointmentsIsDisabled() { + setField(appointmentService, FIELD_NAME_APPOINTMENTS_ENABLED, false); + appointmentService.updateConsultant(consultantAdminResponseDTO); + verify(appointmentConsultantApi, never()).updateConsultant(any(), any()); + verify(appointmentConsultantApi, never()).updateConsultantWithHttpInfo(any(), any()); + } + + @Test + void deleteConsultant_Should_NotCallAppointmentService_WhenAppointmentsIsDisabled() { + setField(appointmentService, FIELD_NAME_APPOINTMENTS_ENABLED, false); + appointmentService.deleteConsultant("testId"); + verify(appointmentConsultantApi, never()).deleteConsultant(any()); + verify(appointmentConsultantApi, never()).deleteConsultantWithHttpInfo(any()); + } + + @Test + void syncAgencies_Should_NotCallAppointmentService_WhenAppointmentsIsDisabled() { + setField(appointmentService, FIELD_NAME_APPOINTMENTS_ENABLED, false); + appointmentService.syncAgencies("testId", new LinkedList<>()); + verify(appointmentAgencyApi, never()).agencyConsultantsSync(any()); + } + + @Test + void createConsultant_Should_CallAppointmentService_WhenAppointmentsIsDisabled() { + setField(appointmentService, FIELD_NAME_APPOINTMENTS_ENABLED, true); + appointmentService.createConsultant(consultantAdminResponseDTO); + verify(appointmentConsultantApi, times(1)).createConsultant(any()); + } + + @Test + void updateConsultant_Should_CallAppointmentService_WhenAppointmentsIsDisabled() { + setField(appointmentService, FIELD_NAME_APPOINTMENTS_ENABLED, true); + appointmentService.updateConsultant(consultantAdminResponseDTO); + verify(appointmentConsultantApi, times(1)).updateConsultant(any(), any()); + } + + @Test + void deleteConsultant_Should_CallAppointmentService_WhenAppointmentsIsDisabled() { + setField(appointmentService, FIELD_NAME_APPOINTMENTS_ENABLED, true); + appointmentService.deleteConsultant("testId"); + verify(appointmentConsultantApi, times(1)).deleteConsultant(any()); + } + + @Test + void syncAgencies_Should_CallAppointmentService_WhenAppointmentsIsDisabled() { + setField(appointmentService, FIELD_NAME_APPOINTMENTS_ENABLED, true); + appointmentService.syncAgencies("testId", new LinkedList<>()); + verify(appointmentAgencyApi, times(1)).agencyConsultantsSync(any()); + } + + +} \ No newline at end of file