Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: P4ADEV-1842 operatorRole changed to enum #20

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions openapi/p4pa-pu-bff.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ paths:
description: Forbidden - Access denied
'500':
description: Internal Server Error
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorDTO'
/brokers/config:
get:
tags:
Expand Down Expand Up @@ -70,7 +74,7 @@ paths:
items:
$ref: '#/components/schemas/ServiceStatus'
security:
- bearerAuth: []
- bearerAuth: [ ]
components:
securitySchemes:
bearerAuth:
Expand All @@ -97,9 +101,10 @@ components:
type: string
description: Organization name
operatorRole:
type: array
items:
type: string
type: string
enum:
- ROLE_ADMIN
- ROLE_OPER
description: Operator role
orgLogo:
type: string
Expand Down Expand Up @@ -154,3 +159,15 @@ components:
statusMessage:
type: string
description: Service status
ErrorDTO:
type: object
required:
- title
- description
properties:
title:
type: string
description: The title describing the error
description:
type: string
description: Description of the error
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package it.gov.pagopa.pu.bff.exception;

import it.gov.pagopa.pu.bff.dto.generated.ErrorDTO;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(InvalidOperatorRoleException.class)
public ResponseEntity<ErrorDTO> handleInvalidOperatorRoleException(InvalidOperatorRoleException e) {
ErrorDTO errorDTO = new ErrorDTO("GENERIC_ERROR", e.getMessage());
return new ResponseEntity<>(errorDTO, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package it.gov.pagopa.pu.bff.exception;

public class InvalidOperatorRoleException extends RuntimeException {
public InvalidOperatorRoleException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.gov.pagopa.pu.bff.mapper;

import it.gov.pagopa.pu.bff.dto.generated.OrganizationDTO;
import it.gov.pagopa.pu.bff.exception.InvalidOperatorRoleException;
import it.gov.pagopa.pu.p4pa_organization.dto.generated.Organization;
import org.springframework.stereotype.Component;

Expand All @@ -10,13 +11,32 @@
public class OrganizationDTOMapper {

public OrganizationDTO mapToOrganizationDTO(Organization organization, List<String> roles) {
OrganizationDTO.OperatorRoleEnum operatorRole = determineOperatorRole(roles);

return OrganizationDTO.builder()
.organizationId(organization.getOrganizationId())
.ipaCode(organization.getIpaCode())
.orgName(organization.getOrgName())
.operatorRole(roles)
.operatorRole(operatorRole)
.orgLogo(organization.getOrgLogoDesc())
.build();
}

private OrganizationDTO.OperatorRoleEnum determineOperatorRole(List<String> roles) {
if (roles == null || roles.isEmpty()) {
return null;
}

String operatorRoleValue = roles.stream()
.filter("ROLE_ADMIN"::equals)
.findFirst()
.orElse(roles.get(0));

try {
return OrganizationDTO.OperatorRoleEnum.fromValue(operatorRoleValue);
} catch (IllegalArgumentException e) {
throw new InvalidOperatorRoleException("INVALID_OPERATOR_ROLE: " + operatorRoleValue);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -42,11 +41,13 @@ void setUp() {
SecurityContextHolder.setContext(securityContext);

organizationDTOList = new ArrayList<>();
OrganizationDTO.OperatorRoleEnum operatorRole = OrganizationDTO.OperatorRoleEnum.ADMIN;

OrganizationDTO organizationDTO = OrganizationDTO.builder()
.organizationId(123L)
.ipaCode("IPA001")
.orgName("Test Organization")
.operatorRole(Arrays.asList("Admin", "User"))
.operatorRole(operatorRole)
.build();

organizationDTOList.add(organizationDTO);
Expand All @@ -61,6 +62,7 @@ void testGetOrganizations() {
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(1, response.getBody().size());
assertEquals("Test Organization", response.getBody().get(0).getOrgName());
assertEquals(OrganizationDTO.OperatorRoleEnum.ADMIN, response.getBody().get(0).getOperatorRole());

verify(organizationService, times(1)).getOrganizations(any(), any());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package it.gov.pagopa.pu.bff.mapper;

import it.gov.pagopa.pu.bff.dto.generated.OrganizationDTO;
import it.gov.pagopa.pu.bff.exception.InvalidOperatorRoleException;
import it.gov.pagopa.pu.bff.util.TestUtils;
import it.gov.pagopa.pu.p4pa_organization.dto.generated.Organization;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand All @@ -19,13 +19,13 @@ class OrganizationDTOMapperTest {
private final OrganizationDTOMapper mapper = new OrganizationDTOMapper();

@Test
void testMapToOrganizationDTO() {
void testMapToOrganizationDTO_ValidRoles() {
Organization organization = new Organization();
organization.setOrganizationId(123L);
organization.setIpaCode("testIpaCode");
organization.setOrgName("Test Organization");
organization.setOrgLogoDesc("base64LogoString");
List<String> roles = Arrays.asList("Admin", "User");
List<String> roles = Collections.singletonList("ROLE_ADMIN");

OrganizationDTO result = mapper.mapToOrganizationDTO(organization, roles);

Expand All @@ -34,10 +34,24 @@ void testMapToOrganizationDTO() {
assertEquals(123L, result.getOrganizationId());
assertEquals("testIpaCode", result.getIpaCode());
assertEquals("Test Organization", result.getOrgName());
assertEquals(roles, result.getOperatorRole());
assertEquals(OrganizationDTO.OperatorRoleEnum.ADMIN, result.getOperatorRole());
assertEquals("base64LogoString", result.getOrgLogo());
}

@Test
void testMapToOrganizationDTO_InvalidRole() {
Organization organization = new Organization();
organization.setOrganizationId(123L);
organization.setIpaCode("testIpaCode");
organization.setOrgName("Test Organization");
List<String> roles = Collections.singletonList("INVALID_ROLE");

Exception exception = assertThrows(InvalidOperatorRoleException.class, () ->
mapper.mapToOrganizationDTO(organization, roles));

assertEquals("INVALID_OPERATOR_ROLE: INVALID_ROLE", exception.getMessage());
}

@Test
void testMapToOrganizationDTO_EmptyRoles() {
Organization organization = new Organization();
Expand All @@ -52,7 +66,7 @@ void testMapToOrganizationDTO_EmptyRoles() {
assertEquals(123L, result.getOrganizationId());
assertEquals("testIpaCode", result.getIpaCode());
assertEquals("Test Organization", result.getOrgName());
assertTrue(result.getOperatorRole().isEmpty());
assertNull(result.getOperatorRole());
assertNull(result.getOrgLogo());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class OrganizationServiceImplTest {
void setUp() {
userOrganizationRoles = new UserOrganizationRoles();
userOrganizationRoles.setOrganizationIpaCode("testIpaCode");
userOrganizationRoles.setRoles(Collections.singletonList("Admin"));
userOrganizationRoles.setRoles(Collections.singletonList("ROLE_ADMIN"));

userInfo = new UserInfo();
userInfo.setOrganizations(Collections.singletonList(userOrganizationRoles));
Expand All @@ -53,7 +53,7 @@ void setUp() {
.organizationId(123L)
.ipaCode("testIpaCode")
.orgName("Test Organization")
.operatorRole(Collections.singletonList("Admin"))
.operatorRole(OrganizationDTO.OperatorRoleEnum.ADMIN)
.build();

organizationService = new OrganizationServiceImpl(organizationSearchClientMock, organizationDTOMapperMock);
Expand All @@ -69,10 +69,10 @@ void testGetOrganizations() {
List<OrganizationDTO> result = organizationService.getOrganizations(userInfo, accessToken);

assertEquals(1, result.size());
assertEquals(123L, result.getFirst().getOrganizationId());
assertEquals("testIpaCode", result.getFirst().getIpaCode());
assertEquals("Test Organization", result.getFirst().getOrgName());
assertEquals(Collections.singletonList("Admin"), result.getFirst().getOperatorRole());
assertEquals(123L, result.get(0).getOrganizationId());
assertEquals("testIpaCode", result.get(0).getIpaCode());
assertEquals("Test Organization", result.get(0).getOrgName());
assertEquals(OrganizationDTO.OperatorRoleEnum.ADMIN, result.get(0).getOperatorRole());
}

@Test
Expand Down
Loading