diff --git a/src/main/java/nl/stil4m/mollie/DynamicClientBuilder.java b/src/main/java/nl/stil4m/mollie/DynamicClientBuilder.java index 77fa588..3f811dc 100644 --- a/src/main/java/nl/stil4m/mollie/DynamicClientBuilder.java +++ b/src/main/java/nl/stil4m/mollie/DynamicClientBuilder.java @@ -30,8 +30,8 @@ public DynamicClientBuilder withMapper(ObjectMapper objectMapper) { } public DynamicClient build() { - final HttpClient client = this.client.orElseGet(() -> HttpClientBuilder.create().build()); - final String endpoint = this.endpoint.orElseGet(() -> "https://api.mollie.com/v1"); + final HttpClient client = this.client.orElseGet(HttpClientBuilder.create()::build); + final String endpoint = this.endpoint.orElse("https://api.mollie.com/v1"); final ObjectMapper objectMapper = this.objectMapper.orElseGet(ObjectMapper::new); objectMapper.registerModule(new Jdk8Module()); return new DynamicClient(endpoint, new RequestExecutor(client, objectMapper)); diff --git a/src/main/java/nl/stil4m/mollie/concepts/Customers.java b/src/main/java/nl/stil4m/mollie/concepts/Customers.java index 5c9183b..0045efc 100644 --- a/src/main/java/nl/stil4m/mollie/concepts/Customers.java +++ b/src/main/java/nl/stil4m/mollie/concepts/Customers.java @@ -9,7 +9,6 @@ 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; diff --git a/src/main/java/nl/stil4m/mollie/domain/CreateCustomer.java b/src/main/java/nl/stil4m/mollie/domain/CreateCustomer.java index 870465e..91f29fb 100644 --- a/src/main/java/nl/stil4m/mollie/domain/CreateCustomer.java +++ b/src/main/java/nl/stil4m/mollie/domain/CreateCustomer.java @@ -2,8 +2,6 @@ import com.fasterxml.jackson.annotation.JsonProperty; -import java.util.Date; -import java.util.List; import java.util.Map; import java.util.Optional; diff --git a/src/test/java/nl/stil4m/mollie/ClientBuilderTest.java b/src/test/java/nl/stil4m/mollie/ClientBuilderTest.java new file mode 100644 index 0000000..d6e561c --- /dev/null +++ b/src/test/java/nl/stil4m/mollie/ClientBuilderTest.java @@ -0,0 +1,24 @@ +package nl.stil4m.mollie; + + +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; + +public class ClientBuilderTest { + + ClientBuilder builder = new ClientBuilder(); + + + @Test(expected=IllegalArgumentException.class) + public void testBuildWithoutApiKey() { + builder.build(); + } + + @Test + public void testBuildWithApiKey() { + Client client = builder.withApiKey("test").build(); + + assertNotNull(client); + } +} \ No newline at end of file diff --git a/src/test/java/nl/stil4m/mollie/ClientIntegrationTest.java b/src/test/java/nl/stil4m/mollie/ClientIntegrationTest.java deleted file mode 100644 index 2b47b92..0000000 --- a/src/test/java/nl/stil4m/mollie/ClientIntegrationTest.java +++ /dev/null @@ -1,232 +0,0 @@ -package nl.stil4m.mollie; - -import com.google.common.collect.Sets; -import nl.stil4m.mollie.domain.CreatePayment; -import nl.stil4m.mollie.domain.CreatedPayment; -import nl.stil4m.mollie.domain.Issuer; -import nl.stil4m.mollie.domain.Method; -import nl.stil4m.mollie.domain.Page; -import nl.stil4m.mollie.domain.Payment; -import nl.stil4m.mollie.domain.Refund; -import nl.stil4m.mollie.domain.subpayments.ideal.CreateIdealPayment; -import nl.stil4m.mollie.domain.subpayments.ideal.IdealPaymentOptions; -import org.junit.Before; -import org.junit.Test; - -import java.io.IOException; -import java.net.URISyntaxException; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.stream.Collectors; - -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; -import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.fail; - -public class ClientIntegrationTest { - - private Client client; - - @Before - public void before() throws InterruptedException { - Thread.sleep(TEST_TIMEOUT); - client = new ClientBuilder().withApiKey(VALID_API_KEY).build(); - } - - @Test - public void testCreatePayment() throws IOException, InterruptedException { - Date beforeTest = new Date(); - ResponseOrError payment = client.payments().create(new CreatePayment(Optional.empty(), 1.00, "Some description", "http://example.com", Optional.empty(), null)); - - DynamicClientIntegrationTest.assertWithin(beforeTest, payment.getData().getCreatedDatetime(), new Date(), 5000L); - } - - @Test - public void testCreateIdealPayment() throws IOException, URISyntaxException, InterruptedException { - Date beforeTest = new Date(); - ResponseOrError> all = client.issuers().all(Optional.empty(), Optional.empty()); - assertThat(all.getSuccess(), is(true)); - Issuer issuer = all.getData().getData().get(0); - - ResponseOrError payment = client.payments().create(new CreateIdealPayment(1.00, "Some description", "http://example.com", Optional.empty(), null, new IdealPaymentOptions(issuer.getId()))); - - assertThat(payment.getSuccess(), is(true)); - DynamicClientIntegrationTest.assertWithin(beforeTest, payment.getData().getCreatedDatetime(), new Date(), 5000L); - } - - @Test - public void testGetPayment() throws IOException, InterruptedException { - ResponseOrError payment = client.payments().create(new CreatePayment(Optional.empty(), 1.00, "Some description", "http://example.com", Optional.empty(), null)); - String id = payment.getData().getId(); - - ResponseOrError paymentStatus = client.payments().get(id); - assertThat(paymentStatus.getData().getStatus(), is("open")); - } - - @Test - public void testGetPaymentWithRefunds() throws IOException, InterruptedException { - ResponseOrError getResponse = client.payments().get("tr_3AdTKpQGii"); - - getResponse.get(payment -> assertThat(payment.getLinks().getRefunds().isPresent(), is(true)), errorData -> System.out.println()); - - } - - - @Test - public void testGetPaymentWithEmptyId() throws IOException, InterruptedException { - try { - client.payments().get(""); - fail(); - } catch (IllegalArgumentException e) { - assertThat(e.getMessage(), is("Payment id may not be an empty string")); - } - } - - @Test - public void testGetPaymentWithNullId() throws IOException, InterruptedException { - try { - client.payments().get(null); - fail(); - } catch (IllegalArgumentException e) { - assertThat(e.getMessage(), is("Payment id may not be null")); - } - } - - @Test - public void testGetMethods() throws IOException, URISyntaxException, InterruptedException { - ResponseOrError> allResponse = client.methods().all(Optional.empty(), Optional.empty()); - assertThat(allResponse.getSuccess(), is(true)); - - Page all = allResponse.getData(); - assertThat(all.getCount(), is(10)); - assertThat(all.getOffset(), is(0)); - assertThat(all.getLinks(), is(notNullValue())); - - Set identifiers = all.getData().stream().map(Method::getId).collect(Collectors.toSet()); - assertThat(identifiers.containsAll(Sets.newHashSet("ideal", "creditcard", "paypal")), is(true)); - } - - @Test - public void testGetMethod() throws IOException, URISyntaxException, InterruptedException { - ResponseOrError methodResponse = client.methods().get("ideal"); - assertThat(methodResponse.getSuccess(), is(true)); - - Method method = methodResponse.getData(); - assertThat(method.getId(), is("ideal")); - assertThat(method.getDescription(), is("iDEAL")); - assertThat(method.getAmount().getMinimum(), is(0.36)); - assertThat(method.getAmount().getMaximum(), is(50000.0)); - assertThat(method.getImage().getNormal(), is("https://www.mollie.com/images/payscreen/methods/ideal.png")); - assertThat(method.getImage().getBigger(), is("https://www.mollie.com/images/payscreen/methods/ideal@2x.png")); - } - - @Test - public void testGetInvalidMethod() throws IOException, InterruptedException { - ResponseOrError methodResponse = client.methods().get("no-such-method"); - assertThat(methodResponse.getSuccess(), is(false)); - assertThat(methodResponse.getStatus(), is(404)); - assertThat(methodResponse.getError().keySet(), is(Sets.newHashSet("error"))); - } - - @Test - public void testGetIssuers() throws IOException, URISyntaxException, InterruptedException { - ResponseOrError> allResponse = client.issuers().all(Optional.empty(), Optional.empty()); - assertThat(allResponse.getSuccess(), is(true)); - - Page all = allResponse.getData(); - assertThat(all.getCount(), is(1)); - assertThat(all.getTotalCount(), is(1)); - assertThat(all.getOffset(), is(0)); - assertThat(all.getLinks(), is(notNullValue())); - assertThat(all.getLinks().getPrevious().isPresent(), is(false)); - assertThat(all.getLinks().getNext().isPresent(), is(false)); - - Set identifiers = all.getData().stream().map(Issuer::getId).collect(Collectors.toSet()); - assertThat(identifiers.containsAll(Sets.newHashSet("ideal_TESTNL99")), is(true)); - } - - @Test - public void testGetIssuer() throws IOException, URISyntaxException, InterruptedException { - ResponseOrError allResponse = client.issuers().get("ideal_TESTNL99"); - assertThat(allResponse.getSuccess(), is(true)); - - Issuer issuer = allResponse.getData(); - assertThat(issuer.getResource(), is("issuer")); - assertThat(issuer.getId(), is("ideal_TESTNL99")); - assertThat(issuer.getName(), is("TBM Bank")); - assertThat(issuer.getMethod(), is("ideal")); - } - - @Test - public void testGetRefunds() throws IOException, URISyntaxException, InterruptedException { - ResponseOrError payment = client.payments().create(new CreatePayment(Optional.empty(), 1.00, "Some description", "http://example.com", Optional.empty(), null)); - String id = payment.getData().getId(); - - ResponseOrError> all = client.refunds().all(id, Optional.empty(), Optional.empty()); - assertThat(all.getSuccess(), is(true)); - Page refundPage = all.getData(); - assertThat(refundPage.getCount(), is(0)); - assertThat(refundPage.getData(), is(notNullValue())); - assertThat(refundPage.getLinks(), is(notNullValue())); - } - - @Test - public void testListRefundsForExistingPayment() throws IOException, URISyntaxException, InterruptedException { - CreatedPayment createdPayment = client.payments().create(new CreatePayment(Optional.empty(), 1.00, "Some description", "http://example.com", Optional.empty(), null)).getData(); - ResponseOrError> all = client.refunds().all(createdPayment.getId(), Optional.empty(), Optional.empty()); - - assertThat(all.getSuccess(), is(true)); - - Page data = all.getData(); - assertThat(data.getData().size(), is(0)); - } - - - @Test - public void testCancelNonExistingRefund() throws IOException { - Map errorData = new HashMap<>(); - errorData.put("type", "request"); - errorData.put("message", "The refund id is invalid"); - ResponseOrError payment = client.payments().create(new CreatePayment(Optional.empty(), 1.00, "Some description", "http://example.com", Optional.empty(), null)); - - assertThat(payment.getSuccess(), is(true)); - String id = payment.getData().getId(); - - ResponseOrError cancel = client.refunds().cancel(id, "foo_bar"); - assertThat(cancel.getSuccess(), is(false)); - assertThat(cancel.getError().get("error"), is(errorData)); - } - - @Test - public void testGetNonExistingRefund() throws IOException { - Map errorData = new HashMap<>(); - errorData.put("type", "request"); - errorData.put("message", "The refund id is invalid"); - ResponseOrError payment = client.payments().create(new CreatePayment(Optional.empty(), 1.00, "Some description", "http://example.com", Optional.empty(), null)); - String id = payment.getData().getId(); - - ResponseOrError get = client.refunds().get(id, "foo_bar"); - assertThat(get.getSuccess(), is(false)); - assertThat(get.getError().get("error"), is(errorData)); - } - - @Test - public void testCreateRefund() throws IOException, URISyntaxException { - Map errorData = new HashMap<>(); - errorData.put("type", "request"); - errorData.put("message", "The payment is already refunded or has not been paid for yet"); - - ResponseOrError payment = client.payments().create(new CreatePayment(Optional.empty(), 1.00, "Some description", "http://example.com", Optional.empty(), null)); - String id = payment.getData().getId(); - - ResponseOrError create = client.refunds().create(id, Optional.of(1.00)); - assertThat(create.getSuccess(), is(false)); - assertThat(create.getError().get("error"), is(errorData)); - } -} diff --git a/src/test/java/nl/stil4m/mollie/DynamicClientIntegrationTest.java b/src/test/java/nl/stil4m/mollie/DynamicClientIntegrationTest.java index e7e2317..f7239b9 100644 --- a/src/test/java/nl/stil4m/mollie/DynamicClientIntegrationTest.java +++ b/src/test/java/nl/stil4m/mollie/DynamicClientIntegrationTest.java @@ -1,26 +1,16 @@ package nl.stil4m.mollie; -import com.fasterxml.jackson.databind.ObjectMapper; -import nl.stil4m.mollie.domain.CreatePayment; -import nl.stil4m.mollie.domain.CreatedPayment; -import nl.stil4m.mollie.domain.Page; -import nl.stil4m.mollie.domain.Payment; -import org.junit.Before; -import org.junit.Test; - -import java.io.IOException; -import java.net.URISyntaxException; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - 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; -import static org.hamcrest.Matchers.notNullValue; -import static org.hamcrest.Matchers.nullValue; + +import java.io.IOException; + +import org.junit.Before; +import org.junit.Test; + +import com.fasterxml.jackson.databind.ObjectMapper; public class DynamicClientIntegrationTest { @@ -43,178 +33,4 @@ public void validateInvalidApiKey() throws IOException { public void validateValidApiKey() throws IOException { assertThat(client.status(VALID_API_KEY).checkApiKey().getValid(), is(true)); } - - @Test - public void testCreatePayment() throws IOException { - Date beforeTest = new Date(); - Map meta = new HashMap<>(); - meta.put("foo", "bar"); - - ResponseOrError payment = client.payments(VALID_API_KEY).create(new CreatePayment(Optional.empty(), 1.00, "Some description", "http://example.com", Optional.empty(), meta)); - - CreatedPayment createdPayment = payment.getData(); - assertWithin(beforeTest, createdPayment.getCreatedDatetime(), new Date(), 5000L); - - assertThat(createdPayment.getMethod(), is(nullValue())); - assertThat(createdPayment.getAmount(), is(1.00)); - assertThat(createdPayment.getDescription(), is("Some description")); - assertThat(createdPayment.getId(), is(notNullValue())); - assertThat(createdPayment.getDetails(), is(nullValue())); - assertThat(createdPayment.getLinks(), is(notNullValue())); - assertThat(createdPayment.getLinks().getPaymentUrl().matches("https://www.mollie.com/payscreen/select-method/[A-Za-z0-9]+"), is(true)); - assertThat(createdPayment.getLinks().getRedirectUrl(), is("http://example.com")); - assertThat(createdPayment.getLinks().getWebhookUrl(), is("https://stil4m.github.io")); - assertThat(createdPayment.getMode(), is("test")); - assertThat(createdPayment.getStatus(), is("open")); - assertThat(createdPayment.getMetadata(), is(meta)); - } - - @Test - public void testCreatePaymentWithMethod() throws IOException { - Date beforeTest = new Date(); - Map meta = new HashMap<>(); - meta.put("foo", "bar"); - - ResponseOrError payment = client.payments(VALID_API_KEY).create(new CreatePayment(Optional.of("ideal"), 1.00, "Some description", "http://example.com", Optional.empty(), meta)); - - CreatedPayment createdPayment = payment.getData(); - assertWithin(beforeTest, createdPayment.getCreatedDatetime(), new Date(), 5000L); - - assertThat(createdPayment.getMethod(), is("ideal")); - assertThat(createdPayment.getAmount(), is(1.00)); - assertThat(createdPayment.getDescription(), is("Some description")); - assertThat(createdPayment.getId(), is(notNullValue())); - assertThat(createdPayment.getDetails(), is(nullValue())); - assertThat(createdPayment.getLinks(), is(notNullValue())); - assertThat(createdPayment.getLinks().getPaymentUrl().matches("https://www.mollie.com/paymentscreen/ideal/select-issuer/[A-Za-z0-9]+"), is(true)); - assertThat(createdPayment.getLinks().getRedirectUrl(), is("http://example.com")); - assertThat(createdPayment.getLinks().getWebhookUrl(), is(is("https://stil4m.github.io"))); - assertThat(createdPayment.getMode(), is("test")); - assertThat(createdPayment.getStatus(), is("open")); - assertThat(createdPayment.getMetadata(), is(meta)); - } - - - @Test - public void testCreateAndGetPayment() throws IOException { - Map meta = new HashMap<>(); - meta.put("foo", "bar"); - - ResponseOrError createResponse = client.payments(VALID_API_KEY).create(new CreatePayment(Optional.empty(), 1.00, "Some description", "http://example.com", Optional.empty(), meta)); - ResponseOrError paymentResponse = client.payments(VALID_API_KEY).get(createResponse.getData().getId()); - Payment payment = paymentResponse.getData(); - - assertThat(payment.getMethod(), is(nullValue())); - assertThat(payment.getAmount(), is(1.00)); - assertThat(payment.getDescription(), is("Some description")); - assertThat(payment.getId(), is(notNullValue())); - assertThat(payment.getDetails(), is(nullValue())); - assertThat(payment.getLinks(), is(notNullValue())); - assertThat(payment.getLinks().getPaymentUrl().matches("https://www.mollie.com/payscreen/select-method/[A-Za-z0-9]+"), is(true)); - assertThat(payment.getLinks().getRedirectUrl(), is("http://example.com")); - assertThat(payment.getLinks().getWebhookUrl(), is(is("https://stil4m.github.io"))); - assertThat(payment.getMode(), is("test")); - assertThat(payment.getStatus(), is("open")); - assertThat(payment.getMetadata(), is(meta)); - - } - - @Test - public void testCreateAndGetCreditCardPayment() throws IOException { - Map meta = new HashMap<>(); - meta.put("foo", "bar"); - - ResponseOrError createResponse = client.payments(VALID_API_KEY).create(new CreatePayment(Optional.of("creditcard"), 2.00, "Some credit card description", "http://example.com", Optional.empty(), meta)); - ResponseOrError paymentResponse = client.payments(VALID_API_KEY).get(createResponse.getData().getId()); - Payment payment = paymentResponse.getData(); - - assertThat(payment.getMethod(), is("creditcard")); - assertThat(payment.getAmount(), is(2.00)); - assertThat(payment.getDescription(), is("Some credit card description")); - assertThat(payment.getId(), is(notNullValue())); - assertThat(payment.getDetails(), is(notNullValue())); // feeRegion=other - assertThat(payment.getLinks(), is(notNullValue())); - assertThat(payment.getLinks().getPaymentUrl().matches("https://www.mollie.com/paymentscreen/creditcard/testmode/[A-Za-z0-9]+"), is(true)); - assertThat(payment.getLinks().getRedirectUrl(), is("http://example.com")); - assertThat(payment.getLinks().getWebhookUrl(), is(is("https://stil4m.github.io"))); - assertThat(payment.getMode(), is("test")); - assertThat(payment.getStatus(), is("open")); - assertThat(payment.getMetadata(), is(meta)); - - } - - - public static void assertWithin(Date before, Date target, Date after, Long additionalSpan) { - long beforeTime = before.getTime() - (before.getTime() % 1000) - additionalSpan; - long afterTime = after.getTime() - (after.getTime() % 1000) + additionalSpan; - assertThat(beforeTime <= target.getTime(), is(true)); - assertThat(target.getTime() <= afterTime, is(true)); - } - - @Test - public void testGetPayment() throws IOException { - ResponseOrError payment = client.payments(VALID_API_KEY).create(new CreatePayment(Optional.empty(), 1.00, "Some description", "http://example.com", Optional.empty(), null)); - String id = payment.getData().getId(); - - ResponseOrError paymentStatus = client.payments(VALID_API_KEY).get(id); - assertThat(paymentStatus.getData().getStatus(), is("open")); - } - - @Test - public void testGetPayments() throws IOException, URISyntaxException { - ResponseOrError> result = client.payments(VALID_API_KEY).list(Optional.empty(), Optional.empty()); - assertThat(result.getSuccess(), is(true)); - Page page = result.getData(); - - assertThat(page.getTotalCount(), is(notNullValue())); - assertThat(page.getData().size(), is(Math.min(10, page.getTotalCount()))); - assertThat(page.getOffset(), is(0)); - assertThat(page.getCount(), is(10)); - - assertThat(page.getLinks().getPrevious().isPresent(), is(false)); - assertThat(page.getLinks().getNext().isPresent(), is(true)); - assertThat(page.getLinks().getLast().isPresent(), is(true)); - assertThat(page.getLinks().getFirst().isPresent(), is(true)); - } - - @Test - public void testGetPaymentsWithOffsetAndCount() throws IOException, URISyntaxException { - ResponseOrError> result = client.payments(VALID_API_KEY).list(Optional.of(20), Optional.of(40)); - assertThat(result.getSuccess(), is(true)); - Page page = result.getData(); - - assertThat(page.getTotalCount(), is(notNullValue())); - assertThat(page.getData().size(), is(Math.min(20, page.getTotalCount()))); - assertThat(page.getOffset(), is(40)); - assertThat(page.getCount(), is(20)); - - assertThat(page.getLinks().getPrevious().isPresent(), is(true)); - assertThat(page.getLinks().getNext().isPresent(), is(true)); - assertThat(page.getLinks().getLast().isPresent(), is(true)); - assertThat(page.getLinks().getFirst().isPresent(), is(true)); - } - - @Test - public void testNextPayments() throws IOException, URISyntaxException { - ResponseOrError> result = client.payments(VALID_API_KEY).list(Optional.empty(), Optional.empty()); - assertThat(result.getSuccess(), is(true)); - Page page = result.getData(); - - Page nextPage = client.payments(VALID_API_KEY).next(page).getData(); - - assertThat(nextPage.getOffset(), is(10)); - assertThat(nextPage.getCount(), is(10)); - } - - @Test - public void testPreviousPayments() throws IOException, URISyntaxException { - ResponseOrError> result = client.payments(VALID_API_KEY).list(Optional.of(20), Optional.of(40)); - assertThat(result.getSuccess(), is(true)); - Page page = result.getData(); - - Page previousPage = client.payments(VALID_API_KEY).previous(page).getData(); - - assertThat(previousPage.getOffset(), is(20)); - assertThat(previousPage.getCount(), is(20)); - } } diff --git a/src/test/java/nl/stil4m/mollie/TestUtil.java b/src/test/java/nl/stil4m/mollie/TestUtil.java index b0ce5c2..49e6a9f 100644 --- a/src/test/java/nl/stil4m/mollie/TestUtil.java +++ b/src/test/java/nl/stil4m/mollie/TestUtil.java @@ -1,8 +1,21 @@ package nl.stil4m.mollie; +import java.util.Date; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.lessThanOrEqualTo; + public class TestUtil { public static final String VALID_API_KEY = "test_35Rjpa5ETePvpeQVU2JQEwzuNyq8BA"; public static final Long TEST_TIMEOUT = 2000L; + + public static void assertWithin(Date before, Date target, Date after, Long additionalSpan) { + long beforeTime = before.getTime() - (before.getTime() % 1000) - additionalSpan; + long afterTime = after.getTime() - (after.getTime() % 1000) + additionalSpan; + + assertThat(new Date(beforeTime), lessThanOrEqualTo(target)); + assertThat(target, lessThanOrEqualTo(new Date(afterTime))); + } } diff --git a/src/test/java/nl/stil4m/mollie/concepts/CustomerPaymentsIntegrationTest.java b/src/test/java/nl/stil4m/mollie/concepts/CustomerPaymentsIntegrationTest.java index a9c3d47..1dd1512 100644 --- a/src/test/java/nl/stil4m/mollie/concepts/CustomerPaymentsIntegrationTest.java +++ b/src/test/java/nl/stil4m/mollie/concepts/CustomerPaymentsIntegrationTest.java @@ -1,5 +1,20 @@ package nl.stil4m.mollie.concepts; +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; + +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 org.junit.Before; +import org.junit.Test; + import nl.stil4m.mollie.Client; import nl.stil4m.mollie.ClientBuilder; import nl.stil4m.mollie.ResponseOrError; @@ -10,30 +25,16 @@ 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 CustomerPayments customerPayments; private Customer customer; @Before public void before() throws InterruptedException, IOException { Thread.sleep(TEST_TIMEOUT); - client = new ClientBuilder().withApiKey(VALID_API_KEY).build(); + Client client = new ClientBuilder().withApiKey(VALID_API_KEY).build(); Map defaultMetadata = new HashMap<>(); defaultMetadata.put("foo", "bar"); @@ -41,18 +42,23 @@ public void before() throws InterruptedException, IOException { String uuid = UUID.randomUUID().toString(); String name = "Test Customer " + uuid; customer = client.customers().create(new CreateCustomer(name, "test@foobar.com", Optional.empty(), defaultMetadata)).getData(); + + customerPayments = client.customerPayments(customer.getId()); } @Test public void testGetCustomerPayments() throws IOException, URISyntaxException { - ResponseOrError> all = client.customerPayments(customer.getId()).all(Optional.empty(), Optional.empty()); + ResponseOrError> all = customerPayments.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); + + ResponseOrError all = customerPayments.create(customerPayment); + assertThat(all.getSuccess(), is(true)); } } diff --git a/src/test/java/nl/stil4m/mollie/concepts/CustomersIntegrationTest.java b/src/test/java/nl/stil4m/mollie/concepts/CustomersIntegrationTest.java index bd80f94..c4990b8 100644 --- a/src/test/java/nl/stil4m/mollie/concepts/CustomersIntegrationTest.java +++ b/src/test/java/nl/stil4m/mollie/concepts/CustomersIntegrationTest.java @@ -1,14 +1,9 @@ package nl.stil4m.mollie.concepts; -import nl.stil4m.mollie.Client; -import nl.stil4m.mollie.ClientBuilder; -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.junit.Before; -import org.junit.Test; +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; import java.io.IOException; import java.net.URISyntaxException; @@ -17,20 +12,25 @@ 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; +import org.junit.Before; +import org.junit.Test; + +import nl.stil4m.mollie.ClientBuilder; +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; public class CustomersIntegrationTest { - private Client client; + private Customers customers; private Map defaultMetadata; @Before public void before() throws InterruptedException { Thread.sleep(TEST_TIMEOUT); - client = new ClientBuilder().withApiKey(VALID_API_KEY).build(); + customers = new ClientBuilder().withApiKey(VALID_API_KEY).build().customers(); defaultMetadata = new HashMap<>(); defaultMetadata.put("foo", "bar"); @@ -39,7 +39,7 @@ public void before() throws InterruptedException { @Test public void testGetCustomers() throws IOException, URISyntaxException { - ResponseOrError> all = client.customers().all(Optional.empty(), Optional.empty()); + ResponseOrError> all = customers.all(Optional.empty(), Optional.empty()); assertThat(all.getSuccess(), is(true)); //TODO Expand } @@ -48,7 +48,7 @@ public void testGetCustomers() throws IOException, URISyntaxException { public void testCreateCustomer() throws IOException, URISyntaxException { String uuid = UUID.randomUUID().toString(); - ResponseOrError createdCustomer = client.customers().create(new CreateCustomer( + ResponseOrError createdCustomer = customers.create(new CreateCustomer( "Test Customer " + uuid, "test@foobar.nl", Optional.of("gb_EN"), @@ -63,9 +63,9 @@ public void testCreateCustomer() throws IOException, URISyntaxException { 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 createdCustomer = customers.create(new CreateCustomer(originalName, "test@foobar.nl", Optional.empty(), defaultMetadata)); - ResponseOrError fetchedCustomer = client.customers().get(createdCustomer.getData().getId()); + ResponseOrError fetchedCustomer = customers.get(createdCustomer.getData().getId()); assertThat(fetchedCustomer.getData().getName(), is(originalName)); //TODO Expand } @@ -74,9 +74,9 @@ public void testGetCustomer() throws IOException, URISyntaxException { public void testUpdateCustomerEmail() 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(), null)); + ResponseOrError createdCustomer = customers.create(new CreateCustomer(originalName, "test@foobar.nl", Optional.empty(), null)); - ResponseOrError update = client.customers().update(createdCustomer.getData().getId(), new UpdateCustomer( + ResponseOrError update = customers.update(createdCustomer.getData().getId(), new UpdateCustomer( Optional.empty(), Optional.of("test+2@foobar.nl"), Optional.empty(), @@ -94,9 +94,9 @@ public void testUpdateCustomerName() throws IOException, URISyntaxException { 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 createdCustomer = customers.create(new CreateCustomer(originalName, "test@foobar.nl", Optional.empty(), null)); - ResponseOrError update = client.customers().update(createdCustomer.getData().getId(), new UpdateCustomer( + ResponseOrError update = customers.update(createdCustomer.getData().getId(), new UpdateCustomer( Optional.of(newName), Optional.empty(), Optional.empty(), diff --git a/src/test/java/nl/stil4m/mollie/concepts/IssuersIntegrationTest.java b/src/test/java/nl/stil4m/mollie/concepts/IssuersIntegrationTest.java new file mode 100644 index 0000000..49101f0 --- /dev/null +++ b/src/test/java/nl/stil4m/mollie/concepts/IssuersIntegrationTest.java @@ -0,0 +1,65 @@ +package nl.stil4m.mollie.concepts; + +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; +import static org.hamcrest.Matchers.notNullValue; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +import org.junit.Before; +import org.junit.Test; + +import com.google.common.collect.Sets; + +import nl.stil4m.mollie.ClientBuilder; +import nl.stil4m.mollie.ResponseOrError; +import nl.stil4m.mollie.domain.Issuer; +import nl.stil4m.mollie.domain.Page; + +public class IssuersIntegrationTest { + + private Issuers issuers; + + @Before + public void before() throws InterruptedException { + Thread.sleep(TEST_TIMEOUT); + issuers = new ClientBuilder().withApiKey(VALID_API_KEY).build().issuers(); + } + + @Test + public void testGetIssuers() throws IOException, URISyntaxException, InterruptedException { + ResponseOrError> allResponse = issuers.all(Optional.empty(), Optional.empty()); + + assertThat(allResponse.getSuccess(), is(true)); + + Page all = allResponse.getData(); + assertThat(all.getCount(), is(1)); + assertThat(all.getTotalCount(), is(1)); + assertThat(all.getOffset(), is(0)); + assertThat(all.getLinks(), is(notNullValue())); + assertThat(all.getLinks().getPrevious().isPresent(), is(false)); + assertThat(all.getLinks().getNext().isPresent(), is(false)); + + Set identifiers = all.getData().stream().map(Issuer::getId).collect(Collectors.toSet()); + assertThat(identifiers.containsAll(Sets.newHashSet("ideal_TESTNL99")), is(true)); + } + + @Test + public void testGetIssuer() throws IOException, URISyntaxException, InterruptedException { + ResponseOrError allResponse = issuers.get("ideal_TESTNL99"); + + assertThat(allResponse.getSuccess(), is(true)); + + Issuer issuer = allResponse.getData(); + assertThat(issuer.getResource(), is("issuer")); + assertThat(issuer.getId(), is("ideal_TESTNL99")); + assertThat(issuer.getName(), is("TBM Bank")); + assertThat(issuer.getMethod(), is("ideal")); + } +} \ No newline at end of file diff --git a/src/test/java/nl/stil4m/mollie/concepts/MethodsIntegrationTest.java b/src/test/java/nl/stil4m/mollie/concepts/MethodsIntegrationTest.java new file mode 100644 index 0000000..311f713 --- /dev/null +++ b/src/test/java/nl/stil4m/mollie/concepts/MethodsIntegrationTest.java @@ -0,0 +1,73 @@ +package nl.stil4m.mollie.concepts; + +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; +import static org.hamcrest.Matchers.notNullValue; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +import org.junit.Before; +import org.junit.Test; + +import com.google.common.collect.Sets; + +import nl.stil4m.mollie.ClientBuilder; +import nl.stil4m.mollie.ResponseOrError; +import nl.stil4m.mollie.domain.Method; +import nl.stil4m.mollie.domain.Page; + +public class MethodsIntegrationTest { + + private Methods methods; + + @Before + public void before() throws InterruptedException { + Thread.sleep(TEST_TIMEOUT); + methods = new ClientBuilder().withApiKey(VALID_API_KEY).build().methods(); + } + + @Test + public void testGetMethods() throws IOException, URISyntaxException, InterruptedException { + ResponseOrError> allResponse = methods.all(Optional.empty(), Optional.empty()); + + assertThat(allResponse.getSuccess(), is(true)); + + Page all = allResponse.getData(); + assertThat(all.getCount(), is(10)); + assertThat(all.getOffset(), is(0)); + assertThat(all.getLinks(), is(notNullValue())); + + Set identifiers = all.getData().stream().map(Method::getId).collect(Collectors.toSet()); + assertThat(identifiers.containsAll(Sets.newHashSet("ideal", "creditcard", "paypal")), is(true)); + } + + @Test + public void testGetMethod() throws IOException, URISyntaxException, InterruptedException { + ResponseOrError methodResponse = methods.get("ideal"); + + assertThat(methodResponse.getSuccess(), is(true)); + + Method method = methodResponse.getData(); + assertThat(method.getId(), is("ideal")); + assertThat(method.getDescription(), is("iDEAL")); + assertThat(method.getAmount().getMinimum(), is(0.36)); + assertThat(method.getAmount().getMaximum(), is(50000.0)); + assertThat(method.getImage().getNormal(), is("https://www.mollie.com/images/payscreen/methods/ideal.png")); + assertThat(method.getImage().getBigger(), is("https://www.mollie.com/images/payscreen/methods/ideal@2x.png")); + } + + @Test + public void testGetInvalidMethod() throws IOException, InterruptedException { + ResponseOrError methodResponse = methods.get("no-such-method"); + + assertThat(methodResponse.getSuccess(), is(false)); + assertThat(methodResponse.getStatus(), is(404)); + assertThat(methodResponse.getError().keySet(), is(Sets.newHashSet("error"))); + } +} diff --git a/src/test/java/nl/stil4m/mollie/concepts/PaymentsIntegrationTest.java b/src/test/java/nl/stil4m/mollie/concepts/PaymentsIntegrationTest.java new file mode 100644 index 0000000..5322983 --- /dev/null +++ b/src/test/java/nl/stil4m/mollie/concepts/PaymentsIntegrationTest.java @@ -0,0 +1,255 @@ +package nl.stil4m.mollie.concepts; + +import static nl.stil4m.mollie.TestUtil.TEST_TIMEOUT; +import static nl.stil4m.mollie.TestUtil.VALID_API_KEY; +import static nl.stil4m.mollie.TestUtil.assertWithin; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import org.junit.Before; +import org.junit.Test; + +import nl.stil4m.mollie.Client; +import nl.stil4m.mollie.ClientBuilder; +import nl.stil4m.mollie.ResponseOrError; +import nl.stil4m.mollie.domain.CreatePayment; +import nl.stil4m.mollie.domain.CreatedPayment; +import nl.stil4m.mollie.domain.Issuer; +import nl.stil4m.mollie.domain.Page; +import nl.stil4m.mollie.domain.Payment; +import nl.stil4m.mollie.domain.subpayments.ideal.CreateIdealPayment; +import nl.stil4m.mollie.domain.subpayments.ideal.IdealPaymentOptions; + +public class PaymentsIntegrationTest { + + private Payments payments; + private Issuers issuers; + + @Before + public void before() throws InterruptedException { + Thread.sleep(TEST_TIMEOUT); + Client client = new ClientBuilder().withApiKey(VALID_API_KEY).build(); + payments = client.payments(); + issuers = client.issuers(); + } + + @Test + public void testCreateIdealPayment() throws IOException, URISyntaxException, InterruptedException { + Date beforeTest = new Date(); + ResponseOrError> all = issuers.all(Optional.empty(), Optional.empty()); + assertThat(all.getSuccess(), is(true)); + Issuer issuer = all.getData().getData().get(0); + + ResponseOrError payment = payments.create(new CreateIdealPayment(1.00, "Some description", "http://example.com", Optional.empty(), null, new IdealPaymentOptions(issuer.getId()))); + + assertThat(payment.getSuccess(), is(true)); + assertWithin(beforeTest, payment.getData().getCreatedDatetime(), new Date(), 5000L); + } + + @Test + public void testGetPayment() throws IOException, InterruptedException { + ResponseOrError payment = payments.create(new CreatePayment(Optional.empty(), 1.00, "Some description", "http://example.com", Optional.empty(), null)); + String id = payment.getData().getId(); + + ResponseOrError paymentStatus = payments.get(id); + + assertThat(paymentStatus.getData().getStatus(), is("open")); + } + + @Test + public void testGetPaymentWithRefunds() throws IOException, InterruptedException { + ResponseOrError getResponse = payments.get("tr_3AdTKpQGii"); + + getResponse.get(payment -> assertThat(payment.getLinks().getRefunds().isPresent(), is(true)), errorData -> System.out.println()); + + } + + @Test + public void testGetPaymentWithEmptyId() throws IOException, InterruptedException { + try { + payments.get(""); + fail(); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage(), is("Payment id may not be an empty string")); + } + } + + @Test + public void testGetPaymentWithNullId() throws IOException, InterruptedException { + try { + payments.get(null); + fail(); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage(), is("Payment id may not be null")); + } + } + + + + @Test + public void testCreatePayment() throws IOException { + Date beforeTest = new Date(); + Map meta = new HashMap<>(); + meta.put("foo", "bar"); + + ResponseOrError payment = payments.create(new CreatePayment(Optional.empty(), 1.00, "Some description", "http://example.com", Optional.empty(), meta)); + + CreatedPayment createdPayment = payment.getData(); + assertWithin(beforeTest, createdPayment.getCreatedDatetime(), new Date(), 5000L); + + assertThat(createdPayment.getMethod(), is(nullValue())); + assertThat(createdPayment.getAmount(), is(1.00)); + assertThat(createdPayment.getDescription(), is("Some description")); + assertThat(createdPayment.getId(), is(notNullValue())); + assertThat(createdPayment.getDetails(), is(nullValue())); + assertThat(createdPayment.getLinks(), is(notNullValue())); + assertThat(createdPayment.getLinks().getPaymentUrl().matches("https://www.mollie.com/payscreen/select-method/[A-Za-z0-9]+"), is(true)); + assertThat(createdPayment.getLinks().getRedirectUrl(), is("http://example.com")); + assertThat(createdPayment.getLinks().getWebhookUrl(), is("https://stil4m.github.io")); + assertThat(createdPayment.getMode(), is("test")); + assertThat(createdPayment.getStatus(), is("open")); + assertThat(createdPayment.getMetadata(), is(meta)); + } + + @Test + public void testCreatePaymentWithMethod() throws IOException { + Date beforeTest = new Date(); + Map meta = new HashMap<>(); + meta.put("foo", "bar"); + + ResponseOrError payment = payments.create(new CreatePayment(Optional.of("ideal"), 1.00, "Some description", "http://example.com", Optional.empty(), meta)); + + CreatedPayment createdPayment = payment.getData(); + assertWithin(beforeTest, createdPayment.getCreatedDatetime(), new Date(), 5000L); + + assertThat(createdPayment.getMethod(), is("ideal")); + assertThat(createdPayment.getAmount(), is(1.00)); + assertThat(createdPayment.getDescription(), is("Some description")); + assertThat(createdPayment.getId(), is(notNullValue())); + assertThat(createdPayment.getDetails(), is(nullValue())); + assertThat(createdPayment.getLinks(), is(notNullValue())); + assertThat(createdPayment.getLinks().getPaymentUrl().matches("https://www.mollie.com/paymentscreen/ideal/select-issuer/[A-Za-z0-9]+"), is(true)); + assertThat(createdPayment.getLinks().getRedirectUrl(), is("http://example.com")); + assertThat(createdPayment.getLinks().getWebhookUrl(), is(is("https://stil4m.github.io"))); + assertThat(createdPayment.getMode(), is("test")); + assertThat(createdPayment.getStatus(), is("open")); + assertThat(createdPayment.getMetadata(), is(meta)); + } + + + @Test + public void testCreateAndGetPayment() throws IOException { + Map meta = new HashMap<>(); + meta.put("foo", "bar"); + + ResponseOrError createResponse = payments.create(new CreatePayment(Optional.empty(), 1.00, "Some description", "http://example.com", Optional.empty(), meta)); + ResponseOrError paymentResponse = payments.get(createResponse.getData().getId()); + Payment payment = paymentResponse.getData(); + + assertThat(payment.getMethod(), is(nullValue())); + assertThat(payment.getAmount(), is(1.00)); + assertThat(payment.getDescription(), is("Some description")); + assertThat(payment.getId(), is(notNullValue())); + assertThat(payment.getDetails(), is(nullValue())); + assertThat(payment.getLinks(), is(notNullValue())); + assertThat(payment.getLinks().getPaymentUrl().matches("https://www.mollie.com/payscreen/select-method/[A-Za-z0-9]+"), is(true)); + assertThat(payment.getLinks().getRedirectUrl(), is("http://example.com")); + assertThat(payment.getLinks().getWebhookUrl(), is(is("https://stil4m.github.io"))); + assertThat(payment.getMode(), is("test")); + assertThat(payment.getStatus(), is("open")); + assertThat(payment.getMetadata(), is(meta)); + + } + + @Test + public void testCreateAndGetCreditCardPayment() throws IOException { + Map meta = new HashMap<>(); + meta.put("foo", "bar"); + + ResponseOrError createResponse = payments.create(new CreatePayment(Optional.of("creditcard"), 2.00, "Some credit card description", "http://example.com", Optional.empty(), meta)); + ResponseOrError paymentResponse = payments.get(createResponse.getData().getId()); + Payment payment = paymentResponse.getData(); + + assertThat(payment.getMethod(), is("creditcard")); + assertThat(payment.getAmount(), is(2.00)); + assertThat(payment.getDescription(), is("Some credit card description")); + assertThat(payment.getId(), is(notNullValue())); + assertThat(payment.getDetails(), is(notNullValue())); // feeRegion=other + assertThat(payment.getLinks(), is(notNullValue())); + assertThat(payment.getLinks().getPaymentUrl().matches("https://www.mollie.com/paymentscreen/creditcard/testmode/[A-Za-z0-9]+"), is(true)); + assertThat(payment.getLinks().getRedirectUrl(), is("http://example.com")); + assertThat(payment.getLinks().getWebhookUrl(), is(is("https://stil4m.github.io"))); + assertThat(payment.getMode(), is("test")); + assertThat(payment.getStatus(), is("open")); + assertThat(payment.getMetadata(), is(meta)); + + } + + @Test + public void testGetPayments() throws IOException, URISyntaxException { + ResponseOrError> result = payments.list(Optional.empty(), Optional.empty()); + assertThat(result.getSuccess(), is(true)); + Page page = result.getData(); + + assertThat(page.getTotalCount(), is(notNullValue())); + assertThat(page.getData().size(), is(Math.min(10, page.getTotalCount()))); + assertThat(page.getOffset(), is(0)); + assertThat(page.getCount(), is(10)); + + assertThat(page.getLinks().getPrevious().isPresent(), is(false)); + assertThat(page.getLinks().getNext().isPresent(), is(true)); + assertThat(page.getLinks().getLast().isPresent(), is(true)); + assertThat(page.getLinks().getFirst().isPresent(), is(true)); + } + + @Test + public void testGetPaymentsWithOffsetAndCount() throws IOException, URISyntaxException { + ResponseOrError> result = payments.list(Optional.of(20), Optional.of(40)); + assertThat(result.getSuccess(), is(true)); + Page page = result.getData(); + + assertThat(page.getTotalCount(), is(notNullValue())); + assertThat(page.getData().size(), is(Math.min(20, page.getTotalCount()))); + assertThat(page.getOffset(), is(40)); + assertThat(page.getCount(), is(20)); + + assertThat(page.getLinks().getPrevious().isPresent(), is(true)); + assertThat(page.getLinks().getNext().isPresent(), is(true)); + assertThat(page.getLinks().getLast().isPresent(), is(true)); + assertThat(page.getLinks().getFirst().isPresent(), is(true)); + } + + @Test + public void testNextPayments() throws IOException, URISyntaxException { + ResponseOrError> result = payments.list(Optional.empty(), Optional.empty()); + assertThat(result.getSuccess(), is(true)); + Page page = result.getData(); + + Page nextPage = payments.next(page).getData(); + + assertThat(nextPage.getOffset(), is(10)); + assertThat(nextPage.getCount(), is(10)); + } + + @Test + public void testPreviousPayments() throws IOException, URISyntaxException { + ResponseOrError> result = payments.list(Optional.of(20), Optional.of(40)); + assertThat(result.getSuccess(), is(true)); + Page page = result.getData(); + + Page previousPage = payments.previous(page).getData(); + + assertThat(previousPage.getOffset(), is(20)); + assertThat(previousPage.getCount(), is(20)); + } +} diff --git a/src/test/java/nl/stil4m/mollie/concepts/RefundsIntegrationTest.java b/src/test/java/nl/stil4m/mollie/concepts/RefundsIntegrationTest.java new file mode 100644 index 0000000..f4bbbbd --- /dev/null +++ b/src/test/java/nl/stil4m/mollie/concepts/RefundsIntegrationTest.java @@ -0,0 +1,107 @@ +package nl.stil4m.mollie.concepts; + +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; +import static org.hamcrest.Matchers.notNullValue; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import org.junit.Before; +import org.junit.Test; + +import nl.stil4m.mollie.Client; +import nl.stil4m.mollie.ClientBuilder; +import nl.stil4m.mollie.ResponseOrError; +import nl.stil4m.mollie.domain.CreatePayment; +import nl.stil4m.mollie.domain.CreatedPayment; +import nl.stil4m.mollie.domain.Page; +import nl.stil4m.mollie.domain.Refund; + +public class RefundsIntegrationTest { + + private Refunds refunds; + private Payments payments; + + @Before + public void before() throws InterruptedException { + Thread.sleep(TEST_TIMEOUT); + Client client = new ClientBuilder().withApiKey(VALID_API_KEY).build(); + refunds = client.refunds(); + payments = client.payments(); + } + + @Test + public void testGetRefunds() throws IOException, URISyntaxException, InterruptedException { + ResponseOrError payment = payments.create(new CreatePayment(Optional.empty(), 1.00, "Some description", "http://example.com", Optional.empty(), null)); + String id = payment.getData().getId(); + + ResponseOrError> all = refunds.all(id, Optional.empty(), Optional.empty()); + + assertThat(all.getSuccess(), is(true)); + Page refundPage = all.getData(); + assertThat(refundPage.getCount(), is(0)); + assertThat(refundPage.getData(), is(notNullValue())); + assertThat(refundPage.getLinks(), is(notNullValue())); + } + + @Test + public void testListRefundsForExistingPayment() throws IOException, URISyntaxException, InterruptedException { + CreatedPayment createdPayment = payments.create(new CreatePayment(Optional.empty(), 1.00, "Some description", "http://example.com", Optional.empty(), null)).getData(); + + ResponseOrError> all = refunds.all(createdPayment.getId(), Optional.empty(), Optional.empty()); + + assertThat(all.getSuccess(), is(true)); + Page data = all.getData(); + assertThat(data.getData().size(), is(0)); + } + + + @Test + public void testCancelNonExistingRefund() throws IOException { + Map errorData = new HashMap<>(); + errorData.put("type", "request"); + errorData.put("message", "The refund id is invalid"); + ResponseOrError payment = payments.create(new CreatePayment(Optional.empty(), 1.00, "Some description", "http://example.com", Optional.empty(), null)); + assertThat(payment.getSuccess(), is(true)); + String id = payment.getData().getId(); + + ResponseOrError cancel = refunds.cancel(id, "foo_bar"); + + assertThat(cancel.getSuccess(), is(false)); + assertThat(cancel.getError().get("error"), is(errorData)); + } + + @Test + public void testGetNonExistingRefund() throws IOException { + Map errorData = new HashMap<>(); + errorData.put("type", "request"); + errorData.put("message", "The refund id is invalid"); + ResponseOrError payment = payments.create(new CreatePayment(Optional.empty(), 1.00, "Some description", "http://example.com", Optional.empty(), null)); + String id = payment.getData().getId(); + + ResponseOrError get = refunds.get(id, "foo_bar"); + + assertThat(get.getSuccess(), is(false)); + assertThat(get.getError().get("error"), is(errorData)); + } + + @Test + public void testCreateRefund() throws IOException, URISyntaxException { + Map errorData = new HashMap<>(); + errorData.put("type", "request"); + errorData.put("message", "The payment is already refunded or has not been paid for yet"); + ResponseOrError payment = payments.create(new CreatePayment(Optional.empty(), 1.00, "Some description", "http://example.com", Optional.empty(), null)); + String id = payment.getData().getId(); + + ResponseOrError create = refunds.create(id, Optional.of(1.00)); + + assertThat(create.getSuccess(), is(false)); + assertThat(create.getError().get("error"), is(errorData)); + } +}