Last active
August 8, 2019 07:34
-
-
Save anurut/ca052405617f533319fac9b80daa63e3 to your computer and use it in GitHub Desktop.
RestAssured commonAPI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package base; | |
| import java.io.IOException; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| import static io.restassured.RestAssured.given; | |
| import static io.restassured.RestAssured.when; | |
| import io.restassured.http.ContentType; | |
| import io.restassured.response.Response; | |
| import utils.commonUtils; | |
| public class CommonAPI { | |
| private String fileName = "Api.properties"; | |
| //Send API request w.r.t the parameters | |
| public Response sendAPIRequest(String URI, String method, | |
| Map<String, ?> parameter, boolean auth) throws IOException | |
| { | |
| /* | |
| * Parameters : | |
| * URI = The complete URL required to hit an end point on the server | |
| * method = Request method e.g GET / POST | |
| * parameter = An object of type Map which contains POST body | |
| * auth = Whether Authentication required or not | |
| */ | |
| System.out.println("\nURI : " + URI + "\nMethod : " + method + "\nParameter : " + parameter.toString() + | |
| "\nSpecialCase : " + specialCase + "\nAuth : " + auth); | |
| commonUtils.loadPropertyfile(fileName); | |
| System.out.println("\nAuth Token : " + commonUtils.AUTH_TOKEN); | |
| Response response = null; | |
| switch (method) { | |
| case "get": | |
| //getting response for a GET request | |
| System.out.println("\nInside GET method"); | |
| response = | |
| when(). | |
| get(URI). | |
| then(). | |
| assertThat(). | |
| contentType(ContentType.JSON). // check that the content type return from the API is JSON | |
| and(). | |
| statusCode(200). | |
| and(). | |
| extract().response(); // extract the response | |
| break; | |
| case "post": | |
| //getting response for a POST request | |
| if(auth == true) { | |
| System.out.println("\nSetting up POST request"); | |
| System.out.println("\nChecking if Auth token is about to expire or not ..."); | |
| commonUtils.loadPropertyfile(fileName); | |
| String authToken = commonUtils.AUTH_TOKEN; | |
| response = | |
| given(). | |
| contentType(ContentType.JSON). | |
| header("Authorization", "Bearer " + authToken). //Set Auth header | |
| with(). | |
| body(parameter). | |
| when(). | |
| post(URI). | |
| then(). | |
| extract(). | |
| response(); | |
| } else { | |
| response = | |
| given(). | |
| contentType(ContentType.JSON). | |
| with(). | |
| body(parameter). | |
| when(). | |
| post(URI). | |
| then(). | |
| assertThat(). | |
| //statusCode(200). | |
| and(). | |
| extract(). | |
| response(); | |
| } | |
| break; | |
| default: | |
| System.out.println("\nUnsupported API request method"); | |
| response = null; | |
| break; | |
| } | |
| return response; | |
| } | |
| //API call to fetch a new auth token | |
| public Response getAuthToken() throws IOException { | |
| Map<String,Object> param = new HashMap<String,Object>(); | |
| param.put("UserName", "username"); | |
| param.put("Password", "password"); | |
| param.put("grant_type", "password"); | |
| String URL = "https://yourAPIurl/endpoint"; | |
| Response response = | |
| given() | |
| .contentType(ContentType.URLENC) | |
| .with() | |
| .formParams(param) | |
| .when() | |
| .post(URL) | |
| .then() | |
| .assertThat() | |
| .contentType(ContentType.JSON) | |
| .and() | |
| .statusCode(200) | |
| .and() | |
| .extract() | |
| .response(); | |
| String accessToken = response.path("access_token"); | |
| //Write to properties file | |
| commonUtils.saveAuthToken(accessToken, fileName); | |
| System.out.println("\nAuth Token from API call : " + accessToken); | |
| return response; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment