8000 Adding response validation types · venkata-qa/rest-assured-tutorial@695ce00 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 695ce00

Browse files
committed
Adding response validation types
1 parent 8f2c7cc commit 695ce00

File tree

10 files changed

+244
-23
lines changed

10 files changed

+244
-23
lines changed

pom.xml

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@
2424
<artifactId>spring-boot-devtools</artifactId>
2525
<scope>runtime</scope>
2626
</dependency>
27-
<dependency>
28-
<groupId>org.springframework.boot</groupId>
29-
<artifactId>spring-boot-starter-test</artifactId>
30-
<scope>test</scope>
31-
</dependency>
3227

3328
<dependency>
3429
<groupId>org.modelmapper</groupId>
@@ -39,45 +34,42 @@
3934
<groupId>org.springframework.boot</groupId>
4035
<artifactId>spring-boot-starter-data-jpa</artifactId>
4136
</dependency>
42-
4337
<dependency>
4438
<groupId>com.h2database</groupId>
4539
<artifactId>h2</artifactId>
4640
<scope>runtime</scope>
4741
</dependency>
48-
49-
<!--<dependency>
50-
<groupId>org.springframework.boot</groupId>
51-
<artifactId>spring-boot-starter-security</artifactId>
52-
</dependency>
5342
<dependency>
54-
<groupId>org.springframework.security</groupId>
55-
<artifactId>spring-security-jwt</artifactId>
56-
<version>1.0.7.RELEASE</version>
43+
<groupId>org.apache.commons</groupId>
44+
<artifactId>commons-lang3</artifactId>
45+
<version>3.8.1</version>
5746
</dependency>
5847
<dependency>
59-
<groupId>org.springframework.security.oauth</groupId>
60-
<artifactId>spring-security-oauth2</artifactId>
61-
<version>2.1.0.RELEASE</version>
62-
</dependency>-->
48+
<groupId>com.fasterxml.jackson.dataformat</groupId>
49+
<artifactId>jackson-dataformat-xml</artifactId>
50+
</dependency>
51+
6352

6453
<dependency>
65-
<groupId>io.rest-assured</groupId>
66-
<artifactId>rest-assured</artifactId>
67-
<version>4.3.0</version>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-starter-test</artifactId>
6856
<scope>test</scope>
6957
</dependency>
7058
<dependency>
7159
<groupId>org.testng</groupId>
7260
<artifactId>testng</artifactId>
7361
<version>7.1.0</version>
62+
</dependency>
63+
<dependency>
64+
<groupId>io.rest-assured</groupId>
65+
<artifactId>rest-assured</artifactId>
66+
<version>3.0.7</version>
7467
<scope>test</scope>
7568
</dependency>
7669
<dependency>
7770
<groupId>io.rest-assured</groupId>
7871
<artifactId>json-schema-validator</artifactId>
7972
<version>3.3.0</version>
80-
<scope>test</scope>
8173
</dependency>
8274
</dependencies>
8375

src/main/java/com/onlyfullstack/springrestexample/datatransferobject/EmployeeDTO.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import javax.validation.constraints.Email;
44
import javax.validation.constraints.NotNull;
55
import javax.validation.constraints.Positive;
6+
import javax.xml.bind.annotation.XmlRootElement;
67

8+
@XmlRootElement
79
public class EmployeeDTO {
810

911
private Long id;

src/test/java/onlyfullstack/httpMethods/GetHttpMethodExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,6 @@ public void getWithIdMethodWithBDDApproach() {
7979
public void getMethodValidateJsonSchema() {
8080
RestAssured.given().baseUri("http://localhost:8088/employees")
8181
.get().then().statusCode(200)
82-
.body(JsonSchemaValidator.matchesJsonSchemaInClasspath("employee_valid_response.json"));
82+
.body(JsonSchemaValidator.matchesJsonSchemaInClasspath("get-all-employee-json-schema.json"));
8383
}
8484
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package onlyfullstack.verifyResponse;
2+
3+
import io.restassured.path.json.JsonPath;
4+
import org.testng.annotations.BeforeClass;
5+
import org.testng.annotations.Test;
6+
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Paths;
10+
11+
public class JsonPathExample {
12+
13+
String exampleJson;
14+
JsonPath jsonPath;
15+
16+
@BeforeClass
17+
public void loadJsonFile() throws IOException {
18+
// Read the json from file and store in a String
19+
exampleJson = new String(Files.readAllBytes(Paths.get("src/test/resources/json-path-example.json")));
20+
// Create a JsonPath from the input String
21+
jsonPath = new JsonPath(exampleJson);
22+
}
23+
24+
@Test
25+
public void jsonPathExamples() {
26+
System.out.println("Nested Value of store.bicycle - \n" + jsonPath.get("store.bicycle"));
27+
System.out.println("\nAccessing the Array Element - store.book[0]- \n" + jsonPath.get("store.book[0]"));
28+
System.out.println("\nAccessing the inner property of an array Element - store.book[0]- \n" + jsonPath.get("store.book[0].author"));
29+
}
30+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package onlyfullstack.verifyResponse;
2+
3+
import io.restassured.RestAssured;
4+
import io.restassured.module.jsv.JsonSchemaValidator;
5+
import onlyfullstack.models.Employee;
6+
import org.hamcrest.Matchers;
7+
import org.json.JSONException;
8+
import org.skyscreamer.jsonassert.JSONAssert;
9+
import org.testng.Assert;
10+
import org.testng.annotations.Test;
11+
12+
import java.io.IOException;
13+
import java.nio.file.Files;
14+
import java.nio.file.Paths;
15+
16+
public class ResponseValidatorTypes {
17+
18+
@Test
19+
public void validateWithJsonPath() {
20+
String requestEmployeeId = "2";
21+
RestAssured.given() //precondition is to give the base uri
22+
.baseUri("http://localhost:8088/")
23+
.pathParam("employee_id", requestEmployeeId)
24+
.when() //actual action is to perform the get request
25+
.get("employees/{employee_id}")
26+
.then() // will return the ValidatadleResponse from the Response object
27+
.statusCode(200)
28+
.header("Content-Type", "application/json;charset=UTF-8")
29+
.body("id", Matchers.equalTo(2)) // json path of a simple object
30+
.body("firstName", Matchers.equalTo("only"))
31+
.body("lastName", Matchers.equalTo("fullstack"))
32+
.body("salary", Matchers.equalTo(2000))
33+
.body("email", Matchers.equalTo("onlyfullstack@abc.com"));
34+
}
35+
36+
@Test
37+
public void validateWithExtractedObject() {
38+
String requestEmployeeId = "2";
39+
Employee expectedResponse = new Employee(2l, "only", "fullstack", 2000l, "onlyfullstack@abc.com");
40+
41+
Employee actualResponse = RestAssured.given() //precondition is to give the base uri
42+
.baseUri("http://localhost:8088/")
43+
.pathParam("employee_id", requestEmployeeId)
44+
.when() //actual action is to perform the get request
45+
.get("employees/{employee_id}")
46+
.then() // will return the ValidatadleResponse from the Response object
47+
.statusCode(200)
48+
.extract()
49+
.as(Employee.class);
50+
51+
Assert.assertEquals(actualResponse, expectedResponse);
52+
}
53+
54+
@Test
55+
public void validateWithJsonString() throws JSONException {
56+
String requestEmployeeId = "2";
57+
String expectedJson = "{\"id\":2,\"firstName\":\"only\",\"lastName\":\"fullstack\",\"salary\":2000,\"email\":\"onlyfullstack@abc.com\"}";
58+
String response = RestAssured.given() //precondition is to give the base uri
59+
.baseUri("http://localhost:8088/")
60+
.pathParam("employee_id", requestEmployeeId)
61+
.when() //actual action is to perform the get request
62+
.get("employees/{employee_id}")
63+
.asString();
64+
65+
System.out.println(response);
66+
JSONAssert.assertEquals(expectedJson, response, true);
67+
}
68+
69+
@Test
70+
public void validateWithJsonFromFile() throws IOException, JSONException {
71+
String requestEmployeeId = "2";
72+
String expectedJsonResponse = new String(Files.readAllBytes(Paths.get("src/test/resources/single-employee.json")));
73+
String actualJsonResponse = RestAssured.given() //precondition is to give the base uri
74+
.baseUri("http://localhost:8088/")
75+
.pathParam("employee_id", requestEmployeeId)
76+
.when() //actual action is to perform the get request
77+
.get("employees/{employee_id}")
78+
.asString();
79+
80+
System.out.println(actualJsonResponse);
81+
JSONAssert.assertEquals(expectedJsonResponse, actualJsonResponse, true);
82+
}
83+
84+
@Test
85+
public void validateJsonSchema() {
86+
RestAssured.given()
87+
.baseUri("http://localhost:8088")
88+
.get("/employees")
89+
.then()
90+
.statusCode(200)
91+
.body(JsonSchemaValidator.matchesJsonSchemaInClasspath("get-all-employee-json-schema.json"));
92+
}
93+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package onlyfullstack.verifyResponse;
2+
3+
import io.restassured.path.xml.XmlPath;
4+
import org.testng.annotations.BeforeClass;
5+
import org.testng.annotations.Test;
6+
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Paths;
10+
11+
public class XmlPathExample {
12+
13+
String exampleXml;
14+
XmlPath xmlPath;
15+
16+
@BeforeClass
17+
public void loadJsonFile() throws IOException {
18+
exampleXml = new String(Files.readAllBytes(Paths.get("src/test/resources/xml-path-example.xml")));
19+
xmlPath = new XmlPath(exampleXml);
20+
}
21+
22+
@Test
23+
public void xmlPathExamples() {
24+
System.out.println("Nested Value of store.bicycle - \n" + xmlPath.get("store.bicycle"));
25+
System.out.println("\nAccessing the Array Element - store.book[0]- \n" + xmlPath.get("store.book[0]"));
26+
System.out.println("\nAccessing the inner property of an array Element - store.book[0]- \n" + xmlPath.get("store.book[0].author"));
27+
}
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"store": {
3+
"book": [
4+
{
5+
"category": "reference",
6+
"author": "Nigel Rees",
7+
"title": "Sayings of the Century",
8+
"price": 8.95
9+
},
10+
{
11+
"category": "fiction",
12+
"author": "Evelyn Waugh",
13+
"title": "Sword of Honour",
14+
"price": 12.99
15+
},
16+
{
17+
"category": "fiction",
18+
"author": "Herman Melville",
19+
"title": "Moby Dick",
20+
"isbn": "0-553-21311-3",
21+
"price": 8.99
22+
},
23+
{
24+
"category": "fiction",
25+
"author": "J. R. R. Tolkien",
26+
"title": "The Lord of the Rings",
27+
"isbn": "0-395-19395-8",
28+
"price": 22.99
29+
}
30+
],
31+
"bicycle": {
32+
"color": "red",
33+
"price": 19.95
34+
}
35+
}
36+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"id": 2,
3+
"firstName": "only",
4+
"lastName": "fullstack",
5+
"salary": 2000,
6+
"email": "onlyfullstack@abc.com"
7+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<store>
3+
<book>
4+
<category>reference</category>
5+
<author>Nigel Rees</author>
6+
<title>Sayings of the Century</title>
7+
<price>8.95</price>
8+
</book>
9+
<book>
10+
<category>fiction</category>
11+
<author>Evelyn Waugh</author>
12+
<title>Sword of Honour</title>
13+
<price>12.99</price>
14+
</book>
15+
<book>
16+
<category>fiction</category>
17+
<author>Herman Melville</author>
18+
<title>Moby Dick</title>
19+
<isbn>0-553-21311-3</isbn>
20+
<price>8.99</price>
21+
</book>
22+
<book>
23+
<category>fiction</category>
24+
<author>J. R. R. Tolkien</author>
25+
<title>The Lord of the Rings</title>
26+
<isbn>0-395-19395-8</isbn>
27+
<price>22.99</price>
28+
</book>
29+
<bicycle>
30+
<color>red</color>
31+
<price>19.95</price>
32+
</bicycle>
33+
</store>

0 commit comments

Comments
 (0)
0