Wednesday 6 May 2020

io.restassured

<dependency>

<groupId>io.rest-assured</groupId>

<artifactId>rest-assured</artifactId>

<version>4.3.0</version>

<scope>test</scope>

</dependency>


<!-- https://mvnrepository.com/artifact/org.testng/testng -->

<dependency>

<groupId>org.testng</groupId>

<artifactId>testng</artifactId>

<version>7.1.0</version>

<scope>test</scope>

</dependency>




package eu.restcountries.restautomation;

import org.testng.Assert;
import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.http.Header;
import io.restassured.http.Headers;
import io.restassured.http.Method;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.response.ResponseBody;
import io.restassured.specification.RequestSpecification;

public class NewTest {
String baseUrl = "https://restcountries.eu/rest/v2/name/";

public Response getResponse(String restApi) {
// Specify the base URL to the RESTful web service
RestAssured.baseURI = baseUrl;

// Get the RequestSpecification of the request that you want to sent
// to the server. The server is specified by the BaseURI that we have
// specified in the above step.
RequestSpecification httpRequest = RestAssured.given();

// Make a request to the server by specifying the method Type and the method
// URL.
// This will return the Response from the server. Store the response in a
// variable.
Response response = httpRequest.request(Method.GET, restApi);
return response;
}

@Test
public void TestsPositive() {
Response response = getResponse("/eesti");
// Now let us print the body of the message to see what response
// we have recieved from the server
String responseBody = response.getBody().asString();
System.out.println("Response Body is =>  " + responseBody);

// Get the status code from the Response. In case of
// a successfull interaction with the web service, we
// should get a status code of 200.
int statusCode = response.getStatusCode();

// Assert that correct status code is returned.
Assert.assertEquals(statusCode, 200, "Correct status code returned");

// Get all the headers. Return value is of type Headers.
// Headers class implements Iterable interface, hence we
// can apply an advance for loop to go through all Headers
// as shown in the code below
Headers allHeaders = response.headers();

// Iterate over all the Headers
for (Header header : allHeaders) {
System.out.println("Key: " + header.getName() + " Value: " + header.getValue());
}

// Reader header of a give name. In this line we will get
// Header named Content-Type
String contentType = response.header("Content-Type");
Assert.assertEquals(contentType, "application/json;charset=utf-8");

// Reader header of a give name. In this line we will get
// Header named Content-Encoding
String contentEncoding = response.header("Content-Encoding");
Assert.assertEquals(contentEncoding, "gzip");

// Retrieve the body of the Response
ResponseBody body = response.getBody();

// To check for sub string presence get the Response body as a String.
// Do a String.contains
String bodyAsString = body.asString();

// By using the ResponseBody.asString() method, we can convert the body
// into the string representation.
System.out.println("Response Body is: " + body.asString());

// convert the body into lower case and then do a comparison to ignore casing.
Assert.assertEquals(bodyAsString.contains("Estonian"), true, "Response body contains Estonian");

// First get the JsonPath object instance from the Response interface
JsonPath jsonPathEvaluator = response.jsonPath();

// Then simply query the JsonPath object to get a String value of the node
// specified by JsonPath: City (Note: You should not put $. in the Java code)
String city = jsonPathEvaluator.get("[0].demonym");

// Let us print the city variable to see what we got
System.out.println("City name received from Response " + city);

// Validate the response
Assert.assertEquals(city, "Estonian", "Correct city name received in the Response");
}

@Test
public void TestsNegative() {
Response response = getResponse("/wrongInput");
// Now let us print the body of the message to see what response
// we have recieved from the server
String responseBody = response.getBody().asString();
System.out.println("Response Body is =>  " + responseBody);

// Get the status code from the Response. In case of
// a successfull interaction with the web service, we
// should get a status code of 200.
int statusCode = response.getStatusCode();
// Assert that correct status code is returned.
Assert.assertEquals(statusCode, 404, "Correct status code returned");
}
}