Skip to content

Commit

Permalink
[SELC-5225] bugfix: update UserInfo more reliable avoid error when pr…
Browse files Browse the repository at this point in the history
…oducts is null (#164)
  • Loading branch information
manuraf authored Jul 3, 2024
1 parent d0be865 commit 5a92bc8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import java.util.*;

import static java.util.function.Predicate.not;


@Slf4j
@RequiredArgsConstructor
Expand All @@ -26,13 +28,13 @@ public class UserInstitutionRepository {
private final UserMapper userMapper;

public Uni<Void> updateUser(UserInstitution userInstitution) {
OnboardedProductState state = retrieveStatusForGivenInstitution(userInstitution.getProducts());
Optional<OnboardedProductState> optState = retrieveStatusForGivenInstitution(userInstitution.getProducts());
return UserInfo.findByIdOptional(userInstitution.getUserId())
.onItem().transformToUni(opt -> opt.map(entityBase -> {
if (VALID_PRODUCT_STATE.contains(state)) {
if (optState.isPresent() && VALID_PRODUCT_STATE.contains(optState.get())) {
Optional<PartyRole> optRole = retrieveRoleForGivenInstitution(userInstitution.getProducts());
return optRole.isPresent()
? updateOrCreateNewUserInfo(opt.get(), userInstitution, optRole.get(), state)
? updateOrCreateNewUserInfo(opt.get(), userInstitution, optRole.get(), optState.get())
: Uni.createFrom().voidItem();
} else {
return deleteInstitutionOrAllUserInfo(opt.get(), userInstitution);
Expand Down Expand Up @@ -135,11 +137,13 @@ private Optional<PartyRole> retrieveRoleForGivenInstitution(List<OnboardedProduc

}

private OnboardedProductState retrieveStatusForGivenInstitution(List<OnboardedProduct> products) {
List<OnboardedProductState> list = products.stream()
.map(OnboardedProduct::getStatus)
.toList();
return Collections.min(list);
private Optional<OnboardedProductState> retrieveStatusForGivenInstitution(List<OnboardedProduct> products) {
return Optional.ofNullable(products)
.map(productsList -> productsList.stream()
.map(OnboardedProduct::getStatus)
.toList())
.filter(not(List::isEmpty))
.map(Collections::min);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,29 @@ void initOrderStreamWithNotFoundValidState(UniAsserter asserter){
});
}

@Test
@RunOnVertxContext
void initOrderStreamWithNotFoundState(UniAsserter asserter){
UserInstitution userInstitution = constructUserInstitution("productId");
userInstitution.setProducts(null);

PanacheMock.mock(UserInfo.class);
UserInfo userInfo = retrieveUserInfo();
List<UserInstitutionRole> userInstitutionRoles = new ArrayList<>(userInfo.getInstitutions());
userInstitutionRoles.removeIf(userInstitutionRole -> userInstitutionRole.getInstitutionId().equalsIgnoreCase("institutionId3"));
userInfo.setInstitutions(userInstitutionRoles);
when(UserInfo.findByIdOptional(any()))
.thenReturn(Uni.createFrom().item(Optional.of(userInfo)));
mockPersistUserInfo(asserter);

userInstitutionRepository.updateUser(userInstitution)
.subscribe().withSubscriber(UniAssertSubscriber.create()).assertCompleted();

asserter.execute(() -> {
PanacheMock.verify(UserInfo.class, Mockito.atLeastOnce()).deleteById(userInfo.getUserId());
});
}

@Test
@RunOnVertxContext
void initOrderStreamWithNotFoundValidStateAndInstitutionId(UniAsserter asserter){
Expand Down

0 comments on commit 5a92bc8

Please sign in to comment.