-
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 17 Common functions in class keyword
- Loading branch information
Showing
14 changed files
with
375 additions
and
27 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
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,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"; | ||
} |
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 |
---|---|---|
|
@@ -2,4 +2,8 @@ | |
|
||
public class TokenGlobal { | ||
public static String TOKEN; | ||
|
||
public static String getBearerToken() { | ||
return TOKEN; | ||
} | ||
} |
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,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."); | ||
} | ||
} |
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,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(); | ||
} | ||
} |
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
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
18 changes: 18 additions & 0 deletions
18
src/test/java/com/anhtester/Bai17_Keyword/BookTest_Keyword.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,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")); | ||
} | ||
} |
Oops, something went wrong.