I am trying to do one Rest Assured API E2E flow on an E-commerce web site which includes Logging in, adding a product and then creating an order for that product, however I am unable to execute the last step of actually creating an order for that specific product - It complains of 'Wrong Product Id".
This is the code of my main class
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.path.json.JsonPath;
import io.restassured.specification.RequestSpecification;
import pojo.EcommerceLoginRequest;
import pojo.EcommerceLoginResponse;
import pojo.EcommerceOrderDetail;
import pojo.EcommerceOrders;
import static io.restassured.RestAssured.given;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class ECommerceAPITest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Login
RequestSpecification req = new RequestSpecBuilder().setBaseUri("https://rahulshettyacademy.com").setContentType(ContentType.JSON).build();
EcommerceLoginRequest loginRequest = new EcommerceLoginRequest();
loginRequest.setUserEmail("[email protected]");
loginRequest.setUserPassword("786Immy110.1");
RequestSpecification reqLogin = given().log().all().spec(req).body(loginRequest);
EcommerceLoginResponse loginResponse = reqLogin.when().post("/api/ecom/auth/login").then().log().all().extract().response().as(EcommerceLoginResponse.class);
System.out.println(loginResponse.getToken());
String token = loginResponse.getToken();
System.out.println(loginResponse.getuserId());
String userId = loginResponse.getuserId();
//Add Product
RequestSpecification addProductBaseReq = new RequestSpecBuilder().setBaseUri("https://rahulshettyacademy.com")
.addHeader("authorization", token)
.build();
RequestSpecification reqAddProduct = given().log().all().spec(addProductBaseReq).param("productName", "screenshot")
.param("productAddedBy", userId).param("productCategory", "fashion")
.param("productSubCategory", "shirts").param("productPrice", "11500")
.param("productDescription", "Lenova").param("productFor", "men")
.multiPart("productImage", new File("/Users/imtiyaz/Desktop/Screenshot 2023-08-25 at 00.22.26.png")); //multiPart is used when sending multimedia - files, images, video etc.
String addProductResponse = reqAddProduct.when().post("/api/ecom/product/add-product")
.then().log().all().extract().response().asString();
JsonPath js = new JsonPath (addProductResponse);
String productId = js.get("productId");
//Create order for the Added Product
RequestSpecification createOrderBaseReq = new RequestSpecBuilder().setBaseUri("https://rahulshettyacademy.com")
.addHeader("authorization", token).setContentType(ContentType.JSON)
.build();
EcommerceOrderDetail orderDetail = new EcommerceOrderDetail();
orderDetail.setCountry("India");
orderDetail.setProductOrderId(productId);
List<EcommerceOrderDetail>orderDetailList = new ArrayList<EcommerceOrderDetail>();
orderDetailList.add(orderDetail);
EcommerceOrders orders = new EcommerceOrders();
orders.setOrders(orderDetailList);
RequestSpecification createOrderReq = given().log().all().spec(createOrderBaseReq).body(orders);
String responseAddOrder = createOrderReq.when().post("/api/ecom/order/create-order").then().log().all().extract().response().asString();
System.out.println(responseAddOrder);
}
}
As you can see in the line where I've put 'orderDetail.setProductOrderId(productId);' I am using the same productId variable when putting that as a String object in previous steps.
Below is the code for EcommerceOrderDetail.java class (pojo class):
package pojo;
public class EcommerceOrderDetail {
String country;
String productOrderId;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getProductOrderId() {
return productOrderId;
}
public void setProductOrderId(String productOrderId) {
this.productOrderId = productOrderId;
}
}
Code for 'EcommerceOrders.java' class:
package pojo;
import java.util.List;
public class EcommerceOrders {
private List<EcommerceOrderDetail> orders; //The value of this 'orders' is nothing but list because it is an array inside 'orders' and in that array there is the 'Country' and 'productOrderId'
public List<EcommerceOrderDetail> getOrders() {
return orders;
}
public void setOrders(List<EcommerceOrderDetail> orders) {
this.orders = orders;
}
}