From fda5107e7635a0d3109b91a7d7a89f6352487a1a Mon Sep 17 00:00:00 2001 From: Mats Stijlaart Date: Tue, 25 Oct 2016 21:03:46 +0200 Subject: [PATCH] #19: Add concept of customers and recurring payments --- src/main/java/nl/stil4m/mollie/Client.java | 15 +++- .../java/nl/stil4m/mollie/DynamicClient.java | 15 +++- .../mollie/concepts/CustomerPayments.java | 55 ++++++++++++ .../nl/stil4m/mollie/concepts/Customers.java | 71 +++++++++++++++ .../stil4m/mollie/domain/CreateCustomer.java | 42 +++++++++ .../nl/stil4m/mollie/domain/Customer.java | 79 +++++++++++++++++ .../stil4m/mollie/domain/CustomerPayment.java | 23 +++++ .../java/nl/stil4m/mollie/domain/Payment.java | 61 ++++++++----- .../stil4m/mollie/domain/UpdateCustomer.java | 45 ++++++++++ .../FirstRecurringPayment.java | 12 +++ .../NormalCustomerPayment.java | 13 +++ .../customerpayments/RecurringPayment.java | 11 +++ .../CustomerPaymentsIntegrationTest.java | 56 ++++++++++++ .../mollie/CustomersIntegerationTest.java | 88 +++++++++++++++++++ .../RecurringPaymentSerializationTest.java | 37 ++++++++ .../expected_first_recurring_payment.json | 10 +++ 16 files changed, 611 insertions(+), 22 deletions(-) create mode 100644 src/main/java/nl/stil4m/mollie/concepts/CustomerPayments.java create mode 100644 src/main/java/nl/stil4m/mollie/concepts/Customers.java create mode 100644 src/main/java/nl/stil4m/mollie/domain/CreateCustomer.java create mode 100644 src/main/java/nl/stil4m/mollie/domain/Customer.java create mode 100644 src/main/java/nl/stil4m/mollie/domain/CustomerPayment.java create mode 100644 src/main/java/nl/stil4m/mollie/domain/UpdateCustomer.java create mode 100644 src/main/java/nl/stil4m/mollie/domain/customerpayments/FirstRecurringPayment.java create mode 100644 src/main/java/nl/stil4m/mollie/domain/customerpayments/NormalCustomerPayment.java create mode 100644 src/main/java/nl/stil4m/mollie/domain/customerpayments/RecurringPayment.java create mode 100644 src/test/java/nl/stil4m/mollie/CustomerPaymentsIntegrationTest.java create mode 100644 src/test/java/nl/stil4m/mollie/CustomersIntegerationTest.java create mode 100644 src/test/java/nl/stil4m/mollie/RecurringPaymentSerializationTest.java create mode 100644 src/test/resources/expected_first_recurring_payment.json diff --git a/src/main/java/nl/stil4m/mollie/Client.java b/src/main/java/nl/stil4m/mollie/Client.java index f87f6e5..a2f4ad7 100644 --- a/src/main/java/nl/stil4m/mollie/Client.java +++ b/src/main/java/nl/stil4m/mollie/Client.java @@ -1,6 +1,12 @@ package nl.stil4m.mollie; -import nl.stil4m.mollie.concepts.*; +import nl.stil4m.mollie.concepts.CustomerPayments; +import nl.stil4m.mollie.concepts.Customers; +import nl.stil4m.mollie.concepts.Issuers; +import nl.stil4m.mollie.concepts.Methods; +import nl.stil4m.mollie.concepts.Payments; +import nl.stil4m.mollie.concepts.Refunds; +import nl.stil4m.mollie.concepts.Status; public class Client { @@ -33,4 +39,11 @@ public Refunds refunds() { return dynamicClient.refunds(apiKey); } + public Customers customers() { + return dynamicClient.customers(apiKey); + } + + public CustomerPayments customerPayments(String customerId) { + return dynamicClient.customerPayments(apiKey, customerId); + } } diff --git a/src/main/java/nl/stil4m/mollie/DynamicClient.java b/src/main/java/nl/stil4m/mollie/DynamicClient.java index 14ab917..5cae9d8 100644 --- a/src/main/java/nl/stil4m/mollie/DynamicClient.java +++ b/src/main/java/nl/stil4m/mollie/DynamicClient.java @@ -1,6 +1,12 @@ package nl.stil4m.mollie; -import nl.stil4m.mollie.concepts.*; +import nl.stil4m.mollie.concepts.CustomerPayments; +import nl.stil4m.mollie.concepts.Customers; +import nl.stil4m.mollie.concepts.Issuers; +import nl.stil4m.mollie.concepts.Methods; +import nl.stil4m.mollie.concepts.Payments; +import nl.stil4m.mollie.concepts.Refunds; +import nl.stil4m.mollie.concepts.Status; public class DynamicClient { @@ -32,4 +38,11 @@ public Refunds refunds(String apiKey) { return new Refunds(apiKey, endpoint, requestExecutor); } + public Customers customers(String apiKey) { + return new Customers(apiKey, endpoint, requestExecutor); + } + + public CustomerPayments customerPayments(String apiKey, String customerId) { + return new CustomerPayments(apiKey, endpoint, requestExecutor, customerId); + } } diff --git a/src/main/java/nl/stil4m/mollie/concepts/CustomerPayments.java b/src/main/java/nl/stil4m/mollie/concepts/CustomerPayments.java new file mode 100644 index 0000000..f20122f --- /dev/null +++ b/src/main/java/nl/stil4m/mollie/concepts/CustomerPayments.java @@ -0,0 +1,55 @@ +package nl.stil4m.mollie.concepts; + +import com.fasterxml.jackson.core.type.TypeReference; +import nl.stil4m.mollie.RequestExecutor; +import nl.stil4m.mollie.ResponseOrError; +import nl.stil4m.mollie.domain.CustomerPayment; +import nl.stil4m.mollie.domain.Page; +import nl.stil4m.mollie.domain.Payment; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.Optional; + +public class CustomerPayments { + + private static final TypeReference> PAGE_PAYMENT_TYPE = new TypeReference>() { + }; + + private static final TypeReference PAYMENT_TYPE = new TypeReference() { + }; + + + private final String apiKey; + private final String endpoint; + private final RequestExecutor requestExecutor; + private final String customerId; + + public CustomerPayments(String apiKey, String endpoint, RequestExecutor requestExecutor, String customerId) { + this.apiKey = apiKey; + this.endpoint = endpoint; + this.requestExecutor = requestExecutor; + this.customerId = customerId; + } + + + public ResponseOrError> all(Optional count, Optional offset) throws URISyntaxException, IOException { + URIBuilder builder = new URIBuilder(endpoint + "/customers/" + customerId + "/payments") + .setParameter("count", String.valueOf(count.orElse(10))) + .setParameter("offset", String.valueOf(offset.orElse(0))); + + HttpGet httpGet = new HttpGet(builder.build()); + return requestExecutor.execute(apiKey, httpGet, PAGE_PAYMENT_TYPE); + } + + public ResponseOrError create(CustomerPayment customerPayment) throws IOException { + HttpPost httpPost = new HttpPost(endpoint + "/customers/" + customerId + "/payments"); + httpPost.setEntity(new StringEntity(requestExecutor.serialize(customerPayment), ContentType.APPLICATION_JSON)); + return requestExecutor.execute(apiKey, httpPost, PAYMENT_TYPE); + } +} diff --git a/src/main/java/nl/stil4m/mollie/concepts/Customers.java b/src/main/java/nl/stil4m/mollie/concepts/Customers.java new file mode 100644 index 0000000..a3436ca --- /dev/null +++ b/src/main/java/nl/stil4m/mollie/concepts/Customers.java @@ -0,0 +1,71 @@ +package nl.stil4m.mollie.concepts; + +import com.fasterxml.jackson.core.type.TypeReference; +import nl.stil4m.mollie.RequestExecutor; +import nl.stil4m.mollie.ResponseOrError; +import nl.stil4m.mollie.domain.CreateCustomer; +import nl.stil4m.mollie.domain.Customer; +import nl.stil4m.mollie.domain.Page; +import nl.stil4m.mollie.domain.UpdateCustomer; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.Optional; + +import static nl.stil4m.mollie.Util.validatePaymentId; + +public class Customers { + + + private static final TypeReference> PAGE_CUSTOMER_TYPE = new TypeReference>() { + }; + private static final TypeReference CUSTOMER_TYPE = new TypeReference() { + }; + + private final String apiKey; + private final String endpoint; + private final RequestExecutor requestExecutor; + + public Customers(String apiKey, String endpoint, RequestExecutor requestExecutor) { + this.apiKey = apiKey; + this.endpoint = endpoint; + this.requestExecutor = requestExecutor; + } + + public ResponseOrError create(CreateCustomer createCustomer) throws IOException { + HttpPost httpPost = new HttpPost(endpoint + "/customers"); + httpPost.setEntity(new StringEntity(requestExecutor.serialize(createCustomer), ContentType.APPLICATION_JSON)); + return requestExecutor.execute(apiKey, httpPost, new TypeReference() { + }); + } + + public ResponseOrError update(String customerId, UpdateCustomer updateCustomer) throws IOException { + HttpPut httpPut = new HttpPut(endpoint + "/customers/" + customerId); + System.out.println(requestExecutor.serialize(updateCustomer)); + httpPut.setEntity(new StringEntity(requestExecutor.serialize(updateCustomer), ContentType.APPLICATION_JSON)); + return requestExecutor.execute(apiKey, httpPut, new TypeReference() { + }); + } + + public ResponseOrError> all(Optional count, Optional offset) throws IOException, URISyntaxException { + URIBuilder builder = new URIBuilder(endpoint + "/customers") + .setParameter("count", String.valueOf(count.orElse(10))) + .setParameter("offset", String.valueOf(offset.orElse(0))); + + HttpGet httpGet = new HttpGet(builder.build()); + return requestExecutor.execute(apiKey, httpGet, PAGE_CUSTOMER_TYPE); + } + + public ResponseOrError get(String id) throws IOException { + validatePaymentId(id); + HttpGet httpGet = new HttpGet(endpoint + "/customers/" + id); + return requestExecutor.execute(apiKey, httpGet, CUSTOMER_TYPE); + } + +} diff --git a/src/main/java/nl/stil4m/mollie/domain/CreateCustomer.java b/src/main/java/nl/stil4m/mollie/domain/CreateCustomer.java new file mode 100644 index 0000000..870465e --- /dev/null +++ b/src/main/java/nl/stil4m/mollie/domain/CreateCustomer.java @@ -0,0 +1,42 @@ +package nl.stil4m.mollie.domain; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +public class CreateCustomer { + + private final String name; + private final String email; + private final Optional locale; + private final Map metadata; + + public CreateCustomer(@JsonProperty("name") String name, + @JsonProperty("email") String email, + @JsonProperty("locale") Optional locale, + @JsonProperty("metadata") Map metadata) { + this.name = name; + this.email = email; + this.locale = locale; + this.metadata = metadata; + } + + public String getName() { + return name; + } + + public String getEmail() { + return email; + } + + public Optional getLocale() { + return locale; + } + + public Map getMetadata() { + return metadata; + } +} diff --git a/src/main/java/nl/stil4m/mollie/domain/Customer.java b/src/main/java/nl/stil4m/mollie/domain/Customer.java new file mode 100644 index 0000000..0e7e5cc --- /dev/null +++ b/src/main/java/nl/stil4m/mollie/domain/Customer.java @@ -0,0 +1,79 @@ +package nl.stil4m.mollie.domain; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +public class Customer { + + private final String resource; + private final String id; + private final String mode; + private final String name; + private final String email; + private final Optional locale; + private final Map metadata; + private final List recentlyUsedMethods; + private final Date createdDatetime; + + public Customer(@JsonProperty("resource") String resource, + @JsonProperty("id") String id, + @JsonProperty("mode") String mode, + @JsonProperty("name") String name, + @JsonProperty("email") String email, + @JsonProperty("locale") Optional locale, + @JsonProperty("metaData") Map metadata, + @JsonProperty("recentlyUsedMethods") List recentlyUsedMethods, + @JsonProperty("createdDatetime") Date createdDatetime) { + this.resource = resource; + this.id = id; + this.mode = mode; + this.name= name; + this.email = email; + this.locale = locale; + this.metadata = metadata; + this.recentlyUsedMethods = recentlyUsedMethods; + this.createdDatetime = createdDatetime; + } + + public String getResource() { + return resource; + } + + public String getId() { + return id; + } + + public String getMode() { + return mode; + } + + public String getName() { + return name; + } + + public String getEmail() { + return email; + } + + public Optional getLocale() { + return locale; + } + + public Map getMetadata() { + return metadata; + } + + public List getRecentlyUsedMethods() { + return recentlyUsedMethods; + } + + public Date getCreatedDatetime() { + return createdDatetime; + } + + +} diff --git a/src/main/java/nl/stil4m/mollie/domain/CustomerPayment.java b/src/main/java/nl/stil4m/mollie/domain/CustomerPayment.java new file mode 100644 index 0000000..472397a --- /dev/null +++ b/src/main/java/nl/stil4m/mollie/domain/CustomerPayment.java @@ -0,0 +1,23 @@ +package nl.stil4m.mollie.domain; + +import com.fasterxml.jackson.annotation.JsonUnwrapped; + +public abstract class CustomerPayment { + + @JsonUnwrapped + private final CreatePayment createPayment; + private final String recurringType; + + public CustomerPayment(CreatePayment createPayment, String recurringType) { + this.createPayment = createPayment; + this.recurringType = recurringType; + } + + public CreatePayment getCreatePayment() { + return createPayment; + } + + public String getRecurringType() { + return recurringType; + } +} diff --git a/src/main/java/nl/stil4m/mollie/domain/Payment.java b/src/main/java/nl/stil4m/mollie/domain/Payment.java index 5bba313..50742ad 100644 --- a/src/main/java/nl/stil4m/mollie/domain/Payment.java +++ b/src/main/java/nl/stil4m/mollie/domain/Payment.java @@ -6,28 +6,34 @@ public class Payment { - private String id; - private String profileId; - private String mode; - private String createdDatetime; - private String status; - private String paidDatetime; - private String cancelledDatetime; - private String expiredDatetime; - private Double amount; - private Double amountRefunded; - private Double amountRemaining; - private String description; - private String method; - private Map details; - private Links links; - private Map metadata; - private String locale; - private String expiryPeriod; - - public Payment(@JsonProperty("id") String id, + private final String resource; + private final String id; + private final String profileId; + private final String mode; + private final String customerId; + private final String recurringType; + private final String createdDatetime; + private final String status; + private final String paidDatetime; + private final String cancelledDatetime; + private final String expiredDatetime; + private final Double amount; + private final Double amountRefunded; + private final Double amountRemaining; + private final String description; + private final String method; + private final Map details; + private final Links links; + private final Map metadata; + private final String locale; + private final String expiryPeriod; + + public Payment(@JsonProperty("resource") String resource, + @JsonProperty("id") String id, @JsonProperty("profileId") String profileId, @JsonProperty("mode") String mode, + @JsonProperty("customerId") String customerId, + @JsonProperty("recurringType") String recurringType, @JsonProperty("createdDatetime") String createdDatetime, @JsonProperty("status") String status, @JsonProperty("paidDatetime") String paidDatetime, @@ -43,9 +49,12 @@ public Payment(@JsonProperty("id") String id, @JsonProperty("metadata") Map metadata, @JsonProperty("locale") String locale, @JsonProperty("expiryPeriod") String expiryPeriod) { + this.resource = resource; this.id = id; this.profileId = profileId; this.mode = mode; + this.customerId = customerId; + this.recurringType = recurringType; this.createdDatetime = createdDatetime; this.status = status; this.paidDatetime = paidDatetime; @@ -63,6 +72,10 @@ public Payment(@JsonProperty("id") String id, this.expiryPeriod = expiryPeriod; } + public String getResource() { + return resource; + } + public String getId() { return id; } @@ -75,6 +88,14 @@ public String getMode() { return mode; } + public String getCustomerId() { + return customerId; + } + + public String getRecurringType() { + return recurringType; + } + public String getCreatedDatetime() { return createdDatetime; } diff --git a/src/main/java/nl/stil4m/mollie/domain/UpdateCustomer.java b/src/main/java/nl/stil4m/mollie/domain/UpdateCustomer.java new file mode 100644 index 0000000..1a23a60 --- /dev/null +++ b/src/main/java/nl/stil4m/mollie/domain/UpdateCustomer.java @@ -0,0 +1,45 @@ +package nl.stil4m.mollie.domain; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Map; +import java.util.Optional; + + +@JsonInclude(value= JsonInclude.Include.NON_ABSENT, content= JsonInclude.Include.NON_EMPTY) +public class UpdateCustomer { + + + private final Optional name; + private final Optional email; + private final Optional locale; + private final Map metadata; + + public UpdateCustomer(@JsonProperty("name") Optional name, + @JsonProperty("email") Optional email, + @JsonProperty("locale") Optional locale, + @JsonProperty("metadata") Map metadata) { + this.name = name; + this.email = email; + this.locale = locale; + this.metadata = metadata; + } + + public Optional getName() { + return name; + } + + public Optional getEmail() { + return email; + } + + public Optional getLocale() { + return locale; + } + + public Map getMetadata() { + return metadata; + } + +} diff --git a/src/main/java/nl/stil4m/mollie/domain/customerpayments/FirstRecurringPayment.java b/src/main/java/nl/stil4m/mollie/domain/customerpayments/FirstRecurringPayment.java new file mode 100644 index 0000000..68fbc04 --- /dev/null +++ b/src/main/java/nl/stil4m/mollie/domain/customerpayments/FirstRecurringPayment.java @@ -0,0 +1,12 @@ +package nl.stil4m.mollie.domain.customerpayments; + +import nl.stil4m.mollie.domain.CreatePayment; +import nl.stil4m.mollie.domain.CustomerPayment; + +public class FirstRecurringPayment extends CustomerPayment { + + public FirstRecurringPayment(CreatePayment createPayment) { + super(createPayment, "first"); + } + +} diff --git a/src/main/java/nl/stil4m/mollie/domain/customerpayments/NormalCustomerPayment.java b/src/main/java/nl/stil4m/mollie/domain/customerpayments/NormalCustomerPayment.java new file mode 100644 index 0000000..a85dfe9 --- /dev/null +++ b/src/main/java/nl/stil4m/mollie/domain/customerpayments/NormalCustomerPayment.java @@ -0,0 +1,13 @@ +package nl.stil4m.mollie.domain.customerpayments; + +import nl.stil4m.mollie.domain.CreatePayment; +import nl.stil4m.mollie.domain.CustomerPayment; + +public class NormalCustomerPayment extends CustomerPayment { + + + public NormalCustomerPayment(CreatePayment createPayment) { + super(createPayment, null); + } + +} diff --git a/src/main/java/nl/stil4m/mollie/domain/customerpayments/RecurringPayment.java b/src/main/java/nl/stil4m/mollie/domain/customerpayments/RecurringPayment.java new file mode 100644 index 0000000..fc21a04 --- /dev/null +++ b/src/main/java/nl/stil4m/mollie/domain/customerpayments/RecurringPayment.java @@ -0,0 +1,11 @@ +package nl.stil4m.mollie.domain.customerpayments; + +import nl.stil4m.mollie.domain.CreatePayment; +import nl.stil4m.mollie.domain.CustomerPayment; + +public class RecurringPayment extends CustomerPayment { + + public RecurringPayment(CreatePayment createPayment) { + super(createPayment, "recurring"); + } +} diff --git a/src/test/java/nl/stil4m/mollie/CustomerPaymentsIntegrationTest.java b/src/test/java/nl/stil4m/mollie/CustomerPaymentsIntegrationTest.java new file mode 100644 index 0000000..b912a22 --- /dev/null +++ b/src/test/java/nl/stil4m/mollie/CustomerPaymentsIntegrationTest.java @@ -0,0 +1,56 @@ +package nl.stil4m.mollie; + +import nl.stil4m.mollie.domain.CreateCustomer; +import nl.stil4m.mollie.domain.CreatePayment; +import nl.stil4m.mollie.domain.Customer; +import nl.stil4m.mollie.domain.CustomerPayment; +import nl.stil4m.mollie.domain.Page; +import nl.stil4m.mollie.domain.Payment; +import nl.stil4m.mollie.domain.customerpayments.FirstRecurringPayment; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; + +import static nl.stil4m.mollie.TestUtil.TEST_TIMEOUT; +import static nl.stil4m.mollie.TestUtil.VALID_API_KEY; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +public class CustomerPaymentsIntegrationTest { + + private Client client; + private Map defaultMetadata; + private Customer customer; + + @Before + public void before() throws InterruptedException, IOException { + Thread.sleep(TEST_TIMEOUT); + client = new ClientBuilder().withApiKey(VALID_API_KEY).build(); + + defaultMetadata = new HashMap<>(); + defaultMetadata.put("foo", "bar"); + + String uuid = UUID.randomUUID().toString(); + String name = "Test Customer " + uuid; + customer = client.customers().create(new CreateCustomer(name, "test@foobar.com", Optional.empty(), null)).getData(); + } + + @Test + public void testGetCustomerPayments() throws IOException, URISyntaxException { + ResponseOrError> all = client.customerPayments(customer.getId()).all(Optional.empty(), Optional.empty()); + assertThat(all.getSuccess(), is(true)); + } + + @Test + public void testCreateCustomerPayment() throws IOException, URISyntaxException { + CustomerPayment customerPayment = new FirstRecurringPayment(new CreatePayment(Optional.empty(), 1.00, "Some description", "http://example.com", Optional.empty(), null)); + ResponseOrError all = client.customerPayments(customer.getId()).create(customerPayment); + assertThat(all.getSuccess(), is(true)); + } +} diff --git a/src/test/java/nl/stil4m/mollie/CustomersIntegerationTest.java b/src/test/java/nl/stil4m/mollie/CustomersIntegerationTest.java new file mode 100644 index 0000000..2d3f050 --- /dev/null +++ b/src/test/java/nl/stil4m/mollie/CustomersIntegerationTest.java @@ -0,0 +1,88 @@ +package nl.stil4m.mollie; + +import nl.stil4m.mollie.domain.CreateCustomer; +import nl.stil4m.mollie.domain.Customer; +import nl.stil4m.mollie.domain.Page; +import nl.stil4m.mollie.domain.UpdateCustomer; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; + +import static nl.stil4m.mollie.TestUtil.TEST_TIMEOUT; +import static nl.stil4m.mollie.TestUtil.VALID_API_KEY; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +public class CustomersIntegerationTest { + + private Client client; + private Map defaultMetadata; + + @Before + public void before() throws InterruptedException { + Thread.sleep(TEST_TIMEOUT); + client = new ClientBuilder().withApiKey(VALID_API_KEY).build(); + + defaultMetadata = new HashMap<>(); + defaultMetadata.put("foo", "bar"); + } + + + @Test + public void testGetCustomers() throws IOException, URISyntaxException { + ResponseOrError> all = client.customers().all(Optional.empty(), Optional.empty()); + assertThat(all.getSuccess(), is(true)); + //TODO Expand + } + + @Test + public void testCreateCustomer() throws IOException, URISyntaxException { + String uuid = UUID.randomUUID().toString(); + + ResponseOrError createdCustomer = client.customers().create(new CreateCustomer( + "Test Customer " + uuid, + "test@foobar.nl", + Optional.of("gb_EN"), + defaultMetadata + )); + + assertThat(createdCustomer.getSuccess(), is(true)); + //TODO Expand + } + + @Test + public void testGetCustomer() throws IOException, URISyntaxException { + String uuid = UUID.randomUUID().toString(); + String originalName= "Test Customer " + uuid; + ResponseOrError createdCustomer = client.customers().create(new CreateCustomer(originalName, "test@foobar.nl", Optional.empty(), defaultMetadata)); + + ResponseOrError fetchedCustomer = client.customers().get(createdCustomer.getData().getId()); + assertThat(fetchedCustomer.getData().getName(),is(originalName)); + //TODO Expand + } + @Test + public void testUpdateCustomer() throws IOException, URISyntaxException { + String uuid = UUID.randomUUID().toString(); + String uuid2 = UUID.randomUUID().toString(); + String originalName = "Test Customer " + uuid; + String newName = "Test Customer " + uuid2; + ResponseOrError createdCustomer = client.customers().create(new CreateCustomer(originalName, "test@foobar.nl", Optional.empty(), null)); + + ResponseOrError update = client.customers().update(createdCustomer.getData().getId(), new UpdateCustomer( + Optional.of(newName), + Optional.of("test+2@foobar.nl"), + Optional.empty(), + defaultMetadata + )); + + assertThat(update.getSuccess(), is(true)); + assertThat(update.getData().getName(), is(newName)); + assertThat(update.getData().getEmail(), is("test+2@foobar.nl")); + } +} diff --git a/src/test/java/nl/stil4m/mollie/RecurringPaymentSerializationTest.java b/src/test/java/nl/stil4m/mollie/RecurringPaymentSerializationTest.java new file mode 100644 index 0000000..f96fc3d --- /dev/null +++ b/src/test/java/nl/stil4m/mollie/RecurringPaymentSerializationTest.java @@ -0,0 +1,37 @@ +package nl.stil4m.mollie; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import nl.stil4m.mollie.domain.CustomerPayment; +import nl.stil4m.mollie.domain.customerpayments.FirstRecurringPayment; +import nl.stil4m.mollie.domain.subpayments.ideal.CreateIdealPayment; +import nl.stil4m.mollie.domain.subpayments.ideal.IdealPaymentOptions; +import org.junit.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +public class RecurringPaymentSerializationTest { + + @Test + public void testSerialize() throws IOException { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.registerModule(new Jdk8Module()); + Map metaData = new HashMap<>(); + + CustomerPayment customerPayment = new FirstRecurringPayment(new CreateIdealPayment(1.0, "Description", "redirectUrl", Optional.empty(), metaData, new IdealPaymentOptions("MyIssuer"))); + + String serialized = objectMapper.writeValueAsString(customerPayment); + Map mapRepresentation = objectMapper.readValue(serialized, Map.class); + InputStream resourceAsStream = this.getClass().getResourceAsStream("/expected_first_recurring_payment.json"); + Map expected = objectMapper.readValue(resourceAsStream, Map.class); + assertThat(mapRepresentation, is(expected)); + } + +} diff --git a/src/test/resources/expected_first_recurring_payment.json b/src/test/resources/expected_first_recurring_payment.json new file mode 100644 index 0000000..5e81b5e --- /dev/null +++ b/src/test/resources/expected_first_recurring_payment.json @@ -0,0 +1,10 @@ +{ + "recurringType" : "first", + "amount": 1.0, + "description": "Description", + "redirectUrl": "redirectUrl", + "webhookUrl": null, + "method": "ideal", + "metadata": {}, + "issuer": "MyIssuer" +}