diff --git a/src/main/java/com/abacatepay/AbacatePay.java b/src/main/java/com/abacatepay/AbacatePay.java index 9925d76..1c68ea6 100644 --- a/src/main/java/com/abacatepay/AbacatePay.java +++ b/src/main/java/com/abacatepay/AbacatePay.java @@ -3,65 +3,33 @@ import com.abacatepay.clients.AbacatePayClient; import com.abacatepay.clients.factories.AbacatePayClientFactory; import com.abacatepay.model.IAbacatePay; -import com.abacatepay.model.IAbacatePayBilling; -import com.abacatepay.model.billing.CreateBillingData; -import com.abacatepay.model.billing.CreateBillingResponse; -import com.abacatepay.model.billing.ListBillingResponse; -import feign.FeignException; -import feign.RequestInterceptor; +import com.abacatepay.services.IAbacatePayBilling; +import com.abacatepay.services.IAbacatePayCustomer; +import com.abacatepay.services.impl.AbacatePayBilling; +import com.abacatepay.services.impl.AbacatePayCustomer; public class AbacatePay implements IAbacatePay { private static final String API_BASE_URL = "https://api.abacatepay.com/v1"; - private final AbacatePayClient client; - private final String apiKey; - private final String userAgent; public AbacatePay(String apiKey) { - this.apiKey = apiKey; - this.client = AbacatePayClientFactory.create(API_BASE_URL, requestInterceptor()); + if (apiKey == null || apiKey.isEmpty()) { + throw new IllegalArgumentException("API key not provided"); + } //TODO: Pegar a versão do SDK dinamicamente - this.userAgent = "Java SDK (1.0.0)"; - } - - private RequestInterceptor requestInterceptor() { - return template -> { - if (apiKey == null || apiKey.isEmpty()) { - throw new IllegalArgumentException("API key not provided"); - } - - template.header("Authorization", "Bearer " + apiKey); - template.header("Content-Type", "application/json"); - template.header("User-Agent", userAgent); - }; + String userAgent = "Java SDK (1.0.0)"; + this.client = AbacatePayClientFactory.create(API_BASE_URL, apiKey, userAgent); } @Override public IAbacatePayBilling billing() { + return new AbacatePayBilling(client); + } - class AbacatePayBilling implements IAbacatePayBilling { - - @Override - public CreateBillingResponse create(CreateBillingData data) { - try { - return client.create(data); - } catch (IllegalArgumentException | FeignException e) { - return new CreateBillingResponse(e.getMessage()); - } - } - - @Override - public ListBillingResponse list() { - try { - return client.list(); - } catch (IllegalArgumentException | FeignException e) { - return new ListBillingResponse(e.getMessage()); - } - } - } - - return new AbacatePayBilling(); + @Override + public IAbacatePayCustomer customer() { + return new AbacatePayCustomer(client); } } diff --git a/src/main/java/com/abacatepay/clients/AbacatePayClient.java b/src/main/java/com/abacatepay/clients/AbacatePayClient.java index cdd1470..65e098f 100644 --- a/src/main/java/com/abacatepay/clients/AbacatePayClient.java +++ b/src/main/java/com/abacatepay/clients/AbacatePayClient.java @@ -3,13 +3,22 @@ import com.abacatepay.model.billing.CreateBillingData; import com.abacatepay.model.billing.CreateBillingResponse; import com.abacatepay.model.billing.ListBillingResponse; +import com.abacatepay.model.customer.CreateCustomerResponse; +import com.abacatepay.model.customer.CustomerMetadata; +import com.abacatepay.model.customer.ListCustomerResponse; import feign.RequestLine; public interface AbacatePayClient { @RequestLine("GET /billing/list") - ListBillingResponse list(); + ListBillingResponse listBillings(); @RequestLine("POST /billing/create") - CreateBillingResponse create(CreateBillingData body); + CreateBillingResponse createBilling(CreateBillingData body); + + @RequestLine("POST /customer/create") + CreateCustomerResponse createCustomer(CustomerMetadata body); + + @RequestLine("GET /customer/list") + ListCustomerResponse listCustomers(); } diff --git a/src/main/java/com/abacatepay/clients/factories/AbacatePayClientFactory.java b/src/main/java/com/abacatepay/clients/factories/AbacatePayClientFactory.java index bc97ac5..05db596 100644 --- a/src/main/java/com/abacatepay/clients/factories/AbacatePayClientFactory.java +++ b/src/main/java/com/abacatepay/clients/factories/AbacatePayClientFactory.java @@ -13,16 +13,26 @@ public class AbacatePayClientFactory { private static final ObjectMapper MAPPER = new ObjectMapper(); - public static AbacatePayClient create( - String baseUrl, RequestInterceptor requestInterceptor - ) { + static { MAPPER.registerModule(new JavaTimeModule()); + } + public static AbacatePayClient create( + String baseUrl, String apiKey, String userAgent + ) { return Feign.builder() .decoder(new JacksonDecoder(MAPPER)) .encoder(new JacksonEncoder(MAPPER)) .errorDecoder(new CustomExceptionDecoder()) - .requestInterceptor(requestInterceptor) + .requestInterceptor(requestInterceptor(apiKey, userAgent)) .target(AbacatePayClient.class, baseUrl); } + + private static RequestInterceptor requestInterceptor(String apiKey, String userAgent) { + return template -> { + template.header("Authorization", "Bearer " + apiKey); + template.header("Content-Type", "application/json"); + template.header("User-Agent", userAgent); + }; + } } diff --git a/src/main/java/com/abacatepay/model/IAbacatePay.java b/src/main/java/com/abacatepay/model/IAbacatePay.java index ca7cde0..3446df2 100644 --- a/src/main/java/com/abacatepay/model/IAbacatePay.java +++ b/src/main/java/com/abacatepay/model/IAbacatePay.java @@ -1,5 +1,9 @@ package com.abacatepay.model; +import com.abacatepay.services.IAbacatePayBilling; +import com.abacatepay.services.IAbacatePayCustomer; + public interface IAbacatePay { IAbacatePayBilling billing(); + IAbacatePayCustomer customer(); } diff --git a/src/main/java/com/abacatepay/model/billing/Billing.java b/src/main/java/com/abacatepay/model/billing/Billing.java index 0c67bdc..afbcf72 100644 --- a/src/main/java/com/abacatepay/model/billing/Billing.java +++ b/src/main/java/com/abacatepay/model/billing/Billing.java @@ -1,5 +1,7 @@ package com.abacatepay.model.billing; +import com.abacatepay.model.customer.Customer; +import com.abacatepay.utils.DateUtils; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.Data; @@ -20,16 +22,10 @@ public class Billing { private List products; private BillingKind frequency; - @JsonFormat(timezone = "America/Sao_Paulo", pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") + @JsonFormat(timezone = DateUtils.DATE_TIMEZONE_DEFAULT, pattern = DateUtils.DATE_FORMAT_PATTERN) private LocalDateTime nextBilling; private Customer customer; - - @JsonFormat(timezone = "America/Sao_Paulo", pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") - private LocalDateTime createdAt; - - @JsonFormat(timezone = "America/Sao_Paulo", pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") - private LocalDateTime updatedAt; } @Data @@ -38,9 +34,3 @@ class Product { private Integer quantity; } -@Data -@JsonIgnoreProperties(ignoreUnknown = true) -class Customer { - private String id; - private CustomerMetadata metadata; -} \ No newline at end of file diff --git a/src/main/java/com/abacatepay/model/billing/CreateBillingResponse.java b/src/main/java/com/abacatepay/model/billing/CreateBillingResponse.java index aa29ec9..e64941c 100644 --- a/src/main/java/com/abacatepay/model/billing/CreateBillingResponse.java +++ b/src/main/java/com/abacatepay/model/billing/CreateBillingResponse.java @@ -1,18 +1,13 @@ package com.abacatepay.model.billing; -import com.fasterxml.jackson.annotation.JsonInclude; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor public class CreateBillingResponse { - - @JsonInclude(JsonInclude.Include.NON_NULL) private String error; - - @JsonInclude(JsonInclude.Include.NON_NULL) - private Billing billing; + private Billing data; public CreateBillingResponse(String error) { this.error = error; diff --git a/src/main/java/com/abacatepay/model/billing/ListBillingResponse.java b/src/main/java/com/abacatepay/model/billing/ListBillingResponse.java index a51256b..b4dca0a 100644 --- a/src/main/java/com/abacatepay/model/billing/ListBillingResponse.java +++ b/src/main/java/com/abacatepay/model/billing/ListBillingResponse.java @@ -1,6 +1,5 @@ package com.abacatepay.model.billing; -import com.fasterxml.jackson.annotation.JsonInclude; import lombok.Data; import lombok.NoArgsConstructor; @@ -9,11 +8,8 @@ @Data @NoArgsConstructor public class ListBillingResponse { - @JsonInclude(JsonInclude.Include.NON_NULL) private String error; - - @JsonInclude(JsonInclude.Include.NON_NULL) - private List billings; + private List data; public ListBillingResponse(String error) { this.error = error; diff --git a/src/main/java/com/abacatepay/model/customer/CreateCustomerResponse.java b/src/main/java/com/abacatepay/model/customer/CreateCustomerResponse.java new file mode 100644 index 0000000..b41f9cf --- /dev/null +++ b/src/main/java/com/abacatepay/model/customer/CreateCustomerResponse.java @@ -0,0 +1,15 @@ +package com.abacatepay.model.customer; + +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +public class CreateCustomerResponse { + private CustomerMetadata data; + private String error; + + public CreateCustomerResponse(String error) { + this.error = error; + } +} diff --git a/src/main/java/com/abacatepay/model/customer/Customer.java b/src/main/java/com/abacatepay/model/customer/Customer.java new file mode 100644 index 0000000..1f47840 --- /dev/null +++ b/src/main/java/com/abacatepay/model/customer/Customer.java @@ -0,0 +1,11 @@ +package com.abacatepay.model.customer; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.Data; + +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +public class Customer { + private String id; + private CustomerMetadata metadata; +} diff --git a/src/main/java/com/abacatepay/model/billing/CustomerMetadata.java b/src/main/java/com/abacatepay/model/customer/CustomerMetadata.java similarity index 70% rename from src/main/java/com/abacatepay/model/billing/CustomerMetadata.java rename to src/main/java/com/abacatepay/model/customer/CustomerMetadata.java index d63699f..25c232b 100644 --- a/src/main/java/com/abacatepay/model/billing/CustomerMetadata.java +++ b/src/main/java/com/abacatepay/model/customer/CustomerMetadata.java @@ -1,8 +1,10 @@ -package com.abacatepay.model.billing; +package com.abacatepay.model.customer; +import lombok.Builder; import lombok.Data; @Data +@Builder public class CustomerMetadata { private String name; private String cellphone; diff --git a/src/main/java/com/abacatepay/model/customer/ListCustomerResponse.java b/src/main/java/com/abacatepay/model/customer/ListCustomerResponse.java new file mode 100644 index 0000000..e92b491 --- /dev/null +++ b/src/main/java/com/abacatepay/model/customer/ListCustomerResponse.java @@ -0,0 +1,17 @@ +package com.abacatepay.model.customer; + +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@NoArgsConstructor +public class ListCustomerResponse { + private String error; + private List data; + + public ListCustomerResponse(String error) { + this.error = error; + } +} diff --git a/src/main/java/com/abacatepay/model/IAbacatePayBilling.java b/src/main/java/com/abacatepay/services/IAbacatePayBilling.java similarity index 90% rename from src/main/java/com/abacatepay/model/IAbacatePayBilling.java rename to src/main/java/com/abacatepay/services/IAbacatePayBilling.java index 043e84f..a2d6972 100644 --- a/src/main/java/com/abacatepay/model/IAbacatePayBilling.java +++ b/src/main/java/com/abacatepay/services/IAbacatePayBilling.java @@ -1,4 +1,4 @@ -package com.abacatepay.model; +package com.abacatepay.services; import com.abacatepay.model.billing.CreateBillingData; import com.abacatepay.model.billing.CreateBillingResponse; diff --git a/src/main/java/com/abacatepay/services/IAbacatePayCustomer.java b/src/main/java/com/abacatepay/services/IAbacatePayCustomer.java new file mode 100644 index 0000000..d4a3e50 --- /dev/null +++ b/src/main/java/com/abacatepay/services/IAbacatePayCustomer.java @@ -0,0 +1,10 @@ +package com.abacatepay.services; + +import com.abacatepay.model.customer.CreateCustomerResponse; +import com.abacatepay.model.customer.CustomerMetadata; +import com.abacatepay.model.customer.ListCustomerResponse; + +public interface IAbacatePayCustomer { + CreateCustomerResponse create(CustomerMetadata data); + ListCustomerResponse list(); +} diff --git a/src/main/java/com/abacatepay/services/impl/AbacatePayBilling.java b/src/main/java/com/abacatepay/services/impl/AbacatePayBilling.java new file mode 100644 index 0000000..429f376 --- /dev/null +++ b/src/main/java/com/abacatepay/services/impl/AbacatePayBilling.java @@ -0,0 +1,33 @@ +package com.abacatepay.services.impl; + +import com.abacatepay.clients.AbacatePayClient; +import com.abacatepay.model.billing.CreateBillingData; +import com.abacatepay.model.billing.CreateBillingResponse; +import com.abacatepay.model.billing.ListBillingResponse; +import com.abacatepay.services.IAbacatePayBilling; +import feign.FeignException; +import lombok.AllArgsConstructor; + +@AllArgsConstructor +public class AbacatePayBilling implements IAbacatePayBilling { + + private final AbacatePayClient client; + + @Override + public CreateBillingResponse create(CreateBillingData data) { + try { + return client.createBilling(data); + } catch (IllegalArgumentException | FeignException e) { + return new CreateBillingResponse(e.getMessage()); + } + } + + @Override + public ListBillingResponse list() { + try { + return client.listBillings(); + } catch (IllegalArgumentException | FeignException e) { + return new ListBillingResponse(e.getMessage()); + } + } +} diff --git a/src/main/java/com/abacatepay/services/impl/AbacatePayCustomer.java b/src/main/java/com/abacatepay/services/impl/AbacatePayCustomer.java new file mode 100644 index 0000000..dd09253 --- /dev/null +++ b/src/main/java/com/abacatepay/services/impl/AbacatePayCustomer.java @@ -0,0 +1,33 @@ +package com.abacatepay.services.impl; + +import com.abacatepay.clients.AbacatePayClient; +import com.abacatepay.model.customer.CreateCustomerResponse; +import com.abacatepay.model.customer.CustomerMetadata; +import com.abacatepay.model.customer.ListCustomerResponse; +import com.abacatepay.services.IAbacatePayCustomer; +import feign.FeignException; +import lombok.AllArgsConstructor; + +@AllArgsConstructor +public class AbacatePayCustomer implements IAbacatePayCustomer { + + private final AbacatePayClient client; + + @Override + public CreateCustomerResponse create(CustomerMetadata data) { + try { + return client.createCustomer(data); + } catch (IllegalArgumentException | FeignException e) { + return new CreateCustomerResponse(e.getMessage()); + } + } + + @Override + public ListCustomerResponse list() { + try { + return client.listCustomers(); + } catch (IllegalArgumentException | FeignException e) { + return new ListCustomerResponse(e.getMessage()); + } + } +} diff --git a/src/main/java/com/abacatepay/utils/DateUtils.java b/src/main/java/com/abacatepay/utils/DateUtils.java new file mode 100644 index 0000000..bc77de5 --- /dev/null +++ b/src/main/java/com/abacatepay/utils/DateUtils.java @@ -0,0 +1,6 @@ +package com.abacatepay.utils; + +public class DateUtils { + public static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; + public static final String DATE_TIMEZONE_DEFAULT = "America/Sao_Paulo"; +} diff --git a/src/test/java/com/abacatepay/AbacatePayTest.java b/src/test/java/com/abacatepay/AbacatePayTest.java index 9db2cde..3684297 100644 --- a/src/test/java/com/abacatepay/AbacatePayTest.java +++ b/src/test/java/com/abacatepay/AbacatePayTest.java @@ -1,26 +1,22 @@ package com.abacatepay; import com.abacatepay.clients.AbacatePayClient; -import com.abacatepay.model.billing.CreateBillingData; -import com.abacatepay.model.billing.CreateBillingResponse; -import com.abacatepay.model.billing.ListBillingResponse; +import com.abacatepay.services.IAbacatePayBilling; +import com.abacatepay.services.IAbacatePayCustomer; +import com.abacatepay.services.impl.AbacatePayBilling; +import com.abacatepay.services.impl.AbacatePayCustomer; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import java.lang.reflect.Field; -import static org.mockito.Mockito.*; - class AbacatePayTest { @Mock private AbacatePayClient abacatePayClient; - - @InjectMocks private AbacatePay abacatePay; @BeforeEach @@ -38,48 +34,30 @@ private void setClientMock(AbacatePay abacatePay, AbacatePayClient mockClient) t } @Test - void shouldReturnCreateBillingResponseOnSuccess() { - CreateBillingData data = CreateBillingData.builder().build(); - CreateBillingResponse expectedResponse = new CreateBillingResponse(); - - when(abacatePayClient.create(data)).thenReturn(expectedResponse); - - CreateBillingResponse result = abacatePay.billing().create(data); - verify(abacatePayClient, atMostOnce()).create(data); - Assertions.assertEquals(expectedResponse, result, "Should return the expected response"); + void testBilling() { + IAbacatePayBilling billingService = abacatePay.billing(); + Assertions.assertNotNull(billingService); + Assertions.assertTrue(billingService instanceof AbacatePayBilling); } @Test - void shouldCreateBillingThrowsAnException() { - CreateBillingData data = CreateBillingData.builder().build(); - CreateBillingResponse expectedResponse = new CreateBillingResponse("API key not provided"); - - when(abacatePayClient.create(data)).thenThrow(new IllegalArgumentException("API key not provided")); - - CreateBillingResponse result = abacatePay.billing().create(data); - verify(abacatePayClient, atMostOnce()).create(data); - Assertions.assertEquals(expectedResponse, result, "Should return the expected response"); + void testCustomer() { + IAbacatePayCustomer customerService = abacatePay.customer(); + Assertions.assertNotNull(customerService); + Assertions.assertTrue(customerService instanceof AbacatePayCustomer); } @Test - void shouldReturnBillingListResponseOnSucess() { - ListBillingResponse expectedResponse = new ListBillingResponse(); - - when(abacatePayClient.list()).thenReturn(expectedResponse); - - ListBillingResponse result = abacatePay.billing().list(); - verify(abacatePayClient, atMostOnce()).list(); - Assertions.assertEquals(expectedResponse, result, "Should return the expected response"); + void shouldThrowsIllegalArgumentExceptionIfApiKeyIsNull() { + Assertions.assertThrows(IllegalArgumentException.class, () -> { + abacatePay = new AbacatePay(null); + }, "Should throw an IllegalArgumentException"); } @Test - void shouldListBillingThrowsAnException() { - ListBillingResponse expectedResponse = new ListBillingResponse("API key not provided"); - - when(abacatePayClient.list()).thenThrow(new IllegalArgumentException("API key not provided")); - - ListBillingResponse result = abacatePay.billing().list(); - verify(abacatePayClient, atMostOnce()).list(); - Assertions.assertEquals(expectedResponse, result, "Should return the expected response"); + void shouldThrowsIllegalArgumentExceptionIfApiKeyIsBlank() { + Assertions.assertThrows(IllegalArgumentException.class, () -> { + abacatePay = new AbacatePay(""); + }, "Should throw an IllegalArgumentException"); } } diff --git a/src/test/java/com/abacatepay/model/billing/CreateBillingResponseTest.java b/src/test/java/com/abacatepay/model/billing/CreateBillingResponseTest.java index aca7545..8f03777 100644 --- a/src/test/java/com/abacatepay/model/billing/CreateBillingResponseTest.java +++ b/src/test/java/com/abacatepay/model/billing/CreateBillingResponseTest.java @@ -15,9 +15,9 @@ void testErrorConstructor() { void testGettersAndSetters() { CreateBillingResponse response = new CreateBillingResponse(); response.setError("Error occurred"); - response.setBilling(null); + response.setData(null); assertEquals("Error occurred", response.getError(), "Error field must be set correctly"); - assertNull(response.getBilling(), "Billing field must be set correctly"); + assertNull(response.getData(), "Billing field must be set correctly"); } } \ No newline at end of file diff --git a/src/test/java/com/abacatepay/model/billing/ListBillingResponseTest.java b/src/test/java/com/abacatepay/model/billing/ListBillingResponseTest.java index 06b67a4..82591db 100644 --- a/src/test/java/com/abacatepay/model/billing/ListBillingResponseTest.java +++ b/src/test/java/com/abacatepay/model/billing/ListBillingResponseTest.java @@ -15,9 +15,9 @@ void testErrorConstructor() { void testGettersAndSetters() { ListBillingResponse response = new ListBillingResponse(); response.setError("Error occurred"); - response.setBillings(null); + response.setData(null); assertEquals("Error occurred", response.getError(), "Error field must be set correctly"); - assertNull(response.getBillings(), "Billings field must be set correctly"); + assertNull(response.getData(), "Billings field must be set correctly"); } } \ No newline at end of file diff --git a/src/test/java/com/abacatepay/model/customer/CreateCustomerResponseTest.java b/src/test/java/com/abacatepay/model/customer/CreateCustomerResponseTest.java new file mode 100644 index 0000000..2a7443c --- /dev/null +++ b/src/test/java/com/abacatepay/model/customer/CreateCustomerResponseTest.java @@ -0,0 +1,24 @@ +package com.abacatepay.model.customer; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +public class CreateCustomerResponseTest { + @Test + void testErrorConstructor() { + CreateCustomerResponse response = new CreateCustomerResponse("Error occurred"); + assertEquals("Error occurred", response.getError(), "Error field must be set correctly"); + } + + @Test + void testGettersAndSetters() { + CreateCustomerResponse response = new CreateCustomerResponse(); + response.setError("Error occurred"); + response.setData(null); + + assertEquals("Error occurred", response.getError(), "Error field must be set correctly"); + assertNull(response.getData(), "Data field must be set correctly"); + } +} diff --git a/src/test/java/com/abacatepay/model/billing/CustomerMetadataTest.java b/src/test/java/com/abacatepay/model/customer/CustomerMetadataTest.java similarity index 65% rename from src/test/java/com/abacatepay/model/billing/CustomerMetadataTest.java rename to src/test/java/com/abacatepay/model/customer/CustomerMetadataTest.java index d0e082e..acef215 100644 --- a/src/test/java/com/abacatepay/model/billing/CustomerMetadataTest.java +++ b/src/test/java/com/abacatepay/model/customer/CustomerMetadataTest.java @@ -1,4 +1,4 @@ -package com.abacatepay.model.billing; +package com.abacatepay.model.customer; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -7,11 +7,12 @@ class CustomerMetadataTest { @Test void testGettersAndSetters() { - CustomerMetadata customerMetadata = new CustomerMetadata(); - customerMetadata.setName("John Doe"); - customerMetadata.setCellphone("123456789"); - customerMetadata.setEmail("john.doe@example.com"); - customerMetadata.setTaxId("12345678901"); + CustomerMetadata customerMetadata = CustomerMetadata.builder() + .name("John Doe") + .cellphone("123456789") + .email("john.doe@example.com") + .taxId("12345678901") + .build(); assertEquals("John Doe", customerMetadata.getName(), "Name field must be set correctly"); assertEquals("123456789", customerMetadata.getCellphone(), "Cellphone field must be set correctly"); diff --git a/src/test/java/com/abacatepay/model/customer/ListCustomerResponseTest.java b/src/test/java/com/abacatepay/model/customer/ListCustomerResponseTest.java new file mode 100644 index 0000000..9d6104a --- /dev/null +++ b/src/test/java/com/abacatepay/model/customer/ListCustomerResponseTest.java @@ -0,0 +1,24 @@ +package com.abacatepay.model.customer; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +public class ListCustomerResponseTest { + @Test + void testErrorConstructor() { + ListCustomerResponse response = new ListCustomerResponse("Error occurred"); + assertEquals("Error occurred", response.getError(), "Error field must be set correctly"); + } + + @Test + void testGettersAndSetters() { + ListCustomerResponse response = new ListCustomerResponse(); + response.setError("Error occurred"); + response.setData(null); + + assertEquals("Error occurred", response.getError(), "Error field must be set correctly"); + assertNull(response.getData(), "Data field must be set correctly"); + } +} diff --git a/src/test/java/com/abacatepay/services/impl/AbacatePayBillingTest.java b/src/test/java/com/abacatepay/services/impl/AbacatePayBillingTest.java new file mode 100644 index 0000000..5ad98c7 --- /dev/null +++ b/src/test/java/com/abacatepay/services/impl/AbacatePayBillingTest.java @@ -0,0 +1,75 @@ +package com.abacatepay.services.impl; + +import com.abacatepay.clients.AbacatePayClient; +import com.abacatepay.model.billing.CreateBillingData; +import com.abacatepay.model.billing.CreateBillingResponse; +import com.abacatepay.model.billing.ListBillingResponse; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.atMostOnce; + +public class AbacatePayBillingTest { + + @Mock + private AbacatePayClient abacatePayClient; + + @InjectMocks + private AbacatePayBilling service; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void shouldReturnCreateBillingResponseOnSuccess() { + CreateBillingData data = CreateBillingData.builder().build(); + CreateBillingResponse expectedResponse = new CreateBillingResponse(); + + when(abacatePayClient.createBilling(data)).thenReturn(expectedResponse); + + CreateBillingResponse result = service.create(data); + verify(abacatePayClient, atMostOnce()).createBilling(data); + Assertions.assertEquals(expectedResponse, result, "Should return the expected response"); + } + + @Test + void shouldCreateBillingThrowsAnException() { + CreateBillingData data = CreateBillingData.builder().build(); + CreateBillingResponse expectedResponse = new CreateBillingResponse("API key not provided"); + + when(abacatePayClient.createBilling(data)).thenThrow(new IllegalArgumentException("API key not provided")); + + CreateBillingResponse result = service.create(data); + verify(abacatePayClient, atMostOnce()).createBilling(data); + Assertions.assertEquals(expectedResponse, result, "Should return the expected response"); + } + + @Test + void shouldReturnBillingListResponseOnSucess() { + ListBillingResponse expectedResponse = new ListBillingResponse(); + + when(abacatePayClient.listBillings()).thenReturn(expectedResponse); + + ListBillingResponse result = service.list(); + verify(abacatePayClient, atMostOnce()).listBillings(); + Assertions.assertEquals(expectedResponse, result, "Should return the expected response"); + } + + @Test + void shouldListBillingThrowsAnException() { + ListBillingResponse expectedResponse = new ListBillingResponse("API key not provided"); + + when(abacatePayClient.listBillings()).thenThrow(new IllegalArgumentException("API key not provided")); + + ListBillingResponse result = service.list(); + verify(abacatePayClient, atMostOnce()).listBillings(); + Assertions.assertEquals(expectedResponse, result, "Should return the expected response"); + } +} diff --git a/src/test/java/com/abacatepay/services/impl/AbacatePayCustomerTest.java b/src/test/java/com/abacatepay/services/impl/AbacatePayCustomerTest.java new file mode 100644 index 0000000..3cd758d --- /dev/null +++ b/src/test/java/com/abacatepay/services/impl/AbacatePayCustomerTest.java @@ -0,0 +1,79 @@ +package com.abacatepay.services.impl; + +import com.abacatepay.clients.AbacatePayClient; +import com.abacatepay.model.customer.CreateCustomerResponse; +import com.abacatepay.model.customer.CustomerMetadata; +import com.abacatepay.model.customer.ListCustomerResponse; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.atMostOnce; + +public class AbacatePayCustomerTest { + + @Mock + private AbacatePayClient abacatePayClient; + + @InjectMocks + private AbacatePayCustomer abacatePayCustomer; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void shouldReturnCreateCustomerResponseOnSuccess() { + CreateCustomerResponse expectedResponse = new CreateCustomerResponse(); + CustomerMetadata request = CustomerMetadata.builder().build(); + + when(abacatePayClient.createCustomer(request)) + .thenReturn(expectedResponse); + + CreateCustomerResponse result = abacatePayCustomer.create(request); + verify(abacatePayClient, atMostOnce()).createCustomer(request); + Assertions.assertEquals(expectedResponse, result, "Should return the expected response"); + } + + @Test + void shouldCreateCustomerThrowsAnException() { + CreateCustomerResponse expectedResponse = new CreateCustomerResponse("Unauthorized"); + CustomerMetadata request = CustomerMetadata.builder().build(); + + when(abacatePayClient.createCustomer(request)) + .thenThrow(new IllegalArgumentException("Unauthorized")); + + CreateCustomerResponse result = abacatePayCustomer.create(request); + verify(abacatePayClient, atMostOnce()).createCustomer(request); + Assertions.assertEquals(expectedResponse, result, "Should return the expected response"); + } + + @Test + void shouldReturnListCustomersResponseOnSuccess() { + ListCustomerResponse expectedResponse = new ListCustomerResponse(); + + when(abacatePayClient.listCustomers()) + .thenReturn(expectedResponse); + + ListCustomerResponse result = abacatePayCustomer.list(); + verify(abacatePayClient, atMostOnce()).listCustomers(); + Assertions.assertEquals(expectedResponse, result, "Should return the expected response"); + } + + @Test + void shouldListCustomersThrowsAnException() { + ListCustomerResponse expectedResponse = new ListCustomerResponse("Unauthorized"); + + when(abacatePayClient.listCustomers()) + .thenThrow(new IllegalArgumentException("Unauthorized")); + + ListCustomerResponse result = abacatePayCustomer.list(); + verify(abacatePayClient, atMostOnce()).listCustomers(); + Assertions.assertEquals(expectedResponse, result, "Should return the expected response"); + } +}