Skip to content

Commit

Permalink
Faults and verifications (#10)
Browse files Browse the repository at this point in the history
* Added verifications to the DSL

* Added faults to the DSL
  • Loading branch information
tomakehurst authored Nov 8, 2023
1 parent fb062e6 commit e550542
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 5 deletions.
12 changes: 10 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ repositories {
}

group 'org.wiremock'
version = "0.1.0"

allprojects {
version = "0.2.0"

sourceCompatibility = 11
targetCompatibility = 11

Expand Down Expand Up @@ -171,8 +172,15 @@ project.tasks.signMainPublication.dependsOn jar

assemble.dependsOn clean, jar

task addGitTag {
doLast {
println "git tag ${version}".execute().text
println "git push origin --tags".execute().text
}
}

task release {
dependsOn clean, assemble, publishAllPublicationsToMavenRepository
dependsOn clean, assemble, publishAllPublicationsToMavenRepository, addGitTag
}

task localRelease {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.wiremock.grpc.dsl;

import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
import com.github.tomakehurst.wiremock.http.Fault;
import org.wiremock.annotations.Beta;

@Beta(justification = "Incubating extension: https://github.com/wiremock/wiremock/issues/2383")
Expand All @@ -25,7 +26,11 @@ public class GrpcResponseDefinitionBuilder {
public static final String GRPC_STATUS_REASON = "grpc-status-reason";
private final WireMockGrpc.Status grpcStatus;
private final String statusReason;

private final Fault fault;

private String json;

private boolean templatingEnabled;

public GrpcResponseDefinitionBuilder(WireMockGrpc.Status grpcStatus) {
Expand All @@ -35,6 +40,13 @@ public GrpcResponseDefinitionBuilder(WireMockGrpc.Status grpcStatus) {
public GrpcResponseDefinitionBuilder(WireMockGrpc.Status grpcStatus, String statusReason) {
this.grpcStatus = grpcStatus;
this.statusReason = statusReason;
this.fault = null;
}

public GrpcResponseDefinitionBuilder(Fault fault) {
this.fault = fault;
this.grpcStatus = null;
this.statusReason = null;
}

public GrpcResponseDefinitionBuilder fromJson(String json) {
Expand All @@ -48,6 +60,10 @@ public GrpcResponseDefinitionBuilder withTemplatingEnabled(boolean enabled) {
}

public ResponseDefinitionBuilder build() {
if (fault != null) {
return ResponseDefinitionBuilder.responseDefinition().withFault(fault);
}

final ResponseDefinitionBuilder responseDefinitionBuilder =
ResponseDefinitionBuilder.responseDefinition()
.withHeader(GRPC_STATUS_NAME, grpcStatus.name());
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/org/wiremock/grpc/dsl/GrpcStubMappingBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
*/
package org.wiremock.grpc.dsl;

import static org.wiremock.grpc.internal.UrlUtils.grpcUrlPath;

import com.github.tomakehurst.wiremock.client.MappingBuilder;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.http.Fault;
import com.github.tomakehurst.wiremock.matching.StringValuePattern;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import java.util.ArrayList;
Expand Down Expand Up @@ -50,9 +53,13 @@ public GrpcStubMappingBuilder willReturn(WireMockGrpc.Status status, String stat
return this;
}

public GrpcStubMappingBuilder willReturn(Fault fault) {
this.responseBuilder = new GrpcResponseDefinitionBuilder(fault);
return this;
}

public StubMapping build(String serviceName) {
final String path = "/" + serviceName + "/" + method;
final MappingBuilder mappingBuilder = WireMock.post(WireMock.urlPathEqualTo(path));
final MappingBuilder mappingBuilder = WireMock.post(grpcUrlPath(serviceName, method));
requestMessageJsonPatterns.forEach(mappingBuilder::withRequestBody);
return mappingBuilder.willReturn(responseBuilder.build()).build();
}
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/org/wiremock/grpc/dsl/GrpcVerification.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2023 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.dsl;

import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static org.wiremock.grpc.internal.UrlUtils.grpcUrlPath;

import com.github.tomakehurst.wiremock.client.CountMatchingStrategy;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.matching.StringValuePattern;

public class GrpcVerification {

private final WireMock wireMock;
private final CountMatchingStrategy countMatch;

private final String serviceName;
private final String method;

public GrpcVerification(
WireMock wireMock, CountMatchingStrategy countMatch, String serviceName, String method) {
this.wireMock = wireMock;
this.countMatch = countMatch;
this.serviceName = serviceName;
this.method = method;
}

public void withRequestMessage(StringValuePattern matcher) {
wireMock.verifyThat(
countMatch, postRequestedFor(grpcUrlPath(serviceName, method)).withRequestBody(matcher));
}
}
16 changes: 16 additions & 0 deletions src/main/java/org/wiremock/grpc/dsl/WireMockGrpcService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package org.wiremock.grpc.dsl;

import static com.github.tomakehurst.wiremock.client.WireMock.exactly;
import static com.github.tomakehurst.wiremock.client.WireMock.moreThanOrExactly;

import com.github.tomakehurst.wiremock.client.CountMatchingStrategy;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import org.wiremock.annotations.Beta;
Expand All @@ -35,4 +39,16 @@ public StubMapping stubFor(GrpcStubMappingBuilder builder) {
wireMock.register(stubMapping);
return stubMapping;
}

public GrpcVerification verify(String method) {
return new GrpcVerification(wireMock, moreThanOrExactly(1), serviceName, method);
}

public GrpcVerification verify(int count, String method) {
return new GrpcVerification(wireMock, exactly(count), serviceName, method);
}

public GrpcVerification verify(CountMatchingStrategy countMatch, String method) {
return new GrpcVerification(wireMock, countMatch, serviceName, method);
}
}
28 changes: 28 additions & 0 deletions src/main/java/org/wiremock/grpc/internal/UrlUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (C) 2023 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.internal;

import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.matching.UrlPattern;

public class UrlUtils {

private UrlUtils() {}

public static UrlPattern grpcUrlPath(String serviceName, String method) {
return WireMock.urlPathEqualTo("/" + serviceName + "/" + method);
}
}
43 changes: 43 additions & 0 deletions src/test/java/org/wiremock/grpc/GrpcAcceptanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.example.grpc.HelloRequest;
import com.example.grpc.HelloResponse;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.http.Fault;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import com.google.protobuf.Empty;
import io.grpc.ManagedChannel;
Expand Down Expand Up @@ -211,4 +212,46 @@ void returnsResponseWithImportedType() {

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

@Test
void verifiesViaJson() {
mockGreetingService.stubFor(
method("greeting").willReturn(message(HelloResponse.newBuilder().setGreeting("Hi"))));

greetingsClient.greet("Peter");
greetingsClient.greet("Peter");

mockGreetingService
.verify(moreThanOrExactly(2), "greeting")
.withRequestMessage(equalToJson("{ \"name\": \"Peter\" }"));

mockGreetingService.verify(0, "oneGreetingEmptyReply");

mockGreetingService
.verify(0, "greeting")
.withRequestMessage(equalToJson("{ \"name\": \"Chris\" }"));
}

@Test
void verifiesViaMessage() {
mockGreetingService.stubFor(
method("greeting").willReturn(message(HelloResponse.newBuilder().setGreeting("Hi"))));

greetingsClient.greet("Peter");

mockGreetingService
.verify("greeting")
.withRequestMessage(equalToMessage(HelloRequest.newBuilder().setName("Peter")));

mockGreetingService.verify(0, "oneGreetingEmptyReply");
}

@Test
void networkFault() {
mockGreetingService.stubFor(method("greeting").willReturn(Fault.CONNECTION_RESET_BY_PEER));

Exception exception =
assertThrows(StatusRuntimeException.class, () -> greetingsClient.greet("Alan"));
assertThat(exception.getMessage(), is("UNKNOWN"));
}
}
1 change: 0 additions & 1 deletion wiremock-grpc-extension-standalone/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ repositories {
}

group 'org.wiremock'
version = "0.1.0"

dependencies {
api project(":")
Expand Down

0 comments on commit e550542

Please sign in to comment.