From f01842fe6b24d547f2322b7ca921cd083b1bb7c5 Mon Sep 17 00:00:00 2001 From: Markus Eberl Date: Fri, 29 Apr 2022 11:36:58 +0200 Subject: [PATCH 1/2] fix: provide tenant id for message service call - added missing tests for tenant header supplier --- .../api/service/RejectVideoCallService.java | 2 + .../api/service/TenantHeaderSupplier.java | 9 ++- .../service/RejectVideoCallServiceTest.java | 13 ++-- .../TenantHeaderSupplierSupplierTest.java | 69 +++++++++++++++++++ 4 files changed, 83 insertions(+), 10 deletions(-) create mode 100644 src/test/java/de/caritas/cob/videoservice/api/service/TenantHeaderSupplierSupplierTest.java diff --git a/src/main/java/de/caritas/cob/videoservice/api/service/RejectVideoCallService.java b/src/main/java/de/caritas/cob/videoservice/api/service/RejectVideoCallService.java index c2004e1..b225737 100644 --- a/src/main/java/de/caritas/cob/videoservice/api/service/RejectVideoCallService.java +++ b/src/main/java/de/caritas/cob/videoservice/api/service/RejectVideoCallService.java @@ -19,6 +19,7 @@ public class RejectVideoCallService { private final @NonNull MessageControllerApi messageControllerApi; private final @NonNull SecurityHeaderSupplier securityHeaderSupplier; + private final @NonNull TenantHeaderSupplier tenantHeaderSupplier; /** * Sends a system message with rejection type to the message service. @@ -34,6 +35,7 @@ public void rejectVideoCall(RejectVideoCallDTO rejectVideoCallDto) { private void addDefaultHeaders(ApiClient apiClient) { var headers = this.securityHeaderSupplier.getKeycloakAndCsrfHttpHeaders(); + tenantHeaderSupplier.addTenantHeader(headers); headers.forEach((key, value) -> apiClient.addDefaultHeader(key, value.iterator().next())); } diff --git a/src/main/java/de/caritas/cob/videoservice/api/service/TenantHeaderSupplier.java b/src/main/java/de/caritas/cob/videoservice/api/service/TenantHeaderSupplier.java index 39f4ad8..7989075 100644 --- a/src/main/java/de/caritas/cob/videoservice/api/service/TenantHeaderSupplier.java +++ b/src/main/java/de/caritas/cob/videoservice/api/service/TenantHeaderSupplier.java @@ -2,7 +2,6 @@ import de.caritas.cob.videoservice.api.tenant.TenantContext; import java.util.Optional; -import javax.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -33,12 +32,11 @@ public void addTenantHeader(HttpHeaders headers) { /** * Resolve tenantID from current request. * - * @return + * @return the id of the tenant */ public Optional getTenantFromHeader() { - HttpServletRequest request = - ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()) - .getRequest(); + var request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()) + .getRequest(); try { return Optional.of(Long.parseLong(request.getHeader("tenantId"))); } catch (NumberFormatException exception) { @@ -46,4 +44,5 @@ public Optional getTenantFromHeader() { return Optional.empty(); } } + } diff --git a/src/test/java/de/caritas/cob/videoservice/api/service/RejectVideoCallServiceTest.java b/src/test/java/de/caritas/cob/videoservice/api/service/RejectVideoCallServiceTest.java index 96891c0..9ee0b38 100644 --- a/src/test/java/de/caritas/cob/videoservice/api/service/RejectVideoCallServiceTest.java +++ b/src/test/java/de/caritas/cob/videoservice/api/service/RejectVideoCallServiceTest.java @@ -1,7 +1,6 @@ package de.caritas.cob.videoservice.api.service; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.times; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -31,6 +30,9 @@ public class RejectVideoCallServiceTest { @Mock private SecurityHeaderSupplier securityHeaderSupplier; + @Mock + private TenantHeaderSupplier tenantHeaderSupplier; + @Mock private ApiClient apiClient; @@ -46,9 +48,10 @@ public void rejectVideoCall_Should_useServicesCorrectly() { .eventType(EventTypeEnum.IGNORED_CALL) .initiatorUserName(rejectVideoCallDto.getInitiatorUsername()) .initiatorRcUserId(rejectVideoCallDto.getInitiatorRcUserId()); - verify(this.securityHeaderSupplier, times(1)).getKeycloakAndCsrfHttpHeaders(); - verify(this.messageControllerApi, times(1)) - .createVideoHintMessage(eq(rejectVideoCallDto.getRcGroupId()), eq(expectedMessage)); + verify(this.securityHeaderSupplier).getKeycloakAndCsrfHttpHeaders(); + verify(this.tenantHeaderSupplier).addTenantHeader(any()); + verify(this.messageControllerApi) + .createVideoHintMessage(rejectVideoCallDto.getRcGroupId(), expectedMessage); } } diff --git a/src/test/java/de/caritas/cob/videoservice/api/service/TenantHeaderSupplierSupplierTest.java b/src/test/java/de/caritas/cob/videoservice/api/service/TenantHeaderSupplierSupplierTest.java new file mode 100644 index 0000000..75e3421 --- /dev/null +++ b/src/test/java/de/caritas/cob/videoservice/api/service/TenantHeaderSupplierSupplierTest.java @@ -0,0 +1,69 @@ +package de.caritas.cob.videoservice.api.service; + + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.core.Is.is; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.springframework.test.util.ReflectionTestUtils.setField; + +import de.caritas.cob.videoservice.api.tenant.TenantContext; +import javax.servlet.http.HttpServletRequest; +import org.junit.jupiter.api.Test; +import org.springframework.http.HttpHeaders; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +class TenantHeaderSupplierSupplierTest { + + private final TenantHeaderSupplier tenantHeaderSupplier = new TenantHeaderSupplier(); + + @Test + void addTenantHeader_Should_addTenantIdToHeaders_When_multiTenancyIsEnabled() { + setField(tenantHeaderSupplier, "multitenancy", true); + var httpHeaders = new HttpHeaders(); + TenantContext.setCurrentTenant(1L); + + tenantHeaderSupplier.addTenantHeader(httpHeaders); + + assertThat(httpHeaders.get("tenantId").get(0), is("1")); + } + + @Test + void addTenantHeader_Should_notAddTenantIdToHeaders_When_multiTenancyIsDisabled() { + setField(tenantHeaderSupplier, "multitenancy", false); + var httpHeaders = new HttpHeaders(); + + tenantHeaderSupplier.addTenantHeader(httpHeaders); + + assertThat(httpHeaders.get("tenantId"), nullValue()); + } + + @Test + void getTenantFromHeader_Should_returnTenantId_When_tenantIdExistsInHeader() { + givenMockedServletRequestWithTenandIdHeaderValue("1"); + + var result = tenantHeaderSupplier.getTenantFromHeader(); + + assertThat(result.isPresent(), is(true)); + assertThat(result.get(), is(1L)); + } + + @Test + void getTenantFromHeader_Should_returnOptionalEmpty_When_tenantIdIsNoNumber() { + givenMockedServletRequestWithTenandIdHeaderValue("no number"); + + var result = tenantHeaderSupplier.getTenantFromHeader(); + + assertThat(result.isPresent(), is(false)); + } + + private void givenMockedServletRequestWithTenandIdHeaderValue(String tenantIdValue) { + var mockedServletRequest = mock(HttpServletRequest.class); + when(mockedServletRequest.getHeader("tenantId")).thenReturn(tenantIdValue); + var requestAttributes = new ServletRequestAttributes(mockedServletRequest); + RequestContextHolder.setRequestAttributes(requestAttributes); + } + +} From 094525d2b365fdbcea230433514ddcc35ada5470 Mon Sep 17 00:00:00 2001 From: Markus Eberl Date: Fri, 29 Apr 2022 11:45:30 +0200 Subject: [PATCH 2/2] chore: renamed testclass and optimized assertion --- .../api/service/RejectVideoCallServiceTest.java | 7 ++++--- ...lierSupplierTest.java => TenantHeaderSupplierTest.java} | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) rename src/test/java/de/caritas/cob/videoservice/api/service/{TenantHeaderSupplierSupplierTest.java => TenantHeaderSupplierTest.java} (98%) diff --git a/src/test/java/de/caritas/cob/videoservice/api/service/RejectVideoCallServiceTest.java b/src/test/java/de/caritas/cob/videoservice/api/service/RejectVideoCallServiceTest.java index 9ee0b38..fd79d9f 100644 --- a/src/test/java/de/caritas/cob/videoservice/api/service/RejectVideoCallServiceTest.java +++ b/src/test/java/de/caritas/cob/videoservice/api/service/RejectVideoCallServiceTest.java @@ -38,9 +38,10 @@ public class RejectVideoCallServiceTest { @Test public void rejectVideoCall_Should_useServicesCorrectly() { + var securityHeaders = new HttpHeaders(); when(this.messageControllerApi.getApiClient()).thenReturn(this.apiClient); - when(this.securityHeaderSupplier.getKeycloakAndCsrfHttpHeaders()).thenReturn(new HttpHeaders()); - RejectVideoCallDTO rejectVideoCallDto = new EasyRandom().nextObject(RejectVideoCallDTO.class); + when(this.securityHeaderSupplier.getKeycloakAndCsrfHttpHeaders()).thenReturn(securityHeaders); + var rejectVideoCallDto = new EasyRandom().nextObject(RejectVideoCallDTO.class); this.rejectVideoCallService.rejectVideoCall(rejectVideoCallDto); @@ -49,7 +50,7 @@ public void rejectVideoCall_Should_useServicesCorrectly() { .initiatorUserName(rejectVideoCallDto.getInitiatorUsername()) .initiatorRcUserId(rejectVideoCallDto.getInitiatorRcUserId()); verify(this.securityHeaderSupplier).getKeycloakAndCsrfHttpHeaders(); - verify(this.tenantHeaderSupplier).addTenantHeader(any()); + verify(this.tenantHeaderSupplier).addTenantHeader(securityHeaders); verify(this.messageControllerApi) .createVideoHintMessage(rejectVideoCallDto.getRcGroupId(), expectedMessage); } diff --git a/src/test/java/de/caritas/cob/videoservice/api/service/TenantHeaderSupplierSupplierTest.java b/src/test/java/de/caritas/cob/videoservice/api/service/TenantHeaderSupplierTest.java similarity index 98% rename from src/test/java/de/caritas/cob/videoservice/api/service/TenantHeaderSupplierSupplierTest.java rename to src/test/java/de/caritas/cob/videoservice/api/service/TenantHeaderSupplierTest.java index 75e3421..bb85658 100644 --- a/src/test/java/de/caritas/cob/videoservice/api/service/TenantHeaderSupplierSupplierTest.java +++ b/src/test/java/de/caritas/cob/videoservice/api/service/TenantHeaderSupplierTest.java @@ -15,7 +15,7 @@ import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; -class TenantHeaderSupplierSupplierTest { +class TenantHeaderSupplierTest { private final TenantHeaderSupplier tenantHeaderSupplier = new TenantHeaderSupplier();