Skip to content

Commit

Permalink
Extract class and method
Browse files Browse the repository at this point in the history
  • Loading branch information
giangnd2508 committed Jul 6, 2020
1 parent 75827ff commit f4bc269
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 52 deletions.
17 changes: 17 additions & 0 deletions src/test/java/com/giangtester/api/DeleteStudent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.giangtester.api;

import com.giangtester.models.Student;
import io.restassured.http.ContentType;
import io.restassured.response.Response;

import static io.restassured.RestAssured.given;

public class DeleteStudent {

public Response deleteStudent(Student student) {
return given().log().all()
.contentType(ContentType.JSON)
.when()
.delete("/{id}", student.getId());
}
}
19 changes: 19 additions & 0 deletions src/test/java/com/giangtester/api/PostStudent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.giangtester.api;

import com.giangtester.models.Student;
import io.restassured.http.ContentType;
import io.restassured.response.Response;

import static io.restassured.RestAssured.given;

public class PostStudent {

public Response createAStudent(Student student) {
return given().log().all()
.contentType(ContentType.JSON)
.when()
.body(student)
.post();
}

}
18 changes: 18 additions & 0 deletions src/test/java/com/giangtester/api/PutStudent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.giangtester.api;

import com.giangtester.models.Student;
import io.restassured.http.ContentType;
import io.restassured.response.Response;

import static io.restassured.RestAssured.given;

public class PutStudent {

public Response updateStudent(Student student, String id) {
return given().log().all()
.contentType(ContentType.JSON)
.when()
.body(student)
.put("/{id}", id);
}
}
29 changes: 14 additions & 15 deletions src/test/java/com/giangtester/features/DELETEStudentTest.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
package com.giangtester.features;

import com.giangtester.api.DeleteStudent;
import com.giangtester.api.PostStudent;
import com.giangtester.base.BaseClass;
import com.giangtester.models.Student;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;

public class DELETEStudentTest extends BaseClass {

@Test
void deleteAnExistingStudent() {
// Arrange: Setup data
Student student = Student.getInstance();
given().log().all().contentType(ContentType.JSON)
.when().body(student).post();
private Student student;
private final PostStudent postStudent = new PostStudent();
private final DeleteStudent deleteStudent = new DeleteStudent();

// Act: Call API delete student
Response res = given()
.contentType(ContentType.JSON)
.when()
.delete("/{id}", student.getId());
@BeforeEach
void createStudent() {
student = Student.getInstance();
postStudent.createAStudent(student);
}

// Assert
@Test
void deleteAnExistingStudent() {
Response res = deleteStudent.deleteStudent(student);
res.then().statusCode(204);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ class GETListStudentTest extends BaseClass {
@Test
public void getListStudent() {
Response res = getStudent.getListStudent();
res.prettyPrint();
res.then().statusCode(200);
}

@Test
public void getFirstStudent() {
Response res = getStudent.getListStudentById("1");
res.then().statusCode(200).body("email", equalTo("adalberto.glover@yahoo.com"));
public void getSecondStudent() {
Response res = getStudent.getListStudentById("2");
res.then().statusCode(200).body("email", equalTo("faucibus.orci.luctus@Duisac.net"));
}
}
34 changes: 11 additions & 23 deletions src/test/java/com/giangtester/features/POSTNewStudentTest.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
package com.giangtester.features;

import com.giangtester.api.PostStudent;
import com.giangtester.base.BaseClass;
import com.giangtester.models.Student;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

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

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

public class POSTNewStudentTest extends BaseClass {

@Test
void postNewStudent() {
List<String> courses = new ArrayList<>();
courses.add("Math");
courses.add("Economics");
private Student student;
private final PostStudent postStudent = new PostStudent();

Student student = Student.getInstance();
student.setId(101);
student.setFirstName("Giang");
student.setLastName("Nguyen");
student.setEmail("giang.nguyen@gc.com");
student.setProgramme("Computer Science");
student.setCourses(courses);
@BeforeEach
void createStudent() {
student = Student.getInstance();
}

Response res = given()
.contentType(ContentType.JSON)
.when()
.body(student)
.post();
//res.prettyPrint();
@Test
void postNewStudent() {
Response res = postStudent.createAStudent(student);
res.then().statusCode(201).body("msg", equalTo("Student added"));
}
}
25 changes: 14 additions & 11 deletions src/test/java/com/giangtester/features/PUTStudentTest.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
package com.giangtester.features;

import com.giangtester.api.GetStudent;
import com.giangtester.api.PutStudent;
import com.giangtester.base.BaseClass;
import com.giangtester.models.Student;
import io.restassured.http.ContentType;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;

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

public class PUTStudentTest extends BaseClass {

private Student student;
private final PutStudent putStudent = new PutStudent();
private final GetStudent getStudent = new GetStudent();

@BeforeEach
void createStudent() {
student = Student.getInstance();
}

@Test
void changeInformationOfAnExistingStudent() {
Student student = Student.getInstance();
student.setFirstName("Giang");

Response res = given()
.contentType(ContentType.JSON)
.when()
.body(student)
.put("/1");

res.prettyPrint();
Response res = putStudent.updateStudent(student, "1");
res.then().statusCode(200).body("msg", equalTo("Student Updated"));

JsonPath jsonPath = given().when().get("/list").then().extract().body().jsonPath();
JsonPath jsonPath = getStudent.getListStudent().then().extract().body().jsonPath();
List<Student> students = jsonPath.getList("", Student.class);
assertThat(students.get(0).getFirstName(), equalTo("Giang"));
}
Expand Down

0 comments on commit f4bc269

Please sign in to comment.