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

staging #140

Merged
merged 11 commits into from
Aug 29, 2024
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<commons-lang3.version>3.11</commons-lang3.version>
<liquibase-maven-plugin.version>4.23.2</liquibase-maven-plugin.version>
<h2.version>1.4.200</h2.version>
<powermock-module-junit4.version>2.0.9</powermock-module-junit4.version>
<ehcache.version>2.10.6</ehcache.version>
<easy-random-core.version>5.0.0</easy-random-core.version>
<spring-boot-autoconfigure.version>3.0.6</spring-boot-autoconfigure.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import de.caritas.cob.agencyservice.api.repository.agency.Agency;
import de.caritas.cob.agencyservice.api.repository.agencytopic.AgencyTopic;
import de.caritas.cob.agencyservice.api.service.TopicService;
import de.caritas.cob.agencyservice.api.tenant.TenantContext;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package de.caritas.cob.agencyservice.api.service;

import de.caritas.cob.agencyservice.api.service.securityheader.SecurityHeaderSupplier;
import de.caritas.cob.agencyservice.config.CacheManagerConfig;
import de.caritas.cob.agencyservice.config.apiclient.TopicServiceApiControllerFactory;
import de.caritas.cob.agencyservice.topicservice.generated.ApiClient;
import de.caritas.cob.agencyservice.topicservice.generated.web.TopicControllerApi;
import de.caritas.cob.agencyservice.topicservice.generated.web.model.TopicDTO;
import java.util.List;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class TopicCachingService {

private final @NonNull TopicServiceApiControllerFactory topicServiceApiControllerFactory;
private final @NonNull SecurityHeaderSupplier securityHeaderSupplier;
private final @NonNull TenantHeaderSupplier tenantHeaderSupplier;

@Cacheable(cacheNames = CacheManagerConfig.TOPICS_CACHE)
public List<TopicDTO> getAllTopics(Long tenantId) {
// tenantId parameter is just used for caching per tenant, it is not required for the actual implementation, because it will be added by the tenantHeaderSupplier
TopicControllerApi controllerApi = topicServiceApiControllerFactory.createControllerApi();
addDefaultHeaders(controllerApi.getApiClient());
return controllerApi.getAllTopics();
}

private void addDefaultHeaders(ApiClient apiClient) {
var headers = this.securityHeaderSupplier.getKeycloakAndCsrfHttpHeaders();
tenantHeaderSupplier.addTenantHeader(headers);
headers.forEach((key, value) -> apiClient.addDefaultHeader(key, value.iterator().next()));
}

}
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
package de.caritas.cob.agencyservice.api.service;

import de.caritas.cob.agencyservice.api.service.securityheader.SecurityHeaderSupplier;
import de.caritas.cob.agencyservice.config.CacheManagerConfig;
import de.caritas.cob.agencyservice.config.apiclient.TopicServiceApiControllerFactory;
import de.caritas.cob.agencyservice.topicservice.generated.web.TopicControllerApi;
import de.caritas.cob.agencyservice.api.tenant.TenantContext;

import java.util.List;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import de.caritas.cob.agencyservice.topicservice.generated.web.model.TopicDTO;
import de.caritas.cob.agencyservice.topicservice.generated.ApiClient;

@Service
@RequiredArgsConstructor
public class TopicService {

private final @NonNull TopicServiceApiControllerFactory topicServiceApiControllerFactory;
private final @NonNull SecurityHeaderSupplier securityHeaderSupplier;
private final @NonNull TenantHeaderSupplier tenantHeaderSupplier;
private final @NonNull TopicCachingService topicCachingService;

public List<TopicDTO> getAllTopics() {
TopicControllerApi controllerApi = topicServiceApiControllerFactory.createControllerApi();
addDefaultHeaders(controllerApi.getApiClient());
return controllerApi.getAllTopics();
}

private void addDefaultHeaders(ApiClient apiClient) {
var headers = this.securityHeaderSupplier.getKeycloakAndCsrfHttpHeaders();
tenantHeaderSupplier.addTenantHeader(headers);
headers.forEach((key, value) -> apiClient.addDefaultHeader(key, value.iterator().next()));
public List<TopicDTO> getAllTopics() {
return topicCachingService.getAllTopics(TenantContext.getCurrentTenant());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

import de.caritas.cob.agencyservice.tenantservice.generated.ApiClient;
import de.caritas.cob.agencyservice.tenantservice.generated.web.TenantControllerApi;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Component
public class TenantServiceApiControllerFactory {

private static final String DEFAULT_LANGUAGE = "de";
private static final String LANGUAGE_COOKIE_NAME = "lang";
@Value("${tenant.service.api.url}")
private String tenantServiceApiUrl;

Expand All @@ -18,6 +26,27 @@ public class TenantServiceApiControllerFactory {

public TenantControllerApi createControllerApi() {
var apiClient = new ApiClient(restTemplate).setBasePath(this.tenantServiceApiUrl);
apiClient.addDefaultCookie(LANGUAGE_COOKIE_NAME, getCurrentLanguageContext());
return new TenantControllerApi(apiClient);
}


public String getCurrentLanguageContext() {
HttpServletRequest currentRequest =
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

if (currentRequest.getCookies() != null) {
Optional<Cookie> languageCookie = findLanguageCookie(currentRequest);
if (languageCookie.isPresent()) {
return languageCookie.get().getValue();
}
}
return DEFAULT_LANGUAGE;
}

private static Optional<Cookie> findLanguageCookie(HttpServletRequest currentRequest) {
return Arrays.stream(currentRequest.getCookies())
.filter(cookie -> LANGUAGE_COOKIE_NAME.equals(cookie.getName()))
.findFirst();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;
import org.springframework.boot.test.context.SpringBootTest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import de.caritas.cob.agencyservice.topicservice.generated.web.model.TopicDTO;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package de.caritas.cob.agencyservice.api.service;


import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

import com.google.common.collect.Lists;

import de.caritas.cob.agencyservice.AgencyServiceApplication;
import de.caritas.cob.agencyservice.api.service.securityheader.SecurityHeaderSupplier;
import de.caritas.cob.agencyservice.config.apiclient.TopicServiceApiControllerFactory;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import de.caritas.cob.agencyservice.topicservice.generated.web.model.TopicDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpHeaders;
import org.springframework.test.context.TestPropertySource;


@SpringBootTest(classes = AgencyServiceApplication.class)
@TestPropertySource(properties = "spring.profiles.active=testing")
@AutoConfigureTestDatabase(replace = Replace.ANY)
class TopicCachingServiceTest {

@MockBean
TopicServiceApiControllerFactory topicServiceApiControllerFactory;

@MockBean SecurityHeaderSupplier securityHeaderSupplier;

@MockBean TenantHeaderSupplier tenantHeaderSupplier;

@MockBean
de.caritas.cob.agencyservice.topicservice.generated.web.TopicControllerApi topicControllerApi;

@Autowired
TopicCachingService topicCachingService;

@Test
void getAllTopics_Should_CallTopicControllerApiToGetTopics_AndCacheResponsePerTenant() {
// given
when(securityHeaderSupplier.getKeycloakAndCsrfHttpHeaders()).thenReturn(new HttpHeaders());
when(topicServiceApiControllerFactory.createControllerApi()).thenReturn(topicControllerApi);
var expectedTopics = Lists.newArrayList(new TopicDTO(), new TopicDTO());
when(topicControllerApi.getAllTopics()).thenReturn(expectedTopics);

// when
var actualTopics = topicCachingService.getAllTopics(1L);
topicCachingService.getAllTopics(1L);

// then
assertThat(actualTopics).isEqualTo(expectedTopics);
Mockito.verify(topicControllerApi, Mockito.times(1)).getAllTopics();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.caritas.cob.agencyservice.api.service;

import static org.mockito.Mockito.verify;

import de.caritas.cob.agencyservice.api.tenant.TenantContext;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class TopicServiceTest {

@Mock
private TopicCachingService topicCachingService;

@Test
void getTopics_Should_CallTopicCachingService() {
// given
TopicService topicService = new TopicService(topicCachingService);

// when
topicService.getAllTopics();

// then
verify(topicCachingService).getAllTopics(TenantContext.getCurrentTenant());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package de.caritas.cob.agencyservice.config.apiclient;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import static org.assertj.core.api.Assertions.assertThat;

class TenantServiceApiControllerFactoryTest {

@AfterEach
void tearDown() {
RequestContextHolder.resetRequestAttributes();
}

@Test
void createControllerApi_Should_CreateApiClientWithoutExceptions() {
// given
MockHttpServletRequest mockRequest = new MockHttpServletRequest();
mockRequest.addHeader("Authorization", "Bearer token");
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockRequest));

var tenantServiceApiControllerFactory = new TenantServiceApiControllerFactory();

// when
var tenantControllerApi = tenantServiceApiControllerFactory.createControllerApi();

// then
assertThat(tenantControllerApi).isNotNull();
}
}
Loading