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

fix: delete rooms even if user is already deleted from MariaDB #28

Merged
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
@@ -1,8 +1,5 @@
package de.caritas.cob.userservice.api.workflow.delete.service;

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.apache.commons.collections4.CollectionUtils.isNotEmpty;

import de.caritas.cob.userservice.api.model.Session;
Expand Down Expand Up @@ -86,14 +83,8 @@ private List<DeletionWorkflowError> performDeletionWorkflow(
user.ifPresentOrElse(
u -> workflowErrors.addAll(deleteInactiveGroupsOrUser(userInactiveGroupEntry, u)),
() ->
workflowErrors.add(
DeletionWorkflowError.builder()
.deletionSourceType(ASKER)
.deletionTargetType(ALL)
.identifier(userInactiveGroupEntry.getKey())
.reason(USER_NOT_FOUND_REASON)
.timestamp(nowInUtc())
.build()));
workflowErrors.addAll(
performUserSessionDeletionForNonExistingUser(userInactiveGroupEntry.getValue())));

return workflowErrors;
}
Expand All @@ -102,11 +93,14 @@ private List<DeletionWorkflowError> deleteInactiveGroupsOrUser(
Entry<String, List<String>> userInactiveGroupEntry, User user) {

List<Session> userSessionList = sessionRepository.findByUser(user);

if (allSessionsOfUserAreInactive(userInactiveGroupEntry, userSessionList)) {
return deleteUserAccountService.performUserDeletion(user);
}
return perfomUserSessionDeletion(userInactiveGroupEntry, userSessionList);
}

private List<DeletionWorkflowError> perfomUserSessionDeletion(
Entry<String, List<String>> userInactiveGroupEntry, List<Session> userSessionList) {
return userInactiveGroupEntry.getValue().stream()
.map(rcGroupId -> performSessionDeletion(rcGroupId, userSessionList))
.flatMap(Collection::stream)
Expand All @@ -120,13 +114,26 @@ private boolean allSessionsOfUserAreInactive(

private List<DeletionWorkflowError> performSessionDeletion(
String rcGroupId, List<Session> userSessionList) {

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

Optional<Session> session = findSessionInUserSessionList(rcGroupId, userSessionList);

session.ifPresent(s -> workflowErrors.addAll(deleteSessionService.performSessionDeletion(s)));
return workflowErrors;
}

private List<DeletionWorkflowError> performUserSessionDeletionForNonExistingUser(
List<String> rcGroupIds) {
List<DeletionWorkflowError> workflowErrors = new ArrayList<>();
rcGroupIds.forEach(
rcGroupId ->
workflowErrors.addAll(performUserSessionDeletionForNonExistingUser(rcGroupId)));
return workflowErrors;
}

private Collection<? extends DeletionWorkflowError> performUserSessionDeletionForNonExistingUser(
String rcGroupId) {
List<DeletionWorkflowError> workflowErrors = new ArrayList<>();
Optional<Session> session = sessionRepository.findByGroupId(rcGroupId);
session.ifPresent(s -> workflowErrors.addAll(deleteSessionService.performSessionDeletion(s)));
return workflowErrors;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class DeleteInactiveSessionsAndUserServiceTest {

@Test
void deleteInactiveSessionsAndUsers_Should_SendWorkflowErrorsMail_When_userNotFoundReason() {

// given
EasyRandom easyRandom = new EasyRandom();
User user = easyRandom.nextObject(User.class);
Session session = easyRandom.nextObject(Session.class);
Expand All @@ -62,8 +62,10 @@ void deleteInactiveSessionsAndUsers_Should_SendWorkflowErrorsMail_When_userNotFo
when(deleteUserAccountService.performUserDeletion(user))
.thenReturn(Collections.singletonList(deletionWorkflowError));

// when
deleteInactiveSessionsAndUserService.deleteInactiveSessionsAndUsers();

// then
verify(workflowErrorLogService, Mockito.times(1)).logWorkflowErrors(Collections.emptyList());
verify(workflowErrorMailService, Mockito.times(1))
.buildAndSendErrorMail(argThat(list -> !list.isEmpty()));
Expand All @@ -72,7 +74,7 @@ void deleteInactiveSessionsAndUsers_Should_SendWorkflowErrorsMail_When_userNotFo
@Test
void
deleteInactiveSessionsAndUsers_Should_DeleteEntireUserAccount_WhenUserHasOnlyInactiveSessions() {

// given
EasyRandom easyRandom = new EasyRandom();
User user = easyRandom.nextObject(User.class);
Session session1 = easyRandom.nextObject(Session.class);
Expand All @@ -89,15 +91,17 @@ void deleteInactiveSessionsAndUsers_Should_SendWorkflowErrorsMail_When_userNotFo
.thenReturn(Optional.of(user));
when(sessionRepository.findByUser(user)).thenReturn(Arrays.asList(session1, session2));

// when
deleteInactiveSessionsAndUserService.deleteInactiveSessionsAndUsers();

// then
verify(deleteUserAccountService, Mockito.times(1)).performUserDeletion(user);
}

@Test
void
deleteInactiveSessionsAndUsers_Should_DeleteSingleSession_WhenUserHasActiveAndInactiveSessions() {

// given
EasyRandom easyRandom = new EasyRandom();
User user = easyRandom.nextObject(User.class);
Session session1 = easyRandom.nextObject(Session.class);
Expand All @@ -114,15 +118,17 @@ void deleteInactiveSessionsAndUsers_Should_SendWorkflowErrorsMail_When_userNotFo
.thenReturn(Optional.of(user));
when(sessionRepository.findByUser(user)).thenReturn(Arrays.asList(session1, session2));

// when
deleteInactiveSessionsAndUserService.deleteInactiveSessionsAndUsers();

// then
verify(deleteSessionService, Mockito.times(1)).performSessionDeletion(session1);
}

@Test
void
deleteInactiveSessionsAndUsers_Should_logWorkflowErrorMail_WhenUserHasActiveAndInactiveSessionsAndHasErrors() {

// given
EasyRandom easyRandom = new EasyRandom();
User user = easyRandom.nextObject(User.class);
Session session1 = easyRandom.nextObject(Session.class);
Expand All @@ -149,8 +155,10 @@ void deleteInactiveSessionsAndUsers_Should_SendWorkflowErrorsMail_When_userNotFo
when(deleteSessionService.performSessionDeletion(session1))
.thenReturn(Collections.singletonList(deletionWorkflowError));

// when
deleteInactiveSessionsAndUserService.deleteInactiveSessionsAndUsers();

// then
verify(workflowErrorLogService, Mockito.times(1))
.logWorkflowErrors(argThat(list -> !list.isEmpty()));
verify(workflowErrorMailService, Mockito.times(1))
Expand All @@ -160,7 +168,7 @@ void deleteInactiveSessionsAndUsers_Should_SendWorkflowErrorsMail_When_userNotFo
@Test
void
deleteInactiveSessionsAndUsers_Should_notLogError_WhenSessionCouldNotBeFound_BecauseItMayHaveBeenDeletedByPreviousWorkflowRun() {

// given
EasyRandom easyRandom = new EasyRandom();
User user = easyRandom.nextObject(User.class);
Session session1 = easyRandom.nextObject(Session.class);
Expand All @@ -178,15 +186,17 @@ void deleteInactiveSessionsAndUsers_Should_SendWorkflowErrorsMail_When_userNotFo
.thenReturn(Optional.of(user));
when(sessionRepository.findByUser(user)).thenReturn(Arrays.asList(session2, session3));

// when
deleteInactiveSessionsAndUserService.deleteInactiveSessionsAndUsers();

// then
verify(workflowErrorLogService, Mockito.never()).logWorkflowErrors(Mockito.anyList());
verify(workflowErrorMailService, Mockito.never()).buildAndSendErrorMail(Mockito.anyList());
}

@Test
void deleteInactiveSessionsAndUsers_Should_SendWorkflowErrorMail_WhenUserCouldNotBeFound() {

void deleteInactiveSessionsAndUsers_Should_AttemptToDeleteUserSessions_WhenUserCouldNotBeFound() {
// given
EasyRandom easyRandom = new EasyRandom();
User user = easyRandom.nextObject(User.class);
Session session1 = easyRandom.nextObject(Session.class);
Expand All @@ -201,10 +211,10 @@ void deleteInactiveSessionsAndUsers_Should_SendWorkflowErrorMail_WhenUserCouldNo
when(userRepository.findByRcUserIdAndDeleteDateIsNull(anyString()))
.thenReturn(Optional.empty());

// when
deleteInactiveSessionsAndUserService.deleteInactiveSessionsAndUsers();

verify(workflowErrorLogService, Mockito.times(1)).logWorkflowErrors(Collections.emptyList());
verify(workflowErrorMailService, Mockito.times(1))
.buildAndSendErrorMail(argThat(list -> !list.isEmpty()));
// then
deleteSessionService.performSessionDeletion(session1);
}
}
Loading