Skip to content

Commit

Permalink
test for multi-client, same cache
Browse files Browse the repository at this point in the history
  • Loading branch information
dili91 committed Jan 15, 2025
1 parent 3356360 commit 861293e
Showing 1 changed file with 68 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.truelayer.java.Constants.HeaderNames.AUTHORIZATION;
import static com.truelayer.java.TestUtils.deserializeJsonFileTo;
import static com.truelayer.java.TestUtils.getSigningOptions;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.truelayer.java.ClientCredentials;
import com.truelayer.java.Environment;
import com.truelayer.java.TestUtils;
import com.truelayer.java.TestUtils.RequestStub;
Expand All @@ -15,10 +18,11 @@
import com.truelayer.java.integration.IntegrationTests;
import com.truelayer.java.payments.entities.CreatePaymentRequest;
import java.net.URI;
import java.text.MessageFormat;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import lombok.SneakyThrows;
import org.apache.commons.lang3.NotImplementedException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -27,6 +31,9 @@ public class CustomCredentialsCacheTests extends IntegrationTests {

private MyCustomCache customCredentialsCache;

private TrueLayerClient tlClient2;
private String clientId2;

// overrides the default implementation by setting a custom cache for credentials
@BeforeEach
@Override
Expand All @@ -39,6 +46,17 @@ public void setup(WireMockRuntimeInfo wireMockRuntimeInfo) {
.environment(testEnvironment)
.withCredentialsCaching(customCredentialsCache)
.build();

clientId2 = UUID.randomUUID().toString();
tlClient2 = TrueLayerClient.New()
.clientCredentials(ClientCredentials.builder()
.clientId(clientId2)
.clientSecret(UUID.randomUUID().toString())
.build())
.signingOptions(getSigningOptions())
.environment(testEnvironment)
.withCredentialsCaching(customCredentialsCache)
.build();
}

@SneakyThrows
Expand Down Expand Up @@ -73,17 +91,60 @@ public void itShouldUseACachedToken() {
.withHeader(AUTHORIZATION, equalTo("Bearer " + expectedToken.getAccessToken())));

assertEquals(1, customCredentialsCache.cache.mappingCount());
customCredentialsCache.cache.entrySet().stream()
.findFirst()
.get()
.getValue()
.equals(expectedToken);
assertEquals(
expectedToken,
customCredentialsCache.cache.entrySet().stream()
.findFirst()
.get()
.getValue());
}

@SneakyThrows
@Test
@DisplayName("Should keep credentials segregated when the same cache is used across different clients")
public void itShouldUse2IndependentClients() {
throw new NotImplementedException();
String clientId1 = TestUtils.getClientCredentials().clientId();
String accessTokenJsonFile = "auth/200.access_token.json";
RequestStub.New()
.method("post")
.path(urlPathEqualTo("/connect/token"))
.status(200)
.bodyFile(accessTokenJsonFile)
.build();
RequestStub.New()
.method("post")
.path(urlPathEqualTo("/payments"))
.withAuthorization()
.withSignature()
.status(201)
.bodyFile("payments/201.create_payment.authorization_required.json")
.build();
CreatePaymentRequest paymentRequest = CreatePaymentRequest.builder().build();

tlClient.payments().createPayment(paymentRequest).get();
tlClient2.payments().createPayment(paymentRequest).get();

verify(2, postRequestedFor(urlPathEqualTo("/connect/token")));
verify(
1,
postRequestedFor(urlPathEqualTo("/connect/token"))
.withRequestBody(matchingJsonPath("$.client_id", equalTo(clientId1))));
verify(
1,
postRequestedFor(urlPathEqualTo("/connect/token"))
.withRequestBody(matchingJsonPath("$.client_id", equalTo(clientId2))));

AccessToken expectedToken = deserializeJsonFileTo(accessTokenJsonFile, AccessToken.class);
verify(
2,
postRequestedFor(urlPathEqualTo("/payments"))
.withHeader(AUTHORIZATION, equalTo("Bearer " + expectedToken.getAccessToken())));

assertEquals(2, customCredentialsCache.cache.mappingCount());
assertTrue(customCredentialsCache.cache.entrySet().stream()
.anyMatch(k -> k.getKey().startsWith(MessageFormat.format("tl-auth-token:{0}", clientId1))));
assertTrue(customCredentialsCache.cache.entrySet().stream()
.anyMatch(k -> k.getKey().startsWith(MessageFormat.format("tl-auth-token:{0}", clientId2))));
}

public static class MyCustomCache implements ICredentialsCache {
Expand Down

0 comments on commit 861293e

Please sign in to comment.