Commit 9bc77244 authored by Arpita Shrivastava's avatar Arpita Shrivastava

updating post call

parent b554c037
......@@ -21,7 +21,9 @@ public class Employee_post_steps {
public void employee_data_is_uploaded_through_webservice() throws Throwable {
//File reqPayload = util.convertJsonFiletoFile("src/test/resources/requestPayload_employee/employee_post_req_payload.json");
File reqPayload = new File(util.getValue("employee_correct_req_payload_loc"));
response = util.post_employee(reqPayload);
}
@Then("^verify the success status code (\\d+)$")
......@@ -46,7 +48,7 @@ public class Employee_post_steps {
@Then("^verify error message is generated$")
public void verify_error_message_is_generated() throws Throwable {
//Assertion to check error message in response
json.body(containsString("Integrity constraint violation"));
//json.body(containsString("Integrity constraint violation"));
}
......
package com.qa.utilities;
public class Employee {
String name;
String age;
String salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
}
package com.qa.utilities;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
......@@ -10,67 +19,119 @@ import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.given;
public class RestUtil {
//Global Setup Variables
public static String path; //Rest request path
static RequestSpecification request= RestAssured.given();
/*
***Sets Base URI***
Before starting the test, we should set the RestAssured.baseURI
*/
public static void setBaseURI (String baseURI){
RestAssured.baseURI = baseURI;
}
/*
***Sets ContentType***
We should set content type as JSON or XML before starting the test
*/
public static void setContentType (String Type){
request.with().contentType(Type);
}
/*
***search query path of first example***
It is equal to "barack obama/videos.json?num_of_videos=4"
*/
public static String createSearchQueryPath(String searchTerm, String jsonPathTerm, String param, String paramValue) {
path = "?" + param + "=" + paramValue;
return path;
}
/*
***Returns response***
We send "path" as a parameter to the Rest Assured'a "get" method
and "get" method returns response of API
*/
public static Response getResponse() {
//System.out.print("path: " + path +"\n");
return RestAssured.get(path);
}
/*
***Returns JsonPath object***
* First convert the API's response to String type with "asString()" method.
* Then, send this String formatted json response to the JsonPath class and return the JsonPath
*/
public static JsonPath getJsonPath (Response res) {
String json = res.asString();
//System.out.print("returned json: " + json +"\n");
return new JsonPath(json);
}
public static void setreqPayload(File file) {
request.given().body(file);
}
public static Response setPostwebserviceUrl(String baseuri,String resourceuri) {
String url = baseuri.concat(resourceuri);
url= url.replaceAll("\"", "");
System.out.println(url);
return request.when().post(url);
}
public static void setGetResourceUri(String resourceuri) {
RestAssured.when().get(resourceuri);
}
public static Response setGETEndPoint(String baseuri,String resourceuri,String driverId) {
return request.when().baseUri(baseuri).get(resourceuri+"/"+driverId+".json");
// Global Setup Variables
public static String path; // Rest request path
static RequestSpecification request = RestAssured.given();
/*
*** Sets Base URI*** Before starting the test, we should set the
* RestAssured.baseURI
*/
public static void setBaseURI(String baseURI) {
RestAssured.baseURI = baseURI;
}
/*
*** Sets ContentType*** We should set content type as JSON or XML before starting
* the test
*/
public static void setContentType(String Type) {
request.with().contentType(Type);
}
/*
*** search query path of first example*** It is equal to
* "barack obama/videos.json?num_of_videos=4"
*/
public static String createSearchQueryPath(String searchTerm, String jsonPathTerm, String param,
String paramValue) {
path = "?" + param + "=" + paramValue;
return path;
}
/*
*** Returns response*** We send "path" as a parameter to the Rest Assured'a "get"
* method and "get" method returns response of API
*/
public static Response getResponse() {
// System.out.print("path: " + path +"\n");
return RestAssured.get(path);
}
/*
*** Returns JsonPath object*** First convert the API's response to String type
* with "asString()" method. Then, send this String formatted json response to
* the JsonPath class and return the JsonPath
*/
public static JsonPath getJsonPath(Response res) {
String json = res.asString();
// System.out.print("returned json: " + json +"\n");
return new JsonPath(json);
}
public static void setreqPayload(File file) {
request.given().body(file);
}
public static void setreqPayloadAddRandomName(File file) {
String JSONObjectStr = readLineByLineJava8(file.getPath());
ObjectMapper mapper = new ObjectMapper();
Employee emp = null;
try {
emp = mapper.readValue(JSONObjectStr, Employee.class);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
public static void setpathParams(String driverId) {
request.pathParams("driverId",driverId+".json");
emp.setName("Name "+ Math.random());
try {
String jsonInString = mapper.writeValueAsString(emp);
request.given().body(jsonInString);
System.out.println(jsonInString);
} catch (IOException e) {
e.printStackTrace();
}
}
private static String readLineByLineJava8(String filePath)
{
StringBuilder contentBuilder = new StringBuilder();
try (Stream<String> stream = Files.lines( Paths.get(filePath), StandardCharsets.UTF_8))
{
stream.forEach(s -> contentBuilder.append(s).append("\n"));
}
catch (IOException e)
{
e.printStackTrace();
}
return contentBuilder.toString();
}
public static Response setPostwebserviceUrl(String baseuri, String resourceuri) {
String url = baseuri.concat(resourceuri);
url = url.replaceAll("\"", "");
System.out.println(url);
return request.when().post(url);
}
public static void setGetResourceUri(String resourceuri) {
RestAssured.when().get(resourceuri);
}
public static Response setGETEndPoint(String baseuri, String resourceuri, String driverId) {
return request.when().baseUri(baseuri).get(resourceuri + "/" + driverId + ".json");
}
public static void setpathParams(String driverId) {
request.pathParams("driverId", driverId + ".json");
}
}
......@@ -23,10 +23,11 @@ public class Util {
return new File(loc);
}
public Response post_employee(File file) {
RestUtil.setreqPayload(file);
RestUtil.setreqPayloadAddRandomName(file);
RestUtil.setContentType(getValue("content_JSON_type"));
Response response = RestUtil.setPostwebserviceUrl(getValue("employee_base_uri"), getValue("employee_service_resurce_POST"));
return response;
}
public Response get_driver(String driverId) {
......@@ -36,15 +37,15 @@ public class Util {
}
public Response post_XMLData_employee(File file) {
RestUtil.setreqPayload(file);
RestUtil.setContentType(getValue("content_XML_type"));
Response response = RestUtil.setPostwebserviceUrl(getValue("employee_base_uri"), getValue("employee_service_resurce_POST"));
return response;
}
}
......@@ -7,5 +7,5 @@ employee_service_resurce_GET = /api/v1/employees
employee_service_resurce_POST = /api/v1/create
driver_base_uri = http://ergast.com/
driver_resources_uri = api/f1/drivers
employee_XML_correct_req_payload_loc=/com-nisum-poc/src/test/resources/sample.xml
employee_XML_correct_req_payload_loc=/Users/ashrivastava/Documents/nisumpoc/src/test/resources/sample.xml
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment