Skip to content

Commit 1e2e154

Browse files
committed
Extracting Response value
1 parent edefef4 commit 1e2e154

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import io.restassured.builder.*;
2+
import io.restassured.http.ContentType;
3+
import io.restassured.specification.RequestSpecification;
4+
import io.restassured.specification.ResponseSpecification;
5+
import org.testng.Assert;
6+
import org.testng.annotations.BeforeClass;
7+
import org.testng.annotations.BeforeTest;
8+
import org.testng.annotations.DataProvider;
9+
import org.testng.annotations.Test;
10+
11+
import static io.restassured.RestAssured.given;
12+
import static org.hamcrest.Matchers.*;
13+
import static org.hamcrest.Matchers.hasItem;
14+
15+
public class ExtractResponseValueTest {
16+
17+
private static RequestSpecification ReqSpec;
18+
private static ResponseSpecification ResSpec;
19+
20+
@BeforeTest
21+
public static void RequestSpecificBuilder(){
22+
ReqSpec = new RequestSpecBuilder().
23+
setBaseUri("https://zippopotam.us").
24+
setContentType(ContentType.JSON).
25+
build();
26+
}
27+
28+
@BeforeTest
29+
public static void ResponseSpecificBuilder(){
30+
ResSpec = new ResponseSpecBuilder().
31+
expectStatusCode(200).
32+
expectContentType(ContentType.JSON).
33+
build();
34+
}
35+
36+
@DataProvider(name="place")
37+
public Object[][] ZipCodePlaceDetails() {
38+
return new Object[][]{
39+
{"400001", "IN", "University Road", "Maharashtra", "India"},
40+
{"140001", "IN", "Rup Nagar College", "Punjab", "India"},
41+
{"110001", "IN", "Janpath", "New Delhi", "India"},
42+
{"400001", "IN", "Cannon Road", "Maharashtra", "India"},
43+
{"400001", "IN", "Fort Market", "Maharashtra", "India"},
44+
{"400001", "IN", "G T Hospital", "Maharashtra", "India"}
45+
};
46+
}
47+
48+
@BeforeClass
49+
public void logRequestAndResponseDetails(){
50+
given().
51+
spec(ReqSpec).
52+
log().all().
53+
when().
54+
get("IN/400001").
55+
then().
56+
log().body();
57+
58+
}
59+
60+
@Test(priority = 0, dataProvider = "place")
61+
public void checkStatusCodeAndContentType_expectHttp200andApplicationJSON(String zipcode, String countryCode, String Place, String State, String Country){
62+
given().
63+
spec(ReqSpec).
64+
pathParams("zipcode",zipcode).
65+
pathParams("countryCode",countryCode).
66+
when().
67+
get("{countryCode}/{zipcode}").
68+
then().
69+
spec(ResSpec);
70+
}
71+
72+
@Test (priority = 2, dependsOnMethods = "checkStatusCodeAndContentType_expectHttp200andApplicationJSON", dataProvider = "place")
73+
public void checkPostCodeInResponse_expectTrue(String zipcode, String countryCode, String Place, String State, String Country){
74+
String code =
75+
given().
76+
spec(ReqSpec).
77+
pathParams("zipcode",zipcode).
78+
pathParams("countryCode",countryCode).
79+
when().
80+
get("/{countryCode}/{zipcode}").
81+
then().
82+
spec(ResSpec).
83+
and().extract().path("'post code'");
84+
Assert.assertEquals(code,zipcode, "'post code' is not same");
85+
}
86+
87+
@Test(priority = 3, dependsOnMethods = "checkStatusCodeAndContentType_expectHttp200andApplicationJSON", dataProvider = "place")
88+
public void checkStateNameInResponse_expectTrue(String zipcode, String countryCode, String Place, String State, String Country){
89+
String state =
90+
given().spec(ReqSpec).
91+
pathParams("zipcode",zipcode).
92+
pathParams("countryCode",countryCode).
93+
when().
94+
get("/{countryCode}/{zipcode}").
95+
then().
96+
spec(ResSpec).
97+
and().extract().path("places[0].state");
98+
Assert.assertEquals(state,State, "State is not same");
99+
}
100+
101+
@Test (priority = 4, dependsOnMethods = "checkStatusCodeAndContentType_expectHttp200andApplicationJSON", dataProvider = "place")
102+
public void checkCountryInResponse_expectTrue(String zipcode, String countryCode, String place, String State, String Country){
103+
String country=
104+
given().spec(ReqSpec).
105+
pathParams("zipcode",zipcode).
106+
pathParams("countryCode",countryCode).
107+
when().
108+
get("/{countryCode}/{zipcode}").
109+
then().extract().path("country");
110+
Assert.assertEquals(country,Country, "Country is not same");
111+
}
112+
113+
@Test (priority = 5, dependsOnMethods = "checkStatusCodeAndContentType_expectHttp200andApplicationJSON", dataProvider = "place")
114+
public void checkCountryAbbreviationInResponse_expectTrue(String zipcode, String countryCode, String Place, String State, String Country){
115+
String CC=
116+
given().spec(ReqSpec).
117+
pathParams("zipcode",zipcode).
118+
pathParams("countryCode",countryCode).
119+
when().
120+
get("/{countryCode}/{zipcode}").
121+
then().extract().path("'country abbreviation'");
122+
Assert.assertEquals(CC,countryCode, "Code is not same");
123+
}
124+
125+
@Test (priority = 6, dependsOnMethods = "checkStatusCodeAndContentType_expectHttp200andApplicationJSON", dataProvider = "place")
126+
public void checkIfPlaceAvailable_expectTrue(String zipcode, String countryCode, String Place, String State, String Country){
127+
given().spec(ReqSpec).
128+
pathParams("zipcode",zipcode).
129+
pathParams("countryCode",countryCode).
130+
when().
131+
get("/{countryCode}/{zipcode}").
132+
then().assertThat().
133+
body("places.'place name'", hasItem(Place));
134+
}
135+
136+
@Test (priority = 7, dependsOnMethods = "checkStatusCodeAndContentType_expectHttp200andApplicationJSON", dataProvider = "place")
137+
public void checkIfPlaceNotAvailable_expectTrue(String zipcode, String countryCode, String Place, String State, String Country){
138+
given().spec(ReqSpec).
139+
pathParams("zipcode",zipcode).
140+
pathParams("countryCode",countryCode).
141+
when().
142+
get("/{countryCode}/{zipcode}").
143+
then().spec(ResSpec).
144+
and().
145+
body("places.'place name'", not(hasItem("Mumbai")));
146+
}
147+
148+
}

0 commit comments

Comments
 (0)