Skip to content

Commit

Permalink
added token generation and update order tests, modified the assertion…
Browse files Browse the repository at this point in the history
…s for get order tests
  • Loading branch information
mfaisalkhatri committed Sep 18, 2024
1 parent c328322 commit 4003b6d
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.List;

import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.OrderDataBuilder.getNewOrder;
import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.OrderDataBuilder.getUpdatedOrder;
import static io.github.mfaisalkhatri.api.restfulecommerce.testdata.TokenBuilder.getCredentials;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;

Expand Down Expand Up @@ -70,7 +72,7 @@ public void testShouldGetAllOrders() {
}

@Test
public void testShouldGetOrderUsingId() {
public void testShouldGetOrderUsingOrderId() {
int orderId = 1;
APIResponse response = request.get("/getOrder", RequestOptions.create().setQueryParam("id", orderId));

Expand All @@ -88,7 +90,7 @@ public void testShouldGetOrderUsingId() {

@Test
public void testShouldGetOrdersUsingUserId() {
int userId = 2;
String userId = "2";

APIResponse response = request.get("/getOrder", RequestOptions.create().setQueryParam("user_id", userId));

Expand All @@ -100,8 +102,73 @@ public void testShouldGetOrdersUsingUserId() {

assertEquals(response.status(), 200);
assertEquals(responseObject.get("message"), "Order found!!");
assertEquals(orderList.get(0).getUserId(), ordersArray.getJSONObject(0).get("user_id"));
assertEquals(ordersArray.getJSONObject(0).get("user_id"), userId);

}

@Test
public void testShouldGetOrdersUsingProductId() {
String productId = "332";


APIResponse response = request.get("/getOrder", RequestOptions.create().setQueryParam("product_id", productId));

Helper helper = new Helper(response);
helper.logResponseDetails();

final JSONObject responseObject = new JSONObject(response.text());
final JSONArray ordersArray = responseObject.getJSONArray("orders");

assertEquals(response.status(), 200);
assertEquals(responseObject.get("message"), "Order found!!");
assertEquals(ordersArray.getJSONObject(0).get("product_id"), productId);

}

@Test
public void testTokenGeneration() {
APIResponse response = request.post("/auth", RequestOptions.create().setData(getCredentials()));

Helper helper = new Helper(response);
helper.logResponseDetails();

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

assertEquals(response.status(), 201);
assertNotNull(responseObject.get("token"));
assertEquals(responseObject.get("message"), "Authentication Successful!");
}

@Test
public void testShouldUpdateTheOrderUsingPut() {

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

final JSONObject authResponseObject = new JSONObject(authResponse.text());
String token = authResponseObject.get("token").toString();

OrderData updatedOrder = getUpdatedOrder();

int orderId = 2;
APIResponse response = request.put("/updateOrder/" + orderId, RequestOptions.create()
.setHeader("Authorization", token)
.setData(updatedOrder));

Helper helper = new Helper(response);
helper.logResponseDetails();

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

assertEquals(response.status(), 200);
assertEquals(updateOrderResponseObject.get("message"), "Order updated successfully!");
assertEquals(orderId, orderObject.get("id"));
assertEquals(updatedOrder.getUserId(), orderObject.get("user_id"));
assertEquals(updatedOrder.getProductId(), orderObject.get("product_id"));
assertEquals(updatedOrder.getProductName(), orderObject.get("product_name"));
assertEquals(updatedOrder.getProductAmount(), orderObject.get("product_amount"));
assertEquals(updatedOrder.getTotalAmt(), orderObject.get("total_amt"));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,26 @@ public static OrderData getNewOrder() {
.totalAmt(totalAmount)
.build();
}

public static OrderData getUpdatedOrder() {
int userId = FAKER.number().numberBetween(4, 5);
int productId = FAKER.number().numberBetween(335,337);
int productAmount = FAKER.number().numberBetween(510, 515);
int quantity = FAKER.number().numberBetween(1, 2);
int taxAmount = FAKER.number().numberBetween(35,45);
int totalAmount = (productAmount*quantity)+taxAmount;


return OrderData.builder()
.userId(String.valueOf(userId))
.productId(String.valueOf(productId))
.productName(FAKER.commerce().productName())
.productAmount(productAmount)
.qty(quantity)
.taxAmt(taxAmount)
.totalAmt(totalAmount)
.build();


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.mfaisalkhatri.api.restfulecommerce.testdata;

public class TokenBuilder {

public static TokenData getCredentials() {
return TokenData.builder().username("admin")
.password("secretPass123")
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.mfaisalkhatri.api.restfulecommerce.testdata;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class TokenData {

private String username;
private String password;

}
19 changes: 19 additions & 0 deletions test-suite/testng-restfulecommerce.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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 Each API of ECommerce App separately">
<classes>
<class name="io.github.mfaisalkhatri.api.restfulecommerce.APITests">
<methods>
<include name="testShouldCreateNewOrders"/>
<include name="testShouldGetAllOrders"/>
<include name="testShouldGetOrderUsingOrderId"/>
<include name="testShouldGetOrdersUsingUserId"/>
<include name="testShouldGetOrdersUsingProductId"/>
<include name="testTokenGeneration"/>
<include name="testShouldUpdateTheOrderUsingPut"/>
</methods>
</class>
</classes>
</test>
</suite>

0 comments on commit 4003b6d

Please sign in to comment.