|
| 1 | +package com.baeldung.restassured; |
| 2 | + |
| 3 | +import static io.restassured.RestAssured.get; |
| 4 | +import static io.restassured.RestAssured.post; |
| 5 | +import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath; |
| 6 | +import static io.restassured.module.jsv.JsonSchemaValidatorSettings.settings; |
| 7 | +import static org.hamcrest.Matchers.equalTo; |
| 8 | +import static org.hamcrest.Matchers.hasItems; |
| 9 | +import static org.hamcrest.Matchers.containsString; |
| 10 | +import io.restassured.module.jsv.JsonSchemaValidator; |
| 11 | +import static org.hamcrest.xml.HasXPath.hasXPath; |
| 12 | + |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +import com.github.fge.jsonschema.SchemaVersion; |
| 16 | +import com.github.fge.jsonschema.cfg.ValidationConfiguration; |
| 17 | +import com.github.fge.jsonschema.main.JsonSchemaFactory; |
| 18 | + |
| 19 | +public class RestAssuredTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + public void givenJsonResponse_whenKeyValuePairMatches_thenCorrect() { |
| 23 | + JsonSchemaFactory factory = JsonSchemaFactory |
| 24 | + .newBuilder() |
| 25 | + .setValidationConfiguration( |
| 26 | + ValidationConfiguration.newBuilder() |
| 27 | + .setDefaultVersion(SchemaVersion.DRAFTV3) |
| 28 | + .freeze()).freeze(); |
| 29 | + JsonSchemaValidator.settings = settings().with() |
| 30 | + .jsonSchemaFactory(factory).and().with() |
| 31 | + .checkedValidation(false); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void givenJsonArrayOfSimilarObjects_whenHasGivenValuesForGivenKey_thenCorrect() { |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void givenUrl_whenSuccessOnGetsResponse_andJsonHasRequiredKV_thenCorrect() { |
| 41 | + |
| 42 | + get("/events?id=390").then().statusCode(200).assertThat() |
| 43 | + .body("data.id", equalTo(390)); |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void givenUrl_whenJsonResponseHasArrayWithGivenValuesUnderKey_thenCorrect() { |
| 49 | + |
| 50 | + get("/events?id=390").then().assertThat() |
| 51 | + .body("odds.price", hasItems("1.30", "5.25")); |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void givenUrl_whenJsonResponseConformsToSchema_thenCorrect() { |
| 57 | + get("/events?id=390").then().assertThat() |
| 58 | + .body(matchesJsonSchemaInClasspath("event_0.json")); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void givenUrl_whenValidatesResponseWithInstanceSettings_thenCorrect() { |
| 63 | + JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory |
| 64 | + .newBuilder() |
| 65 | + .setValidationConfiguration( |
| 66 | + ValidationConfiguration.newBuilder() |
| 67 | + .setDefaultVersion(SchemaVersion.DRAFTV4) |
| 68 | + .freeze()).freeze(); |
| 69 | + |
| 70 | + get("/events?id=390") |
| 71 | + .then() |
| 72 | + .assertThat() |
| 73 | + .body(matchesJsonSchemaInClasspath("event_0.json").using( |
| 74 | + jsonSchemaFactory)); |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void givenUrl_whenValidatesResponseWithStaticSettings_thenCorrect() { |
| 80 | + |
| 81 | + get("/events?id=390") |
| 82 | + .then() |
| 83 | + .assertThat() |
| 84 | + .body(matchesJsonSchemaInClasspath("event_0.json").using( |
| 85 | + settings().with().checkedValidation(false))); |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void givenUrl_whenCheckingFloatValuePasses_thenCorrect() { |
| 91 | + get("/odd").then().assertThat().body("odd.ck", equalTo(12.2f)); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void givenUrl_whenXmlResponseValueTestsEqual_thenCorrect() { |
| 96 | + post("/employees").then().assertThat() |
| 97 | + .body("employees.employee.first-name", equalTo("Jane")); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void givenUrl_whenMultipleXmlValuesTestEqual_thenCorrect() { |
| 102 | + post("/employees").then().assertThat() |
| 103 | + .body("employees.employee.first-name", equalTo("Jane")) |
| 104 | + .body("employees.employee.last-name", equalTo("Daisy")) |
| 105 | + .body("employees.employee.sex", equalTo("f")); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void givenUrl_whenMultipleXmlValuesTestEqualInShortHand_thenCorrect() { |
| 110 | + post("/employees") |
| 111 | + .then() |
| 112 | + .assertThat() |
| 113 | + .body("employees.employee.first-name", equalTo("Jane"), |
| 114 | + "employees.employee.last-name", equalTo("Daisy"), |
| 115 | + "employees.employee.sex", equalTo("f")); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void givenUrl_whenValidatesXmlUsingXpath_thenCorrect() { |
| 120 | + post("/employees") |
| 121 | + .then() |
| 122 | + .assertThat() |
| 123 | + .body(hasXPath("/employees/employee/first-name", |
| 124 | + containsString("Ja"))); |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void givenUrl_whenValidatesXmlUsingXpath2_thenCorrect() { |
| 130 | + post("/employees") |
| 131 | + .then() |
| 132 | + .assertThat() |
| 133 | + .body(hasXPath("/employees/employee/first-name[text()='Jane']")); |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void givenUrl_whenVerifiesScienceTeacherFromXml_thenCorrect() { |
| 139 | + get("/teachers") |
| 140 | + .then() |
| 141 | + .body("teachers.teacher.find { it.@department == 'science' }.subject", |
| 142 | + hasItems("math", "physics")); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void givenUrl_whenVerifiesOddPricesAccuratelyByStatus_thenCorrect() { |
| 147 | + get("/odds").then().body("odds.findAll { it.status > 0 }.price", |
| 148 | + hasItems(1.30f, 1.20f)); |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments