Skip to content

Commit

Permalink
feat(EWT-389): Full support for cVRP and simplified headless authoris…
Browse files Browse the repository at this point in the history
…ation in acceptance tests (#243)

Co-authored-by: tl-luca-baggi <luca.baggi@truelayer.com>
  • Loading branch information
dili91 and tl-luca-baggi authored Jan 8, 2024
1 parent 617ead0 commit 55ff477
Show file tree
Hide file tree
Showing 21 changed files with 462 additions and 166 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/acceptance-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ jobs:
"text": "I don't like that",
"emoji": true
},
"image_url": "https://media.giphy.com/media/4cuyucPeVWbNS/giphy.gif",
"alt_text": "marg"
"image_url": "https://media.giphy.com/media/jOpLbiGmHR9S0/giphy.gif",
"alt_text": "this is the worst"
}
]
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Main properties
group=com.truelayer
archivesBaseName=truelayer-java
version=10.3.1
version=11.0.0

# Artifacts properties
sonatype_repository_url=https://s01.oss.sonatype.org/service/local/
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/truelayer/java/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public static final class Scopes {
public static final String PAYMENTS = "payments";

public static final String RECURRING_PAYMENTS_SWEEPING = "recurring_payments:sweeping";

public static final String RECURRING_PAYMENTS_COMMERCIAL = "recurring_payments:commercial";
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/truelayer/java/ITrueLayerClient.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.truelayer.java;

import com.truelayer.java.auth.IAuthenticationHandler;
import com.truelayer.java.commonapi.entities.SubmitPaymentReturnParametersRequest;
import com.truelayer.java.commonapi.entities.SubmitPaymentReturnParametersResponse;
import com.truelayer.java.commonapi.entities.SubmitPaymentsProviderReturnRequest;
import com.truelayer.java.commonapi.entities.SubmitPaymentsProviderReturnResponse;
import com.truelayer.java.hpp.IHostedPaymentPageLinkBuilder;
import com.truelayer.java.http.entities.ApiResponse;
import com.truelayer.java.mandates.IMandatesHandler;
Expand Down Expand Up @@ -65,6 +65,6 @@ public interface ITrueLayerClient {
* @return the response of the <i>Submit payment returns parameters</i> operation
* @see <a href="https://docs.truelayer.com/reference/submit-payments-provider-return-parameters"><i>Submit payments return parameters</i> API reference</a>
*/
CompletableFuture<ApiResponse<SubmitPaymentReturnParametersResponse>> submitPaymentReturnParameters(
SubmitPaymentReturnParametersRequest request);
CompletableFuture<ApiResponse<SubmitPaymentsProviderReturnResponse>> submitPaymentReturnParameters(
SubmitPaymentsProviderReturnRequest request);
}
8 changes: 4 additions & 4 deletions src/main/java/com/truelayer/java/TrueLayerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.truelayer.java.auth.IAuthenticationHandler;
import com.truelayer.java.commonapi.ICommonHandler;
import com.truelayer.java.commonapi.entities.SubmitPaymentReturnParametersRequest;
import com.truelayer.java.commonapi.entities.SubmitPaymentReturnParametersResponse;
import com.truelayer.java.commonapi.entities.SubmitPaymentsProviderReturnRequest;
import com.truelayer.java.commonapi.entities.SubmitPaymentsProviderReturnResponse;
import com.truelayer.java.hpp.IHostedPaymentPageLinkBuilder;
import com.truelayer.java.http.entities.ApiResponse;
import com.truelayer.java.mandates.IMandatesHandler;
Expand Down Expand Up @@ -120,8 +120,8 @@ public IHostedPaymentPageLinkBuilder hpp() {
* {@inheritDoc}
*/
@Override
public CompletableFuture<ApiResponse<SubmitPaymentReturnParametersResponse>> submitPaymentReturnParameters(
SubmitPaymentReturnParametersRequest request) {
public CompletableFuture<ApiResponse<SubmitPaymentsProviderReturnResponse>> submitPaymentReturnParameters(
SubmitPaymentsProviderReturnRequest request) {
return commonHandler.submitPaymentReturnParameters(request);
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/truelayer/java/commonapi/CommonHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import static com.truelayer.java.http.mappers.HeadersMapper.toMap;
import static java.util.Collections.emptyMap;

import com.truelayer.java.commonapi.entities.SubmitPaymentReturnParametersRequest;
import com.truelayer.java.commonapi.entities.SubmitPaymentReturnParametersResponse;
import com.truelayer.java.commonapi.entities.SubmitPaymentsProviderReturnRequest;
import com.truelayer.java.commonapi.entities.SubmitPaymentsProviderReturnResponse;
import com.truelayer.java.http.entities.ApiResponse;
import com.truelayer.java.http.entities.Headers;
import java.util.concurrent.CompletableFuture;
Expand All @@ -16,14 +16,14 @@ public class CommonHandler implements ICommonHandler {
ICommonApi commonApi;

@Override
public CompletableFuture<ApiResponse<SubmitPaymentReturnParametersResponse>> submitPaymentReturnParameters(
SubmitPaymentReturnParametersRequest request) {
public CompletableFuture<ApiResponse<SubmitPaymentsProviderReturnResponse>> submitPaymentReturnParameters(
SubmitPaymentsProviderReturnRequest request) {
return commonApi.submitPaymentReturnParameters(emptyMap(), request);
}

@Override
public CompletableFuture<ApiResponse<SubmitPaymentReturnParametersResponse>> submitPaymentReturnParameters(
Headers headers, SubmitPaymentReturnParametersRequest request) {
public CompletableFuture<ApiResponse<SubmitPaymentsProviderReturnResponse>> submitPaymentReturnParameters(
Headers headers, SubmitPaymentsProviderReturnRequest request) {
return commonApi.submitPaymentReturnParameters(toMap(headers), request);
}
}
8 changes: 4 additions & 4 deletions src/main/java/com/truelayer/java/commonapi/ICommonApi.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.truelayer.java.commonapi;

import com.truelayer.java.commonapi.entities.SubmitPaymentReturnParametersRequest;
import com.truelayer.java.commonapi.entities.SubmitPaymentReturnParametersResponse;
import com.truelayer.java.commonapi.entities.SubmitPaymentsProviderReturnRequest;
import com.truelayer.java.commonapi.entities.SubmitPaymentsProviderReturnResponse;
import com.truelayer.java.http.entities.ApiResponse;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
Expand All @@ -24,6 +24,6 @@ public interface ICommonApi {
* @see <a href="https://docs.truelayer.com/reference/submit-payments-provider-return-parameters"><i>Submit payments return parameters</i> API reference</a>
*/
@POST("/payments-provider-return")
CompletableFuture<ApiResponse<SubmitPaymentReturnParametersResponse>> submitPaymentReturnParameters(
@HeaderMap Map<String, String> headers, @Body SubmitPaymentReturnParametersRequest request);
CompletableFuture<ApiResponse<SubmitPaymentsProviderReturnResponse>> submitPaymentReturnParameters(
@HeaderMap Map<String, String> headers, @Body SubmitPaymentsProviderReturnRequest request);
}
12 changes: 6 additions & 6 deletions src/main/java/com/truelayer/java/commonapi/ICommonHandler.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.truelayer.java.commonapi;

import com.truelayer.java.commonapi.entities.SubmitPaymentReturnParametersRequest;
import com.truelayer.java.commonapi.entities.SubmitPaymentReturnParametersResponse;
import com.truelayer.java.commonapi.entities.SubmitPaymentsProviderReturnRequest;
import com.truelayer.java.commonapi.entities.SubmitPaymentsProviderReturnResponse;
import com.truelayer.java.http.entities.ApiResponse;
import com.truelayer.java.http.entities.Headers;
import java.util.concurrent.CompletableFuture;

public interface ICommonHandler {

CompletableFuture<ApiResponse<SubmitPaymentReturnParametersResponse>> submitPaymentReturnParameters(
SubmitPaymentReturnParametersRequest request);
CompletableFuture<ApiResponse<SubmitPaymentsProviderReturnResponse>> submitPaymentReturnParameters(
SubmitPaymentsProviderReturnRequest request);

CompletableFuture<ApiResponse<SubmitPaymentReturnParametersResponse>> submitPaymentReturnParameters(
Headers headers, SubmitPaymentReturnParametersRequest request);
CompletableFuture<ApiResponse<SubmitPaymentsProviderReturnResponse>> submitPaymentReturnParameters(
Headers headers, SubmitPaymentsProviderReturnRequest request);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.truelayer.java.commonapi.entities;

import lombok.EqualsAndHashCode;
import lombok.Value;

@Value
@EqualsAndHashCode(callSuper = false)
public class MandateProviderReturnResource extends ProviderReturnResource {
Type type = Type.MANDATE;

String mandateId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.truelayer.java.commonapi.entities;

import lombok.EqualsAndHashCode;
import lombok.Value;

@Value
@EqualsAndHashCode(callSuper = false)
public class PaymentProviderReturnResource extends ProviderReturnResource {
Type type = Type.PAYMENT;

String paymentId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.truelayer.java.commonapi.entities;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonValue;
import com.truelayer.java.TrueLayerException;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", defaultImpl = PaymentProviderReturnResource.class)
@JsonSubTypes({
@JsonSubTypes.Type(value = PaymentProviderReturnResource.class, name = "payment"),
@JsonSubTypes.Type(value = MandateProviderReturnResource.class, name = "mandate")
})
@Getter
public abstract class ProviderReturnResource {
String type;

String paymentId;

@JsonIgnore
public abstract Type getType();

@JsonIgnore
public boolean isPaymentProviderReturnResponse() {
return this instanceof PaymentProviderReturnResource;
}

@JsonIgnore
public boolean isMandateProviderReturnResponse() {
return this instanceof MandateProviderReturnResource;
}

@JsonIgnore
public PaymentProviderReturnResource asPaymentProviderReturnResponse() {
if (!isPaymentProviderReturnResponse()) {
throw new TrueLayerException(buildErrorMessage());
}
return (PaymentProviderReturnResource) this;
}

@JsonIgnore
public MandateProviderReturnResource asMandateProviderReturnResponse() {
if (!isMandateProviderReturnResponse()) {
throw new TrueLayerException(buildErrorMessage());
}
return (MandateProviderReturnResource) this;
}

private String buildErrorMessage() {
return String.format(
"Provider return resource is of type %s.", this.getClass().getSimpleName());
}

@RequiredArgsConstructor
@Getter
public enum Type {
PAYMENT("payment"),
MANDATE("mandate");

@JsonValue
private final String type;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@Getter
@ToString
@EqualsAndHashCode
public class SubmitPaymentReturnParametersRequest {
public class SubmitPaymentsProviderReturnRequest {
private String query;

private String fragment;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.truelayer.java.commonapi.entities;

import lombok.Value;

@Value
public class SubmitPaymentsProviderReturnResponse {
ProviderReturnResource resource;
}
80 changes: 78 additions & 2 deletions src/test/java/com/truelayer/java/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import static com.truelayer.java.Constants.HeaderNames.*;
import static com.truelayer.java.Utils.getObjectMapper;
import static org.apache.commons.lang3.ObjectUtils.*;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.*;

import com.github.tomakehurst.wiremock.client.MappingBuilder;
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
import com.github.tomakehurst.wiremock.matching.UrlPattern;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import com.truelayer.java.auth.entities.AccessToken;
import com.truelayer.java.commonapi.entities.PaymentProviderReturnResource;
import com.truelayer.java.commonapi.entities.SubmitPaymentsProviderReturnRequest;
import com.truelayer.java.commonapi.entities.SubmitPaymentsProviderReturnResponse;
import com.truelayer.java.http.entities.ApiResponse;
import com.truelayer.java.http.entities.Headers;
import com.truelayer.java.versioninfo.VersionInfo;
Expand All @@ -20,8 +23,10 @@
import java.time.Duration;
import java.util.UUID;
import java.util.regex.Pattern;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import okhttp3.OkHttpClient;
import okhttp3.*;
import org.apache.commons.lang3.RandomStringUtils;

public class TestUtils {
Expand Down Expand Up @@ -217,4 +222,75 @@ public static Headers buildTestHeaders() {
.xDeviceUserAgent("ADummyUserAgen")
.build();
}

@SneakyThrows
public static void runAndAssertHeadlessResourceAuthorisation(
TrueLayerClient tlClient, URI redirectUri, HeadlessResourceAuthorization headlessResourceAuthorization) {
assertNotNull(redirectUri);
String protocol = redirectUri.getScheme();
String host = redirectUri.getHost();
String resourceId =
redirectUri.getPath().substring(redirectUri.getPath().lastIndexOf("/") + 1);
var body = RequestBody.create(MediaType.get("application/json"), headlessResourceAuthorization.payload);
Request request = new Request.Builder()
.url(String.format(
"%s://%s/api/%s/%s/action", protocol, host, headlessResourceAuthorization.path, resourceId))
.post(body)
.addHeader(
"Authorization",
String.format("Bearer %s", redirectUri.getFragment().replaceFirst("token=", "")))
.build();
Response authorisationResponse =
getHttpClientInstance().newCall(request).execute();
assertTrue(authorisationResponse.isSuccessful());
String responseString = authorisationResponse.body().string();
assertNotNull(responseString);

// Grab the provider return query and fragment from the mock payment api response
URI responseUrl = URI.create(responseString);

// Mandates require some adjustments to the query string...
// TODO: review if we want this to be a permanent solution
String query = headlessResourceAuthorization == HeadlessResourceAuthorization.MANDATES
? responseUrl.getQuery().replaceFirst("mandate-", "")
: responseUrl.getQuery();

SubmitPaymentsProviderReturnRequest submitProviderReturneRequest = SubmitPaymentsProviderReturnRequest.builder()
.query(query)
.fragment(responseUrl.getFragment())
.build();
ApiResponse<SubmitPaymentsProviderReturnResponse> submitPaymentReturnParametersResponse =
tlClient.submitPaymentReturnParameters(submitProviderReturneRequest)
.get();
assertNotError(submitPaymentReturnParametersResponse);

switch (headlessResourceAuthorization) {
case PAYMENTS:
assertEquals(
PaymentProviderReturnResource.Type.PAYMENT,
submitPaymentReturnParametersResponse
.getData()
.getResource()
.getType());
break;
case MANDATES:
assertEquals(
PaymentProviderReturnResource.Type.MANDATE,
submitPaymentReturnParametersResponse
.getData()
.getResource()
.getType());
break;
}
}

@RequiredArgsConstructor
@Getter
public enum HeadlessResourceAuthorization {
PAYMENTS("single-immediate-payments", "{\"action\":\"Execute\", \"redirect\": false}"),
MANDATES("vrp-consents", "{\"action\":\"Authorise\", \"redirect\": false}");

private final String path;
private final String payload;
}
}
Loading

0 comments on commit 55ff477

Please sign in to comment.