Skip to content

Commit

Permalink
Merge pull request #447 from Onlineberatung/feat/VIC-837-Admin-can-cr…
Browse files Browse the repository at this point in the history
…eate-and-edit-team

test: add tests for AppointmentService
  • Loading branch information
PhilippFr authored Jul 25, 2022
2 parents b25849e + c351aad commit fe3d5fb
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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 =
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.<Class<ConsultantDTO>>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());
}


}

0 comments on commit fe3d5fb

Please sign in to comment.