package com.covidtracking.tests;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import org.testng.Assert;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.Headers;
import io.restassured.path.json.JsonPath;
import io.restassured.response.ResponseBody;
import io.restassured.specification.RequestSpecification;
//blog details : http://total-qa.com
public class Covid19APIPostMethodTests {
@Test(description="TC_001 Validate the response code for covid19 api" )
public void TC_001_validatePostRequestRespCode()
{
//Username:go-corona-admin
//Password:5577YvzU4bK63a1WIQ3Z043H
//AUTHORIZATION: Basic Auth
String userCreds = ("go-corona-admin"+":"+"5577YvzU4bK63a1WIQ3Z043H");
String token = new String(Base64.encodeBase64(userCreds.getBytes()));
System.out.println("Token Value :" + token);
RequestSpecification request = RestAssured.given();
Map<String,Object> headersMap = new LinkedHashMap<String,Object>();
headersMap.put("Content-Type", "application/json");
headersMap.put("AUTHORIZATION", "Basic "+ token);
request.headers(headersMap);
int actualRespCode = request.when().
urlEncodingEnabled(true).
body("{\"Email\":\"test1@covid19api.com\", \"Subscription\":\"basic\"}").
post("https://api.covid19api.com/auth/access_token").getStatusCode();
System.out.println("ResponseCode:: "+ actualRespCode);
int expectedRespCode = 200;
Assert.assertEquals(actualRespCode, expectedRespCode);
}
@Test(description="TC_002 Validate the response body contains Status:active" )
public void TC_002_validatePostRequestRespBody()
{
//Username:go-corona-admin
//Password:5577YvzU4bK63a1WIQ3Z043H
//AUTHORIZATION: Basic Auth
String userCreds = ("go-corona-admin"+":"+"5577YvzU4bK63a1WIQ3Z043H");
String token = new String(Base64.encodeBase64(userCreds.getBytes()));
System.out.println("Token Value :" + token);
RequestSpecification request = RestAssured.given();
Map<String,Object> headersMap = new LinkedHashMap<String,Object>();
headersMap.put("Content-Type", "application/json");
headersMap.put("AUTHORIZATION", "Basic "+ token);
request.headers(headersMap);
ResponseBody respBody = request.when().
urlEncodingEnabled(true).
body("{\"Email\":\"test1@covid19api.com\", \"Subscription\":\"basic\"}").
post("https://api.covid19api.com/auth/access_token").body();
System.out.println("ResponseCode:: "+ respBody.asString());
JsonPath jsonPath = new JsonPath(respBody.asString());
String actual = jsonPath.get("Status");
String expected ="active";
Assert.assertEquals(actual, expected);
}
@Test(description="TC_003 Validate the response Headers contains Content-Length:132")
public void TC_003_validatePostRequestRespHeaders()
{
String userCreds = ("go-corona-admin"+":"+"5577YvzU4bK63a1WIQ3Z043H");
String token = new String(Base64.encodeBase64(userCreds.getBytes()));
System.out.println("Token Value :" + token);
RequestSpecification request = RestAssured.given();
Map<String,Object> headersMap = new LinkedHashMap<String,Object>();
headersMap.put("Content-Type", "application/json");
headersMap.put("AUTHORIZATION", "Basic "+ token);
request.headers(headersMap);
Headers respHeaders = request.when().
urlEncodingEnabled(true).
body("{\"Email\":\"test1@covid19api.com\", \"Subscription\":\"basic\"}").
post("https://api.covid19api.com/auth/access_token").getHeaders();
System.out.println("Response Header Value:: "+ respHeaders.get("Content-Length"));
int actual = Integer.parseInt(respHeaders.get("Content-Length").toString().split("=")[1]);
int expected =132;
Assert.assertEquals(actual, expected);
}
@Test(description="TC_004 Validate the 401 Unauthorized http code in the response")
public void TC_004_validatePostRequest401Unauthorized()
{
RequestSpecification request = RestAssured.given();
Map<String,Object> headersMap = new LinkedHashMap<String,Object>();
headersMap.put("Content-Type", "application/json");
request.headers(headersMap);
int actual = request.when().
urlEncodingEnabled(true).
body("{\"Email\":\"test1@covid19api.com\", \"Subscription\":\"basic\"}").
post("https://api.covid19api.com/auth/access_token").statusCode();
int expected=401;
Assert.assertEquals(actual, expected);
}
}