-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: P4ADEV-1276-create-access-token (#98)
- Loading branch information
1 parent
df0f2bc
commit 7bdc711
Showing
13 changed files
with
161 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/it/gov/pagopa/payhub/auth/mapper/ClientDTO2UserInfoMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package it.gov.pagopa.payhub.auth.mapper; | ||
|
||
import it.gov.pagopa.payhub.auth.dto.IamUserInfoDTO; | ||
import it.gov.pagopa.payhub.auth.dto.IamUserOrganizationRolesDTO; | ||
import it.gov.pagopa.payhub.auth.utils.Constants; | ||
import it.gov.pagopa.payhub.model.generated.ClientDTO; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Collections; | ||
import java.util.function.Function; | ||
|
||
@Service | ||
public class ClientDTO2UserInfoMapper implements Function<ClientDTO, IamUserInfoDTO> { | ||
@Override | ||
public IamUserInfoDTO apply(ClientDTO clientDTO) { | ||
return IamUserInfoDTO.builder() | ||
.systemUser(true) | ||
.issuer(clientDTO.getOrganizationIpaCode()) | ||
.userId(clientDTO.getClientId()) | ||
.name(clientDTO.getClientName()) | ||
.familyName(clientDTO.getOrganizationIpaCode()) | ||
.fiscalCode(clientDTO.getOrganizationIpaCode()) | ||
.organizationAccess(IamUserOrganizationRolesDTO.builder() | ||
.organizationIpaCode(clientDTO.getOrganizationIpaCode()) | ||
.roles(Collections.singletonList(Constants.ROLE_ADMIN)) | ||
.build()) | ||
.build(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...e/exchange/AccessTokenBuilderService.java → ...th/service/AccessTokenBuilderService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
src/main/java/it/gov/pagopa/payhub/auth/service/a2a/ClientServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/test/java/it/gov/pagopa/payhub/auth/mapper/ClientDTO2UserInfoMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package it.gov.pagopa.payhub.auth.mapper; | ||
|
||
import it.gov.pagopa.payhub.auth.dto.IamUserInfoDTO; | ||
import it.gov.pagopa.payhub.auth.dto.IamUserOrganizationRolesDTO; | ||
import it.gov.pagopa.payhub.auth.utils.Constants; | ||
import it.gov.pagopa.payhub.auth.utils.TestUtils; | ||
import it.gov.pagopa.payhub.model.generated.ClientDTO; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.Collections; | ||
import java.util.UUID; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class ClientDTO2UserInfoMapperTest { | ||
|
||
@InjectMocks | ||
private ClientDTO2UserInfoMapper mapper; | ||
|
||
@Test | ||
void givenDTOWhenApplyTheOk() { | ||
// Given | ||
String plainClientSecret = UUID.randomUUID().toString(); | ||
String organizationIpaCode = "organizationIpaCode"; | ||
String clientName = "clientName"; | ||
String clientId = organizationIpaCode + clientName; | ||
|
||
ClientDTO clientDTO = ClientDTO.builder() | ||
.clientId(clientId) | ||
.clientName(clientName) | ||
.organizationIpaCode(organizationIpaCode) | ||
.clientSecret(plainClientSecret) | ||
.build(); | ||
IamUserInfoDTO iamUserInfoDTO = IamUserInfoDTO.builder() | ||
.systemUser(true) | ||
.issuer(clientDTO.getOrganizationIpaCode()) | ||
.userId(clientDTO.getClientId()) | ||
.name(clientDTO.getClientName()) | ||
.familyName(clientDTO.getOrganizationIpaCode()) | ||
.fiscalCode(clientDTO.getOrganizationIpaCode()) | ||
.organizationAccess(IamUserOrganizationRolesDTO.builder() | ||
.organizationIpaCode(clientDTO.getOrganizationIpaCode()) | ||
.roles(Collections.singletonList(Constants.ROLE_ADMIN)) | ||
.build()) | ||
.build(); | ||
// When | ||
IamUserInfoDTO result = mapper.apply(clientDTO); | ||
//Then | ||
Assertions.assertEquals(iamUserInfoDTO, result); | ||
TestUtils.checkNotNullFields(result, "innerUserId"); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...change/AccessTokenBuilderServiceTest.java → ...ervice/AccessTokenBuilderServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/test/java/it/gov/pagopa/payhub/auth/utils/TestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package it.gov.pagopa.payhub.auth.utils; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class TestUtils { | ||
private TestUtils(){} | ||
|
||
/** | ||
* It will assert not null on all o's fields | ||
*/ | ||
public static void checkNotNullFields(Object o, String... excludedFields) { | ||
Set<String> excludedFieldsSet = new HashSet<>(Arrays.asList(excludedFields)); | ||
org.springframework.util.ReflectionUtils.doWithFields(o.getClass(), | ||
f -> { | ||
f.setAccessible(true); | ||
Assertions.assertNotNull(f.get(o), "The field "+f.getName()+" of the input object of type "+o.getClass()+" is null!"); | ||
}, | ||
f -> !excludedFieldsSet.contains(f.getName())); | ||
} | ||
|
||
} |