Skip to content

Commit

Permalink
[SELC-5862] fix: serialize ADMIN_EA updating openapi of user-ms (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
manuraf authored Oct 24, 2024
1 parent 2c2ed5f commit 7bcecfd
Show file tree
Hide file tree
Showing 9 changed files with 585 additions and 190 deletions.
650 changes: 535 additions & 115 deletions src/main/docs/openapi/api-selfcare-user-docs.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package it.pagopa.selfcare.external_api.client;

import it.pagopa.selfcare.user.generated.openapi.v1.api.UserControllerApi;
import it.pagopa.selfcare.user.generated.openapi.v1.api.UserApi;
import org.springframework.cloud.openfeign.FeignClient;

@FeignClient(name = "${rest-client.ms-user-api.serviceCode}", url = "${rest-client.ms-user-api.base-url}")
public interface MsUserApiRestClient extends UserControllerApi {
public interface MsUserApiRestClient extends UserApi {
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public ProductService getProductService(){
@Named("toProductRoleLabel")
protected String toProductRoleLabel(OnboardedProductResponse onboardedProduct, Product product) {
ProductRole productRole = null;
try { productRole = ProductUtils.getProductRole(onboardedProduct.getProductRole(), PartyRole.valueOf(onboardedProduct.getRole().name()), product); }
try { productRole = ProductUtils.getProductRole(onboardedProduct.getProductRole(), PartyRole.valueOf(onboardedProduct.getRole()), product); }
catch (IllegalArgumentException ignored) {}

return Optional.ofNullable(productRole)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public List<ProductResource> getInstitutionUserProductsV2(String institutionId,
InstitutionResponse institutionResponse = institutionApiClient._retrieveInstitutionByIdUsingGET(institutionId).getBody();

Set<String> productsSet = new HashSet<>();
ResponseEntity<List<UserDataResponse>> response = msUserApiRestClient._usersUserIdInstitutionInstitutionIdGet(institutionId, userId, userId, null, null, null, List.of(ACTIVE.name()));
ResponseEntity<List<UserDataResponse>> response = msUserApiRestClient._retrieveUsers(institutionId, userId, userId, null, null, null, List.of(ACTIVE.name()));
if (Objects.nonNull(response) && Objects.nonNull(response.getBody()) && !response.getBody().isEmpty()) {
//There is only a document for the couple institutionId/userId
productsSet = response.getBody().get(0).getProducts().stream()
Expand All @@ -154,7 +154,7 @@ public Collection<UserProductResponse> getInstitutionProductUsersV2(String insti
Assert.hasText(productId, "A Product id is required");
List<String> productRolesList = CollectionUtils.isEmpty(productRoles) ? null : new ArrayList<>(productRoles);

List<UserInstitution> usersInstitutions = Objects.requireNonNull(msUserApiRestClient._usersGet(
List<UserInstitution> usersInstitutions = Objects.requireNonNull(msUserApiRestClient._retrievePaginatedAndFilteredUser(
institutionId, null, productRolesList, List.of(productId), null
, null, List.of(ACTIVE.name()), userId).getBody())
.stream().map(userMapper::toUserInstitutionsFromUserInstitutionResponse).toList();
Expand All @@ -170,7 +170,7 @@ public Collection<UserProductResponse> getInstitutionProductUsersV2(String insti
userProduct.setRoles(userInstitution.getProducts().stream().map(OnboardedProductResponse::getProductRole).toList());
return userProduct;
})
.collect(Collectors.toList());
.toList();

log.debug(LogUtils.CONFIDENTIAL_MARKER, "getInstitutionProductUsers result = {}", userProductResponses);
log.trace("getInstitutionProductUsers end");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private List<RelationshipInfo> processUsersWithId(Map<String, List<UserToOnboard
private String addUserRole(String userId, Institution institution, String productId, String role, List<String> productRoles) {
it.pagopa.selfcare.user.generated.openapi.v1.dto.Product product = it.pagopa.selfcare.user.generated.openapi.v1.dto.Product.builder()
.productId(productId)
.role(it.pagopa.selfcare.user.generated.openapi.v1.dto.PartyRole.valueOf(role))
.role(role)
.productRoles(productRoles)
.build();

Expand All @@ -117,7 +117,7 @@ private String addUserRole(String userId, Institution institution, String produc
.institutionRootName(institution.getParentDescription())
.build();

msUserApiRestClient._usersUserIdPost(userId, addUserRoleDto);
msUserApiRestClient._createOrUpdateByUserId(userId, addUserRoleDto);

return userId;
}
Expand All @@ -141,7 +141,7 @@ private String createUser(Institution institution, String productId, String role

Product1 product = Product1.builder()
.productId(productId)
.role(it.pagopa.selfcare.user.generated.openapi.v1.dto.PartyRole.valueOf(role))
.role(role)
.productRoles(productRoles)
.build();

Expand All @@ -154,7 +154,7 @@ private String createUser(Institution institution, String productId, String role
.institutionRootName(institution.getParentDescription())
.build();

String userId = msUserApiRestClient._usersPost(createUserDto).getBody();
String userId = msUserApiRestClient._createOrUpdateByFiscalCode(createUserDto).getBody();
log.info("User created with id: {}", userId);
return userId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class UserServiceImpl implements UserService {
public UserInfoWrapper getUserInfoV2(String fiscalCode, List<RelationshipState> userStatuses) {
log.trace("geUserInfo start");
it.pagopa.selfcare.user.generated.openapi.v1.dto.SearchUserDto searchUserDto = new it.pagopa.selfcare.user.generated.openapi.v1.dto.SearchUserDto(fiscalCode);
final User user = userMapper.toUserFromUserDetailResponse(msUserApiRestClient._usersSearchPost(null, searchUserDto).getBody());
final User user = userMapper.toUserFromUserDetailResponse(msUserApiRestClient._searchUserByFiscalCode(null, searchUserDto).getBody());
List<OnboardedInstitutionInfo> onboardedInstitutions = getOnboardedInstitutionsDetails(user.getId(), null);
List<String> userStatusesString = userStatuses == null ? Collections.emptyList()
: userStatuses.stream().map(RelationshipState::toString).toList();
Expand All @@ -67,7 +67,7 @@ public UserInfoWrapper getUserInfoV2(String fiscalCode, List<RelationshipState>

@Override
public UserDetailsWrapper getUserOnboardedProductsDetailsV2(String userId, String institutionId, String productId) {
List<UserInstitution> usersInstitutions = Objects.requireNonNull(msUserApiRestClient._usersGet(
List<UserInstitution> usersInstitutions = Objects.requireNonNull(msUserApiRestClient._retrievePaginatedAndFilteredUser(
institutionId, null, null, List.of(productId), null
, null, null, userId).getBody())
.stream().map(userMapper::toUserInstitutionsFromUserInstitutionResponse).toList();
Expand Down Expand Up @@ -106,7 +106,7 @@ public UserDetailsWrapper getUserOnboardedProductsDetailsV2(String userId, Strin
public List<OnboardedInstitutionInfo> getOnboardedInstitutionsDetails(String userId, String productId) {
//fix temporanea per il funzionamento della getUserInfo di support

List<UserInstitution> usersInstitutions = Objects.requireNonNull(msUserApiRestClient._usersGet(
List<UserInstitution> usersInstitutions = Objects.requireNonNull(msUserApiRestClient._retrievePaginatedAndFilteredUser(
null, null, null, Objects.isNull(productId) ? null : List.of(productId), null
, 350, null, userId).getBody())
.stream().map(userMapper::toUserInstitutionsFromUserInstitutionResponse).toList();
Expand Down Expand Up @@ -167,7 +167,7 @@ private List<OnboardedInstitutionInfo> getInstitutionDetails(String institutionI
@Override
public List<OnboardedInstitutionResource> getOnboardedInstitutionsDetailsActive(String userId, String productId) {

List<UserInstitution> institutionsWithProductActive = Objects.requireNonNull(msUserApiRestClient._usersGet(
List<UserInstitution> institutionsWithProductActive = Objects.requireNonNull(msUserApiRestClient._retrievePaginatedAndFilteredUser(
null, null, null, Objects.isNull(productId) ? null : List.of(productId), null
, null, List.of(ACTIVE.name()), userId).getBody())
.stream().map(userMapper::toUserInstitutionsFromUserInstitutionResponse)
Expand Down Expand Up @@ -218,22 +218,9 @@ private void retrieveBilling(Institution institution, String productId, Onboarde

@Override
public List<UserInstitution> getUsersInstitutions(String userId, String institutionId, Integer page, Integer size, List<String> productRoles, List<String> products, List<PartyRole> roles, List<String> states){
return Objects.requireNonNull(msUserApiRestClient._usersGet(
institutionId, page, productRoles, products, toDtoPartyRole(roles)
, size, states, userId).getBody())
return Objects.requireNonNull(msUserApiRestClient._retrievePaginatedAndFilteredUser(
institutionId, page, productRoles, products, Optional.ofNullable(roles).map(rolesList -> rolesList.stream().map(PartyRole::name).toList()).orElse(null),
size, states, userId).getBody())
.stream().map(userMapper::toUserInstitutionsFromUserInstitutionResponse).toList();
}

private List<it.pagopa.selfcare.user.generated.openapi.v1.dto.PartyRole> toDtoPartyRole(List<PartyRole> roles) {
List<it.pagopa.selfcare.user.generated.openapi.v1.dto.PartyRole> partyRoles = new ArrayList<>();
if (roles != null) {
roles.forEach(partyRole -> {
it.pagopa.selfcare.user.generated.openapi.v1.dto.PartyRole role = it.pagopa.selfcare.user.generated.openapi.v1.dto.PartyRole.valueOf(partyRole.name());
partyRoles.add(role);
});
} else {
return Collections.emptyList();
}
return partyRoles;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void getInstitutionUserProducts_WithOneMatch() {
it.pagopa.selfcare.user.generated.openapi.v1.dto.OnboardedProductResponse onboardedProductResponse2 = new it.pagopa.selfcare.user.generated.openapi.v1.dto.OnboardedProductResponse();
onboardedProductResponse2.setProductId("123");
userDataResponse.setProducts(List.of(onboardedProductResponse, onboardedProductResponse2));
when(msUserApiRestClient._usersUserIdInstitutionInstitutionIdGet(institutionId, userId, userId, null, null, null, List.of(ACTIVE.name())))
when(msUserApiRestClient._retrieveUsers(institutionId, userId, userId, null, null, null, List.of(ACTIVE.name())))
.thenReturn(ResponseEntity.ok(List.of(userDataResponse)));

Product product = new Product();
Expand All @@ -129,7 +129,7 @@ void getInstitutionUserProducts_WithTwoMatch() {
it.pagopa.selfcare.user.generated.openapi.v1.dto.OnboardedProductResponse onboardedProductResponse2 = new it.pagopa.selfcare.user.generated.openapi.v1.dto.OnboardedProductResponse();
onboardedProductResponse.setProductId("id2");
userDataResponse.setProducts(List.of(onboardedProductResponse, onboardedProductResponse2));
when(msUserApiRestClient._usersUserIdInstitutionInstitutionIdGet(institutionId, userId, userId, null, null, null, List.of(ACTIVE.name())))
when(msUserApiRestClient._retrieveUsers(institutionId, userId, userId, null, null, null, List.of(ACTIVE.name())))
.thenReturn(ResponseEntity.ok(List.of(userDataResponse)));

Product product = new Product();
Expand Down Expand Up @@ -172,7 +172,7 @@ void getInstitutionProductUserV2EmptyList() throws Exception {
List<UserInstitutionResponse> userInstitutions = objectMapper.readValue(resourceStream, new TypeReference<>() {
});
userInstitutions.forEach(userInstitutionResponse -> userInstitutionResponse.setProducts(Collections.emptyList()));
Mockito.when(msUserApiRestClient._usersGet(institutionId, null, null, List.of(productId), null, null, List.of(ACTIVE.name()), userId))
Mockito.when(msUserApiRestClient._retrievePaginatedAndFilteredUser(institutionId, null, null, List.of(productId), null, null, List.of(ACTIVE.name()), userId))
.thenReturn(ResponseEntity.ok(userInstitutions));


Expand All @@ -195,7 +195,7 @@ void getInstitutionProductUserV2WithUuidInServiceType() throws Exception {
byte[] resourceStream = Files.readAllBytes(resource.getFile().toPath());
List<UserInstitutionResponse> userInstitutions = objectMapper.readValue(resourceStream, new TypeReference<>() {
});
Mockito.when(msUserApiRestClient._usersGet(institutionId, null, null, List.of(productId), null, null, List.of(ACTIVE.name()), userId))
Mockito.when(msUserApiRestClient._retrievePaginatedAndFilteredUser(institutionId, null, null, List.of(productId), null, null, List.of(ACTIVE.name()), userId))
.thenReturn(ResponseEntity.ok(userInstitutions));
when(productService.getProductRaw(productId)).thenReturn(dummyProduct(productId));

Expand Down Expand Up @@ -225,7 +225,7 @@ void getInstitutionProductUserV2WithoutUuidInServiceType() throws Exception {
byte[] resourceStream = Files.readAllBytes(resource.getFile().toPath());
List<UserInstitutionResponse> userInstitutions = objectMapper.readValue(resourceStream, new TypeReference<>() {
});
Mockito.when(msUserApiRestClient._usersGet(institutionId, null, null, List.of(productId), null, null, List.of(ACTIVE.name()), userId))
Mockito.when(msUserApiRestClient._retrievePaginatedAndFilteredUser(institutionId, null, null, List.of(productId), null, null, List.of(ACTIVE.name()), userId))
.thenReturn(ResponseEntity.ok(userInstitutions));

ClassPathResource userResource = new ClassPathResource("expectations/UserV2.json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
import it.pagopa.selfcare.external_api.client.MsOnboardingControllerApi;
import it.pagopa.selfcare.external_api.client.MsUserApiRestClient;
import it.pagopa.selfcare.external_api.exception.ResourceNotFoundException;
import it.pagopa.selfcare.external_api.mapper.OnboardingMapper;
import it.pagopa.selfcare.external_api.mapper.OnboardingMapperImpl;
import it.pagopa.selfcare.external_api.mapper.UserResourceMapper;
import it.pagopa.selfcare.external_api.model.institution.Institution;
import it.pagopa.selfcare.external_api.model.onboarding.InstitutionUpdate;
import it.pagopa.selfcare.external_api.model.onboarding.OnboardingData;
import it.pagopa.selfcare.external_api.model.onboarding.OnboardingUsersRequest;
Expand All @@ -21,7 +19,6 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.core.io.ClassPathResource;
Expand All @@ -32,7 +29,9 @@
import java.util.List;

import static it.pagopa.selfcare.onboarding.common.InstitutionType.*;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.when;

@ExtendWith({MockitoExtension.class})
class OnboardingServiceImplTest extends BaseServiceTestUtils {
Expand All @@ -54,6 +53,7 @@ class OnboardingServiceImplTest extends BaseServiceTestUtils {
@Spy
private OnboardingMapperImpl onboardingMapper;

@Override
@BeforeEach
public void setUp() {
super.setUp();
Expand Down Expand Up @@ -105,18 +105,6 @@ void onboardingUsers_noInstitutionFound() throws Exception {
"Institution not found for given value");
}

@Test
void onboardingUsers_noInstitutionFound2() throws Exception {
ClassPathResource inputResource = new ClassPathResource("expectations/OnboardingUsersRequest.json");
byte[] onboardingUsersRequestStream = Files.readAllBytes(inputResource.getFile().toPath());
OnboardingUsersRequest onboardingUsersRequest = objectMapper.readValue(onboardingUsersRequestStream, OnboardingUsersRequest.class);
when(institutionApiClient._getInstitutionsUsingGET(onboardingUsersRequest.getInstitutionTaxCode(), onboardingUsersRequest.getInstitutionSubunitCode(), null, null))
.thenReturn(ResponseEntity.ok(new InstitutionsResponse().institutions(Collections.emptyList())));
Assertions.assertThrows(ResourceNotFoundException.class,
() -> onboardingService.onboardingUsers(onboardingUsersRequest, "userName", "surname"),
"Institution not found for given value");
}


@Test
void onboardingUsers_happyPath() throws Exception {
Expand All @@ -130,10 +118,10 @@ void onboardingUsers_happyPath() throws Exception {
});
when(institutionApiClient._getInstitutionsUsingGET(onboardingUsersRequest.getInstitutionTaxCode(), onboardingUsersRequest.getInstitutionSubunitCode(), null, null))
.thenReturn(ResponseEntity.ok(new InstitutionsResponse().institutions(institutions)));
when(msUserApiRestClient._usersPost(any())).thenReturn(ResponseEntity.ok("userId"));
when(msUserApiRestClient._usersUserIdPost(any(), any())).thenReturn(ResponseEntity.ok().build());
when(msUserApiRestClient._createOrUpdateByFiscalCode(any())).thenReturn(ResponseEntity.ok("userId"));
when(msUserApiRestClient._createOrUpdateByUserId(any(), any())).thenReturn(ResponseEntity.ok().build());
List<RelationshipInfo> result = onboardingService.onboardingUsers(onboardingUsersRequest, "userName", "surname");

assert result.size() == 2;
assertEquals(result.size(), 2);
}
}
Loading

0 comments on commit 7bcecfd

Please sign in to comment.