diff --git a/router/src/main/java/de/ozgcloud/eingang/router/VorgangManagerServerResolver.java b/router/src/main/java/de/ozgcloud/eingang/router/VorgangManagerServerResolver.java index a4563b1df33ac97015b472ae870fdc4f7f03765a..996bc649d7d78262422b8c63b854b8070e6c0ba5 100644 --- a/router/src/main/java/de/ozgcloud/eingang/router/VorgangManagerServerResolver.java +++ b/router/src/main/java/de/ozgcloud/eingang/router/VorgangManagerServerResolver.java @@ -95,7 +95,12 @@ public class VorgangManagerServerResolver { } } - private AbstractStub<?> createStubByConfiguredChannels(Optional<String> organisationsEinheitId, StubFactory stubFactory, + AbstractStub<?> createStubUsingZufi(Optional<String> organisationsEinheitId, StubFactory stubFactory, + Class<? extends AbstractStub<?>> stubClass) { + return null; + } + + AbstractStub<?> createStubByConfiguredChannels(Optional<String> organisationsEinheitId, StubFactory stubFactory, Class<? extends AbstractStub<?>> stubClass) { var channelName = getChannelName(organisationsEinheitId); var stub = stubFactory.createStub(stubClass, createChannelByName(channelName)); @@ -103,23 +108,16 @@ public class VorgangManagerServerResolver { return applyStubTransformers(stub, channelName); } - AbstractStub<?> applyStubTransformers(AbstractStub<?> stub, String channelName) { // NOSONAR wildcard given by StubTransformer - for (var transformer : stubTransformers) { - stub = transformer.transform(channelName, stub); - } - return stub; - } - String getChannelName(Optional<String> organisationsEinheitId) { - Optional<String> target; + return getChannelTarget(organisationsEinheitId).map(this::addChannelPrefix).orElseGet(this::getFundstelleChannelName); + } + private Optional<String> getChannelTarget(Optional<String> organisationsEinheitId) { if (properties.getRoutingStrategy() == RoutingStrategy.SINGLE) { - target = properties.getTargetVorgangManagerName(); + return properties.getTargetVorgangManagerName(); } else { - target = organisationsEinheitId.map(properties.getOrganisationseinheiten()::get); + return organisationsEinheitId.map(properties.getOrganisationseinheiten()::get); } - - return target.map(this::addChannelPrefix).orElseGet(this::getFundstelleChannelName); } private String addChannelPrefix(@NonNull String name) { @@ -144,14 +142,10 @@ public class VorgangManagerServerResolver { return grpcChannelFactory.createChannel(name); } - AbstractStub<?> createStubUsingZufi(Optional<String> organisationsEinheitId, StubFactory stubFactory, - Class<? extends AbstractStub<?>> stubClass) { - // TODO implement me - - // call zufiManager to get URL - // create channel - // create stub - - return null; + AbstractStub<?> applyStubTransformers(AbstractStub<?> stub, String channelName) { // NOSONAR wildcard given by StubTransformer + for (var transformer : stubTransformers) { + stub = transformer.transform(channelName, stub); + } + return stub; } } \ No newline at end of file diff --git a/router/src/test/java/de/ozgcloud/eingang/router/VorgangManagerServerResolverTest.java b/router/src/test/java/de/ozgcloud/eingang/router/VorgangManagerServerResolverTest.java index 925311a7104b83c98c426c3b316f0f009cc1ac5e..f4274268163ca9745e5e3a4a325c21578b335ac6 100644 --- a/router/src/test/java/de/ozgcloud/eingang/router/VorgangManagerServerResolverTest.java +++ b/router/src/test/java/de/ozgcloud/eingang/router/VorgangManagerServerResolverTest.java @@ -33,6 +33,7 @@ import java.util.Collections; import java.util.Optional; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; @@ -40,8 +41,10 @@ import org.mockito.Mock; import org.mockito.Spy; import org.springframework.test.util.ReflectionTestUtils; +import de.ozgcloud.eingang.common.formdata.ZustaendigeStelleTestFactory; import de.ozgcloud.eingang.router.errorhandling.AdapterConfigurationException; import de.ozgcloud.eingang.router.errorhandling.UnknownOrganisationseinheitException; +import de.ozgcloud.vorgang.grpc.binaryFile.BinaryFileServiceGrpc.BinaryFileServiceStub; import de.ozgcloud.vorgang.vorgang.VorgangServiceGrpc.VorgangServiceBlockingStub; import io.grpc.Channel; import io.grpc.stub.AbstractStub; @@ -50,6 +53,7 @@ import net.devh.boot.grpc.client.inject.StubTransformer; import net.devh.boot.grpc.client.stubfactory.StubFactory; class VorgangManagerServerResolverTest { + @Spy @InjectMocks private VorgangManagerServerResolver resolver; @@ -60,115 +64,167 @@ class VorgangManagerServerResolverTest { private StubFactory stubFactory; @Nested - class TestCreateChannel { + class TestFindStubFactory { + + @Mock + private StubFactory stubFactory; @Test - void shouldCallChannelFactory() { - resolver.createChannelByName(VorgangManagerListPropertiesTestFactory.VORGANG_MANAGER_NAME); + void shouldSetApplicableFactory() { + when(stubFactory.isApplicable(any())).thenReturn(true); + setStubFactories(stubFactory, stubFactory); - verify(channelFactory).createChannel(VorgangManagerListPropertiesTestFactory.VORGANG_MANAGER_NAME); + resolver.findApplicableStubFactories(); + + assertThat(ReflectionTestUtils.getField(resolver, "vorgangStubFactory")).isSameAs(stubFactory); + } + + @Test + void shouldThrowExceptionIfNotFound() { + setStubFactories(stubFactory); + + assertThrows(AdapterConfigurationException.class, () -> resolver.findApplicableStubFactories()); + } + + private void setStubFactories(StubFactory... factories) { + ReflectionTestUtils.setField(resolver, "stubFactories", Arrays.asList(factories)); } } + @DisplayName("Resolve vorgangManager service by organisationsEinheitenId") @Nested - class TestGetFundstellenChannelName { + class TestResolveVorgangManagerServiceStubByOrganisationsEinheitenId { - @Nested - class TestStrategyFundstelle { - - @Test - void shouldCreateChannel() { - setProperties(VorgangManagerListPropertiesTestFactory.createWithFundstelle()); + @Mock + private VorgangServiceBlockingStub stub; - var channel = resolver.getFundstelleChannelName(); + private final Optional<String> organisationsEinheitenId = Optional.of(ZustaendigeStelleTestFactory.ORGANISATIONSEINHEIT_ID); - assertThat(channel).isNotNull().isEqualTo(VorgangManagerListPropertiesTestFactory.FUNDSTELLE_CHANNEL_NAME); - } + @BeforeEach + void mock() { + doReturn(stub).when(resolver).createStub(any(), any(), any()); + } - @Test - void shouldThrowExceptionIfFundstelleIsMissing() { - var props = VorgangManagerListPropertiesTestFactory.createWithFundstelle(); - props.setFundstelleVorgangManagerName(Optional.empty()); - setProperties(props); + @Test + void shouldCallCreateStub() { + resolveStub(); - assertThrows(AdapterConfigurationException.class, () -> resolver.getFundstelleChannelName()); - } + verify(resolver).createStub(eq(organisationsEinheitenId), any(), eq(VorgangServiceBlockingStub.class)); } - @Nested - class TestStrategyDeny { + @Test + void shouldReturnManageableStub() { + var createdStub = resolveStub(); - @Test - void shouldThrowException() { - setProperties(VorgangManagerListPropertiesTestFactory.createForMultiRouting()); + assertThat(createdStub).isEqualTo(stub); + } - assertThrows(UnknownOrganisationseinheitException.class, () -> resolver.getFundstelleChannelName()); - } + private VorgangServiceBlockingStub resolveStub() { + return resolver.resolveVorgangServiceBlockingStubByOrganisationseinheitenId(organisationsEinheitenId); } } + @DisplayName("Resolve binaryFile service by organisationsEinheitenId") @Nested - class TestGetChannelName { + class TestResolveBinaryFileServiceStubByOrganisationsEinheitenId { - @Test - void shouldUseSingleName() { - setProperties(VorgangManagerListPropertiesTestFactory.createForSingleRouting()); + @Mock + private BinaryFileServiceStub stub; - var name = resolver.getChannelName(Optional.empty()); + private final Optional<String> organisationsEinheitenId = Optional.of(ZustaendigeStelleTestFactory.ORGANISATIONSEINHEIT_ID); - assertThat(name).contains(VorgangManagerListPropertiesTestFactory.CHANNEL_NAME); + @BeforeEach + void mock() { + doReturn(stub).when(resolver).createStub(any(), any(), any()); } @Test - void shouldUseNameFromMap() { - setProperties(VorgangManagerListPropertiesTestFactory.createForMultiRouting()); + void shouldCallCreateStub() { + resolveStub(); - var name = resolver.getChannelName(Optional.of(VorgangManagerListPropertiesTestFactory.ORGANISATIONSEINHEIT_ID)); - - assertThat(name).contains(VorgangManagerListPropertiesTestFactory.CHANNEL_NAME); + verify(resolver).createStub(eq(organisationsEinheitenId), any(), eq(BinaryFileServiceStub.class)); } @Test - void shouldGetFundstellenName() { - setProperties(VorgangManagerListPropertiesTestFactory.createWithFundstelle()); + void shouldReturnManageableStub() { + var createdStub = resolveStub(); - var name = resolver.getChannelName(Optional.of("4711")); + assertThat(createdStub).isEqualTo(stub); + } - verify(resolver).getFundstelleChannelName(); - assertThat(name).isEqualTo(VorgangManagerListPropertiesTestFactory.FUNDSTELLE_CHANNEL_NAME); + private BinaryFileServiceStub resolveStub() { + return resolver.resolveBinaryFileServiceStubByOrganisationsEinheitId(organisationsEinheitenId); } } + @DisplayName("Create stub") @Nested - class TestFindStubFactory { + class TestCreateStub { @Mock - private StubFactory stubFactory; + private AbstractStub<?> createdStub; - @Test - void shouldSetApplicableFactory() { - when(stubFactory.isApplicable(any())).thenReturn(true); - setStubFactories(stubFactory, stubFactory); + private Class<? extends AbstractStub<?>> stubClass = VorgangServiceBlockingStub.class; + private Optional<String> organisationsEinheitenId = Optional.of(VorgangManagerListPropertiesTestFactory.ORGANISATIONSEINHEIT_ID); - resolver.findApplicableStubFactories(); + @DisplayName("on zufi routing") + @Nested + class TestOnZufiRouting { - assertThat(ReflectionTestUtils.getField(resolver, "vorgangStubFactory")).isSameAs(stubFactory); + @BeforeEach + void mock() { + setProperties(VorgangManagerListPropertiesTestFactory.createForZufiRouting()); + doReturn(createdStub).when(resolver).createStubUsingZufi(any(), any(), any()); + } + + @Test + void shouldCreateStubUsingZufi() { + createStub(); + + verify(resolver).createStubUsingZufi(organisationsEinheitenId, stubFactory, stubClass); + } + + @Test + void shouldReturnCreatedStub() { + var stub = createStub(); + + assertThat(stub).isEqualTo(createdStub); + } } - @Test - void shouldThrowExceptionIfNotFound() { - setStubFactories(stubFactory); + @DisplayName("On other routing") + @Nested + class TestOnOtherRouting { - assertThrows(AdapterConfigurationException.class, () -> resolver.findApplicableStubFactories()); + @BeforeEach + void mock() { + setProperties(VorgangManagerListPropertiesTestFactory.createForSingleRouting()); + doReturn(createdStub).when(resolver).createStubByConfiguredChannels(any(), any(), any()); + } + + @Test + void shouldCreateStubByConfiguredChannels() { + createStub(); + + verify(resolver).createStubByConfiguredChannels(organisationsEinheitenId, stubFactory, stubClass); + } + + @Test + void shouldReturnCreatedStub() { + var stub = createStub(); + + assertThat(stub).isEqualTo(createdStub); + } } - private void setStubFactories(StubFactory... factories) { - ReflectionTestUtils.setField(resolver, "stubFactories", Arrays.asList(factories)); + private AbstractStub<?> createStub() { + return resolver.createStub(organisationsEinheitenId, stubFactory, stubClass); } } + @DisplayName("Create stub by configured channels") @Nested - class TestCreateStub { + class TestCreateStubByConfiguredChannels { @Mock private Channel channel; @@ -181,31 +237,133 @@ class VorgangManagerServerResolverTest { void initTest() { doReturn(VorgangManagerListPropertiesTestFactory.CHANNEL_NAME).when(resolver).getChannelName(any()); doReturn(channel).when(resolver).createChannelByName(any()); + setProperties(VorgangManagerListPropertiesTestFactory.createForSingleRouting()); } @Test void shouldGetChannel() { - createStub(); + createStubByConfiguredChannels(); verify(resolver).createChannelByName(VorgangManagerListPropertiesTestFactory.CHANNEL_NAME); } @Test void shouldApplyTransformers() { - createStub(); + createStubByConfiguredChannels(); verify(resolver).applyStubTransformers(any(), any()); } @Test void shouldCreateStubByFactory() { - createStub(); + createStubByConfiguredChannels(); verify(stubFactory).createStub(eq(stubClass), any()); } - private AbstractStub<?> createStub() { - return resolver.createStub(Optional.of(VorgangManagerListPropertiesTestFactory.ORGANISATIONSEINHEIT_ID), stubFactory, stubClass); + private AbstractStub<?> createStubByConfiguredChannels() { + return resolver.createStubByConfiguredChannels(Optional.of(VorgangManagerListPropertiesTestFactory.ORGANISATIONSEINHEIT_ID), stubFactory, + stubClass); + } + } + + @DisplayName("Create stub using ZuFi") + @Nested + class TestCreateStubUsingZuFi { + + @Test + void shouldCallOrganisationsEinhetiService() { + + } + + @Test + void shouldCreateStubByGivenUrl() { + + } + + @Test + void shouldReturnCreatedStub() { + + } + } + + @Nested + class TestGetChannelName { + + @Test + void shouldUseSingleName() { + setProperties(VorgangManagerListPropertiesTestFactory.createForSingleRouting()); + + var name = resolver.getChannelName(Optional.empty()); + + assertThat(name).contains(VorgangManagerListPropertiesTestFactory.CHANNEL_NAME); + } + + @Test + void shouldUseNameFromMap() { + setProperties(VorgangManagerListPropertiesTestFactory.createForMultiRouting()); + + var name = resolver.getChannelName(Optional.of(VorgangManagerListPropertiesTestFactory.ORGANISATIONSEINHEIT_ID)); + + assertThat(name).contains(VorgangManagerListPropertiesTestFactory.CHANNEL_NAME); + } + + @Test + void shouldGetFundstellenName() { + setProperties(VorgangManagerListPropertiesTestFactory.createWithFundstelle()); + + var name = resolver.getChannelName(Optional.of("4711")); + + verify(resolver).getFundstelleChannelName(); + assertThat(name).isEqualTo(VorgangManagerListPropertiesTestFactory.FUNDSTELLE_CHANNEL_NAME); + } + } + + @Nested + class TestGetFundstellenChannelName { + + @Nested + class TestStrategyFundstelle { + + @Test + void shouldCreateChannel() { + setProperties(VorgangManagerListPropertiesTestFactory.createWithFundstelle()); + + var channel = resolver.getFundstelleChannelName(); + + assertThat(channel).isNotNull().isEqualTo(VorgangManagerListPropertiesTestFactory.FUNDSTELLE_CHANNEL_NAME); + } + + @Test + void shouldThrowExceptionIfFundstelleIsMissing() { + var props = VorgangManagerListPropertiesTestFactory.createWithFundstelle(); + props.setFundstelleVorgangManagerName(Optional.empty()); + setProperties(props); + + assertThrows(AdapterConfigurationException.class, () -> resolver.getFundstelleChannelName()); + } + } + + @Nested + class TestStrategyDeny { + + @Test + void shouldThrowException() { + setProperties(VorgangManagerListPropertiesTestFactory.createForMultiRouting()); + + assertThrows(UnknownOrganisationseinheitException.class, () -> resolver.getFundstelleChannelName()); + } + } + } + + @Nested + class TestCreateChannelByName { + + @Test + void shouldCallChannelFactory() { + resolver.createChannelByName(VorgangManagerListPropertiesTestFactory.VORGANG_MANAGER_NAME); + + verify(channelFactory).createChannel(VorgangManagerListPropertiesTestFactory.VORGANG_MANAGER_NAME); } } @@ -230,17 +388,13 @@ class VorgangManagerServerResolverTest { } } - private void setProperties(VorgangManagerListProperties properties) { - ReflectionTestUtils.setField(resolver, "properties", properties); - } - @Nested class TestZufiBasedRouting { private Class<? extends AbstractStub<?>> stubClass = VorgangServiceBlockingStub.class; @Nested - class createStub { + class CreateStub { private VorgangManagerListProperties properties = VorgangManagerListPropertiesTestFactory.createForZufiRouting(); @@ -258,6 +412,9 @@ class VorgangManagerServerResolverTest { verify(resolver).createStubUsingZufi(oeId, stubFactory, stubClass); } } + } + private void setProperties(VorgangManagerListProperties properties) { + ReflectionTestUtils.setField(resolver, "properties", properties); } -} +} \ No newline at end of file