Skip to content

Commit

Permalink
Bài 10 - Common class - Properties - Data Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
anhtester committed Dec 28, 2023
1 parent 562ba85 commit d248e72
Show file tree
Hide file tree
Showing 14 changed files with 318 additions and 17 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/anhtester/globals/ConfigsGlobal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.anhtester.globals;

import com.anhtester.helpers.PropertiesHelper;

public class ConfigsGlobal {
public static String URI = PropertiesHelper.getValue("URI");
public static String USERNAME = PropertiesHelper.getValue("USERNAME");
public static String PASSWORD = PropertiesHelper.getValue("PASSWORD");
}
101 changes: 101 additions & 0 deletions src/main/java/com/anhtester/helpers/PropertiesHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.anhtester.helpers;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Properties;

public class PropertiesHelper {

private static Properties properties;
private static String linkFile;
private static FileInputStream file;
private static FileOutputStream out;
private static String relPropertiesFilePathDefault = "src/test/resources/config/configs.properties";

public static Properties loadAllFiles() {
LinkedList<String> files = new LinkedList<>();
//Add tất cả file Properties vào đây theo mẫu
files.add("src/test/resources/config/configs.properties");
//files.add("src/test/resources/config/configs2.properties");

try {
properties = new Properties();

for (String f : files) {
Properties tempProp = new Properties();
linkFile = SystemHelper.getCurrentDir() + f;
file = new FileInputStream(linkFile);
tempProp.load(file);
properties.putAll(tempProp);
}
return properties;
} catch (IOException ioe) {
return new Properties();
}
}

public static void setFile(String relPropertiesFilePath) {
properties = new Properties();
try {
linkFile = SystemHelper.getCurrentDir() + relPropertiesFilePath;
file = new FileInputStream(linkFile);
properties.load(file);
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void setDefaultFile() {
properties = new Properties();
try {
linkFile = SystemHelper.getCurrentDir() + relPropertiesFilePathDefault;
file = new FileInputStream(linkFile);
properties.load(file);
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public static String getValue(String key) {
String value = null;
try {
if (file == null) {
properties = new Properties();
linkFile = SystemHelper.getCurrentDir() + relPropertiesFilePathDefault;
file = new FileInputStream(linkFile);
properties.load(file);
file.close();
}
// Lấy giá trị từ file đã Set
value = properties.getProperty(key);
} catch (Exception e) {
System.out.println(e.getMessage());
}
return value;
}

public static void setValue(String key, String keyValue) {
try {
if (file == null) {
properties = new Properties();
file = new FileInputStream(SystemHelper.getCurrentDir() + relPropertiesFilePathDefault);
properties.load(file);
file.close();
out = new FileOutputStream(SystemHelper.getCurrentDir() + relPropertiesFilePathDefault);
}
//Ghi vào cùng file Prop với file lấy ra
out = new FileOutputStream(linkFile);
System.out.println(linkFile);
properties.setProperty(key, keyValue);
properties.store(out, null);
out.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

}
9 changes: 9 additions & 0 deletions src/main/java/com/anhtester/helpers/SystemHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.anhtester.helpers;

import java.io.File;

public class SystemHelper {
public static String getCurrentDir() {
return System.getProperty("user.dir") + File.separator;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.anhtester.Bai10_ReadPropertiesFile;

import com.anhtester.common.BaseTest;
import com.anhtester.globals.ConfigsGlobal;
import com.anhtester.globals.TokenGlobal;
import com.anhtester.model.RegisterUserPOJO_Lombok;
import com.anhtester.model.data.UserPOJO_Lombok_Builder;
import com.google.gson.Gson;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import net.datafaker.Faker;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.Locale;

import static io.restassured.RestAssured.given;

public class DemoBaseTest extends BaseTest {
@Test
public void testUpdateUser_PATCH() {

Faker faker = new Faker(new Locale("vi"));
String phoneNumber = faker.phoneNumber().cellPhone();
phoneNumber = phoneNumber.replace(" ", "");

RegisterUserPOJO_Lombok registerUserPOJO_lombok = new RegisterUserPOJO_Lombok();
registerUserPOJO_lombok.setFirstName(faker.name().firstName());
registerUserPOJO_lombok.setLastName(faker.name().lastName());
registerUserPOJO_lombok.setEmail(faker.internet().emailAddress());
registerUserPOJO_lombok.setPhone(phoneNumber);
registerUserPOJO_lombok.setUserStatus(0);

Gson gson = new Gson();

RequestSpecification request = given();
request.baseUri(ConfigsGlobal.URI)
.accept("application/json")
.contentType("application/json")
.header("Authorization", "Bearer " + TokenGlobal.TOKEN)
.body(gson.toJson(registerUserPOJO_lombok));

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_PATCH_Builder() {

RegisterUserPOJO_Lombok registerUserPOJO_lombok = UserPOJO_Lombok_Builder.getUserData();

Gson gson = new Gson();

RequestSpecification request = given();
request.baseUri(ConfigsGlobal.URI)
.accept("application/json")
.contentType("application/json")
.header("Authorization", "Bearer " + TokenGlobal.TOKEN)
.body(gson.toJson(registerUserPOJO_lombok));

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.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.anhtester.Bai10_ReadPropertiesFile;

import com.anhtester.helpers.PropertiesHelper;
import org.testng.annotations.Test;

public class DemoReadProperties {
@Test
public void testReadFile() {
//Gọi hàm loadAllFiles trước tiên để load tất cả các file properties vào chung bộ nhớ
PropertiesHelper.loadAllFiles();

//Sau đó gọi hàm getValue để lấy giá trị theo tên key
System.out.println("URI: " + PropertiesHelper.getValue("URI"));
System.out.println("USERNAME: " + PropertiesHelper.getValue("USERNAME"));
System.out.println("PASSWORD: " + PropertiesHelper.getValue("PASSWORD"));
}

@Test
public void testWriteValue() {
//Trước tiên chỉ định file cần set giá trị vào
//Dùng đường dẫn tương đối
//Ví dụ file configs.properties

PropertiesHelper.setFile("src/test/resources/config/demo.properties");

//Gọi hàm setValue để gán giá trị theo key
PropertiesHelper.setValue("author", "Anh Tester");
PropertiesHelper.setValue("project", "API Automation");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.anhtester.Bai7_Authentication_PUTmethod;

import com.anhtester.common.VerifyDataUserBody;
import com.anhtester.model.LoginPOJO;
import com.anhtester.model.RegisterUserPOJO;
import com.google.gson.Gson;
Expand Down Expand Up @@ -72,6 +73,9 @@ public void testEditUser_NoAuth() {

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

VerifyDataUserBody.verifyDataBodyUser(response, registerUserPOJO);

}

}
28 changes: 17 additions & 11 deletions src/test/java/com/anhtester/common/BaseTest.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
package com.anhtester.common;

import com.anhtester.globals.ConfigsGlobal;
import com.anhtester.globals.TokenGlobal;
import com.anhtester.helpers.PropertiesHelper;
import com.anhtester.model.LoginPOJO;
import com.anhtester.model.data.LoginPOJO_Builder;
import com.google.gson.Gson;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.testng.annotations.*;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;

import static io.restassured.RestAssured.given;

public class BaseTest {

@BeforeSuite
public void setupSuite() {
PropertiesHelper.loadAllFiles();
}

@BeforeTest
public void loginUser() {
//LoginPOJO loginPOJO = new LoginPOJO(ConfigsGlobal.USERNAME, ConfigsGlobal.PASSWORD);
LoginPOJO loginPOJO = LoginPOJO_Builder.getDataLogin();

//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")
request.baseUri(ConfigsGlobal.URI)
.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);
System.out.println("Token Global: " + TokenGlobal.TOKEN);
}
}
}
22 changes: 22 additions & 0 deletions src/test/java/com/anhtester/common/VerifyDataUserBody.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.anhtester.common;

import com.anhtester.model.RegisterUserPOJO;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import org.testng.Assert;

public class VerifyDataUserBody {
public static void verifyDataBodyUser(Response response, RegisterUserPOJO registerUserPOJO){

JsonPath jsonPath = response.jsonPath();

Assert.assertEquals(jsonPath.get("response.username"), registerUserPOJO.getUsername(), "The username not match.");
Assert.assertEquals(jsonPath.get("response.firstName"), registerUserPOJO.getFirstName(), "The firstName not match.");
Assert.assertEquals(jsonPath.get("response.lastName"), registerUserPOJO.getLastName(), "The lastName not match.");
Assert.assertEquals(jsonPath.get("response.email"), registerUserPOJO.getEmail(), "The email not match.");
Assert.assertEquals(jsonPath.get("response.phone"), registerUserPOJO.getPhone(), "The phone not match.");
Assert.assertEquals(Integer.parseInt(jsonPath.get("response.userStatus").toString()), registerUserPOJO.getUserStatus(), "The userStatus not match.");
Assert.assertTrue(jsonPath.get("response.id").toString().length() > 0, "The ID not exsiting.");

}
}
3 changes: 3 additions & 0 deletions src/test/java/com/anhtester/model/LoginPOJO.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.anhtester.model;

import lombok.Builder;

@Builder
public class LoginPOJO {
private String username;
private String password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@Data //Bao gồm Getter và Setter
//@Getter
//@Setter
@Builder
public class RegisterUserPOJO_Lombok {
private String username;
private String firstName;
Expand All @@ -15,10 +16,4 @@ public class RegisterUserPOJO_Lombok {
private String password;
private String phone;
private int userStatus;

public RegisterUserPOJO_Lombok(String username, String firstName, String lastName) {
this.username = username;
this.firstName = firstName;
this.lastName = lastName;
}
}
15 changes: 15 additions & 0 deletions src/test/java/com/anhtester/model/data/LoginPOJO_Builder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.anhtester.model.data;

import com.anhtester.globals.ConfigsGlobal;
import com.anhtester.model.LoginPOJO;

public class LoginPOJO_Builder {

public static LoginPOJO getDataLogin(){
return LoginPOJO.builder()
.username(ConfigsGlobal.USERNAME)
.password(ConfigsGlobal.PASSWORD)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.anhtester.model.data;

import com.anhtester.model.RegisterUserPOJO_Lombok;
import net.datafaker.Faker;

import java.util.Locale;

public class UserPOJO_Lombok_Builder {

public static RegisterUserPOJO_Lombok getUserData() {

Faker faker = new Faker(new Locale("vi"));
String phoneNumber = faker.phoneNumber().cellPhone();
phoneNumber = phoneNumber.replace(" ", "");

return RegisterUserPOJO_Lombok.builder()
.firstName(faker.name().firstName())
.lastName(faker.name().lastName())
.phone(phoneNumber)
.email(faker.internet().emailAddress())
.userStatus(1)
.build();
}

}
Loading

0 comments on commit d248e72

Please sign in to comment.