Skip to content

Commit

Permalink
P4ADEV-1341 add unitTest
Browse files Browse the repository at this point in the history
  • Loading branch information
antocalo committed Nov 22, 2024
1 parent 9f5c044 commit 9516cd0
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 128 deletions.
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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<Test> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Egt0Cao7gEGE1CKsaXpuZkofVgGo5f8K8IdETLJPFuspDTR4UPofDraL2HCxbsVx
dE0UBFXgB9vQmBMkPk27cz+Ze6j5wgSGME/A+YCCp1uvJqWK/uRfGxMRyVYInR5H
bDiI06iZwiLLW1Pf6gE3CCYSUw42VnPHODaitjJ6XLkolB5xsUprkttIg+UrRGSa
9J3xg3gNAgMBAAECggEASKjRCS/KjntVK1xg1F7e0yjiWyyoeId8f4oApzfbni6X
vFDtr3vb/x4VHjJWkZiZ7oL9Pb7oO8cfnrf/Ge1gOq3gycdFZU/6JM5VfpkNMj2Y
Pcxi2cLCy91fyMPKmjfg81ojfKNDU4/yhr+EuvRImsTO63fgtP149aXxQmXZmOTu
TFjSNTRfvtMgHN0Em1PUgQxO8oUh3Djf5spjAJ/w+gVBSYsYSv5sOOi2H/qZSALZ
hc1t4GfzNKZuyG8FxNwH1SIVkKTYQnDhyiE9426tq6Kiuqvh2MspVJcRGpbaxgr2
q++ZZrAl60ma5U2hUEgG5oLGjyrgQjEyroZhEokgLQKBgQDKIeAJ/FYdEX4cvHhS
kuUpHQjpZtwOwC+vr4ojudpjLDOTTdkFXzd7jeCmjp4r1/arRxx1KZWP0fxlUEov
0LDiaU0zBeol/q0ayq5XnhJNVngCyKjQQ+Np1eIGTIIGOkAm8LlnEsvlQLbuOYZ4
eeeplBW3h321MFKgch7IyqBb5wKBgQC7UBG/ypw6RWPUOHYdtY1nLCQQJjvKCOMT
DolkFB2UUuNfNGK6PDUL9KbPIsrHJLw0oGoqQyBkInVMG5jJb/bHdH0spiKGn51u
orMk/xsA990Kqt+DT1Z5fEpoPchGMc529JR5h43n1n5s8/6jyDa5JNLFnS9xKZTm
IvV/Nayt6wKBgGxpSs5QRqeEkE09UJOJMduhNPxqLLDEp07lKYQL1HPIa0kgQbu9
2/YqnEj4ySDezfADTeIREaR3jZWRQJjwp05oB/3LuE/0jkeGWYeowkw0il2D3fcF
0l0bWATk2AAbEflQtz/vNuiYkwSmWdcYGwY65ILw6p1Zc5eWXah39RYVAoGAI93Y
GDZupcXFsMxC6btq4ReVrDX1+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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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-----
Expand Down Expand Up @@ -53,7 +53,7 @@ void whenPemKey2PrivateKeyThenValidKey() throws InvalidKeySpecException, NoSuchA
}

@Test
void whenPemKey2PrivateKeyThenInvalidKey() {
void givenInvalidPrivateKeyWhenPemKey2PrivateKeyThenInvalidKey() {
// Given
String invalidPemKey = """
-----BEGIN PRIVATE KEY-----
Expand All @@ -66,7 +66,7 @@ void whenPemKey2PrivateKeyThenInvalidKey() {
}

@Test
void whenPemKey2PrivateKeyThenNullKey() {
void givenNullPrivateKeyWhenPemKey2PrivateKeyThenNullKey() {
// Given
String nullKey = null;

Expand All @@ -75,7 +75,7 @@ void whenPemKey2PrivateKeyThenNullKey() {
}

@Test
void whenExtractInlinePemBodyThenValidPem() {
void givenValidPemWhenExtractInlinePemBodyThenValidPem() {
// Given
String pemKey = """
-----BEGIN PRIVATE KEY-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -33,7 +33,7 @@ public void givenExpiredTokenWhenIsJWTExpiredThenTokenExpired() {
}

@Test
public void givenInvalidTokenWhenIsJWTExpiredThenException() {
void givenInvalidTokenWhenIsJWTExpiredThenException() {
// Given
String invalidtoken = "INVALIDTOKEN";
// Then
Expand Down
120 changes: 0 additions & 120 deletions src/test/java/it/gov/pagopa/payhub/pdnd/utils/PdndUtilsTest.java

This file was deleted.

0 comments on commit 9516cd0

Please sign in to comment.