Skip to content

Commit

Permalink
feat: integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jpdev01 committed Dec 28, 2024
1 parent 7635d81 commit bcf398e
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions config.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test.asaas.api.token=$aact_YTU5YTE0M2M2N2I4MTliNzk0YTI5N2U5MzdjNWZmNDQ6OjAwMDAwMDAwMDAwMDAwNDAyMjQ6OiRhYWNoXzBkNTZkYmI5LTQ1ZDUtNDIyNi04NWVhLTY4NTFhODQ0YzZlOA==
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
<artifactId>jackson-databind</artifactId>
<version>2.16.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.11.0-M2</version>
<scope>test</scope>
</dependency>
</dependencies>

<distributionManagement>
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/io/github/jpdev/asaassdk/AsaasClientMock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.jpdev.asaassdk;

import io.github.jpdev.asaassdk.http.Asaas;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class AsaasClientMock {

public static void create() {
Asaas.initSandbox(getApiToken());
}

private static String getApiToken() {
Properties properties = new Properties();
try (FileInputStream input = new FileInputStream("config.properties")) {
properties.load(input);
} catch (IOException e) {
throw new RuntimeException("Erro ao carregar o arquivo de configuração.", e);
}
return properties.getProperty("test.asaas.api.token");
}
}
34 changes: 34 additions & 0 deletions src/test/java/io/github/jpdev/asaassdk/http/AsaasTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.github.jpdev.asaassdk.http;

import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.junit.jupiter.api.Assertions.*;

import io.github.jpdev.asaassdk.AsaasClientMock;
import io.github.jpdev.asaassdk.exception.ConnectionException;
import io.github.jpdev.asaassdk.rest.payment.Payment;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class AsaasTest {

@BeforeAll
static void setup() {
AsaasClientMock.create();
}

@Test
@DisplayName("Teste de token informado")
void testInformedToken() {
assumeTrue(Asaas.getRestClient() != null, "Token da API não configurado.");
}

@Test
@DisplayName("Teste de token incorreto")
void testIncorrectToken() {
assumeTrue(Asaas.getRestClient() != null, "Token da API não configurado.");

Asaas.setToken("incorrectToken");
assertThrowsExactly(ConnectionException.class, () -> Payment.reader().read());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.github.jpdev.asaassdk.rest.pix.transaction;

import io.github.jpdev.asaassdk.AsaasClientMock;
import io.github.jpdev.asaassdk.http.Asaas;
import io.github.jpdev.asaassdk.rest.action.ResourceSet;
import io.github.jpdev.asaassdk.rest.pix.enums.PixAddressKeyType;
import io.github.jpdev.asaassdk.rest.transfer.Transfer;
import io.github.jpdev.asaassdk.rest.transfer.children.response.TransferOperationType;
import io.github.jpdev.asaassdk.utils.Money;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

public class PixTransactionTest {

@BeforeAll
static void setup() {
AsaasClientMock.create();
}

@Test
@DisplayName("Integração | Criação de transação Pix com chave")
void testCreatePixKey() {
assumeTrue(Asaas.getRestClient() != null, "Token da API não configurado.");

BigDecimal value = BigDecimal.valueOf(0.01);

Transfer transfer = Transfer.pixAddressKeyCreator()
.setPixAddressKey("+5547999999999")
.setPixAddressKeyType(PixAddressKeyType.PHONE)
.setValue(value)
.setDescription("Teste unitário")
.create();

assertNotNull(transfer, "Transferência não criada.");
assertEquals(value, Money.create(transfer.getValue()), "Valor da transferência incorreto.");
assertEquals("Teste unitário", transfer.getDescription(), "Descrição da transferência incorreta.");
assertEquals(TransferOperationType.PIX, transfer.getOperationType(), "Tipo de operação incorreto.");
}

@Test
@DisplayName("Integração | Leitura de transação Pix")
void testListing() {
assumeTrue(Asaas.getRestClient() != null, "Token da API não configurado.");

List<Transfer> transferList = Transfer.reader().read().getData();
assumeTrue(transferList != null && !transferList.isEmpty(), "Nenhuma transferência encontrada.");

Transfer transfer = transferList.stream().findFirst().orElse(null);

assertNotNull(transfer, "Transferência não encontrada.");
assertNotNull(transfer.getId(), "ID da transferência não encontrado.");
assertNotNull(transfer.getValue(), "Valor da transferência não encontrado.");
assertNotNull(transfer.getDateCreated());
}
}

0 comments on commit bcf398e

Please sign in to comment.