diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index f998dd5..c66eeb4 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -9,6 +9,9 @@ on: - master - issue-* +permissions: + contents: write + jobs: build_and_test: name: Build and Test @@ -16,80 +19,22 @@ jobs: steps: - name: checkout Git repository - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Install Java and Maven - uses: actions/setup-java@v2 + uses: actions/setup-java@v4 with: - java-version: '15' + java-version: '21' distribution: 'adopt' cache: maven - - name: Build the Project - run: mvn clean install -DskipTests - - - name: Test Execution - run: mvn org.jacoco:jacoco-maven-plugin:prepare-agent install -Pcoverage-per-test - - - name: Upload target folder - uses: actions/upload-artifact@v2 - with: - name: target - path: | - ${{ github.workspace }}/target - ${{ github.workspace }}/reports - code_analysis: - name: Code Analysis - needs: - - build_and_test - - runs-on: ubuntu-latest - - steps: - - name: checkout Git repository - uses: actions/checkout@v2 - with: - fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis - - - name: Install Java and Maven - uses: actions/setup-java@v2 - with: - java-version: '15' - distribution: 'adopt' - cache: maven - - - name: Cache SonarCloud packages - uses: actions/cache@v2 - with: - path: ~/.sonar/cache - key: ${{ runner.os }}-sonar - restore-keys: ${{ runner.os }}-sonar - - - name: Cache Maven packages - uses: actions/cache@v2 - with: - path: ~/.m2 - key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} - restore-keys: ${{ runner.os }}-m2 - - - name: Download target folder - uses: actions/download-artifact@v2 - with: - name: target + - name: Build the Project and run tests + run: mvn clean install - - name: Sonar Code Analysis - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} - SONAR_KEY: ${{ secrets.SONAR_KEY }} - run: | - mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar \ - -Dsonar.projectKey=$SONAR_KEY - name: Test Report - uses: dorny/test-reporter@v1 + uses: dorny/test-reporter@v2 if: success() || failure() with: name: Test Results path: ${{ github.workspace }}/target/surefire-reports/TEST-TestSuite.xml - reporter: java-junit - java-version: 11 + reporter: java-junit \ No newline at end of file diff --git a/.gitignore b/.gitignore index eb1b62e..0ec06ee 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,40 @@ -/target/ -/test-output/ -/settings/ -/reports/ -/logs/ -/.settings/ -/.classpath -/.project -/.idea/ -/*.iml/ \ No newline at end of file +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ +test-output/ + +### IntelliJ IDEA ### +.idea/ +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/README.md b/README.md index fe33337..9841ae5 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,6 @@ -![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![Github Actions Workflow](https://github.com/mfaisalkhatri/OkHttpRestAssuredExamples/actions/workflows/maven.yml/badge.svg)](https://github.com/mfaisalkhatri/OkHttpRestAssuredExamples/actions/workflows/maven.yml) -[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=mfaisalkhatri_OkHttpRestAssuredExamples&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=mfaisalkhatri_OkHttpRestAssuredExamples) -[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=mfaisalkhatri_OkHttpRestAssuredExamples&metric=coverage)](https://sonarcloud.io/summary/new_code?id=mfaisalkhatri_OkHttpRestAssuredExamples) -[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=mfaisalkhatri_OkHttpRestAssuredExamples&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=mfaisalkhatri_OkHttpRestAssuredExamples) -[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=mfaisalkhatri_OkHttpRestAssuredExamples&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=mfaisalkhatri_OkHttpRestAssuredExamples) - ## Don't forget to give a :star: to make the project popular. @@ -17,10 +11,9 @@ I heard a lot about Rest-Assured and OkHttp and how it made the QA's life easier Hence, I started learning about these frameworks and have documented all my learnings in this repository. -Checkout my blog [API Testing using RestAssured and OkHttp][blog] where I talk about these frameworks in details +Checkout my blog [API Testing using RestAssured and OkHttp](https://mfaisalkhatri.github.io/2020/05/29/restassuredokhttp/) where I talk about these frameworks in details and which one to choose for testing your APIs. -To get a better understanding on API Testing, check [What is API Testing?][blog_apitesting] ## Details about this Project: @@ -31,18 +24,31 @@ To get a better understanding on API Testing, check [What is API Testing?][blog_ - Lombok has been used to generate Getter and Setters automatically for post body requests. - Rest APIs on https://reqres.in/ have been used for testing. +## :writing_hand: Blog Links + + - [What is API Testing?](https://mfaisalkhatri.github.io/2020/08/08/apitesting/) + - [End to End API Testing using Rest-Assured](https://medium.com/@iamfaisalkhatri/end-to-end-api-testing-using-rest-assured-a58c4ea80255) + +## :movie_camera: Tutorial Video + +[![Watch the video](https://img.youtube.com/vi/xLKpdQE0oKY/hqdefault.jpg)](https://www.youtube.com/watch?v=xLKpdQE0oKY&t=1s) +[![Watch the video](https://img.youtube.com/vi/AFQSolEeu74/hqdefault.jpg)](https://www.youtube.com/live/AFQSolEeu74?si=8WROMbunjUuzqqQj&t=1) + + ## :question: Need Assistance? -- Discuss your queries by writing to me at [mohammadfaisalkhatri@gmail.com][mail] or you can ping me on the following social media sites: -- Twitter: [mfaisal_khatri][twitter] -- LinkedIn: [Mohammad Faisal Khatri][linkedin] -- Contact me for 1:1 trainings related to Test Automation. +- Discuss your queries by writing to me @ `mohammadfaisalkhatri@gmail.com` + OR ping me on any of the social media sites using the below link: + - [Linktree](https://linktr.ee/faisalkhatri) + +## :computer: Paid Trainings + +Contact me for Paid trainings related to Test Automation and Software Testing, +mail me @`mohammadfaisalkhatri@gmail.com` or ping me on [LinkedIn](https://www.linkedin.com/in/faisalkhatri/) -## :thought_balloon: Checkout the blogs related to Testing on my [website][] +## :thought_balloon: Checkout the blogs related to Software Testing and Test Automation on the following links: +- [Medium Blogs](https://medium.com/@iamfaisalkhatri) +- [LambdaTest Blogs](https://www.lambdatest.com/blog/author/mfaisalkhatri/) +- [My Website](https://mfaisalkhatri.github.io) -[mail]: mohammadfaisalkhatri@gmail.com -[linkedin]: https://www.linkedin.com/in/faisalkhatri/ -[twitter]: https://twitter.com/mfaisal_khatri -[website]: https://mfaisalkhatri.github.io -[blog]: https://mfaisalkhatri.github.io/2020/05/29/restassuredokhttp/ -[blog_apitesting]: https://mfaisalkhatri.github.io/2020/08/08/apitesting/ \ No newline at end of file +## Subscribe to my [YouTube Channel](https://www.youtube.com/@faisalkhatriqa) \ No newline at end of file diff --git a/okhttppoc.iml b/okhttppoc.iml deleted file mode 100644 index 2c394a8..0000000 --- a/okhttppoc.iml +++ /dev/null @@ -1,58 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/pom.xml b/pom.xml index 43fbe06..bcddc62 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,7 @@ - 4.0.0 @@ -13,91 +13,83 @@ https://mfaisalkhatri.github.io - UTF-8 - 3.8.1 - 3.0.0-M5 - 15 + 7.11.0 + 4.9.3 + 5.5.5 + 1.3 + 2.25.1 + 2.25.1 + 1.1.1 + 1.18.38 + 2.19.2 + 20250517 + 1.0.2 + 3.14.0 + 3.5.3 + -Dfile.encoding=UTF-8 -Xdebug -Xnoagent + 3.8.1 + 17 UTF-8 testng.xml - -Dfile.encoding=UTF-8 -Xdebug -Xnoagent - https://sonarcloud.io - ${project.basedir}/target/reports/jacoco.exec - mfaisalkhatri - ${project.basedir}/reports/jacoco.exec - ${project.basedir}/target/site/jacoco/jacoco.xml - - 15 - 0.8.7 - 5.14.0.18788 - target/dependency/*.jar - 3.2.0 + UTF-8 - - org.testng testng - 7.5 + ${testng.version} - com.squareup.okhttp3 okhttp - 4.9.3 + ${okhttp3.version} + provided io.rest-assured rest-assured - 4.4.0 + ${restassured.version} - org.hamcrest hamcrest-all - 1.3 + ${hamcrest.version} - org.apache.logging.log4j log4j-core - 2.17.1 + ${log4j.core.version} - org.apache.logging.log4j log4j-api - 2.17.1 + ${log4j.api.version} - com.googlecode.json-simple json-simple - 1.1.1 + ${jsonsimple.version} - org.projectlombok lombok - 1.18.22 + ${lombok.version} provided - com.fasterxml.jackson.core jackson-databind - 2.13.4.1 + ${jackson.databind.version} - org.json json - 20211205 + ${orgjson.version} com.github.javafaker javafaker - 1.0.2 + ${javafaker.version} @@ -154,74 +146,6 @@ - - org.sonarsource.scanner.maven - sonar-maven-plugin - 3.9.1.2184 - - - org.jacoco - jacoco-maven-plugin - ${jacoco.version} - - - prepare-agent - - prepare-agent - - - ${sonar.report} - surefireArgLine - - - - report - test - - report - - - ${sonar.report} - ${project.reporting.outputDirectory}/jacoco - - - - - - - coverage-per-test - - - - org.apache.maven.plugins - maven-surefire-plugin - ${surefire-version} - - - - test - - - - - - ${suite-xml} - - ${argLine} ${surefireArgLine} - - - - - - - org.sonarsource.java - sonar-jacoco-listeners - ${sonar.version} - test - - - - \ No newline at end of file diff --git a/src/main/java/com/faisalkhatri/okhttppoc/AuthenticationPojo.java b/src/main/java/com/faisalkhatri/okhttppoc/AuthenticationPojo.java index 37dc857..0448082 100644 --- a/src/main/java/com/faisalkhatri/okhttppoc/AuthenticationPojo.java +++ b/src/main/java/com/faisalkhatri/okhttppoc/AuthenticationPojo.java @@ -21,7 +21,6 @@ /** * @author Faisal Khatri * @since Aug 2, 2020 - * */ @Data public class AuthenticationPojo { @@ -30,9 +29,10 @@ public class AuthenticationPojo { private String password; /** - *@author Faisal Khatri - *@param email - *@param password + * @param email + * @param password + * + * @author Faisal Khatri */ public AuthenticationPojo (String email, String password) { this.email = email; diff --git a/src/main/java/com/faisalkhatri/okhttppoc/PostData.java b/src/main/java/com/faisalkhatri/okhttppoc/PostData.java index b66a20d..8e669fa 100644 --- a/src/main/java/com/faisalkhatri/okhttppoc/PostData.java +++ b/src/main/java/com/faisalkhatri/okhttppoc/PostData.java @@ -21,25 +21,23 @@ /** * @since Mar 7, 2020 - * */ @Getter @Setter public class PostData { - private final String name; - private final String job; + private final String job; + private final String name; - /** - * - * @author Faisal Khatri - * @param name - * @param job - */ - public PostData (final String name, final String job) { - this.name = name; - this.job = job; - - } + /** + * @param name + * @param job + * + * @author Faisal Khatri + */ + public PostData (final String name, final String job) { + this.name = name; + this.job = job; + } } \ No newline at end of file diff --git a/src/main/java/com/faisalkhatri/okhttppoc/TestListener.java b/src/main/java/com/faisalkhatri/okhttppoc/TestListener.java index a2a34f4..99af70e 100644 --- a/src/main/java/com/faisalkhatri/okhttppoc/TestListener.java +++ b/src/main/java/com/faisalkhatri/okhttppoc/TestListener.java @@ -28,15 +28,18 @@ * @since Mar 8, 2020 */ public class TestListener implements ITestListener { - Logger log = LogManager.getLogger (TestListener.class); - private void logMessage (final String message) { - this.log.info ("\n"); - this.log.info (repeat ("=", 75)); - this.log.info (message); - this.log.info (repeat ("=", 75)); - this.log.info ("\n"); + @Override + public void onFinish (final ITestContext context) { + logMessage ("Test Execution Completed Successfully for all tests!!" + context.getSuite () + .getAllMethods ()); + + } + + @Override + public void onTestFailure (final ITestResult result) { + logMessage ("Test Failed!!!!" + result.getName ()); } @Override @@ -50,16 +53,11 @@ public void onTestSuccess (final ITestResult result) { } - @Override - public void onTestFailure (final ITestResult result) { - logMessage ("Test Failed!!!!" + result.getName ()); - } - - @Override - public void onFinish (final ITestContext context) { - logMessage ("Test Execution Completed Successfully for all tests!!" + context.getSuite () - .getAllMethods ()); - + private void logMessage (final String message) { + this.log.info ("\n"); + this.log.info (repeat ("=", 75)); + this.log.info (message); + this.log.info (repeat ("=", 75)); + this.log.info ("\n"); } - } \ No newline at end of file diff --git a/src/test/java/com/faisalkhatri/okhttppoc/SetupConfig.java b/src/test/java/com/faisalkhatri/okhttppoc/SetupConfig.java index 3a01348..81c0c67 100644 --- a/src/test/java/com/faisalkhatri/okhttppoc/SetupConfig.java +++ b/src/test/java/com/faisalkhatri/okhttppoc/SetupConfig.java @@ -16,6 +16,8 @@ package com.faisalkhatri.okhttppoc; +import static org.hamcrest.Matchers.lessThan; + import io.restassured.RestAssured; import io.restassured.builder.RequestSpecBuilder; import io.restassured.builder.ResponseSpecBuilder; @@ -25,24 +27,19 @@ import io.restassured.specification.ResponseSpecification; import org.testng.annotations.BeforeClass; -import static org.hamcrest.Matchers.lessThan; - public class SetupConfig { @BeforeClass - public void setup() { + public void setup () { RestAssured.baseURI = "https://reqres.in/"; - - RequestSpecification request = new RequestSpecBuilder() - .addHeader("Content-Type", "application/json") - .addHeader("Accept", "application/json") - .addFilter(new RequestLoggingFilter()) - .addFilter(new ResponseLoggingFilter()) - .build(); - - ResponseSpecification response = new ResponseSpecBuilder() - .expectResponseTime(lessThan(5000L)) - .build(); + RequestSpecification request = new RequestSpecBuilder ().addHeader ("Content-Type", "application/json") + .addHeader ("Accept", "application/json") + .addFilter (new RequestLoggingFilter ()) + .addFilter (new ResponseLoggingFilter ()) + .build (); + + ResponseSpecification response = new ResponseSpecBuilder ().expectResponseTime (lessThan (5000L)) + .build (); RestAssured.requestSpecification = request; RestAssured.responseSpecification = response; diff --git a/src/test/java/com/faisalkhatri/okhttppoc/TestAuthentication.java b/src/test/java/com/faisalkhatri/okhttppoc/TestAuthentication.java index cbea9ec..5d0141f 100644 --- a/src/test/java/com/faisalkhatri/okhttppoc/TestAuthentication.java +++ b/src/test/java/com/faisalkhatri/okhttppoc/TestAuthentication.java @@ -39,54 +39,18 @@ public class TestAuthentication { private static final String URL = "https://reqres.in"; /** - * @author Faisal Khatri - * @since Aug 2, 2020 - * @return test data - */ - @DataProvider - public Iterator getAuthenticationData () { - final List getTestData = new ArrayList<> (); - getTestData.add (new Object [] { "eve.holt@reqres.in", "pistol" }); - return getTestData.iterator (); - } - - /** - * @author Faisal Khatri - * @since Aug 2, 2020 * @param email * @param password - */ - @Test (dataProvider = "getAuthenticationData") - public void testAuthenticationToken (String email, String password) { - final AuthenticationPojo requestBody = new AuthenticationPojo (email, password); - - given ().contentType (ContentType.JSON) - .body (requestBody) - .when () - .log () - .all () - .post (URL + "/api/register") - .then () - .assertThat () - .statusCode (200) - .log () - .all () - .body ("id", notNullValue ()) - .and () - .body ("token", notNullValue ()); - - } - - /** + * + * @return auth details + * * @author Faisal Khatri * @since Aug 2, 2020 - * @param email - * @param password - * @return auth details */ public static Map getToken (String email, String password) { final AuthenticationPojo requestBody = new AuthenticationPojo (email, password); final String response = given ().contentType (ContentType.JSON) + .header ("x-api-key", "reqres-free-v1") .body (requestBody) .when () .log () @@ -113,10 +77,24 @@ public static Map getToken (String email, String password) { } /** + * @return test data + * * @author Faisal Khatri * @since Aug 2, 2020 + */ + @DataProvider + public Iterator getAuthenticationData () { + final List getTestData = new ArrayList<> (); + getTestData.add (new Object[] { "eve.holt@reqres.in", "pistol" }); + return getTestData.iterator (); + } + + /** * @param email * @param password + * + * @author Faisal Khatri + * @since Aug 2, 2020 */ @Test (dataProvider = "getAuthenticationData") public void testAuthToken (String email, String password) { @@ -124,4 +102,33 @@ public void testAuthToken (String email, String password) { .toString ()); } + + /** + * @param email + * @param password + * + * @author Faisal Khatri + * @since Aug 2, 2020 + */ + @Test (dataProvider = "getAuthenticationData") + public void testAuthenticationToken (String email, String password) { + final AuthenticationPojo requestBody = new AuthenticationPojo (email, password); + + given ().contentType (ContentType.JSON) + .header ("x-api-key", "reqres-free-v1") + .body (requestBody) + .when () + .log () + .all () + .post (URL + "/api/register") + .then () + .assertThat () + .statusCode (200) + .log () + .all () + .body ("id", notNullValue ()) + .and () + .body ("token", notNullValue ()); + + } } \ No newline at end of file diff --git a/src/test/java/com/faisalkhatri/okhttppoc/TestDeleteRequests.java b/src/test/java/com/faisalkhatri/okhttppoc/TestDeleteRequests.java index 371f9b4..54feff5 100644 --- a/src/test/java/com/faisalkhatri/okhttppoc/TestDeleteRequests.java +++ b/src/test/java/com/faisalkhatri/okhttppoc/TestDeleteRequests.java @@ -38,38 +38,59 @@ public class TestDeleteRequests { private static final String URL = "https://reqres.in/api/users/"; /** + * @return deleteUserData using rest assured + * * @since Mar 8, 2020 - * @return deleteUserData using okhttp */ - @DataProvider (name = "deleteUserOkHttp") - public Iterator deleteokHttpUsers () { - final List deleteData = new ArrayList<> (); - deleteData.add (new Object [] { 8 }); + @DataProvider (name = "deleteUserRestAssured") + public Iterator deleteRestUsers () { + final List deleteData = new ArrayList<> (); + deleteData.add (new Object[] { 2 }); return deleteData.iterator (); } /** + * @return deleteUserData using okhttp + * * @since Mar 8, 2020 - * @return deleteUserData using rest assured */ - @DataProvider (name = "deleteUserRestAssured") - public Iterator deleteRestUsers () { - final List deleteData = new ArrayList<> (); - deleteData.add (new Object [] { 2 }); + @DataProvider (name = "deleteUserOkHttp") + public Iterator deleteokHttpUsers () { + final List deleteData = new ArrayList<> (); + deleteData.add (new Object[] { 8 }); return deleteData.iterator (); } + /** + * Executing delete requests using Rest-assured + * + * @param userId + * + * @since Mar 08, 2020 + */ + @Test (dataProvider = "deleteUserRestAssured", groups = "DeleteTests") + public void testDeleUsingRestAsured (final int userId) { + given ().when () + .header ("x-api-key", "reqres-free-v1") + .delete (URL + userId) + .then () + .assertThat () + .statusCode (204); + } + /** * Executing delete request using okhttp * - * @since Mar 8, 2020 * @param userId + * * @throws IOException + * @since Mar 8, 2020 */ @Test (dataProvider = "deleteUserOkHttp", groups = "DeleteTests") public void testDeleteUsingOkHttp (final int userId) throws IOException { final OkHttpClient client = new OkHttpClient (); final Request request = new Request.Builder ().url (URL + userId) + .header ("x-api-key", "reqres-free-v1") .delete () .build (); @@ -78,20 +99,4 @@ public void testDeleteUsingOkHttp (final int userId) throws IOException { final int statusCode = response.code (); assertEquals (statusCode, 204); } - - /** - * Executing delete requests using Rest-assured - * - * @since Mar 08, 2020 - * @param userId - */ - @Test (dataProvider = "deleteUserRestAssured", groups = "DeleteTests") - public void testDeleUsingRestAsured (final int userId) { - given ().when () - .delete (URL + userId) - .then () - .assertThat () - .statusCode (204); - } - } \ No newline at end of file diff --git a/src/test/java/com/faisalkhatri/okhttppoc/TestGetRequestWithRestAssuredConfig.java b/src/test/java/com/faisalkhatri/okhttppoc/TestGetRequestWithRestAssuredConfig.java index 63137cc..7ca1b07 100644 --- a/src/test/java/com/faisalkhatri/okhttppoc/TestGetRequestWithRestAssuredConfig.java +++ b/src/test/java/com/faisalkhatri/okhttppoc/TestGetRequestWithRestAssuredConfig.java @@ -26,6 +26,7 @@ public class TestGetRequestWithRestAssuredConfig extends SetupConfig { @Test(groups = "GetTests") public void testGetRequestwithRestAssured() { given() + .header ("x-api-key", "reqres-free-v1") .when() .get("/api/users/2") .then() diff --git a/src/test/java/com/faisalkhatri/okhttppoc/TestGetRequests.java b/src/test/java/com/faisalkhatri/okhttppoc/TestGetRequests.java index 1f18f2f..5c29037 100644 --- a/src/test/java/com/faisalkhatri/okhttppoc/TestGetRequests.java +++ b/src/test/java/com/faisalkhatri/okhttppoc/TestGetRequests.java @@ -41,17 +41,18 @@ */ public class TestGetRequests { - Logger log = LogManager.getLogger (TestGetRequests.class); private static final String URL = "https://reqres.in/api/users/"; + Logger log = LogManager.getLogger (TestGetRequests.class); /** - * @since Mar 7, 2020 * @return getUserData + * + * @since Mar 7, 2020 */ @DataProvider (name = "getUserData") - public Iterator getUsers () { - final List getData = new ArrayList<> (); - getData.add (new Object [] { 2 }); + public Iterator getUsers () { + final List getData = new ArrayList<> (); + getData.add (new Object[] { 2 }); return getData.iterator (); } @@ -59,13 +60,15 @@ public class TestGetRequests { * Executing get request using okhttp * * @param userId - * @since Mar 7, 2020 + * * @throws IOException + * @since Mar 7, 2020 */ @Test (dataProvider = "getUserData", groups = "GetTests") public void testGetRequestWithOkHttp (final int userId) throws IOException { final OkHttpClient client = new OkHttpClient (); final Request request = new Request.Builder ().url (URL + userId) + .header ("x-api-key", "reqres-free-v1") .get () .build (); @@ -85,40 +88,48 @@ public void testGetRequestWithOkHttp (final int userId) throws IOException { } /** - * Executing get requests using Rest-assured + * @param userPage * - * @param userId + * @throws IOException * @since Mar 7, 2020 */ @Test (dataProvider = "getUserData", groups = "GetTests") - public void testGetRequestWithRestAssured (final int userId) { - given ().when () - .get (URL + userId) - .then () - .statusCode (200) - .and () - .assertThat () - .body ("data.id", equalTo (userId)); + public void testGetRequestWithQueryParamOkHttp (final int userPage) throws IOException { + final OkHttpClient client = new OkHttpClient (); + final HttpUrl.Builder urlBuilder = HttpUrl.parse (URL) + .newBuilder (); + urlBuilder.addQueryParameter ("page", String.valueOf (userPage)); - final int statusCode = given ().when () - .get (URL + userId) - .statusCode (); + final String currentUrl = urlBuilder.build () + .toString (); + final Request request = new Request.Builder ().url (currentUrl).header ("x-api-key", "reqres-free-v1") + .build (); + final Response response = client.newCall (request) + .execute (); + final String responseBody = response.body () + .string (); + final int statusCode = response.code (); this.log.info (statusCode); - - final String responseBody = given ().when () - .get (URL + userId) - .getBody () - .asString (); this.log.info (responseBody); + assertEquals (statusCode, 200); + final JSONObject jsonResponse = new JSONObject (responseBody); + assertThat (jsonResponse.getInt ("page"), equalTo (userPage)); + final String firstName = jsonResponse.getJSONArray ("data") + .getJSONObject (0) + .getString ("first_name"); + assertThat (firstName, equalTo ("Michael")); + } /** - * @since Mar 7, 2020 * @param userPage + * + * @since Mar 7, 2020 */ @Test (dataProvider = "getUserData", groups = "GetTests") public void testGetRequestWithQueryParamWithRestAssured (final int userPage) { given ().when () + .header ("x-api-key", "reqres-free-v1") .queryParam ("page", userPage) .get (URL) .then () @@ -136,35 +147,32 @@ public void testGetRequestWithQueryParamWithRestAssured (final int userPage) { } /** + * Executing get requests using Rest-assured + * + * @param userId + * * @since Mar 7, 2020 - * @param userPage - * @throws IOException */ @Test (dataProvider = "getUserData", groups = "GetTests") - public void testGetRequestWithQueryParamOkHttp (final int userPage) throws IOException { - final OkHttpClient client = new OkHttpClient (); - final HttpUrl.Builder urlBuilder = HttpUrl.parse (URL) - .newBuilder (); - urlBuilder.addQueryParameter ("page", String.valueOf (userPage)); + public void testGetRequestWithRestAssured (final int userId) { + given ().when () + .header ("x-api-key", "reqres-free-v1") + .get (URL + userId) + .then () + .statusCode (200) + .and () + .assertThat () + .body ("data.id", equalTo (userId)); - final String currentUrl = urlBuilder.build () - .toString (); - final Request request = new Request.Builder ().url (currentUrl) - .build (); - final Response response = client.newCall (request) - .execute (); - final String responseBody = response.body () - .string (); - final int statusCode = response.code (); + final int statusCode = given ().when () + .get (URL + userId) + .statusCode (); this.log.info (statusCode); - this.log.info (responseBody); - assertEquals (statusCode, 200); - final JSONObject jsonResponse = new JSONObject (responseBody); - assertThat (jsonResponse.getInt ("page"), equalTo (userPage)); - final String firstName = jsonResponse.getJSONArray ("data") - .getJSONObject (0) - .getString ("first_name"); - assertThat (firstName, equalTo ("Michael")); + final String responseBody = given ().when () + .get (URL + userId) + .getBody () + .asString (); + this.log.info (responseBody); } } \ No newline at end of file diff --git a/src/test/java/com/faisalkhatri/okhttppoc/TestPatchRequests.java b/src/test/java/com/faisalkhatri/okhttppoc/TestPatchRequests.java index b00d7fd..281679e 100644 --- a/src/test/java/com/faisalkhatri/okhttppoc/TestPatchRequests.java +++ b/src/test/java/com/faisalkhatri/okhttppoc/TestPatchRequests.java @@ -43,64 +43,31 @@ */ public class TestPatchRequests { - Logger log = LogManager.getLogger (TestPatchRequests.class); private static final String URL = "https://reqres.in"; + Logger log = LogManager.getLogger (TestPatchRequests.class); /** - * @since Mar 8, 2020 * @return putData - */ - @DataProvider (name = "patchData") - public Iterator patchData () { - final List patchData = new ArrayList<> (); - patchData.add (new Object [] { 2, "Michael", "QA Lead" }); - patchData.add (new Object [] { 958, "Yuan", "Project Architect" }); - return patchData.iterator (); - } - - /** - * Executing Put Request using Rest Assured. * * @since Mar 8, 2020 - * @param id - * @param name - * @param job */ - @Test (dataProvider = "patchData", groups = "PatchTests") - public void testPatchWithRestAssured (final int id, final String name, final String job) { - - final PostData postData = new PostData (name, job); - final String response = given ().contentType (ContentType.JSON) - .body (postData) - .when () - .patch (URL + "/api/users/" + id) - .then () - .assertThat () - .statusCode (200) - .and () - .assertThat () - .body ("name", equalTo (name)) - .and () - .assertThat () - .body ("job", equalTo (job)) - .and () - .extract () - .response () - .body () - .asString (); - - this.log.info (response); - + @DataProvider (name = "patchData") + public Iterator patchData () { + final List patchData = new ArrayList<> (); + patchData.add (new Object[] { 2, "Michael", "QA Lead" }); + patchData.add (new Object[] { 958, "Yuan", "Project Architect" }); + return patchData.iterator (); } /** * Executing Put Request using OkHttp * - * @since Mar 8, 2020 * @param id * @param name * @param job + * * @throws IOException + * @since Mar 8, 2020 */ @Test (dataProvider = "patchData", groups = "PatchTests") public void testPatchWithOkHttp (final int id, final String name, final String job) throws IOException { @@ -114,6 +81,7 @@ public void testPatchWithOkHttp (final int id, final String name, final String j final Request request = new Request.Builder ().url (URL + "/api/users/" + id) .addHeader ("Content-Type", "application/json;charset=utf-8") + .header ("x-api-key", "reqres-free-v1") .patch (requestBody) .build (); @@ -132,7 +100,42 @@ public void testPatchWithOkHttp (final int id, final String name, final String j assertEquals (statusCode, 200); assertThat (jsonResponse.getString ("name"), equalTo (name)); assertThat (jsonResponse.getString ("job"), equalTo (job)); - } + /** + * Executing Put Request using Rest Assured. + * + * @param id + * @param name + * @param job + * + * @since Mar 8, 2020 + */ + @Test (dataProvider = "patchData", groups = "PatchTests") + public void testPatchWithRestAssured (final int id, final String name, final String job) { + + final PostData postData = new PostData (name, job); + final String response = given ().contentType (ContentType.JSON) + .header ("x-api-key", "reqres-free-v1") + .body (postData) + .when () + .patch (URL + "/api/users/" + id) + .then () + .assertThat () + .statusCode (200) + .and () + .assertThat () + .body ("name", equalTo (name)) + .and () + .assertThat () + .body ("job", equalTo (job)) + .and () + .extract () + .response () + .body () + .asString (); + + this.log.info (response); + + } } \ No newline at end of file diff --git a/src/test/java/com/faisalkhatri/okhttppoc/TestPostRequest.java b/src/test/java/com/faisalkhatri/okhttppoc/TestPostRequest.java index ea59e54..bc833ae 100644 --- a/src/test/java/com/faisalkhatri/okhttppoc/TestPostRequest.java +++ b/src/test/java/com/faisalkhatri/okhttppoc/TestPostRequest.java @@ -45,62 +45,30 @@ */ public class TestPostRequest { - Logger log = LogManager.getLogger (TestPostRequest.class); private static final String URL = "https://reqres.in"; + Logger log = LogManager.getLogger (TestPostRequest.class); /** - * @since Mar 7, 2020 * @return postData + * + * @since Mar 7, 2020 */ @DataProvider (name = "postData") - public Iterator postData () { - final List postData = new ArrayList<> (); - postData.add (new Object [] { "Rahul", "QA" }); - postData.add (new Object [] { "Jane", "Sr.Dev" }); - postData.add (new Object [] { "Albert", "Dev" }); - postData.add (new Object [] { "Johnny", "Project Manager" }); + public Iterator postData () { + final List postData = new ArrayList<> (); + postData.add (new Object[] { "Rahul", "QA" }); + postData.add (new Object[] { "Jane", "Sr.Dev" }); + postData.add (new Object[] { "Albert", "Dev" }); + postData.add (new Object[] { "Johnny", "Project Manager" }); return postData.iterator (); } /** * @param name * @param job - * @since Mar 7, 2020 - */ - @Test (dataProvider = "postData", groups = "PostTests") - public void testPostWithRestAssured (final String name, final String job) { - final PostData postData = new PostData (name, job); - final String response = given ().contentType (ContentType.JSON) - .body (postData) - .when () - .post (URL + "/api/users") - .then () - .assertThat () - .statusCode (201) - .and () - .assertThat () - .body ("name", equalTo (name)) - .and () - .assertThat () - .body ("job", equalTo (job)) - .and () - .assertThat () - .body ("id", notNullValue ()) - .and () - .extract () - .response () - .body () - .asString (); - - this.log.info (response); - - } - - /** - * @since Mar 7, 2020 - * @param name - * @param job + * * @throws IOException + * @since Mar 7, 2020 */ @Test (dataProvider = "postData", groups = "PostTests") public void testPostWithOkHttp (final String name, final String job) throws IOException { @@ -114,6 +82,7 @@ public void testPostWithOkHttp (final String name, final String job) throws IOEx final Request request = new Request.Builder ().url (URL + "/api/users") .addHeader ("Content-Type", "application/json;charset=utf-8") + .header ("x-api-key", "reqres-free-v1") .post (requestBody) .build (); @@ -137,10 +106,47 @@ public void testPostWithOkHttp (final String name, final String job) throws IOEx } /** + * @param name + * @param job + * * @since Mar 7, 2020 + */ + @Test (dataProvider = "postData", groups = "PostTests") + public void testPostWithRestAssured (final String name, final String job) { + final PostData postData = new PostData (name, job); + final String response = given ().contentType (ContentType.JSON) + .header ("x-api-key", "reqres-free-v1") + .body (postData) + .when () + .post (URL + "/api/users") + .then () + .assertThat () + .statusCode (201) + .and () + .assertThat () + .body ("name", equalTo (name)) + .and () + .assertThat () + .body ("job", equalTo (job)) + .and () + .assertThat () + .body ("id", notNullValue ()) + .and () + .extract () + .response () + .body () + .asString (); + + this.log.info (response); + + } + + /** * @param name * @param job + * * @throws IOException + * @since Mar 7, 2020 */ @Test (dataProvider = "postData", groups = "PostTests") public void testPostwithOkHttpForm (final String name, final String job) throws IOException { @@ -149,6 +155,7 @@ public void testPostwithOkHttpForm (final String name, final String job) throws .add ("job", job) .build (); final Request request = new Request.Builder ().url (URL + "/api/users") + .header ("x-api-key", "reqres-free-v1") .post (formBody) .build (); diff --git a/src/test/java/com/faisalkhatri/okhttppoc/TestPostRequestBuilderExample.java b/src/test/java/com/faisalkhatri/okhttppoc/TestPostRequestBuilderExample.java index 249903b..f9d3507 100644 --- a/src/test/java/com/faisalkhatri/okhttppoc/TestPostRequestBuilderExample.java +++ b/src/test/java/com/faisalkhatri/okhttppoc/TestPostRequestBuilderExample.java @@ -23,32 +23,34 @@ import data.UserData; import org.testng.annotations.Test; - public class TestPostRequestBuilderExample extends SetupConfig { - @Test - public void postUsingBuilderPatternWithRestAssured() { - - UserData userData = userDataBuilder(); - given() - .body(userData) - .when() - .post("/api/users") - .then() - .statusCode(201) - .and() - .assertThat() - .body("name", equalTo(userData.getName())) - .body("job", equalTo(userData.getJob())); + public void postUsingBuilderPatternWithRestAssured () { + + UserData userData = userDataBuilder (); + given () + .header ("x-api-key", "reqres-free-v1") + .body (userData) + .when () + .post ("/api/users") + .then () + .statusCode (201) + .and () + .assertThat () + .body ("name", equalTo (userData.getName ())) + .body ("job", equalTo (userData.getJob ())); } - private UserData userDataBuilder() { - Faker faker = Faker.instance(); - return UserData.builder().name(faker.name().firstName()).job(faker.company().profession()) - .build(); + private UserData userDataBuilder () { + Faker faker = Faker.instance (); + return UserData.builder () + .name (faker.name () + .firstName ()) + .job (faker.company () + .profession ()) + .build (); } - } \ No newline at end of file diff --git a/src/test/java/com/faisalkhatri/okhttppoc/TestPutRequests.java b/src/test/java/com/faisalkhatri/okhttppoc/TestPutRequests.java index fbca6f9..82c70fa 100644 --- a/src/test/java/com/faisalkhatri/okhttppoc/TestPutRequests.java +++ b/src/test/java/com/faisalkhatri/okhttppoc/TestPutRequests.java @@ -43,64 +43,31 @@ */ public class TestPutRequests { - Logger log = LogManager.getLogger (TestPutRequests.class); private static final String URL = "https://reqres.in"; + Logger log = LogManager.getLogger (TestPutRequests.class); /** - * @since Mar 8, 2020 * @return putData - */ - @DataProvider (name = "putData") - public Iterator putData () { - final List putData = new ArrayList<> (); - putData.add (new Object [] { 2, "Michael", "QA Lead" }); - putData.add (new Object [] { 958, "Yuan", "Project Architect" }); - return putData.iterator (); - } - - /** - * Executing Put Request using Rest Assured. * * @since Mar 8, 2020 - * @param id - * @param name - * @param job */ - @Test (dataProvider = "putData", groups = "PutTests") - public void testPutWithRestAssured (final int id, final String name, final String job) { - - final PostData postData = new PostData (name, job); - final String response = given ().contentType (ContentType.JSON) - .body (postData) - .when () - .put (URL + "/api/users/" + id) - .then () - .assertThat () - .statusCode (200) - .and () - .assertThat () - .body ("name", equalTo (name)) - .and () - .assertThat () - .body ("job", equalTo (job)) - .and () - .extract () - .response () - .body () - .asString (); - - this.log.info (response); - + @DataProvider (name = "putData") + public Iterator putData () { + final List putData = new ArrayList<> (); + putData.add (new Object[] { 2, "Michael", "QA Lead" }); + putData.add (new Object[] { 958, "Yuan", "Project Architect" }); + return putData.iterator (); } /** * Executing Put Request using OkHttp * - * @since Mar 8, 2020 * @param id * @param name * @param job + * * @throws IOException + * @since Mar 8, 2020 */ @Test (dataProvider = "putData", groups = "PutTests") public void testPutWithOkHttp (final int id, final String name, final String job) throws IOException { @@ -114,6 +81,7 @@ public void testPutWithOkHttp (final int id, final String name, final String job final Request request = new Request.Builder ().url (URL + "/api/users/" + id) .addHeader ("Content-Type", "application/json;charset=utf-8") + .header ("x-api-key", "reqres-free-v1") .put (requestBody) .build (); @@ -135,4 +103,41 @@ public void testPutWithOkHttp (final int id, final String name, final String job } + /** + * Executing Put Request using Rest Assured. + * + * @param id + * @param name + * @param job + * + * @since Mar 8, 2020 + */ + @Test (dataProvider = "putData", groups = "PutTests") + public void testPutWithRestAssured (final int id, final String name, final String job) { + + final PostData postData = new PostData (name, job); + final String response = given ().contentType (ContentType.JSON) + .header ("x-api-key", "reqres-free-v1") + .body (postData) + .when () + .put (URL + "/api/users/" + id) + .then () + .assertThat () + .statusCode (200) + .and () + .assertThat () + .body ("name", equalTo (name)) + .and () + .assertThat () + .body ("job", equalTo (job)) + .and () + .extract () + .response () + .body () + .asString (); + + this.log.info (response); + + } + } \ No newline at end of file diff --git a/src/test/java/data/UserData.java b/src/test/java/data/UserData.java index 6389be7..0d1ac24 100644 --- a/src/test/java/data/UserData.java +++ b/src/test/java/data/UserData.java @@ -23,6 +23,6 @@ @Builder public class UserData { - private String name; private String job; + private String name; } \ No newline at end of file diff --git a/testng.xml b/testng.xml index 23e77cb..3f3ad93 100644 --- a/testng.xml +++ b/testng.xml @@ -1,83 +1,80 @@ - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file