|
1 | 1 | package com.baeldung.restassured; |
2 | 2 |
|
| 3 | +import com.github.fge.jsonschema.SchemaVersion; |
| 4 | +import com.github.fge.jsonschema.cfg.ValidationConfiguration; |
| 5 | +import com.github.fge.jsonschema.main.JsonSchemaFactory; |
| 6 | +import com.github.tomakehurst.wiremock.WireMockServer; |
| 7 | +import org.junit.AfterClass; |
| 8 | +import org.junit.BeforeClass; |
| 9 | +import org.junit.Test; |
| 10 | + |
3 | 11 | import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
4 | 12 | import static com.github.tomakehurst.wiremock.client.WireMock.configureFor; |
5 | 13 | import static com.github.tomakehurst.wiremock.client.WireMock.get; |
|
11 | 19 | import static org.hamcrest.Matchers.equalTo; |
12 | 20 | import static org.hamcrest.Matchers.hasItems; |
13 | 21 |
|
14 | | -import org.junit.After; |
15 | | -import org.junit.Before; |
16 | | -import org.junit.Test; |
17 | | - |
18 | | -import com.github.fge.jsonschema.SchemaVersion; |
19 | | -import com.github.fge.jsonschema.cfg.ValidationConfiguration; |
20 | | -import com.github.fge.jsonschema.main.JsonSchemaFactory; |
21 | | -import com.github.tomakehurst.wiremock.WireMockServer; |
22 | | - |
23 | 22 | public class RestAssuredIntegrationTest { |
24 | 23 |
|
25 | | - private WireMockServer wireMockServer = new WireMockServer(); |
26 | | - private static final String EVENTS_PATH = "/events?id=390"; |
27 | | - private static final String APPLICATION_JSON = "application/json"; |
28 | | - private static final String GAME_ODDS = getEventJson(); |
29 | | - |
30 | | - @Before |
31 | | - public void before() throws Exception { |
32 | | - System.out.println("Setting up!"); |
33 | | - wireMockServer.start(); |
34 | | - configureFor("localhost", 8080); |
35 | | - stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn( |
36 | | - aResponse().withStatus(200) |
37 | | - .withHeader("Content-Type", APPLICATION_JSON) |
38 | | - .withBody(GAME_ODDS))); |
39 | | - } |
40 | | - |
41 | | - @Test |
42 | | - public void givenUrl_whenCheckingFloatValuePasses_thenCorrect() { |
43 | | - get("/events?id=390").then().assertThat() |
44 | | - .body("odd.ck", equalTo(12.2f)); |
45 | | - } |
46 | | - |
47 | | - @Test |
48 | | - public void givenUrl_whenSuccessOnGetsResponseAndJsonHasRequiredKV_thenCorrect() { |
49 | | - |
50 | | - get("/events?id=390").then().statusCode(200).assertThat() |
51 | | - .body("id", equalTo("390")); |
52 | | - |
53 | | - } |
54 | | - |
55 | | - @Test |
56 | | - public void givenUrl_whenJsonResponseHasArrayWithGivenValuesUnderKey_thenCorrect() { |
57 | | - get("/events?id=390").then().assertThat() |
58 | | - .body("odds.price", hasItems("1.30", "5.25", "2.70", "1.20")); |
59 | | - } |
60 | | - |
61 | | - @Test |
62 | | - public void givenUrl_whenJsonResponseConformsToSchema_thenCorrect() { |
63 | | - |
64 | | - get("/events?id=390").then().assertThat() |
65 | | - .body(matchesJsonSchemaInClasspath("event_0.json")); |
66 | | - } |
67 | | - |
68 | | - @Test |
69 | | - public void givenUrl_whenValidatesResponseWithInstanceSettings_thenCorrect() { |
70 | | - JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory |
71 | | - .newBuilder() |
72 | | - .setValidationConfiguration( |
73 | | - ValidationConfiguration.newBuilder() |
74 | | - .setDefaultVersion(SchemaVersion.DRAFTV4) |
75 | | - .freeze()).freeze(); |
76 | | - |
77 | | - get("/events?id=390") |
78 | | - .then() |
79 | | - .assertThat() |
80 | | - .body(matchesJsonSchemaInClasspath("event_0.json").using( |
81 | | - jsonSchemaFactory)); |
82 | | - |
83 | | - } |
84 | | - |
85 | | - @Test |
86 | | - public void givenUrl_whenValidatesResponseWithStaticSettings_thenCorrect() { |
87 | | - |
88 | | - get("/events?id=390") |
89 | | - .then() |
90 | | - .assertThat() |
91 | | - .body(matchesJsonSchemaInClasspath("event_0.json").using( |
92 | | - settings().with().checkedValidation(false))); |
93 | | - } |
94 | | - |
95 | | - @After |
96 | | - public void after() throws Exception { |
97 | | - System.out.println("Running: tearDown"); |
98 | | - wireMockServer.stop(); |
99 | | - } |
100 | | - |
101 | | - private static String getEventJson() { |
102 | | - return Util.inputStreamToString(RestAssuredIntegrationTest.class |
103 | | - .getResourceAsStream("/event_0.json")); |
104 | | - } |
105 | | - |
| 24 | + private static WireMockServer wireMockServer = new WireMockServer(); |
| 25 | + private static final String EVENTS_PATH = "/events?id=390"; |
| 26 | + private static final String APPLICATION_JSON = "application/json"; |
| 27 | + private static final String GAME_ODDS = getEventJson(); |
| 28 | + |
| 29 | + @BeforeClass |
| 30 | + public static void before() throws Exception { |
| 31 | + System.out.println("Setting up!"); |
| 32 | + wireMockServer.start(); |
| 33 | + configureFor("localhost", 8080); |
| 34 | + stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn( |
| 35 | + aResponse().withStatus(200) |
| 36 | + .withHeader("Content-Type", APPLICATION_JSON) |
| 37 | + .withBody(GAME_ODDS))); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void givenUrl_whenCheckingFloatValuePasses_thenCorrect() { |
| 42 | + get("/events?id=390").then().assertThat() |
| 43 | + .body("odd.ck", equalTo(12.2f)); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void givenUrl_whenSuccessOnGetsResponseAndJsonHasRequiredKV_thenCorrect() { |
| 48 | + |
| 49 | + get("/events?id=390").then().statusCode(200).assertThat() |
| 50 | + .body("id", equalTo("390")); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void givenUrl_whenJsonResponseHasArrayWithGivenValuesUnderKey_thenCorrect() { |
| 55 | + get("/events?id=390").then().assertThat() |
| 56 | + .body("odds.price", hasItems("1.30", "5.25", "2.70", "1.20")); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void givenUrl_whenJsonResponseConformsToSchema_thenCorrect() { |
| 61 | + |
| 62 | + get("/events?id=390").then().assertThat() |
| 63 | + .body(matchesJsonSchemaInClasspath("event_0.json")); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void givenUrl_whenValidatesResponseWithInstanceSettings_thenCorrect() { |
| 68 | + JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory |
| 69 | + .newBuilder() |
| 70 | + .setValidationConfiguration( |
| 71 | + ValidationConfiguration.newBuilder() |
| 72 | + .setDefaultVersion(SchemaVersion.DRAFTV4) |
| 73 | + .freeze()).freeze(); |
| 74 | + |
| 75 | + get("/events?id=390") |
| 76 | + .then() |
| 77 | + .assertThat() |
| 78 | + .body(matchesJsonSchemaInClasspath("event_0.json").using( |
| 79 | + jsonSchemaFactory)); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void givenUrl_whenValidatesResponseWithStaticSettings_thenCorrect() { |
| 84 | + |
| 85 | + get("/events?id=390") |
| 86 | + .then() |
| 87 | + .assertThat() |
| 88 | + .body(matchesJsonSchemaInClasspath("event_0.json").using( |
| 89 | + settings().with().checkedValidation(false))); |
| 90 | + } |
| 91 | + |
| 92 | + @AfterClass |
| 93 | + public static void after() throws Exception { |
| 94 | + System.out.println("Running: tearDown"); |
| 95 | + wireMockServer.stop(); |
| 96 | + } |
| 97 | + |
| 98 | + private static String getEventJson() { |
| 99 | + return Util.inputStreamToString(RestAssuredIntegrationTest.class |
| 100 | + .getResourceAsStream("/event_0.json")); |
| 101 | + } |
106 | 102 | } |
0 commit comments