added maven project for rest assured tutorial
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
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;
|
||||
|
||||
public class RestAssuredTest {
|
||||
|
||||
@Test
|
||||
public void givenJsonResponse_whenKeyValuePairMatches_thenCorrect() {
|
||||
JsonSchemaFactory factory = JsonSchemaFactory
|
||||
.newBuilder()
|
||||
.setValidationConfiguration(
|
||||
ValidationConfiguration.newBuilder()
|
||||
.setDefaultVersion(SchemaVersion.DRAFTV3)
|
||||
.freeze()).freeze();
|
||||
JsonSchemaValidator.settings = settings().with()
|
||||
.jsonSchemaFactory(factory).and().with()
|
||||
.checkedValidation(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJsonArrayOfSimilarObjects_whenHasGivenValuesForGivenKey_thenCorrect() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenSuccessOnGetsResponse_andJsonHasRequiredKV_thenCorrect() {
|
||||
|
||||
get("/events?id=390").then().statusCode(200).assertThat()
|
||||
.body("data.id", equalTo(390));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenJsonResponseHasArrayWithGivenValuesUnderKey_thenCorrect() {
|
||||
|
||||
get("/events?id=390").then().assertThat()
|
||||
.body("odds.price", hasItems("1.30", "5.25"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenJsonResponseConformsToSchema_thenCorrect() {
|
||||
get("/events?id=390").then().assertThat()
|
||||
.body(matchesJsonSchemaInClasspath("event_0.json"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenValidatesResponseWithInstanceSettings_thenCorrect() {
|
||||
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory
|
||||
.newBuilder()
|
||||
.setValidationConfiguration(
|
||||
ValidationConfiguration.newBuilder()
|
||||
.setDefaultVersion(SchemaVersion.DRAFTV4)
|
||||
.freeze()).freeze();
|
||||
|
||||
get("/events?id=390")
|
||||
.then()
|
||||
.assertThat()
|
||||
.body(matchesJsonSchemaInClasspath("event_0.json").using(
|
||||
jsonSchemaFactory));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenValidatesResponseWithStaticSettings_thenCorrect() {
|
||||
|
||||
get("/events?id=390")
|
||||
.then()
|
||||
.assertThat()
|
||||
.body(matchesJsonSchemaInClasspath("event_0.json").using(
|
||||
settings().with().checkedValidation(false)));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenCheckingFloatValuePasses_thenCorrect() {
|
||||
get("/odd").then().assertThat().body("odd.ck", equalTo(12.2f));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenXmlResponseValueTestsEqual_thenCorrect() {
|
||||
post("/employees").then().assertThat()
|
||||
.body("employees.employee.first-name", equalTo("Jane"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenMultipleXmlValuesTestEqual_thenCorrect() {
|
||||
post("/employees").then().assertThat()
|
||||
.body("employees.employee.first-name", equalTo("Jane"))
|
||||
.body("employees.employee.last-name", equalTo("Daisy"))
|
||||
.body("employees.employee.sex", equalTo("f"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenMultipleXmlValuesTestEqualInShortHand_thenCorrect() {
|
||||
post("/employees")
|
||||
.then()
|
||||
.assertThat()
|
||||
.body("employees.employee.first-name", equalTo("Jane"),
|
||||
"employees.employee.last-name", equalTo("Daisy"),
|
||||
"employees.employee.sex", equalTo("f"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenValidatesXmlUsingXpath_thenCorrect() {
|
||||
post("/employees")
|
||||
.then()
|
||||
.assertThat()
|
||||
.body(hasXPath("/employees/employee/first-name",
|
||||
containsString("Ja")));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenValidatesXmlUsingXpath2_thenCorrect() {
|
||||
post("/employees")
|
||||
.then()
|
||||
.assertThat()
|
||||
.body(hasXPath("/employees/employee/first-name[text()='Jane']"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenVerifiesScienceTeacherFromXml_thenCorrect() {
|
||||
get("/teachers")
|
||||
.then()
|
||||
.body("teachers.teacher.find { it.@department == 'science' }.subject",
|
||||
hasItems("math", "physics"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenVerifiesOddPricesAccuratelyByStatus_thenCorrect() {
|
||||
get("/odds").then().body("odds.findAll { it.status > 0 }.price",
|
||||
hasItems(1.30f, 1.20f));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
@@ -0,0 +1,22 @@
|
||||
<employees>
|
||||
<employee category="skilled">
|
||||
<first-name>Jane</first-name>
|
||||
<last-name>Daisy</last-name>
|
||||
<sex>f</sex>
|
||||
</employee>
|
||||
<employee category="unskilled">
|
||||
<first-name>John</first-name>
|
||||
<last-name>Doe</last-name>
|
||||
<sex>m</sex>
|
||||
</employee>
|
||||
<employee category="skilled">
|
||||
<first-name>Billy</first-name>
|
||||
<last-name>Getty</last-name>
|
||||
<sex>m</sex>
|
||||
</employee>
|
||||
<employee category="skilled">
|
||||
<first-name>Hill</first-name>
|
||||
<last-name>Clinton</last-name>
|
||||
<sex>f</sex>
|
||||
</employee>
|
||||
</employees>
|
||||
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"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": 12.2,
|
||||
"name": "1"
|
||||
},
|
||||
{
|
||||
"price": 5.25,
|
||||
"status": 1,
|
||||
"ck": 13.1,
|
||||
"name": "X"
|
||||
},
|
||||
{
|
||||
"price": 2.70,
|
||||
"status": 0,
|
||||
"ck": 12.2,
|
||||
"name": "0"
|
||||
},
|
||||
{
|
||||
"price": 1.20,
|
||||
"status": 2,
|
||||
"ck": 13.1,
|
||||
"name": "2"
|
||||
}
|
||||
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"odds": [{
|
||||
"price": 1.30,
|
||||
"status": 0,
|
||||
"ck": 12.2,
|
||||
"name": "1"
|
||||
},
|
||||
{
|
||||
"price": 5.25,
|
||||
"status": 1,
|
||||
"ck": 13.1,
|
||||
"name": "X"
|
||||
},
|
||||
{
|
||||
"price": 2.70,
|
||||
"status": 0,
|
||||
"ck": 12.2,
|
||||
"name": "0"
|
||||
},
|
||||
{
|
||||
"price": 1.20,
|
||||
"status": 2,
|
||||
"ck": 13.1,
|
||||
"name": "2"
|
||||
}
|
||||
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<teachers>
|
||||
<teacher department="science" id='309'>
|
||||
<subject>math</subject>
|
||||
<subject>physics</subject>
|
||||
</teacher>
|
||||
<teacher department="arts" id='310'>
|
||||
<subject>political education</subject>
|
||||
<subject>english</subject>
|
||||
</teacher>
|
||||
</teachers>
|
||||
Reference in New Issue
Block a user