I was trying to make a post call using Best Buy api playground, I wrote usual code to make post call in rest assured however I am getting 500 error unsupported charset "ISO-8859-1. I am using data provider class to pass json payload to post call
here is the console logs:-
2021-06-18 11:48:15 INFO BestBuy Rest API:29 - ----Create new product request test started----
2021-06-18 11:48:16 INFO BestBuy Rest API:57 - The response for create product
{
"name": "GeneralError",
"message": "unsupported charset \"ISO-8859-1\"",
"code": 500,
"className": "general-error",
"data": {
},
"errors": {
}
}
2021-06-18 11:48:16 INFO BestBuy Rest API:36 - ----Create new product request test completed
===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0
here is the code for post call
public class TC002_BestBuy_CreateProduct extends TestBase{
private static String prodID;
public static String getVariable(){
return prodID;
}
public static void setVariable(String var){
prodID = var;
}
@BeforeClass
public void createProductData(){
logger.info("----Create new product request test started----");
TestBase.initialization();
}
@AfterClass
public void tearDown(){
logger.info("----Create new product request test completed");
}
@Test(priority = 1,dataProvider = "apiDataProvider",dataProviderClass = APIDataProvider.class,description = "post call and status check for create product")
public void createProduct(String name,String type,String price,String shipping,String upc,String description,String manufacturer,String model,String url,String image){
TestBase.response= given()
.baseUri(property.getProperty("BaseURI"))
.queryParam("name",name)
.queryParam("type",type)
.queryParam("price",price)
.queryParam("shipping",shipping)
.queryParam("upc",upc)
.queryParam("description",description)
.queryParam("manufacturer",manufacturer)
.queryParam("model",model)
.queryParam("url",url)
.queryParam("image",image)
.post(property.getProperty("createProductURL"));
logger.info("The response for create product");
TestBase.response.prettyPrint();
String productId=TestBase.response.jsonPath().get("id");
setVariable(productId);
}
}
Here is the code for excel utility class to read excel data
public static int getRowCount(String xlfile,String xlsheet) throws IOException
{
fi=new FileInputStream(xlfile);
wb=new XSSFWorkbook(fi);
ws=wb.getSheet(xlsheet);
int rowcount=ws.getLastRowNum();
wb.close();
fi.close();
return rowcount;
}
public static int getCellCount(String xlfile,String xlsheet,int rownum) throws IOException
{
fi=new FileInputStream(xlfile);
wb=new XSSFWorkbook(fi);
ws=wb.getSheet(xlsheet);
row=ws.getRow(rownum);
int cellcount=row.getLastCellNum();
wb.close();
fi.close();
return cellcount;
}
public static String getcellData(String sheetname, int rowNum, int colNum) {
row = ws.getRow(rowNum);
cell = row.getCell(colNum);
int index = wb.getSheetIndex(sheetname);
if (rowNum <= 0) {
return "";
} else if (index == -1) {
return "";
} else if (cell == null) {
return "";
} else if (row == null) {
return "";
} else if (cell.getCellTypeEnum() == CellType.STRING) {
return cell.getStringCellValue();
} else if (cell.getCellTypeEnum() == CellType.NUMERIC || cell.getCellTypeEnum() == CellType.FORMULA) {
String cellValue = String.valueOf(cell.getNumericCellValue());
if (HSSFDateUtil.isCellDateFormatted(cell)) {
DateFormat df = new SimpleDateFormat("dd/mm/yy");
Date date = cell.getDateCellValue();
cellValue = df.format(date);
}
return cellValue;
} else if (cell.getCellTypeEnum() == CellType.BLANK) {
return "";
} else return String.valueOf(cell.getBooleanCellValue());
}
public static void setCellData(String xlfile,String xlsheet,int rownum,int colnum,String data) throws IOException
{
fi=new FileInputStream(xlfile);
wb=new XSSFWorkbook(fi);
ws=wb.getSheet(xlsheet);
row=ws.getRow(rownum);
cell=row.createCell(colnum);
cell.setCellValue(data);
fo=new FileOutputStream(xlfile);
wb.write(fo);
wb.close();
fi.close();
fo.close();
}
}
here is the data provider class to supply data after reading from excel
public class APIDataProvider {
@DataProvider(name = "apiDataProvider")
public Object[][] getEmpData() throws IOException {
String path=System.getProperty("user.dir")+"/TestResources/TestData.xlsx";
int row=ExcelUtils.getRowCount(path,"Sheet1");
int col=ExcelUtils.getCellCount(path,"Sheet1",1);
String empdata[][]=new String[row][col];
for (int i = 1; i <= row; i++) {
for (int j = 0; j < col; j++) {
empdata[i - 1][j] = ExcelUtils.getcellData("Sheet1", i, j);
}
}
return (empdata);
}
}
I see on swagger that request is POST, not Get, so you should change the how to setup API call: change queryParam --> post body. Read more: https://github.com/rest-assured/rest-assured/wiki/Usage#request-body. Example:
@Test
void demo() {
BestBuyProduct aProduct = new BestBuyProduct();
aProduct.setName("apple");
aProduct.setType("fruit");
aProduct.setPrice(30);
aProduct.setShipping(30);
aProduct.setUpc("DF400");
aProduct.setDescription("test");
aProduct.setManufacturer("XYZ");
aProduct.setModel("AB123");
aProduct.setUrl("http://example");
aProduct.setImage("/apple.img");
given().contentType(ContentType.JSON)
.body(aProduct)
.post("to_your_api_endpoint");
}
To convert from POJO --> json, don't forget to add jackson or gson to classpath.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.3</version>
</dependency>
Btw, instead of using Apache POI directly, you can use https://github.com/ozlerhakan/poiji to convert excel --> POJO. It will help reduce the complex of reading excel file and decrease the number of parameter for test case also.