fix: Post data using Hashmap

This commit is contained in:
2024-10-08 23:49:13 -04:00
parent 279c27afcd
commit 2902314765
6 changed files with 271 additions and 13 deletions
@@ -5,13 +5,12 @@ import org.testng.annotations.Test;
import java.util.HashMap;
import static io.restassured.RestAssured.*;
import static io.restassured.matcher.ResponseAwareMatcher.*;
import static org.hamcrest.Matcher.*;
import static org.hamcrest.Matchers.equalTo;
public class HttpRequest {
public class HttpRequestTest {
public int id;
public String job;
@Test(priority = 1)
void getUser() {
when()
@@ -21,29 +20,32 @@ public class HttpRequest {
.body("page", equalTo(2))
.log().all();
}
@Test(priority = 2)
void createUser(){
HashMap<String,String> data = new HashMap<>();
void createUser() {
HashMap<String, String> data = new HashMap<>();
data.put("name", "Zakaria");
data.put("job", "Engineer");
id = given().contentType("Application/json").body(data).when().post("https://reqres.in/api/users").jsonPath().getInt("id");
id = given().contentType("Application/json").body(data).when().post("https://reqres.in/api/users").jsonPath()
.getInt("id");
System.out.println(id);
}
@Test(priority = 3, dependsOnMethods = {"createUser"})
void updateUser(){
HashMap<String,String> data = new HashMap<>();
@Test(priority = 3, dependsOnMethods = { "createUser" })
void updateUser() {
HashMap<String, String> data = new HashMap<>();
data.put("name", "Sami");
data.put("job", "Engineer/Surgeon");
given().contentType("Application/json").body(data).when().put("https://reqres.in/api/users/" + id).then().statusCode(200).log().all();
given().contentType("Application/json").body(data).when().put("https://reqres.in/api/users/" + id).then()
.statusCode(200).log().all();
}
@Test(priority = 4)
void deleteUser(){
void deleteUser() {
when().delete("https://reqres.in/api/users/" + id).then().statusCode(204).log().all();
System.out.println("id number: " + id + " is deleted");
}
}
@@ -0,0 +1,44 @@
package zacksolutions.DayTwo;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.equalTo;
public class PostReqBodyTest {
@Test(priority = 1)
void getStudents() {
String name= when().get("http://127.0.0.1:3000/students/7").jsonPath().getString("name");
List<String> courses = new ArrayList<>();
courses = when().get("http://127.0.0.1:3000/students/7def").jsonPath().getList("courses");
System.out.println("The student name is: " + name + " and skill set is as follow " + courses.get(1));
}
@Test(priority = 2)
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");
data.put("courses", courses);
given().contentType("application/json").body(data)
.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();
}
}