Skip to content

Commit

Permalink
Add resetAll method to WireMockGrpcService
Browse files Browse the repository at this point in the history
  • Loading branch information
edeandrea committed Dec 19, 2023
1 parent a832565 commit d86f58c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/main/java/org/wiremock/grpc/dsl/WireMockGrpcService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/** Resets all active mocks for the current gRPC service */
public void resetAllForService() {
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);
}
}
30 changes: 28 additions & 2 deletions src/test/java/org/wiremock/grpc/GrpcAcceptanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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 @@ -46,6 +47,7 @@ public class GrpcAcceptanceTest {
WireMockGrpcService mockGreetingService;
ManagedChannel channel;
GreetingsClient greetingsClient;
WireMock wireMock;

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

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

channel = ManagedChannelBuilder.forAddress("localhost", wm.getPort()).usePlaintext().build();
greetingsClient = new GreetingsClient(channel);
Expand Down Expand Up @@ -286,4 +288,28 @@ void randomDelay() {
assertThat(greeting, is("Delayed hello"));
assertThat(stopwatch.elapsed(MILLISECONDS), greaterThanOrEqualTo(500L));
}

@Test
void resetAllForService() {
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())));

// Starting point assertion
assertThat(wireMock.allStubMappings().getMappings(), iterableWithSize(3));

mockGreetingService.resetAllForService();
assertThat(wireMock.allStubMappings().getMappings(), iterableWithSize(1));

mockAnotherGreetingService.resetAllForService();
assertThat(wireMock.allStubMappings().getMappings(), iterableWithSize(0));
}
}
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.

0 comments on commit d86f58c

Please sign in to comment.