DWQA Ask QuestionCategory: APIAPI Automation Assignment for Go Rest Web Site
admin Staff asked 5 years ago

Complete API automation scenarios. I will continue to add more scenarios in the git repositories. Ideally you should complete the same amount of test cases as I have done. Create your own repo in git hub and post the path in the comments.

Refer this link:

https://github.com/akashdktyagi/APIAutomation

 

Pallavi Gadale Staff replied 5 years ago

My repository path of API Testing using GoRest
https://github.com/PallaviTandale/APIGoRest

1 Answers
Bindiya Patil Staff answered 5 years ago

//***API testing for gorest ***//
//***Bindiya Paril****//
package API_GOREST;
import org.json.simple.JSONObject;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import io.restassured.response.Response;
public class TC_01_get {
String baseurl = “https://gorest.co.in/”;
String validToken = “tqgiwz-unv8OWaZXfyNqSzEqIp8nYBUi3Pgo”;
String invalidAccessToken = “InvalidToken”;
@Test
public void tc_01_validate_getcall()
{
Response response=given().baseUri(baseurl).auth().
oauth2(invalidAccessToken).relaxedHTTPSValidation().when().
get(“/public-api/users”).thenReturn();
Reporter.log(“HTTP Status Code: ” + response.getStatusCode());

int statusCode = response.jsonPath().getInt(“_meta.code”);
String meta_message = response.jsonPath().getString(“_meta.message”);
Assert.assertEquals(statusCode,401,”Invalid Token Test case.Status code should come as 401″);
Assert.assertEquals(meta_message,”Authentication failed.”,”Invalid Token Test case. Message should come as Authentication failed.”);
}
@Test()
public void valid_Access_token()
{
Response resp=given().baseUri(baseurl).auth().oauth2(validToken).
relaxedHTTPSValidation().when().
get(“/public-api/users”).thenReturn();
Reporter.log(“HTTP status code:”, +resp.getStatusCode());
int statuscode=resp.jsonPath().getInt(“_meta.code”);
String meta_message = resp.jsonPath().getString(“_meta.message”);
Reporter.log(“HTTP Status Code: ” + resp.getStatusCode() + resp.getBody().asString(),true);
Reporter.log(“Header Info:” + resp.getHeaders().toString(), true);
Assert.assertEquals(statuscode,200,”valid Token Test case.Status code should come as 401″);
Assert.assertEquals(meta_message,”OK. Everything worked as expected.”,”valid Token Test case. Message should come as Everything worked as expected.”);
}
@Test
public void validate_first_name()
{
Response resp=given().baseUri(baseurl).auth().
oauth2(validToken).relaxedHTTPSValidation().
when().get(“/public-api/users”).thenReturn();
Reporter.log(“HTTP status code”,+resp.getStatusCode());
int statuscode= resp.jsonPath().getInt(“_meta.code”);
Assert.assertEquals(statuscode, 200,”Valid Token Test case. Status code should come as 200″);
List<String> list_of_all_first_name=resp.jsonPath().getList(“result.first_name”);
Reporter.log(“list of first name:”+list_of_all_first_name.toString(),true);
}
@Test
public void validate_email_of_user()
{
Response resp=given().baseUri(baseurl).auth().
oauth2(validToken).relaxedHTTPSValidation().
when().get(“/public-api/users”).thenReturn();
Reporter.log(“HTTP status code”,+resp.getStatusCode());
int statuscode= resp.jsonPath().getInt(“_meta.code”);
Assert.assertEquals(statuscode, 200,”Valid Token Test case. Status code should come as 200″);
List<String> list_of_email_user=resp.jsonPath().getList(“result.email”);
Reporter.log(“list of emailid:”+list_of_email_user,true);
}
@Test
public void validate_last_name_of_user()
{
Response resp=given().baseUri(baseurl).auth().
oauth2(validToken).relaxedHTTPSValidation().
when().get(“/public-api/users”).thenReturn();
//System.out.println(resp.asString());
int statuscode= resp.jsonPath().getInt(“_meta.code”);
Assert.assertEquals(statuscode, 200,”Valid Token Test case. Status code should come as 200″);
List<String> list_of_lastname_user=resp.jsonPath().getList(“result.last_name”);
Reporter.log(“list of last name:”+list_of_lastname_user.toString(),true);
}
@Test
public void validate_get_id()
{
Response resp=given().baseUri(baseurl).auth().
oauth2(validToken).relaxedHTTPSValidation().
when().get(“/public-api/users”).thenReturn();
Reporter.log(“HTTP status code”,+resp.getStatusCode());
int statuscode= resp.jsonPath().getInt(“_meta.code”);
Assert.assertEquals(statuscode, 200,”Valid Token Test case. Status code should come as 200″);
List<Integer> id=resp.jsonPath().getList(“result.id”);
Reporter.log(“list of id name:”+id,true);
}
@Test()
public void tc_02_validate_postcall()
{
JSONObject obj=new JSONObject();
String first_name= GetRandomString(5);
String second_name= GetRandomString(4);
String email=first_name+”.”+second_name+”@gmail.com”;
obj.put(“first_name”, first_name);
obj.put(“last_name”, second_name);
obj.put(“email”, email);
obj.put(“gender”, “female”);
Map<String,String> headers = new HashMap<String,String>();
headers.put(“Content-Type”, “application/json”);
//creat a information
String request_body=obj.toJSONString();
Response resp=given().baseUri(baseurl).body(request_body).
headers(headers).auth().
oauth2(validToken).relaxedHTTPSValidation().
when().post(“/public-api/users”).thenReturn();
Reporter.log(resp.getBody().asString(), true);
//validation by statuscode
int statuscode=resp.jsonPath().getInt(“_meta.code”);
//System.out.println(statuscode);
Assert.assertEquals(statuscode, 201,”Status code should come as 201″);
}
@Test
public void creat_info_without_mandatory_field()
{
JSONObject obj=new JSONObject();
String first_name= GetRandomString(8);
String second_name= GetRandomString(7);
String email=first_name+”.”+second_name+”@gmail.com”;
obj.put(“first_name”, first_name);
obj.put(“second_name”, second_name);
//obj.put(“email”, email);
//obj.put(“gender”, “female”);

Map<String,String> headers = new HashMap<String,String>();
headers.put(“Content-Type”, “application/json”);
//creat a information
String request_body=obj.toJSONString();

Response resp=given().baseUri(baseurl).body(request_body).headers(headers).auth().
oauth2(validToken).relaxedHTTPSValidation().
when().post(“/public-api/users”).thenReturn();
Reporter.log(resp.getBody().asString(), true);
//validation by statuscode
int statuscode1=resp.jsonPath().getInt(“_meta.code”);
String meta_message = resp.jsonPath().getString(“_meta.message”);
//System.out.println(statuscode1);
Assert.assertEquals(statuscode1, 422,”Status code should come as 422″);
Assert.assertEquals(meta_message, “Data validation failed. Please check the response body for detailed error messages.”);
}

public String GetRandomString(int n) {
// lower limit for LowerCase Letters
int lowerLimit = 97;
// lower limit for LowerCase Letters
int upperLimit = 122;
Random random = new Random();
// Create a StringBuffer to store the result
StringBuffer r = new StringBuffer(n);
for (int i = 0; i < n; i++) {
// take a random value between 97 and 122
int nextRandomChar = lowerLimit
+ (int)(random.nextFloat()
* (upperLimit – lowerLimit + 1));
// append a character at the end of bs
r.append((char)nextRandomChar);
}
// return the resultant string
return r.toString();
}
}