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

OZG-2220 OZG-3412 Added OrganisationsEinheitServiceBlockingStub and

tests
parent 397d0bd8
No related branches found
No related tags found
No related merge requests found
Showing
with 265 additions and 22 deletions
...@@ -49,6 +49,7 @@ ...@@ -49,6 +49,7 @@
<kop.license.version>1.3.0</kop.license.version> <kop.license.version>1.3.0</kop.license.version>
<kop.zufi.version>0.1.0-SNAPSHOT</kop.zufi.version> <kop.zufi.version>0.1.0-SNAPSHOT</kop.zufi.version>
<user-manager-interface.version>1.4.0-SNAPSHOT</user-manager-interface.version>
<zip.version>2.11.1</zip.version> <zip.version>2.11.1</zip.version>
<jsoup.version>1.15.3</jsoup.version> <jsoup.version>1.15.3</jsoup.version>
...@@ -89,6 +90,22 @@ ...@@ -89,6 +90,22 @@
<version>${kop.zufi.version}</version> <version>${kop.zufi.version}</version>
</dependency> </dependency>
<dependency>
<groupId>de.itvsh.kop.user</groupId>
<artifactId>user-manager-interface</artifactId>
<version>${user-manager-interface.version}</version>
<exclusions>
<exclusion>
<groupId>io.grpc</groupId>
<artifactId>grpc-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.slf4j</groupId>
<artifactId>slf4j-jboss-logmanager</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Spring --> <!-- Spring -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
......
/*
* Copyright (C) 2022 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.itvsh.ozg.pluto.common.callcontext;
import java.util.UUID;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
import io.grpc.Metadata;
import io.grpc.Metadata.Key;
import io.grpc.MethodDescriptor;
public class RequestIdClientCallContextAttachingInterceptor implements ClientInterceptor {
static final String KEY_REQUEST_ID = "REQUEST_ID-bin";
// <A> = Request, <B> = Response
@Override
public <A, B> ClientCall<A, B> interceptCall(MethodDescriptor<A, B> method, CallOptions callOptions, Channel next) {
return new CallContextAttachingClientCall<>(next.newCall(method, callOptions));
}
final class CallContextAttachingClientCall<A, B> extends SimpleForwardingClientCall<A, B> {
protected CallContextAttachingClientCall(ClientCall<A, B> delegate) {
super(delegate);
}
@Override
public void start(Listener<B> responseListener, Metadata headers) {
headers.merge(buildCallContextMetadata());
super.start(responseListener, headers);
}
private Metadata buildCallContextMetadata() {
var metadata = new Metadata();
metadata.put(createKeyOf(KEY_REQUEST_ID), generateRequestId().getBytes());
return metadata;
}
// TODO OZG-1974 requestId zentraler erzeugen
private String generateRequestId() {
return UUID.randomUUID().toString();
}
}
// TODO move to common grpc utils
public static Key<byte[]> createKeyOf(String key) {
return Key.of(key, Metadata.BINARY_BYTE_MARSHALLER);
}
}
...@@ -11,7 +11,7 @@ class RegistryScheduler { ...@@ -11,7 +11,7 @@ class RegistryScheduler {
@Autowired @Autowired
private RegistryService registryService; private RegistryService registryService;
@Scheduled(fixedDelayString = "${kop.vorgangmanager.registry.intervall}", initialDelay = 5, timeUnit = TimeUnit.MINUTES) @Scheduled(fixedDelayString = "${kop.vorgangmanager.registry.intervall:360}", initialDelay = 1, timeUnit = TimeUnit.MINUTES)
void register() { void register() {
registryService.registerVorgangManager(); registryService.registerVorgangManager();
} }
......
...@@ -4,11 +4,27 @@ import java.util.List; ...@@ -4,11 +4,27 @@ import java.util.List;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.google.protobuf.Empty;
import de.itvsh.kop.user.grpc.organisationseinheit.OrganisationsEinheitServiceGrpc.OrganisationsEinheitServiceBlockingStub;
import de.itvsh.kop.user.organisationseinheit.GrpcOrganisationsEinheit;
import de.itvsh.ozg.pluto.common.callcontext.RequestIdClientCallContextAttachingInterceptor;
import net.devh.boot.grpc.client.inject.GrpcClient;
@Service @Service
class UserOrganisationsEinheitenRemoteService { class UserOrganisationsEinheitenRemoteService {
@GrpcClient("user-manager")
private OrganisationsEinheitServiceBlockingStub serviceStub;
List<String> getSupportedOrganisationsEinheiten() { List<String> getSupportedOrganisationsEinheiten() {
return null; return getGrpcOrganisationsEinheiten().stream().map(GrpcOrganisationsEinheit::getOrganisationsEinheitId)
.toList();
}
List<GrpcOrganisationsEinheit> getGrpcOrganisationsEinheiten() {
return serviceStub.withInterceptors(new RequestIdClientCallContextAttachingInterceptor())
.getSupportedOrganisationsEinheiten(Empty.getDefaultInstance()).getOrganisationseinheitenList();
} }
} }
...@@ -7,7 +7,7 @@ import org.springframework.beans.factory.annotation.Value; ...@@ -7,7 +7,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import de.itvsh.kop.zufi.grpc.registration.GrpcRegistrationRequest; import de.itvsh.kop.zufi.grpc.registration.GrpcRegistrationRequest;
import de.itvsh.kop.zufi.grpc.registration.RegistrationServiceGrpc; import de.itvsh.kop.zufi.grpc.registration.RegistrationServiceGrpc.RegistrationServiceBlockingStub;
import lombok.NonNull; import lombok.NonNull;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import net.devh.boot.grpc.client.inject.GrpcClient; import net.devh.boot.grpc.client.inject.GrpcClient;
...@@ -15,25 +15,29 @@ import net.devh.boot.grpc.client.inject.GrpcClient; ...@@ -15,25 +15,29 @@ import net.devh.boot.grpc.client.inject.GrpcClient;
@Log4j2 @Log4j2
@Service @Service
class ZufiRemoteService { class ZufiRemoteService {
@Value(value = "${kop.vorgangmanager.register.external.address:}") @Value(value = "${kop.vorgangmanager.address:}")
private String externalVorgangManagerAddress; private String externalVorgangManagerAddress;
@GrpcClient("zufi-manager") @GrpcClient("zufi-manager")
private RegistrationServiceGrpc.RegistrationServiceBlockingStub registryGrpcService; private RegistrationServiceBlockingStub serviceStub;
void registerVorgangManager(@NonNull List<String> organistationsEinheitenIds) { void registerVorgangManager(@NonNull List<String> organistationsEinheitenIds) {
var proceed = checkPreconditions(); var proceed = checkPreconditions();
if (proceed) { if (proceed) {
var request = GrpcRegistrationRequest.newBuilder().addAllOrganisationseinheitenId(organistationsEinheitenIds) var request = createRequest(organistationsEinheitenIds);
.setAddress(externalVorgangManagerAddress).build(); serviceStub.register(request);
registryGrpcService.register(request); }
} }
private GrpcRegistrationRequest createRequest(List<String> organistationsEinheitenIds) {
return GrpcRegistrationRequest.newBuilder().addAllOrganisationseinheitenId(organistationsEinheitenIds)
.setAddress(externalVorgangManagerAddress).build();
} }
boolean checkPreconditions() { boolean checkPreconditions() {
if (StringUtils.isEmpty(externalVorgangManagerAddress)) { if (StringUtils.isEmpty(externalVorgangManagerAddress)) {
LOG.warn("Property kop.vorgangmanager.register.external.address not set. Not registering this VorgangsManager"); LOG.warn("Property kop.vorgangmanager.address not set. Not registering this VorgangsManager");
return false; return false;
} }
......
...@@ -33,7 +33,6 @@ spring: ...@@ -33,7 +33,6 @@ spring:
username: elastic username: elastic
password: password password: password
pluto: pluto:
redirect: redirect:
mail-from: ea@ozg-sh.de mail-from: ea@ozg-sh.de
...@@ -55,6 +54,8 @@ kop: ...@@ -55,6 +54,8 @@ kop:
mail-from: ea@ozg-sh.de mail-from: ea@ozg-sh.de
usermanager: usermanager:
url: http://localhost:9092/migration/user url: http://localhost:9092/migration/user
vorgangmanager:
address: static://127.0.0.1:9090
aktenzeichen: de.itvsh.ozg.pluto.vorgang.AktenzeichenProviderEA aktenzeichen: de.itvsh.ozg.pluto.vorgang.AktenzeichenProviderEA
......
...@@ -31,7 +31,7 @@ grpc: ...@@ -31,7 +31,7 @@ grpc:
address: self:self address: self:self
negotiationType: PLAINTEXT negotiationType: PLAINTEXT
zufi-manager: zufi-manager:
address: static://127.0.0.1:9010 address: static://127.0.0.1:9190
negotiationType: PLAINTEXT negotiationType: PLAINTEXT
pluto: pluto:
......
package de.itvsh.ozg.pluto.registry;
import de.itvsh.kop.user.grpc.organisationseinheit.GrpcGetSupportedOrganisationsEinheitenResponse;
import de.itvsh.kop.user.organisationseinheit.GrpcOrganisationsEinheit;
public class GrpcGetSupportedOrganisationsEinheitenResponseTestFactory {
public static String ORGANISATIONS_EINHEIT_ID = "123456";
public static GrpcGetSupportedOrganisationsEinheitenResponse createResponse() {
return createResponseBuilder()
.addOrganisationseinheiten(GrpcOrganisationsEinheit.newBuilder().setOrganisationsEinheitId(ORGANISATIONS_EINHEIT_ID).build()).build();
}
public static GrpcGetSupportedOrganisationsEinheitenResponse.Builder createResponseBuilder() {
return GrpcGetSupportedOrganisationsEinheitenResponse.newBuilder();
}
}
package de.itvsh.ozg.pluto.registry;
import de.itvsh.kop.zufi.grpc.registration.GrpcRegistrationResponse;
class GrpcRegistrationResponseTestFactory {
static final String OK = "OK";
static GrpcRegistrationResponse create() {
return createBuilder().build();
}
static GrpcRegistrationResponse.Builder createBuilder() {
return GrpcRegistrationResponse.newBuilder().setStatus(OK);
}
}
...@@ -12,11 +12,16 @@ import org.mockito.InjectMocks; ...@@ -12,11 +12,16 @@ import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Spy; import org.mockito.Spy;
import io.grpc.StatusRuntimeException;
class RegistryServiceTest { class RegistryServiceTest {
@DisplayName("Test registry service") @DisplayName("Test registry service")
@Nested @Nested
class TestService { class TestService {
private static final String ORGANISATIONS_EINHEITEN_ID_1 = "123456";
private static final String ORGANISATIONS_EINHEITEN_ID_2 = "987654";
@Spy @Spy
@InjectMocks @InjectMocks
private RegistryService registryService; private RegistryService registryService;
...@@ -29,7 +34,8 @@ class RegistryServiceTest { ...@@ -29,7 +34,8 @@ class RegistryServiceTest {
@BeforeEach @BeforeEach
void init() { void init() {
when(userOrganisationsEinheitenRemoteService.getSupportedOrganisationsEinheiten()).thenReturn(List.of("123456", "987654")); when(userOrganisationsEinheitenRemoteService.getSupportedOrganisationsEinheiten())
.thenReturn(List.of(ORGANISATIONS_EINHEITEN_ID_1, ORGANISATIONS_EINHEITEN_ID_2));
} }
@Test @Test
...@@ -57,12 +63,11 @@ class RegistryServiceTest { ...@@ -57,12 +63,11 @@ class RegistryServiceTest {
@Test @Test
void shouldNotCallZuFiRemoteServiceOnException() { void shouldNotCallZuFiRemoteServiceOnException() {
when(userOrganisationsEinheitenRemoteService.getSupportedOrganisationsEinheiten()).thenThrow(Exception.class); when(userOrganisationsEinheitenRemoteService.getSupportedOrganisationsEinheiten()).thenThrow(StatusRuntimeException.class);
registryService.registerVorgangManager(); registryService.registerVorgangManager();
verify(zufiRemoteService, never()).registerVorgangManager(any()); verify(zufiRemoteService, never()).registerVorgangManager(any());
} }
} }
} }
package de.itvsh.ozg.pluto.registry;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
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;
import org.mockito.Mock;
import org.mockito.Spy;
import de.itvsh.kop.user.grpc.organisationseinheit.OrganisationsEinheitServiceGrpc.OrganisationsEinheitServiceBlockingStub;
import de.itvsh.ozg.pluto.common.callcontext.RequestIdClientCallContextAttachingInterceptor;
class UserOrganisationsEinheitenRemoteServiceTest {
@DisplayName("Test loading Organsiationseinheiten Ids of the users")
@Nested
class TestLoadingOrganisationsEinheitenIds {
@Spy
@InjectMocks
private UserOrganisationsEinheitenRemoteService remoteService;
@Mock
private OrganisationsEinheitServiceBlockingStub stub;
@BeforeEach
void init() {
when(stub.getSupportedOrganisationsEinheiten(any()))
.thenReturn(GrpcGetSupportedOrganisationsEinheitenResponseTestFactory.createResponse());
}
@Test
void shouldLoadOrganisationsEinheiten() {
var organisationsEinheiten = remoteService.getSupportedOrganisationsEinheiten();
assertThat(organisationsEinheiten).hasSize(1)
.contains(GrpcGetSupportedOrganisationsEinheitenResponseTestFactory.ORGANISATIONS_EINHEIT_ID);
}
@Test
void shouldUseInterceptor() {
remoteService.getSupportedOrganisationsEinheiten();
verify(stub).withInterceptors(any(RequestIdClientCallContextAttachingInterceptor.class));
}
}
}
package de.itvsh.ozg.pluto.registry; package de.itvsh.ozg.pluto.registry;
import static org.assertj.core.api.Assertions.*; import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.util.List;
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.InjectMocks; import org.mockito.InjectMocks;
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.itvsh.kop.zufi.grpc.registration.GrpcRegistrationRequest;
import de.itvsh.kop.zufi.grpc.registration.RegistrationServiceGrpc.RegistrationServiceBlockingStub;
class ZufiManagerRemoteServiceTest { class ZufiManagerRemoteServiceTest {
@DisplayName("Test ZuFiRemoteService") @DisplayName("Test ZuFiRemoteService")
@Nested @Nested
class TestZufiManagerRemoteService { class TestZufiManagerRemoteService {
private static final String ORGANISATIONS_EINHEITEN_ID = "123456";
private static final List<String> ORGANISATIONS_EINHEITEN_IDS = List.of(ORGANISATIONS_EINHEITEN_ID);
@Spy @Spy
@InjectMocks @InjectMocks
private ZufiRemoteService zufiRemoteService; private ZufiRemoteService zufiRemoteService;
@Mock
private RegistrationServiceBlockingStub serviceStub;
@Test @Test
void shouldProceed() { void shouldCheckPreconditions() {
zufiRemoteService.registerVorgangManager(ORGANISATIONS_EINHEITEN_IDS);
verify(zufiRemoteService).checkPreconditions();
}
@DisplayName("Test ZuFiRemoteService with VorgangManager configured")
@Nested
class TestWithAddress {
@BeforeEach
void init() {
ReflectionTestUtils.setField(zufiRemoteService, "externalVorgangManagerAddress", "value"); ReflectionTestUtils.setField(zufiRemoteService, "externalVorgangManagerAddress", "value");
}
@Test
void shouldProceed() {
var proceed = zufiRemoteService.checkPreconditions(); var proceed = zufiRemoteService.checkPreconditions();
assertThat(proceed).isTrue(); assertThat(proceed).isTrue();
} }
@Test
void shouldCallServiceStub() {
zufiRemoteService.registerVorgangManager(ORGANISATIONS_EINHEITEN_IDS);
verify(serviceStub).register(any(GrpcRegistrationRequest.class));
}
}
@DisplayName("Test ZuFiRemoteService without VorgangManager configured")
@Nested
class TestWithoutAddress {
@Test @Test
void shouldNotProceed() { void shouldNotProceed() {
var proceed = zufiRemoteService.checkPreconditions(); var proceed = zufiRemoteService.checkPreconditions();
...@@ -35,3 +73,4 @@ class ZufiManagerRemoteServiceTest { ...@@ -35,3 +73,4 @@ class ZufiManagerRemoteServiceTest {
} }
} }
} }
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment