Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IPL-228] Refactor objetos customers #23

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/main/java/conekta/io/client/impl/CustomersClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import conekta.io.error.ConektaErrorResponse;
import conekta.io.model.PaginatedConektaObject;
import conekta.io.model.impl.Customer;
import conekta.io.model.request.CustomerReq;
import conekta.io.model.submodel.Event;
import conekta.io.model.submodel.PaymentSource;

Expand All @@ -21,8 +22,8 @@ public class CustomersClient extends ConektaRequestor {
* @param customer The customer to be created.
* @return The created ConektaResponse<Customer>.
*/
public ConektaResponse<Customer> createCustomer(Customer customer){
HttpResponse<String> customerResponse = doRequest(customer, Constants.CUSTOMERS_PATH, Constants.POST);
public ConektaResponse<Customer> createCustomer(CustomerReq customerReq) {
HttpResponse<String> customerResponse = doRequest(customerReq, Constants.CUSTOMERS_PATH, Constants.POST);
return ConektaResponse.<Customer>builder()
.response(customerResponse)
.statusCode(customerResponse.statusCode())
Expand All @@ -37,7 +38,7 @@ public ConektaResponse<Customer> createCustomer(Customer customer){
* @param customerId The id of the customer to be retrieved.
* @return The retrieved ConektaResponse<Customer>.
*/
public ConektaResponse<Customer> retrieveCustomer(String customerId){
public ConektaResponse<Customer> retrieveCustomer(String customerId) {
HttpResponse<String> customerResponse = doRequest(null, Constants.CUSTOMERS_PATH + Constants.SLASH + customerId, Constants.GET);
return ConektaResponse.<Customer>builder()
.response(customerResponse)
Expand All @@ -49,12 +50,13 @@ public ConektaResponse<Customer> retrieveCustomer(String customerId){

/**
* Retrieves all customers paginated in Conekta.
*
* @param next The next page of the customers to be retrieved.
* If null, the first page will be retrieved.
* If not null, the next page will be retrieved.
* @return The retrieved ConektaResponse<PaginatedConektaObject<Customer>>.
*/
public ConektaResponse<PaginatedConektaObject<Customer>> getCustomers(String next){
public ConektaResponse<PaginatedConektaObject<Customer>> getCustomers(String next) {
HttpResponse<String> customersResponse = doRequest(null, Constants.CUSTOMERS_PATH + (next != null ? Constants.NEXT + next : ""), Constants.GET);
return ConektaResponse.<PaginatedConektaObject<Customer>>builder()
.response(customersResponse)
Expand All @@ -71,7 +73,7 @@ public ConektaResponse<PaginatedConektaObject<Customer>> getCustomers(String nex
* @param customer The customer to be updated.
* @return The updated ConektaResponse<Customer>.
*/
public ConektaResponse<Customer> updateCustomer(String customerId, Customer customer){
public ConektaResponse<Customer> updateCustomer(String customerId, Customer customer) {
HttpResponse<String> customerResponse = doRequest(customer, Constants.CUSTOMERS_PATH + Constants.SLASH + customerId, Constants.PUT);
return ConektaResponse.<Customer>builder()
.response(customerResponse)
Expand All @@ -87,7 +89,7 @@ public ConektaResponse<Customer> updateCustomer(String customerId, Customer cust
* @param customerId The id of the customer to be deleted.
* @return The deleted ConektaResponse<Customer>.
*/
public ConektaResponse<Customer> deleteCustomer(String customerId){
public ConektaResponse<Customer> deleteCustomer(String customerId) {
HttpResponse<String> customerResponse = doRequest(null, Constants.CUSTOMERS_PATH + Constants.SLASH + customerId, Constants.DELETE);
return ConektaResponse.<Customer>builder()
.response(customerResponse)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/conekta/io/client/impl/OrdersClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import conekta.io.config.Constants;
import conekta.io.error.ConektaErrorResponse;
import conekta.io.model.PaginatedConektaObject;
import conekta.io.model.impl.Order;
import conekta.io.model.request.OrderRefundReq;
import conekta.io.model.request.OrderReq;
import conekta.io.model.response.Order;
import conekta.io.model.submodel.Charge;
import conekta.io.utils.Utils;

Expand Down
9 changes: 6 additions & 3 deletions src/main/java/conekta/io/model/impl/Customer.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package conekta.io.model.impl;

import conekta.io.model.ConektaObject;
import conekta.io.model.PaginatedConektaObject;
import conekta.io.model.submodel.AntifraudInfo;
import conekta.io.model.submodel.PaymentSource;
import conekta.io.model.submodel.ShippingContact;
import lombok.Data;

@Data
Expand All @@ -17,8 +20,8 @@ public class Customer extends ConektaObject {
private String customReference;
private AntifraudInfo antifraudInfo;
private String defaultPaymentSourceId;
private Object paymentSources;
private Object shippingContacts;
//TODO REVISAR CON UN EXPERTO URGENTE
private PaginatedConektaObject<PaymentSource> paymentSources;
private PaginatedConektaObject<ShippingContact> shippingContacts;
private Subscription subscription;
private Boolean deleted;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package conekta.io.model.response;
package conekta.io.model.impl;

import conekta.io.model.ConektaObject;
import conekta.io.model.PaginatedConektaObject;
import conekta.io.model.impl.Customer;
import conekta.io.model.submodel.*;
import lombok.Data;

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/conekta/io/model/impl/Subscription.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
package conekta.io.model.impl;

import conekta.io.model.ConektaObject;
import lombok.Data;

@Data
public class Subscription extends ConektaObject {

private String status;
private String chargeId;
private String createAt;
private Long subscriptionStart;
private Long billingCycleStart;
private Long billingCycleEnd;
private String planId;
private String customerId;
private String cardId;
}
5 changes: 4 additions & 1 deletion src/main/java/conekta/io/model/request/CustomerReq.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package conekta.io.model.request;

import conekta.io.model.ConektaObject;
import conekta.io.model.submodel.PaymentSource;
import conekta.io.model.submodel.ShippingContact;
import lombok.Data;

import java.util.List;

@Data
public class CustomerReq extends ConektaObject {
private String customerId;
private String name;
private String phone;
private String email;
private List<PaymentSourceCreateReq> paymentSources;
private List<PaymentSource> paymentSources;
private Boolean corporate;
private String defaultPaymentSourceID;
private List<ShippingContact> shippingContacts;
private Boolean deleted;
}
14 changes: 0 additions & 14 deletions src/main/java/conekta/io/model/request/PaymentSourceCreateReq.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package conekta.io.model.request;
package conekta.io.model.submodel;

import conekta.io.model.ConektaObject;
import conekta.io.model.request.TokenAddressReq;
import lombok.Data;

@Data
public class CardReq extends ConektaObject {
public class Card extends ConektaObject {
private String number;
private String name;
private String expYear;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/conekta/io/model/submodel/PaymentSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import conekta.io.model.ConektaObject;
import lombok.Data;

import java.math.BigDecimal;

@Data
public class PaymentSource extends ConektaObject {
private String type;
Expand All @@ -18,4 +20,7 @@ public class PaymentSource extends ConektaObject {
private String parentId;
private Boolean isDefault;
private Boolean visibleOnCheckout;
private String paymentType;
private BigDecimal expiresAt;
private Card card;
}
42 changes: 31 additions & 11 deletions src/test/java/conekta/io/CustomersClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import conekta.io.error.ConektaErrorResponse;
import conekta.io.model.PaginatedConektaObject;
import conekta.io.model.impl.Customer;
import conekta.io.model.request.CustomerReq;
import conekta.io.model.submodel.Event;
import conekta.io.model.submodel.PaymentSource;
import okhttp3.mockwebserver.MockWebServer;
Expand Down Expand Up @@ -34,17 +35,20 @@ void generateAuthenticator() {

@Test
void createCustomer() throws URISyntaxException, IOException {

// Arrange
String customerJson = Utils.readFile("clients/customer.json");
Customer cus = ConektaObjectMapper.getInstance().stringJsonToObject(customerJson, Customer.class);
String customerResponseJson = Utils.readFile("clients/customerCreateResponse.json");
CustomerReq customerReq = ConektaObjectMapper.getInstance().stringJsonToObject(customerJson, CustomerReq.class);
Customer customerResponse = ConektaObjectMapper.getInstance().stringJsonToObject(customerResponseJson, Customer.class);

Utils.buildMockServer(this.mockWebServer, customerJson, 201);
Utils.buildMockServer(this.mockWebServer, customerResponseJson, 201);

// Act
ConektaResponse<Customer> customerConektaResponse = customersClient.createCustomer(cus);
ConektaResponse<Customer> customerConektaResponse = customersClient.createCustomer(customerReq);

// Assert
Assertions.assertEquals(customerConektaResponse.getData(), cus);
Assertions.assertEquals(customerConektaResponse.getData(), customerResponse);
Assertions.assertEquals(201, customerConektaResponse.getStatusCode());
}

Expand All @@ -53,7 +57,7 @@ void createCustomerWithError() throws URISyntaxException, IOException {
// Arrange
String customerJson = Utils.readFile("clients/customerWithNoMail.json");
String errorJson = Utils.readFile("clients/errorMail.json");
Customer cus = ConektaObjectMapper.getInstance().stringJsonToObject(customerJson, Customer.class);
CustomerReq cus = ConektaObjectMapper.getInstance().stringJsonToObject(customerJson, CustomerReq.class);
ConektaErrorResponse error = ConektaObjectMapper.getInstance().stringJsonToObject(errorJson, ConektaErrorResponse.class);

Utils.buildMockServer(this.mockWebServer, errorJson, 404);
Expand All @@ -69,7 +73,7 @@ void createCustomerWithError() throws URISyntaxException, IOException {
@Test
void retrieveCustomer() throws IOException, URISyntaxException {
// Arrange
String customerJson = Utils.readFile("clients/customer.json");
String customerJson = Utils.readFile("clients/customerResponse.json");
Customer cus = ConektaObjectMapper.getInstance().stringJsonToObject(customerJson, Customer.class);

Utils.buildMockServer(this.mockWebServer, customerJson, 200);
Expand All @@ -83,8 +87,8 @@ void retrieveCustomer() throws IOException, URISyntaxException {

@Test
void getCustomers() throws IOException, URISyntaxException {
String customerJson = Utils.readFile("clients/customer.json");
Customer cus = ConektaObjectMapper.getInstance().stringJsonToObject(customerJson, Customer.class);
String customerListJson = Utils.readFile("clients/customerListResponse.json");
Customer cus = ConektaObjectMapper.getInstance().stringJsonToObject(customerListJson, Customer.class);
PaginatedConektaObject<Customer> paginatedConektaObject = new PaginatedConektaObject<>();
paginatedConektaObject.setData(List.of(cus));
String s = ConektaObjectMapper.getInstance().conektaObjectToString(paginatedConektaObject);
Expand All @@ -102,7 +106,7 @@ void getCustomers() throws IOException, URISyntaxException {
void updateCustomer() throws IOException, URISyntaxException {
String customerJson = Utils.readFile("clients/customer.json");
String customerJsonModified = Utils.readFile("clients/customerModified.json");
Customer cus = ConektaObjectMapper.getInstance().stringJsonToObject(customerJson, Customer.class);
CustomerReq cus = ConektaObjectMapper.getInstance().stringJsonToObject(customerJson, CustomerReq.class);
Customer cusModified = ConektaObjectMapper.getInstance().stringJsonToObject(customerJsonModified, Customer.class);

Utils.buildMockServer(this.mockWebServer, customerJsonModified, 200);
Expand All @@ -114,13 +118,12 @@ void updateCustomer() throws IOException, URISyntaxException {
Assertions.assertEquals(customerConektaResponse.getData(), cusModified);
Assertions.assertNotEquals(cus, customerConektaResponse.getData());
Assertions.assertNotEquals(cus, cusModified);

}

@Test
void deleteCustomer() throws IOException, URISyntaxException {
// Arrange
String customerJson = Utils.readFile("clients/customer.json");
String customerJson = Utils.readFile("clients/customerDeleteResponse.json");
Customer cus = ConektaObjectMapper.getInstance().stringJsonToObject(customerJson, Customer.class);
cus.setDeleted(true);
String deletedJson = ConektaObjectMapper.getInstance().conektaObjectToString(cus);
Expand All @@ -134,6 +137,23 @@ void deleteCustomer() throws IOException, URISyntaxException {
Assertions.assertEquals(customerConektaResponse.getData(), cus);
}

@Test
void deleteCustomerFail() throws IOException, URISyntaxException {
// Arrange
String customerDeleteFailResponseJson = Utils.readFile("clients/customerDeleteFailResponse.json");
Customer cus = ConektaObjectMapper.getInstance().stringJsonToObject(customerDeleteFailResponseJson, Customer.class);
cus.setDeleted(true);
String deletedJson = ConektaObjectMapper.getInstance().conektaObjectToString(cus);

Utils.buildMockServer(this.mockWebServer, deletedJson, 404);

// Act
ConektaResponse<Customer> customerConektaResponse = customersClient.deleteCustomer("1");

// Assert
Assertions.assertEquals(customerConektaResponse.getData(), cus);
}

@Test
void getCustomerEvents() throws IOException, URISyntaxException {
// Arrange
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/conekta/io/OrdersClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import conekta.io.config.ConektaObjectMapper;
import conekta.io.error.ConektaError;
import conekta.io.model.PaginatedConektaObject;
import conekta.io.model.impl.Order;
import conekta.io.model.request.OrderRefundReq;
import conekta.io.model.request.OrderReq;
import conekta.io.model.response.Order;
import conekta.io.model.submodel.Charge;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.jupiter.api.Assertions;
Expand Down
67 changes: 67 additions & 0 deletions src/test/resources/clients/customerCreateResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"livemode": false,
"name": "Jhon Doe",
"email": "[email protected]",
"phone": "+5215555555555",
"default_shipping_contact_id": "ship_cont_2sTTpLxTSUnV63Eeb",
"id": "cus_2sTTpLxTSUnV63Eec",
"object": "customer",
"created_at": 1662473589,
"corporate": true,
"custom_reference": "",
"antifraud_info": {
"first_paid_at": 1485151007,
"account_created_at": 1484040996
},
"default_payment_source_id": "src_2sTTpLxTSUnV63Eeg",
"payment_sources": {
"object": "list",
"has_more": false,
"total": 1,
"data": [
{
"id": "src_2sTTpLxTSUnV63Eeg",
"object": "payment_source",
"type": "card",
"created_at": 1662473590,
"last4": "4242",
"bin": "424242",
"card_type": "credit",
"exp_month": "09",
"exp_year": "23",
"brand": "visa",
"name": "Jorge Lopez",
"parent_id": "cus_2sTTpLxTSUnV63Eec",
"default": true,
"visible_on_checkout": false
}
]
},
"shipping_contacts": {
"object": "list",
"has_more": false,
"total": 1,
"data": [
{
"receiver": "Marvin Fuller",
"phone": "+5215555555555",
"between_streets": "Morelos Campeche",
"address": {
"street1": "Nuevo Leon 4",
"street2": "fake street",
"city": "Ciudad de Mexico",
"state": "Ciudad de Mexico",
"country": "mx",
"residential": true,
"object": "shipping_address",
"postal_code": "06100"
},
"id": "ship_cont_2sTTpLxTSUnV63Eeb",
"object": "shipping_contact",
"created_at": 1662473589,
"parent_id": "cus_2sTTpLxTSUnV63Eec",
"default": true
}
]
}
}
12 changes: 12 additions & 0 deletions src/test/resources/clients/customerDeleteFailResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"details": [
{
"debug_message": "The object customer \"cus_2sTTpLxTSUnV63Eec\" could not be found.",
"message": "El recurso no ha sido encontrado.",
"code": "conekta.errors.resource_not_found.entity"
}
],
"object": "error",
"type": "resource_not_found_error",
"log_id": "63176fdf9e9c3150da5e44fd"
}
Loading