Skip to content
Snippets Groups Projects
Commit fcaefe14 authored by OZG-Cloud Team's avatar OZG-Cloud Team
Browse files

OZG-6354 handle zufi routing; adjust ITCase

parent 449b1bdc
Branches
Tags
No related merge requests found
...@@ -21,6 +21,7 @@ import org.springframework.test.context.ActiveProfiles; ...@@ -21,6 +21,7 @@ import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import de.ozgcloud.common.test.TestUtils; import de.ozgcloud.common.test.TestUtils;
import de.ozgcloud.eingang.router.ManagableStub;
import de.ozgcloud.eingang.router.VorgangManagerServerResolver; import de.ozgcloud.eingang.router.VorgangManagerServerResolver;
import de.ozgcloud.vorgang.grpc.binaryFile.BinaryFileServiceGrpc.BinaryFileServiceStub; import de.ozgcloud.vorgang.grpc.binaryFile.BinaryFileServiceGrpc.BinaryFileServiceStub;
import de.ozgcloud.vorgang.grpc.binaryFile.GrpcUploadBinaryFileRequest; import de.ozgcloud.vorgang.grpc.binaryFile.GrpcUploadBinaryFileRequest;
...@@ -49,14 +50,20 @@ public class FormsolutionsITCase { ...@@ -49,14 +50,20 @@ public class FormsolutionsITCase {
@Mock @Mock
private VorgangServiceBlockingStub blockingStub; private VorgangServiceBlockingStub blockingStub;
@Mock @Mock
private ManagableStub<VorgangServiceBlockingStub> managableVorgangServiceBlockingStub;
@Mock
private BinaryFileServiceStub fileStub; private BinaryFileServiceStub fileStub;
@Mock @Mock
private ManagableStub<BinaryFileServiceStub> managableBinaryFileStub;
@Mock
private CallStreamObserver<GrpcUploadBinaryFileRequest> fileStreamObserver; private CallStreamObserver<GrpcUploadBinaryFileRequest> fileStreamObserver;
@BeforeEach @BeforeEach
void initVorgangManagerResolver() { void initVorgangManagerResolver() {
when(resolver.resolveVorgangServiceBlockingStubByOrganisationseinheitenId(any())).thenReturn(blockingStub); when(resolver.resolveVorgangServiceBlockingStubByOrganisationseinheitenId(any())).thenReturn(managableVorgangServiceBlockingStub);
when(resolver.resolveBinaryFileServiceStubByOrganisationsEinheitId(any())).thenReturn(fileStub); when(managableVorgangServiceBlockingStub.get()).thenReturn(blockingStub);
when(resolver.resolveBinaryFileServiceStubByOrganisationsEinheitId(any())).thenReturn(managableBinaryFileStub);
when(managableBinaryFileStub.get()).thenReturn(fileStub);
Channel mockChannel = mock(Channel.class); Channel mockChannel = mock(Channel.class);
when(blockingStub.getChannel()).thenReturn(mockChannel); when(blockingStub.getChannel()).thenReturn(mockChannel);
......
...@@ -5,24 +5,25 @@ import java.util.concurrent.TimeUnit; ...@@ -5,24 +5,25 @@ import java.util.concurrent.TimeUnit;
import java.util.function.Function; import java.util.function.Function;
import de.ozgcloud.eingang.common.errorhandling.TechnicalException; import de.ozgcloud.eingang.common.errorhandling.TechnicalException;
import io.grpc.Channel;
import io.grpc.ManagedChannel; import io.grpc.ManagedChannel;
import lombok.Builder; import lombok.Builder;
import lombok.Getter;
@Getter
@Builder @Builder
class ClosableStub<T> implements ManagableStub<T> { class ClosableStub<T> implements ManagableStub<T> {
private static final int SHUTDOWM_TIME = 10; private static final int SHUTDOWM_TIME = 10;
private ManagedChannel channel; private ManagedChannel channel;
private Function<Channel, T> creationFunction; private Function<ManagedChannel, T> createFunction;
private T stub; private T stub;
@Override @Override
public T get() { public T get() {
if (Objects.isNull(stub)) { if (Objects.isNull(stub)) {
stub = creationFunction.apply(channel); stub = createFunction.apply(channel);
} }
return stub; return stub;
} }
......
package de.ozgcloud.eingang.router; package de.ozgcloud.eingang.router;
interface ManagableStub<T> { public interface ManagableStub<T> {
T get(); T get();
......
...@@ -26,6 +26,7 @@ package de.ozgcloud.eingang.router; ...@@ -26,6 +26,7 @@ package de.ozgcloud.eingang.router;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Optional; import java.util.Optional;
import java.util.function.Function;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
import jakarta.validation.Valid; import jakarta.validation.Valid;
...@@ -33,13 +34,19 @@ import jakarta.validation.Valid; ...@@ -33,13 +34,19 @@ import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import de.ozgcloud.common.errorhandling.TechnicalException;
import de.ozgcloud.eingang.common.zufi.ZufiService;
import de.ozgcloud.eingang.router.VorgangManagerListProperties.FallbackStrategy; import de.ozgcloud.eingang.router.VorgangManagerListProperties.FallbackStrategy;
import de.ozgcloud.eingang.router.VorgangManagerListProperties.RoutingStrategy; import de.ozgcloud.eingang.router.VorgangManagerListProperties.RoutingStrategy;
import de.ozgcloud.eingang.router.errorhandling.AdapterConfigurationException; import de.ozgcloud.eingang.router.errorhandling.AdapterConfigurationException;
import de.ozgcloud.eingang.router.errorhandling.UnknownOrganisationseinheitException; import de.ozgcloud.eingang.router.errorhandling.UnknownOrganisationseinheitException;
import de.ozgcloud.vorgang.grpc.binaryFile.BinaryFileServiceGrpc;
import de.ozgcloud.vorgang.grpc.binaryFile.BinaryFileServiceGrpc.BinaryFileServiceStub; import de.ozgcloud.vorgang.grpc.binaryFile.BinaryFileServiceGrpc.BinaryFileServiceStub;
import de.ozgcloud.vorgang.vorgang.VorgangServiceGrpc;
import de.ozgcloud.vorgang.vorgang.VorgangServiceGrpc.VorgangServiceBlockingStub; import de.ozgcloud.vorgang.vorgang.VorgangServiceGrpc.VorgangServiceBlockingStub;
import io.grpc.Channel; import io.grpc.Channel;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.AbstractStub; import io.grpc.stub.AbstractStub;
import lombok.NonNull; import lombok.NonNull;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
...@@ -67,6 +74,9 @@ public class VorgangManagerServerResolver { ...@@ -67,6 +74,9 @@ public class VorgangManagerServerResolver {
private StubFactory vorgangStubFactory; private StubFactory vorgangStubFactory;
private StubFactory binaryFileAsynStubFactory; private StubFactory binaryFileAsynStubFactory;
@Autowired
private ZufiService zufiService;
@PostConstruct @PostConstruct
void findApplicableStubFactories() { void findApplicableStubFactories() {
vorgangStubFactory = findStubFactory(VorgangServiceBlockingStub.class); vorgangStubFactory = findStubFactory(VorgangServiceBlockingStub.class);
...@@ -81,24 +91,41 @@ public class VorgangManagerServerResolver { ...@@ -81,24 +91,41 @@ public class VorgangManagerServerResolver {
public ManagableStub<VorgangServiceBlockingStub> resolveVorgangServiceBlockingStubByOrganisationseinheitenId( public ManagableStub<VorgangServiceBlockingStub> resolveVorgangServiceBlockingStubByOrganisationseinheitenId(
Optional<String> organisationsEinheitId) { Optional<String> organisationsEinheitId) {
return (ManagableStub<VorgangServiceBlockingStub>) createStub(organisationsEinheitId, vorgangStubFactory, VorgangServiceBlockingStub.class); if (isZufiStrategy()) {
return createStub(organisationsEinheitId, VorgangServiceGrpc::newBlockingStub);
}
return (ManagableStub<VorgangServiceBlockingStub>) createStubByConfiguredChannels(organisationsEinheitId, vorgangStubFactory,
VorgangServiceBlockingStub.class);
} }
public ManagableStub<BinaryFileServiceStub> resolveBinaryFileServiceStubByOrganisationsEinheitId(Optional<String> organisationsEinheitId) { public ManagableStub<BinaryFileServiceStub> resolveBinaryFileServiceStubByOrganisationsEinheitId(Optional<String> organisationsEinheitId) {
return (ManagableStub<BinaryFileServiceStub>) createStub(organisationsEinheitId, binaryFileAsynStubFactory, BinaryFileServiceStub.class); if (isZufiStrategy()) {
return createStub(organisationsEinheitId, BinaryFileServiceGrpc::newStub);
}
return (ManagableStub<BinaryFileServiceStub>) createStubByConfiguredChannels(organisationsEinheitId, binaryFileAsynStubFactory,
BinaryFileServiceStub.class);
} }
ManagableStub<?> createStub(Optional<String> organisationsEinheitId, StubFactory stubFactory, Class<? extends AbstractStub<?>> stubClass) {// NOSONAR private boolean isZufiStrategy() {
if (properties.getRoutingStrategy() == RoutingStrategy.ZUFI) { return properties.getRoutingStrategy() == RoutingStrategy.ZUFI;
return createStubUsingZufi(organisationsEinheitId, stubFactory, stubClass);
} else {
return createStubByConfiguredChannels(organisationsEinheitId, stubFactory, stubClass);
} }
<T> ManagableStub<T> createStub(Optional<String> organisationsEinheitId, Function<ManagedChannel, T> createFunction) {
return ClosableStub.<T>builder()
.channel(createChannel(organisationsEinheitId))
.createFunction(createFunction)
.build();
} }
ManagableStub<?> createStubUsingZufi(Optional<String> organisationsEinheitId, StubFactory stubFactory, ManagedChannel createChannel(Optional<String> organisationsEinheitId) {
Class<? extends AbstractStub<?>> stubClass) { return ManagedChannelBuilder.forTarget(getVorgangManagerAddress(organisationsEinheitId)).usePlaintext().build();
return null; }
private String getVorgangManagerAddress(Optional<String> organisationsEinheitId) {
if (organisationsEinheitId.isEmpty()) {
throw new TechnicalException("No organisationsEinheitId exists, can not build connection to vorgang-manager.");
}
return zufiService.getVorgangManagerUrl(organisationsEinheitId.get());
} }
ManagableStub<?> createStubByConfiguredChannels(Optional<String> organisationsEinheitId, StubFactory stubFactory, ManagableStub<?> createStubByConfiguredChannels(Optional<String> organisationsEinheitId, StubFactory stubFactory,
......
...@@ -31,22 +31,27 @@ import static org.mockito.Mockito.*; ...@@ -31,22 +31,27 @@ import static org.mockito.Mockito.*;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Optional; import java.util.Optional;
import java.util.function.Function;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Spy; import org.mockito.Spy;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import de.ozgcloud.common.errorhandling.TechnicalException;
import de.ozgcloud.eingang.common.formdata.ZustaendigeStelleTestFactory; import de.ozgcloud.eingang.common.formdata.ZustaendigeStelleTestFactory;
import de.ozgcloud.eingang.common.zufi.ZufiService;
import de.ozgcloud.eingang.router.errorhandling.AdapterConfigurationException; import de.ozgcloud.eingang.router.errorhandling.AdapterConfigurationException;
import de.ozgcloud.eingang.router.errorhandling.UnknownOrganisationseinheitException; import de.ozgcloud.eingang.router.errorhandling.UnknownOrganisationseinheitException;
import de.ozgcloud.vorgang.grpc.binaryFile.BinaryFileServiceGrpc.BinaryFileServiceStub; import de.ozgcloud.vorgang.grpc.binaryFile.BinaryFileServiceGrpc.BinaryFileServiceStub;
import de.ozgcloud.vorgang.vorgang.VorgangServiceGrpc.VorgangServiceBlockingStub; import de.ozgcloud.vorgang.vorgang.VorgangServiceGrpc.VorgangServiceBlockingStub;
import io.grpc.Channel; import io.grpc.Channel;
import io.grpc.ManagedChannel;
import io.grpc.stub.AbstractStub; import io.grpc.stub.AbstractStub;
import net.devh.boot.grpc.client.channelfactory.GrpcChannelFactory; import net.devh.boot.grpc.client.channelfactory.GrpcChannelFactory;
import net.devh.boot.grpc.client.inject.StubTransformer; import net.devh.boot.grpc.client.inject.StubTransformer;
...@@ -63,6 +68,9 @@ class VorgangManagerServerResolverTest { ...@@ -63,6 +68,9 @@ class VorgangManagerServerResolverTest {
@Mock @Mock
private StubFactory stubFactory; private StubFactory stubFactory;
@Mock
private ZufiService zufiService;
@Nested @Nested
class TestFindStubFactory { class TestFindStubFactory {
...@@ -100,16 +108,22 @@ class VorgangManagerServerResolverTest { ...@@ -100,16 +108,22 @@ class VorgangManagerServerResolverTest {
private final Optional<String> organisationsEinheitenId = Optional.of(ZustaendigeStelleTestFactory.ORGANISATIONSEINHEIT_ID); private final Optional<String> organisationsEinheitenId = Optional.of(ZustaendigeStelleTestFactory.ORGANISATIONSEINHEIT_ID);
@DisplayName("On zufi strategy")
@Nested
class TestOnZufiRoutingStrategy {
@BeforeEach @BeforeEach
void mock() { void mock() {
doReturn(stub).when(resolver).createStub(any(), any(), any()); doReturn(stub).when(resolver).createStub(any(), any());
setProperties(VorgangManagerListPropertiesTestFactory.createForZufiRouting());
} }
@Test @Test
void shouldCallCreateStub() { void shouldCallCreateStub() {
resolveStub(); resolveStub();
verify(resolver).createStub(eq(organisationsEinheitenId), any(), eq(VorgangServiceBlockingStub.class)); verify(resolver).createStub(eq(organisationsEinheitenId),
ArgumentMatchers.<Function<ManagedChannel, VorgangServiceBlockingStub>>any());
} }
@Test @Test
...@@ -118,6 +132,32 @@ class VorgangManagerServerResolverTest { ...@@ -118,6 +132,32 @@ class VorgangManagerServerResolverTest {
assertThat(createdStub).isEqualTo(stub); assertThat(createdStub).isEqualTo(stub);
} }
}
@DisplayName("On other routing strategy")
@Nested
class TestOnOtherRoutingStrategy {
@BeforeEach
void mock() {
doReturn(stub).when(resolver).createStubByConfiguredChannels(any(), any(), any());
setProperties(VorgangManagerListPropertiesTestFactory.createForSingleRouting());
}
@Test
void shouldCallCreateStub() {
resolveStub();
verify(resolver).createStubByConfiguredChannels(eq(organisationsEinheitenId), any(), eq(VorgangServiceBlockingStub.class));
}
@Test
void shouldReturnStub() {
var createdStub = resolveStub();
assertThat(createdStub).isEqualTo(stub);
}
}
private ManagableStub<VorgangServiceBlockingStub> resolveStub() { private ManagableStub<VorgangServiceBlockingStub> resolveStub() {
return resolver.resolveVorgangServiceBlockingStubByOrganisationseinheitenId(organisationsEinheitenId); return resolver.resolveVorgangServiceBlockingStubByOrganisationseinheitenId(organisationsEinheitenId);
...@@ -133,24 +173,56 @@ class VorgangManagerServerResolverTest { ...@@ -133,24 +173,56 @@ class VorgangManagerServerResolverTest {
private final Optional<String> organisationsEinheitenId = Optional.of(ZustaendigeStelleTestFactory.ORGANISATIONSEINHEIT_ID); private final Optional<String> organisationsEinheitenId = Optional.of(ZustaendigeStelleTestFactory.ORGANISATIONSEINHEIT_ID);
@DisplayName("On zufi strategy")
@Nested
class TestOnZufiRoutingStrategy {
@BeforeEach @BeforeEach
void mock() { void mock() {
doReturn(stub).when(resolver).createStub(any(), any(), any()); doReturn(stub).when(resolver).createStub(any(), any());
setProperties(VorgangManagerListPropertiesTestFactory.createForZufiRouting());
} }
@Test @Test
void shouldCallCreateStub() { void shouldCallCreateStub() {
resolveStub(); resolveStub();
verify(resolver).createStub(eq(organisationsEinheitenId), any(), eq(BinaryFileServiceStub.class)); verify(resolver).createStub(eq(organisationsEinheitenId),
ArgumentMatchers.<Function<ManagedChannel, BinaryFileServiceStub>>any());
} }
@Test @Test
void shouldStub() { void shouldReturnStub() {
var createdStub = resolveStub(); var createdStub = resolveStub();
assertThat(createdStub).isEqualTo(stub); assertThat(createdStub).isEqualTo(stub);
} }
}
@DisplayName("On other routing strategy")
@Nested
class TestOnOtherRoutingStrategy {
@BeforeEach
void mock() {
doReturn(stub).when(resolver).createStubByConfiguredChannels(any(), any(), any());
setProperties(VorgangManagerListPropertiesTestFactory.createForSingleRouting());
}
@Test
void shouldCallCreateStub() {
resolveStub();
verify(resolver).createStubByConfiguredChannels(eq(organisationsEinheitenId), any(), eq(BinaryFileServiceStub.class));
}
@Test
void shouldReturnStub() {
var createdStub = resolveStub();
assertThat(createdStub).isEqualTo(stub);
}
}
private ManagableStub<BinaryFileServiceStub> resolveStub() { private ManagableStub<BinaryFileServiceStub> resolveStub() {
return resolver.resolveBinaryFileServiceStubByOrganisationsEinheitId(organisationsEinheitenId); return resolver.resolveBinaryFileServiceStubByOrganisationsEinheitId(organisationsEinheitenId);
...@@ -163,62 +235,73 @@ class VorgangManagerServerResolverTest { ...@@ -163,62 +235,73 @@ class VorgangManagerServerResolverTest {
@Mock @Mock
private ManagableStub<?> createdStub; private ManagableStub<?> createdStub;
@Mock
private Function<ManagedChannel, AbstractStub<?>> createFunction;
@Mock
private ManagedChannel managedChannel;
private Class<? extends AbstractStub<?>> stubClass = VorgangServiceBlockingStub.class;
private Optional<String> organisationsEinheitenId = Optional.of(VorgangManagerListPropertiesTestFactory.ORGANISATIONSEINHEIT_ID); private Optional<String> organisationsEinheitenId = Optional.of(VorgangManagerListPropertiesTestFactory.ORGANISATIONSEINHEIT_ID);
@DisplayName("on zufi routing")
@Nested
class TestOnZufiRouting {
@BeforeEach @BeforeEach
void mock() { void mock() {
setProperties(VorgangManagerListPropertiesTestFactory.createForZufiRouting()); doReturn(managedChannel).when(resolver).createChannel(any());
doReturn(createdStub).when(resolver).createStubUsingZufi(any(), any(), any());
} }
@Test @Test
void shouldCreateStubUsingZufi() { void shouldCreateChannel() {
createStub(); createStub();
verify(resolver).createStubUsingZufi(organisationsEinheitenId, stubFactory, stubClass); verify(resolver).createChannel(organisationsEinheitenId);
} }
@Test @Test
void shouldReturnCreatedStub() { void shouldReturnStub() {
var stub = createStub(); var createdStub = (ClosableStub) createStub();
assertThat(stub).isEqualTo(createdStub); assertThat(createdStub).isNotNull();
assertThat(createdStub.getChannel()).isEqualTo(managedChannel);
assertThat(createdStub.getCreateFunction()).isEqualTo(createFunction);
}
private ManagableStub<?> createStub() {
return resolver.createStub(organisationsEinheitenId, createFunction);
} }
} }
@DisplayName("On other routing") @DisplayName("Create Channel")
@Nested @Nested
class TestOnOtherRouting { class TestCreateChannel {
@BeforeEach private final Optional<String> organisationsEinheitenId = Optional.of(VorgangManagerListPropertiesTestFactory.ORGANISATIONSEINHEIT_ID);
void mock() { private final String vorgangManagerAddress = "DummyVorgangManagerAddress";
setProperties(VorgangManagerListPropertiesTestFactory.createForSingleRouting());
doReturn(createdStub).when(resolver).createStubByConfiguredChannels(any(), any(), any());
}
@Test @Test
void shouldCreateStubByConfiguredChannels() { void shouldCallZufiService() {
createStub(); when(zufiService.getVorgangManagerUrl(any())).thenReturn(vorgangManagerAddress);
createChannel();
verify(resolver).createStubByConfiguredChannels(organisationsEinheitenId, stubFactory, stubClass); verify(zufiService).getVorgangManagerUrl(VorgangManagerListPropertiesTestFactory.ORGANISATIONSEINHEIT_ID);
} }
@Test @Test
void shouldReturnCreatedStub() { void shouldThrowExceptionIfOrganisationsEinheitIsNotPresent() {
var stub = createStub(); var emptyOrganisationsEinheitId = Optional.<String>empty();
assertThat(stub).isEqualTo(createdStub); assertThatThrownBy(() -> resolver.createChannel(emptyOrganisationsEinheitId)).isInstanceOf(TechnicalException.class);
} }
@Test
void shouldReturnChannel() {
when(zufiService.getVorgangManagerUrl(any())).thenReturn(vorgangManagerAddress);
var createdChannel = createChannel();
assertThat(createdChannel).isNotNull();
} }
private ManagableStub<?> createStub() { private ManagedChannel createChannel() {
return resolver.createStub(organisationsEinheitenId, stubFactory, stubClass); return resolver.createChannel(organisationsEinheitenId);
} }
} }
...@@ -413,14 +496,14 @@ class VorgangManagerServerResolverTest { ...@@ -413,14 +496,14 @@ class VorgangManagerServerResolverTest {
ReflectionTestUtils.setField(resolver, "properties", properties); ReflectionTestUtils.setField(resolver, "properties", properties);
} }
@Test // @Test
void shouldUseZufiToCreateChannel() { // void shouldUseZufiToCreateChannel() {
Optional<String> oeId = Optional.of(VorgangManagerListPropertiesTestFactory.ORGANISATIONSEINHEIT_ID); // Optional<String> oeId = Optional.of(VorgangManagerListPropertiesTestFactory.ORGANISATIONSEINHEIT_ID);
//
resolver.createStub(oeId, stubFactory, stubClass); // resolver.createStub(oeId, stubFactory, stubClass);
//
verify(resolver).createStubUsingZufi(oeId, stubFactory, stubClass); // verify(resolver).createStubUsingZufi(oeId, stubFactory, stubClass);
} // }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment