From cf54c2fec5adf254475698b7aba63757100f2ed8 Mon Sep 17 00:00:00 2001 From: antoniocalo Date: Fri, 22 Nov 2024 17:50:55 +0100 Subject: [PATCH] P4ADEV-1341 fix PdndServiceTest and Dockerfile --- Dockerfile | 4 +++- .../payhub/pdnd/service/PdndService.java | 2 +- .../payhub/pdnd/service/PdndServiceTest.java | 19 ++++++++++++++++--- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index ee3557a..64ddf9c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -101,7 +101,9 @@ RUN mkdir -p src/main/java && \ USER ${APP_USER} -RUN gradle openApiGenerate dependencies --no-daemon +RUN gradle openApiGeneratePayhub dependencies --no-daemon + +RUN gradle openApiGeneratePdndClient dependencies --no-daemon # # 🏗️ Build Stage diff --git a/src/main/java/it/gov/pagopa/payhub/pdnd/service/PdndService.java b/src/main/java/it/gov/pagopa/payhub/pdnd/service/PdndService.java index 4492cbd..9b8b1e0 100644 --- a/src/main/java/it/gov/pagopa/payhub/pdnd/service/PdndService.java +++ b/src/main/java/it/gov/pagopa/payhub/pdnd/service/PdndService.java @@ -19,7 +19,7 @@ public class PdndService { private final PdndClientImpl pdndClientImpl; private final PdndClientAssertionBuilderService pdndClientAssertionBuilderService; - private final ConcurrentHashMap jwtCache = new ConcurrentHashMap<>(); + protected final ConcurrentHashMap jwtCache = new ConcurrentHashMap<>(); public PdndService(PdndClientImpl pdndClientImpl, PdndClientAssertionBuilderService pdndClientAssertionBuilderService) { diff --git a/src/test/java/it/gov/pagopa/payhub/pdnd/service/PdndServiceTest.java b/src/test/java/it/gov/pagopa/payhub/pdnd/service/PdndServiceTest.java index 434fcee..2ca16a5 100644 --- a/src/test/java/it/gov/pagopa/payhub/pdnd/service/PdndServiceTest.java +++ b/src/test/java/it/gov/pagopa/payhub/pdnd/service/PdndServiceTest.java @@ -2,15 +2,12 @@ import static org.junit.jupiter.api.Assertions.*; -import com.nimbusds.jose.JOSEException; import it.gov.pagopa.common.pdnd.generated.dto.ClientCredentialsResponseDTO; import it.gov.pagopa.payhub.pdnd.connector.pdnd.client.PdndClientImpl; import it.gov.pagopa.payhub.pdnd.connector.pdnd.service.PdndClientAssertionBuilderService; import it.gov.pagopa.payhub.pdnd.exception.custom.JwtClaimBuildException; import it.gov.pagopa.payhub.pdnd.model.PdndGenericConfig; import it.gov.pagopa.payhub.pdnd.utils.JWTUtils; -import java.io.IOException; -import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -58,6 +55,22 @@ void givenValidConfigWhenGenerateTokenThenGeneratesNewToken() throws Exception { Mockito.verify(pdndClientImpl, Mockito.times(1)).getAccessToken(clientId, clientAssertion); } + @Test + void givenTokenInCacheWhenGenerateTokenThenReturnCachedToken() { + // Given + PdndGenericConfig config = Mockito.mock(PdndGenericConfig.class); + String cachedToken = "CACHED_TOKEN"; + pdndService.jwtCache.put(config, cachedToken); + + try (MockedStatic mockedStatic = Mockito.mockStatic(JWTUtils.class)) { + // When + mockedStatic.when(() -> JWTUtils.isJWTExpired(cachedToken)).thenReturn(false); + String token = pdndService.generateToken(config); + + // Then + assertEquals(cachedToken, token); + } + } @Test void givenInvalidAssertionWhenGenerateTokenThenException() throws Exception {