Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the configured Notifier to log messages #77

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static org.wiremock.grpc.dsl.GrpcResponseDefinitionBuilder.GRPC_STATUS_NAME;
import static org.wiremock.grpc.dsl.GrpcResponseDefinitionBuilder.GRPC_STATUS_REASON;

import com.github.tomakehurst.wiremock.common.LocalNotifier;
import com.github.tomakehurst.wiremock.common.Notifier;
import com.github.tomakehurst.wiremock.http.HttpHeader;
import com.github.tomakehurst.wiremock.http.StubRequestHandler;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
Expand All @@ -32,12 +34,16 @@
public class ClientStreamingServerCallHandler extends BaseCallHandler
implements ServerCalls.ClientStreamingMethod<DynamicMessage, DynamicMessage> {

private final Notifier notifier;

public ClientStreamingServerCallHandler(
StubRequestHandler stubRequestHandler,
Descriptors.ServiceDescriptor serviceDescriptor,
Descriptors.MethodDescriptor methodDescriptor,
JsonMessageConverter jsonMessageConverter) {
JsonMessageConverter jsonMessageConverter,
Notifier notifier) {
super(stubRequestHandler, serviceDescriptor, methodDescriptor, jsonMessageConverter);
this.notifier = notifier;
}

@Override
Expand All @@ -64,6 +70,7 @@ public void onNext(DynamicMessage request) {
methodDescriptor.getName(),
jsonMessageConverter.toJson(request));

LocalNotifier.set(notifier);
stubRequestHandler.handle(
wireMockRequest,
(req, resp, attributes) -> {
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/wiremock/grpc/internal/GrpcFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static java.util.concurrent.TimeUnit.SECONDS;

import com.github.tomakehurst.wiremock.common.Exceptions;
import com.github.tomakehurst.wiremock.common.Notifier;
import com.github.tomakehurst.wiremock.http.StubRequestHandler;
import com.google.protobuf.Descriptors;
import com.google.protobuf.DynamicMessage;
Expand All @@ -42,13 +43,15 @@ public class GrpcFilter extends HttpFilter {
private final GrpcServlet grpcServlet;
private final StubRequestHandler stubRequestHandler;
private final List<Descriptors.FileDescriptor> fileDescriptors;
private final Notifier notifier;

private final JsonMessageConverter jsonMessageConverter;

public GrpcFilter(
StubRequestHandler stubRequestHandler, List<Descriptors.FileDescriptor> fileDescriptors) {
StubRequestHandler stubRequestHandler, List<Descriptors.FileDescriptor> fileDescriptors, Notifier notifier) {
this.stubRequestHandler = stubRequestHandler;
this.fileDescriptors = fileDescriptors;
this.notifier = notifier;

final TypeRegistry.Builder typeRegistryBuilder = TypeRegistry.newBuilder();
fileDescriptors.forEach(
Expand Down Expand Up @@ -90,10 +93,10 @@ private ServerCallHandler<DynamicMessage, DynamicMessage> buildHandler(
return methodDescriptor.isClientStreaming()
? ServerCalls.asyncClientStreamingCall(
new ClientStreamingServerCallHandler(
stubRequestHandler, serviceDescriptor, methodDescriptor, jsonMessageConverter))
stubRequestHandler, serviceDescriptor, methodDescriptor, jsonMessageConverter, notifier))
: ServerCalls.asyncUnaryCall(
new UnaryServerCallHandler(
stubRequestHandler, serviceDescriptor, methodDescriptor, jsonMessageConverter));
stubRequestHandler, serviceDescriptor, methodDescriptor, jsonMessageConverter, notifier));
}

private static MethodDescriptor<DynamicMessage, DynamicMessage> buildMessageDescriptorInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public HttpServer buildHttpServer(
protected void decorateMockServiceContextBeforeConfig(
ServletContextHandler mockServiceContext) {

final GrpcFilter grpcFilter = new GrpcFilter(stubRequestHandler, fileDescriptors);
final GrpcFilter grpcFilter = new GrpcFilter(stubRequestHandler, fileDescriptors, options.notifier());
final FilterHolder filterHolder = new FilterHolder(grpcFilter);
mockServiceContext.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import static org.wiremock.grpc.dsl.GrpcResponseDefinitionBuilder.GRPC_STATUS_REASON;
import static org.wiremock.grpc.internal.Delays.delayIfRequired;

import com.github.tomakehurst.wiremock.common.LocalNotifier;
import com.github.tomakehurst.wiremock.common.Notifier;
import com.github.tomakehurst.wiremock.http.HttpHeader;
import com.github.tomakehurst.wiremock.http.StubRequestHandler;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
Expand All @@ -32,12 +34,16 @@
public class UnaryServerCallHandler extends BaseCallHandler
implements ServerCalls.UnaryMethod<DynamicMessage, DynamicMessage> {

private final Notifier notifier;

public UnaryServerCallHandler(
StubRequestHandler stubRequestHandler,
Descriptors.ServiceDescriptor serviceDescriptor,
Descriptors.MethodDescriptor methodDescriptor,
JsonMessageConverter jsonMessageConverter) {
JsonMessageConverter jsonMessageConverter,
Notifier notifier) {
super(stubRequestHandler, serviceDescriptor, methodDescriptor, jsonMessageConverter);
this.notifier = notifier;
}

@Override
Expand All @@ -53,6 +59,7 @@ public void invoke(DynamicMessage request, StreamObserver<DynamicMessage> respon
methodDescriptor.getName(),
jsonMessageConverter.toJson(request));

LocalNotifier.set(notifier);
stubRequestHandler.handle(
wireMockRequest,
(req, resp, attributes) -> {
Expand Down
90 changes: 89 additions & 1 deletion src/test/java/org/wiremock/grpc/GrpcAcceptanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -54,6 +55,7 @@ public class GrpcAcceptanceTest {
GreetingsClient greetingsClient;
AnotherGreetingsClient anotherGreetingsClient;
WireMock wireMock;
TestNotifier notifier;

@RegisterExtension
public static WireMockExtension wm =
Expand All @@ -62,7 +64,8 @@ public class GrpcAcceptanceTest {
wireMockConfig()
.dynamicPort()
.withRootDirectory("src/test/resources/wiremock")
.extensions(new GrpcExtensionFactory()))
.extensions(new GrpcExtensionFactory())
.notifier(new TestNotifier()))
.build();

@BeforeEach
Expand All @@ -78,6 +81,9 @@ void init() {
anotherChannel =
ManagedChannelBuilder.forAddress("localhost", wm.getPort()).usePlaintext().build();
anotherGreetingsClient = new AnotherGreetingsClient(anotherChannel);

notifier = (TestNotifier) wm.getOptions().notifier();
notifier.clear();
}

@AfterEach
Expand All @@ -100,6 +106,9 @@ void shouldReturnGreetingBuiltViaTemplatedJsonWithRawStubbing() {
String greeting = greetingsClient.greet("Tom");

assertThat(greeting, is("Hello Tom"));

assertNoErrorMessages();
assertSomeInfoMessages();
}

@Test
Expand All @@ -112,6 +121,9 @@ void shouldReturnGreetingBuiltViaTemplatedJson() {
String greeting = greetingsClient.greet("Tom");

assertThat(greeting, is("Hello Tom"));

assertNoErrorMessages();
assertSomeInfoMessages();
}

@Test
Expand All @@ -122,6 +134,9 @@ void returnsResponseBuiltFromJson() {
String greeting = greetingsClient.greet("Whatever");

assertThat(greeting, is("Hi Tom from JSON"));

assertNoErrorMessages();
assertSomeInfoMessages();
}

@Test
Expand All @@ -133,6 +148,9 @@ void returnsResponseBuiltFromMessageObject() {
String greeting = greetingsClient.greet("Whatever");

assertThat(greeting, is("Hi Tom from object"));

assertNoErrorMessages();
assertSomeInfoMessages();
}

@Test
Expand All @@ -145,6 +163,9 @@ void matchesRequestViaCoreJsonMatcher() {
assertThat(greetingsClient.greet("Tom"), is("OK"));

assertThrows(StatusRuntimeException.class, () -> greetingsClient.greet("Wrong"));

assertSomeErrorMessages();
assertSomeInfoMessages();
}

@Test
Expand All @@ -160,6 +181,9 @@ void matchesRequestViaExactMessageEquality() {
assertThrows(StatusRuntimeException.class, () -> greetingsClient.greet("Wrong"));
assertThat(
exception.getMessage(), is("NOT_FOUND: No matching stub mapping found for gRPC request"));

assertSomeErrorMessages();
assertSomeInfoMessages();
}

@Test
Expand All @@ -170,6 +194,9 @@ void returnsResponseWithStatus() {
StatusRuntimeException exception =
assertThrows(StatusRuntimeException.class, () -> greetingsClient.greet("Whatever"));
assertThat(exception.getMessage(), is("FAILED_PRECONDITION: Failed some blah prerequisite"));

assertNoErrorMessages();
assertSomeInfoMessages();
}

@Test
Expand All @@ -180,6 +207,9 @@ void returnsUnaryResponseToFirstMatchingMessagesInStreamingRequest() {
.willReturn(message(HelloResponse.newBuilder().setGreeting("Hi Rob"))));

assertThat(greetingsClient.manyGreetingsOneReply("Tom", "Uri", "Rob", "Mark"), is("Hi Rob"));

assertSomeErrorMessages();
assertSomeInfoMessages();
}

@Test
Expand All @@ -197,6 +227,9 @@ void throwsNotFoundWhenNoStreamingClientMessageMatches() {
assertThat(
exception.getCause().getMessage(),
is("NOT_FOUND: No matching stub mapping found for gRPC request"));

assertSomeErrorMessages();
assertSomeInfoMessages();
}

@Test
Expand All @@ -211,6 +244,9 @@ void throwsReturnedErrorFromStreamingClientCall() {
Exception.class, () -> greetingsClient.manyGreetingsOneReply("Tom", "Jerf", "Rob"));
assertThat(exception.getCause(), instanceOf(StatusRuntimeException.class));
assertThat(exception.getCause().getMessage(), is("INVALID_ARGUMENT: Jerf is not a valid name"));

assertSomeErrorMessages();
assertSomeInfoMessages();
}

@Test
Expand All @@ -220,6 +256,9 @@ void returnsStreamedResponseToUnaryRequest() {
.willReturn(message(HelloResponse.newBuilder().setGreeting("Hi Tom"))));

assertThat(greetingsClient.oneGreetingManyReplies("Tom"), hasItem("Hi Tom"));

assertNoErrorMessages();
assertSomeInfoMessages();
}

@Test
Expand All @@ -228,6 +267,9 @@ void returnsResponseWithImportedType() {
method("oneGreetingEmptyReply").willReturn(message(Empty.newBuilder())));

assertThat(greetingsClient.oneGreetingEmptyReply("Tom"), is(true));

assertNoErrorMessages();
assertSomeInfoMessages();
}

@Test
Expand All @@ -247,6 +289,9 @@ void verifiesViaJson() {
mockGreetingService
.verify(0, "greeting")
.withRequestMessage(equalToJson("{ \"name\": \"Chris\" }"));

assertNoErrorMessages();
assertSomeInfoMessages();
}

@Test
Expand All @@ -261,6 +306,9 @@ void verifiesViaMessage() {
.withRequestMessage(equalToMessage(HelloRequest.newBuilder().setName("Peter")));

mockGreetingService.verify(0, "oneGreetingEmptyReply");

assertNoErrorMessages();
assertSomeInfoMessages();
}

@Test
Expand All @@ -270,6 +318,9 @@ void networkFault() {
Exception exception =
assertThrows(StatusRuntimeException.class, () -> greetingsClient.greet("Alan"));
assertThat(exception.getMessage(), startsWith("UNKNOWN"));

assertNoErrorMessages();
assertSomeInfoMessages();
}

@Test
Expand All @@ -285,6 +336,9 @@ void fixedDelay() {

assertThat(greeting, is("Delayed hello"));
assertThat(stopwatch.elapsed(), greaterThanOrEqualTo(Duration.ofMillis(1000L)));

assertNoErrorMessages();
assertSomeInfoMessages();
}

@Test
Expand All @@ -300,6 +354,9 @@ void randomDelay() {

assertThat(greeting, is("Delayed hello"));
assertThat(stopwatch.elapsed(), greaterThanOrEqualTo(Duration.ofMillis(500L)));

assertNoErrorMessages();
assertSomeInfoMessages();
}

@Test
Expand All @@ -326,6 +383,9 @@ void resetStubs() {
anotherMockGreetingService.removeAllStubs();

verifyDefaultMappings();

assertNoErrorMessages();
assertNoInfoMessages();
}

private void verifyDefaultMappings() {
Expand Down Expand Up @@ -391,6 +451,9 @@ void resetAll() {
anotherMockGreetingService
.verify(6, "anotherGreeting")
.withRequestMessage(equalToMessage(HelloRequest.newBuilder().setName("Tom")));

assertNoErrorMessages();
assertSomeInfoMessages();
}

@Test
Expand All @@ -402,6 +465,9 @@ void unaryMethodWithAnyRequest() {
String greeting = greetingsClient.greetAnyRequest();

assertThat(greeting, is("Hiya"));

assertNoErrorMessages();
assertSomeInfoMessages();
}

@Test
Expand All @@ -413,6 +479,9 @@ void unaryMethodWithAnyResponse() {
String typeUrl = greetingsClient.greetAnyResponse();

assertThat(typeUrl, is("type.googleapis.com/com.example.grpc.response.HelloResponse"));

assertNoErrorMessages();
assertSomeInfoMessages();
}

@Test
Expand All @@ -426,5 +495,24 @@ void unaryMethodWithAnyResponseFromJson() {
String typeUrl = greetingsClient.greetAnyResponse();

assertThat(typeUrl, is("type.googleapis.com/com.example.grpc.response.HelloResponse"));

assertNoErrorMessages();
assertSomeInfoMessages();
}

private void assertNoInfoMessages() {
assertThat("Should not be any info messages", notifier.infoMessages, is(empty()));
}

private void assertSomeInfoMessages() {
assertThat("Should be some info messages", notifier.infoMessages, is(not(empty())));
}

private void assertNoErrorMessages() {
assertThat("Should not be any error messages", notifier.errorMessages, is(empty()));
}

private void assertSomeErrorMessages() {
assertThat("Should be some error messages", notifier.errorMessages, is(not(empty())));
}
}
Loading