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

chore: P4ADEV-668 operators list retrieve #46

Merged
merged 1 commit into from
Jul 2, 2024
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
14 changes: 6 additions & 8 deletions openapi/p4pa-auth.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ components:
content:
type: array
items:
$ref: '#/components/schemas/Operator'
$ref: '#/components/schemas/OperatorDTO'
description: "The list of organization operators"
pageNo:
type: integer
Expand All @@ -262,7 +262,7 @@ components:
type: integer
format: int32
description: "Number of total pages"
Operator:
OperatorDTO:
type: object
required:
- userId
Expand All @@ -273,15 +273,13 @@ components:
- organizationIpaCode
properties:
userId:
type: integer
format: int32
type: string
mappedExternalUserId:
type: string
userCode:
type: string
operatorId:
type: integer
format: int32
type: string
roles:
type: array
items:
Expand All @@ -290,9 +288,9 @@ components:
type: string
fiscalCode:
type: string
name:
firstName:
type: string
familyName:
lastName:
type: string
email:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@

public interface OperatorsRepository extends OperatorsRepositoryExt, MongoRepository<Operator, String> {
List<Operator> findAllByUserId(String userId);
List<Operator> findAllByOrganizationIpaCode(String organizationIpaCode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import it.gov.pagopa.payhub.auth.model.Operator;
import it.gov.pagopa.payhub.auth.model.User;
import it.gov.pagopa.payhub.model.generated.OperatorDTO;
import it.gov.pagopa.payhub.model.generated.UserInfo;

import java.util.List;
import java.util.Set;

public interface UserService {
User registerUser(String externalUserId, String fiscalCode, String iamIssuer, String firstName, String lastName, String email);
Operator registerOperator(String userId, String organizationIpaCode, Set<String> roles);
UserInfo getUserInfo(String accessToken);
List<OperatorDTO> retrieveOrganizationOperators(String organizationIpaCode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,31 @@
import it.gov.pagopa.payhub.auth.service.TokenStoreService;
import it.gov.pagopa.payhub.auth.service.user.registration.OperatorRegistrationService;
import it.gov.pagopa.payhub.auth.service.user.registration.UserRegistrationService;
import it.gov.pagopa.payhub.auth.service.user.retrieve.OrganizationOperatorRetrieverService;
import it.gov.pagopa.payhub.model.generated.OperatorDTO;
import it.gov.pagopa.payhub.model.generated.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class UserServiceImpl implements UserService{
public class UserServiceImpl implements UserService {

private final TokenStoreService tokenStoreService;
private final UserRegistrationService userRegistrationService;
private final OperatorRegistrationService operatorRegistrationService;
private final IamUserInfoDTO2UserInfoMapper userInfoMapper;
private final OrganizationOperatorRetrieverService organizationOperatorRetrieverService;

public UserServiceImpl(TokenStoreService tokenStoreService, UserRegistrationService userRegistrationService, OperatorRegistrationService operatorRegistrationService, IamUserInfoDTO2UserInfoMapper userInfoMapper) {
public UserServiceImpl(TokenStoreService tokenStoreService, UserRegistrationService userRegistrationService, OperatorRegistrationService operatorRegistrationService, IamUserInfoDTO2UserInfoMapper userInfoMapper, OrganizationOperatorRetrieverService organizationOperatorRetrieverService) {
this.tokenStoreService = tokenStoreService;
this.userRegistrationService = userRegistrationService;
this.operatorRegistrationService = operatorRegistrationService;
this.userInfoMapper = userInfoMapper;
this.organizationOperatorRetrieverService = organizationOperatorRetrieverService;
}

@Override
Expand All @@ -43,10 +48,16 @@ public Operator registerOperator(String userId, String organizationIpaCode, Set<
public UserInfo getUserInfo(String accessToken) {
log.info("Retrieving user info");
IamUserInfoDTO userInfo = tokenStoreService.load(accessToken);
if(userInfo==null){
if (userInfo == null) {
throw new InvalidAccessTokenException("AccessToken not found");
} else {
return userInfoMapper.apply(userInfo);
}
}

@Override
public List<OperatorDTO> retrieveOrganizationOperators(String organizationIpaCode) {
log.info("Retrieving organization {} operators", organizationIpaCode);
return organizationOperatorRetrieverService.retrieveOrganizationOperators(organizationIpaCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package it.gov.pagopa.payhub.auth.service.user.retrieve;

import it.gov.pagopa.payhub.auth.model.Operator;
import it.gov.pagopa.payhub.auth.model.User;
import it.gov.pagopa.payhub.model.generated.OperatorDTO;

import java.util.ArrayList;
import java.util.function.BiFunction;

public class OperatorDTOMapper implements BiFunction<User, Operator, OperatorDTO> {
@Override
public OperatorDTO apply(User user, Operator operator) {
return OperatorDTO.builder()
.userId(user.getUserId())
.mappedExternalUserId(user.getMappedExternalUserId())
.userCode(user.getUserCode())
.operatorId(operator.getOperatorId())
.roles(new ArrayList<>(operator.getRoles()))
.organizationIpaCode(operator.getOrganizationIpaCode())
.fiscalCode(user.getFiscalCode())
.firstName(user.getFirstName())
.lastName(user.getLastName())
.email(user.getEmail())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package it.gov.pagopa.payhub.auth.service.user.retrieve;

import it.gov.pagopa.payhub.auth.model.Operator;
import it.gov.pagopa.payhub.auth.model.User;
import it.gov.pagopa.payhub.auth.repository.OperatorsRepository;
import it.gov.pagopa.payhub.auth.repository.UsersRepository;
import it.gov.pagopa.payhub.model.generated.OperatorDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
@Slf4j
public class OrganizationOperatorRetrieverService {

private final OperatorsRepository operatorsRepository;
private final UsersRepository usersRepository;
private final OperatorDTOMapper operatorDTOMapper;

public OrganizationOperatorRetrieverService(OperatorsRepository operatorsRepository, UsersRepository usersRepository, OperatorDTOMapper operatorDTOMapper) {
this.operatorsRepository = operatorsRepository;
this.usersRepository = usersRepository;
this.operatorDTOMapper = operatorDTOMapper;
}

public List<OperatorDTO> retrieveOrganizationOperators(String organizationIpaCode) {
List<Operator> operators = operatorsRepository.findAllByOrganizationIpaCode(organizationIpaCode);
return operators.stream()
.map(op -> {
Optional<User> user = usersRepository.findById(op.getUserId());
if(user.isEmpty()){
log.warn("Found an operator without a user: {}", op);
return null;
} else {
return operatorDTOMapper.apply(user.get(), op);
}
})
.filter(Objects::nonNull)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import it.gov.pagopa.payhub.auth.service.TokenStoreService;
import it.gov.pagopa.payhub.auth.service.user.registration.OperatorRegistrationService;
import it.gov.pagopa.payhub.auth.service.user.registration.UserRegistrationService;
import it.gov.pagopa.payhub.auth.service.user.retrieve.OrganizationOperatorRetrieverService;
import it.gov.pagopa.payhub.model.generated.OperatorDTO;
import it.gov.pagopa.payhub.model.generated.UserInfo;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
Expand All @@ -17,6 +19,8 @@
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Collections;
import java.util.List;
import java.util.Set;

@ExtendWith(MockitoExtension.class)
Expand All @@ -30,12 +34,14 @@ class UserServiceTest {
private OperatorRegistrationService operatorRegistrationServiceMock;
@Mock
private IamUserInfoDTO2UserInfoMapper userInfoMapperMock;
@Mock
private OrganizationOperatorRetrieverService organizationOperatorRetrieverServiceMock;

private UserService service;

@BeforeEach
void init() {
service = new UserServiceImpl(tokenStoreServiceMock, userRegistrationServiceMock, operatorRegistrationServiceMock, userInfoMapperMock);
service = new UserServiceImpl(tokenStoreServiceMock, userRegistrationServiceMock, operatorRegistrationServiceMock, userInfoMapperMock, organizationOperatorRetrieverServiceMock);
}

@AfterEach
Expand All @@ -44,7 +50,8 @@ void verifyNotMoreInteractions() {
tokenStoreServiceMock,
userRegistrationServiceMock,
operatorRegistrationServiceMock,
userInfoMapperMock);
userInfoMapperMock,
organizationOperatorRetrieverServiceMock);
}

@Test
Expand Down Expand Up @@ -113,4 +120,19 @@ void whenRegisterOperatorThenReturnStoredOperator() {
// Then
Assertions.assertSame(storedOperator, result);
}

@Test
void whenRetrieveOrganizationOperatorsThenReturnOperatorList(){
// Given
String organizationIpaCode = "IPACODE";

List<OperatorDTO> expectedOperators = Collections.emptyList();
Mockito.when(organizationOperatorRetrieverServiceMock.retrieveOrganizationOperators(organizationIpaCode)).thenReturn(expectedOperators);

// When
List<OperatorDTO> result = service.retrieveOrganizationOperators(organizationIpaCode);

// Then
Assertions.assertSame(expectedOperators, result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package it.gov.pagopa.payhub.auth.service.user.retrieve;

import it.gov.pagopa.payhub.auth.model.Operator;
import it.gov.pagopa.payhub.auth.model.User;
import it.gov.pagopa.payhub.model.generated.OperatorDTO;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Set;

class OperatorDTOMapperTest {

private final OperatorDTOMapper mapper = new OperatorDTOMapper();

@Test
void test(){
// Given
User user = User.builder()
.userId("USERID")
.iamIssuer("IAMISSUER")
.mappedExternalUserId("MAPPEDEXTERNALUSERID")
.userCode("USERCODE")
.fiscalCode("FISCALCODE")
.firstName("FIRSTNAME")
.lastName("LASTNAME")
.email("EMAIL")
.build();

Operator operator = Operator.builder()
.operatorId("OPERATORID")
.userId("USERID")
.roles(Set.of("ROLES"))
.organizationIpaCode("ORGANIZATIONIPACODE")
.build();

// When
OperatorDTO result = mapper.apply(user, operator);

// Then
Assertions.assertEquals(
OperatorDTO.builder()
.userId("USERID")
.mappedExternalUserId("MAPPEDEXTERNALUSERID")
.userCode("USERCODE")
.operatorId("OPERATORID")
.roles(List.of("ROLES"))
.organizationIpaCode("ORGANIZATIONIPACODE")
.fiscalCode("FISCALCODE")
.firstName("FIRSTNAME")
.lastName("LASTNAME")
.email("EMAIL")
.build(),
result
);
}
}
Loading