Skip to content

Commit

Permalink
feat(PAYINS-504): Set Authorization header to `GET v3/payments-provid…
Browse files Browse the repository at this point in the history
…ers/{id}` (#270)
  • Loading branch information
tl-facundo-aita authored Feb 13, 2024
1 parent 171cdde commit 5233eec
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 51 deletions.
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=11.0.0
version=11.0.1

# Artifacts properties
sonatype_repository_url=https://s01.oss.sonatype.org/service/local/
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/com/truelayer/java/TrueLayerClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,5 +15,5 @@ public interface IPaymentsProvidersApi {

@GET("/payments-providers/{id}")
CompletableFuture<ApiResponse<PaymentsProvider>> getProvider(
@Path("id") String providerId, @Query("client_id") String clientId);
@Tag RequestScopes scopes, @Path("id") String providerId);
}
Original file line number Diff line number Diff line change
@@ -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<ApiResponse<PaymentsProvider>> getProvider(String providerId) {
return paymentsProvidersApi.getProvider(providerId, clientId);
return paymentsProvidersApi.getProvider(getRequestScopes(), providerId);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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;

import com.truelayer.java.TestUtils;
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;
Expand All @@ -20,19 +22,25 @@ 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();

ApiResponse<PaymentsProvider> 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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 5233eec

Please sign in to comment.