forked from quarkusio/quarkus-super-heroes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #164
- Loading branch information
Showing
12 changed files
with
587 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...fights/src/test/java/io/quarkus/sample/superheroes/fight/client/HeroClientTestRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package io.quarkus.sample.superheroes.fight.client; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.time.Duration; | ||
|
||
import javax.inject.Inject; | ||
|
||
import io.smallrye.mutiny.helpers.test.UniAssertSubscriber; | ||
|
||
/** | ||
* This class exists so that some of the tests and assertions with the Hero service | ||
* can be re-used between the Pact consumer contract tests and the actual tests | ||
* that use WireMock and perform more thorough verifications that don't belong | ||
* in a consumer contract test. | ||
*/ | ||
abstract class HeroClientTestRunner { | ||
protected static final String HERO_API_BASE_URI = "/api/heroes"; | ||
protected static final String HERO_RANDOM_URI = HERO_API_BASE_URI + "/random"; | ||
protected static final String HERO_HELLO_URI = HERO_API_BASE_URI + "/hello"; | ||
protected static final String DEFAULT_HERO_NAME = "Super Baguette"; | ||
protected static final String DEFAULT_HERO_PICTURE = "super_baguette.png"; | ||
protected static final String DEFAULT_HERO_POWERS = "eats baguette really quickly"; | ||
protected static final int DEFAULT_HERO_LEVEL = 42; | ||
protected static final String DEFAULT_HELLO_RESPONSE = "Hello heroes!"; | ||
|
||
@Inject | ||
HeroClient heroClient; | ||
|
||
protected final void runHelloHeroes() { | ||
this.heroClient.helloHeroes() | ||
.subscribe().withSubscriber(UniAssertSubscriber.create()) | ||
.assertSubscribed() | ||
.awaitItem(Duration.ofSeconds(5)) | ||
.assertItem(DEFAULT_HELLO_RESPONSE); | ||
} | ||
|
||
protected final void runRandomHeroNotFound() { | ||
this.heroClient.findRandomHero() | ||
.subscribe().withSubscriber(UniAssertSubscriber.create()) | ||
.assertSubscribed() | ||
.awaitItem(Duration.ofSeconds(5)) | ||
.assertItem(null); | ||
} | ||
|
||
protected final void runRandomHeroFound(boolean verifyPowers) { | ||
var hero = this.heroClient.findRandomHero() | ||
.subscribe().withSubscriber(UniAssertSubscriber.create()) | ||
.assertSubscribed() | ||
.awaitItem(Duration.ofSeconds(5)) | ||
.getItem(); | ||
|
||
assertThat(hero) | ||
.isNotNull() | ||
.extracting( | ||
Hero::getName, | ||
Hero::getLevel, | ||
Hero::getPicture, | ||
Hero::getPowers | ||
) | ||
.containsExactly( | ||
DEFAULT_HERO_NAME, | ||
DEFAULT_HERO_LEVEL, | ||
DEFAULT_HERO_PICTURE, | ||
verifyPowers ? DEFAULT_HERO_POWERS : null | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...s/src/test/java/io/quarkus/sample/superheroes/fight/client/HeroConsumerContractTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package io.quarkus.sample.superheroes.fight.client; | ||
|
||
import static au.com.dius.pact.consumer.dsl.LambdaDsl.newJsonBody; | ||
|
||
import java.util.Map; | ||
|
||
import javax.ws.rs.HttpMethod; | ||
import javax.ws.rs.core.HttpHeaders; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response.Status; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.condition.EnabledIfSystemProperty; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.junit.TestProfile; | ||
|
||
import au.com.dius.pact.consumer.dsl.PactDslRootValue; | ||
import au.com.dius.pact.consumer.dsl.PactDslWithProvider; | ||
import au.com.dius.pact.consumer.junit5.PactConsumerTestExt; | ||
import au.com.dius.pact.consumer.junit5.PactTestFor; | ||
import au.com.dius.pact.core.model.PactSpecVersion; | ||
import au.com.dius.pact.core.model.V4Pact; | ||
import au.com.dius.pact.core.model.annotations.Pact; | ||
|
||
/** | ||
* Pact consumer contract tests for the {@link io.quarkus.sample.superheroes.fight.client.HeroClient}. | ||
*/ | ||
@QuarkusTest | ||
@TestProfile(PactConsumerContractTestProfile.class) | ||
@ExtendWith(PactConsumerTestExt.class) | ||
@PactTestFor( | ||
providerName = "rest-heroes", | ||
pactVersion = PactSpecVersion.V4, | ||
hostInterface = "localhost", | ||
// Make an assumption and hard-code the Pact MockServer to be running on port 8081 | ||
// I don't like it but couldn't figure out any other way | ||
port = "8081" | ||
) | ||
@EnabledIfSystemProperty(named = "runConsumerContractTests", matches = "true", disabledReason = "runConsumerContractTests system property not set or equals false") | ||
public class HeroConsumerContractTests extends HeroClientTestRunner { | ||
@Pact(consumer = "rest-fights") | ||
public V4Pact helloPact(PactDslWithProvider builder) { | ||
return builder | ||
.uponReceiving("A hello request") | ||
.path(HERO_HELLO_URI) | ||
.method(HttpMethod.GET) | ||
.headers(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN) | ||
.willRespondWith() | ||
.headers(Map.of(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN)) | ||
.status(Status.OK.getStatusCode()) | ||
.body(PactDslRootValue.stringMatcher(".+", DEFAULT_HELLO_RESPONSE)) | ||
.toPact(V4Pact.class); | ||
} | ||
|
||
@Pact(consumer = "rest-fights") | ||
public V4Pact randomHeroNotFoundPact(PactDslWithProvider builder) { | ||
return builder | ||
.given("No random hero found") | ||
.uponReceiving("A request for a random hero") | ||
.path(HERO_RANDOM_URI) | ||
.method(HttpMethod.GET) | ||
.headers(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON) | ||
.willRespondWith() | ||
.status(Status.NOT_FOUND.getStatusCode()) | ||
.toPact(V4Pact.class); | ||
} | ||
|
||
@Pact(consumer = "rest-fights") | ||
public V4Pact randomHeroFoundPact(PactDslWithProvider builder) { | ||
return builder | ||
.uponReceiving("A request for a random hero") | ||
.path(HERO_RANDOM_URI) | ||
.method(HttpMethod.GET) | ||
.headers(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON) | ||
.willRespondWith() | ||
.status(Status.OK.getStatusCode()) | ||
.headers(Map.of(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)) | ||
.body(newJsonBody(body -> | ||
body | ||
.stringType("name", DEFAULT_HERO_NAME) | ||
.integerType("level", DEFAULT_HERO_LEVEL) | ||
.stringType("picture", DEFAULT_HERO_PICTURE) | ||
).build() | ||
) | ||
.toPact(V4Pact.class); | ||
} | ||
|
||
@Test | ||
@PactTestFor(pactMethod = "helloPact") | ||
void helloHeroes() { | ||
runHelloHeroes(); | ||
} | ||
|
||
@Test | ||
@PactTestFor(pactMethod = "randomHeroNotFoundPact") | ||
void randomHeroNotFound() { | ||
runRandomHeroNotFound(); | ||
} | ||
|
||
@Test | ||
@PactTestFor(pactMethod = "randomHeroFoundPact") | ||
void randomHeroFound() { | ||
runRandomHeroFound(false); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...test/java/io/quarkus/sample/superheroes/fight/client/PactConsumerContractTestProfile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.quarkus.sample.superheroes.fight.client; | ||
|
||
import java.util.Map; | ||
|
||
import io.quarkus.test.junit.QuarkusTestProfile; | ||
|
||
/** | ||
* Quarkus {@link io.quarkus.test.junit.QuarkusTestProfile} for deriving a new profile for handling Pact consumer | ||
* contract tests. Mostly here so that the Hero and Villain rest client URLs are | ||
* set to point to the Pact {@link au.com.dius.pact.consumer.MockServer MockServer} and | ||
* not the WireMock mocks. | ||
* <p> | ||
* Also makes an assumption and hard-codes the Pact {@link au.com.dius.pact.consumer.MockServer MockServer} | ||
* to be running on {@code localhost:8081}. | ||
* </p> | ||
* <p> | ||
* Quarkus itself is set to run its tests on a random port, so port {@code 8081} should be available. | ||
* </p> | ||
*/ | ||
public class PactConsumerContractTestProfile implements QuarkusTestProfile { | ||
// Make an assumption and hard-code the Pact MockServer to be running on port 8081 | ||
// I don't like it but couldn't figure out any other way | ||
private static final String URL = "localhost:8081"; | ||
|
||
@Override | ||
public Map<String, String> getConfigOverrides() { | ||
return Map.of( | ||
"quarkus.stork.hero-service.service-discovery.address-list", URL, | ||
"quarkus.stork.villain-service.service-discovery.address-list", URL | ||
); | ||
} | ||
|
||
@Override | ||
public String getConfigProfile() { | ||
return "pact-consumer-contract"; | ||
} | ||
|
||
@Override | ||
public boolean disableGlobalTestResources() { | ||
return true; | ||
} | ||
} |
Oops, something went wrong.