forked from quarkusio/quarkus-quickstarts
-
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.
Fix rest-clients quickstarts by using wiremock to get countries
The REST api https://restcountries.eu/rest used to get a list of countries was moved to https://countrylayer.com/ and requires an API key to keep using it. With these changes, we'll start up a REST API mock server (does not require Docker) to provide a list of countries. I confirmed that these two modules work fine on JVM and Native. This change `quarkus.tls.trust-all=true` is not really necessary but by adding it, users will see the real issue (unauthorized access to https://restcountries.eu/rest)
- Loading branch information
Showing
10 changed files
with
217 additions
and
0 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
1 change: 1 addition & 0 deletions
1
rest-client-quickstart/src/main/resources/application.properties
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
quarkus.tls.trust-all=true | ||
org.acme.rest.client.CountriesService/mp-rest/url=https://restcountries.eu/rest |
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
71 changes: 71 additions & 0 deletions
71
...nt-quickstart/src/test/java/org/acme/rest/client/resources/WireMockCountriesResource.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,71 @@ | ||
package org.acme.rest.client.resources; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.get; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.okJson; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.StringReader; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import javax.json.Json; | ||
import javax.json.JsonValue; | ||
import javax.json.stream.JsonParser; | ||
|
||
import com.github.tomakehurst.wiremock.WireMockServer; | ||
|
||
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; | ||
|
||
public class WireMockCountriesResource implements QuarkusTestResourceLifecycleManager { | ||
|
||
private static final String COUNTRIES_JSON_FILE = "/countries.json"; | ||
private static final String BASE_PATH = "/rest"; | ||
private static final int WIREMOCK_PORT = 7777; | ||
|
||
private WireMockServer wireMockServer; | ||
|
||
@Override | ||
public Map<String, String> start() { | ||
wireMockServer = new WireMockServer(WIREMOCK_PORT); | ||
wireMockServer.start(); | ||
stubCountries(); | ||
return Collections.singletonMap("org.acme.rest.client.CountriesService/mp-rest/url", | ||
wireMockServer.baseUrl() + BASE_PATH); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
if (Objects.nonNull(wireMockServer)) | ||
wireMockServer.stop(); | ||
} | ||
|
||
private void stubCountries() { | ||
|
||
try (InputStream is = WireMockCountriesResource.class.getResourceAsStream(COUNTRIES_JSON_FILE)) { | ||
String countries = new String(is.readAllBytes()); | ||
|
||
// Stub for full list of countries: | ||
wireMockServer.stubFor(get(urlEqualTo(BASE_PATH)) | ||
.willReturn(okJson(countries))); | ||
|
||
// Stub for each country | ||
try (StringReader sr = new StringReader(countries); | ||
JsonParser parser = Json.createParser(sr)) { | ||
parser.next(); | ||
for (JsonValue country : parser.getArray()) { | ||
String name = country.asJsonObject().getString("name"); | ||
|
||
wireMockServer.stubFor(get(urlEqualTo(BASE_PATH + "/v2/name/" + name)) | ||
.willReturn(okJson("[" + country + "]"))); | ||
} | ||
} | ||
|
||
} catch (IOException e) { | ||
fail("Could not configure Wiremock server. Caused by: " + e.getMessage()); | ||
} | ||
} | ||
} |
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,26 @@ | ||
[ | ||
{ | ||
"name": "greece", | ||
"capital": "Athens", | ||
"currencies": [ | ||
{ | ||
"code": "EUR", | ||
"name": "Euro", | ||
"symbol": "€" | ||
} | ||
], | ||
"alpha2Code": "GR" | ||
}, | ||
{ | ||
"name": "Germany", | ||
"capital": "Berlin", | ||
"currencies": [ | ||
{ | ||
"code": "EUR", | ||
"name": "Euro", | ||
"symbol": "€" | ||
} | ||
], | ||
"alpha2Code": "DE" | ||
} | ||
] |
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
1 change: 1 addition & 0 deletions
1
rest-client-reactive-quickstart/src/main/resources/application.properties
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
quarkus.tls.trust-all=true | ||
country-api/mp-rest/url=https://restcountries.eu/rest |
4 changes: 4 additions & 0 deletions
4
...-client-reactive-quickstart/src/test/java/org/acme/rest/client/CountriesResourceTest.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
71 changes: 71 additions & 0 deletions
71
...ve-quickstart/src/test/java/org/acme/rest/client/resources/WireMockCountriesResource.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,71 @@ | ||
package org.acme.rest.client.resources; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.get; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.okJson; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.StringReader; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import javax.json.Json; | ||
import javax.json.JsonValue; | ||
import javax.json.stream.JsonParser; | ||
|
||
import com.github.tomakehurst.wiremock.WireMockServer; | ||
|
||
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; | ||
|
||
public class WireMockCountriesResource implements QuarkusTestResourceLifecycleManager { | ||
|
||
private static final String COUNTRIES_JSON_FILE = "/countries.json"; | ||
private static final String BASE_PATH = "/rest"; | ||
private static final int WIREMOCK_PORT = 7777; | ||
|
||
private WireMockServer wireMockServer; | ||
|
||
@Override | ||
public Map<String, String> start() { | ||
wireMockServer = new WireMockServer(WIREMOCK_PORT); | ||
wireMockServer.start(); | ||
stubCountries(); | ||
return Collections.singletonMap("org.acme.rest.client.CountriesService/mp-rest/url", | ||
wireMockServer.baseUrl() + BASE_PATH); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
if (Objects.nonNull(wireMockServer)) | ||
wireMockServer.stop(); | ||
} | ||
|
||
private void stubCountries() { | ||
|
||
try (InputStream is = WireMockCountriesResource.class.getResourceAsStream(COUNTRIES_JSON_FILE)) { | ||
String countries = new String(is.readAllBytes()); | ||
|
||
// Stub for full list of countries: | ||
wireMockServer.stubFor(get(urlEqualTo(BASE_PATH)) | ||
.willReturn(okJson(countries))); | ||
|
||
// Stub for each country | ||
try (StringReader sr = new StringReader(countries); | ||
JsonParser parser = Json.createParser(sr)) { | ||
parser.next(); | ||
for (JsonValue country : parser.getArray()) { | ||
String name = country.asJsonObject().getString("name"); | ||
|
||
wireMockServer.stubFor(get(urlEqualTo(BASE_PATH + "/v2/name/" + name)) | ||
.willReturn(okJson("[" + country + "]"))); | ||
} | ||
} | ||
|
||
} catch (IOException e) { | ||
fail("Could not configure Wiremock server. Caused by: " + e.getMessage()); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
rest-client-reactive-quickstart/src/test/resources/countries.json
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,26 @@ | ||
[ | ||
{ | ||
"name": "greece", | ||
"capital": "Athens", | ||
"currencies": [ | ||
{ | ||
"code": "EUR", | ||
"name": "Euro", | ||
"symbol": "€" | ||
} | ||
], | ||
"alpha2Code": "GR" | ||
}, | ||
{ | ||
"name": "Germany", | ||
"capital": "Berlin", | ||
"currencies": [ | ||
{ | ||
"code": "EUR", | ||
"name": "Euro", | ||
"symbol": "€" | ||
} | ||
], | ||
"alpha2Code": "DE" | ||
} | ||
] |