From 84238d730734be3c57a6dae2a2ff268d92bdbab4 Mon Sep 17 00:00:00 2001 From: dadgam3er Date: Thu, 10 Oct 2024 11:03:26 -0400 Subject: [PATCH] dayFive:XML Parsing --- jsonServer/store.json | 34 +++++++ jsonServer/students.json | 22 ++--- .../DayFive/ParsingXMLResTest.java | 20 ++++ .../DayFour/ParsingJSONResDataTest.java | 93 +++++++++++++++++++ .../DayThree/CookiesDemoTest.java | 31 +++++++ .../DayThree/PathandQuerryParTest.java | 16 ++++ testNG.xml | 6 +- 7 files changed, 209 insertions(+), 13 deletions(-) create mode 100644 jsonServer/store.json create mode 100644 src/test/java/zacksolutions/DayFive/ParsingXMLResTest.java create mode 100644 src/test/java/zacksolutions/DayFour/ParsingJSONResDataTest.java create mode 100644 src/test/java/zacksolutions/DayThree/CookiesDemoTest.java create mode 100644 src/test/java/zacksolutions/DayThree/PathandQuerryParTest.java diff --git a/jsonServer/store.json b/jsonServer/store.json new file mode 100644 index 0000000..7ae05d9 --- /dev/null +++ b/jsonServer/store.json @@ -0,0 +1,34 @@ +{ + "store": { + "books": [ + { + "Author": "Harper Lee", + "Category": "Fiction", + "SerialNum": "BK001", + "Price": 10.99, + "Name": "To Kill a Mockingbird" + }, + { + "Author": "George Orwell", + "Category": "Dystopian", + "SerialNum": "BK002", + "Price": 9.99, + "Name": "1984" + }, + { + "Author": "J.R.R. Tolkien", + "Category": "Fantasy", + "SerialNum": "BK003", + "Price": 13.99, + "Name": "The Lord of the Rings" + }, + { + "Author": "F. Scott Fitzgerald", + "Category": "Classic", + "SerialNum": "BK004", + "Price": 8.99, + "Name": "The Great Gatsby" + } + ] + } +} diff --git a/jsonServer/students.json b/jsonServer/students.json index b8a7242..f949ca8 100644 --- a/jsonServer/students.json +++ b/jsonServer/students.json @@ -77,17 +77,6 @@ "Software Architecture" ] }, - { - "id": "9", - "name": "Ian Clark", - "location": "Denver", - "phone": "+1-555-4455", - "courses": [ - "C++", - "Game Development", - "Graphics Programming" - ] - }, { "id": "10", "name": "Jessica Martinez", @@ -99,6 +88,17 @@ "R Programming" ] }, + { + "id": "9", + "courses": [ + "C++", + "Game Development", + "Graphics Programming" + ], + "phone": "+1-555-4455", + "name": "Leena", + "location": "Denver" + }, { "id": "9", "courses": [ diff --git a/src/test/java/zacksolutions/DayFive/ParsingXMLResTest.java b/src/test/java/zacksolutions/DayFive/ParsingXMLResTest.java new file mode 100644 index 0000000..60bb1bd --- /dev/null +++ b/src/test/java/zacksolutions/DayFive/ParsingXMLResTest.java @@ -0,0 +1,20 @@ +package zacksolutions.DayFive; + +import org.testng.annotations.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; + +public class ParsingXMLResTest { + @Test + public void testXMLRes(){ + given() + .when() + .get("https://api.nbp.pl/api/exchangerates/tables/a/") + .then() + .statusCode(200) + .header("Content-Type", "application/json; charset=utf-8") + .log().all(); + + } +} diff --git a/src/test/java/zacksolutions/DayFour/ParsingJSONResDataTest.java b/src/test/java/zacksolutions/DayFour/ParsingJSONResDataTest.java new file mode 100644 index 0000000..20caee6 --- /dev/null +++ b/src/test/java/zacksolutions/DayFour/ParsingJSONResDataTest.java @@ -0,0 +1,93 @@ +package zacksolutions.DayFour; + +import io.restassured.http.ContentType; +import io.restassured.path.json.JsonPath; +import io.restassured.response.Response; +import org.json.JSONObject; +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.util.List; + +import static io.restassured.RestAssured.given; +import static io.restassured.RestAssured.when; +import static org.hamcrest.Matchers.equalTo; + +public class ParsingJSONResDataTest { + + @Test + void JSONResData(){ + given() + .contentType(ContentType.JSON) + .when() + .get("http://localhost:3000/store/") + .then() + .statusCode(200) + .header("Content-Type", "application/json") + .body("books[2].Name", equalTo("The Lord of the Rings")); + } + @Test + void returnBooks(){ + // Create JSON object for the book data + JSONObject data = new JSONObject(); + data.put("Author", "George R. R. Martin"); + data.put("Category", "Fantasy"); + data.put("SerialNum", "D34C"); + data.put("Price", 14.99); + data.put("Name", "A Song Of Ice and Fire"); + + //post the book to the server + given().contentType("text/plain; charset=utf-8") + .body(data.toString()) + .when().post("http://localhost:3000/store/books") + .then().statusCode(201) + .body("Price", equalTo(14.99f)) + .log().all(); + + } + @Test + void getTitleJSONOBJECT(){ + Response res = + given() + .contentType(ContentType.JSON) + .when() + .get("http://localhost:3000/store/"); + JSONObject jsonObject = new JSONObject(res.asString()); + int length = jsonObject.getJSONArray("books").length(); + + boolean status = false; + for (int i = 0; i < length; i++) { + String title = jsonObject.getJSONArray("books").getJSONObject(i).get("Name").toString(); + //System.out.println("Books number " + i + " is " + title); + if (title.equalsIgnoreCase("1984")){ + status = true; + break; + } + } + Assert.assertTrue(status); +/* + List booksTitle = new JsonPath(res.asString()).getList("Name"); + for (String title : booksTitle) { + + System.out.println(title); + } +*/ + } + @Test + void getPrices(){ + Response res = given() + .contentType(ContentType.JSON) + .when().get("http://localhost:3000/store/"); + JSONObject jsonObject = new JSONObject(res.asString()); + int numOfBooks = 0; + for (int i = 0; i < jsonObject.getJSONArray("books").length(); i++) { + String title = jsonObject.getJSONArray("books").getJSONObject(i).get("Name").toString(); + double price = jsonObject.getJSONArray("books").getJSONObject(i).getDouble("Price"); + if (price >10.00){ + System.out.println(title); + numOfBooks ++; + } + } + System.out.println(numOfBooks); + } +} diff --git a/src/test/java/zacksolutions/DayThree/CookiesDemoTest.java b/src/test/java/zacksolutions/DayThree/CookiesDemoTest.java new file mode 100644 index 0000000..bb2a0b8 --- /dev/null +++ b/src/test/java/zacksolutions/DayThree/CookiesDemoTest.java @@ -0,0 +1,31 @@ +package zacksolutions.DayThree; + +import io.restassured.response.Response; +import org.testng.annotations.Test; + +import java.util.Map; + +import static io.restassured.RestAssured.given; +import static io.restassured.RestAssured.when; + +public class CookiesDemoTest { + //@Test + void getCookies(){ + given() + .when().get("https://www.google.com/") + .then().cookie("AEC","").log().all(); + } + @Test(priority = 2) + void getCookiesInfo() { + Response res = given() + .when().get("https://www.google.com/"); + String cookieVal = res.getCookie("AEC"); + System.out.println("The value of AEC cookie is: " + cookieVal); + + Map cookiesValue = res.getCookies(); + for (String key : cookiesValue.keySet()) { + String cookiesVal = res.getCookie(key); + System.out.println( key + " ===> " + cookiesVal); + } + } +} diff --git a/src/test/java/zacksolutions/DayThree/PathandQuerryParTest.java b/src/test/java/zacksolutions/DayThree/PathandQuerryParTest.java new file mode 100644 index 0000000..084405d --- /dev/null +++ b/src/test/java/zacksolutions/DayThree/PathandQuerryParTest.java @@ -0,0 +1,16 @@ +package zacksolutions.DayThree; + +import org.testng.annotations.Test; + +import static io.restassured.RestAssured.given; + +public class PathandQuerryParTest { + @Test + void testQuerryPath(){ + given().pathParams("mypath", "students") + .queryParam("id", 9) + .when() + .get("http://127.0.0.1:3000/{mypath}") + .then().statusCode(200).log().all(); + } +} diff --git a/testNG.xml b/testNG.xml index 5a64e73..5997253 100644 --- a/testNG.xml +++ b/testNG.xml @@ -3,8 +3,10 @@ - - + + + +