Skip to content

Commit

Permalink
Initial commit: Setup JavaRestAssuredTestNG project
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosvagnoni committed Nov 28, 2023
0 parents commit 182eb68
Show file tree
Hide file tree
Showing 15 changed files with 877 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
logs/
reports/

### IntelliJ IDEA ###
.idea/
*.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
109 changes: 109 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Automated API Testing with Java, REST Assured, and TestNG 🤖☕

This project provides a structure and tools for automated API testing using Java, REST Assured, and TestNG, following Data-Driven Testing (DDT) best practices and employing the Service Object Model design pattern.

## Testing Swagger Petstore User Management Endpoints 🧪

This suite of tests is focused on validating and testing CRUD (Create, Read, Update, Delete) operations related to user management within the Swagger Petstore API (Base URL: petstore.swagger.io/v2). Specifically, the tests interact with the following endpoints:

Create User (POST): Endpoint URL: https://petstore.swagger.io/v2/user
Get User (GET): Endpoint URL: https://petstore.swagger.io/v2/user/{username}
Update User (PUT): Endpoint URL: https://petstore.swagger.io/v2/user/{username}
Delete User (DELETE): Endpoint URL: https://petstore.swagger.io/v2/user/{username}

These tests are designed to ensure the functionality and correctness of these user management endpoints provided by the Swagger Petstore API. They interact with the defined URLs in the Routes class to perform comprehensive testing of user-related functionalities.

## Table of Contents 📑
- [Requirements](#requirements-)
- [Folder Structure](#folder-structure-)
- [Installation](#installation-)
- [Configuration](#configuration-)
- [Test Execution](#test-execution-)
- [Contact](#contact-)

## Requirements 📋

- JDK 21
- Lombok 1.18.30
- REST Assured 5.3.2
- TestNG 7.8.0

## Folder Structure 📂

- **pom.xml:** Maven configuration file specifying project dependencies.
- **run.bat:** Batch script for execution in a Windows environment.

### Directory "src/test/java/api"

#### Directory "endpoints"

- **Routes.java:** Defines URLs for CRUD operations related to user management.
- **UserEndPoints.java:** Encapsulates HTTP requests for user-related API endpoints.

#### Directory "payload"

- **User.java:** Class representing user data for API operations.

#### Directory "test"

- **DDTests.java:** Test class implementing Data-Driven Testing (DDT) methodologies.
- **testng.xml:** TestNG configuration file for test execution.
- **UserTests.java:** Contains test methods to perform CRUD operations on user endpoints using generated/fake data.

#### Directory "utilities"

- **DataProviders.java:** Utility class providing data for tests.
- **ExtentReportManager.java:** Manages Extent reports for test execution.
- **XLUtility.java:** Utility class for handling Excel file operations.

### Directory "resources"

- **log4j2.xml:** Configuration file for Log4j2 for logging purposes.

### Directory "testData"

- **Userdata.xlsx:** Excel file containing user data used in testing.

## Installation 🛠️

1. Clone this repository:

```bash
git clone https://github.com/carlosvagnoni/JavaRestAssuredTestNG.git
cd JavaRestAssuredTestNG
```

2. Compile the project:

```bash
mvn clean compile
```

## Configuration ⚙️

- Userdata.xlsx can be modified to add any necessary data. The file accepts an arbitrary amount of data, accommodating any quantity of entries.

## Test Execution ▶️

Run all the tests:

```bash
mvn test
```

Open report:

```bash
start "" "reports\Test-Report.html"
```

**NOTE:**

- Set up the respective environment variables beforehand.
- On Windows environments, you can directly execute the `run.bat` file.

## Contact 📧

If you have any questions or suggestions, feel free to contact me through my social media accounts.

Thank you for your interest in this project!
112 changes: 112 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>JavaRestAssuredTestNG</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/java/api/test/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.9</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20231013</version>
</dependency>
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>5.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.scribejava</groupId>
<artifactId>scribejava-apis</artifactId>
<version>8.3.3</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.22.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.22.0</version>
</dependency>
</dependencies>

</project>
16 changes: 16 additions & 0 deletions run.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@echo off

echo Compiling the project...
call mvn clean compile

echo Running automated tests...
call mvn test

echo Opening report...
for /F "delims=" %%I in ('dir /b /o:d reports\Test-Report-*.html') do set latestReport=reports\%%I
if defined latestReport (
start "" "%latestReport%"
) else (
echo No reports were found.
)
REM start "" "reports\Test-Report-2023.11.28.13.35.23.html"
22 changes: 22 additions & 0 deletions src/test/java/api/endpoints/Routes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package api.endpoints;

/**
Defines URLs for CRUD operations related to user management in the pet store API.
Swagger URL --> https://petstore.swagger.io
Create user (Post): https://petstore.swagger.io/v2/user
Get user (Get): https://petstore.swagger.io/v2/user/{username}
Update user (Put): https://petstore.swagger.io/v2/user/{username}
Delete user (Delete): https://petstore.swagger.io/v2/user/{username}
*/
public class Routes {
// Base URL for the pet store API
public static String base_url = "https://petstore.swagger.io/v2";

// User module endpoints
public static String post_url = base_url + "/user";
public static String get_url = base_url + "/user/{username}";
public static String update_url = base_url + "/user/{username}";
public static String delete_url = base_url + "/user/{username}";
}
60 changes: 60 additions & 0 deletions src/test/java/api/endpoints/UserEndPoints.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package api.endpoints;

import api.payload.User;
import io.restassured.http.ContentType;
import io.restassured.response.Response;

import static io.restassured.RestAssured.given;

/**
Handles user-related API endpoints by encapsulating HTTP requests using RestAssured.
This class provides methods to perform CRUD (Create, Read, Update, Delete) operations on user data.
*/
public class UserEndPoints {

// Creates a new user via POST request
public static Response createUser(User payload) {
Response response = given()
.contentType(ContentType.JSON)
.accept(ContentType.JSON)
.body(payload)
.when()
.post(Routes.post_url);

return response;
}

// Retrieves user details via GET request
public static Response readUser(String userName) {
Response response = given()
.pathParam("username", userName)
.when()
.get(Routes.get_url);

return response;
}

// Updates user information via PUT request
public static Response updateUser(String userName, User payload) {
Response response = given()
.contentType(ContentType.JSON)
.accept(ContentType.JSON)
.pathParam("username", userName)
.body(payload)
.when()
.put(Routes.update_url);

return response;
}

// Deletes a user via DELETE request
public static Response deleteUser(String userName) {
Response response = given()
.pathParam("username", userName)
.when()
.delete(Routes.delete_url);

return response;
}
}
22 changes: 22 additions & 0 deletions src/test/java/api/payload/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package api.payload;

import lombok.Getter;
import lombok.Setter;

/**
* Represents a user entity for interaction with the API.
* This class models user data including their ID, username, name, email, password, phone, and user status.
* Lombok annotations (@Getter and @Setter) auto-generate getter and setter methods for class fields.
*/
@Getter
@Setter
public class User {
int id;
String username;
String firstName;
String lastName;
String email;
String password;
String phone;
int userStatus = 0;
}
Loading

0 comments on commit 182eb68

Please sign in to comment.