diff --git a/rest-assured-tutorial/.classpath b/rest-assured-tutorial/.classpath new file mode 100644 index 0000000000..8ebf6d9c31 --- /dev/null +++ b/rest-assured-tutorial/.classpath @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/rest-assured-tutorial/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch b/rest-assured-tutorial/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch new file mode 100644 index 0000000000..627021fb96 --- /dev/null +++ b/rest-assured-tutorial/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch @@ -0,0 +1,7 @@ + + + + + + + diff --git a/rest-assured-tutorial/.gitignore b/rest-assured-tutorial/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/rest-assured-tutorial/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/rest-assured-tutorial/.project b/rest-assured-tutorial/.project new file mode 100644 index 0000000000..8333cfc36c --- /dev/null +++ b/rest-assured-tutorial/.project @@ -0,0 +1,36 @@ + + + rest-assured-tutorial + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + org.eclipse.wst.common.project.facet.core.nature + + diff --git a/rest-assured-tutorial/README.md b/rest-assured-tutorial/README.md new file mode 100644 index 0000000000..8b81626967 --- /dev/null +++ b/rest-assured-tutorial/README.md @@ -0,0 +1,7 @@ +========= + +## A Guide To REST-Assured + + +### Relevant Articles: + diff --git a/rest-assured-tutorial/pom.xml b/rest-assured-tutorial/pom.xml new file mode 100644 index 0000000000..12c1fe25ca --- /dev/null +++ b/rest-assured-tutorial/pom.xml @@ -0,0 +1,86 @@ + + 4.0.0 + com.baeldung + rest-assured + 0.1.0-SNAPSHOT + + rest-assured + + + + + + io.rest-assured + rest-assured + 3.0.0 + test + + + io.rest-assured + json-schema-validator + 3.0.0 + + + com.github.fge + json-schema-validator + 2.2.6 + + + com.github.fge + json-schema-core + 1.2.5 + + + + + + + + junit + junit + 4.3 + test + + + + org.hamcrest + hamcrest-all + 1.3 + + + + + + rest-assured + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + + + + + + \ No newline at end of file diff --git a/rest-assured-tutorial/src/test/java/com/baeldung/restassured/RestAssuredTest.java b/rest-assured-tutorial/src/test/java/com/baeldung/restassured/RestAssuredTest.java new file mode 100644 index 0000000000..55c7a6f0d0 --- /dev/null +++ b/rest-assured-tutorial/src/test/java/com/baeldung/restassured/RestAssuredTest.java @@ -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)); + } + +} diff --git a/rest-assured-tutorial/src/test/resources/.gitignore b/rest-assured-tutorial/src/test/resources/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/rest-assured-tutorial/src/test/resources/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/rest-assured-tutorial/src/test/resources/employees.xml b/rest-assured-tutorial/src/test/resources/employees.xml new file mode 100644 index 0000000000..64e382976d --- /dev/null +++ b/rest-assured-tutorial/src/test/resources/employees.xml @@ -0,0 +1,22 @@ + + + Jane + Daisy + f + + + John + Doe + m + + + Billy + Getty + m + + + Hill + Clinton + f + + \ No newline at end of file diff --git a/rest-assured-tutorial/src/test/resources/event0.json b/rest-assured-tutorial/src/test/resources/event0.json new file mode 100644 index 0000000000..77e26c347e --- /dev/null +++ b/rest-assured-tutorial/src/test/resources/event0.json @@ -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" + } + + ] +} \ No newline at end of file diff --git a/rest-assured-tutorial/src/test/resources/odds.json b/rest-assured-tutorial/src/test/resources/odds.json new file mode 100644 index 0000000000..8b3dc166c5 --- /dev/null +++ b/rest-assured-tutorial/src/test/resources/odds.json @@ -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" + } + + ] +} \ No newline at end of file diff --git a/rest-assured-tutorial/src/test/resources/teachers.xml b/rest-assured-tutorial/src/test/resources/teachers.xml new file mode 100644 index 0000000000..9c073d5a38 --- /dev/null +++ b/rest-assured-tutorial/src/test/resources/teachers.xml @@ -0,0 +1,10 @@ + + + math + physics + + + political education + english + + \ No newline at end of file