From 5233eecba31910182622e1aec8346a3f05768d8a Mon Sep 17 00:00:00 2001 From: Facundo Aita <80763964+tl-facundo-aita@users.noreply.github.com> Date: Tue, 13 Feb 2024 14:01:26 +0000 Subject: [PATCH] feat(PAYINS-504): Set Authorization header to `GET v3/payments-providers/{id}` (#270) --- gradle.properties | 2 +- .../java/TrueLayerClientBuilder.java | 15 ++++-- .../IPaymentsProvidersApi.java | 5 +- .../PaymentsProvidersHandler.java | 22 +++++--- .../PaymentsProvidersHandlerBuilder.java | 33 ------------ .../PaymentsProvidersIntegrationTests.java | 14 +++-- .../PaymentsProvidersHandlerTests.java | 52 +++++++++++++++++++ 7 files changed, 92 insertions(+), 51 deletions(-) delete mode 100644 src/main/java/com/truelayer/java/paymentsproviders/PaymentsProvidersHandlerBuilder.java create mode 100644 src/test/java/com/truelayer/java/paymentsproviders/PaymentsProvidersHandlerTests.java diff --git a/gradle.properties b/gradle.properties index 5e1a2915..76494032 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,7 +1,7 @@ # Main properties group=com.truelayer archivesBaseName=truelayer-java -version=11.0.0 +version=11.0.1 # Artifacts properties sonatype_repository_url=https://s01.oss.sonatype.org/service/local/ diff --git a/src/main/java/com/truelayer/java/TrueLayerClientBuilder.java b/src/main/java/com/truelayer/java/TrueLayerClientBuilder.java index edf0a041..2fed371e 100644 --- a/src/main/java/com/truelayer/java/TrueLayerClientBuilder.java +++ b/src/main/java/com/truelayer/java/TrueLayerClientBuilder.java @@ -25,6 +25,7 @@ import com.truelayer.java.payments.IPaymentsApi; import com.truelayer.java.payments.IPaymentsHandler; import com.truelayer.java.payments.PaymentsHandler; +import com.truelayer.java.paymentsproviders.IPaymentsProvidersApi; import com.truelayer.java.paymentsproviders.IPaymentsProvidersHandler; import com.truelayer.java.paymentsproviders.PaymentsProvidersHandler; import com.truelayer.java.payouts.IPayoutsApi; @@ -252,10 +253,16 @@ public TrueLayerClient build() { } IPaymentsHandler paymentsHandler = paymentsHandlerBuilder.build(); - IPaymentsProvidersHandler paymentsProvidersHandler = PaymentsProvidersHandler.New() - .clientCredentials(clientCredentials) - .httpClient(RetrofitFactory.build(baseHttpClient, environment.getPaymentsApiUri())) - .build(); + IPaymentsProvidersApi paymentsProvidersApi = RetrofitFactory.build( + paymentsHttpClient, environment.getPaymentsApiUri()) + .create(IPaymentsProvidersApi.class); + + PaymentsProvidersHandler.PaymentsProvidersHandlerBuilder paymentsProvidersHandlerBuilder = + PaymentsProvidersHandler.builder().paymentsProvidersApi(paymentsProvidersApi); + if (customScopesPresent()) { + paymentsProvidersHandlerBuilder.scopes(globalScopes); + } + IPaymentsProvidersHandler paymentsProvidersHandler = paymentsProvidersHandlerBuilder.build(); IMerchantAccountsApi merchantAccountsApi = RetrofitFactory.build( paymentsHttpClient, environment.getPaymentsApiUri()) diff --git a/src/main/java/com/truelayer/java/paymentsproviders/IPaymentsProvidersApi.java b/src/main/java/com/truelayer/java/paymentsproviders/IPaymentsProvidersApi.java index 39823cf7..8166551c 100644 --- a/src/main/java/com/truelayer/java/paymentsproviders/IPaymentsProvidersApi.java +++ b/src/main/java/com/truelayer/java/paymentsproviders/IPaymentsProvidersApi.java @@ -1,11 +1,12 @@ package com.truelayer.java.paymentsproviders; +import com.truelayer.java.entities.RequestScopes; import com.truelayer.java.http.entities.ApiResponse; import com.truelayer.java.paymentsproviders.entities.PaymentsProvider; import java.util.concurrent.CompletableFuture; import retrofit2.http.GET; import retrofit2.http.Path; -import retrofit2.http.Query; +import retrofit2.http.Tag; /** * Interface that models /payments-providers/* endpoints @@ -14,5 +15,5 @@ public interface IPaymentsProvidersApi { @GET("/payments-providers/{id}") CompletableFuture> getProvider( - @Path("id") String providerId, @Query("client_id") String clientId); + @Tag RequestScopes scopes, @Path("id") String providerId); } diff --git a/src/main/java/com/truelayer/java/paymentsproviders/PaymentsProvidersHandler.java b/src/main/java/com/truelayer/java/paymentsproviders/PaymentsProvidersHandler.java index 5d36859e..e8b3017a 100644 --- a/src/main/java/com/truelayer/java/paymentsproviders/PaymentsProvidersHandler.java +++ b/src/main/java/com/truelayer/java/paymentsproviders/PaymentsProvidersHandler.java @@ -1,26 +1,32 @@ package com.truelayer.java.paymentsproviders; +import static com.truelayer.java.Constants.Scopes.PAYMENTS; + +import com.truelayer.java.IAuthenticatedHandler; +import com.truelayer.java.entities.RequestScopes; import com.truelayer.java.http.entities.ApiResponse; import com.truelayer.java.paymentsproviders.entities.PaymentsProvider; import java.util.concurrent.CompletableFuture; -import lombok.Value; +import lombok.Builder; /** * {@inheritDoc} */ -@Value -public class PaymentsProvidersHandler implements IPaymentsProvidersHandler { - - String clientId; +@Builder +public class PaymentsProvidersHandler implements IAuthenticatedHandler, IPaymentsProvidersHandler { IPaymentsProvidersApi paymentsProvidersApi; - public static PaymentsProvidersHandlerBuilder New() { - return new PaymentsProvidersHandlerBuilder(); + @Builder.Default + private RequestScopes scopes = RequestScopes.builder().scope(PAYMENTS).build(); + + @Override + public RequestScopes getRequestScopes() { + return scopes; } @Override public CompletableFuture> getProvider(String providerId) { - return paymentsProvidersApi.getProvider(providerId, clientId); + return paymentsProvidersApi.getProvider(getRequestScopes(), providerId); } } diff --git a/src/main/java/com/truelayer/java/paymentsproviders/PaymentsProvidersHandlerBuilder.java b/src/main/java/com/truelayer/java/paymentsproviders/PaymentsProvidersHandlerBuilder.java deleted file mode 100644 index 9321733c..00000000 --- a/src/main/java/com/truelayer/java/paymentsproviders/PaymentsProvidersHandlerBuilder.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.truelayer.java.paymentsproviders; - -import com.truelayer.java.ClientCredentials; -import com.truelayer.java.TrueLayerException; -import org.apache.commons.lang3.ObjectUtils; -import retrofit2.Retrofit; - -public class PaymentsProvidersHandlerBuilder { - - private Retrofit httpClient; - - private ClientCredentials clientCredentials; - - PaymentsProvidersHandlerBuilder() {} - - public PaymentsProvidersHandlerBuilder clientCredentials(ClientCredentials clientCredentials) { - this.clientCredentials = clientCredentials; - return this; - } - - public PaymentsProvidersHandlerBuilder httpClient(Retrofit httpClient) { - this.httpClient = httpClient; - return this; - } - - public PaymentsProvidersHandler build() { - if (ObjectUtils.isEmpty(clientCredentials)) { - throw new TrueLayerException("client credentials must be set"); - } - return new PaymentsProvidersHandler( - clientCredentials.clientId(), httpClient.create(IPaymentsProvidersApi.class)); - } -} diff --git a/src/test/java/com/truelayer/java/integration/PaymentsProvidersIntegrationTests.java b/src/test/java/com/truelayer/java/integration/PaymentsProvidersIntegrationTests.java index 8358648e..af8b73ca 100644 --- a/src/test/java/com/truelayer/java/integration/PaymentsProvidersIntegrationTests.java +++ b/src/test/java/com/truelayer/java/integration/PaymentsProvidersIntegrationTests.java @@ -1,6 +1,7 @@ package com.truelayer.java.integration; import static com.github.tomakehurst.wiremock.client.WireMock.*; +import static com.truelayer.java.Constants.Scopes.PAYMENTS; import static com.truelayer.java.TestUtils.assertNotError; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -8,6 +9,7 @@ import com.truelayer.java.TestUtils.RequestStub; import com.truelayer.java.http.entities.ApiResponse; import com.truelayer.java.paymentsproviders.entities.PaymentsProvider; +import java.util.Collections; import lombok.SneakyThrows; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -20,11 +22,17 @@ public class PaymentsProvidersIntegrationTests extends IntegrationTests { @SneakyThrows public void shouldReturnAPaymentsProvider() { String jsonResponseFile = "payments_providers/200.get_payments_provider.json"; + RequestStub.New() + .method("post") + .path(urlPathEqualTo("/connect/token")) + .status(200) + .bodyFile("auth/200.access_token.json") + .build(); String providerId = "ob-barclays"; - String clientId = TestUtils.getClientCredentials().clientId(); RequestStub.New() .method("get") - .path(urlEqualTo("/payments-providers/" + providerId + "?client_id=" + clientId)) + .path(urlEqualTo("/payments-providers/" + providerId)) + .withAuthorization() .status(200) .bodyFile(jsonResponseFile) .build(); @@ -32,7 +40,7 @@ public void shouldReturnAPaymentsProvider() { ApiResponse response = tlClient.paymentsProviders().getProvider(providerId).get(); - verify(exactly(0), postRequestedFor(urlPathEqualTo("/connect/token"))); + verifyGeneratedToken(Collections.singletonList(PAYMENTS)); assertNotError(response); PaymentsProvider expected = TestUtils.deserializeJsonFileTo(jsonResponseFile, PaymentsProvider.class); assertEquals(expected, response.getData()); diff --git a/src/test/java/com/truelayer/java/paymentsproviders/PaymentsProvidersHandlerTests.java b/src/test/java/com/truelayer/java/paymentsproviders/PaymentsProvidersHandlerTests.java new file mode 100644 index 00000000..d983dadb --- /dev/null +++ b/src/test/java/com/truelayer/java/paymentsproviders/PaymentsProvidersHandlerTests.java @@ -0,0 +1,52 @@ +package com.truelayer.java.paymentsproviders; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import com.truelayer.java.Constants; +import com.truelayer.java.entities.RequestScopes; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +class PaymentsProvidersHandlerTests { + + private static final String A_PROVIDER_ID = "a-provider-id"; + private static final RequestScopes SCOPES = + RequestScopes.builder().scope("a-custom-scope").build(); + + private PaymentsProvidersHandler sut; + private IPaymentsProvidersApi paymentsProvidersApiMock; + + @BeforeEach + public void setup() { + paymentsProvidersApiMock = Mockito.mock(IPaymentsProvidersApi.class); + sut = PaymentsProvidersHandler.builder() + .paymentsProvidersApi(paymentsProvidersApiMock) + .scopes(SCOPES) + .build(); + } + + @Test + @DisplayName("It should call the get payments-providers by id endpoint with the default scopes") + public void shouldCallCreatePaymentWithDefaultScopes() { + PaymentsProvidersHandler sut = PaymentsProvidersHandler.builder() + .paymentsProvidersApi(paymentsProvidersApiMock) + .build(); + + sut.getProvider(A_PROVIDER_ID); + + RequestScopes expectedDefaultScopes = + RequestScopes.builder().scope(Constants.Scopes.PAYMENTS).build(); + verify(paymentsProvidersApiMock, times(1)).getProvider(expectedDefaultScopes, A_PROVIDER_ID); + } + + @Test + @DisplayName("It should call the get payments-providers by id endpoint") + public void shouldCallGetPaymentById() { + sut.getProvider(A_PROVIDER_ID); + + verify(paymentsProvidersApiMock, times(1)).getProvider(SCOPES, A_PROVIDER_ID); + } +}