-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bài 10 - Common class - Properties - Data Builder
- Loading branch information
Showing
14 changed files
with
318 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
101
src/main/java/com/anhtester/helpers/PropertiesHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/test/java/com/anhtester/Bai10_ReadPropertiesFile/DemoBaseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/test/java/com/anhtester/Bai10_ReadPropertiesFile/DemoReadProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
src/test/java/com/anhtester/common/VerifyDataUserBody.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/test/java/com/anhtester/model/data/LoginPOJO_Builder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/test/java/com/anhtester/model/data/UserPOJO_Lombok_Builder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
Oops, something went wrong.