From 290231476541e0ca74f1077aa73c056bef3f5c41 Mon Sep 17 00:00:00 2001 From: dadgam3er Date: Tue, 8 Oct 2024 23:49:13 -0400 Subject: [PATCH] fix: Post data using Hashmap --- .idea/vcs.xml | 6 + jsonServer/students.json | 182 ++++++++++++++++++ pom.xml | 13 ++ ...{HttpRequest.java => HttpRequestTest.java} | 28 +-- .../zacksolutions/DayTwo/PostReqBodyTest.java | 44 +++++ testNG.xml | 11 ++ 6 files changed, 271 insertions(+), 13 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 jsonServer/students.json rename src/test/java/zacksolutions/DayOne/{HttpRequest.java => HttpRequestTest.java} (61%) create mode 100644 src/test/java/zacksolutions/DayTwo/PostReqBodyTest.java diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/jsonServer/students.json b/jsonServer/students.json new file mode 100644 index 0000000..2356bb3 --- /dev/null +++ b/jsonServer/students.json @@ -0,0 +1,182 @@ +{ + "students": [ + { + "id": "1", + "name": "Alice Johnson", + "location": "New York", + "phone": "+1-555-1234", + "courses": [ + "Python", + "JavaScript", + "Data Science" + ] + }, + { + "id": "2", + "name": "Bob Smith", + "location": "Los Angeles", + "phone": "+1-555-5678", + "courses": [ + "Java", + "C++", + "Machine Learning" + ] + }, + { + "id": "3", + "name": "Charlie Williams", + "location": "Chicago", + "phone": "+1-555-9101", + "courses": [ + "Python", + "React", + "Cybersecurity" + ] + }, + { + "id": "4", + "name": "Diana Evans", + "location": "Houston", + "phone": "+1-555-1122", + "courses": [ + "Ruby", + "DevOps", + "Cloud Computing" + ] + }, + { + "id": "5", + "name": "Ethan Davis", + "location": "San Francisco", + "phone": "+1-555-3344", + "courses": [ + "JavaScript", + "Node.js", + "Web Development" + ] + }, + { + "id": "6", + "name": "Fiona Taylor", + "location": "Seattle", + "phone": "+1-555-5566", + "courses": [ + "Python", + "AI", + "Statistics" + ] + }, + { + "id": "7", + "name": "George Harris", + "location": "Austin", + "phone": "+1-555-7788", + "courses": [ + "Java", + "Android Development", + "Kotlin" + ] + }, + { + "id": "8", + "name": "Hannah Brown", + "location": "Miami", + "phone": "+1-555-9900", + "courses": [ + "C#", + ".NET", + "Software Architecture" + ] + }, + { + "id": "9", + "name": "Ian Clark", + "location": "Denver", + "phone": "+1-555-4455", + "courses": [ + "C++", + "Game Development", + "Graphics Programming" + ] + }, + { + "id": "10", + "name": "Jessica Martinez", + "location": "Boston", + "phone": "+1-555-6677", + "courses": [ + "Python", + "Data Analytics", + "R Programming" + ] + }, + { + "id": "a786", + "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": [ + "SeleniumTesting", + "DevOps" + ], + "phone": "+1 2019361028", + "name": "Zakaria", + "location": "Queens" + }, + { + "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" + } + ] +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index b85344b..8901c74 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,10 @@ restAssured http://maven.apache.org + + 8 + 8 UTF-8 @@ -131,4 +134,14 @@ 5.5.0 + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M5 + + + diff --git a/src/test/java/zacksolutions/DayOne/HttpRequest.java b/src/test/java/zacksolutions/DayOne/HttpRequestTest.java similarity index 61% rename from src/test/java/zacksolutions/DayOne/HttpRequest.java rename to src/test/java/zacksolutions/DayOne/HttpRequestTest.java index d66ab3a..d82d0fb 100644 --- a/src/test/java/zacksolutions/DayOne/HttpRequest.java +++ b/src/test/java/zacksolutions/DayOne/HttpRequestTest.java @@ -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 data = new HashMap<>(); + void createUser() { + HashMap 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 data = new HashMap<>(); + @Test(priority = 3, dependsOnMethods = { "createUser" }) + void updateUser() { + + HashMap 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"); } - - } diff --git a/src/test/java/zacksolutions/DayTwo/PostReqBodyTest.java b/src/test/java/zacksolutions/DayTwo/PostReqBodyTest.java new file mode 100644 index 0000000..4221d0f --- /dev/null +++ b/src/test/java/zacksolutions/DayTwo/PostReqBodyTest.java @@ -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 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(); + } + +} diff --git a/testNG.xml b/testNG.xml index e69de29..5a64e73 100644 --- a/testNG.xml +++ b/testNG.xml @@ -0,0 +1,11 @@ + + + + + + + + + + +