|
1 | 1 | package io.github.mfaisalkhatri.api.restfulecommerce;
|
2 | 2 |
|
3 | 3 |
|
| 4 | +import com.microsoft.playwright.APIResponse; |
| 5 | +import com.microsoft.playwright.options.RequestOptions; |
| 6 | +import io.github.mfaisalkhatri.api.restfulecommerce.testdata.OrderData; |
| 7 | +import org.json.JSONArray; |
| 8 | +import org.json.JSONObject; |
| 9 | +import org.testng.annotations.BeforeClass; |
4 | 10 | import org.testng.annotations.Test;
|
5 | 11 |
|
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.OrderDataBuilder.getNewOrder; |
| 16 | +import static org.testng.Assert.assertEquals; |
| 17 | +import static org.testng.Assert.assertNotNull; |
| 18 | + |
6 | 19 | public class APITests extends BaseTest{
|
7 | 20 |
|
| 21 | + |
| 22 | + private OrderData orderData; |
| 23 | + |
| 24 | + @BeforeClass |
| 25 | + public void setupTest() { |
| 26 | + orderData = getNewOrder(); |
| 27 | + } |
8 | 28 | @Test
|
9 | 29 | public void testShouldCreateNewOrders() {
|
10 |
| - //request.post("/addOrder").body() |
| 30 | + |
| 31 | + int totalOrders = 4; |
| 32 | + List<OrderData> orderList = new ArrayList<>(); |
| 33 | + for (int i = 0; i < totalOrders; i++) { |
| 34 | + orderList.add(orderData); |
| 35 | + } |
| 36 | + |
| 37 | + APIResponse response = request.post("/addOrder", RequestOptions.create() |
| 38 | + .setData(orderList)); |
| 39 | + assertEquals(response.status(), 201); |
| 40 | + |
| 41 | + final JSONObject responseObject = new JSONObject(response.text()); |
| 42 | + final JSONArray ordersArray = responseObject.getJSONArray("orders"); |
| 43 | + |
| 44 | + assertEquals(responseObject.get("message"), "Orders added successfully!"); |
| 45 | + assertNotNull(ordersArray.getJSONObject(0).get("id")); |
| 46 | + assertEquals(orderList.get(0).getUserId(), ordersArray.getJSONObject(0).get("user_id")); |
| 47 | + assertEquals(orderList.get(0).getUserId(), ordersArray.getJSONObject(0).get("product_id")); |
11 | 48 | }
|
12 | 49 |
|
| 50 | + @Test |
| 51 | + public void testShouldGetAllOrders() { |
| 52 | + |
| 53 | + |
13 | 54 |
|
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + } |
14 | 59 | }
|
0 commit comments