Skip to content

Commit

Permalink
Update wiremock grpc (#907)
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Deandrea <edeandrea@redhat.com>
  • Loading branch information
edeandrea authored May 9, 2024
1 parent 7437984 commit fb57afe
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 50 deletions.
2 changes: 1 addition & 1 deletion rest-fights/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<quarkus.platform.version>3.10.0</quarkus.platform.version>
<surefire-plugin.version>3.2.5</surefire-plugin.version>
<wiremock.version>3.5.4</wiremock.version>
<wiremock-grpc.version>0.5.0</wiremock-grpc.version>
<wiremock-grpc.version>0.6.0</wiremock-grpc.version>
</properties>
<dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,13 @@
* Annotation that can be used to inject one of the following into a test annotated with either {@link io.quarkus.test.junit.QuarkusTest @QuarkusTest} or {@link io.quarkus.test.junit.QuarkusIntegrationTest @QuarkusIntegrationTest}:
* <p>
* <ul>
* <li>{@link com.github.tomakehurst.wiremock.WireMockServer WireMockServer}</li>
* <li>{@link com.github.tomakehurst.wiremock.client.WireMock WireMock}</li>
* <li>{@link org.wiremock.grpc.dsl.WireMockGrpcService WireMockGrpcService}</li>
* </ul>
* </p>
* <p>
* <pre>
* {@code
* @InjectWireMock
* WireMockServer wireMockServer;
* }
* </pre>
* </p>
* <p>
* <pre>
* {@code
* @InjectWireMock
* WireMock wireMock;
* }
* </pre>
* </p>
* <p>
* <pre>
* {@code
* @InjectWireMock
* WireMockGrpcService wireMockGrpcServer;
* }
* </pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,10 @@ public void stop() {

@Override
public void inject(TestInjector testInjector) {
var wireMock = new WireMock(getPort());

testInjector.injectIntoFields(
wireMock,
new AnnotatedAndMatchesType(InjectGrpcWireMock.class, WireMock.class)
);

testInjector.injectIntoFields(
new WireMockGrpcService(wireMock, LocationsGrpc.SERVICE_NAME),
new WireMockGrpcService(new WireMock(getPort()), LocationsGrpc.SERVICE_NAME),
new AnnotatedAndMatchesType(InjectGrpcWireMock.class, WireMockGrpcService.class)
);

testInjector.injectIntoFields(
this.wireMockServer,
new AnnotatedAndMatchesType(InjectGrpcWireMock.class, WireMockServer.class)
);
}

private int getPort() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@
import io.quarkus.sample.superheroes.location.grpc.LocationType;
import io.quarkus.sample.superheroes.location.grpc.RandomLocationRequest;

import com.github.tomakehurst.wiremock.WireMockServer;
import io.grpc.StatusRuntimeException;
import io.smallrye.faulttolerance.api.CircuitBreakerMaintenance;
import io.smallrye.faulttolerance.api.CircuitBreakerState;
import io.smallrye.mutiny.helpers.test.UniAssertSubscriber;

@QuarkusTest
@QuarkusTestResource(value = LocationsWiremockGrpcServerResource.class, restrictToAnnotatedClass = true)
public class LocationClientTests {
class LocationClientTests {
private static final String DEFAULT_HELLO_RESPONSE = "Hello locations!";
private static final String DEFAULT_LOCATION_NAME = "Gotham City";
private static final String DEFAULT_LOCATION_DESCRIPTION = "Dark city where Batman lives.";
Expand All @@ -46,6 +45,7 @@ public class LocationClientTests {
.setPicture(DEFAULT_LOCATION_PICTURE)
.setType(DEFAULT_LOCATION_TYPE)
.build();

private static final FightLocation DEFAULT_FIGHT_LOCATION = new FightLocation(DEFAULT_LOCATION_NAME, DEFAULT_LOCATION_DESCRIPTION, DEFAULT_LOCATION_PICTURE);

@Inject
Expand All @@ -54,25 +54,22 @@ public class LocationClientTests {
@InjectGrpcWireMock
WireMockGrpcService wireMockGrpc;

@InjectGrpcWireMock
WireMockServer wireMockServer;

@Inject
CircuitBreakerMaintenance circuitBreakerMaintenance;

@BeforeEach
public void beforeEach() {
this.wireMockServer.resetAll();
void beforeEach() {
this.wireMockGrpc.resetAll();
}

@AfterEach
public void afterEach() {
void afterEach() {
// Reset all circuit breaker counts after each test
this.circuitBreakerMaintenance.resetAll();
}

@Test
public void helloLocations() {
void helloLocations() {
this.wireMockGrpc.stubFor(
method("Hello")
.willReturn(message(HelloReply.newBuilder().setMessage(DEFAULT_HELLO_RESPONSE)))
Expand All @@ -89,7 +86,7 @@ public void helloLocations() {
}

@Test
public void findsRandom() {
void findsRandom() {
this.wireMockGrpc.stubFor(
method("GetRandomLocation")
.willReturn(message(DEFAULT_LOCATION))
Expand All @@ -113,7 +110,7 @@ public void findsRandom() {
}

@Test
public void findRandomRecoversFromNotFound() {
void findRandomRecoversFromNotFound() {
this.wireMockGrpc.stubFor(
method("GetRandomLocation")
.willReturn(Status.NOT_FOUND, "A location was not found")
Expand All @@ -132,7 +129,7 @@ public void findRandomRecoversFromNotFound() {
}

@Test
public void findRandomDoesntRecoverFromError() {
void findRandomDoesntRecoverFromError() {
this.wireMockGrpc.stubFor(
method("GetRandomLocation")
.willReturn(Status.UNAVAILABLE, "Service isn't there")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,6 @@ class FightResourceIT {
@InjectWireMock
WireMockServer wireMockServer;

@InjectGrpcWireMock
WireMockServer wireMockGrpcServer;

@InjectGrpcWireMock
WireMockGrpcService wireMockGrpc;

Expand Down Expand Up @@ -237,7 +234,7 @@ static void afterAll() {
void beforeEach() {
// Reset WireMock
this.wireMockServer.resetAll();
this.wireMockGrpcServer.resetAll();
this.wireMockGrpc.resetAll();

// Configure Avro Serde for Fight
companion.setCommonClientConfig(Map.of(AvroKafkaSerdeConfig.AVRO_DATUM_PROVIDER, ReflectAvroDatumProvider.class.getName()));
Expand Down Expand Up @@ -1526,7 +1523,7 @@ private void resetLocationCircuitBreakerToClosedState() {
}

// Reset all the mocks on the GrpcMock
this.wireMockGrpcServer.resetAll();
this.wireMockGrpc.resetAll();

// Stub successful requests
this.wireMockGrpc.stubFor(
Expand All @@ -1553,7 +1550,7 @@ private void resetLocationCircuitBreakerToClosedState() {
this.wireMockGrpc.verify(9, "GetRandomLocation")
.withRequestMessage(equalToMessage(RandomLocationRequest.newBuilder()));

this.wireMockGrpcServer.resetAll();
this.wireMockGrpc.resetAll();
}

private void resetNarrationCircuitBreakersToClosedState() {
Expand Down

0 comments on commit fb57afe

Please sign in to comment.