Skip to content

Commit d512017

Browse files
committed
serialization and deserialization
1 parent 7564227 commit d512017

File tree

4 files changed

+228
-0
lines changed

4 files changed

+228
-0
lines changed

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@
2828
<version>7.7.0</version>
2929
<scope>test</scope>
3030
</dependency>
31+
32+
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
33+
<dependency>
34+
<groupId>com.fasterxml.jackson.core</groupId>
35+
<artifactId>jackson-databind</artifactId>
36+
<version>2.18.2</version>
37+
38+
</dependency>
39+
40+
3141
</dependencies>
3242

3343
</project>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/* the commented code is implementation of list of <maps>.
2+
we can also use it instead of making separate class for "Place".
3+
But creating list of <maps> will not be very efficient way in many cases.
4+
*/
5+
6+
package dataentities;
7+
8+
import com.fasterxml.jackson.annotation.JsonProperty;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
//import java.util.HashMap;
13+
//import java.util.Map;
14+
15+
16+
public class Location {
17+
18+
private String postCode;
19+
private String country;
20+
private String countryAbbreviation;
21+
//private List <Map <String, String>> places;
22+
private List<place> places;
23+
24+
public Location(){
25+
this.postCode = "1050";
26+
this.country = "Lativa";
27+
this.countryAbbreviation = "LV";
28+
29+
this.places = new ArrayList<>();
30+
place Place = new place();
31+
this.places.add(Place);
32+
//this.places.add(addPlaces("Riga", "1", "Rigaa", "RI", "2"));
33+
}
34+
35+
/* public Map<String, String> addPlaces(String placeName, String longitude, String state, String stateAbbreviation, String latitude){
36+
Map<String, String> place = new HashMap<>();
37+
place.put("place name", placeName);
38+
place.put("longitude", longitude);
39+
place.put("state", state);
40+
place.put("state abbreviation", stateAbbreviation);
41+
place.put("latitude", latitude);
42+
return place;
43+
} */
44+
45+
@JsonProperty("post code")
46+
public String getPostCode(){
47+
return postCode;
48+
}
49+
50+
@JsonProperty("post code")
51+
public void setPostCode(String postCode){
52+
this.postCode = postCode;
53+
}
54+
55+
public void setCountry(String country) {
56+
this.country = country;
57+
}
58+
59+
public String getCountry() {
60+
return country;
61+
}
62+
63+
@JsonProperty("country abbreviation")
64+
public void setCountryAbbreviation(String countryAbbreviation) {
65+
this.countryAbbreviation = countryAbbreviation;
66+
}
67+
68+
@JsonProperty("country abbreviation")
69+
public String getCountryAbbreviation() {
70+
return countryAbbreviation;
71+
}
72+
73+
/* public void setPlaces(List<Map<String, String>> places) {
74+
this.places = places;
75+
}
76+
77+
public List<Map<String, String>> getPlaces() {
78+
return places;
79+
} */
80+
81+
82+
public List<place> getPlaces() {
83+
return places;
84+
}
85+
86+
public void setPlaces(List<place> places) {
87+
this.places = places;
88+
}
89+
}
90+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package dataentities;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
public class place {
6+
private String placeName;
7+
private String longitude;
8+
private String state;
9+
private String stateAbbreviation;
10+
private String latitude;
11+
12+
place(){
13+
this.placeName = "Riga";
14+
this.longitude="1";
15+
this.state="Rigaa";
16+
this.stateAbbreviation="RI";
17+
this.latitude="2";
18+
}
19+
@JsonProperty("place name")
20+
public String getPlaceName() {
21+
return placeName;
22+
}
23+
24+
public String getLongitude() {
25+
return longitude;
26+
}
27+
28+
public String getState() {
29+
return state;
30+
}
31+
@JsonProperty("state abbreviation")
32+
public String getStateAbbreviation() {
33+
return stateAbbreviation;
34+
}
35+
36+
public String getLatitude() {
37+
return latitude;
38+
}
39+
@JsonProperty("place name")
40+
public void setPlaceName(String placeName) {
41+
this.placeName = placeName;
42+
}
43+
44+
public void setLongitude(String longitude) {
45+
this.longitude = longitude;
46+
}
47+
48+
public void setState(String state) {
49+
this.state = state;
50+
}
51+
@JsonProperty("state abbreviation")
52+
public void setStateAbbreviation(String stateAbbreviation) {
53+
this.stateAbbreviation = stateAbbreviation;
54+
}
55+
56+
public void setLatitude(String latitude) {
57+
this.latitude = latitude;
58+
}
59+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import dataentities.Location;
2+
import io.restassured.builder.RequestSpecBuilder;
3+
import io.restassured.builder.ResponseSpecBuilder;
4+
import io.restassured.http.ContentType;
5+
import io.restassured.specification.RequestSpecification;
6+
import io.restassured.specification.ResponseSpecification;
7+
import org.testng.Assert;
8+
import org.testng.annotations.BeforeTest;
9+
import org.testng.annotations.Test;
10+
11+
import java.io.Serializable;
12+
import java.util.Map;
13+
14+
import static io.restassured.RestAssured.given;
15+
16+
public class serializationDeserialization {
17+
18+
private static RequestSpecification ReqSpec;
19+
private static ResponseSpecification ResSpec;
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+
@Test
37+
public void checkPlaceName_expectedBeverlyHills(){
38+
Location location =
39+
given().
40+
spec(ReqSpec).log().body().
41+
when().
42+
get("us/90210").
43+
then().log().all().
44+
spec(ResSpec).
45+
extract().
46+
as(Location.class); //deserialization
47+
// Map<String , String> firstPlace = location.getPlaces().get(0);
48+
Assert.assertEquals(location.getPlaces().get(0).getPlaceName(), "Beverly Hills");
49+
}
50+
51+
@Test
52+
public void checkPlaceName_expectedRiga(){
53+
Location location = new Location();
54+
given().
55+
body(location). // this is serialization,where data of java object will be converted into JSON
56+
spec(ReqSpec).
57+
log().body().
58+
when().
59+
get("us/90210").
60+
then().
61+
log().all().
62+
spec(ResSpec);
63+
// extract().
64+
// as(Location.class); //deserialization
65+
66+
// Map<String , String> firstPlace = location.getPlaces().get(0);
67+
Assert.assertEquals(location.getPlaces().get(0).getPlaceName(), "Riga"); // API response will be "Beverly hills", but we updated it to "Rega" with serialization. hence it will pass.
68+
}
69+
}

0 commit comments

Comments
 (0)