diff --git a/api-lib-core/src/main/java/de/ozgcloud/apilib/file/grpc/GrpcOzgCloudFileService.java b/api-lib-core/src/main/java/de/ozgcloud/apilib/file/grpc/GrpcOzgCloudFileService.java index a3a6af6ba7375e0ef74b9e6b208a120f3f160dab..6223653f6f0333fdb86e0374cec8c02ac42e602c 100644 --- a/api-lib-core/src/main/java/de/ozgcloud/apilib/file/grpc/GrpcOzgCloudFileService.java +++ b/api-lib-core/src/main/java/de/ozgcloud/apilib/file/grpc/GrpcOzgCloudFileService.java @@ -9,8 +9,6 @@ import java.util.concurrent.TimeoutException; import java.util.logging.Level; import org.apache.commons.io.IOUtils; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.stereotype.Service; import com.google.protobuf.ByteString; @@ -40,8 +38,6 @@ import lombok.RequiredArgsConstructor; import lombok.extern.java.Log; import net.devh.boot.grpc.client.inject.GrpcClient; -@Service -@ConditionalOnProperty("ozgcloud.file-manager.address") @RequiredArgsConstructor @Log public class GrpcOzgCloudFileService implements OzgCloudFileService { diff --git a/ozg-cloud-spring-boot-starter/src/main/java/de/ozgcloud/client/autoconfigure/OzgCloudClientAutoConfiguration.java b/ozg-cloud-spring-boot-starter/src/main/java/de/ozgcloud/client/autoconfigure/OzgCloudClientAutoConfiguration.java index 7772fc7788051c04d86a94ffba188b4baab30ad5..8435df921992fd12b642683eda277b4739c78f9c 100644 --- a/ozg-cloud-spring-boot-starter/src/main/java/de/ozgcloud/client/autoconfigure/OzgCloudClientAutoConfiguration.java +++ b/ozg-cloud-spring-boot-starter/src/main/java/de/ozgcloud/client/autoconfigure/OzgCloudClientAutoConfiguration.java @@ -4,7 +4,6 @@ import java.util.Collection; import java.util.Map; import org.mapstruct.factory.Mappers; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -22,9 +21,11 @@ import de.ozgcloud.apilib.common.callcontext.OzgCloudCallContextProvider; import de.ozgcloud.apilib.common.command.OzgCloudCommandService; import de.ozgcloud.apilib.common.command.grpc.CommandMapper; import de.ozgcloud.apilib.common.command.grpc.GrpcOzgCloudCommandService; -import de.ozgcloud.apilib.file.dummy.DummyOzgCloudFileService; +import de.ozgcloud.apilib.file.OzgCloudFileService; import de.ozgcloud.apilib.file.grpc.GrpcOzgCloudFileService; import de.ozgcloud.apilib.file.grpc.OzgCloudFileMapper; +import de.ozgcloud.apilib.user.GrpcOzgCloudUserProfileService; +import de.ozgcloud.apilib.user.OzgCloudUserProfileService; import de.ozgcloud.apilib.user.UserProfileMapper; import de.ozgcloud.apilib.vorgang.OzgCloudVorgangIdMapper; import de.ozgcloud.apilib.vorgang.OzgCloudVorgangService; @@ -33,20 +34,20 @@ import de.ozgcloud.apilib.vorgang.grpc.GrpcOzgCloudVorgangService; import de.ozgcloud.apilib.vorgang.grpc.OzgCloudVorgangMapper; import de.ozgcloud.apilib.vorgang.grpc.OzgCloudVorgangQueryMapper; import de.ozgcloud.apilib.vorgang.grpc.OzgCloudVorgangStubMapper; +import de.ozgcloud.user.grpc.userprofile.UserProfileServiceGrpc; +import de.ozgcloud.vorgang.grpc.binaryFile.BinaryFileServiceGrpc; import de.ozgcloud.vorgang.grpc.command.CommandServiceGrpc; import de.ozgcloud.vorgang.vorgang.VorgangServiceGrpc.VorgangServiceBlockingStub; import io.grpc.ManagedChannelBuilder; +import lombok.RequiredArgsConstructor; import net.devh.boot.grpc.client.autoconfigure.GrpcClientAutoConfiguration; import net.devh.boot.grpc.client.config.GrpcChannelProperties; import net.devh.boot.grpc.client.config.GrpcChannelsProperties; @AutoConfiguration(before = GrpcClientAutoConfiguration.class) @ComponentScan("de.ozgcloud.client.autoconfigure") -@Import({ - GrpcOzgCloudFileService.class, DummyOzgCloudFileService.class, - OzgCloudCallContextAttachingInterceptor.class -}) - +@Import({ OzgCloudCallContextAttachingInterceptor.class }) +@RequiredArgsConstructor public class OzgCloudClientAutoConfiguration { private static final String CLIENT_NAME_VORGANG_MANAGER = "vorgang-manager"; @@ -54,14 +55,10 @@ public class OzgCloudClientAutoConfiguration { private static final String CLIENT_NAME_COMMAND_MANAGER = "ozgcloud-command-manager"; private static final String CLIENT_NAME_USER_MANAGER = "ozgcloud-user-manager"; - @Autowired - private OzgCloudVorgangManagerProperties vorgangManagerProperties; - @Autowired - private OzgCloudFileManagerProperties fileManagerProperties; - @Autowired - private OzgCloudCommandManagerProperties commandManagerProperties; - @Autowired - private OzgCloudUserManagerProperties userManagerProperties; + private final OzgCloudVorgangManagerProperties vorgangManagerProperties; + private final OzgCloudFileManagerProperties fileManagerProperties; + private final OzgCloudCommandManagerProperties commandManagerProperties; + private final OzgCloudUserManagerProperties userManagerProperties; // @Bean // @ConditionalOnProperty("ozgcloud.vorgang-manager.address") @@ -99,7 +96,7 @@ public class OzgCloudClientAutoConfiguration { @Bean GrpcChannelsProperties clientProperties(Collection<GrpcChannelConfigurator> configurators) { var properties = new GrpcChannelsProperties(); - configurators.stream().forEach(configurator -> configurator.addToProperties(properties)); + configurators.forEach(configurator -> configurator.addToProperties(properties)); return properties; } @@ -138,7 +135,7 @@ public class OzgCloudClientAutoConfiguration { @ConditionalOnProperty("ozgcloud.vorgang-manager.address") @ConditionalOnMissingBean(OzgCloudVorgangService.class) OzgCloudVorgangService grpcOzgCloudVorgangService(VorgangServiceBlockingStub vorgangServiceStub, OzgCloudVorgangMapper mapper, - OzgCloudVorgangStubMapper stubMapper, OzgCloudVorgangQueryMapper queryMapper, OzgCloudCallContextProvider contextProvider) { + OzgCloudVorgangStubMapper stubMapper, OzgCloudVorgangQueryMapper queryMapper, OzgCloudCallContextProvider contextProvider) { return new GrpcOzgCloudVorgangService(vorgangServiceStub, mapper, stubMapper, queryMapper, contextProvider); } @@ -210,13 +207,22 @@ public class OzgCloudClientAutoConfiguration { return Mappers.getMapper(OzgCloudFileMapper.class); } - // @Bean - // @ConditionalOnProperty("ozgcloud.user-manager.address") - // OzgCloudUserProfileService - // grpcOzgCloudUserProfileService(@GrpcClient("ozgcloud-user-manager") - // UserProfileServiceBlockingStub grpcStub, - // UserProfileMapper mapper) { - // System.out.println("#### building UM-ProfileService"); - // return new GrpcOzgCloudUserProfileService(grpcStub, mapper); - // } + @Bean + @ConditionalOnMissingBean + @ConditionalOnProperty("ozgcloud.user-manager.address") + OzgCloudUserProfileService grpcOzgCloudUserProfileService(UserProfileMapper mapper, OzgCloudCallContextProvider contextProvider) { + var channel = ManagedChannelBuilder.forTarget(userManagerProperties.getAddress()).usePlaintext().build(); + var userServiceStub = UserProfileServiceGrpc.newBlockingStub(channel); + return new GrpcOzgCloudUserProfileService(userServiceStub, mapper, contextProvider); + } + + @Bean + @ConditionalOnProperty("ozgcloud.file-manager.address") + @ConditionalOnMissingBean + OzgCloudFileService grpcOzgCloudFileService(OzgCloudFileMapper mapper, OzgCloudCallContextProvider contextProvider) { + var channel = ManagedChannelBuilder.forTarget(fileManagerProperties.getAddress()).usePlaintext().build(); + var blockingStub = BinaryFileServiceGrpc.newBlockingStub(channel); + var asyncStub = BinaryFileServiceGrpc.newStub(channel); + return new GrpcOzgCloudFileService(blockingStub, asyncStub, contextProvider, mapper); + } } diff --git a/ozg-cloud-spring-boot-starter/src/test/java/de/ozgcloud/client/autoconfigure/OzgCloudFileServiceStarterITCase.java b/ozg-cloud-spring-boot-starter/src/test/java/de/ozgcloud/client/autoconfigure/OzgCloudFileServiceStarterITCase.java new file mode 100644 index 0000000000000000000000000000000000000000..0581131a576eb0164e25074b68084a423d2808b7 --- /dev/null +++ b/ozg-cloud-spring-boot-starter/src/test/java/de/ozgcloud/client/autoconfigure/OzgCloudFileServiceStarterITCase.java @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2024 Das Land Schleswig-Holstein vertreten durch den + * Ministerpräsidenten des Landes Schleswig-Holstein + * Staatskanzlei + * Abteilung Digitalisierung und zentrales IT-Management der Landesregierung + * + * Lizenziert unter der EUPL, Version 1.2 oder - sobald + * diese von der Europäischen Kommission genehmigt wurden - + * Folgeversionen der EUPL ("Lizenz"); + * Sie dürfen dieses Werk ausschließlich gemäß + * dieser Lizenz nutzen. + * Eine Kopie der Lizenz finden Sie hier: + * + * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 + * + * Sofern nicht durch anwendbare Rechtsvorschriften + * gefordert oder in schriftlicher Form vereinbart, wird + * die unter der Lizenz verbreitete Software "so wie sie + * ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN - + * ausdrücklich oder stillschweigend - verbreitet. + * Die sprachspezifischen Genehmigungen und Beschränkungen + * unter der Lizenz sind dem Lizenztext zu entnehmen. + */ +package de.ozgcloud.client.autoconfigure; + +import static org.assertj.core.api.Assertions.*; + +import java.util.Optional; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Import; +import org.springframework.test.annotation.DirtiesContext; + +import de.ozgcloud.apilib.autoconfig.ApiLibTestApplication; +import de.ozgcloud.apilib.file.OzgCloudFileService; +import de.ozgcloud.apilib.file.dummy.DummyOzgCloudFileService; +import de.ozgcloud.apilib.file.grpc.GrpcOzgCloudFileService; + +public class OzgCloudFileServiceStarterITCase { + + @Nested + @SpringBootTest(classes = ApiLibTestApplication.class) + @DirtiesContext() + class TestWithoutOzgCloudFileService { + + @Autowired + private Optional<OzgCloudFileService> ozgCloudFileService; + + @Test + void shouldStart() { + assertThat(ozgCloudFileService).isEmpty(); + } + } + + @Nested + @SpringBootTest(classes = ApiLibTestApplication.class, properties = { + "ozgcloud.file-manager.address=address" + }) + @DirtiesContext + class TestWithOzgCloudFileService { + + @Autowired + private OzgCloudFileService ozgCloudFileService; + + @Test + void shouldStart() { + assertThat(ozgCloudFileService).isInstanceOf(GrpcOzgCloudFileService.class); + } + } + + @Nested + @SpringBootTest(classes = ApiLibTestApplication.class, properties = { + "ozgcloud.file-manager.address=address" + }) + @Import(TestWithExistingOzgCloudFileService.Config.class) + class TestWithExistingOzgCloudFileService { + + @Autowired + private OzgCloudFileService ozgCloudFileService; + + @Test + void shouldStart() { // NOSONAR + assertThat(ozgCloudFileService).isInstanceOf(DummyOzgCloudFileService.class); + } + + static class Config { + + @Bean + OzgCloudFileService ozgCloudFileService() { + return new DummyOzgCloudFileService(); + } + } + } +} diff --git a/ozg-cloud-spring-boot-starter/src/test/java/de/ozgcloud/client/autoconfigure/OzgCloudUserProfileServiceStarterITCase.java b/ozg-cloud-spring-boot-starter/src/test/java/de/ozgcloud/client/autoconfigure/OzgCloudUserProfileServiceStarterITCase.java new file mode 100644 index 0000000000000000000000000000000000000000..a3b6aaf8d1a2daff89db50d6c6f2d457020e12e3 --- /dev/null +++ b/ozg-cloud-spring-boot-starter/src/test/java/de/ozgcloud/client/autoconfigure/OzgCloudUserProfileServiceStarterITCase.java @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2024 Das Land Schleswig-Holstein vertreten durch den + * Ministerpräsidenten des Landes Schleswig-Holstein + * Staatskanzlei + * Abteilung Digitalisierung und zentrales IT-Management der Landesregierung + * + * Lizenziert unter der EUPL, Version 1.2 oder - sobald + * diese von der Europäischen Kommission genehmigt wurden - + * Folgeversionen der EUPL ("Lizenz"); + * Sie dürfen dieses Werk ausschließlich gemäß + * dieser Lizenz nutzen. + * Eine Kopie der Lizenz finden Sie hier: + * + * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 + * + * Sofern nicht durch anwendbare Rechtsvorschriften + * gefordert oder in schriftlicher Form vereinbart, wird + * die unter der Lizenz verbreitete Software "so wie sie + * ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN - + * ausdrücklich oder stillschweigend - verbreitet. + * Die sprachspezifischen Genehmigungen und Beschränkungen + * unter der Lizenz sind dem Lizenztext zu entnehmen. + */ +package de.ozgcloud.client.autoconfigure; + +import static org.assertj.core.api.Assertions.*; + +import java.util.Optional; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Import; +import org.springframework.test.annotation.DirtiesContext; + +import de.ozgcloud.apilib.autoconfig.ApiLibTestApplication; +import de.ozgcloud.apilib.user.GrpcOzgCloudUserProfileService; +import de.ozgcloud.apilib.user.OzgCloudUserId; +import de.ozgcloud.apilib.user.OzgCloudUserProfile; +import de.ozgcloud.apilib.user.OzgCloudUserProfileService; + +public class OzgCloudUserProfileServiceStarterITCase { + + @Nested + @SpringBootTest(classes = ApiLibTestApplication.class) + @DirtiesContext() + class TestNoOzgCloudUserProfileService { + + @Autowired + private Optional<OzgCloudUserProfileService> ozgCloudUserProfileService; + + @Test + void shouldStart() { + assertThat(ozgCloudUserProfileService).isEmpty(); + } + } + + @Nested + @SpringBootTest(classes = ApiLibTestApplication.class, properties = { + "ozgcloud.user-manager.address=address" + }) + @DirtiesContext + class TestWithOzgCloudUserProfileService { + + @Autowired + private OzgCloudUserProfileService ozgCloudUserProfileService; + + @Test + void shouldStart() { + assertThat(ozgCloudUserProfileService).isInstanceOf(GrpcOzgCloudUserProfileService.class); + } + } + + @Nested + @SpringBootTest(classes = ApiLibTestApplication.class, properties = { + "ozgcloud.file-manager.address=address" + }) + @Import(TestWithExistingOzgCloudFileService.Config.class) + class TestWithExistingOzgCloudFileService { + + @Autowired + private OzgCloudUserProfileService ozgCloudUserProfileService; + + @Test + void shouldStart() { // NOSONAR + assertThat(ozgCloudUserProfileService).isInstanceOf(DummyOzgCloudUserProfileService.class); + } + + record DummyOzgCloudUserProfileService() implements OzgCloudUserProfileService { + + @Override + public OzgCloudUserProfile getById(final OzgCloudUserId userId) { + return OzgCloudUserProfile.builder().build(); + } + } + + static class Config { + + @Bean + OzgCloudUserProfileService zgCloudUserProfileService() { + return new DummyOzgCloudUserProfileService(); + } + } + } +}