diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..919ce1f --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/git_toolbox_blame.xml b/.idea/git_toolbox_blame.xml new file mode 100644 index 0000000..7dc1249 --- /dev/null +++ b/.idea/git_toolbox_blame.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/jsonServer/node_modules/.package-lock.json b/jsonServer/node_modules/.package-lock.json new file mode 100644 index 0000000..7e0d413 --- /dev/null +++ b/jsonServer/node_modules/.package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "jsonServer", + "lockfileVersion": 3, + "requires": true, + "packages": {} +} diff --git a/jsonServer/package-lock.json b/jsonServer/package-lock.json new file mode 100644 index 0000000..7e0d413 --- /dev/null +++ b/jsonServer/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "jsonServer", + "lockfileVersion": 3, + "requires": true, + "packages": {} +} diff --git a/jsonServer/package.json b/jsonServer/package.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/jsonServer/package.json @@ -0,0 +1 @@ +{} diff --git a/src/test/java/zacksolutions/DayFour/ParsingJSONResDataTest.java b/src/test/java/zacksolutions/DayFour/ParsingJSONResDataTest.java index 20caee6..1a53f0e 100644 --- a/src/test/java/zacksolutions/DayFour/ParsingJSONResDataTest.java +++ b/src/test/java/zacksolutions/DayFour/ParsingJSONResDataTest.java @@ -15,77 +15,78 @@ import static org.hamcrest.Matchers.equalTo; public class ParsingJSONResDataTest { - @Test - void JSONResData(){ + @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")); + .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/"); + // @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")){ + // 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); - } -*/ + 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; + 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){ + if (price > 10.00) { System.out.println(title); - numOfBooks ++; + numOfBooks++; } } System.out.println(numOfBooks); diff --git a/src/test/java/zacksolutions/DaySix/SchemaValidationTest.java b/src/test/java/zacksolutions/DaySix/SchemaValidationTest.java new file mode 100644 index 0000000..3907959 --- /dev/null +++ b/src/test/java/zacksolutions/DaySix/SchemaValidationTest.java @@ -0,0 +1,32 @@ +package zacksolutions.DaySix; + +import org.testng.annotations.Test; + +import io.restassured.matcher.RestAssuredMatchers; +import io.restassured.module.jsv.JsonSchemaValidator; + +import static io.restassured.RestAssured.given; +import static io.restassured.RestAssured.when; + +/** + * SchemaValidationTest + */ +public class SchemaValidationTest { + + @Test + void JsonSchemaValidation() { + + given() + .when() + .get("http://localhost:3000/store/") + .then().assertThat().body(JsonSchemaValidator.matchesJsonSchemaInClasspath("JsonSchema.json")); + } + + @Test + void XMLSchemaValidation() { + when() + .get("http://localhost:3000/store/") + .then().assertThat().body(RestAssuredMatchers.matchesXsdInClasspath("XMLScheme.xsd")); + } + +} diff --git a/src/test/java/zacksolutions/DaySix/XMLScheme.xml b/src/test/java/zacksolutions/DaySix/XMLScheme.xml new file mode 100644 index 0000000..5d21089 --- /dev/null +++ b/src/test/java/zacksolutions/DaySix/XMLScheme.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/JsonSchema.json b/src/test/resources/JsonSchema.json new file mode 100644 index 0000000..1b73521 --- /dev/null +++ b/src/test/resources/JsonSchema.json @@ -0,0 +1,124 @@ + + +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "properties": { + "books": { + "type": "array", + "items": [ + { + "type": "object", + "properties": { + "Author": { + "type": "string" + }, + "Category": { + "type": "string" + }, + "SerialNum": { + "type": "string" + }, + "Price": { + "type": "number" + }, + "Name": { + "type": "string" + } + }, + "required": [ + "Author", + "Category", + "SerialNum", + "Price", + "Name" + ] + }, + { + "type": "object", + "properties": { + "Author": { + "type": "string" + }, + "Category": { + "type": "string" + }, + "SerialNum": { + "type": "string" + }, + "Price": { + "type": "number" + }, + "Name": { + "type": "string" + } + }, + "required": [ + "Author", + "Category", + "SerialNum", + "Price", + "Name" + ] + }, + { + "type": "object", + "properties": { + "Author": { + "type": "string" + }, + "Category": { + "type": "string" + }, + "SerialNum": { + "type": "string" + }, + "Price": { + "type": "number" + }, + "Name": { + "type": "string" + } + }, + "required": [ + "Author", + "Category", + "SerialNum", + "Price", + "Name" + ] + }, + { + "type": "object", + "properties": { + "Author": { + "type": "string" + }, + "Category": { + "type": "string" + }, + "SerialNum": { + "type": "string" + }, + "Price": { + "type": "number" + }, + "Name": { + "type": "string" + } + }, + "required": [ + "Author", + "Category", + "SerialNum", + "Price", + "Name" + ] + } + ] + } + }, + "required": [ + "books" + ] +} \ No newline at end of file diff --git a/src/test/resources/XMLScheme.xsd b/src/test/resources/XMLScheme.xsd new file mode 100644 index 0000000..2bb573e --- /dev/null +++ b/src/test/resources/XMLScheme.xsd @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file