diff --git a/src/main/java/de/caritas/cob/agencyservice/api/admin/service/UserAdminService.java b/src/main/java/de/caritas/cob/agencyservice/api/admin/service/UserAdminService.java index 6bcea033..ab611120 100644 --- a/src/main/java/de/caritas/cob/agencyservice/api/admin/service/UserAdminService.java +++ b/src/main/java/de/caritas/cob/agencyservice/api/admin/service/UserAdminService.java @@ -2,6 +2,7 @@ import de.caritas.cob.agencyservice.api.service.TenantHeaderSupplier; import de.caritas.cob.agencyservice.api.service.securityheader.SecurityHeaderSupplier; +import de.caritas.cob.agencyservice.config.apiclient.UserAdminServiceApiControllerFactory; import de.caritas.cob.agencyservice.useradminservice.generated.ApiClient; import de.caritas.cob.agencyservice.useradminservice.generated.web.AdminUserControllerApi; import de.caritas.cob.agencyservice.useradminservice.generated.web.model.AgencyTypeDTO; @@ -21,7 +22,7 @@ @RequiredArgsConstructor public class UserAdminService { - private final @NonNull AdminUserControllerApi adminUserControllerApi; + private final @NonNull UserAdminServiceApiControllerFactory userAdminServiceApiControllerFactory; private final @NonNull SecurityHeaderSupplier securityHeaderSupplier; private final @NonNull TenantHeaderSupplier tenantHeaderSupplier; @@ -34,8 +35,9 @@ public class UserAdminService { * tagged as team consultants */ public void adaptRelatedConsultantsForChange(Long agencyId, String agencyType) { - addDefaultHeaders(this.adminUserControllerApi.getApiClient()); - this.adminUserControllerApi + AdminUserControllerApi controllerApi = userAdminServiceApiControllerFactory.createControllerApi(); + addDefaultHeaders(controllerApi.getApiClient()); + controllerApi .changeAgencyType(agencyId, new AgencyTypeDTO() .agencyType(AgencyTypeEnum.fromValue(agencyType))); @@ -57,14 +59,15 @@ protected void addDefaultHeaders(ApiClient apiClient) { */ public List getConsultantsOfAgency(Long agencyId, int currentPage, int perPage) { - addDefaultHeaders(this.adminUserControllerApi.getApiClient()); + var controllerApi = userAdminServiceApiControllerFactory.createControllerApi(); + addDefaultHeaders(controllerApi.getApiClient()); ConsultantFilter consultantFilter = new ConsultantFilter().agencyId(agencyId); Sort sortBy = new Sort(); sortBy.setField(FieldEnum.LASTNAME); sortBy.setOrder(OrderEnum.ASC); - return this.adminUserControllerApi + return controllerApi .getConsultants(currentPage, perPage, consultantFilter, sortBy) .getEmbedded(); } diff --git a/src/main/java/de/caritas/cob/agencyservice/api/service/ApplicationSettingsService.java b/src/main/java/de/caritas/cob/agencyservice/api/service/ApplicationSettingsService.java index 2639d711..6f074a4a 100644 --- a/src/main/java/de/caritas/cob/agencyservice/api/service/ApplicationSettingsService.java +++ b/src/main/java/de/caritas/cob/agencyservice/api/service/ApplicationSettingsService.java @@ -4,6 +4,7 @@ import de.caritas.cob.agencyservice.applicationsettingsservice.generated.web.model.ApplicationSettingsDTO; import de.caritas.cob.agencyservice.config.CacheManagerConfig; +import de.caritas.cob.agencyservice.config.apiclient.ApplicationSettingsApiControllerFactory; import lombok.NonNull; import lombok.RequiredArgsConstructor; import org.springframework.cache.annotation.Cacheable; @@ -18,15 +19,16 @@ @RequiredArgsConstructor public class ApplicationSettingsService { - private final @NonNull ApplicationsettingsControllerApi applicationsettingsControllerApi; + private final @NonNull ApplicationSettingsApiControllerFactory applicationSettingsApiControllerFactory; private final @NonNull SecurityHeaderSupplier securityHeaderSupplier; private final @NonNull TenantHeaderSupplier tenantHeaderSupplier; @Cacheable(value = CacheManagerConfig.APPLICATION_SETTINGS_CACHE) public ApplicationSettingsDTO getApplicationSettings() { - addDefaultHeaders(this.applicationsettingsControllerApi.getApiClient()); - return this.applicationsettingsControllerApi.getApplicationSettings(); + ApplicationsettingsControllerApi controllerApi = applicationSettingsApiControllerFactory.createControllerApi(); + addDefaultHeaders(controllerApi.getApiClient()); + return controllerApi.getApplicationSettings(); } private void addDefaultHeaders(ApiClient apiClient) { diff --git a/src/main/java/de/caritas/cob/agencyservice/api/service/AppointmentService.java b/src/main/java/de/caritas/cob/agencyservice/api/service/AppointmentService.java index 4511ac6c..43128ad2 100644 --- a/src/main/java/de/caritas/cob/agencyservice/api/service/AppointmentService.java +++ b/src/main/java/de/caritas/cob/agencyservice/api/service/AppointmentService.java @@ -5,6 +5,7 @@ import de.caritas.cob.agencyservice.appointmentservice.generated.ApiClient; import de.caritas.cob.agencyservice.appointmentservice.generated.web.AgencyApi; import de.caritas.cob.agencyservice.appointmentservice.generated.web.model.AgencyMasterDataSyncRequestDTO; +import de.caritas.cob.agencyservice.config.apiclient.AppointmentServiceAgencyApiControllerFactory; import lombok.NonNull; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; @@ -15,7 +16,7 @@ @RequiredArgsConstructor public class AppointmentService { - private final @NonNull AgencyApi appointmentAgencyApi; + private final @NonNull AppointmentServiceAgencyApiControllerFactory appointmentServiceAgencyApiControllerFactory; private final @NonNull SecurityHeaderSupplier securityHeaderSupplier; private final @NonNull TenantHeaderSupplier tenantHeaderSupplier; @@ -29,16 +30,18 @@ public void syncAgencyDataToAppointmentService(Agency agency) { AgencyMasterDataSyncRequestDTO request = new AgencyMasterDataSyncRequestDTO(); request.setId(agency.getId()); request.setName(agency.getName()); - addDefaultHeaders(appointmentAgencyApi.getApiClient()); - appointmentAgencyApi.agencyMasterDataSync(request); + AgencyApi controllerApi = appointmentServiceAgencyApiControllerFactory.createControllerApi(); + addDefaultHeaders(controllerApi.getApiClient()); + controllerApi.agencyMasterDataSync(request); } public void deleteAgency(Agency agency) { if (!appointmentFeatureEnabled) { return; } - addDefaultHeaders(appointmentAgencyApi.getApiClient()); - appointmentAgencyApi.deleteAgency(agency.getId()); + AgencyApi controllerApi = appointmentServiceAgencyApiControllerFactory.createControllerApi(); + addDefaultHeaders(controllerApi.getApiClient()); + controllerApi.deleteAgency(agency.getId()); } diff --git a/src/main/java/de/caritas/cob/agencyservice/api/service/ConsultingTypeService.java b/src/main/java/de/caritas/cob/agencyservice/api/service/ConsultingTypeService.java index bc9e0e8a..067cbe88 100644 --- a/src/main/java/de/caritas/cob/agencyservice/api/service/ConsultingTypeService.java +++ b/src/main/java/de/caritas/cob/agencyservice/api/service/ConsultingTypeService.java @@ -2,6 +2,7 @@ import de.caritas.cob.agencyservice.api.service.securityheader.SecurityHeaderSupplier; import de.caritas.cob.agencyservice.config.CacheManagerConfig; +import de.caritas.cob.agencyservice.config.apiclient.ConsultingTypeServiceApiControllerFactory; import de.caritas.cob.agencyservice.consultingtypeservice.generated.ApiClient; import de.caritas.cob.agencyservice.consultingtypeservice.generated.web.ConsultingTypeControllerApi; import de.caritas.cob.agencyservice.consultingtypeservice.generated.web.model.ExtendedConsultingTypeResponseDTO; @@ -17,7 +18,7 @@ @RequiredArgsConstructor public class ConsultingTypeService { - private final @NonNull ConsultingTypeControllerApi consultingTypeControllerApi; + private final @NonNull ConsultingTypeServiceApiControllerFactory consultingTypeServiceApiControllerFactory; private final @NonNull SecurityHeaderSupplier securityHeaderSupplier; private final @NonNull TenantHeaderSupplier tenantHeaderSupplier; @@ -31,8 +32,9 @@ public class ConsultingTypeService { @Cacheable(value = CacheManagerConfig.CONSULTING_TYPE_CACHE, key = "#consultingTypeId") public ExtendedConsultingTypeResponseDTO getExtendedConsultingTypeResponseDTO( int consultingTypeId) { - addDefaultHeaders(this.consultingTypeControllerApi.getApiClient()); - return this.consultingTypeControllerApi.getExtendedConsultingTypeById(consultingTypeId); + ConsultingTypeControllerApi controllerApi = consultingTypeServiceApiControllerFactory.createControllerApi(); + addDefaultHeaders(controllerApi.getApiClient()); + return controllerApi.getExtendedConsultingTypeById(consultingTypeId); } private void addDefaultHeaders(ApiClient apiClient) { diff --git a/src/main/java/de/caritas/cob/agencyservice/api/service/TenantService.java b/src/main/java/de/caritas/cob/agencyservice/api/service/TenantService.java index 26b7921e..452253fe 100644 --- a/src/main/java/de/caritas/cob/agencyservice/api/service/TenantService.java +++ b/src/main/java/de/caritas/cob/agencyservice/api/service/TenantService.java @@ -1,6 +1,7 @@ package de.caritas.cob.agencyservice.api.service; import de.caritas.cob.agencyservice.config.CacheManagerConfig; +import de.caritas.cob.agencyservice.config.apiclient.TenantServiceApiControllerFactory; import de.caritas.cob.agencyservice.tenantservice.generated.web.TenantControllerApi; import de.caritas.cob.agencyservice.tenantservice.generated.web.model.RestrictedTenantDTO; import lombok.NonNull; @@ -13,21 +14,24 @@ @RequiredArgsConstructor public class TenantService { - private final @NonNull TenantControllerApi tenantControllerApi; + private final @NonNull TenantServiceApiControllerFactory tenantServiceApiControllerFactory; @Value("${multitenancy.enabled}") private boolean multitenancy; @Cacheable(cacheNames = CacheManagerConfig.TENANT_CACHE, key = "#subdomain") public RestrictedTenantDTO getRestrictedTenantDataBySubdomain(String subdomain) { - return tenantControllerApi.getRestrictedTenantDataBySubdomainWithHttpInfo(subdomain).getBody(); + TenantControllerApi controllerApi = tenantServiceApiControllerFactory.createControllerApi(); + return controllerApi.getRestrictedTenantDataBySubdomainWithHttpInfo(subdomain).getBody(); } public RestrictedTenantDTO getRestrictedTenantDataByTenantId(Long tenantId) { - return tenantControllerApi.getRestrictedTenantDataByTenantId(tenantId); + TenantControllerApi controllerApi = tenantServiceApiControllerFactory.createControllerApi(); + return controllerApi.getRestrictedTenantDataByTenantId(tenantId); } public RestrictedTenantDTO getRestrictedTenantDataForSingleTenant() { - return tenantControllerApi.getRestrictedSingleTenantData(); + TenantControllerApi controllerApi = tenantServiceApiControllerFactory.createControllerApi(); + return controllerApi.getRestrictedSingleTenantData(); } } diff --git a/src/main/java/de/caritas/cob/agencyservice/api/service/TopicService.java b/src/main/java/de/caritas/cob/agencyservice/api/service/TopicService.java index d5df9205..3681f6a8 100644 --- a/src/main/java/de/caritas/cob/agencyservice/api/service/TopicService.java +++ b/src/main/java/de/caritas/cob/agencyservice/api/service/TopicService.java @@ -2,6 +2,7 @@ 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 java.util.List; import lombok.NonNull; @@ -15,14 +16,15 @@ @RequiredArgsConstructor public class TopicService { - private final @NonNull TopicControllerApi topicControllerApi; + private final @NonNull TopicServiceApiControllerFactory topicServiceApiControllerFactory; private final @NonNull SecurityHeaderSupplier securityHeaderSupplier; private final @NonNull TenantHeaderSupplier tenantHeaderSupplier; @Cacheable(cacheNames = CacheManagerConfig.TOPICS_CACHE) public List getAllTopics() { - addDefaultHeaders(this.topicControllerApi.getApiClient()); - return topicControllerApi.getAllTopics(); + TopicControllerApi controllerApi = topicServiceApiControllerFactory.createControllerApi(); + addDefaultHeaders(controllerApi.getApiClient()); + return controllerApi.getAllTopics(); } private void addDefaultHeaders(ApiClient apiClient) { diff --git a/src/main/java/de/caritas/cob/agencyservice/api/tenant/MultitenancyWithSingleDomainTenantResolver.java b/src/main/java/de/caritas/cob/agencyservice/api/tenant/MultitenancyWithSingleDomainTenantResolver.java index 20474002..55957d4f 100644 --- a/src/main/java/de/caritas/cob/agencyservice/api/tenant/MultitenancyWithSingleDomainTenantResolver.java +++ b/src/main/java/de/caritas/cob/agencyservice/api/tenant/MultitenancyWithSingleDomainTenantResolver.java @@ -2,6 +2,8 @@ import de.caritas.cob.agencyservice.applicationsettingsservice.generated.web.model.ApplicationSettingsDTO; import de.caritas.cob.agencyservice.applicationsettingsservice.generated.web.model.SettingDTO; +import de.caritas.cob.agencyservice.config.apiclient.ApplicationSettingsApiControllerFactory; +import de.caritas.cob.agencyservice.config.apiclient.TenantServiceApiControllerFactory; import de.caritas.cob.agencyservice.tenantservice.generated.web.model.RestrictedTenantDTO; import java.util.Optional; import javax.servlet.http.HttpServletRequest; @@ -25,11 +27,10 @@ public class MultitenancyWithSingleDomainTenantResolver implements TenantResolve private boolean multitenancyWithSingleDomain; @Autowired - @Qualifier("applicationsettingsControllerApiPrimary") - private ApplicationsettingsControllerApi applicationsettingsControllerApi; + private ApplicationSettingsApiControllerFactory applicationSettingsApiControllerFactory; @Autowired - private TenantControllerApi tenantControllerApi; + private TenantServiceApiControllerFactory tenantServiceApiControllerFactory; @Override public Optional resolve(HttpServletRequest request) { @@ -52,13 +53,14 @@ private Optional resolveForMultitenancyWithSingleDomain() { } private Optional resolveFromTenantServiceBasedOnMainTenantSubdomain(String rootTenantSubdomain) { + var tenantControllerApi = tenantServiceApiControllerFactory.createControllerApi(); RestrictedTenantDTO rootTenantData = tenantControllerApi.getRestrictedTenantDataBySubdomain( rootTenantSubdomain); return Optional.of(rootTenantData.getId()); } private Optional getMainTenantSubdomainFromApplicationSettings() { - ApplicationSettingsDTO applicationSettings = applicationsettingsControllerApi.getApplicationSettings(); + ApplicationSettingsDTO applicationSettings = applicationSettingsApiControllerFactory.createControllerApi().getApplicationSettings(); SettingDTO mainTenantSubdomainForSingleDomainMultitenancy = applicationSettings.getMainTenantSubdomainForSingleDomainMultitenancy(); if (mainTenantSubdomainForSingleDomainMultitenancy == null) { return Optional.empty(); diff --git a/src/main/java/de/caritas/cob/agencyservice/config/apiclient/ApplicationSettingsApiControllerFactory.java b/src/main/java/de/caritas/cob/agencyservice/config/apiclient/ApplicationSettingsApiControllerFactory.java new file mode 100644 index 00000000..57ee7b44 --- /dev/null +++ b/src/main/java/de/caritas/cob/agencyservice/config/apiclient/ApplicationSettingsApiControllerFactory.java @@ -0,0 +1,21 @@ +package de.caritas.cob.agencyservice.config.apiclient; + +import de.caritas.cob.agencyservice.applicationsettingsservice.generated.web.ApplicationsettingsControllerApi; +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; + +@Component +public class ApplicationSettingsApiControllerFactory { + + @Value("${consulting.type.service.api.url}") + private String applicationsettingsServiceApiUrl; + + @Autowired private RestTemplate restTemplate; + + public ApplicationsettingsControllerApi createControllerApi() { + var apiClient = new ApplicationSettingsApiClient(restTemplate).setBasePath(this.applicationsettingsServiceApiUrl); + return new ApplicationsettingsControllerApi(apiClient); + } +} diff --git a/src/main/java/de/caritas/cob/agencyservice/config/apiclient/ApplicationSettingsClientApiConfig.java b/src/main/java/de/caritas/cob/agencyservice/config/apiclient/ApplicationSettingsClientApiConfig.java deleted file mode 100644 index 49975c66..00000000 --- a/src/main/java/de/caritas/cob/agencyservice/config/apiclient/ApplicationSettingsClientApiConfig.java +++ /dev/null @@ -1,34 +0,0 @@ -package de.caritas.cob.agencyservice.config.apiclient; - -import de.caritas.cob.agencyservice.applicationsettingsservice.generated.ApiClient; -import de.caritas.cob.agencyservice.applicationsettingsservice.generated.web.ApplicationsettingsControllerApi; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.beans.factory.config.ConfigurableBeanFactory; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Primary; -import org.springframework.context.annotation.Scope; -import org.springframework.stereotype.Component; -import org.springframework.web.client.RestTemplate; - -@Component -public class ApplicationSettingsClientApiConfig { - - @Value("${consulting.type.service.api.url}") - private String applicationsettingsServiceApiUrl; - - @Bean("applicationsettingsControllerApiPrimary") - @Primary - public ApplicationsettingsControllerApi applicationsettingsControllerApi(@Qualifier("applicationsettingsControllerApiClientPrimary") ApiClient apiClient) { - return new ApplicationsettingsControllerApi(apiClient); - } - - @Bean("applicationsettingsControllerApiClientPrimary") - @Primary - @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) - public ApiClient adminAgencyApiClient(RestTemplate restTemplate) { - ApiClient apiClient = new ApplicationSettingsApiClient(restTemplate); - apiClient.setBasePath(this.applicationsettingsServiceApiUrl); - return apiClient; - } -} \ No newline at end of file diff --git a/src/main/java/de/caritas/cob/agencyservice/config/apiclient/AppointmentServiceAgencyApiControllerFactory.java b/src/main/java/de/caritas/cob/agencyservice/config/apiclient/AppointmentServiceAgencyApiControllerFactory.java new file mode 100644 index 00000000..3a66ce7c --- /dev/null +++ b/src/main/java/de/caritas/cob/agencyservice/config/apiclient/AppointmentServiceAgencyApiControllerFactory.java @@ -0,0 +1,23 @@ +package de.caritas.cob.agencyservice.config.apiclient; + +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 de.caritas.cob.agencyservice.appointmentservice.generated.ApiClient; +import de.caritas.cob.agencyservice.appointmentservice.generated.web.AgencyApi; + +@Component +public class AppointmentServiceAgencyApiControllerFactory { + + @Value("${appointment.service.api.url}") + private String appointmentServiceApiUrl; + + @Autowired + private RestTemplate restTemplate; + + public AgencyApi createControllerApi() { + var apiClient = new ApiClient(restTemplate).setBasePath(this.appointmentServiceApiUrl); + return new de.caritas.cob.agencyservice.appointmentservice.generated.web.AgencyApi(apiClient); + } +} diff --git a/src/main/java/de/caritas/cob/agencyservice/config/apiclient/AppointmentsApiClientConfig.java b/src/main/java/de/caritas/cob/agencyservice/config/apiclient/AppointmentsApiClientConfig.java deleted file mode 100644 index 4e14082c..00000000 --- a/src/main/java/de/caritas/cob/agencyservice/config/apiclient/AppointmentsApiClientConfig.java +++ /dev/null @@ -1,31 +0,0 @@ -package de.caritas.cob.agencyservice.config.apiclient; - -import de.caritas.cob.agencyservice.appointmentservice.generated.ApiClient; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Primary; -import org.springframework.web.client.RestTemplate; - -/** - * Configuration class for the AppointmentService API client. - */ -@Configuration -public class AppointmentsApiClientConfig { - - @Value("${appointment.service.api.url}") - private String appointmentServiceApiUrl; - - /** - * AppointmentService API client bean. - * - * @param restTemplate {@link RestTemplate} - * @return the AppointmentService {@link ApiClient} - */ - @Bean - @Primary - public ApiClient appointmentConsultantApiClient(RestTemplate restTemplate) { - return new ApiClient(restTemplate).setBasePath(this.appointmentServiceApiUrl); - } - -} diff --git a/src/main/java/de/caritas/cob/agencyservice/config/apiclient/ConsultingTypeServiceApiClientConfig.java b/src/main/java/de/caritas/cob/agencyservice/config/apiclient/ConsultingTypeServiceApiClientConfig.java deleted file mode 100644 index 2b7ffbed..00000000 --- a/src/main/java/de/caritas/cob/agencyservice/config/apiclient/ConsultingTypeServiceApiClientConfig.java +++ /dev/null @@ -1,43 +0,0 @@ -package de.caritas.cob.agencyservice.config.apiclient; - -import de.caritas.cob.agencyservice.consultingtypeservice.generated.web.ConsultingTypeControllerApi; -import de.caritas.cob.agencyservice.consultingtypeservice.generated.ApiClient; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Primary; -import org.springframework.stereotype.Component; -import org.springframework.web.client.RestTemplate; - -/** - * Configuration class for the ConsultingTypeService API client. - */ -@Component -public class ConsultingTypeServiceApiClientConfig { - - @Value("${consulting.type.service.api.url}") - private String consultingTypeServiceApiUrl; - - /** - * ConsultingTypeService controller bean. - * - * @param apiClient {@link ApiClient} - * @return the ConsultingTypeService controller {@link ConsultingTypeControllerApi} - */ - @Bean - public ConsultingTypeControllerApi consultingTypeControllerApi(ApiClient apiClient) { - return new ConsultingTypeControllerApi(apiClient); - } - - /** - * ConsultingTypeService API client bean. - * - * @param restTemplate {@link RestTemplate} - * @return the ConsultingTypeService {@link ApiClient} - */ - @Bean - @Primary - public ApiClient consultingTypeApiClient(RestTemplate restTemplate) { - return new ApiClient(restTemplate).setBasePath(this.consultingTypeServiceApiUrl); - } - -} diff --git a/src/main/java/de/caritas/cob/agencyservice/config/apiclient/ConsultingTypeServiceApiControllerFactory.java b/src/main/java/de/caritas/cob/agencyservice/config/apiclient/ConsultingTypeServiceApiControllerFactory.java new file mode 100644 index 00000000..13f121b0 --- /dev/null +++ b/src/main/java/de/caritas/cob/agencyservice/config/apiclient/ConsultingTypeServiceApiControllerFactory.java @@ -0,0 +1,23 @@ +package de.caritas.cob.agencyservice.config.apiclient; + +import de.caritas.cob.agencyservice.consultingtypeservice.generated.ApiClient; +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 de.caritas.cob.agencyservice.consultingtypeservice.generated.web.ConsultingTypeControllerApi; + +@Component +public class ConsultingTypeServiceApiControllerFactory { + + @Value("${consulting.type.service.api.url}") + private String consultingTypeServiceApiUrl; + + @Autowired + private RestTemplate restTemplate; + + public ConsultingTypeControllerApi createControllerApi() { + var apiClient = new ApiClient(restTemplate).setBasePath(this.consultingTypeServiceApiUrl); + return new ConsultingTypeControllerApi(apiClient); + } +} diff --git a/src/main/java/de/caritas/cob/agencyservice/config/apiclient/TenantServiceApiClientConfig.java b/src/main/java/de/caritas/cob/agencyservice/config/apiclient/TenantServiceApiControllerFactory.java similarity index 52% rename from src/main/java/de/caritas/cob/agencyservice/config/apiclient/TenantServiceApiClientConfig.java rename to src/main/java/de/caritas/cob/agencyservice/config/apiclient/TenantServiceApiControllerFactory.java index 8ac3854b..7d183e3b 100644 --- a/src/main/java/de/caritas/cob/agencyservice/config/apiclient/TenantServiceApiClientConfig.java +++ b/src/main/java/de/caritas/cob/agencyservice/config/apiclient/TenantServiceApiControllerFactory.java @@ -2,29 +2,22 @@ import de.caritas.cob.agencyservice.tenantservice.generated.ApiClient; import de.caritas.cob.agencyservice.tenantservice.generated.web.TenantControllerApi; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; @Component -public class TenantServiceApiClientConfig { +public class TenantServiceApiControllerFactory { @Value("${tenant.service.api.url}") private String tenantServiceApiUrl; - @Bean - @Primary - public TenantControllerApi tenantControllerApi(ApiClient apiClient) { - return new TenantControllerApi(apiClient); - } + @Autowired + private RestTemplate restTemplate; - @Bean - @Primary - public ApiClient tenantApiClient(RestTemplate restTemplate) { - ApiClient apiClient = new TenantServiceApiClient(restTemplate); - apiClient.setBasePath(this.tenantServiceApiUrl); - return apiClient; + public TenantControllerApi createControllerApi() { + var apiClient = new ApiClient(restTemplate).setBasePath(this.tenantServiceApiUrl); + return new TenantControllerApi(apiClient); } -} \ No newline at end of file +} diff --git a/src/main/java/de/caritas/cob/agencyservice/config/apiclient/TopicServiceApiClientConfig.java b/src/main/java/de/caritas/cob/agencyservice/config/apiclient/TopicServiceApiClientConfig.java deleted file mode 100644 index 3b210b8a..00000000 --- a/src/main/java/de/caritas/cob/agencyservice/config/apiclient/TopicServiceApiClientConfig.java +++ /dev/null @@ -1,29 +0,0 @@ -package de.caritas.cob.agencyservice.config.apiclient; - -import de.caritas.cob.agencyservice.topicservice.generated.web.TopicControllerApi; -import de.caritas.cob.agencyservice.topicservice.generated.ApiClient; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Primary; -import org.springframework.stereotype.Component; -import org.springframework.web.client.RestTemplate; - -@Component -public class TopicServiceApiClientConfig { - - @Value("${consulting.type.service.api.url}") - private String topicServiceApiUrl; - - @Bean - public TopicControllerApi topicControllerApi(ApiClient apiClient) { - return new de.caritas.cob.agencyservice.topicservice.generated.web.TopicControllerApi(apiClient); - } - - @Bean - @Primary - public ApiClient topicApiClient(RestTemplate restTemplate) { - de.caritas.cob.agencyservice.topicservice.generated.ApiClient apiClient = new TopicServiceApiClient(restTemplate); - apiClient.setBasePath(this.topicServiceApiUrl); - return apiClient; - } -} \ No newline at end of file diff --git a/src/main/java/de/caritas/cob/agencyservice/config/apiclient/TopicServiceApiControllerFactory.java b/src/main/java/de/caritas/cob/agencyservice/config/apiclient/TopicServiceApiControllerFactory.java new file mode 100644 index 00000000..ee9eee75 --- /dev/null +++ b/src/main/java/de/caritas/cob/agencyservice/config/apiclient/TopicServiceApiControllerFactory.java @@ -0,0 +1,23 @@ +package de.caritas.cob.agencyservice.config.apiclient; + +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 de.caritas.cob.agencyservice.topicservice.generated.ApiClient; +import de.caritas.cob.agencyservice.topicservice.generated.web.TopicControllerApi; + +@Component +public class TopicServiceApiControllerFactory { + + @Value("${consulting.type.service.api.url}") + private String topicServiceApiUrl; + + @Autowired + private RestTemplate restTemplate; + + public TopicControllerApi createControllerApi() { + var apiClient = new ApiClient(restTemplate).setBasePath(this.topicServiceApiUrl); + return new TopicControllerApi(apiClient); + } +} diff --git a/src/main/java/de/caritas/cob/agencyservice/config/apiclient/UserAdminServiceApiClientConfig.java b/src/main/java/de/caritas/cob/agencyservice/config/apiclient/UserAdminServiceApiControllerFactory.java similarity index 52% rename from src/main/java/de/caritas/cob/agencyservice/config/apiclient/UserAdminServiceApiClientConfig.java rename to src/main/java/de/caritas/cob/agencyservice/config/apiclient/UserAdminServiceApiControllerFactory.java index c706cccf..0a24fe06 100644 --- a/src/main/java/de/caritas/cob/agencyservice/config/apiclient/UserAdminServiceApiClientConfig.java +++ b/src/main/java/de/caritas/cob/agencyservice/config/apiclient/UserAdminServiceApiControllerFactory.java @@ -1,29 +1,23 @@ package de.caritas.cob.agencyservice.config.apiclient; -import de.caritas.cob.agencyservice.useradminservice.generated.ApiClient; -import de.caritas.cob.agencyservice.useradminservice.generated.web.AdminUserControllerApi; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; +import de.caritas.cob.agencyservice.useradminservice.generated.ApiClient; +import de.caritas.cob.agencyservice.useradminservice.generated.web.AdminUserControllerApi; @Component -public class UserAdminServiceApiClientConfig { +public class UserAdminServiceApiControllerFactory { @Value("${user.admin.service.api.url}") private String userAdminServiceApiUrl; - @Bean - public AdminUserControllerApi adminUserControllerApi(ApiClient apiClient) { - return new AdminUserControllerApi(apiClient); - } + @Autowired + private RestTemplate restTemplate; - @Bean - @Primary - public ApiClient adminAgencyApiClient(RestTemplate restTemplate) { - ApiClient apiClient = new UserAdminApiClient(restTemplate); - apiClient.setBasePath(this.userAdminServiceApiUrl); - return apiClient; + public AdminUserControllerApi createControllerApi() { + var apiClient = new ApiClient(restTemplate).setBasePath(this.userAdminServiceApiUrl); + return new AdminUserControllerApi(apiClient); } -} \ No newline at end of file +} diff --git a/src/test/java/de/caritas/cob/agencyservice/api/admin/service/UserAdminServiceTest.java b/src/test/java/de/caritas/cob/agencyservice/api/admin/service/UserAdminServiceTest.java index 6773164c..97ec319b 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/admin/service/UserAdminServiceTest.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/admin/service/UserAdminServiceTest.java @@ -11,6 +11,7 @@ import de.caritas.cob.agencyservice.api.service.TenantHeaderSupplier; import de.caritas.cob.agencyservice.api.service.securityheader.SecurityHeaderSupplier; import de.caritas.cob.agencyservice.api.tenant.TenantContext; +import de.caritas.cob.agencyservice.config.apiclient.UserAdminServiceApiControllerFactory; import de.caritas.cob.agencyservice.useradminservice.generated.ApiClient; import de.caritas.cob.agencyservice.useradminservice.generated.web.AdminUserControllerApi; import de.caritas.cob.agencyservice.useradminservice.generated.web.model.AgencyTypeDTO; @@ -45,6 +46,9 @@ public class UserAdminServiceTest { @Mock private TenantHeaderSupplier tenantHeaderSupplier; + @Mock + private UserAdminServiceApiControllerFactory userAdminServiceApiControllerFactory; + @Mock private ApiClient apiClient; @@ -55,6 +59,7 @@ public void setup() { when(this.adminUserControllerApi.getApiClient()).thenReturn(this.apiClient); when(this.securityHeaderSupplier.getKeycloakAndCsrfHttpHeaders()) .thenReturn(this.httpHeaders); + when(userAdminServiceApiControllerFactory.createControllerApi()).thenReturn(adminUserControllerApi); } @Test @@ -63,8 +68,8 @@ public void adaptRelatedConsultantsForChange_Should_callServicesCorrectly() { this.userAdminService.adaptRelatedConsultantsForChange(agencyId, TEAM_AGENCY.getValue()); - verify(this.adminUserControllerApi, times(1)).changeAgencyType(eq(agencyId), - eq(new AgencyTypeDTO().agencyType(TEAM_AGENCY))); + verify(this.adminUserControllerApi, times(1)).changeAgencyType(agencyId, + new AgencyTypeDTO().agencyType(TEAM_AGENCY)); verify(this.apiClient, times(this.httpHeaders.size())).addDefaultHeader(any(), any()); } diff --git a/src/test/java/de/caritas/cob/agencyservice/api/config/apiclient/UserAdminServiceApiClientConfigIT.java b/src/test/java/de/caritas/cob/agencyservice/api/config/apiclient/UserAdminServiceApiClientConfigIT.java deleted file mode 100644 index 84f2aa32..00000000 --- a/src/test/java/de/caritas/cob/agencyservice/api/config/apiclient/UserAdminServiceApiClientConfigIT.java +++ /dev/null @@ -1,45 +0,0 @@ -package de.caritas.cob.agencyservice.api.config.apiclient; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.instanceOf; -import static org.hamcrest.core.Is.is; - -import de.caritas.cob.agencyservice.AgencyServiceApplication; -import de.caritas.cob.agencyservice.config.apiclient.UserAdminApiClient; -import de.caritas.cob.agencyservice.useradminservice.generated.ApiClient; -import de.caritas.cob.agencyservice.useradminservice.generated.web.AdminUserControllerApi; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -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.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@SpringBootTest(classes = AgencyServiceApplication.class) -@TestPropertySource(properties = "spring.profiles.active=testing") -@AutoConfigureTestDatabase(replace = Replace.ANY) -public class UserAdminServiceApiClientConfigIT { - - @Autowired private AdminUserControllerApi adminUserControllerApi; - - @Value("${user.admin.service.api.url}") - private String adminUserApiBaseUrl; - - @Test - public void configureUserControllerApi_Should_setCorrectApiUrl() { - String apiClientUrl = this.adminUserControllerApi.getApiClient().getBasePath(); - - assertThat(apiClientUrl, is(this.adminUserApiBaseUrl)); - } - - @Test - public void configureUserControllerApi_Should_setUserAdminApiClient() { - ApiClient apiClient = this.adminUserControllerApi.getApiClient(); - - assertThat(apiClient, instanceOf(UserAdminApiClient.class)); - } -} diff --git a/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyControllerWithSingleDomainMultitenancyIT.java b/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyControllerWithSingleDomainMultitenancyIT.java index 8ed73817..50884604 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyControllerWithSingleDomainMultitenancyIT.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyControllerWithSingleDomainMultitenancyIT.java @@ -15,6 +15,8 @@ import de.caritas.cob.agencyservice.api.tenant.TenantContext; import de.caritas.cob.agencyservice.applicationsettingsservice.generated.web.model.ApplicationSettingsDTO; import de.caritas.cob.agencyservice.applicationsettingsservice.generated.web.model.SettingDTO; +import de.caritas.cob.agencyservice.config.apiclient.ApplicationSettingsApiControllerFactory; +import de.caritas.cob.agencyservice.config.apiclient.TenantServiceApiControllerFactory; import de.caritas.cob.agencyservice.tenantservice.generated.web.model.RestrictedTenantDTO; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -54,23 +56,32 @@ public void setup() { @MockBean private ConsultingTypeManager consultingTypeManager; + @MockBean + private ApplicationSettingsApiControllerFactory applicationSettingsApiControllerFactory; @MockBean private ApplicationsettingsControllerApi applicationsettingsControllerApi; @MockBean private TenantControllerApi tenantControllerApi; + + @MockBean + private TenantServiceApiControllerFactory tenantServiceApiControllerFactory; + @Autowired private WebApplicationContext context; + @BeforeEach public void setUp() throws MissingConsultingTypeException { + when(applicationSettingsApiControllerFactory.createControllerApi()).thenReturn(applicationsettingsControllerApi); when(consultingTypeManager.getConsultingTypeSettings(anyInt())) .thenReturn( new de.caritas.cob.agencyservice.consultingtypeservice.generated.web.model.ExtendedConsultingTypeResponseDTO()); when(applicationsettingsControllerApi.getApplicationSettings()).thenReturn(new ApplicationSettingsDTO() .mainTenantSubdomainForSingleDomainMultitenancy(new SettingDTO().value("app"))); when(tenantControllerApi.getRestrictedTenantDataBySubdomain("app")).thenReturn(new RestrictedTenantDTO().id(0L)); + when(tenantServiceApiControllerFactory.createControllerApi()).thenReturn(tenantControllerApi); } @Test diff --git a/src/test/java/de/caritas/cob/agencyservice/api/service/AppointmentServiceTest.java b/src/test/java/de/caritas/cob/agencyservice/api/service/AppointmentServiceTest.java index 9d7eca10..8b4dc058 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/service/AppointmentServiceTest.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/service/AppointmentServiceTest.java @@ -12,6 +12,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import de.caritas.cob.agencyservice.api.repository.agency.Agency; import de.caritas.cob.agencyservice.api.service.securityheader.SecurityHeaderSupplier; +import de.caritas.cob.agencyservice.config.apiclient.AppointmentServiceAgencyApiControllerFactory; import lombok.NonNull; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -48,9 +49,13 @@ class AppointmentServiceTest { @Mock Agency agency; + @Mock + AppointmentServiceAgencyApiControllerFactory appointmentServiceAgencyApiControllerFactory; + @BeforeEach public void beforeEach() { when(securityHeaderSupplier.getKeycloakAndCsrfHttpHeaders()).thenReturn(httpHeaders); + when(appointmentServiceAgencyApiControllerFactory.createControllerApi()).thenReturn(appointmentAgencyApi); } 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 89b151ea..76f81151 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 @@ -5,6 +5,7 @@ import de.caritas.cob.agencyservice.api.service.securityheader.SecurityHeaderSupplier; import de.caritas.cob.agencyservice.api.tenant.TenantContext; +import de.caritas.cob.agencyservice.config.apiclient.ConsultingTypeServiceApiControllerFactory; import de.caritas.cob.agencyservice.consultingtypeservice.generated.ApiClient; import de.caritas.cob.agencyservice.consultingtypeservice.generated.web.ConsultingTypeControllerApi; import org.junit.jupiter.api.Test; @@ -31,6 +32,9 @@ class ConsultingTypeServiceTest { @Spy TenantHeaderSupplier tenantHeaderSupplier; + @Mock + ConsultingTypeServiceApiControllerFactory consultingTypeServiceApiControllerFactory; + @Spy ApiClient apiClient; @@ -38,6 +42,7 @@ class ConsultingTypeServiceTest { void getExtendedConsultingTypeResponseDTO_Should_addTenantHeaderForMultiTenancy() { TenantContext.setCurrentTenant(1L); var headers = new HttpHeaders(); + when(this.consultingTypeServiceApiControllerFactory.createControllerApi()).thenReturn(consultingTypeControllerApi); when(this.consultingTypeControllerApi.getApiClient()).thenReturn(apiClient); when(this.securityHeaderSupplier.getCsrfHttpHeaders()).thenReturn(headers); ReflectionTestUtils.setField(tenantHeaderSupplier, "multitenancy", true); @@ -45,7 +50,7 @@ void getExtendedConsultingTypeResponseDTO_Should_addTenantHeaderForMultiTenancy( HttpHeaders apiClientHeaders = (HttpHeaders) ReflectionTestUtils .getField(apiClient, "defaultHeaders"); - assertEquals(apiClientHeaders.get("tenantId").get(0), "1"); + assertEquals("1", apiClientHeaders.get("tenantId").get(0)); TenantContext.clear(); } @@ -53,12 +58,13 @@ void getExtendedConsultingTypeResponseDTO_Should_addTenantHeaderForMultiTenancy( void getExtendedConsultingTypeResponseDTO_Should_addSecurityHeader() { var headers = new HttpHeaders(); headers.add("header1", "header1"); + when(this.consultingTypeServiceApiControllerFactory.createControllerApi()).thenReturn(consultingTypeControllerApi); when(this.securityHeaderSupplier.getCsrfHttpHeaders()).thenReturn(headers); when(this.consultingTypeControllerApi.getApiClient()).thenReturn(apiClient); this.consultingTypeService.getExtendedConsultingTypeResponseDTO(0); HttpHeaders apiClientHeaders = (HttpHeaders) ReflectionTestUtils .getField(apiClient, "defaultHeaders"); - assertEquals(apiClientHeaders.get("header1").get(0), "header1"); + assertEquals("header1", apiClientHeaders.get("header1").get(0)); TenantContext.clear(); } diff --git a/src/test/java/de/caritas/cob/agencyservice/api/tenant/MultitenancyWithSingleDomainTenantResolverTest.java b/src/test/java/de/caritas/cob/agencyservice/api/tenant/MultitenancyWithSingleDomainTenantResolverTest.java index e62cfd3f..1117b67c 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/tenant/MultitenancyWithSingleDomainTenantResolverTest.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/tenant/MultitenancyWithSingleDomainTenantResolverTest.java @@ -6,6 +6,8 @@ import de.caritas.cob.agencyservice.applicationsettingsservice.generated.web.model.ApplicationSettingsDTO; import de.caritas.cob.agencyservice.applicationsettingsservice.generated.web.model.SettingDTO; +import de.caritas.cob.agencyservice.config.apiclient.ApplicationSettingsApiControllerFactory; +import de.caritas.cob.agencyservice.config.apiclient.TenantServiceApiControllerFactory; import de.caritas.cob.agencyservice.tenantservice.generated.web.model.RestrictedTenantDTO; import java.util.Optional; import javax.servlet.http.HttpServletRequest; @@ -36,6 +38,13 @@ class MultitenancyWithSingleDomainTenantResolverTest { @Mock ApplicationsettingsControllerApi applicationsettingsControllerApi; + @Mock + ApplicationSettingsApiControllerFactory applicationSettingsApiControllerFactory; + + @Mock + TenantServiceApiControllerFactory tenantServiceApiControllerFactory; + + @AfterEach public void tearDown() { setMultitenancyWithSingleDomain(false); @@ -57,6 +66,7 @@ void resolve_Should_NotResolve_When_multitenancyWithSingleDomainNotEnabled() { void resolve_Should_resolveToTenantIdZero_When_ApplicationSettingsNotFound() { // given setMultitenancyWithSingleDomain(true); + when(applicationSettingsApiControllerFactory.createControllerApi()).thenReturn(applicationsettingsControllerApi); when(applicationsettingsControllerApi.getApplicationSettings()).thenReturn( new ApplicationSettingsDTO()); @@ -72,6 +82,9 @@ void resolve_Should_resolveToTenantIdZero_When_ApplicationSettingsNotFound() { void resolve_Should_resolveToTenantIdBasedOnMainTenantSubdomainValue_When_ApplicationSettingsFound() { // given setMultitenancyWithSingleDomain(true); + + when(tenantServiceApiControllerFactory.createControllerApi()).thenReturn(tenantControllerApi); + when(applicationSettingsApiControllerFactory.createControllerApi()).thenReturn(applicationsettingsControllerApi); when(applicationsettingsControllerApi.getApplicationSettings()).thenReturn( new ApplicationSettingsDTO().mainTenantSubdomainForSingleDomainMultitenancy( new SettingDTO().value("app")));