Reputation: 21
so i have this stub ive created
stubFor(get(urlEqualTo("/skyhost/v1/rms/resourceid/8471jd1da7362f0eb28642s2"))
.willReturn(aResponse().withStatus(200)
.withBodyFile("getstub.json")
.withHeader("Content-Type", "application/json")));
}
what i wane do is make this dynamic as there might be different resources ID not just the one ive provided in the stub. im confused on how to go about this. i want to be able to provide different resourceid and still be able to get that response back
Upvotes: 1
Views: 254
Reputation: 1082
In the querkus archive there is an example under the rest-client-quickstart on how to do this:
public class WireMockExtensions implements QuarkusTestResourceLifecycleManager {
private static final String COUNTRIES_JSON_FILE = "/extensions.json";
private static final String BASE_PATH = "/api";
private static final int WIREMOCK_PORT = 7777;
private WireMockServer wireMockServer;
@Override
public Map<String, String> start() {
wireMockServer = new WireMockServer(WIREMOCK_PORT);
wireMockServer.start();
stubExtensions();
return Collections.singletonMap("quarkus.rest-client.\"org.acme.rest.client.ExtensionsService\".url",
wireMockServer.baseUrl() + BASE_PATH);
}
@Override
public void stop() {
if (Objects.nonNull(wireMockServer))
wireMockServer.stop();
}
private void stubExtensions() {
try (InputStream is = WireMockExtensions.class.getResourceAsStream(COUNTRIES_JSON_FILE)) {
String extensions = new String(is.readAllBytes());
// Stub for full list of extensions:
wireMockServer.stubFor(get(urlEqualTo(BASE_PATH))
.willReturn(okJson(extensions)));
// Stub for each country
try (StringReader sr = new StringReader(extensions);
JsonParser parser = Json.createParser(sr)) {
parser.next();
for (JsonValue extension : parser.getArray()) {
String id = extension.asJsonObject().getString("id");
wireMockServer.stubFor(get(urlEqualTo(BASE_PATH + "/extensions?id=" + URLEncoder.encode(id, StandardCharsets.UTF_8)))
.willReturn(okJson("[" + extension + "]")));
}
}
} catch (IOException e) {
fail("Could not configure Wiremock server. Caused by: " + e.getMessage());
}
}
}
the extensions.json
:
[
{
"id": "io.quarkus:quarkus-rest-client",
"keywords": [
"call",
"microprofile-rest-client",
"quarkus-rest-client",
"rest",
"rest-client",
"services",
"web-client"
],
"name": "REST Client",
"shortName": "REST Client"
},
{
"id": "io.quarkus:quarkus-resteasy",
"keywords": [
"endpoint",
"framework",
"jax",
"jaxrs",
"quarkus-resteasy",
"rest",
"resteasy",
"web"
],
"name": "RESTEasy JAX-RS",
"shortName": "jax-rs"
}
]
Upvotes: 0