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

updating post call

parent b554c037
...@@ -21,7 +21,9 @@ public class Employee_post_steps { ...@@ -21,7 +21,9 @@ public class Employee_post_steps {
public void employee_data_is_uploaded_through_webservice() throws Throwable { 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 = util.convertJsonFiletoFile("src/test/resources/requestPayload_employee/employee_post_req_payload.json");
File reqPayload = new File(util.getValue("employee_correct_req_payload_loc")); File reqPayload = new File(util.getValue("employee_correct_req_payload_loc"));
response = util.post_employee(reqPayload); response = util.post_employee(reqPayload);
} }
@Then("^verify the success status code (\\d+)$") @Then("^verify the success status code (\\d+)$")
...@@ -46,7 +48,7 @@ public class Employee_post_steps { ...@@ -46,7 +48,7 @@ public class Employee_post_steps {
@Then("^verify error message is generated$") @Then("^verify error message is generated$")
public void verify_error_message_is_generated() throws Throwable { public void verify_error_message_is_generated() throws Throwable {
//Assertion to check error message in response //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; package com.qa.utilities;
import java.io.File; 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.RestAssured;
import io.restassured.http.ContentType; import io.restassured.http.ContentType;
...@@ -10,67 +19,119 @@ import io.restassured.specification.RequestSpecification; ...@@ -10,67 +19,119 @@ import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.given; import static io.restassured.RestAssured.given;
public class RestUtil { public class RestUtil {
//Global Setup Variables // Global Setup Variables
public static String path; //Rest request path public static String path; // Rest request path
static RequestSpecification request= RestAssured.given(); static RequestSpecification request = RestAssured.given();
/* /*
***Sets Base URI*** *** Sets Base URI*** Before starting the test, we should set the
Before starting the test, we should set the RestAssured.baseURI * RestAssured.baseURI
*/ */
public static void setBaseURI (String baseURI){ public static void setBaseURI(String baseURI) {
RestAssured.baseURI = baseURI; RestAssured.baseURI = baseURI;
} }
/* /*
***Sets ContentType*** *** Sets ContentType*** We should set content type as JSON or XML before starting
We should set content type as JSON or XML before starting the test * the test
*/ */
public static void setContentType (String Type){ public static void setContentType(String Type) {
request.with().contentType(Type); request.with().contentType(Type);
} }
/* /*
***search query path of first example*** *** search query path of first example*** It is equal to
It is equal to "barack obama/videos.json?num_of_videos=4" * "barack obama/videos.json?num_of_videos=4"
*/ */
public static String createSearchQueryPath(String searchTerm, String jsonPathTerm, String param, String paramValue) { public static String createSearchQueryPath(String searchTerm, String jsonPathTerm, String param,
String paramValue) {
path = "?" + param + "=" + paramValue; path = "?" + param + "=" + paramValue;
return path; return path;
} }
/* /*
***Returns response*** *** Returns response*** We send "path" as a parameter to the Rest Assured'a "get"
We send "path" as a parameter to the Rest Assured'a "get" method * method and "get" method returns response of API
and "get" method returns response of API
*/ */
public static Response getResponse() { public static Response getResponse() {
//System.out.print("path: " + path +"\n"); // System.out.print("path: " + path +"\n");
return RestAssured.get(path); return RestAssured.get(path);
} }
/* /*
***Returns JsonPath object*** *** Returns JsonPath object*** First convert the API's response to String type
* First convert the API's response to String type with "asString()" method. * with "asString()" method. Then, send this String formatted json response to
* Then, send this String formatted json response to the JsonPath class and return the JsonPath * the JsonPath class and return the JsonPath
*/ */
public static JsonPath getJsonPath (Response res) { public static JsonPath getJsonPath(Response res) {
String json = res.asString(); String json = res.asString();
//System.out.print("returned json: " + json +"\n"); // System.out.print("returned json: " + json +"\n");
return new JsonPath(json); return new JsonPath(json);
} }
public static void setreqPayload(File file) { public static void setreqPayload(File file) {
request.given().body(file); request.given().body(file);
} }
public static Response setPostwebserviceUrl(String baseuri,String resourceuri) {
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();
}
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); String url = baseuri.concat(resourceuri);
url= url.replaceAll("\"", ""); url = url.replaceAll("\"", "");
System.out.println(url); System.out.println(url);
return request.when().post(url); return request.when().post(url);
} }
public static void setGetResourceUri(String resourceuri) { public static void setGetResourceUri(String resourceuri) {
RestAssured.when().get(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 Response setGETEndPoint(String baseuri, String resourceuri, String driverId) {
return request.when().baseUri(baseuri).get(resourceuri + "/" + driverId + ".json");
} }
public static void setpathParams(String driverId) { public static void setpathParams(String driverId) {
request.pathParams("driverId",driverId+".json"); request.pathParams("driverId", driverId + ".json");
} }
} }
...@@ -23,10 +23,11 @@ public class Util { ...@@ -23,10 +23,11 @@ public class Util {
return new File(loc); return new File(loc);
} }
public Response post_employee(File file) { public Response post_employee(File file) {
RestUtil.setreqPayload(file); RestUtil.setreqPayloadAddRandomName(file);
RestUtil.setContentType(getValue("content_JSON_type")); RestUtil.setContentType(getValue("content_JSON_type"));
Response response = RestUtil.setPostwebserviceUrl(getValue("employee_base_uri"), getValue("employee_service_resurce_POST")); Response response = RestUtil.setPostwebserviceUrl(getValue("employee_base_uri"), getValue("employee_service_resurce_POST"));
return response; return response;
} }
public Response get_driver(String driverId) { public Response get_driver(String driverId) {
...@@ -36,15 +37,15 @@ public class Util { ...@@ -36,15 +37,15 @@ public class Util {
} }
public Response post_XMLData_employee(File file) { public Response post_XMLData_employee(File file) {
RestUtil.setreqPayload(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;
}
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 ...@@ -7,5 +7,5 @@ employee_service_resurce_GET = /api/v1/employees
employee_service_resurce_POST = /api/v1/create employee_service_resurce_POST = /api/v1/create
driver_base_uri = http://ergast.com/ driver_base_uri = http://ergast.com/
driver_resources_uri = api/f1/drivers 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