Skip to content

Commit

Permalink
Bài 8 - PATCH - DELETE
Browse files Browse the repository at this point in the history
  • Loading branch information
anhtester committed Dec 21, 2023
1 parent 6698b11 commit 6e890f8
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/anhtester/globals/TokenGlobal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.anhtester.globals;

public class TokenGlobal {
public static String TOKEN;
}
35 changes: 35 additions & 0 deletions src/test/java/com/anhtester/Bai8_PATCH_DELETE/DemoDELETE.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.anhtester.Bai8_PATCH_DELETE;

import com.anhtester.common.BaseTest;
import com.anhtester.globals.TokenGlobal;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.testng.Assert;
import org.testng.annotations.Test;

import static io.restassured.RestAssured.given;

public class DemoDELETE extends BaseTest {

@Test
public void testDeleteUser_DELETE() {

//Chuẩn bị thông tin username để delete
String username = "an01";

RequestSpecification request = given();
request.baseUri("https://api.anhtester.com/api")
.accept("application/json")
.contentType("application/json")
.header("Authorization", "Bearer " + TokenGlobal.TOKEN)
.queryParam("username", username);

Response response = request.when().delete("/user");
response.prettyPrint();

response.then().statusCode(200);

String message = response.getBody().path("message");
Assert.assertEquals(message, "Success", "The message not match.");
}
}
59 changes: 59 additions & 0 deletions src/test/java/com/anhtester/Bai8_PATCH_DELETE/DemoPATCH.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.anhtester.Bai8_PATCH_DELETE;

import com.anhtester.common.BaseTest;
import com.anhtester.globals.TokenGlobal;
import com.anhtester.model.LoginPOJO;
import com.anhtester.model.PatchUserPOJO;
import com.google.gson.Gson;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static io.restassured.RestAssured.given;

public class DemoPATCH extends BaseTest {

@Test
public void testUpdateUser_PATCH() {

//Chuẩn bị data cho edit user
PatchUserPOJO patchUserPOJO = new PatchUserPOJO();
patchUserPOJO.setFirstName("Thái");
patchUserPOJO.setLastName("Uyên");
patchUserPOJO.setEmail("[email protected]");
patchUserPOJO.setPhone("0111123456");
patchUserPOJO.setUserStatus(0);
patchUserPOJO.setUsername("boiboi2");

Gson gson = new Gson();

RequestSpecification request = given();
request.baseUri("https://api.anhtester.com/api")
.accept("application/json")
.contentType("application/json")
.header("Authorization", "Bearer " + TokenGlobal.TOKEN)
.body(gson.toJson(patchUserPOJO));

Response response = request.when().patch("/user/2");
response.prettyPrint();

response.then().statusCode(200);

String message = response.getBody().path("message");
Assert.assertEquals(message, "Success", "The message not match.");
}

@Test
public void testUpdateUser_PATCH2(){
System.out.println(TokenGlobal.TOKEN);
System.out.println("Test case thứ 2");
}

@Test
public void testUpdateUser_PATCH3(){
System.out.println(TokenGlobal.TOKEN);
System.out.println("Test case thứ 3");
}
}
37 changes: 37 additions & 0 deletions src/test/java/com/anhtester/common/BaseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.anhtester.common;

import com.anhtester.globals.TokenGlobal;
import com.anhtester.model.LoginPOJO;
import com.google.gson.Gson;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.testng.annotations.*;

import static io.restassured.RestAssured.given;

public class BaseTest {
@BeforeTest
public void loginUser() {

//Khởi tạo giá trị cho các fields thông qua hàm xây dựng
LoginPOJO loginPOJO = new LoginPOJO("anhtester", "Demo@123");

//Dùng thư viện Gson để chuyển class POJO về dạng JSON
Gson gson = new Gson();

RequestSpecification request = given();
request.baseUri("https://api.anhtester.com/api")
.accept("application/json")
.contentType("application/json")
.body(gson.toJson(loginPOJO));

Response response = request.when().post("/login");
//response.prettyPrint();

response.then().statusCode(200);

//Lưu giá trị token vào biến TOKEN nhé
TokenGlobal.TOKEN = response.getBody().path("token");
System.out.println("Token: " + TokenGlobal.TOKEN);
}
}
71 changes: 71 additions & 0 deletions src/test/java/com/anhtester/model/PatchUserPOJO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.anhtester.model;

public class PatchUserPOJO {

private String firstName;
private String lastName;
private String email;
private String phone;
private int userStatus;
private String username;

public PatchUserPOJO() {
}

public PatchUserPOJO(String firstName, String lastName, String email, String phone, int userStatus, String username) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.phone = phone;
this.userStatus = userStatus;
this.username = username;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public int getUserStatus() {
return userStatus;
}

public void setUserStatus(int userStatus) {
this.userStatus = userStatus;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}
}

0 comments on commit 6e890f8

Please sign in to comment.