fix: Post Data in 4 differentway

This commit is contained in:
dadgam3er 2024-10-09 11:32:06 -04:00
parent 2902314765
commit ae5c6152ec
6 changed files with 141 additions and 80 deletions

View File

@ -0,0 +1,8 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="ThrowablePrintStackTrace" enabled="true" level="WARNING" enabled_by_default="true">
<scope name="Tests" level="WARNING" enabled="false" />
</inspection_tool>
</profile>
</component>

11
body.json Normal file
View File

@ -0,0 +1,11 @@
{
"id": "9",
"name": "Leena",
"location": "Denver",
"phone": "+1-555-4455",
"courses": [
"C++",
"Game Development",
"Graphics Programming"
]
}

View File

@ -22,17 +22,6 @@
"Machine Learning" "Machine Learning"
] ]
}, },
{
"id": "3",
"name": "Charlie Williams",
"location": "Chicago",
"phone": "+1-555-9101",
"courses": [
"Python",
"React",
"Cybersecurity"
]
},
{ {
"id": "4", "id": "4",
"name": "Diana Evans", "name": "Diana Evans",
@ -111,72 +100,15 @@
] ]
}, },
{ {
"id": "a786", "id": "9",
"courses": "[SeleniumTesting, DevOps]",
"phone": "+1 2019361028",
"name": "Zakaria",
"location": "Queens"
},
{
"id": "49ae",
"courses": "[SeleniumTesting, DevOps]",
"phone": "+1 2019361028",
"name": "Zakaria",
"location": "Queens"
},
{
"id": "dc1f",
"courses": "[SeleniumTesting, DevOps]",
"phone": "+1 2019361028",
"name": "Zakaria",
"location": "Queens"
},
{
"id": "8e58",
"courses": "[SeleniumTesting, DevOps]",
"phone": "+1 2019361028",
"name": "Zakaria",
"location": "Queens"
},
{
"id": "22b2",
"courses": [ "courses": [
"SeleniumTesting", "C++",
"DevOps" "Game Development",
"Graphics Programming"
], ],
"phone": "+1 2019361028", "phone": "+1-555-4455",
"name": "Zakaria", "name": "Leena",
"location": "Queens" "location": "Denver"
},
{
"id": "60b3",
"courses": [
"SeleniumTesting",
"DevOps"
],
"phone": "+1 2019361028",
"name": "Zakaria",
"location": "Queens"
},
{
"id": "54d0",
"courses": [
"SeleniumTesting",
"DevOps"
],
"phone": "+1 2019361028",
"name": "Zakaria",
"location": "Queens"
},
{
"id": "7def",
"courses": [
"SeleniumTesting",
"DevOps"
],
"phone": "+1 2019361028",
"name": "Zakaria",
"location": "Queens"
} }
] ]
} }

View File

@ -114,6 +114,13 @@
<artifactId>json-schema-validator</artifactId> <artifactId>json-schema-validator</artifactId>
<version>5.5.0</version> <version>5.5.0</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20240303</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.scribejava/scribejava-apis --> <!-- https://mvnrepository.com/artifact/com.github.scribejava/scribejava-apis -->
<dependency> <dependency>
<groupId>com.github.scribejava</groupId> <groupId>com.github.scribejava</groupId>

View File

@ -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;
}
}

View File

@ -1,7 +1,12 @@
package zacksolutions.DayTwo; package zacksolutions.DayTwo;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
@ -12,23 +17,25 @@ import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
public class PostReqBodyTest { public class PostReqBodyTest {
@Test(priority = 1) public String id;
//@Test(priority = 1)
void getStudents() { 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<>(); 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)); 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(){ void postStudent(){
//first method to add data using HashMap //first method to add data using HashMap
HashMap data = new HashMap<>(); HashMap data = new HashMap<>();
String[] courses = {"SeleniumTesting", "DevOps"};
data.put("name", "Zakaria"); data.put("name", "Zakaria");
data.put("location", "Queens"); data.put("location", "Queens");
data.put("phone", "+1 2019361028"); data.put("phone", "+1 2019361028");
String[] courses = {"SeleniumTesting", "DevOps"};
data.put("courses", courses); data.put("courses", courses);
given().contentType("application/json").body(data) given().contentType("application/json").body(data)
.when().post("http://127.0.0.1:3000/students/") .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") .body("courses[1]", equalTo("DevOps")).header("Content-Type", "application/json")
.log().all(); .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");
}
} }