|
| 1 | +package restfulecommerce.tutorial; |
| 2 | + |
| 3 | +import static io.restassured.RestAssured.given; |
| 4 | +import static org.hamcrest.core.IsEqual.equalTo; |
| 5 | +import static org.testng.Assert.assertEquals; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Paths; |
| 10 | +import java.util.Objects; |
| 11 | + |
| 12 | +import com.google.gson.Gson; |
| 13 | +import com.google.gson.JsonArray; |
| 14 | +import com.google.gson.JsonObject; |
| 15 | +import io.restassured.path.json.JsonPath; |
| 16 | +import io.restassured.response.Response; |
| 17 | +import org.skyscreamer.jsonassert.JSONAssert; |
| 18 | +import org.testng.annotations.Test; |
| 19 | +import restfulecommerce.tutorial.pojo.OrderResponse; |
| 20 | + |
| 21 | +public class ResponseBodyVerificationTest { |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testResponseWithMatchers () { |
| 25 | + |
| 26 | + given ().when () |
| 27 | + .queryParam ("id", 1) |
| 28 | + .log () |
| 29 | + .all () |
| 30 | + .get ("http://localhost:3004/getOrder") |
| 31 | + .then () |
| 32 | + .log () |
| 33 | + .all () |
| 34 | + .statusCode (200) |
| 35 | + .and () |
| 36 | + .assertThat () |
| 37 | + .body ("message", equalTo ("Order found!!")) |
| 38 | + .body ("orders[0].id", equalTo (1)); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testExtractResponseAndVerify () { |
| 43 | + Response response = given ().when () |
| 44 | + .log () |
| 45 | + .all () |
| 46 | + .get ("http://localhost:3004/getAllOrders") |
| 47 | + .then () |
| 48 | + .log () |
| 49 | + .all () |
| 50 | + .statusCode (200) |
| 51 | + .extract () |
| 52 | + .response (); |
| 53 | + |
| 54 | + String orderMessage = response.jsonPath () |
| 55 | + .getString ("message"); |
| 56 | + assertEquals (orderMessage, "Orders fetched successfully!"); |
| 57 | + String orderTwoUserId = response.jsonPath () |
| 58 | + .getString ("orders[1].user_id"); |
| 59 | + assertEquals (orderTwoUserId, "484"); |
| 60 | + |
| 61 | + JsonPath jsonPath = new JsonPath (response.asString ()); |
| 62 | + assertEquals (jsonPath.getString ("orders[0].product_name"), "Small Marble Shoes"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testExtractResponseWithGson () { |
| 67 | + String response = given ().when () |
| 68 | + .log () |
| 69 | + .all () |
| 70 | + .get ("http://localhost:3004/getAllOrders") |
| 71 | + .then () |
| 72 | + .log () |
| 73 | + .all () |
| 74 | + .statusCode (200) |
| 75 | + .extract () |
| 76 | + .response () |
| 77 | + .asString (); |
| 78 | + |
| 79 | + Gson gson = new Gson (); |
| 80 | + JsonObject responseObject = gson.fromJson (response, JsonObject.class); |
| 81 | + String orderText = responseObject.get ("message") |
| 82 | + .getAsString (); |
| 83 | + assertEquals (orderText, "Orders fetched successfully!"); |
| 84 | + |
| 85 | + JsonArray orderArray = responseObject.getAsJsonArray ("orders"); |
| 86 | + String orderThreeProductName = orderArray.get (2) |
| 87 | + .getAsJsonObject () |
| 88 | + .get ("product_name") |
| 89 | + .getAsString (); |
| 90 | + assertEquals (orderThreeProductName, "Mediocre Aluminum Coat"); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testMultipleResponseConditions () { |
| 95 | + given ().when () |
| 96 | + .log () |
| 97 | + .all () |
| 98 | + .get ("http://localhost:3004/getAllOrders") |
| 99 | + .then () |
| 100 | + .log () |
| 101 | + .all () |
| 102 | + .statusCode (200) |
| 103 | + .and () |
| 104 | + .assertThat () |
| 105 | + .body ("message", equalTo ("Orders fetched successfully!"), "orders[0].user_id", equalTo ("466"), |
| 106 | + "orders[0].product_id", equalTo ("357"), "orders[0].product_name", equalTo ("Small Marble Shoes"), |
| 107 | + "orders[0].product_amount", equalTo (1558)); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testDeserializeResponseWithPojo () { |
| 112 | + OrderResponse orderResponse = given ().when () |
| 113 | + .log () |
| 114 | + .all () |
| 115 | + .get ("http://localhost:3004/getAllOrders") |
| 116 | + .then () |
| 117 | + .log () |
| 118 | + .all () |
| 119 | + .statusCode (200) |
| 120 | + .extract () |
| 121 | + .body () |
| 122 | + .as (OrderResponse.class); |
| 123 | + |
| 124 | + assertEquals (orderResponse.getMessage (), "Orders fetched successfully!"); |
| 125 | + assertEquals (orderResponse.getOrders () |
| 126 | + .get (0) |
| 127 | + .getId (), 1); |
| 128 | + assertEquals (orderResponse.getOrders () |
| 129 | + .get (1) |
| 130 | + .getTaxAmt (), 1228); |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void testResponseFromJsonFile () { |
| 136 | + String actualResponse = given ().when () |
| 137 | + .log () |
| 138 | + .all () |
| 139 | + .get ("http://localhost:3004/getAllOrders") |
| 140 | + .then () |
| 141 | + .log () |
| 142 | + .all () |
| 143 | + .statusCode (200) |
| 144 | + .extract () |
| 145 | + .response () |
| 146 | + .asString (); |
| 147 | + |
| 148 | + String expectedJson = null; |
| 149 | + try { |
| 150 | + expectedJson = Files.readString (Paths.get (Objects.requireNonNull (this.getClass () |
| 151 | + .getClassLoader () |
| 152 | + .getResource ("AllOrders.json")) |
| 153 | + .getPath ())); |
| 154 | + } catch (IOException e) { |
| 155 | + throw new RuntimeException (e); |
| 156 | + } |
| 157 | + |
| 158 | + JSONAssert.assertEquals (expectedJson, actualResponse, true); |
| 159 | + |
| 160 | + } |
| 161 | +} |
0 commit comments