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

merge to staging #40

Merged
merged 6 commits into from
Dec 19, 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
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ protected void deleteSession(Session session, List<DeletionWorkflowError> workfl

void performSessionDeletion(Session session, List<DeletionWorkflowError> workflowErrors) {

deleteRocketChatGroup(session.getGroupId(), workflowErrors);
deleteRocketChatGroup(session.getFeedbackGroupId(), workflowErrors);
deleteSessionData(session, workflowErrors);
deleteSession(session, workflowErrors);
deleteRocketChatGroup(session.getGroupId(), workflowErrors);
deleteRocketChatGroup(session.getFeedbackGroupId(), workflowErrors);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.persistence.NonUniqueResultException;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/** Service to trigger deletion of inactive sessions and asker accounts. */
@Service
@RequiredArgsConstructor
@Slf4j
public class DeleteInactiveSessionsAndUserService {

private final @NonNull UserRepository userRepository;
Expand Down Expand Up @@ -78,14 +81,20 @@ private List<DeletionWorkflowError> performDeletionWorkflow(

List<DeletionWorkflowError> workflowErrors = new ArrayList<>();

Optional<User> user =
userRepository.findByRcUserIdAndDeleteDateIsNull(userInactiveGroupEntry.getKey());
user.ifPresentOrElse(
u -> workflowErrors.addAll(deleteInactiveGroupsOrUser(userInactiveGroupEntry, u)),
() ->
workflowErrors.addAll(
performUserSessionDeletionForNonExistingUser(userInactiveGroupEntry.getValue())));

try {
Optional<User> user =
userRepository.findByRcUserIdAndDeleteDateIsNull(userInactiveGroupEntry.getKey());
user.ifPresentOrElse(
u -> workflowErrors.addAll(deleteInactiveGroupsOrUser(userInactiveGroupEntry, u)),
() ->
workflowErrors.addAll(
performUserSessionDeletionForNonExistingUser(userInactiveGroupEntry.getValue())));
} catch (NonUniqueResultException ex) {
log.error(
"Non unique result for findByRcUserIdAndDeleteDateIsNull found. RcUserId:",
userInactiveGroupEntry.getKey());
return workflowErrors;
}
return workflowErrors;
}

Expand Down Expand Up @@ -138,7 +147,11 @@ private Collection<? extends DeletionWorkflowError> performUserSessionDeletionFo
String rcGroupId) {
List<DeletionWorkflowError> workflowErrors = new ArrayList<>();
Optional<Session> session = sessionRepository.findByGroupId(rcGroupId);
session.ifPresent(s -> workflowErrors.addAll(deleteSessionService.performSessionDeletion(s)));
if (session.isPresent()) {
workflowErrors.addAll(deleteSessionService.performSessionDeletion(session.get()));
} else {
workflowErrors.addAll(deleteSessionService.performRocketchatSessionDeletion(rcGroupId));
}
return workflowErrors;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package de.caritas.cob.userservice.api.workflow.delete.action.asker;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.inOrder;

import de.caritas.cob.userservice.api.adapters.rocketchat.RocketChatService;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatDeleteGroupException;
import de.caritas.cob.userservice.api.model.Session;
import de.caritas.cob.userservice.api.port.out.SessionDataRepository;
import de.caritas.cob.userservice.api.port.out.SessionRepository;
import java.util.ArrayList;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InOrder;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class DeleteRoomsAndSessionActionTest {

@Mock private SessionRepository sessionRepository;

@Mock private SessionDataRepository sessionDataRepository;

@Mock private RocketChatService rocketChatService;

@InjectMocks private DeleteSingleRoomAndSessionAction deleteRoomsAndSessionAction;

@Test
void performSessionDeletion_Should_ExecuteMethodsInCorrectOrder()
throws RocketChatDeleteGroupException {
// given
Session session = new Session();
session.setGroupId("group-id");
session.setFeedbackGroupId("feedback-group-id");

// when
deleteRoomsAndSessionAction.performSessionDeletion(session, new ArrayList<>());

// then
InOrder inOrder = inOrder(sessionDataRepository, sessionRepository, rocketChatService);

inOrder.verify(sessionDataRepository).findBySessionId(any());
inOrder.verify(sessionDataRepository).deleteAll(anyList());
inOrder.verify(sessionRepository).delete(eq(session));
inOrder.verify(rocketChatService).deleteGroupAsTechnicalUser(eq("group-id"));
inOrder.verify(rocketChatService).deleteGroupAsTechnicalUser(eq("feedback-group-id"));

inOrder.verifyNoMoreInteractions();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import static de.caritas.cob.userservice.api.helper.CustomLocalDateTime.nowInUtc;
import static de.caritas.cob.userservice.api.workflow.delete.model.DeletionSourceType.ASKER;
import static de.caritas.cob.userservice.api.workflow.delete.model.DeletionTargetType.ALL;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import de.caritas.cob.userservice.api.model.Session;
import de.caritas.cob.userservice.api.model.User;
import de.caritas.cob.userservice.api.port.out.SessionRepository;
Expand All @@ -20,15 +25,20 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.persistence.NonUniqueResultException;
import lombok.extern.slf4j.Slf4j;
import org.jeasy.random.EasyRandom;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.slf4j.LoggerFactory;

@ExtendWith(MockitoExtension.class)
@Slf4j
class DeleteInactiveSessionsAndUserServiceTest {

@InjectMocks private DeleteInactiveSessionsAndUserService deleteInactiveSessionsAndUserService;
Expand All @@ -41,6 +51,16 @@ class DeleteInactiveSessionsAndUserServiceTest {
@Mock private DeleteSessionService deleteSessionService;
@Mock private InactivePrivateGroupsProvider inactivePrivateGroupsProvider;

private ListAppender<ILoggingEvent> listAppender;

@BeforeEach
void setUp() {
Logger logger = (Logger) LoggerFactory.getLogger(DeleteInactiveSessionsAndUserService.class);
listAppender = new ListAppender<>();
listAppender.start();
logger.addAppender(listAppender);
}

@Test
void deleteInactiveSessionsAndUsers_Should_SendWorkflowErrorsMail_When_userNotFoundReason() {
// given
Expand Down Expand Up @@ -217,4 +237,63 @@ void deleteInactiveSessionsAndUsers_Should_AttemptToDeleteUserSessions_WhenUserC
// then
deleteSessionService.performSessionDeletion(session1);
}

@Test
void
deleteInactiveSessionsAndUsers_Should_deleteSessionFromRocketChat_WhenSessionDoesNotExistOnMariaDB() {
// given
EasyRandom easyRandom = new EasyRandom();
User user = easyRandom.nextObject(User.class);
Session session1 = easyRandom.nextObject(Session.class);
Map<String, List<String>> userWithInactiveGroupsMap =
new HashMap<>() {
{
put(user.getUserId(), Collections.singletonList(session1.getGroupId()));
}
};
when(inactivePrivateGroupsProvider.retrieveUserWithInactiveGroupsMap())
.thenReturn(userWithInactiveGroupsMap);
when(userRepository.findByRcUserIdAndDeleteDateIsNull(anyString()))
.thenReturn(Optional.empty());
when(sessionRepository.findByGroupId(anyString())).thenReturn(Optional.empty());

// when
deleteInactiveSessionsAndUserService.deleteInactiveSessionsAndUsers();

// then
verify(deleteSessionService, times(1)).performRocketchatSessionDeletion(anyString());
}

@Test
void
deleteInactiveSessionsAndUsers_Should_catchAndLogNonUniqueResultException_WhenThrownByFindByRcUserIdAndDeleteDateIsNull() {
// given
EasyRandom easyRandom = new EasyRandom();
User user = easyRandom.nextObject(User.class);
Session session1 = easyRandom.nextObject(Session.class);
Map<String, List<String>> userWithInactiveGroupsMap =
new HashMap<>() {
{
put(user.getUserId(), Collections.singletonList(session1.getGroupId()));
}
};
when(inactivePrivateGroupsProvider.retrieveUserWithInactiveGroupsMap())
.thenReturn(userWithInactiveGroupsMap);
when(userRepository.findByRcUserIdAndDeleteDateIsNull(anyString()))
.thenThrow(new NonUniqueResultException());

// when
deleteInactiveSessionsAndUserService.deleteInactiveSessionsAndUsers();

// then
List<ILoggingEvent> logEvents = listAppender.list;
assertTrue(
logEvents.stream()
.anyMatch(
event ->
event
.getFormattedMessage()
.contains(
"Non unique result for findByRcUserIdAndDeleteDateIsNull found")));
}
}
Loading