Skip to content

Commit

Permalink
test: testing recurring
Browse files Browse the repository at this point in the history
  • Loading branch information
jpdev01 committed Dec 28, 2024
1 parent 6ce9a76 commit 24b3ac9
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/main/java/io/github/jpdev/asaassdk/http/Domain.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public enum Domain {
STATUS("myAccount/status"),
ACCOUNT("accounts"),
SUBSCRIPTION("subscriptions"),
WEBHOOK("webhooks");
WEBHOOK("webhooks"),
PIX_RECURRING("pix/transactions/recurrings");

private final String value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ public class PixRecurring {
PixRecurringFrequency frequency;
int quantity;

public static PixRecurringFetcher fetcher(String id) {
return new PixRecurringFetcher(id);
}

public PixRecurringFrequency getFrequency() {
return frequency;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.github.jpdev.asaassdk.rest.transfer.children;

import io.github.jpdev.asaassdk.http.Domain;
import io.github.jpdev.asaassdk.rest.action.Fetcher;

public class PixRecurringFetcher extends Fetcher<PixRecurring> {

private final String id;

public PixRecurringFetcher(String id) {
this.id = id;
}

@Override
public String getResourceUrl() {
return Domain.PIX_RECURRING.addPathVariable(id).toString();
}

@Override
public Class<PixRecurring> getResourceClass() {
return PixRecurring.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package integration.io.github.jpdev.asaassdk;

import io.github.jpdev.asaassdk.utils.Money;

import java.math.BigDecimal;
import java.util.Random;

public class CustomMockUtils {

public static BigDecimal randomValue() {
int value = new Random().nextInt(100) + 1;
return Money.create(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package integration.io.github.jpdev.asaassdk.rest.pix.recurring;

import integration.io.github.jpdev.asaassdk.AsaasClientMock;
import integration.io.github.jpdev.asaassdk.CustomMockUtils;
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.PixRecurring;
import io.github.jpdev.asaassdk.rest.transfer.children.PixRecurringFrequency;
import io.github.jpdev.asaassdk.utils.Money;
import org.junit.jupiter.api.*;

import java.math.BigDecimal;
import java.util.Random;

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

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class PixRecurringTest {

private static String recurringId;
private static PixRecurringFrequency recurringFrequency;
private static Integer recurringQuantity;

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

@Test
@Tag("integration")
@Order(1)
@DisplayName("Teste de criação de recorrência")
void save() {
BigDecimal value = CustomMockUtils.randomValue();
recurringFrequency = PixRecurringFrequency.MONTHLY;
recurringQuantity = new Random().nextInt(8) + 1;

PixRecurring recurring = new PixRecurring()
.setFrequency(PixRecurringFrequency.MONTHLY)
.setQuantity(2);

Transfer transfer = Transfer.pixAddressKeyCreator()
.setPixAddressKey("+5547999999999")
.setValue(value)
.setDescription("teste")
.setPixAddressKeyType(PixAddressKeyType.PHONE)
.setRecurring(recurring)
.create();

assertNotNull(transfer, "Transferência não criada.");
assertNotNull(transfer.getRecurring());

recurringId = transfer.getRecurring();
}

@Test
@Tag("integration")
@Order(2)
@DisplayName("Teste de leitura de recorrência")
void fetcher() {
assumeTrue(recurringId != null, "Recorrência não criada.");

PixRecurring recurring = PixRecurring.fetcher(recurringId)
.fetch();

assertNotNull(recurring, "Recorrência não encontrada.");
assertEquals(recurringFrequency, recurring.getFrequency(), "Frequência incorreta.");
assertEquals(recurringQuantity, recurring.getQuantity(), "Quantidade incorreta.");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package integration.io.github.jpdev.asaassdk.rest.pix.transaction;

import integration.io.github.jpdev.asaassdk.AsaasClientMock;
import integration.io.github.jpdev.asaassdk.CustomMockUtils;
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;
Expand All @@ -27,7 +28,7 @@ static void setup() {
@DisplayName("Integração | Criação de transação Pix com chave")
@Order(1)
void testCreatePixKey() {
BigDecimal value = buildValue();
BigDecimal value = CustomMockUtils.randomValue();

Transfer transfer = Transfer.pixAddressKeyCreator()
.setPixAddressKey("+5547999999999")
Expand Down Expand Up @@ -76,9 +77,4 @@ void testReadById() {
assertNotNull(transfer.getValue(), "Valor da transferência não encontrado.");
assertNotNull(transfer.getDateCreated());
}

private BigDecimal buildValue() {
int value = new Random().nextInt(100) + 1;
return Money.create(value);
}
}

0 comments on commit 24b3ac9

Please sign in to comment.