From f251b5681aebb2696f02e6bb01127b8f623a5be7 Mon Sep 17 00:00:00 2001 From: aalicic Date: Fri, 4 Feb 2022 00:49:11 +0100 Subject: [PATCH] VIC-35: Make all data accessible per tenant fix tests --- .../service/ConsultingTypeServiceTest.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/test/java/de/caritas/cob/agencyservice/api/service/ConsultingTypeServiceTest.java b/src/test/java/de/caritas/cob/agencyservice/api/service/ConsultingTypeServiceTest.java index 979b95ea..c984ae80 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/service/ConsultingTypeServiceTest.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/service/ConsultingTypeServiceTest.java @@ -7,12 +7,16 @@ import de.caritas.cob.agencyservice.api.service.securityheader.SecurityHeaderSupplier; import de.caritas.cob.agencyservice.consultingtypeservice.generated.ApiClient; import de.caritas.cob.agencyservice.consultingtypeservice.generated.web.ConsultingTypeControllerApi; -import org.junit.jupiter.api.Test; +import java.util.Enumeration; +import javax.servlet.http.HttpServletRequest; +import org.junit.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.http.HttpHeaders; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; @ExtendWith(MockitoExtension.class) class ConsultingTypeServiceTest { @@ -26,14 +30,36 @@ class ConsultingTypeServiceTest { @Mock private SecurityHeaderSupplier securityHeaderSupplier; + @Mock + private ServletRequestAttributes requestAttributes; + + @Mock + private HttpServletRequest httpServletRequest; + + @Mock + private Enumeration headers; + @Test void getExtendedConsultingTypeResponseDTO_Should_useServicesCorrectly() { + givenRequestContextIsSet(); when(this.consultingTypeControllerApi.getApiClient()).thenReturn(new ApiClient()); when(this.securityHeaderSupplier.getCsrfHttpHeaders()).thenReturn(new HttpHeaders()); this.consultingTypeService.getExtendedConsultingTypeResponseDTO(0); verify(this.consultingTypeControllerApi, times(1)).getExtendedConsultingTypeById(0); + resetRequestAttributes(); } + private void resetRequestAttributes() { + RequestContextHolder.setRequestAttributes(null); + } + + private void givenRequestContextIsSet() { + when(requestAttributes.getRequest()).thenReturn(httpServletRequest); + when(httpServletRequest.getHeaderNames()).thenReturn(headers); + RequestContextHolder.setRequestAttributes(requestAttributes); + } + + }