Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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!");
}

}
12 changes: 12 additions & 0 deletions test-suite/testng-restfulecommerce.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<include name="testShouldNotFetchOrder_WhenNoOrderExistsForOrderId"/>
<include name="testShouldNotFetchOrder_WhenNoOrderExistsForUserId"/>
<include name="testShouldNotFetchOrder_WhenNoOrderExistsForProductId"/>
<include name="testShouldNotUpdateOrder_WhenTokenIsMissing"/>
<include name="testShouldNotUpdateOrder_WhenOrderIdIsNotFound"/>
<include name="testShouldNotUpdateOrderWithInvalidToken"/>
</methods>
</class>
</classes>
Expand All @@ -35,4 +38,13 @@
</class>
</classes>
</test>
<test name="Testing Sad Path Scenarios of Restful E-Commerce APIs after orders are created">
<classes>
<class name="io.github.mfaisalkhatri.api.restfulecommerce.SadPathTests">
<methods>
<include name="testShouldNotUpdateOrder_WhenOrderDetailsAreNotProvided"/>
</methods>
</class>
</classes>
</test>
</suite>
Loading