From 959d352c1d20c79ce57ccdbc40495a50dbcf2ed8 Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Thu, 1 Aug 2024 13:38:17 +0200 Subject: [PATCH] fix: delete rooms even if user is already deleted from MariaDB --- .../DeleteInactiveSessionsAndUserService.java | 37 +++++++++++-------- ...eteInactiveSessionsAndUserServiceTest.java | 30 ++++++++++----- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/src/main/java/de/caritas/cob/userservice/api/workflow/delete/service/DeleteInactiveSessionsAndUserService.java b/src/main/java/de/caritas/cob/userservice/api/workflow/delete/service/DeleteInactiveSessionsAndUserService.java index 23778d8d0..e390a3087 100644 --- a/src/main/java/de/caritas/cob/userservice/api/workflow/delete/service/DeleteInactiveSessionsAndUserService.java +++ b/src/main/java/de/caritas/cob/userservice/api/workflow/delete/service/DeleteInactiveSessionsAndUserService.java @@ -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; @@ -86,14 +83,8 @@ private List 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; } @@ -102,11 +93,14 @@ private List deleteInactiveGroupsOrUser( Entry> userInactiveGroupEntry, User user) { List userSessionList = sessionRepository.findByUser(user); - if (allSessionsOfUserAreInactive(userInactiveGroupEntry, userSessionList)) { return deleteUserAccountService.performUserDeletion(user); } + return perfomUserSessionDeletion(userInactiveGroupEntry, userSessionList); + } + private List perfomUserSessionDeletion( + Entry> userInactiveGroupEntry, List userSessionList) { return userInactiveGroupEntry.getValue().stream() .map(rcGroupId -> performSessionDeletion(rcGroupId, userSessionList)) .flatMap(Collection::stream) @@ -120,13 +114,26 @@ private boolean allSessionsOfUserAreInactive( private List performSessionDeletion( String rcGroupId, List userSessionList) { - List workflowErrors = new ArrayList<>(); - Optional session = findSessionInUserSessionList(rcGroupId, userSessionList); - session.ifPresent(s -> workflowErrors.addAll(deleteSessionService.performSessionDeletion(s))); + return workflowErrors; + } + private List performUserSessionDeletionForNonExistingUser( + List rcGroupIds) { + List workflowErrors = new ArrayList<>(); + rcGroupIds.forEach( + rcGroupId -> + workflowErrors.addAll(performUserSessionDeletionForNonExistingUser(rcGroupId))); + return workflowErrors; + } + + private Collection performUserSessionDeletionForNonExistingUser( + String rcGroupId) { + List workflowErrors = new ArrayList<>(); + Optional session = sessionRepository.findByGroupId(rcGroupId); + session.ifPresent(s -> workflowErrors.addAll(deleteSessionService.performSessionDeletion(s))); return workflowErrors; } diff --git a/src/test/java/de/caritas/cob/userservice/api/workflow/delete/service/DeleteInactiveSessionsAndUserServiceTest.java b/src/test/java/de/caritas/cob/userservice/api/workflow/delete/service/DeleteInactiveSessionsAndUserServiceTest.java index 71cc33fd1..ae6aa561f 100644 --- a/src/test/java/de/caritas/cob/userservice/api/workflow/delete/service/DeleteInactiveSessionsAndUserServiceTest.java +++ b/src/test/java/de/caritas/cob/userservice/api/workflow/delete/service/DeleteInactiveSessionsAndUserServiceTest.java @@ -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); @@ -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())); @@ -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); @@ -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); @@ -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); @@ -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)) @@ -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); @@ -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); @@ -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); } }