Skip to content

Commit

Permalink
Bài 17 Common functions in class keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
anhtester committed Mar 7, 2024
1 parent 473a699 commit 446c08e
Show file tree
Hide file tree
Showing 14 changed files with 375 additions and 27 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/anhtester/globals/ConfigsGlobal.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.anhtester.helpers.PropertiesHelper;

public class ConfigsGlobal {
public static String URI = PropertiesHelper.getValue("BASE_URI");
public static String AUTHOR = PropertiesHelper.getValue("AUTHOR");
public static String BASE_URI = PropertiesHelper.getValue("BASE_URI");
public static String BASE_PATH = PropertiesHelper.getValue("BASE_PATH");
public static String USERNAME = PropertiesHelper.getValue("USERNAME");
public static String PASSWORD = PropertiesHelper.getValue("PASSWORD");
public static int TCS_TOTAL;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/anhtester/globals/EndPointGlobal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.anhtester.globals;

public class EndPointGlobal {
public static final String EP_LOGIN = "/login";
public static final String EP_REGISTER = "/register";
public static final String EP_CATEGORY = "/category";
public static final String EP_BOOKS = "/books";
}
4 changes: 4 additions & 0 deletions src/main/java/com/anhtester/globals/TokenGlobal.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

public class TokenGlobal {
public static String TOKEN;

public static String getBearerToken() {
return TOKEN;
}
}
203 changes: 203 additions & 0 deletions src/main/java/com/anhtester/keywords/ApiKeyword.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package com.anhtester.keywords;

import com.anhtester.utils.LogUtils;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import org.testng.Assert;

import java.io.File;
import java.util.Map;

import static io.restassured.RestAssured.given;

public class ApiKeyword {

public static Response get(String path) {
LogUtils.info("GET: " + path);
Response response =
given(SpecBuilder.getRequestSpecBuilder()).
when().
get(path).
then().
spec(SpecBuilder.getResponseSpecBuilder()).
extract().
response();

LogUtils.info("RESPONSE: \n" + response.prettyPrint());
return response;
}

public static Response get(String path, Map<String, String> headers) {
LogUtils.info("GET: " + path);
LogUtils.info("HEADERS: " + headers);
Response response =
given(SpecBuilder.getRequestSpecBuilder().headers(headers)).
when().
get(path).
then().
spec(SpecBuilder.getResponseSpecBuilder()).
extract().
response();

LogUtils.info("RESPONSE: \n" + response.prettyPrint());
return response;
}

public static Response get(String path, String authBearerToken) {
LogUtils.info("GET: " + path);
LogUtils.info("BEARER TOKEN: " + authBearerToken);
Response response =
given(SpecBuilder.getRequestSpecBuilder().header("Authorization", "Bearer " + authBearerToken)).
when().
get(path).
then().
spec(SpecBuilder.getResponseSpecBuilder()).
extract().
response();

LogUtils.info("RESPONSE: \n" + response.prettyPrint());
return response;
}

public static Response getNotAuth(String path) {
LogUtils.info("GET not authorization: " + path);
Response response =
given(SpecBuilder.getRequestNotAuthSpecBuilder()).
when().
get(path).
then().
spec(SpecBuilder.getResponseSpecBuilder()).
extract().
response();

LogUtils.info("RESPONSE: \n" + response.prettyPrint());
return response;
}

public static Response post(String path, Object payLoad) {
LogUtils.info("POST: " + path);
LogUtils.info("Body: " + payLoad);
Response response =
given(SpecBuilder.getRequestSpecBuilder()).
body(payLoad).
when().
post(path).
then().
spec(SpecBuilder.getResponseSpecBuilder()).
extract().response();

LogUtils.info("RESPONSE: \n" + response.prettyPrint());
return response;
}

public static Response postNotAuth(String path, Object payLoad) {
LogUtils.info("POST not authorization: " + path);
LogUtils.info("Body: " + payLoad);
Response response =
given(SpecBuilder.getRequestNotAuthSpecBuilder()).
body(payLoad).
when().
post(path).
then().
spec(SpecBuilder.getResponseSpecBuilder()).
extract().response();

LogUtils.info("RESPONSE: \n" + response.prettyPrint());
return response;
}

public static Response post(String path, String filePathBody) {
LogUtils.info("POST: " + path);
LogUtils.info("Body: " + filePathBody);
Response response =
given(SpecBuilder.getRequestSpecBuilder()).
body(new File(filePathBody)).
when().
post(path).
then().
spec(SpecBuilder.getResponseSpecBuilder()).
extract().response();

LogUtils.info("RESPONSE: \n" + response.prettyPrint());
return response;
}

public static Response put(String path, Object payLoad) {
LogUtils.info("PUT: " + path);
LogUtils.info("Body: " + payLoad);
Response response =
given(SpecBuilder.getRequestSpecBuilder()).
body(payLoad).
when().
put(path).
then().
extract().response();

LogUtils.info("RESPONSE: \n" + response.prettyPrint());
return response;
}

public static Response delete(String path, Object payLoad) {
LogUtils.info("DELETE: " + path);
LogUtils.info("Body: " + payLoad);
Response response =
given(SpecBuilder.getRequestSpecBuilder()).
body(payLoad).
when().
delete(path).
then().
extract().response();

LogUtils.info("RESPONSE: \n" + response.prettyPrint());
return response;
}

public static String getResponseKeyValue(Response response, String responseKey) {
JsonPath jsonPath = response.jsonPath();
String key_value = jsonPath.get(responseKey).toString();
LogUtils.info("Get body by key (" + responseKey + "): " + key_value);
return key_value;
}

public static String getResponseKeyValue(String responseBody, String responseKey) {
JsonPath jsonPath = new JsonPath(responseBody);
String key_value = jsonPath.get(responseKey).toString();
LogUtils.info("Get body by key (" + responseKey + "): " + key_value);
return key_value;
}

public static int getStatusCode(Response response) {
int status_code = response.getStatusCode();
LogUtils.info("Get Status Code: " + status_code);
return status_code;
}

public static String getStatusLine(Response response) {
String status_line = response.getStatusLine();
LogUtils.info("Get Status Line: " + status_line);
return status_line;
}

public static String getResponseHeader(Response response, String header_key) {
String response_header = response.getHeader(header_key);
LogUtils.info("Get Header by key (" + header_key + "): " + response_header);
return response_header;
}

public static String getResponseContentType(Response response) {
String content_type = response.getContentType();
LogUtils.info("Get Content Type: " + content_type);
return content_type;
}

public static String getResponseCookieName(Response response, String cookieName) {
String cookie_value = response.getCookie(cookieName);
LogUtils.info("Get Cookie by name (" + cookieName + "): " + cookie_value);
return cookie_value;
}

public static void verifyStatusCode(Response response, int expectedStatusCode) {
LogUtils.info("Verify Status code: " + response.getStatusCode() + " == " + expectedStatusCode);
Assert.assertEquals(response.getStatusCode(), expectedStatusCode, "FAIL. The status code not match.");
}
}
48 changes: 48 additions & 0 deletions src/main/java/com/anhtester/keywords/SpecBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.anhtester.keywords;

import com.anhtester.globals.ConfigsGlobal;
import com.anhtester.globals.TokenGlobal;
import com.anhtester.utils.LogUtils;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.filter.log.LogDetail;
import io.restassured.filter.log.RequestLoggingFilter;
import io.restassured.filter.log.ResponseLoggingFilter;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.ResponseSpecification;

public class SpecBuilder {

public static RequestSpecification getRequestSpecBuilder() {
return new RequestSpecBuilder().
setBaseUri(ConfigsGlobal.BASE_URI).
setBasePath(ConfigsGlobal.BASE_PATH).
addHeader("Authorization", "Bearer " + TokenGlobal.TOKEN).
setContentType(ContentType.JSON).
setAccept(ContentType.JSON).
addFilter(new RequestLoggingFilter()).
addFilter(new ResponseLoggingFilter()).
log(LogDetail.ALL).
build();
}

public static ResponseSpecification getResponseSpecBuilder() {
return new ResponseSpecBuilder().
expectContentType(ContentType.JSON).
log(LogDetail.ALL).
build();
}

public static RequestSpecification getRequestNotAuthSpecBuilder() {
return new RequestSpecBuilder().
setBaseUri(ConfigsGlobal.BASE_URI).
setBasePath(ConfigsGlobal.BASE_PATH).
setContentType(ContentType.JSON).
setAccept(ContentType.JSON).
addFilter(new RequestLoggingFilter()).
addFilter(new ResponseLoggingFilter()).
log(LogDetail.ALL).
build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void testUpdateUser_PATCH() {
Gson gson = new Gson();

RequestSpecification request = given();
request.baseUri(ConfigsGlobal.URI)
request.baseUri(ConfigsGlobal.BASE_URI)
.accept("application/json")
.contentType("application/json")
.header("Authorization", "Bearer " + TokenGlobal.TOKEN)
Expand All @@ -57,7 +57,7 @@ public void testUpdateUser_PATCH_Builder() {
Gson gson = new Gson();

RequestSpecification request = given();
request.baseUri(ConfigsGlobal.URI)
request.baseUri(ConfigsGlobal.BASE_URI)
.accept("application/json")
.contentType("application/json")
.header("Authorization", "Bearer " + TokenGlobal.TOKEN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void validateJsonSchema_GetBookById() {

// Perform the API request and get the response
Response response = given()
.baseUri(ConfigsGlobal.URI)
.baseUri(ConfigsGlobal.BASE_URI)
.when()
.get("/book/10")
.then()
Expand All @@ -44,7 +44,7 @@ public void validateJsonSchema_GetBookAll() {

// Perform the API request and get the response
Response response = given()
.baseUri(ConfigsGlobal.URI)
.baseUri(ConfigsGlobal.BASE_URI)
.when()
.get("/books")
.then()
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/anhtester/Bai13_ThucHanh/BookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void testAddNewBook(){
Gson gson = new Gson();

RequestSpecification request = given();
request.baseUri(ConfigsGlobal.URI)
request.baseUri(ConfigsGlobal.BASE_URI)
.accept(ContentType.JSON)
.contentType(ContentType.JSON)
.header("Authorization", "Bearer " + TokenGlobal.TOKEN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void testAddNewBook() {
Gson gson = new Gson();

RequestSpecification request = given();
request.baseUri(ConfigsGlobal.URI)
request.baseUri(ConfigsGlobal.BASE_URI)
.accept(ContentType.JSON)
.contentType(ContentType.JSON)
.header("Authorization", "Bearer " + TokenGlobal.TOKEN)
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/anhtester/Bai17_Keyword/BookTest_Keyword.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.anhtester.Bai17_Keyword;

import com.anhtester.common.BaseTest;
import com.anhtester.globals.EndPointGlobal;
import com.anhtester.keywords.ApiKeyword;
import com.anhtester.utils.LogUtils;
import io.restassured.response.Response;
import org.testng.annotations.Test;

public class BookTest_Keyword extends BaseTest {

@Test
public void testGetBooks(){
Response response = ApiKeyword.get(EndPointGlobal.EP_BOOKS);
ApiKeyword.verifyStatusCode(response, 200);
LogUtils.info(ApiKeyword.getResponseKeyValue(response, "response[0].name"));
}
}
Loading

0 comments on commit 446c08e

Please sign in to comment.