Skip to content
Snippets Groups Projects
Commit 034c5041 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-6354 reorder methods; split/add tests

parent 5882012d
No related branches found
No related tags found
No related merge requests found
......@@ -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
......@@ -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());
}
@Nested
class TestGetFundstellenChannelName {
private void setStubFactories(StubFactory... factories) {
ReflectionTestUtils.setField(resolver, "stubFactories", Arrays.asList(factories));
}
}
@DisplayName("Resolve vorgangManager service by organisationsEinheitenId")
@Nested
class TestStrategyFundstelle {
class TestResolveVorgangManagerServiceStubByOrganisationsEinheitenId {
@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);
void shouldCallCreateStub() {
resolveStub();
assertThrows(AdapterConfigurationException.class, () -> resolver.getFundstelleChannelName());
}
verify(resolver).createStub(eq(organisationsEinheitenId), any(), eq(VorgangServiceBlockingStub.class));
}
@Nested
class TestStrategyDeny {
@Test
void shouldThrowException() {
setProperties(VorgangManagerListPropertiesTestFactory.createForMultiRouting());
void shouldReturnManageableStub() {
var createdStub = resolveStub();
assertThrows(UnknownOrganisationseinheitException.class, () -> resolver.getFundstelleChannelName());
assertThat(createdStub).isEqualTo(stub);
}
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;
private Class<? extends AbstractStub<?>> stubClass = VorgangServiceBlockingStub.class;
private Optional<String> organisationsEinheitenId = Optional.of(VorgangManagerListPropertiesTestFactory.ORGANISATIONSEINHEIT_ID);
@DisplayName("on zufi routing")
@Nested
class TestOnZufiRouting {
@BeforeEach
void mock() {
setProperties(VorgangManagerListPropertiesTestFactory.createForZufiRouting());
doReturn(createdStub).when(resolver).createStubUsingZufi(any(), any(), any());
}
@Test
void shouldSetApplicableFactory() {
when(stubFactory.isApplicable(any())).thenReturn(true);
setStubFactories(stubFactory, stubFactory);
void shouldCreateStubUsingZufi() {
createStub();
resolver.findApplicableStubFactories();
verify(resolver).createStubUsingZufi(organisationsEinheitenId, stubFactory, stubClass);
}
assertThat(ReflectionTestUtils.getField(resolver, "vorgangStubFactory")).isSameAs(stubFactory);
@Test
void shouldReturnCreatedStub() {
var stub = createStub();
assertThat(stub).isEqualTo(createdStub);
}
}
@DisplayName("On other routing")
@Nested
class TestOnOtherRouting {
@BeforeEach
void mock() {
setProperties(VorgangManagerListPropertiesTestFactory.createForSingleRouting());
doReturn(createdStub).when(resolver).createStubByConfiguredChannels(any(), any(), any());
}
@Test
void shouldThrowExceptionIfNotFound() {
setStubFactories(stubFactory);
void shouldCreateStubByConfiguredChannels() {
createStub();
assertThrows(AdapterConfigurationException.class, () -> resolver.findApplicableStubFactories());
verify(resolver).createStubByConfiguredChannels(organisationsEinheitenId, stubFactory, stubClass);
}
private void setStubFactories(StubFactory... factories) {
ReflectionTestUtils.setField(resolver, "stubFactories", Arrays.asList(factories));
@Test
void shouldReturnCreatedStub() {
var stub = createStub();
assertThat(stub).isEqualTo(createdStub);
}
}
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment