Skip to content

Commit

Permalink
Fixing reset to also clear counters (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
edeandrea authored Apr 8, 2024
1 parent 33a60e7 commit 6bdfeba
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 10 deletions.
13 changes: 13 additions & 0 deletions src/main/java/org/wiremock/grpc/dsl/WireMockGrpcService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
73 changes: 67 additions & 6 deletions src/test/java/org/wiremock/grpc/GrpcAcceptanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"))));
Expand All @@ -314,7 +323,7 @@ void resetStubs() {
mockGreetingService.removeAllStubs();
assertThat(wireMock.allStubMappings().getMappings(), iterableWithSize(2));

mockAnotherGreetingService.removeAllStubs();
anotherMockGreetingService.removeAllStubs();

verifyDefaultMappings();
}
Expand All @@ -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(
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/org/wiremock/grpc/client/AnotherGreetingsClient.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
5 changes: 1 addition & 4 deletions src/test/proto/ExampleServices.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Binary file modified src/test/resources/wiremock/grpc/services.dsc
Binary file not shown.

0 comments on commit 6bdfeba

Please sign in to comment.