Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sad path tests for patch api requests #34

Merged
merged 2 commits into from
Oct 5, 2024
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
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
Loading