forked from Zakaria/RestAssured
fix: xml&jsonValidation
This commit is contained in:
Generated
+7
@@ -0,0 +1,7 @@
|
|||||||
|
<component name="ProjectCodeStyleConfiguration">
|
||||||
|
<code_scheme name="Project" version="173">
|
||||||
|
<ScalaCodeStyleSettings>
|
||||||
|
<option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" />
|
||||||
|
</ScalaCodeStyleSettings>
|
||||||
|
</code_scheme>
|
||||||
|
</component>
|
||||||
Generated
+5
@@ -0,0 +1,5 @@
|
|||||||
|
<component name="ProjectCodeStyleConfiguration">
|
||||||
|
<state>
|
||||||
|
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
|
||||||
|
</state>
|
||||||
|
</component>
|
||||||
Generated
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="GitToolBoxBlameSettings">
|
||||||
|
<option name="version" value="2" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"name": "jsonServer",
|
||||||
|
"lockfileVersion": 3,
|
||||||
|
"requires": true,
|
||||||
|
"packages": {}
|
||||||
|
}
|
||||||
Generated
+6
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"name": "jsonServer",
|
||||||
|
"lockfileVersion": 3,
|
||||||
|
"requires": true,
|
||||||
|
"packages": {}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
{}
|
||||||
@@ -16,7 +16,7 @@ import static org.hamcrest.Matchers.equalTo;
|
|||||||
public class ParsingJSONResDataTest {
|
public class ParsingJSONResDataTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void JSONResData(){
|
void JSONResData() {
|
||||||
given()
|
given()
|
||||||
.contentType(ContentType.JSON)
|
.contentType(ContentType.JSON)
|
||||||
.when()
|
.when()
|
||||||
@@ -26,29 +26,29 @@ public class ParsingJSONResDataTest {
|
|||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
.body("books[2].Name", equalTo("The Lord of the Rings"));
|
.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
|
// @Test
|
||||||
given().contentType("text/plain; charset=utf-8")
|
// void returnBooks(){
|
||||||
.body(data.toString())
|
// // Create JSON object for the book data
|
||||||
.when().post("http://localhost:3000/store/books")
|
// JSONObject data = new JSONObject();
|
||||||
.then().statusCode(201)
|
// data.put("Author", "George R. R. Martin");
|
||||||
.body("Price", equalTo(14.99f))
|
// data.put("Category", "Fantasy");
|
||||||
.log().all();
|
// data.put("SerialNum", "D34C");
|
||||||
|
// data.put("Price", 14.99);
|
||||||
}
|
// data.put("Name", "A Song Of Ice and Fire");
|
||||||
@Test
|
//
|
||||||
void getTitleJSONOBJECT(){
|
// //post the book to the server
|
||||||
Response res =
|
// given().contentType("text/plain; charset=utf-8")
|
||||||
given()
|
// .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)
|
.contentType(ContentType.JSON)
|
||||||
.when()
|
.when()
|
||||||
.get("http://localhost:3000/store/");
|
.get("http://localhost:3000/store/");
|
||||||
@@ -58,23 +58,24 @@ public class ParsingJSONResDataTest {
|
|||||||
boolean status = false;
|
boolean status = false;
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
String title = jsonObject.getJSONArray("books").getJSONObject(i).get("Name").toString();
|
String title = jsonObject.getJSONArray("books").getJSONObject(i).get("Name").toString();
|
||||||
//System.out.println("Books number " + i + " is " + title);
|
// System.out.println("Books number " + i + " is " + title);
|
||||||
if (title.equalsIgnoreCase("1984")){
|
if (title.equalsIgnoreCase("1984")) {
|
||||||
status = true;
|
status = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Assert.assertTrue(status);
|
Assert.assertTrue(status);
|
||||||
/*
|
/*
|
||||||
List<String> booksTitle = new JsonPath(res.asString()).getList("Name");
|
* List<String> booksTitle = new JsonPath(res.asString()).getList("Name");
|
||||||
for (String title : booksTitle) {
|
* for (String title : booksTitle) {
|
||||||
|
*
|
||||||
|
* System.out.println(title);
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
System.out.println(title);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
@Test
|
@Test
|
||||||
void getPrices(){
|
void getPrices() {
|
||||||
Response res = given()
|
Response res = given()
|
||||||
.contentType(ContentType.JSON)
|
.contentType(ContentType.JSON)
|
||||||
.when().get("http://localhost:3000/store/");
|
.when().get("http://localhost:3000/store/");
|
||||||
@@ -83,9 +84,9 @@ public class ParsingJSONResDataTest {
|
|||||||
for (int i = 0; i < jsonObject.getJSONArray("books").length(); i++) {
|
for (int i = 0; i < jsonObject.getJSONArray("books").length(); i++) {
|
||||||
String title = jsonObject.getJSONArray("books").getJSONObject(i).get("Name").toString();
|
String title = jsonObject.getJSONArray("books").getJSONObject(i).get("Name").toString();
|
||||||
double price = jsonObject.getJSONArray("books").getJSONObject(i).getDouble("Price");
|
double price = jsonObject.getJSONArray("books").getJSONObject(i).getDouble("Price");
|
||||||
if (price >10.00){
|
if (price > 10.00) {
|
||||||
System.out.println(title);
|
System.out.println(title);
|
||||||
numOfBooks ++;
|
numOfBooks++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println(numOfBooks);
|
System.out.println(numOfBooks);
|
||||||
|
|||||||
@@ -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"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- Created with Liquid Technologies Online Tools 1.0 (https://www.liquid-technologies.com) -->
|
||||||
|
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
|
||||||
|
<xs:element name="root">
|
||||||
|
<xs:complexType>
|
||||||
|
<xs:sequence>
|
||||||
|
<xs:element maxOccurs="unbounded" name="books">
|
||||||
|
<xs:complexType>
|
||||||
|
<xs:sequence>
|
||||||
|
<xs:element name="Author" type="xs:string" />
|
||||||
|
<xs:element name="Category" type="xs:string" />
|
||||||
|
<xs:element name="SerialNum" type="xs:string" />
|
||||||
|
<xs:element name="Price" type="xs:decimal" />
|
||||||
|
<xs:element name="Name" type="xs:string" />
|
||||||
|
</xs:sequence>
|
||||||
|
</xs:complexType>
|
||||||
|
</xs:element>
|
||||||
|
</xs:sequence>
|
||||||
|
</xs:complexType>
|
||||||
|
</xs:element>
|
||||||
|
</xs:schema>
|
||||||
@@ -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"
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- Created with Liquid Technologies Online Tools 1.0 (https://www.liquid-technologies.com) -->
|
||||||
|
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
|
||||||
|
<xs:element name="root">
|
||||||
|
<xs:complexType>
|
||||||
|
<xs:sequence>
|
||||||
|
<xs:element maxOccurs="unbounded" name="books">
|
||||||
|
<xs:complexType>
|
||||||
|
<xs:sequence>
|
||||||
|
<xs:element name="Author" type="xs:string" />
|
||||||
|
<xs:element name="Category" type="xs:string" />
|
||||||
|
<xs:element name="SerialNum" type="xs:string" />
|
||||||
|
<xs:element name="Price" type="xs:decimal" />
|
||||||
|
<xs:element name="Name" type="xs:string" />
|
||||||
|
</xs:sequence>
|
||||||
|
</xs:complexType>
|
||||||
|
</xs:element>
|
||||||
|
</xs:sequence>
|
||||||
|
</xs:complexType>
|
||||||
|
</xs:element>
|
||||||
|
</xs:schema>
|
||||||
Reference in New Issue
Block a user