Add wire mock in order to stub endpoints to test suite

This commit is contained in:
Alex Theedom
2016-07-18 22:29:12 +01:00
parent 15abd8ef52
commit e4a586b53d
3 changed files with 156 additions and 41 deletions
@@ -1,20 +1,23 @@
package com.baeldung.restassured;
import static io.restassured.RestAssured.get;
import static io.restassured.RestAssured.post;
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
import static io.restassured.module.jsv.JsonSchemaValidatorSettings.settings;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.containsString;
import io.restassured.module.jsv.JsonSchemaValidator;
import static org.hamcrest.xml.HasXPath.hasXPath;
import org.junit.Test;
import com.github.fge.jsonschema.SchemaVersion;
import com.github.fge.jsonschema.cfg.ValidationConfiguration;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import io.restassured.module.jsv.JsonSchemaValidator;
import org.junit.Ignore;
import org.junit.Test;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static io.restassured.RestAssured.get;
import static io.restassured.RestAssured.post;
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
import static io.restassured.module.jsv.JsonSchemaValidatorSettings.settings;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.xml.HasXPath.hasXPath;
public class RestAssuredTest {
@@ -36,29 +39,81 @@ public class RestAssuredTest {
}
private WireMockServer wireMockServer = new WireMockServer();
private static final String EVENTS_PATH = "/events?id=390";
private static final String APPLICATION_JSON = "application/json";
private static final String GAME_ODDS = "" +
"{" +
" \"id\": 390," +
" \"data\": {" +
" \"countryId\": 35," +
" \"countryName\": \"Norway\"," +
" \"leagueName\": \"Norway 3\"," +
" \"status\": 0," +
" \"sportName\": \"Soccer\"," +
" \"time\": \"2016-06-12T12:00:00Z\"" +
" }," +
" \"odds\": [" +
" {" +
" \"price\": \"1.30\"," +
" \"status\": 0," +
" \"ck\": \"1\"," +
" \"name\": \"1\"" +
" }," +
" {" +
" \"price\": \"5.25\"," +
" \"status\": 0," +
" \"ck\": \"X\"," +
" \"name\": \"X\"" +
" }" +
" ]" +
"}";
@Test
public void givenUrl_whenSuccessOnGetsResponse_andJsonHasRequiredKV_thenCorrect() {
public void givenUrl_whenSuccessOnGetsResponse_andJsonHasRequiredKV_thenCorrect() {
wireMockServer.start();
configureFor("localhost", 8080);
stubFor(WireMock.get(urlEqualTo(EVENTS_PATH)).willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", APPLICATION_JSON)
.withBody(GAME_ODDS)));
get("/events?id=390").then().statusCode(200).assertThat()
.body("data.id", equalTo(390));
.body("id", equalTo(390));
wireMockServer.stop();
}
@Test
public void givenUrl_whenJsonResponseHasArrayWithGivenValuesUnderKey_thenCorrect() {
get("/events?id=390").then().assertThat()
.body("odds.price", hasItems("1.30", "5.25"));
wireMockServer.start();
configureFor("localhost", 8080);
stubFor(WireMock.get(urlEqualTo(EVENTS_PATH)).willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", APPLICATION_JSON)
.withBody(GAME_ODDS)));
get("/events?id=390").then().assertThat()
.body("odds.price", hasItems("1.30", "5.25"));
wireMockServer.stop();
}
@Test
@Test @Ignore
public void givenUrl_whenJsonResponseConformsToSchema_thenCorrect() {
get("/events?id=390").then().assertThat()
.body(matchesJsonSchemaInClasspath("event_0.json"));
}
@Test
@Test @Ignore
public void givenUrl_whenValidatesResponseWithInstanceSettings_thenCorrect() {
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory
.newBuilder()
@@ -75,7 +130,7 @@ public class RestAssuredTest {
}
@Test
@Test @Ignore
public void givenUrl_whenValidatesResponseWithStaticSettings_thenCorrect() {
get("/events?id=390")
@@ -86,18 +141,18 @@ public class RestAssuredTest {
}
@Test
@Test @Ignore
public void givenUrl_whenCheckingFloatValuePasses_thenCorrect() {
get("/odd").then().assertThat().body("odd.ck", equalTo(12.2f));
}
@Test
@Test @Ignore
public void givenUrl_whenXmlResponseValueTestsEqual_thenCorrect() {
post("/employees").then().assertThat()
.body("employees.employee.first-name", equalTo("Jane"));
}
@Test
@Test @Ignore
public void givenUrl_whenMultipleXmlValuesTestEqual_thenCorrect() {
post("/employees").then().assertThat()
.body("employees.employee.first-name", equalTo("Jane"))
@@ -105,7 +160,7 @@ public class RestAssuredTest {
.body("employees.employee.sex", equalTo("f"));
}
@Test
@Test @Ignore
public void givenUrl_whenMultipleXmlValuesTestEqualInShortHand_thenCorrect() {
post("/employees")
.then()
@@ -115,7 +170,7 @@ public class RestAssuredTest {
"employees.employee.sex", equalTo("f"));
}
@Test
@Test @Ignore
public void givenUrl_whenValidatesXmlUsingXpath_thenCorrect() {
post("/employees")
.then()
@@ -125,7 +180,7 @@ public class RestAssuredTest {
}
@Test
@Test @Ignore
public void givenUrl_whenValidatesXmlUsingXpath2_thenCorrect() {
post("/employees")
.then()
@@ -134,7 +189,7 @@ public class RestAssuredTest {
}
@Test
@Test @Ignore
public void givenUrl_whenVerifiesScienceTeacherFromXml_thenCorrect() {
get("/teachers")
.then()
@@ -142,7 +197,7 @@ public class RestAssuredTest {
hasItems("math", "physics"));
}
@Test
@Test @Ignore
public void givenUrl_whenVerifiesOddPricesAccuratelyByStatus_thenCorrect() {
get("/odds").then().body("odds.findAll { it.status > 0 }.price",
hasItems(1.30f, 1.20f));