From 9516cd068330ca81c00704d4865cc53a4a5a6b6f Mon Sep 17 00:00:00 2001 From: antoniocalo Date: Fri, 22 Nov 2024 16:04:15 +0100 Subject: [PATCH] P4ADEV-1341 add unitTest --- build.gradle.kts | 4 + .../connector/pdnd/client/PdndClientImpl.java | 1 - ...PdndClientAssertionBuilderServiceTest.java | 73 +++++++++++ .../payhub/pdnd/utils/CertUtilsTest.java | 8 +- .../payhub/pdnd/utils/JWTUtilsTest.java | 6 +- .../payhub/pdnd/utils/PdndUtilsTest.java | 120 ------------------ 6 files changed, 84 insertions(+), 128 deletions(-) create mode 100644 src/test/java/it/gov/pagopa/payhub/pdnd/connector/pdnd/service/PdndClientAssertionBuilderServiceTest.java delete mode 100644 src/test/java/it/gov/pagopa/payhub/pdnd/utils/PdndUtilsTest.java diff --git a/build.gradle.kts b/build.gradle.kts index 4f1b0cb..bb85b2a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -35,6 +35,7 @@ val javaJwtVersion = "4.4.0" val jwksRsaVersion = "0.22.1" val nimbusJoseJwtVersion = "9.47" val jjwtVersion = "0.12.6" +val wiremockVersion = "3.9.2" dependencies { implementation("org.springframework.boot:spring-boot-starter") @@ -58,8 +59,11 @@ dependencies { // Testing testImplementation("org.springframework.boot:spring-boot-starter-test") testImplementation("org.springframework.security:spring-security-test") + testImplementation("org.junit.jupiter:junit-jupiter-api") + testImplementation("org.junit.jupiter:junit-jupiter-engine") testImplementation("org.mockito:mockito-core") testImplementation ("org.projectlombok:lombok") + testImplementation ("org.wiremock:wiremock-standalone:$wiremockVersion") } tasks.withType { diff --git a/src/main/java/it/gov/pagopa/payhub/pdnd/connector/pdnd/client/PdndClientImpl.java b/src/main/java/it/gov/pagopa/payhub/pdnd/connector/pdnd/client/PdndClientImpl.java index 27c2126..9b525a8 100644 --- a/src/main/java/it/gov/pagopa/payhub/pdnd/connector/pdnd/client/PdndClientImpl.java +++ b/src/main/java/it/gov/pagopa/payhub/pdnd/connector/pdnd/client/PdndClientImpl.java @@ -18,7 +18,6 @@ public class PdndClientImpl implements PdndClient { public PdndClientImpl(RestTemplateBuilder restTemplateBuilder, @Value("${app.pdnd.base-url}") String pdndBaseUrl) { RestTemplate restTemplate = restTemplateBuilder.build(); - ApiClient apiClient = new ApiClient(restTemplate); apiClient.setBasePath(pdndBaseUrl); authApi = new AuthApi(apiClient); diff --git a/src/test/java/it/gov/pagopa/payhub/pdnd/connector/pdnd/service/PdndClientAssertionBuilderServiceTest.java b/src/test/java/it/gov/pagopa/payhub/pdnd/connector/pdnd/service/PdndClientAssertionBuilderServiceTest.java new file mode 100644 index 0000000..bb53481 --- /dev/null +++ b/src/test/java/it/gov/pagopa/payhub/pdnd/connector/pdnd/service/PdndClientAssertionBuilderServiceTest.java @@ -0,0 +1,73 @@ +package it.gov.pagopa.payhub.pdnd.connector.pdnd.service; + +import static org.junit.jupiter.api.Assertions.*; + +import it.gov.pagopa.payhub.pdnd.config.PdndConfig; +import it.gov.pagopa.payhub.pdnd.model.PdndGenericConfig; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class PdndClientAssertionBuilderServiceTest { + + + @Mock + private PdndConfig pdndConfig; + + @Mock + private PdndGenericConfig pdndGenericConfig; + + @InjectMocks + private PdndClientAssertionBuilderService pdndClientAssertionBuilderService; + + private String pemKey = """ + -----BEGIN PRIVATE KEY----- + MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCT5fdA/ZKoyLas + R5/kxfFm8KBz4v3i8k76Xd8j2vh8kBaapzn9hAHWJXOJ+GOUFOxkw1bnI2PUtZjj + tw49XrjcxQ37sOV407+B3ko49zZjNB97OPFQyZx9V3uNcBjKnM3UqNbcBwdIIlVW + Egt0Cao7gEGE1CKsaXpuZkofVgGo5f8K8IdETLJPFuspDTR4UPofDra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ReVrDX1+uCqwmplKnGjnFQmz4MTaH/A1JI7IqyR0YIaO6V/ + zqnd2O60MSeToPa8dUK7+UGymL6VgarLzMjAXfYYMEO52sXlVAvVn5I8+BvvYd3B + VGf9ZyguOySZXLkoqVkAtvA7Nlr09QA6q+oWL5MCgYAsLS2PEMY/HMR1Z5P/uMxw + q7eQ7K3YYKcJpbM2da7r38UaZc/HhtiaU/XOdTnT/M/eF4hoW0yxO5YKfgurgosz + OjAnn7+Ed5S5Sh8E4EHUGCcawErZEZCtlsns0fNPGfNjadZAjq0X+5VP1EVXca0B + VrSp9ZTif3cvyxNTOogbgA== + -----END PRIVATE KEY----- + """; + + @Test + void givenValidPDNDConfigWhenBuildPdndClientAssertionThenVerifyToken() throws Exception { + // Given + Mockito.when(pdndConfig.getAudience()).thenReturn("AUDIENCE"); + Mockito.when(pdndConfig.getKey()).thenReturn(pemKey); + + Mockito.when(pdndGenericConfig.getClientId()).thenReturn("CLIENTID"); + Mockito.when(pdndGenericConfig.getPurposeId()).thenReturn("PURPOSEID"); + Mockito.when(pdndGenericConfig.getKid()).thenReturn("KID"); + // When + String token = pdndClientAssertionBuilderService.buildPdndClientAssertion(pdndGenericConfig); + + // Then + assertNotNull(token); + } +} \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/payhub/pdnd/utils/CertUtilsTest.java b/src/test/java/it/gov/pagopa/payhub/pdnd/utils/CertUtilsTest.java index f1f7458..025f2fa 100644 --- a/src/test/java/it/gov/pagopa/payhub/pdnd/utils/CertUtilsTest.java +++ b/src/test/java/it/gov/pagopa/payhub/pdnd/utils/CertUtilsTest.java @@ -11,7 +11,7 @@ class CertUtilsTest { @Test - void whenPemKey2PrivateKeyThenValidKey() throws InvalidKeySpecException, NoSuchAlgorithmException, IOException { + void givenValidPrivateKeyWhenPemKey2PrivateKeyThenValidKey() throws InvalidKeySpecException, NoSuchAlgorithmException, IOException { // Given String pemKey = """ -----BEGIN PRIVATE KEY----- @@ -53,7 +53,7 @@ void whenPemKey2PrivateKeyThenValidKey() throws InvalidKeySpecException, NoSuchA } @Test - void whenPemKey2PrivateKeyThenInvalidKey() { + void givenInvalidPrivateKeyWhenPemKey2PrivateKeyThenInvalidKey() { // Given String invalidPemKey = """ -----BEGIN PRIVATE KEY----- @@ -66,7 +66,7 @@ void whenPemKey2PrivateKeyThenInvalidKey() { } @Test - void whenPemKey2PrivateKeyThenNullKey() { + void givenNullPrivateKeyWhenPemKey2PrivateKeyThenNullKey() { // Given String nullKey = null; @@ -75,7 +75,7 @@ void whenPemKey2PrivateKeyThenNullKey() { } @Test - void whenExtractInlinePemBodyThenValidPem() { + void givenValidPemWhenExtractInlinePemBodyThenValidPem() { // Given String pemKey = """ -----BEGIN PRIVATE KEY----- diff --git a/src/test/java/it/gov/pagopa/payhub/pdnd/utils/JWTUtilsTest.java b/src/test/java/it/gov/pagopa/payhub/pdnd/utils/JWTUtilsTest.java index 5dc8829..6603edd 100644 --- a/src/test/java/it/gov/pagopa/payhub/pdnd/utils/JWTUtilsTest.java +++ b/src/test/java/it/gov/pagopa/payhub/pdnd/utils/JWTUtilsTest.java @@ -10,7 +10,7 @@ class JWTUtilsTest { @Test - public void givenValidTokenWhenIsJWTExpiredThenTokenNotExpired() { + void givenValidTokenWhenIsJWTExpiredThenTokenNotExpired() { // Given Date futureDate = new Date(System.currentTimeMillis() + 3600 * 1000); // 1 hour from now String token = JWT.create() @@ -22,7 +22,7 @@ public void givenValidTokenWhenIsJWTExpiredThenTokenNotExpired() { } @Test - public void givenExpiredTokenWhenIsJWTExpiredThenTokenExpired() { + void givenExpiredTokenWhenIsJWTExpiredThenTokenExpired() { // Given Date pastDate = new Date(System.currentTimeMillis() - 3600 * 1000); // 1 hour ago String token = JWT.create() @@ -33,7 +33,7 @@ public void givenExpiredTokenWhenIsJWTExpiredThenTokenExpired() { } @Test - public void givenInvalidTokenWhenIsJWTExpiredThenException() { + void givenInvalidTokenWhenIsJWTExpiredThenException() { // Given String invalidtoken = "INVALIDTOKEN"; // Then diff --git a/src/test/java/it/gov/pagopa/payhub/pdnd/utils/PdndUtilsTest.java b/src/test/java/it/gov/pagopa/payhub/pdnd/utils/PdndUtilsTest.java deleted file mode 100644 index cb8c633..0000000 --- a/src/test/java/it/gov/pagopa/payhub/pdnd/utils/PdndUtilsTest.java +++ /dev/null @@ -1,120 +0,0 @@ -package it.gov.pagopa.payhub.pdnd.utils; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.times; - -import com.nimbusds.jwt.JWTClaimsSet; -import com.nimbusds.jwt.SignedJWT; -import it.gov.pagopa.payhub.pdnd.config.PdndConfig; -import it.gov.pagopa.payhub.pdnd.connector.pdnd.service.PdndClientAssertionBuilderService; -import java.util.Date; -import java.util.UUID; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.junit.jupiter.MockitoExtension; - -@ExtendWith(MockitoExtension.class) -class PdndUtilsTest { - - @InjectMocks - private PdndClientAssertionBuilderService pdndClientAssertionBuilderService; - - @Mock - private PdndConfig pdndConfig; - - private String pemKey = """ ------BEGIN PRIVATE KEY----- -MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCT5fdA/ZKoyLas -R5/kxfFm8KBz4v3i8k76Xd8j2vh8kBaapzn9hAHWJXOJ+GOUFOxkw1bnI2PUtZjj -tw49XrjcxQ37sOV407+B3ko49zZjNB97OPFQyZx9V3uNcBjKnM3UqNbcBwdIIlVW -Egt0Cao7gEGE1CKsaXpuZkofVgGo5f8K8IdETLJPFuspDTR4UPofDraL2HCxbsVx -dE0UBFXgB9vQmBMkPk27cz+Ze6j5wgSGME/A+YCCp1uvJqWK/uRfGxMRyVYInR5H -bDiI06iZwiLLW1Pf6gE3CCYSUw42VnPHODaitjJ6XLkolB5xsUprkttIg+UrRGSa -9J3xg3gNAgMBAAECggEASKjRCS/KjntVK1xg1F7e0yjiWyyoeId8f4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bM2da7r38UaZc/HhtiaU/XOdTnT/M/eF4hoW0yxO5YKfgurgosz -OjAnn7+Ed5S5Sh8E4EHUGCcawErZEZCtlsns0fNPGfNjadZAjq0X+5VP1EVXca0B -VrSp9ZTif3cvyxNTOogbgA== ------END PRIVATE KEY----- - """; - -/* - @Test - void whenBuildPdndClientAssertionThesVerify() throws Exception { - // When - Mockito.when(pdndConfig.getClientId()).thenReturn("CLIENTID"); - Mockito.when(pdndConfig.getAudience()).thenReturn("AUDIENCE"); - Mockito.when(pdndConfig.getPurposeId()).thenReturn("PURPOSEID"); - Mockito.when(pdndConfig.getKey()).thenReturn(pemKey); - Mockito.when(pdndConfig.getKid()).thenReturn("KID"); - - String token = pdndClientAssertionBuilderService.buildPdndClientAssertion(); - - // Then - assertNotNull(token); - Mockito.verify(pdndConfig, times(2)).getClientId(); - Mockito.verify(pdndConfig).getAudience(); - Mockito.verify(pdndConfig).getPurposeId(); - Mockito.verify(pdndConfig).getKey(); - Mockito.verify(pdndConfig).getKid(); - } - - @Test - void whenBuildPdndClientAssertionClaimsThenVerify() { - // Given - Mockito.when(pdndConfig.getClientId()).thenReturn("CLIENTID"); - Mockito.when(pdndConfig.getAudience()).thenReturn("AUDIENCE"); - Mockito.when(pdndConfig.getPurposeId()).thenReturn("PURPOSEID"); - // When - JWTClaimsSet claims = pdndClientAssertionBuilderService.buildPdndClientAssertionClaims(pdndConfig.getPurposeId()); - - // Then - assertNotNull(claims); - assertEquals("CLIENTID", claims.getIssuer()); - assertEquals("CLIENTID", claims.getSubject()); - assertEquals("AUDIENCE", claims.getAudience().get(0)); - assertEquals("PURPOSEID", claims.getClaim("purposeId")); - assertNotNull(claims.getIssueTime()); - assertNotNull(claims.getExpirationTime()); - assertNotNull(claims.getJWTID()); - } - - @Test - void whenSignPdndJWTThenVerify() throws Exception { - Mockito.when(pdndConfig.getKey()).thenReturn(pemKey); - - JWTClaimsSet claims = new JWTClaimsSet.Builder() - .issuer("CLIENTID") - .subject("SUBJECT") - .audience("AUDIENCE") - .issueTime(new Date()) - .expirationTime(new Date(System.currentTimeMillis() + 300000)) - .jwtID(UUID.randomUUID().toString()) - .build(); - - String signedJWT = pdndClientAssertionBuilderService.signPdndJWT(claims); - - SignedJWT parsedJWT = SignedJWT.parse(signedJWT); - assertNotNull(parsedJWT); - assertEquals("CLIENTID", parsedJWT.getJWTClaimsSet().getIssuer()); - } - */ -} \ No newline at end of file