Skip to content

Commit

Permalink
Add sad path tests for patch api requests (#34)
Browse files Browse the repository at this point in the history
* added sad path scenarios for patch api and updated patch happy path scenario method name, added new testng xml for partial update

* updated testng.xml
  • Loading branch information
mfaisalkhatri authored Oct 5, 2024
1 parent 9257489 commit b934e72
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public void testShouldUpdateTheOrderUsingPut() {
}

@Test
public void testShouldUpdateTheOrderUsingPatch() {
public void testShouldPartialUpdateTheOrderUsingPatch() {

final APIResponse authResponse = this.request.post("/auth", RequestOptions.create().setData(getCredentials()));

Expand All @@ -221,10 +221,7 @@ public void testShouldUpdateTheOrderUsingPatch() {
final APIResponse response = this.request.patch("/partialUpdateOrder/" + orderId, RequestOptions.create()
.setHeader("Authorization", token)
.setData(partialUpdatedOrder));

final Logger logger = new Logger(response);
logger.logResponseDetails();


final JSONObject updateOrderResponseObject = new JSONObject(response.text());
final JSONObject orderObject = updateOrderResponseObject.getJSONObject("order");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,73 @@ public void testShouldNotUpdateOrderWithInvalidToken() {
assertEquals(responseObject.get("message"), "Failed to authenticate token!");
}

@Test
public void testShouldNotPartialUpdateOrder_WhenTokenIsMissing() {

int orderId = 1;

final OrderData partialUpdatedOrder = getPartialUpdatedOrder();

final APIResponse response = this.request.patch("/partialUpdateOrder/" + orderId, RequestOptions.create()
.setData(partialUpdatedOrder));

final JSONObject responseObject = new JSONObject(response.text());

assertEquals(response.status(), 403);
assertEquals(responseObject.get("message"), "Forbidden! Token is missing!");
}

@Test
public void testShouldNotPartialUpdateOrder_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 = getPartialUpdatedOrder();

final int orderId = 90;

final APIResponse response = this.request.patch("/partialUpdateOrder/" + 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 testShouldNotPartialUpdateOrder_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.patch("/partialUpdateOrder/" + orderId, RequestOptions.create()
.setHeader("Authorization", token));

final JSONObject responseObject = new JSONObject(response.text());

assertEquals(response.status(), 400);
assertEquals(responseObject.get("message"), "Invalid request, no data provided to update!");
}

@Test
public void testShouldNotPartialUpdateOrderWithInvalidToken() {
final int orderId = 2;

final APIResponse response = this.request.patch("/partialUpdateOrder/" + 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!");
}
}
14 changes: 14 additions & 0 deletions test-suite/testng-restfulecommerce-partialupdateorder.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Restful ECommerce Test Suite">
<test name="Testing Happy Path Scenarios of Creating and Updating Orders">
<classes>
<class name="io.github.mfaisalkhatri.api.restfulecommerce.HappyPathTests">
<methods>
<include name="testShouldCreateNewOrders"/>
<include name="testShouldPartialUpdateTheOrderUsingPatch"/>
</methods>
</class>
</classes>
</test>
</suite>
6 changes: 5 additions & 1 deletion test-suite/testng-restfulecommerce.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<include name="testShouldNotUpdateOrder_WhenTokenIsMissing"/>
<include name="testShouldNotUpdateOrder_WhenOrderIdIsNotFound"/>
<include name="testShouldNotUpdateOrderWithInvalidToken"/>
<include name="testShouldNotPartialUpdateOrder_WhenTokenIsMissing"/>
<include name="testShouldNotPartialUpdateOrder_WhenOrderIdIsNotFound"/>
<include name="testShouldNotPartialUpdateOrder_WhenOrderDetailsAreNotProvided"/>
<include name="testShouldNotPartialUpdateOrderWithInvalidToken"/>
</methods>
</class>
</classes>
Expand All @@ -31,7 +35,7 @@
<include name="testShouldGetOrdersUsingOrderIdProductIdAndUserId"/>
<include name="testTokenGeneration"/>
<include name="testShouldUpdateTheOrderUsingPut"/>
<include name="testShouldUpdateTheOrderUsingPatch"/>
<include name="testShouldPartialUpdateTheOrderUsingPatch"/>
<include name="testShouldDeleteTheOrder"/>
<include name="testShouldNotRetrieveDeletedOrder"/>
</methods>
Expand Down

0 comments on commit b934e72

Please sign in to comment.