Skip to content

Commit

Permalink
Merge pull request #31 from pagopa/feature/SELC-3902
Browse files Browse the repository at this point in the history
[SELC-3902] Feat: added API to delete userInstitution product
  • Loading branch information
flaminiaScarciofolo authored Jan 29, 2024
2 parents d406577 + 030ac06 commit 5e9aad8
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 4 deletions.
6 changes: 3 additions & 3 deletions apps/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-azure-storage</artifactId>
<version>0.1.4</version>
<version>0.1.6</version>
</dependency>
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-common</artifactId>
<version>0.1.4</version>
<version>0.1.6</version>
</dependency>
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-product</artifactId>
<version>0.1.4</version>
<version>0.1.6</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,23 @@ public Uni<UserResponse> getUserInfo(@PathParam(value = "id") String userId,
return userService.retrievePerson(userId, productId, institutionId)
.map(user -> userMapper.toUserResponse(user, institutionId));
}

/**
* The deleteProducts function is used to delete logically the association institution and product.
*
* @param userId String
* @param institutionId String
* @param productId String
*
* @return A uni&lt;void&gt;
*/
@Operation(summary = "Delete logically the association institution and product")
@DELETE
@Path(value = "/{userId}/institutions/{institutionId}/products/{productId}")
public Uni<Void> deleteProducts(@PathParam(value = "userId") String userId,
@PathParam(value = "institutionId") String institutionId,
@PathParam(value = "productId") String productId) {
return userService.deleteUserInstitutionProduct(userId, institutionId, productId);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ public interface UserInstitutionService {
Multi<UserInstitution> findAllWithFilter(Map<String, Object> queryParameter);

Uni<UserInstitution> retrieveFirstFilteredUserInstitution(Map<String, Object> queryParameter);

Uni<Long> deleteUserInstitutionProduct(String userId, String institutionId, String productId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import it.pagopa.selfcare.user.controller.response.UserInstitutionResponse;
import it.pagopa.selfcare.user.entity.OnboardedProduct;
import it.pagopa.selfcare.user.entity.UserInstitution;
import it.pagopa.selfcare.user.entity.filter.OnboardedProductFilter;
import it.pagopa.selfcare.user.entity.filter.UserInstitutionFilter;
import it.pagopa.selfcare.user.mapper.UserInstitutionMapper;
import it.pagopa.selfcare.user.util.QueryUtils;
import it.pagopa.selfcare.user.util.UserUtils;
import jakarta.enterprise.context.ApplicationScoped;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -33,6 +36,7 @@ public class UserInstitutionServiceDefault implements UserInstitutionService {

private final UserInstitutionMapper userInstitutionMapper;
private final QueryUtils queryUtils;
private final UserUtils userUtils;

@Override
public Uni<UserInstitutionResponse> findById(String id) {
Expand Down Expand Up @@ -64,6 +68,14 @@ public Uni<UserInstitution> retrieveFirstFilteredUserInstitution(Map<String, Obj
return runUserInstitutionFindQuery(query, null).firstResult();
}

@Override
public Uni<Long> deleteUserInstitutionProduct(String userId, String institutionId, String productId) {
OnboardedProductFilter onboardedProductFilter = OnboardedProductFilter.builder().productId(productId).build();
UserInstitutionFilter userInstitutionFilter = UserInstitutionFilter.builder().userId(userId).institutionId(institutionId).build();
Map<String, Object> filterMap = userUtils.retrieveMapForFilter(onboardedProductFilter.constructMap(), userInstitutionFilter.constructMap());
return updateUserStatusDao(filterMap, OnboardedProductState.DELETED);
}

@Override
public Multi<UserInstitution> findAllWithFilter(Map<String, Object> queryParameter) {
Document query = queryUtils.buildQueryDocument(queryParameter, USER_INSTITUTION_COLLECTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ Multi<UserInstitutionResponse> findAllUserInstitutions(String institutionId,
List<String> states,
List<String> products,
List<String> productRoles);

Uni<Void> deleteUserInstitutionProduct(String userId, String institutionId, String productId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import it.pagopa.selfcare.user.constant.OnboardedProductState;
import it.pagopa.selfcare.user.controller.response.UserInstitutionResponse;
import it.pagopa.selfcare.user.controller.response.UserProductResponse;
import it.pagopa.selfcare.user.entity.UserInstitution;
Expand All @@ -25,6 +26,7 @@
import java.util.*;

import static it.pagopa.selfcare.user.constant.CustomError.USER_NOT_FOUND_ERROR;
import static it.pagopa.selfcare.user.constant.CustomError.USER_TO_UPDATE_NOT_FOUND;
import static it.pagopa.selfcare.user.util.GeneralUtils.formatQueryParameterList;

@RequiredArgsConstructor
Expand Down Expand Up @@ -97,6 +99,17 @@ public Multi<UserInstitutionResponse> findAllUserInstitutions(String institution
return userInstitutions.onItem().transform(userInstitutionMapper::toResponse);
}

@Override
public Uni<Void> deleteUserInstitutionProduct(String userId, String institutionId, String productId) {
return userInstitutionService.deleteUserInstitutionProduct(userId, institutionId, productId)
.onItem().transformToUni(aLong -> {
if (aLong < 1) {
return Uni.createFrom().failure(new ResourceNotFoundException(USER_TO_UPDATE_NOT_FOUND.getMessage()));
}
return Uni.createFrom().nullItem();
});
}

private Map<String, Object> buildQueryParams(String userId, String productId, String institutionId) {
OnboardedProductFilter onboardedProductFilter = OnboardedProductFilter.builder()
.productId(productId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@ApplicationScoped
@RequiredArgsConstructor
@Slf4j
public class UserUtils {


@SafeVarargs
public final Map<String, Object> retrieveMapForFilter(Map<String, Object>... maps) {
Map<String, Object> map = new HashMap<>();
Arrays.stream(maps).forEach(map::putAll);
return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ void findAllWithFilter() {
assertEquals(1, actual.size());
}

@Test
void deleteUserInstitutionProduct(){
final String userId = "userId";
String institutionId = "institutionId";
PanacheMock.mock(UserInstitution.class);
ReactivePanacheUpdate update = Mockito.mock(ReactivePanacheUpdate.class);
when(UserInstitution.update(any(Document.class)))
.thenReturn(update);
when(update.where(any())).thenReturn(Uni.createFrom().item(1L));
UniAssertSubscriber<Long> subscriber = userInstitutionService.deleteUserInstitutionProduct(userId, institutionId, "productID")
.subscribe().withSubscriber(UniAssertSubscriber.create());
subscriber.assertCompleted().assertItem(1L);
}

private UserInstitution createDummyUserInstitution() {
UserInstitution userInstitution = new UserInstitution();
userInstitution.setId(ObjectId.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.helpers.test.AssertSubscriber;
import io.smallrye.mutiny.helpers.test.UniAssertSubscriber;
import io.smallrye.mutiny.subscription.UniSubscriber;
import it.pagopa.selfcare.user.controller.response.UserInstitutionResponse;
import it.pagopa.selfcare.user.controller.response.UserProductResponse;
import it.pagopa.selfcare.user.entity.OnboardedProduct;
Expand All @@ -35,9 +36,11 @@
import java.util.Map;
import java.util.UUID;

import static it.pagopa.selfcare.user.constant.CustomError.USER_TO_UPDATE_NOT_FOUND;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

@QuarkusTest
Expand Down Expand Up @@ -177,4 +180,28 @@ void retrieveUsersTest() {
assertEquals(1, actual.size());
assertEquals(userInstitution.getUserId(), actual.get(0).getUserId());
}

@Test
void deleteUserInstitutionProductFound(){
when(userInstitutionService.deleteUserInstitutionProduct("userId", "institutionId", "productId")).thenReturn(Uni.createFrom().item(1L));
UniAssertSubscriber<Void> subscriber = userService
.deleteUserInstitutionProduct("userId", "institutionId", "productId")
.subscribe()
.withSubscriber(UniAssertSubscriber.create());

subscriber.assertCompleted();


}

@Test
void deleteUserInstitutionProductNotFound(){
when(userInstitutionService.deleteUserInstitutionProduct("userId", "institutionId", "productId")).thenReturn(Uni.createFrom().item(0L));
UniAssertSubscriber<Void> subscriber = userService
.deleteUserInstitutionProduct("userId", "institutionId", "productId")
.subscribe()
.withSubscriber(UniAssertSubscriber.create());

subscriber.assertFailedWith(ResourceNotFoundException.class, USER_TO_UPDATE_NOT_FOUND.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import io.quarkus.test.security.TestSecurity;
import io.restassured.http.ContentType;
import io.smallrye.mutiny.Uni;
import it.pagopa.selfcare.user.exception.InvalidRequestException;
import it.pagopa.selfcare.user.exception.ResourceNotFoundException;
import it.pagopa.selfcare.user.service.UserService;
import org.apache.http.HttpStatus;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfLocalDate;
Expand Down Expand Up @@ -133,4 +135,62 @@ void testGetUserInfoFails() {
.statusCode(404);
}

/**
* Method under test:
* {@link UserController#deleteProducts(String, String, String)}
*/
@Test
@TestSecurity(user = "userJwt")
void deleteDeleteProductsErrorTest() {
String PATH_USER_ID = "userId";
String PATH_INSTITUTION_ID = "institutionId";
String PATH_PRODUCT_ID = "productId";
String PATH_DELETE_PRODUCT = "{userId}/institutions/{institutionId}/products/{productId}";

var user = "user1";
var institution = "institution1";
var product = "product1";
Mockito.when(userService.deleteUserInstitutionProduct("user1","institution1", "product1"))
.thenThrow(InvalidRequestException.class);

given()
.when()
.pathParam(PATH_USER_ID, user)
.pathParam(PATH_INSTITUTION_ID, institution)
.pathParam(PATH_PRODUCT_ID, product)
.delete(PATH_DELETE_PRODUCT)
.then()
.statusCode(HttpStatus.SC_BAD_REQUEST);
}

/**
* Method under test:
* {@link UserController#deleteProducts(String, String, String)}
*/
@Test
@TestSecurity(user = "userJwt")
void deleteDeleteProductsOKTest() {

String PATH_USER_ID = "userId";
String PATH_INSTITUTION_ID = "institutionId";
String PATH_PRODUCT_ID = "productId";
String PATH_DELETE_PRODUCT = "{userId}/institutions/{institutionId}/products/{productId}";

var user = "user123";
var institution = "institution123";
var product = "prod-pagopa";

Mockito.when(userService.deleteUserInstitutionProduct("user123", "institution123", "prod-pagopa"))
.thenReturn(Uni.createFrom().voidItem());

given()
.when()
.pathParam(PATH_USER_ID, user)
.pathParam(PATH_INSTITUTION_ID, institution)
.pathParam(PATH_PRODUCT_ID, product)
.delete(PATH_DELETE_PRODUCT)
.then()
.statusCode(HttpStatus.SC_NO_CONTENT);
}

}

0 comments on commit 5e9aad8

Please sign in to comment.