Skip to content
Snippets Groups Projects
Commit a4fc76aa authored by Jan Zickermann's avatar Jan Zickermann
Browse files

OZG-4097 itcase: Try out grpc mocking with wiremock

parent a750deaa
No related branches found
No related tags found
Loading
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
<openapi-generator.version>7.11.0</openapi-generator.version> <openapi-generator.version>7.11.0</openapi-generator.version>
<swagger-parser.version>2.1.23</swagger-parser.version> <swagger-parser.version>2.1.23</swagger-parser.version>
<wiremock-spring-boot.version>3.6.0</wiremock-spring-boot.version> <wiremock-spring-boot.version>3.6.0</wiremock-spring-boot.version>
<wiremock-grpc-extension.version>0.9.0</wiremock-grpc-extension.version>
</properties> </properties>
<dependencies> <dependencies>
<!-- OZG-Cloud --> <!-- OZG-Cloud -->
...@@ -85,6 +86,13 @@ ...@@ -85,6 +86,13 @@
<version>${wiremock-spring-boot.version}</version> <version>${wiremock-spring-boot.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-grpc-extension-jetty12</artifactId>
<version>${wiremock-grpc-extension.version}</version>
<scope>test</scope>
</dependency>
<!-- commons --> <!-- commons -->
<dependency> <dependency>
......
package de.ozgcloud.nachrichten.postfach.osiv2; package de.ozgcloud.nachrichten.postfach.osiv2;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import de.ozgcloud.nachrichten.postfach.osiv2.config.Osi2PostfachProperties; import de.ozgcloud.nachrichten.postfach.osiv2.config.Osi2PostfachProperties;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Service @Service
@ConditionalOnProperty(prefix = Osi2PostfachProperties.PREFIX, name = "enabled", havingValue = "true") @ConditionalOnProperty(prefix = Osi2PostfachProperties.PREFIX, name = "enabled", havingValue = "true")
public @interface ServiceIfOsi2Enabled { public @interface ServiceIfOsi2Enabled {
......
...@@ -11,7 +11,6 @@ import org.springframework.context.annotation.Configuration; ...@@ -11,7 +11,6 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.FormHttpMessageConverter; import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.oauth2.client.AuthorizedClientServiceOAuth2AuthorizedClientManager; import org.springframework.security.oauth2.client.AuthorizedClientServiceOAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService; import org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider; import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider;
...@@ -31,6 +30,7 @@ import org.springframework.web.client.RestClient; ...@@ -31,6 +30,7 @@ import org.springframework.web.client.RestClient;
import de.ozgcloud.nachrichten.postfach.osiv2.gen.ApiClient; import de.ozgcloud.nachrichten.postfach.osiv2.gen.ApiClient;
import de.ozgcloud.nachrichten.postfach.osiv2.gen.api.MessageExchangeApi; import de.ozgcloud.nachrichten.postfach.osiv2.gen.api.MessageExchangeApi;
import de.ozgcloud.nachrichten.postfach.osiv2.gen.api.QuarantineApi;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@Configuration @Configuration
...@@ -49,6 +49,11 @@ public class ApiClientConfiguration { ...@@ -49,6 +49,11 @@ public class ApiClientConfiguration {
return new MessageExchangeApi(apiClient); return new MessageExchangeApi(apiClient);
} }
@Bean
QuarantineApi quarantineApi(ApiClient apiClient) {
return new QuarantineApi(apiClient);
}
@Bean @Bean
ApiClient apiClient() { ApiClient apiClient() {
var apiClient = new ApiClient(restClient()); var apiClient = new ApiClient(restClient());
......
...@@ -11,11 +11,13 @@ import de.ozgcloud.vorgang.grpc.binaryFile.GrpcBinaryFilesRequest; ...@@ -11,11 +11,13 @@ import de.ozgcloud.vorgang.grpc.binaryFile.GrpcBinaryFilesRequest;
import de.ozgcloud.vorgang.grpc.binaryFile.GrpcGetBinaryFileDataRequest; import de.ozgcloud.vorgang.grpc.binaryFile.GrpcGetBinaryFileDataRequest;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import net.devh.boot.grpc.client.inject.GrpcClient;
@Log4j2 @Log4j2
@ServiceIfOsi2Enabled @ServiceIfOsi2Enabled
@RequiredArgsConstructor @RequiredArgsConstructor
public class Osi2BinaryFileRemoteService { public class Osi2BinaryFileRemoteService {
@GrpcClient("vorgang-manager")
private final BinaryFileServiceGrpc.BinaryFileServiceBlockingStub binaryFileService; private final BinaryFileServiceGrpc.BinaryFileServiceBlockingStub binaryFileService;
private final Osi2BinaryFileMapper binaryFileMapper; private final Osi2BinaryFileMapper binaryFileMapper;
......
...@@ -2,4 +2,10 @@ logging: ...@@ -2,4 +2,10 @@ logging:
level: level:
de.ozgcloud.nachrichten.postfach.osiv2: DEBUG de.ozgcloud.nachrichten.postfach.osiv2: DEBUG
org.springframework.http: DEBUG org.springframework.http: DEBUG
org.springframework.web.client: DEBUG org.springframework.web.client: DEBUG
\ No newline at end of file
grpc:
client:
vorgang-manager:
address: static://127.0.0.1:9090
negotiationType: PLAINTEXT
\ No newline at end of file
spring: spring:
jackson: jackson:
default-property-inclusion: NON_NULL default-property-inclusion: NON_NULL
\ No newline at end of file
...@@ -17,13 +17,18 @@ import org.springframework.test.context.ActiveProfiles; ...@@ -17,13 +17,18 @@ import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource; import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.TestPropertySource;
import org.wiremock.grpc.dsl.WireMockGrpcService;
import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
import de.ozgcloud.nachrichten.postfach.PostfachException;
import de.ozgcloud.nachrichten.postfach.PostfachMessageCode;
import de.ozgcloud.nachrichten.postfach.PostfachNachricht; import de.ozgcloud.nachrichten.postfach.PostfachNachricht;
import de.ozgcloud.nachrichten.postfach.osiv2.exception.Osi2PostfachException;
import de.ozgcloud.nachrichten.postfach.osiv2.extension.Jwt; import de.ozgcloud.nachrichten.postfach.osiv2.extension.Jwt;
import de.ozgcloud.nachrichten.postfach.osiv2.extension.OsiMockServerExtension; import de.ozgcloud.nachrichten.postfach.osiv2.extension.OsiMockServerExtension;
import de.ozgcloud.nachrichten.postfach.osiv2.extension.VorgangManagerServerExtension;
import de.ozgcloud.nachrichten.postfach.osiv2.factory.JsonUtil; import de.ozgcloud.nachrichten.postfach.osiv2.factory.JsonUtil;
import de.ozgcloud.nachrichten.postfach.osiv2.factory.MessageExchangeReceiveMessagesResponseTestFactory; import de.ozgcloud.nachrichten.postfach.osiv2.factory.MessageExchangeReceiveMessagesResponseTestFactory;
import de.ozgcloud.nachrichten.postfach.osiv2.factory.MessageExchangeSendMessageResponseTestFactory; import de.ozgcloud.nachrichten.postfach.osiv2.factory.MessageExchangeSendMessageResponseTestFactory;
...@@ -41,6 +46,9 @@ class OsiPostfachRemoteServiceITCase { ...@@ -41,6 +46,9 @@ class OsiPostfachRemoteServiceITCase {
@RegisterExtension @RegisterExtension
static final OsiMockServerExtension OSI_MOCK_SERVER_EXTENSION = new OsiMockServerExtension(); static final OsiMockServerExtension OSI_MOCK_SERVER_EXTENSION = new OsiMockServerExtension();
@RegisterExtension
static final VorgangManagerServerExtension VORGANG_MANAGER_SERVER_EXTENSION = new VorgangManagerServerExtension();
private final PostfachNachricht postfachNachricht = PostfachNachrichtTestFactory.create(); private final PostfachNachricht postfachNachricht = PostfachNachrichtTestFactory.create();
@Autowired @Autowired
...@@ -53,14 +61,17 @@ class OsiPostfachRemoteServiceITCase { ...@@ -53,14 +61,17 @@ class OsiPostfachRemoteServiceITCase {
registry.add("ozgcloud.osiv2.auth.client-id", () -> CLIENT_ID); registry.add("ozgcloud.osiv2.auth.client-id", () -> CLIENT_ID);
registry.add("ozgcloud.osiv2.api.url", OSI_MOCK_SERVER_EXTENSION::getPostfachFacadeUrl); registry.add("ozgcloud.osiv2.api.url", OSI_MOCK_SERVER_EXTENSION::getPostfachFacadeUrl);
registry.add("ozgcloud.osiv2.auth.resource", () -> RESOURCE_URN); registry.add("ozgcloud.osiv2.auth.resource", () -> RESOURCE_URN);
registry.add("grpc.client.vorgang-manager.address", VORGANG_MANAGER_SERVER_EXTENSION::getVorgangManagerAddress);
} }
private WireMockServer postfachFacadeMockServer; private WireMockServer postfachFacadeMockServer;
private WireMockGrpcService binaryFileMockService;
@BeforeEach @BeforeEach
@SneakyThrows @SneakyThrows
public void setup() { public void setup() {
postfachFacadeMockServer = OSI_MOCK_SERVER_EXTENSION.getPostfachFacadeMockServer(); postfachFacadeMockServer = OSI_MOCK_SERVER_EXTENSION.getPostfachFacadeMockServer();
binaryFileMockService = VORGANG_MANAGER_SERVER_EXTENSION.getBinaryFileServiceGrpcService();
} }
@DisplayName("should send request with jwt") @DisplayName("should send request with jwt")
...@@ -87,6 +98,16 @@ class OsiPostfachRemoteServiceITCase { ...@@ -87,6 +98,16 @@ class OsiPostfachRemoteServiceITCase {
assertThat(jwt.body().read("$.aud", String.class)).isEqualTo(RESOURCE_URN); assertThat(jwt.body().read("$.aud", String.class)).isEqualTo(RESOURCE_URN);
} }
@DisplayName("should throw postfach exception with connection error code")
@Test
void shouldThrowPostfachExceptionWithConnectionErrorCode() {
postfachFacadeMockServer.stop();
assertThatThrownBy(() -> osiPostfachRemoteService.sendMessage(postfachNachricht))
.isInstanceOf(Osi2PostfachException.class)
.hasFieldOrPropertyWithValue("messageCode", PostfachMessageCode.SERVER_CONNECTION_FAILED_MESSAGE_CODE);
}
@DisplayName("should receive one messages") @DisplayName("should receive one messages")
@Test @Test
@SneakyThrows @SneakyThrows
......
package de.ozgcloud.nachrichten.postfach.osiv2.extension;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.*;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.wiremock.grpc.GrpcExtensionFactory;
import org.wiremock.grpc.Jetty12GrpcExtensionFactory;
import org.wiremock.grpc.dsl.WireMockGrpcService;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import de.ozgcloud.vorgang.grpc.binaryFile.BinaryFileServiceGrpc;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
@Log4j2
@Getter
@RequiredArgsConstructor
public class VorgangManagerServerExtension implements BeforeAllCallback, AfterAllCallback, AfterEachCallback {
private WireMockServer vorgangManagerMockServer;
private WireMockGrpcService binaryFileServiceGrpcService;
@Override
public void beforeAll(ExtensionContext context) {
setupVorgangManagerMock();
binaryFileServiceGrpcService = new WireMockGrpcService(
new WireMock(vorgangManagerMockServer),
BinaryFileServiceGrpc.SERVICE_NAME
);
}
@Override
public void afterEach(ExtensionContext context) {
binaryFileServiceGrpcService.resetAll();
vorgangManagerMockServer.resetAll();
}
@Override
public void afterAll(ExtensionContext context) {
vorgangManagerMockServer.shutdown();
}
private void setupVorgangManagerMock() {
vorgangManagerMockServer = new WireMockServer(
wireMockConfig()
.dynamicPort()
.extensions(new Jetty12GrpcExtensionFactory())
);
vorgangManagerMockServer.start();
}
public String getVorgangManagerAddress() {
return "static://127.0.0.1:%d".formatted(vorgangManagerMockServer.port());
}
}
...@@ -5,4 +5,10 @@ logging: ...@@ -5,4 +5,10 @@ logging:
level: level:
de.ozgcloud.nachrichten.postfach.osiv2: DEBUG de.ozgcloud.nachrichten.postfach.osiv2: DEBUG
org.springframework.http: DEBUG org.springframework.http: DEBUG
org.springframework.web.client: DEBUG org.springframework.web.client: DEBUG
\ No newline at end of file
grpc:
client:
vorgang-manager:
address: static://127.0.0.1:9090
negotiationType: PLAINTEXT
\ 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