diff --git a/src/main/java/org/wiremock/grpc/dsl/WireMockGrpcService.java b/src/main/java/org/wiremock/grpc/dsl/WireMockGrpcService.java index 840ed38..c9dbb79 100644 --- a/src/main/java/org/wiremock/grpc/dsl/WireMockGrpcService.java +++ b/src/main/java/org/wiremock/grpc/dsl/WireMockGrpcService.java @@ -17,6 +17,8 @@ import static com.github.tomakehurst.wiremock.client.WireMock.exactly; import static com.github.tomakehurst.wiremock.client.WireMock.moreThanOrExactly; +import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching; import com.github.tomakehurst.wiremock.client.CountMatchingStrategy; import com.github.tomakehurst.wiremock.client.WireMock; @@ -72,4 +74,15 @@ public void removeAllStubs() { }) .forEach(wireMock::removeStubMapping); } + + /** Resets the request counters for the current gRPC service */ + public void resetRequests() { + wireMock.removeEvents(postRequestedFor(urlPathMatching("/" + serviceName + "/.+"))); + } + + /** Removes all stubs and resets all requests for the current gRPC service */ + public void resetAll() { + removeAllStubs(); + resetRequests(); + } } diff --git a/src/test/java/org/wiremock/grpc/GrpcAcceptanceTest.java b/src/test/java/org/wiremock/grpc/GrpcAcceptanceTest.java index bf0f24c..096d4e6 100644 --- a/src/test/java/org/wiremock/grpc/GrpcAcceptanceTest.java +++ b/src/test/java/org/wiremock/grpc/GrpcAcceptanceTest.java @@ -41,14 +41,18 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import org.wiremock.grpc.client.AnotherGreetingsClient; import org.wiremock.grpc.client.GreetingsClient; import org.wiremock.grpc.dsl.WireMockGrpcService; public class GrpcAcceptanceTest { WireMockGrpcService mockGreetingService; + WireMockGrpcService anotherMockGreetingService; ManagedChannel channel; + ManagedChannel anotherChannel; GreetingsClient greetingsClient; + AnotherGreetingsClient anotherGreetingsClient; WireMock wireMock; @RegisterExtension @@ -65,14 +69,21 @@ public class GrpcAcceptanceTest { void init() { wireMock = wm.getRuntimeInfo().getWireMock(); mockGreetingService = new WireMockGrpcService(wireMock, GreetingServiceGrpc.SERVICE_NAME); + anotherMockGreetingService = + new WireMockGrpcService(wireMock, AnotherGreetingServiceGrpc.SERVICE_NAME); channel = ManagedChannelBuilder.forAddress("localhost", wm.getPort()).usePlaintext().build(); greetingsClient = new GreetingsClient(channel); + + anotherChannel = + ManagedChannelBuilder.forAddress("localhost", wm.getPort()).usePlaintext().build(); + anotherGreetingsClient = new AnotherGreetingsClient(anotherChannel); } @AfterEach void tearDown() { channel.shutdown(); + anotherChannel.shutdown(); } @Test @@ -297,11 +308,9 @@ void resetStubs() { // 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")))); + anotherMockGreetingService.stubFor( + method("anotherGreeting") + .willReturn(message(HelloResponse.newBuilder().setGreeting("Hello")))); mockGreetingService.stubFor( method("greeting").willReturn(message(HelloResponse.newBuilder().setGreeting("Hi")))); @@ -314,7 +323,7 @@ void resetStubs() { mockGreetingService.removeAllStubs(); assertThat(wireMock.allStubMappings().getMappings(), iterableWithSize(2)); - mockAnotherGreetingService.removeAllStubs(); + anotherMockGreetingService.removeAllStubs(); verifyDefaultMappings(); } @@ -332,6 +341,58 @@ private void verifyDefaultMappings() { assertThat(request.getUrlPath(), Matchers.equalTo("/hello")); } + @Test + void resetAll() { + // Create a single stub for 2 different services + anotherMockGreetingService.stubFor( + method("anotherGreeting") + .willReturn(message(HelloResponse.newBuilder().setGreeting("Hello")))); + + mockGreetingService.stubFor( + method("greeting").willReturn(message(HelloResponse.newBuilder().setGreeting("Hi")))); + + // Perform some actions on each + assertThat(greetingsClient.greet("Tom"), is("Hi")); + assertThat(greetingsClient.greet("Tom"), is("Hi")); + assertThat(anotherGreetingsClient.greet("Tom"), is("Hello")); + assertThat(anotherGreetingsClient.greet("Tom"), is("Hello")); + + // Verify the interactions with each + mockGreetingService + .verify(2, "greeting") + .withRequestMessage(equalToMessage(HelloRequest.newBuilder().setName("Tom"))); + + anotherMockGreetingService + .verify(2, "anotherGreeting") + .withRequestMessage(equalToMessage(HelloRequest.newBuilder().setName("Tom"))); + + // Remove all from one of the services + mockGreetingService.resetAll(); + + // Create a new stub + mockGreetingService.stubFor( + method("greeting").willReturn(message(HelloResponse.newBuilder().setGreeting("Hello")))); + + // Perform some actions on each + assertThat(greetingsClient.greet("Tom"), is("Hello")); + assertThat(greetingsClient.greet("Tom"), is("Hello")); + assertThat(greetingsClient.greet("Tom"), is("Hello")); + assertThat(greetingsClient.greet("Tom"), is("Hello")); + assertThat(anotherGreetingsClient.greet("Tom"), is("Hello")); + assertThat(anotherGreetingsClient.greet("Tom"), is("Hello")); + assertThat(anotherGreetingsClient.greet("Tom"), is("Hello")); + assertThat(anotherGreetingsClient.greet("Tom"), is("Hello")); + + // Verify the interactions with each + mockGreetingService + .verify(4, "greeting") + .withRequestMessage(equalToMessage(HelloRequest.newBuilder().setName("Tom"))); + + anotherMockGreetingService + .verify(6, "anotherGreeting") + .withRequestMessage(equalToMessage(HelloRequest.newBuilder().setName("Tom"))); + } + @Test void unaryMethodWithAnyRequest() { mockGreetingService.stubFor( diff --git a/src/test/java/org/wiremock/grpc/client/AnotherGreetingsClient.java b/src/test/java/org/wiremock/grpc/client/AnotherGreetingsClient.java new file mode 100644 index 0000000..2b6c2aa --- /dev/null +++ b/src/test/java/org/wiremock/grpc/client/AnotherGreetingsClient.java @@ -0,0 +1,33 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wiremock.grpc.client; + +import com.example.grpc.AnotherGreetingServiceGrpc; +import com.example.grpc.request.HelloRequest; +import io.grpc.Channel; + +public class AnotherGreetingsClient { + + private final AnotherGreetingServiceGrpc.AnotherGreetingServiceBlockingStub stub; + + public AnotherGreetingsClient(Channel channel) { + stub = AnotherGreetingServiceGrpc.newBlockingStub(channel); + } + + public String greet(String name) { + return stub.anotherGreeting(HelloRequest.newBuilder().setName(name).build()).getGreeting(); + } +} diff --git a/src/test/proto/ExampleServices.proto b/src/test/proto/ExampleServices.proto index e3bb8f0..c8451cb 100644 --- a/src/test/proto/ExampleServices.proto +++ b/src/test/proto/ExampleServices.proto @@ -36,8 +36,5 @@ service GreetingService { } 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); + rpc anotherGreeting(com.example.grpc.request.HelloRequest) returns (com.example.grpc.response.HelloResponse); } \ No newline at end of file diff --git a/src/test/resources/wiremock/grpc/services.dsc b/src/test/resources/wiremock/grpc/services.dsc index 1d3ff59..adc96c5 100644 Binary files a/src/test/resources/wiremock/grpc/services.dsc and b/src/test/resources/wiremock/grpc/services.dsc differ