Skip to content

Commit

Permalink
Add resetAll method to WireMockGrpcService (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
edeandrea authored Jan 18, 2024
1 parent 98f27c9 commit 0ee8cd7
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 5 deletions.
23 changes: 22 additions & 1 deletion src/main/java/org/wiremock/grpc/dsl/WireMockGrpcService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023 Thomas Akehurst
* Copyright (C) 2023-2024 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,8 @@

import com.github.tomakehurst.wiremock.client.CountMatchingStrategy;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.http.RequestMethod;
import com.github.tomakehurst.wiremock.matching.RequestPattern;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import org.wiremock.annotations.Beta;

Expand Down Expand Up @@ -51,4 +53,23 @@ public GrpcVerification verify(int count, String method) {
public GrpcVerification verify(CountMatchingStrategy countMatch, String method) {
return new GrpcVerification(wireMock, countMatch, serviceName, method);
}

/** Removes all transient stubs for the current gRPC service */
public void removeAllStubs() {
final String servicePath = "/" + serviceName;

wireMock.allStubMappings().getMappings().stream()
.filter(
mapping -> {
final RequestPattern requestMatcher = mapping.getRequest();
final RequestMethod requestMethod = requestMatcher.getMethod();
final String requestPath = requestMatcher.getUrlPath();

return (requestMethod != null)
&& (requestPath != null)
&& requestMethod.match(RequestMethod.POST).isExactMatch()
&& requestPath.startsWith(servicePath);
})
.forEach(wireMock::removeStubMapping);
}
}
53 changes: 49 additions & 4 deletions src/test/java/org/wiremock/grpc/GrpcAcceptanceTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023 Thomas Akehurst
* Copyright (C) 2023-2024 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,9 +19,11 @@
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.wiremock.grpc.dsl.WireMockGrpc.*;

import com.example.grpc.AnotherGreetingServiceGrpc;
import com.example.grpc.GreetingServiceGrpc;
import com.example.grpc.request.HelloRequest;
import com.example.grpc.response.HelloResponse;
Expand All @@ -34,6 +36,7 @@
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import java.time.Duration;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -46,6 +49,7 @@ public class GrpcAcceptanceTest {
WireMockGrpcService mockGreetingService;
ManagedChannel channel;
GreetingsClient greetingsClient;
WireMock wireMock;

@RegisterExtension
public static WireMockExtension wm =
Expand All @@ -59,8 +63,8 @@ public class GrpcAcceptanceTest {

@BeforeEach
void init() {
mockGreetingService =
new WireMockGrpcService(new WireMock(wm.getPort()), GreetingServiceGrpc.SERVICE_NAME);
wireMock = wm.getRuntimeInfo().getWireMock();
mockGreetingService = new WireMockGrpcService(wireMock, GreetingServiceGrpc.SERVICE_NAME);

channel = ManagedChannelBuilder.forAddress("localhost", wm.getPort()).usePlaintext().build();
greetingsClient = new GreetingsClient(channel);
Expand Down Expand Up @@ -254,7 +258,7 @@ void networkFault() {

Exception exception =
assertThrows(StatusRuntimeException.class, () -> greetingsClient.greet("Alan"));
assertThat(exception.getMessage(), is("UNKNOWN"));
assertThat(exception.getMessage(), startsWith("UNKNOWN"));
}

@Test
Expand Down Expand Up @@ -286,4 +290,45 @@ void randomDelay() {
assertThat(greeting, is("Delayed hello"));
assertThat(stopwatch.elapsed(), greaterThanOrEqualTo(Duration.ofMillis(500L)));
}

@Test
void resetStubs() {
// Starting point assertion
// There should be a single mapping (the hello-world one)
verifyDefaultMappings();

WireMockGrpcService mockAnotherGreetingService =
new WireMockGrpcService(wireMock, AnotherGreetingServiceGrpc.SERVICE_NAME);

mockAnotherGreetingService.stubFor(
method("greeting").willReturn(message(HelloResponse.newBuilder().setGreeting("Hi"))));

mockGreetingService.stubFor(
method("greeting").willReturn(message(HelloResponse.newBuilder().setGreeting("Hi"))));

mockGreetingService.stubFor(
method("oneGreetingEmptyReply").willReturn(message(Empty.newBuilder())));

assertThat(wireMock.allStubMappings().getMappings(), iterableWithSize(4));

mockGreetingService.removeAllStubs();
assertThat(wireMock.allStubMappings().getMappings(), iterableWithSize(2));

mockAnotherGreetingService.removeAllStubs();

verifyDefaultMappings();
}

private void verifyDefaultMappings() {
var mappings = wireMock.allStubMappings().getMappings();
assertThat(mappings, iterableWithSize(1));

var mapping = mappings.get(0);
assertNotNull(mapping);
assertThat(mapping.getName(), Matchers.equalTo("Hello"));

var request = mapping.getRequest();
assertThat(request.getMethod().value(), Matchers.equalTo("GET"));
assertThat(request.getUrlPath(), Matchers.equalTo("/hello"));
}
}
7 changes: 7 additions & 0 deletions src/test/proto/ExampleServices.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ service GreetingService {
rpc oneGreetingManyReplies(com.example.grpc.request.HelloRequest) returns (stream com.example.grpc.response.HelloResponse);
rpc oneGreetingEmptyReply(com.example.grpc.request.HelloRequest) returns (google.protobuf.Empty);
}

service AnotherGreetingService {
rpc greeting(com.example.grpc.request.HelloRequest) returns (com.example.grpc.response.HelloResponse);
rpc manyGreetingsOneReply(stream com.example.grpc.request.HelloRequest) returns (com.example.grpc.response.HelloResponse);
rpc oneGreetingManyReplies(com.example.grpc.request.HelloRequest) returns (stream com.example.grpc.response.HelloResponse);
rpc oneGreetingEmptyReply(com.example.grpc.request.HelloRequest) returns (google.protobuf.Empty);
}
Binary file modified src/test/resources/wiremock/grpc/services.dsc
Binary file not shown.
37 changes: 37 additions & 0 deletions src/test/resources/wiremock/mappings/hello-world.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"mappings": [{
"request": {
"headers": {"Accept": {"or": [
{"equalTo": "*/*"},
{"equalTo": "text/plain"}
]}},
"method": "GET",
"urlPath": "/hello"
},
"metadata": {"mocklab": {
"response-example-attachment": "Hello World",
"created": {
"at": "2024-01-09T15:32:35.424189986Z",
"by": "vqe9q",
"via": "ADMIN_API"
},
"updated": {
"at": "2024-01-09T15:34:11.584464236Z",
"by": "vqe9q",
"via": "ADMIN_API"
}
}},
"response": {
"headers": {"Content-Type": "text/plain"},
"body": "Hello World!",
"status": 200
},
"name": "Hello",
"postServeActions": [],
"id": "d469c289-22fb-4186-90ab-deb6ffb6db5c",
"persistent": true,
"priority": 5,
"uuid": "d469c289-22fb-4186-90ab-deb6ffb6db5c"
}],
"meta": {"total": 1}
}

0 comments on commit 0ee8cd7

Please sign in to comment.