Skip to content

Commit

Permalink
Bai 14 - Annotation - Run test XML
Browse files Browse the repository at this point in the history
  • Loading branch information
anhtester committed Jan 27, 2024
1 parent 3caee6b commit a73e892
Show file tree
Hide file tree
Showing 8 changed files with 275 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/test/java/com/anhtester/Bai14_Annotation/CommonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.anhtester.Bai14_Annotation;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;

public class CommonTest {

@BeforeClass
public void beforeBaseClass() {
System.out.println("Parent Before Class method");
}

@AfterClass
public void afterBaseClass() {
System.out.println("Parent After Class method");
}

@BeforeMethod
public void beforeBaseMethod() {
System.out.println("Parent Before method");
}

@AfterMethod
public void afterBaseMethod() {
System.out.println("Parent After method");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.anhtester.Bai14_Annotation;

import org.testng.annotations.*;

public class DemoRunTestAnnotation extends CommonTest {

@BeforeSuite
public void beforeSuite() {
System.out.println("Before Suite");
}

@AfterSuite
public void afterSuite() {
System.out.println("After Suite");
}

@BeforeTest
public void beforeTest() {
System.out.println("Before Test");
}

@AfterTest
public void afterTest() {
System.out.println("After Test");
}

@BeforeClass
public void beforeClass() {
System.out.println("Before Class");
}

@AfterClass
public void afterClass() {
System.out.println("After Class");
}

@BeforeGroups(groups = { "testOne" })
public void beforeGroupOne() {
System.out.println("Before Group testOne");
}

@AfterGroups(groups = { "testOne" })
public void afterGroupOne() {
System.out.println("After Group testOne");
}

@BeforeGroups(groups = { "testTwo" })
public void beforeGroupTwo() {
System.out.println("Before Group testTwo");
}

@AfterGroups(groups = { "testTwo" })
public void afterGroupTwo() {
System.out.println("After Group testTwo");
}

@BeforeMethod
public void beforeMethod() {
System.out.println("Before Method");
}

@AfterMethod
public void afterMethod() {
System.out.println("After Method");
}

@Test(groups = { "testOne" })
public void testOneMethod() {
System.out.println("Test method 1");
}

@Test(groups = { "testTwo" })
public void testTwoMethod() {
System.out.println("Test method 2");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.anhtester.Bai14_Annotation;

import com.anhtester.common.BaseTest;
import com.anhtester.globals.ConfigsGlobal;
import com.anhtester.globals.TokenGlobal;
import com.anhtester.helpers.JsonHelper;
import com.anhtester.model.LoginPOJO;
import com.anhtester.model.data.LoginPOJO_Builder;
import com.google.gson.Gson;
import io.restassured.http.ContentType;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import net.datafaker.Faker;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.File;
import java.util.Locale;

import static io.restassured.RestAssured.given;

public class DemoRunTestXML_01 extends BaseTest {

@BeforeClass
public void beforeClass1(){
System.out.println("This is before class CON1");
}

public static int CATEGORY_ID;
public static String CATEGORY_NAME;

@Test(priority = 1)
public void testAddNewCategory() {
System.out.println("Create Category");
String dataFile = "src/test/resources/testdata/CreateCategory.json";

Faker faker = new Faker(new Locale("vi"));
CATEGORY_NAME = faker.job().title();
System.out.println("CATEGORY_NAME: " + CATEGORY_NAME);

JsonHelper.updateValueJsonFile(dataFile, "name", CATEGORY_NAME);

RequestSpecification request = given();
request.baseUri("https://api.anhtester.com/api")
.accept(ContentType.JSON)
.contentType(ContentType.JSON)
.header("Authorization", "Bearer " + TokenGlobal.TOKEN)
.body(new File(dataFile));

Response response = request.post("/category");

response.prettyPrint();
response.then().statusCode(200);

CATEGORY_ID = Integer.parseInt(response.path("response.id").toString());
System.out.println("CATEGORY_ID: " + CATEGORY_ID);
}

@Test(priority = 2)
public void testGetCategoryById() {
System.out.println("Get Category By Id");

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

System.out.println("CATEGORY_ID: " + CATEGORY_ID);
Response response = request.get("/category/" + CATEGORY_ID);

response.prettyPrint();
response.then().statusCode(200);

JsonPath jsonPath = response.jsonPath();
Assert.assertEquals(jsonPath.get("response.name"), CATEGORY_NAME, "Category name not match.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.anhtester.Bai14_Annotation;

import com.anhtester.common.BaseTest;
import com.anhtester.globals.ConfigsGlobal;
import com.anhtester.globals.TokenGlobal;
import com.anhtester.model.BookPOJO;
import com.google.gson.Gson;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import net.datafaker.Faker;
import org.joda.time.LocalDateTime;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;

import static io.restassured.RestAssured.given;

public class DemoRunTestXML_02 extends BaseTest {

@BeforeClass
public void beforeClass2(){
System.out.println("This is before class CON2");
}

@Test
public void testAddNewBook() {

Faker faker = new Faker(new Locale("vi"));
String BOOK_NAME = faker.book().title() + "_" + LocalDateTime.now();

BookPOJO bookPOJO = new BookPOJO();

bookPOJO.setName(BOOK_NAME);
bookPOJO.setCategory_id(DemoRunTestXML_01.CATEGORY_ID);
bookPOJO.setPrice(120000);
bookPOJO.setRelease_date("2024-01-27");
bookPOJO.setStatus(true);

bookPOJO.setImage_ids(new ArrayList<>(Arrays.asList(2, 18, 19)));

Gson gson = new Gson();

RequestSpecification request = given();
request.baseUri(ConfigsGlobal.URI)
.accept(ContentType.JSON)
.contentType(ContentType.JSON)
.header("Authorization", "Bearer " + TokenGlobal.TOKEN)
.body(gson.toJson(bookPOJO));

Response response = request.post("book");
response.prettyPrint();

response.then().statusCode(200);

}
}
6 changes: 6 additions & 0 deletions src/test/java/com/anhtester/common/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.gson.Gson;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
Expand All @@ -16,6 +17,11 @@

public class BaseTest {

@BeforeClass
public void beforeClass(){
System.out.println("This is before class CHA");
}

@BeforeSuite
public void setupSuite() {
PropertiesHelper.loadAllFiles();
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/testdata/CreateCategory.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"name":"Global Education Orchestrator"}
{"name":"Internal Hospitality Designer"}
10 changes: 10 additions & 0 deletions suites/SuiteRunAnnotation.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="SuiteRunAnnotation" verbose="1">
<test name="FirstTest">
<classes>
<class name="com.anhtester.Bai14_Annotation.DemoRunTestXML_01"/>
<class name="com.anhtester.Bai14_Annotation.DemoRunTestXML_02"/>
</classes>
</test>
</suite>
10 changes: 10 additions & 0 deletions suites/SuiteRunTest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="SuiteRunTest" verbose="1">
<test name="FirstTest">
<classes>
<class name="com.anhtester.Bai14_Annotation.DemoRunTestXML_01"/>
<class name="com.anhtester.Bai14_Annotation.DemoRunTestXML_02"/>
</classes>
</test>
</suite>

0 comments on commit a73e892

Please sign in to comment.