Serialization -Converting pojo into Json; De-Serialization -Converting json into pojo;

Serialization -Converting pojo into Json;  



1- create a pojo class

package Deserialization;

package lombokeg;

import java.util.List;

import lombok.AllArgsConstructor;

import lombok.Builder;

import lombok.Data;

import lombok.NoArgsConstructor;


@Builder

@Data

@NoArgsConstructor

@AllArgsConstructor


public class User {


private int page;

private int per_page;

private int total_pages;

private int total;

private List<UserData> data;

private Support suprt;


@Builder

@Data

@NoArgsConstructor

@AllArgsConstructor

public static class UserData {

private int id;

private String email;

private String first_name;

private String last_name;

private String avatar;

}


@Builder

@Data

@NoArgsConstructor

@AllArgsConstructor

public static class Support {

private String url;

private String text;

}


}


2-

package Test;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;

import java.util.Arrays;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import lombokeg.User;

public class CreateUSerWithJsonarrayLombokPojo {


@Test
public void createUserPage() {

RestAssured.baseURI = "https://postman-echo.com";


User.UserData udate1 = new User.UserData(1, randomemail(), "sudheer11", "kumar",
"https://reqres.in/img/faces/6-image.jpgAvatar.gpg");
User.UserData udate2 = new User.UserData(1, randomemail(), "myra", "kumar",
"https://reqres.in/img/faces/6-image.jpgAvatar.gpg");
User.UserData udate3 = new User.UserData(1, randomemail(), "nirvana", "kumar",
"https://reqres.in/img/faces/6-image.jpgAvatar.gpg");
User.UserData udate4 = new User.UserData(1, randomemail(), "bindu", "kumar",
"https://reqres.in/img/faces/6-image.jpgAvatar.gpg");

@SuppressWarnings("static-access")
User.Support sup = new User.Support().builder().url("https://www.google.com").text("helloworld").build();

@SuppressWarnings("unused")
User user = new User(1, 4, 10, 20, Arrays.asList(udate1, udate2, udate3, udate4), sup);

// here it will do auto serialization internally - converting pojo to json-
// will be done using jackson data bind library-ObjectMapper
given().log().all().contentType(ContentType.JSON).body(user).when().log().all().post("/post").then().log().all()
.assertThat().statusCode(200).header("Server", equalTo("nginx/1.25.3"));

}

public String randomemail() {
return System.currentTimeMillis() + "@gmail.com";
}
}



De-serialzation:
Converting json into pojo; 

Pojo 

package Deserialization;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data

public class FakeStoreProductsPojo {

private int id;
private String title;
private float price;
private String Description;
private String category;
private String image;
Rating rating;

@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public static class Rating {
private float rate;
private float count;

}

}


UserAPITestJsonarrayResponsea


UserPojo[] ipm= mapper.readValue(res.getBody().asString(), UserPojo[].class);
List<Integer>lst = new ArrayList<Integer>();
for(UserPojo e:ipm) {
/*}

package Deserialization;

import static io.restassured.RestAssured.given;

import java.util.ArrayList;
import java.util.List;

import org.testng.Assert;
import org.testng.annotations.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;

public class UserAPITestJsonarrayResponse {

@Test
public void UserAPITestJsonarrayResponsea() {
RestAssured.baseURI = "https://gorest.co.in";

Response res = given().log().all().contentType(ContentType.JSON)
.header("Authorization", "Bearer 4bca2c8e24d669e65c44c1c6788ecdef1fe48b1acfb5e0864847e0cc8510c04c")
.when().log().all().get("/public/v2/users/");
//res.prettyPeek();
ObjectMapper mapper = new ObjectMapper();
try {
UserPojo[] ipm= mapper.readValue(res.getBody().asString(), UserPojo[].class);
List<Integer>lst = new ArrayList<Integer>();
for(UserPojo e:ipm) {
/*
* System.out.println(e.getName()+"==<>");
* System.out.println(e.getGender()+"==<>");
* System.out.println(e.getStatus()+"==<>");
* System.out.println(e.getEmail()+"==<>"); System.out.println("*****");
*/
lst.add(e.getId());
}
System.out.println("Total Size --" + lst.size());
System.out.println("Validating ID list is not or not null");
Assert.assertNotNull(lst,"Not Null");
//System.out.println(ipm.);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}


UserAPiTestJsonObjectResponse



---ObjectMapper mapper = new ObjectMapper();
try {
UserPojo upojo = mapper.readValue(res.getBody().asString(), UserPojo.class);
package Deserialization;

import static io.restassured.RestAssured.given;

import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;

public class UserAPiTestJsonObjectResponse {

/*
* Create a User
*/

@Test
public void createUSerwithLombak() {

RestAssured.baseURI = "https://gorest.co.in";

UserPojo up = new UserPojo().builder().name("hello world").email(randonemail()).gender("female")
.status("active").build();

Integer id = given().log().all().body(up).contentType(ContentType.JSON)
.header("Authorization", "Bearer 4bca2c8e24d669e65c44c1c6788ecdef1fe48b1acfb5e0864847e0cc8510c04c")
.when().log().all().post("/public/v2/users").then().log().all().assertThat().statusCode(201).extract()
.jsonPath().get("id");

System.out.println("the ID is====>: " + id);

Response res = given().log().all().body(up).contentType(ContentType.JSON)
.header("Authorization", "Bearer 4bca2c8e24d669e65c44c1c6788ecdef1fe48b1acfb5e0864847e0cc8510c04c")
.when().log().all().get("/public/v2/users/" + id);

res.prettyPrint();

ObjectMapper mapper = new ObjectMapper();
try {
UserPojo upojo = mapper.readValue(res.getBody().asString(), UserPojo.class);// de-serialization
System.out.println("ID:-   " + upojo.getId() + "Email:-   " + upojo.getEmail());

Assert.assertEquals(upojo.getId(), id);
Assert.assertEquals(upojo.getEmail(), up.getEmail());
Assert.assertEquals(upojo.getGender(), up.getGender());

SoftAssert soft = new SoftAssert();
soft.assertEquals(upojo.getId(), id);
soft.assertEquals(upojo.getEmail(), up.getEmail());
soft.assertEquals(upojo.getGender(), up.getGender());
soft.assertAll();

} catch (JsonProcessingException e) {

e.printStackTrace();
}

}

/*
* Get the USer
*/

public String randonemail() {
return System.currentTimeMillis() + "@gmail.com";
}
}




Comments

Popular posts from this blog

Implicit and Explicit Waits,FluentWait,PageLoadTimeOut

A Interview Questions- selenium