-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HPP link builder implemented with tests
- Loading branch information
1 parent
822ad7d
commit 1f02664
Showing
9 changed files
with
177 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
package truelayer.java; | ||
|
||
import truelayer.java.auth.IAuthenticationHandler; | ||
import truelayer.java.hpp.IHostedPaymentPageLinkBuilder; | ||
import truelayer.java.payments.IPaymentHandler; | ||
|
||
public interface ITrueLayerClient { | ||
IAuthenticationHandler auth(); | ||
|
||
IPaymentHandler payments(); | ||
|
||
IHostedPaymentPageLinkBuilder hpp(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
lib/src/main/java/truelayer/java/hpp/HostedPaymentPageLinkBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package truelayer.java.hpp; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import truelayer.java.TrueLayerException; | ||
|
||
import java.net.URI; | ||
|
||
import static org.apache.commons.lang3.ObjectUtils.isEmpty; | ||
|
||
@Builder | ||
@Getter | ||
public class HostedPaymentPageLinkBuilder implements IHostedPaymentPageLinkBuilder { | ||
private String endpoint; | ||
|
||
@Override | ||
public URI getHostedPaymentPageLink(String paymentId, String resourceToken, URI returnUrl) { | ||
if(isEmpty(paymentId)){ | ||
throw new TrueLayerException("payment_id must be set"); | ||
} | ||
|
||
if(isEmpty(resourceToken)){ | ||
throw new TrueLayerException("resource_token must be set"); | ||
} | ||
|
||
if(isEmpty(returnUrl) || isEmpty(returnUrl.toString())){ | ||
throw new TrueLayerException("return_url must be set"); | ||
} | ||
|
||
var link = String.format("%s/payments#payment_id=%s&resource_token=%s&return_url=%s", | ||
endpoint, | ||
paymentId, | ||
resourceToken, | ||
returnUrl); | ||
return URI.create(link); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
lib/src/main/java/truelayer/java/hpp/IHostedPaymentPageLinkBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package truelayer.java.hpp; | ||
|
||
import java.net.URI; | ||
|
||
public interface IHostedPaymentPageLinkBuilder { | ||
URI getHostedPaymentPageLink(String paymentId, String resourceToken, URI returnUrl); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
lib/src/test/java/truelayer/java/hpp/HostedPaymentPageLinkBuilderTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package truelayer.java.hpp; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import truelayer.java.TrueLayerException; | ||
|
||
import java.net.URI; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class HostedPaymentPageLinkBuilderTests { | ||
|
||
public static final String AN_ENDPOINT = "https://an-endpoint.com"; | ||
public static final String A_RETURN_URL = "https://a-redirect-url.com"; | ||
public static final String A_RESOURCE_TOKEN = "a-resource-token"; | ||
public static final String A_PAYMENT_ID = "a-payment-id"; | ||
|
||
@Test | ||
@DisplayName("it should yield an HPP link with the given details") | ||
public void itShouldYieldAnHppLink() { | ||
var sut = buildHppBuilder(); | ||
|
||
var link = sut.getHostedPaymentPageLink(A_PAYMENT_ID, A_RESOURCE_TOKEN, URI.create(A_RETURN_URL)); | ||
|
||
assertEquals( | ||
new StringBuilder(AN_ENDPOINT) | ||
.append("/payments#payment_id=") | ||
.append(A_PAYMENT_ID) | ||
.append("&resource_token=") | ||
.append(A_RESOURCE_TOKEN) | ||
.append("&return_url=") | ||
.append(A_RETURN_URL) | ||
.toString(), | ||
link.toString() | ||
); | ||
} | ||
|
||
@Test | ||
@DisplayName("it should thrown an exception if redirect_url is empty") | ||
public void itShouldThrowExceptionForEmptyRedirectUrl() { | ||
var sut = buildHppBuilder(); | ||
|
||
var thrown = assertThrows(TrueLayerException.class, () -> sut.getHostedPaymentPageLink(A_PAYMENT_ID, A_RESOURCE_TOKEN, URI.create(""))); | ||
|
||
assertEquals("return_url must be set", thrown.getMessage()); | ||
} | ||
|
||
@Test | ||
@DisplayName("it should thrown an exception if payment_id is empty") | ||
public void itShouldThrowExceptionForEmptyPaymentId() { | ||
var sut = buildHppBuilder(); | ||
|
||
var thrown = assertThrows(TrueLayerException.class, () -> sut.getHostedPaymentPageLink("", A_RESOURCE_TOKEN, URI.create(A_RETURN_URL))); | ||
|
||
assertEquals("payment_id must be set", thrown.getMessage()); | ||
} | ||
|
||
@Test | ||
@DisplayName("it should thrown an exception if resource_token is empty") | ||
public void itShouldThrowExceptionForEmptyResourceToken() { | ||
var sut = buildHppBuilder(); | ||
|
||
var thrown = assertThrows(TrueLayerException.class, () -> sut.getHostedPaymentPageLink(A_PAYMENT_ID, "", URI.create(A_RETURN_URL))); | ||
|
||
assertEquals("resource_token must be set", thrown.getMessage()); | ||
} | ||
|
||
private HostedPaymentPageLinkBuilder buildHppBuilder() { | ||
return HostedPaymentPageLinkBuilder.builder() | ||
.endpoint(AN_ENDPOINT) | ||
.build(); | ||
} | ||
} |