From 6cfdee1a3bce6246c3b96aa319f6f98502c43232 Mon Sep 17 00:00:00 2001 From: Krzysztof Witukiewicz <krzysztof.witukiewicz@mgm-tp.com> Date: Fri, 17 Jan 2025 17:10:29 +0100 Subject: [PATCH] OZG-6706 OZG-7497 Remove Optional from result type --- .../alfa/bescheid/BescheidRemoteService.java | 25 +++++--- .../alfa/bescheid/BescheidService.java | 2 +- .../bescheid/BescheidRemoteServiceTest.java | 61 +++++-------------- .../alfa/bescheid/BescheidServiceTest.java | 13 +--- lombok.config | 3 +- 5 files changed, 36 insertions(+), 68 deletions(-) diff --git a/alfa-service/src/main/java/de/ozgcloud/alfa/bescheid/BescheidRemoteService.java b/alfa-service/src/main/java/de/ozgcloud/alfa/bescheid/BescheidRemoteService.java index d3abd51e1b..61e2022de3 100644 --- a/alfa-service/src/main/java/de/ozgcloud/alfa/bescheid/BescheidRemoteService.java +++ b/alfa-service/src/main/java/de/ozgcloud/alfa/bescheid/BescheidRemoteService.java @@ -26,28 +26,28 @@ package de.ozgcloud.alfa.bescheid; import java.util.Optional; import java.util.stream.Stream; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import de.ozgcloud.alfa.common.GrpcUtil; import de.ozgcloud.document.bescheid.BescheidServiceGrpc.BescheidServiceBlockingStub; import de.ozgcloud.document.bescheid.GrpcBescheid; import de.ozgcloud.document.bescheid.GrpcBescheidManagerConfigRequest; +import de.ozgcloud.document.bescheid.GrpcBescheidManagerConfigResponse; import de.ozgcloud.document.bescheid.GrpcGetAllBescheidRequest; import de.ozgcloud.document.bescheid.GrpcGetBescheidDraftRequest; import de.ozgcloud.document.bescheid.GrpcGetBescheidDraftResponse; import de.ozgcloud.document.bescheid.GrpcGetBescheidRequest; +import lombok.RequiredArgsConstructor; import net.devh.boot.grpc.client.inject.GrpcClient; @Service +@RequiredArgsConstructor class BescheidRemoteService { @GrpcClient(GrpcUtil.VORGANG_MANAGER_GRPC_CLIENT) - private BescheidServiceBlockingStub bescheidServiceStub; - @Autowired - private BescheidMapper bescheidMapper; - @Autowired - private BescheidManagerFeaturesMapper bescheidManagerFeaturesMapper; + private final BescheidServiceBlockingStub bescheidServiceStub; + private final BescheidMapper bescheidMapper; + private final BescheidManagerFeaturesMapper bescheidManagerFeaturesMapper; public Optional<Bescheid> getBescheidDraft(String vorgangId) { var request = buildGetBescheidDraftRequest(vorgangId); @@ -85,8 +85,15 @@ class BescheidRemoteService { return GrpcGetAllBescheidRequest.newBuilder().setVorgangId(vorgangId).build(); } - public Optional<BescheidManagerFeatures> getBescheidManagerFeatures() { - var response = bescheidServiceStub.getConfig(GrpcBescheidManagerConfigRequest.newBuilder().build()); - return response.hasFeatures() ? Optional.of(bescheidManagerFeaturesMapper.fromGrpc(response.getFeatures())) : Optional.empty(); + public BescheidManagerFeatures getBescheidManagerFeatures() { + return getFeatures(getConfig()); + } + + private GrpcBescheidManagerConfigResponse getConfig() { + return bescheidServiceStub.getConfig(GrpcBescheidManagerConfigRequest.newBuilder().build()); + } + + private BescheidManagerFeatures getFeatures(GrpcBescheidManagerConfigResponse configResponse) { + return bescheidManagerFeaturesMapper.fromGrpc(configResponse.getFeatures()); } } \ No newline at end of file diff --git a/alfa-service/src/main/java/de/ozgcloud/alfa/bescheid/BescheidService.java b/alfa-service/src/main/java/de/ozgcloud/alfa/bescheid/BescheidService.java index df312594f1..347586b079 100644 --- a/alfa-service/src/main/java/de/ozgcloud/alfa/bescheid/BescheidService.java +++ b/alfa-service/src/main/java/de/ozgcloud/alfa/bescheid/BescheidService.java @@ -59,7 +59,7 @@ public class BescheidService { } public boolean canCreateBescheidDocumentAutomatically() { - return remoteService.getBescheidManagerFeatures().map(BescheidManagerFeatures::isCanCreateBescheidDocument).orElse(false); + return remoteService.getBescheidManagerFeatures().isCanCreateBescheidDocument(); } public boolean existsBescheid(String vorgangId) { diff --git a/alfa-service/src/test/java/de/ozgcloud/alfa/bescheid/BescheidRemoteServiceTest.java b/alfa-service/src/test/java/de/ozgcloud/alfa/bescheid/BescheidRemoteServiceTest.java index e5aae0f2f5..8b2de8c267 100644 --- a/alfa-service/src/test/java/de/ozgcloud/alfa/bescheid/BescheidRemoteServiceTest.java +++ b/alfa-service/src/test/java/de/ozgcloud/alfa/bescheid/BescheidRemoteServiceTest.java @@ -212,64 +212,33 @@ class BescheidRemoteServiceTest { private final GrpcBescheidManagerConfigRequest request = GrpcBescheidManagerConfigRequestTestFactory.create(); private final GrpcBescheidManagerConfigResponse respone = GrpcBescheidManagerConfigResponseTestFactory.create(); + private final BescheidManagerFeatures mappedFeatures = BescheidManagerFeatures.builder().build(); - @Test - void shouldCallGrpcService() { + @BeforeEach + void setUp() { when(bescheidServiceStub.getConfig(request)).thenReturn(respone); - when(featuresMapper.fromGrpc(any())).thenReturn(BescheidManagerFeatures.builder().build()); + when(featuresMapper.fromGrpc(any())).thenReturn(mappedFeatures); + } + @Test + void shouldCallGrpcService() { service.getBescheidManagerFeatures(); verify(bescheidServiceStub).getConfig(request); } - @Nested - class OnFeaturesArePresent { - - private final BescheidManagerFeatures mappedFeatures = BescheidManagerFeatures.builder().build(); - - @BeforeEach - void setUp() { - when(bescheidServiceStub.getConfig(request)).thenReturn(respone); - when(featuresMapper.fromGrpc(any())).thenReturn(mappedFeatures); - } - - @Test - void shouldCallMapper() { - service.getBescheidManagerFeatures(); - - verify(featuresMapper).fromGrpc(GrpcBescheidManagerFeaturesTestFactory.create()); - } - - @Test - void shouldReturnMappedFeatures() { - var features = service.getBescheidManagerFeatures(); + @Test + void shouldCallMapper() { + service.getBescheidManagerFeatures(); - assertThat(features).isNotEmpty().get().isSameAs(mappedFeatures); - } + verify(featuresMapper).fromGrpc(GrpcBescheidManagerFeaturesTestFactory.create()); } - @Nested - class OnFeaturesAreAbsent { - - @BeforeEach - void init() { - when(bescheidServiceStub.getConfig(request)).thenReturn(GrpcBescheidManagerConfigResponse.newBuilder().build()); - } - - @Test - void shouldNotCallMapper() { - service.getBescheidManagerFeatures(); - - verify(featuresMapper, never()).fromGrpc(any()); - } - - @Test - void shouldReturnEmpty() { - var features = service.getBescheidManagerFeatures(); + @Test + void shouldReturnMappedFeatures() { + var features = service.getBescheidManagerFeatures(); - assertThat(features).isEmpty(); - } + assertThat(features).isSameAs(mappedFeatures); } } diff --git a/alfa-service/src/test/java/de/ozgcloud/alfa/bescheid/BescheidServiceTest.java b/alfa-service/src/test/java/de/ozgcloud/alfa/bescheid/BescheidServiceTest.java index 95914f9d61..64f4c2b236 100644 --- a/alfa-service/src/test/java/de/ozgcloud/alfa/bescheid/BescheidServiceTest.java +++ b/alfa-service/src/test/java/de/ozgcloud/alfa/bescheid/BescheidServiceTest.java @@ -168,27 +168,18 @@ class BescheidServiceTest { @Test void shouldCallRemoteService() { - when(remoteService.getBescheidManagerFeatures()).thenReturn(Optional.of(BescheidManagerFeatures.builder().build())); + when(remoteService.getBescheidManagerFeatures()).thenReturn(BescheidManagerFeatures.builder().build()); service.canCreateBescheidDocumentAutomatically(); verify(remoteService).getBescheidManagerFeatures(); } - @Test - void shouldReturnFalseIfFeaturesAreEmpty() { - when(remoteService.getBescheidManagerFeatures()).thenReturn(Optional.empty()); - - var canCreate = service.canCreateBescheidDocumentAutomatically(); - - assertThat(canCreate).isFalse(); - } - @ParameterizedTest @ValueSource(booleans = { true, false }) void shouldReturnFeatureValue(boolean featureValue) { when(remoteService.getBescheidManagerFeatures()).thenReturn( - Optional.of(BescheidManagerFeatures.builder().canCreateBescheidDocument(featureValue).build())); + BescheidManagerFeatures.builder().canCreateBescheidDocument(featureValue).build()); var canCreate = service.canCreateBescheidDocumentAutomatically(); diff --git a/lombok.config b/lombok.config index 32903abaf7..d248ae3c4d 100644 --- a/lombok.config +++ b/lombok.config @@ -27,4 +27,5 @@ lombok.log.slf4j.flagUsage = ERROR lombok.log.log4j.flagUsage = ERROR lombok.data.flagUsage = ERROR lombok.nonNull.exceptionType = IllegalArgumentException -lombok.addLombokGeneratedAnnotation = true \ No newline at end of file +lombok.addLombokGeneratedAnnotation = true +lombok.copyableAnnotations += net.devh.boot.grpc.client.inject.GrpcClient \ No newline at end of file -- GitLab