import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.restassured.RestAssured;
import io.restassured.response.ResponseOptions;
public class StudentIntegrationTest {
private static Logger LOG = LoggerFactory.getLogger(StudentIntegrationTest.class);
@Test
public void testIntegrationFlow() {
//Target resource
String url = "http://ezifyautomationlabs.com:6565/educative-rest/students";
// Message body object.
Student body = new Student("David", "Paul", "Male");
//Step 1 - Create a new Student
ResponseOptions<?> create = RestAssured.given()
.header("accept", "application/json")
.header("content-type", "application/json")
.body(body)
.post(url)
.andReturn();
//print response message body in JSON format
LOG.info("Server response for created Student record : " +create.getBody().prettyPrint());
//Step -2 Assert that request was successful
assertTrue(create.statusCode() == 201);
long createdStudentId = create.getBody().jsonPath().getLong("id");
//Step 3 - fetch the newly created Student details
ResponseOptions<?> fetch = RestAssured.given()
.get(url + "/" + createdStudentId)
.andReturn();
//print response message body in JSON format
LOG.info("fetch newly created Student's record with id "+createdStudentId+ ":" +fetch.getBody().prettyPrint());
//Step -4 parse the response data and verify and assert fetch Student details
assertTrue(fetch.statusCode() == 200);
long studentID = fetch.getBody().jsonPath().getLong("id");
assertEquals(createdStudentId,studentID);
//Step 5 - Delete the newly created Student Object
LOG.info("Delete Student with id "+studentID+"'s record");
ResponseOptions<?> delete = RestAssured.given()
.delete(url + "/" + studentID);
//Assert request was successful
assertTrue(delete.statusCode() == 204);
// Step 6 - Try getting Deleted Student's record
ResponseOptions<?> deletedStudent = RestAssured.given()
.get(url + "/" + createdStudentId)
.andReturn();
//Assert record is deleted - NO record found
LOG.info("HTTP GET response statusLine of deleted record "+deletedStudent.getStatusLine());
assertTrue(deletedStudent.statusCode() == 404);
}
}
// This POJO class will be used for serialization and deserialzation of the data
class Student {
public Student(String firstName, String lastName, String gender) {
this.firstName = firstName;
this.lastName = lastName;
this.gender = gender;
}
@JsonProperty("id")
Long id;
@JsonProperty("first_name")
String firstName;
@JsonProperty("last_name")
String lastName;
@JsonProperty("gender")
String gender;
}