diff --git a/README.md b/README.md index 0d3b635..61a38c4 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ This project is the outcome of my self-learning about the API Testing Automation - [Lombok](https://projectlombok.org/) has been used to generate Getter and Setters automatically for post body requests. - FAKE Rest APIs on [Reqres.in](https://reqres.in/) has been used for testing. - End to End scenarios have been added for the [restful booker APIs](https://restful-booker.herokuapp.com/apidoc/index.html). +- Happy and Sad Path Scenarios have been added for the [restful-ecommerce APIs](https://github.com/mfaisalkhatri/restful-ecommerce) ## :hammer_and_wrench: Talking more about the Scenarios Covered in this project: You will get the answers to the following questions and its respective working code example with [Playwright Java](https://playwright.dev/java/docs/api-testing#writing-api-test) framework in this repository: @@ -32,10 +33,14 @@ You will get the answers to the following questions and its respective working c - How to use `Lombok` for writing the builder pattern code? - How to use Builder Pattern for test data generation using [Data Faker](https://github.com/datafaker-net/datafaker)? - How to write end-to-end api tests? +- How to write Happy Path scenarios for the APIs? +- How to write Sad Path scenarios for the APIs? ## :writing_hand: Blog Links - [What is API Testing?](https://mfaisalkhatri.github.io/2020/08/08/apitesting/) - [How to perform End to End API Testing using Playwright with Java and TestNG](https://medium.com/@iamfaisalkhatri/how-to-perform-end-to-end-api-testing-using-playwright-with-java-and-testng-26b318927115) +- [Playwright Java API Testing | How to test POST requests?](https://medium.com/@iamfaisalkhatri/playwright-java-api-testing-how-to-test-post-requests-4c9102d3ab03) +- [Playwright Java API Testing | How to test GET requests?](https://medium.com/@iamfaisalkhatri/playwright-java-api-testing-how-to-test-get-requests-c036b984cc6d) ## :question: Need Assistance? diff --git a/src/test/java/io/github/mfaisalkhatri/api/restfulecommerce/SadPathTests.java b/src/test/java/io/github/mfaisalkhatri/api/restfulecommerce/SadPathTests.java index 10e0c96..5d54ce5 100644 --- a/src/test/java/io/github/mfaisalkhatri/api/restfulecommerce/SadPathTests.java +++ b/src/test/java/io/github/mfaisalkhatri/api/restfulecommerce/SadPathTests.java @@ -9,8 +9,8 @@ import java.util.ArrayList; import java.util.List; -import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.OrderDataBuilder.getNewOrder; -import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.OrderDataBuilder.getOrderDataWithMissingProductId; +import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.OrderDataBuilder.*; +import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.TokenBuilder.getCredentials; import static org.testng.Assert.assertEquals; public class SadPathTests extends BaseTest { @@ -103,4 +103,74 @@ public void testShouldNotFetchOrder_WhenNoOrderExistsForProductId() { assertEquals(responseObject.get("message"), "No Order found with the given parameters!"); } + @Test + public void testShouldNotUpdateOrder_WhenTokenIsMissing() { + + int orderId = 1; + + final OrderData updatedOrder = getUpdatedOrder(); + + final APIResponse response = this.request.put("/updateOrder/" + orderId, RequestOptions.create() + .setData(updatedOrder)); + + final JSONObject responseObject = new JSONObject(response.text()); + + assertEquals(response.status(), 403); + assertEquals(responseObject.get("message"), "Forbidden! Token is missing!"); + } + + @Test + public void testShouldNotUpdateOrder_WhenOrderIdIsNotFound() { + final APIResponse authResponse = this.request.post("/auth", RequestOptions.create().setData(getCredentials())); + + final JSONObject authResponseObject = new JSONObject(authResponse.text()); + final String token = authResponseObject.get("token").toString(); + + final OrderData updatedOrder = getUpdatedOrder(); + + final int orderId = 90; + + final APIResponse response = this.request.put("/updateOrder/" + orderId, RequestOptions.create() + .setHeader("Authorization", token) + .setData(updatedOrder)); + + + final JSONObject responseObject = new JSONObject(response.text()); + + assertEquals(response.status(), 404); + assertEquals(responseObject.get("message"), "No Order found with the given Order Id!"); + + } + + @Test + public void testShouldNotUpdateOrder_WhenOrderDetailsAreNotProvided() { + final APIResponse authResponse = this.request.post("/auth", RequestOptions.create().setData(getCredentials())); + + final JSONObject authResponseObject = new JSONObject(authResponse.text()); + final String token = authResponseObject.get("token").toString(); + + final int orderId = 2; + + final APIResponse response = this.request.put("/updateOrder/" + orderId, RequestOptions.create() + .setHeader("Authorization", token)); + + final JSONObject responseObject = new JSONObject(response.text()); + + assertEquals(response.status(), 400); + assertEquals(responseObject.get("message"), "Each Order must have user_id, product_id, product_name, product_amount, qty, tax_amt, and total_amt!"); + } + + @Test + public void testShouldNotUpdateOrderWithInvalidToken() { + final int orderId = 2; + + final APIResponse response = this.request.put("/updateOrder/" + orderId, RequestOptions.create() + .setHeader("Authorization", "token273678")); + + final JSONObject responseObject = new JSONObject(response.text()); + + assertEquals(response.status(), 400); + assertEquals(responseObject.get("message"), "Failed to authenticate token!"); + } + } diff --git a/test-suite/testng-restfulecommerce.xml b/test-suite/testng-restfulecommerce.xml index 23897ca..53be956 100644 --- a/test-suite/testng-restfulecommerce.xml +++ b/test-suite/testng-restfulecommerce.xml @@ -11,6 +11,9 @@ + + + @@ -35,4 +38,13 @@ + + + + + + + + + \ No newline at end of file