Skip to content

Commit

Permalink
Refatoração no projeto
Browse files Browse the repository at this point in the history
  • Loading branch information
thinogueiras committed Mar 1, 2024
1 parent 84195e8 commit 661028e
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 49 deletions.
Binary file added .github/images/allure-results.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 31 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,49 @@
# Testes de API com REST-assured e JUnit 5.

## Pré-requisitos de ambiente 📋 💻
## Pré-requisitos mínimos de ambiente:

[Java](https://www.oracle.com/br/java/technologies/downloads/#java11) 11.0.20.
* [Java](https://www.oracle.com/br/java/technologies/downloads/#java11) 11.0.x.

## Execução dos testes pelo CMD ✅ ✅
## IDE de desenvolvimento:

* [Eclipse](https://eclipseide.org).

## Clone do repositório:

```
git clone https://github.com/thinogueiras/REST-assured.git
```

## Abrir o projeto no Eclipse:

* Abra o Eclipse >> Menu File >> Import >> Maven >> Existing Maven Projects.
* Navegar até o repositório clonado >> Selecionar a pasta >> Finish.

## Execução dos testes:

### JUnit:

* Após importar o projeto com sucesso no Eclipse, clique para expandir o Source Folder: `src/test/java`, clique com o botão `direito` do mouse sobre o package: `br.qa.thinogueiras.suite` >> Run As >> JUnit Test.

### Terminal:

```
mvn clean test
```

## Relatório da execução 📝 📄
## Relatório:
* #### Abrir o terminal e digitar:

```
allure serve allure-results
```
```
allure serve allure-results
```

![Alt text](/images/allure-report.png)
![Alt text](/.github/images/allure-results.png)

---

<a href="https://www.linkedin.com/in/thinogueiras"><img alt="Linkedin" src="https://img.shields.io/badge/-LinkedIn-blue?style=for-the-badge&logo=Linkedin&logoColor=white"></a>

<strong>Thiago Nogueira dos Santos</strong> 🤓 🫰🏽
<strong>Thiago Nogueira dos Santos</strong> 🤓 ✌🏻

QA Automation Engineer 🔎 🐞
Binary file removed images/allure-report.png
Binary file not shown.
21 changes: 7 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.qa.thinogueiras</groupId>
<artifactId>REST-assured</artifactId>
Expand All @@ -18,7 +16,7 @@
<junit.jupiter.engine.version>5.10.0</junit.jupiter.engine.version>
<json-schema-validator.version>5.1.1</json-schema-validator.version>
<com.google.code.gson.version>2.9.1</com.google.code.gson.version>
<junit-platform-suite-engine.version>1.8.2</junit-platform-suite-engine.version>
<junit-platform-suite-engine.version>1.8.2</junit-platform-suite-engine.version>
<allure.junit5.version>2.19.0</allure.junit5.version>
<allure-maven.version>2.11.2</allure-maven.version>
<maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
Expand All @@ -32,11 +30,6 @@
<artifactId>rest-assured</artifactId>
<version>${rest-assured.version}</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>${json-schema-validator.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand All @@ -59,7 +52,7 @@
<artifactId>junit-platform-suite-engine</artifactId>
<version>${junit-platform-suite-engine.version}</version>
<scope>test</scope>
</dependency>
</dependency>
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-junit5</artifactId>
Expand All @@ -75,7 +68,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<testFailureIgnore>false</testFailureIgnore>
<testFailureIgnore>false</testFailureIgnore>
<properties>
<property>
<name>listener</name>
Expand Down Expand Up @@ -105,21 +98,21 @@
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.engine.version}</version>
</dependency>
</dependencies>
</plugin>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
Expand Down
5 changes: 1 addition & 4 deletions src/test/java/br/qa/thinogueiras/core/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static io.restassured.RestAssured.basePath;
import static io.restassured.RestAssured.baseURI;
import static io.restassured.RestAssured.enableLoggingOfRequestAndResponseIfValidationFails;
import static io.restassured.RestAssured.get;
import static io.restassured.RestAssured.port;
import static io.restassured.RestAssured.requestSpecification;
import static io.restassured.RestAssured.responseSpecification;
Expand All @@ -15,7 +14,7 @@
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;

public class BaseTest implements Constants {
public class BaseTest implements Constants {

@BeforeAll
public static void setup() {
Expand All @@ -34,7 +33,5 @@ public static void setup() {
enableLoggingOfRequestAndResponseIfValidationFails();

getToken();

get("/reset").then().statusCode(200);
}
}
33 changes: 18 additions & 15 deletions src/test/java/br/qa/thinogueiras/core/TokenFactory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package br.qa.thinogueiras.core;

import static br.qa.thinogueiras.utils.Utils.resetUserData;
import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.requestSpecification;

Expand All @@ -11,24 +12,10 @@

public class TokenFactory {

private static String TOKEN;
private static String TOKEN = "";
private static RequestSpecification HEADER;
private static FilterableRequestSpecification request;

public static String getToken() {
if(TOKEN == null || HEADER == null) {
generateToken();
}

return TOKEN;
}

public static void removeAuthorization() {
request = (FilterableRequestSpecification) requestSpecification;
request.removeHeader("Authorization");
HEADER = null;
}

private static void generateToken() {
Map<String, String> login = new HashMap<>();
login.put("email", "[email protected]");
Expand All @@ -45,4 +32,20 @@ private static void generateToken() {

HEADER = requestSpecification.header("Authorization", "JWT " + TOKEN);
}

public static String getToken() {
if(HEADER == null) {
generateToken();

resetUserData();;
}

return TOKEN;
}

public static void removeAuthorization() {
request = (FilterableRequestSpecification) requestSpecification;
request.removeHeader("Authorization");
HEADER = null;
}
}
20 changes: 16 additions & 4 deletions src/test/java/br/qa/thinogueiras/tests/AccountTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;

import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand All @@ -12,10 +15,15 @@
@DisplayName("Account Tests")
public class AccountTest extends BaseTest {

Map<String, String> payload = new HashMap<>();

@Test
public void shouldInsertAccount() {
public void shouldInsertAccount() {

payload.put("nome", "Conta corrente");

given()
.body("{\"nome\": \"Conta corrente\"}")
.body(payload)
.when()
.post("/contas")
.then()
Expand All @@ -26,8 +34,10 @@ public void shouldInsertAccount() {
public void shouldEditAccount() {
Integer ID_ACCOUNT = getAccountIdByName("Conta para alterar");

payload.put("nome", "Conta salário");

given()
.body("{\"nome\": \"Conta salário\"}")
.body(payload)
.pathParam("id", ID_ACCOUNT)
.when()
.put("/contas/{id}")
Expand All @@ -38,8 +48,10 @@ public void shouldEditAccount() {

@Test
public void shouldNotInsertAccountWithSameName() {
payload.put("nome", "Conta mesmo nome");

given()
.body("{\"nome\": \"Conta mesmo nome\"}")
.body(payload)
.when()
.post("/contas")
.then()
Expand Down
40 changes: 37 additions & 3 deletions src/test/java/br/qa/thinogueiras/tests/AuthTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static br.qa.thinogueiras.core.TokenFactory.removeAuthorization;
import static io.restassured.RestAssured.given;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand All @@ -11,15 +12,48 @@
@DisplayName("Authorization Tests")
public class AuthTest extends BaseTest {

@Test
public void shouldNotAccessWithoutToken() {

@BeforeAll
public static void authorization() {
removeAuthorization();
}

@Test
public void shouldNotAccessContasWithoutToken() {

given()
.when()
.get("/contas")
.then()
.statusCode(401);
}

@Test
public void shouldNotAccessSaldoWithoutToken() {

given()
.when()
.get("/saldo")
.then()
.statusCode(401);
}

@Test
public void shouldNotAccessTransacoesWithoutToken() {

given()
.when()
.get("/transacoes")
.then()
.statusCode(401);
}

@Test
public void shouldNotAccessMovimentacoesWithoutToken() {

given()
.when()
.get("/movimentacoes")
.then()
.statusCode(401);
}
}
9 changes: 9 additions & 0 deletions src/test/java/br/qa/thinogueiras/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package br.qa.thinogueiras.utils;

import static io.restassured.RestAssured.get;
import static io.restassured.RestAssured.given;

import br.qa.thinogueiras.core.Movement;

Expand Down Expand Up @@ -28,4 +29,12 @@ public static Integer getAccountIdByName(String name) {
public static Integer getMovementIdByDescription(String description) {
return get("/transacoes?descricao="+description).then().extract().path("id[0]");
}

public static void resetUserData() {
given()
.when()
.get("/reset")
.then()
.statusCode(200);
}
}

0 comments on commit 661028e

Please sign in to comment.