fix: Post Data in 4 differentway

This commit is contained in:
2024-10-09 11:32:06 -04:00
parent 2902314765
commit ae5c6152ec
6 changed files with 141 additions and 80 deletions
@@ -0,0 +1,40 @@
package zacksolutions.DayTwo;
public class Pojo_PostReq {
String name;
String location;
String phone;
String [] courses;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String[] getCourses() {
return courses;
}
public void setCourses(String[] courses) {
this.courses = courses;
}
}
@@ -1,7 +1,12 @@
package zacksolutions.DayTwo;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.testng.annotations.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@@ -12,23 +17,25 @@ import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.equalTo;
public class PostReqBodyTest {
@Test(priority = 1)
public String id;
//@Test(priority = 1)
void getStudents() {
String name= when().get("http://127.0.0.1:3000/students/7").jsonPath().getString("name");
String name= when().get("http://127.0.0.1:3000/students/54d0").jsonPath().getString("name");
List<String> courses = new ArrayList<>();
courses = when().get("http://127.0.0.1:3000/students/7def").jsonPath().getList("courses");
courses = when().get("http://127.0.0.1:3000/students/54d0").jsonPath().getList("courses");
System.out.println("The student name is: " + name + " and skill set is as follow " + courses.get(1));
}
@Test(priority = 2)
/*
@Test(priority = 1)
void postStudent(){
//first method to add data using HashMap
HashMap data = new HashMap<>();
String[] courses = {"SeleniumTesting", "DevOps"};
data.put("name", "Zakaria");
data.put("location", "Queens");
data.put("phone", "+1 2019361028");
String[] courses = {"SeleniumTesting", "DevOps"};
data.put("courses", courses);
given().contentType("application/json").body(data)
.when().post("http://127.0.0.1:3000/students/")
@@ -40,5 +47,61 @@ public class PostReqBodyTest {
.body("courses[1]", equalTo("DevOps")).header("Content-Type", "application/json")
.log().all();
}
@Test(priority = 2)
void DeleteStudent(){
when().delete("http://127.0.0.1:3000/students/3");
}
*/
/*
@Test(priority = 1)
void postStudentORGLibrary(){
//first method to add data using HashMap
JSONObject data = new JSONObject();
data.put("name", "Zakaria");
data.put("location", "Queens");
data.put("phone", "+1 2019361028");
String[] courses = {"SeleniumTesting", "DevOps"};
data.put("courses", courses);
given().contentType("application/json").body(data.toString())
.when().post("http://127.0.0.1:3000/students/")
.then().statusCode(201)
.body("name", equalTo("Zakaria"))
.body("location",equalTo("Queens"))
.body("phone", equalTo("+1 2019361028"))
.body("courses[0]", equalTo("SeleniumTesting"))
.body("courses[1]", equalTo("DevOps")).header("Content-Type", "application/json")
.log().all();
}
@Test(priority = 2)
void DeleteStudent(){
when().delete("http://127.0.0.1:3000/students/3");
}
*/
@Test(priority = 1)
void postStudentJsonFile() throws FileNotFoundException {
//first method to add data using HashMap
FileReader fr = new FileReader(new File("body.json"));
JSONTokener jt = new JSONTokener(fr);
JSONObject data = new JSONObject(jt);
given().contentType("application/json").body(data.toString())
.when().post("http://127.0.0.1:3000/students/")
.then().statusCode(201)
.body("name", equalTo("Leena"))
.body("location", equalTo("Denver"))
.body("phone", equalTo("+1-555-4455"))
.body("courses[0]", equalTo("C++"))
.body("courses[1]", equalTo("Game Development"))
.body("courses[2]", equalTo("Graphics Programming")).header("Content-Type", "application/json")
.log().all();
}
@Test(priority = 2)
void DeleteStudent(){
when().delete("http://127.0.0.1:3000/students/3");
}
}