diff --git a/src/main/java/de/caritas/cob/userservice/api/adapters/rocketchat/RocketChatService.java b/src/main/java/de/caritas/cob/userservice/api/adapters/rocketchat/RocketChatService.java index ee3d590b4..1bd8d764d 100644 --- a/src/main/java/de/caritas/cob/userservice/api/adapters/rocketchat/RocketChatService.java +++ b/src/main/java/de/caritas/cob/userservice/api/adapters/rocketchat/RocketChatService.java @@ -135,6 +135,7 @@ public class RocketChatService implements MessageClient { "Could not get users list from Rocket.Chat"; private static final String USER_LIST_GET_FIELD_SELECTION = "{\"_id\":1}"; private static final Integer PAGE_SIZE = 100; + private static final String ERROR_ROOM_NOT_FOUND = "error-room-not-found"; private final LocalDateTime localDateTime1900 = LocalDateTime.of(1900, 1, 1, 0, 0); private final LocalDateTime localDateTimeFuture = nowInUtc().plusYears(1L); @@ -652,13 +653,7 @@ public void removeUserFromGroup(String rcUserId, String rcGroupId) GroupResponseDTO response; try { - RocketChatCredentials technicalUser = rcCredentialHelper.getTechnicalUser(); - var header = getStandardHttpHeaders(technicalUser); - var body = new GroupRemoveUserBodyDTO(rcUserId, rcGroupId); - HttpEntity request = new HttpEntity<>(body, header); - - var url = rocketChatConfig.getApiUrl(ENDPOINT_GROUP_KICK); - response = restTemplate.postForObject(url, request, GroupResponseDTO.class); + response = tryRemoveUserFromGroup(rcUserId, rcGroupId); } catch (Exception ex) { log.error( @@ -677,6 +672,19 @@ public void removeUserFromGroup(String rcUserId, String rcGroupId) } } + private GroupResponseDTO tryRemoveUserFromGroup(String rcUserId, String rcGroupId) + throws RocketChatUserNotInitializedException { + GroupResponseDTO response; + RocketChatCredentials technicalUser = rcCredentialHelper.getTechnicalUser(); + var header = getStandardHttpHeaders(technicalUser); + var body = new GroupRemoveUserBodyDTO(rcUserId, rcGroupId); + HttpEntity request = new HttpEntity<>(body, header); + + var url = rocketChatConfig.getApiUrl(ENDPOINT_GROUP_KICK); + response = restTemplate.postForObject(url, request, GroupResponseDTO.class); + return response; + } + public boolean removeUserFromSession(String chatUserId, String chatId) { try { addTechnicalUserToGroup(chatId); @@ -1340,4 +1348,31 @@ public boolean saveRoomSettings(String chatId, boolean encrypted) { return false; } } + + public void removeUserFromGroupIgnoreGroupNotFound(String rcUserId, String rcGroupId) + throws RocketChatRemoveUserFromGroupException { + { + GroupResponseDTO response; + try { + response = tryRemoveUserFromGroup(rcUserId, rcGroupId); + } catch (Exception ex) { + if (ex.getMessage().contains(ERROR_ROOM_NOT_FOUND)) { + return; + } + log.error( + "Rocket.Chat Error: Could not remove user {} from Rocket.Chat group with id {}. Reason: ", + rcUserId, + rcGroupId, + ex); + throw new RocketChatRemoveUserFromGroupException( + String.format( + "Could not remove user %s from Rocket.Chat group with id %s", rcUserId, rcGroupId)); + } + + if (response != null && !response.isSuccess()) { + var error = "Could not remove user %s from Rocket.Chat group with id %s"; + throw new RocketChatRemoveUserFromGroupException(String.format(error, rcUserId, rcGroupId)); + } + } + } } diff --git a/src/main/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatGroupOperation.java b/src/main/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatGroupOperation.java index d07516bf4..372f249d9 100644 --- a/src/main/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatGroupOperation.java +++ b/src/main/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatGroupOperation.java @@ -9,6 +9,7 @@ import de.caritas.cob.userservice.api.port.out.IdentityClient; import de.caritas.cob.userservice.api.service.LogService; import java.util.List; +import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.stream.Collectors; import lombok.NonNull; @@ -54,16 +55,27 @@ String resolveTypeOfSession(Session session) { } void removeConsultantsFromSessionGroups(Session session, List consultants) { - removeConsultantsFromRocketChatGroup(session.getGroupId(), consultants); - removeConsultantsFromRocketChatGroup(session.getFeedbackGroupId(), consultants); + removeConsultantsFromRocketChatGroup( + session.getGroupId(), consultants, rocketChatFacade::removeUserFromGroup); + removeConsultantsFromRocketChatGroup( + session.getFeedbackGroupId(), consultants, rocketChatFacade::removeUserFromGroup); } void removeConsultantsFromSessionGroup(String rcGroupId, List consultants) { - removeConsultantsFromRocketChatGroup(rcGroupId, consultants); + removeConsultantsFromRocketChatGroup( + rcGroupId, consultants, rocketChatFacade::removeUserFromGroup); } - private void removeConsultantsFromRocketChatGroup( + void removeConsultantsFromSessionGroupAndIgnoreGroupNotFound( String rcGroupId, List consultants) { + removeConsultantsFromRocketChatGroup( + rcGroupId, consultants, rocketChatFacade::removeUserFromGroupIgnoreGroupNotFound); + } + + private void removeConsultantsFromRocketChatGroup( + String rcGroupId, + List consultants, + BiConsumer removeFromRocketchatGroupMethod) { if (rcGroupId == null) { return; } @@ -73,7 +85,7 @@ private void removeConsultantsFromRocketChatGroup( consultants.stream() .map(Consultant::getRocketChatId) .filter(groupMemberList::contains) - .forEach(rcUserId -> rocketChatFacade.removeUserFromGroup(rcUserId, rcGroupId)); + .forEach(rcUserId -> removeFromRocketchatGroupMethod.accept(rcUserId, rcGroupId)); rocketChatFacade.leaveFromGroupAsTechnicalUser(rcGroupId); } diff --git a/src/main/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatRemoveFromGroupOperationService.java b/src/main/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatRemoveFromGroupOperationService.java index e91a7da6b..e08560e11 100644 --- a/src/main/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatRemoveFromGroupOperationService.java +++ b/src/main/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatRemoveFromGroupOperationService.java @@ -74,10 +74,11 @@ private void performGroupsRemove(Session session, List consultants) } /** Removes the given consultant from Rocket.Chat group of given session. */ - public void removeFromGroup() { + public void removeFromGroupAndIgnoreGroupNotFound() { this.consultantsToRemoveFromSessions.forEach( ((session, consultants) -> - removeConsultantsFromSessionGroup(session.getGroupId(), consultants))); + removeConsultantsFromSessionGroupAndIgnoreGroupNotFound( + session.getGroupId(), consultants))); } /** @@ -104,7 +105,7 @@ public void removeFromFeedbackGroupOrRollbackOnFailure() { private void performGroupRemove(Session session, List consultants) { try { - removeConsultantsFromSessionGroup(session.getGroupId(), consultants); + removeConsultantsFromSessionGroupAndIgnoreGroupNotFound(session.getGroupId(), consultants); } catch (Exception e) { rollback(); throw new InternalServerErrorException( diff --git a/src/main/java/de/caritas/cob/userservice/api/config/auth/SecurityConfig.java b/src/main/java/de/caritas/cob/userservice/api/config/auth/SecurityConfig.java index ee245104d..67efd0797 100644 --- a/src/main/java/de/caritas/cob/userservice/api/config/auth/SecurityConfig.java +++ b/src/main/java/de/caritas/cob/userservice/api/config/auth/SecurityConfig.java @@ -219,7 +219,7 @@ protected void configure(HttpSecurity http) throws Exception { .antMatchers("/userstatistics", "/userstatistics/**") .permitAll() .antMatchers(HttpMethod.DELETE, "/useradmin/consultants/{consultantId:[0-9]+}/delete") - .hasAuthority(USER_ADMIN) + .hasAnyAuthority(USER_ADMIN, RESTRICTED_AGENCY_ADMIN) .antMatchers(HttpMethod.GET, "/actuator/health") .permitAll() .antMatchers(HttpMethod.GET, "/actuator/health/*") diff --git a/src/main/java/de/caritas/cob/userservice/api/facade/RocketChatFacade.java b/src/main/java/de/caritas/cob/userservice/api/facade/RocketChatFacade.java index 48a30022f..e77fdd5eb 100644 --- a/src/main/java/de/caritas/cob/userservice/api/facade/RocketChatFacade.java +++ b/src/main/java/de/caritas/cob/userservice/api/facade/RocketChatFacade.java @@ -139,6 +139,17 @@ public void removeUserFromGroup(String rcUserId, String groupId) { } } + public void removeUserFromGroupIgnoreGroupNotFound(String rcUserId, String groupId) { + try { + this.rocketChatService.removeUserFromGroupIgnoreGroupNotFound(rcUserId, groupId); + } catch (RocketChatRemoveUserFromGroupException e) { + var message = + String.format( + "Could not remove user with id %s from Rocket.Chat group id %s", rcUserId, groupId); + throw new InternalServerErrorException(message, LogService::logInternalServerError); + } + } + /** * Get all standard members (all users except system user and technical user) of a rocket chat * group. diff --git a/src/main/java/de/caritas/cob/userservice/api/facade/assignsession/AssignEnquiryFacade.java b/src/main/java/de/caritas/cob/userservice/api/facade/assignsession/AssignEnquiryFacade.java index 9983253bb..060369c44 100644 --- a/src/main/java/de/caritas/cob/userservice/api/facade/assignsession/AssignEnquiryFacade.java +++ b/src/main/java/de/caritas/cob/userservice/api/facade/assignsession/AssignEnquiryFacade.java @@ -163,7 +163,7 @@ private void removeUnauthorizedMembers( .onSessionConsultants(Map.of(session, consultantsToRemoveFromRocketChat)); if (rcGroupId.equalsIgnoreCase(session.getGroupId())) { - rocketChatRemoveFromGroupOperationService.removeFromGroup(); + rocketChatRemoveFromGroupOperationService.removeFromGroupAndIgnoreGroupNotFound(); } if (rcGroupId.equalsIgnoreCase(session.getFeedbackGroupId())) { rocketChatRemoveFromGroupOperationService.removeFromFeedbackGroup(); 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 83d49f609..e90eec53a 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,28 +114,34 @@ 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; + } - session.ifPresentOrElse( - s -> workflowErrors.addAll(deleteSessionService.performSessionDeletion(s)), - () -> - workflowErrors.add( - DeletionWorkflowError.builder() - .deletionSourceType(ASKER) - .deletionTargetType(ALL) - .identifier(rcGroupId) - .reason(RC_SESSION_GROUP_NOT_FOUND_REASON) - .timestamp(nowInUtc()) - .build())); + 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; } private Optional findSessionInUserSessionList( String rcGroupId, List userSessionList) { - return userSessionList.stream().filter(s -> s.getGroupId().equals(rcGroupId)).findFirst(); + + return userSessionList.stream() + .filter(s -> s.getGroupId() != null && s.getGroupId().equals(rcGroupId)) + .findFirst(); } } diff --git a/src/test/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatRemoveFromGroupOperationServiceTest.java b/src/test/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatRemoveFromGroupOperationServiceTest.java index 9dd34df30..740f1a11c 100644 --- a/src/test/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatRemoveFromGroupOperationServiceTest.java +++ b/src/test/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatRemoveFromGroupOperationServiceTest.java @@ -133,7 +133,7 @@ public void removeFromGroupOrRollbackOnFailure_Should_executeRemoveForRocketChat this.removeService.removeFromGroupOrRollbackOnFailure(); - verify(this.rocketChatFacade, times(1)).removeUserFromGroup("rcId", "group"); + verify(this.rocketChatFacade, times(1)).removeUserFromGroupIgnoreGroupNotFound("rcId", "group"); verify(this.rocketChatFacade, never()).removeUserFromGroup("rcId", "feedback"); } @@ -149,7 +149,7 @@ public void removeFromGroupOrRollbackOnFailure_Should_executeRemoveForRocketChat .thenReturn(singletonList(groupMemberDTO)); doThrow(new RuntimeException("")) .when(this.rocketChatFacade) - .removeUserFromGroup(anyString(), anyString()); + .removeUserFromGroupIgnoreGroupNotFound(anyString(), anyString()); doThrow(new RuntimeException("")) .when(this.rocketChatFacade) .addUserToRocketChatGroup(anyString(), anyString()); @@ -172,7 +172,9 @@ public void removeFromGroupOrRollbackOnFailure_Should_executeRemoveForRocketChat groupMemberDTO.set_id(this.consultant.getRocketChatId()); when(this.rocketChatFacade.retrieveRocketChatMembers(any())) .thenReturn(singletonList(groupMemberDTO)); - doThrow(new RuntimeException("")).when(this.rocketChatFacade).removeUserFromGroup(any(), any()); + doThrow(new RuntimeException("")) + .when(this.rocketChatFacade) + .removeUserFromGroupIgnoreGroupNotFound(any(), any()); try { this.removeService.removeFromGroupOrRollbackOnFailure(); diff --git a/src/test/java/de/caritas/cob/userservice/api/facade/assignsession/AssignEnquiryFacadeTest.java b/src/test/java/de/caritas/cob/userservice/api/facade/assignsession/AssignEnquiryFacadeTest.java index a64d58dea..9b72f0581 100644 --- a/src/test/java/de/caritas/cob/userservice/api/facade/assignsession/AssignEnquiryFacadeTest.java +++ b/src/test/java/de/caritas/cob/userservice/api/facade/assignsession/AssignEnquiryFacadeTest.java @@ -383,7 +383,8 @@ void assignEnquiry_Should_removeAllUnauthorizedMembers_When_sessionIsNotATeamSes verifyAsync( (a) -> verify(this.rocketChatFacade, times(1)) - .removeUserFromGroup(consultantToRemove.getRocketChatId(), session.getGroupId())); + .removeUserFromGroupIgnoreGroupNotFound( + consultantToRemove.getRocketChatId(), session.getGroupId())); verifyAsync( (a) -> verify(this.rocketChatFacade, times(1)) @@ -424,7 +425,8 @@ void assignEnquiry_ShouldNot_removeTeamMembers_When_sessionIsTeamSession() { verifyAsync( (a) -> verify(this.rocketChatFacade, atLeastOnce()) - .removeUserFromGroup(consultantToRemove.getRocketChatId(), session.getGroupId())); + .removeUserFromGroupIgnoreGroupNotFound( + consultantToRemove.getRocketChatId(), session.getGroupId())); verifyAsync( (a) -> verify(this.rocketChatFacade, atLeastOnce()) diff --git a/src/test/java/de/caritas/cob/userservice/api/facade/assignsession/AssignSessionFacadeTest.java b/src/test/java/de/caritas/cob/userservice/api/facade/assignsession/AssignSessionFacadeTest.java index 019c03fb1..eeac66b94 100644 --- a/src/test/java/de/caritas/cob/userservice/api/facade/assignsession/AssignSessionFacadeTest.java +++ b/src/test/java/de/caritas/cob/userservice/api/facade/assignsession/AssignSessionFacadeTest.java @@ -159,11 +159,13 @@ public void assignSession_Should_removeAllUnauthorizedMembers_When_sessionIsNotA verifyAsync( a -> verify(this.rocketChatFacade, atLeastOnce()) - .removeUserFromGroup(consultantToRemove.getRocketChatId(), session.getGroupId())); + .removeUserFromGroupIgnoreGroupNotFound( + consultantToRemove.getRocketChatId(), session.getGroupId())); verifyAsync( a -> verify(this.rocketChatFacade, atLeastOnce()) - .removeUserFromGroup(consultantToRemove.getRocketChatId(), session.getGroupId())); + .removeUserFromGroupIgnoreGroupNotFound( + consultantToRemove.getRocketChatId(), session.getGroupId())); verify(this.emailNotificationFacade, times(1)) .sendAssignEnquiryEmailNotification(any(), any(), any(), any()); } @@ -281,23 +283,28 @@ public void assignSession_ShouldNot_removeTeamMembers_When_sessionIsTeamSession( verifyAsync( a -> verify(this.rocketChatFacade, atLeastOnce()) - .removeUserFromGroup(consultantToRemove.getRocketChatId(), session.getGroupId())); + .removeUserFromGroupIgnoreGroupNotFound( + consultantToRemove.getRocketChatId(), session.getGroupId())); verifyAsync( a -> verify(this.rocketChatFacade, never()) - .removeUserFromGroup("teamConsultantRcId", session.getGroupId())); + .removeUserFromGroupIgnoreGroupNotFound( + "teamConsultantRcId", session.getGroupId())); verifyAsync( a -> verify(this.rocketChatFacade, never()) - .removeUserFromGroup("teamConsultantRcId", session.getFeedbackGroupId())); + .removeUserFromGroupIgnoreGroupNotFound( + "teamConsultantRcId", session.getFeedbackGroupId())); verifyAsync( a -> verify(this.rocketChatFacade, never()) - .removeUserFromGroup("teamConsultantRcId2", session.getGroupId())); + .removeUserFromGroupIgnoreGroupNotFound( + "teamConsultantRcId2", session.getGroupId())); verifyAsync( a -> verify(this.rocketChatFacade, never()) - .removeUserFromGroup("teamConsultantRcId2", session.getFeedbackGroupId())); + .removeUserFromGroupIgnoreGroupNotFound( + "teamConsultantRcId2", session.getFeedbackGroupId())); verifyAsync( a -> verify(this.emailNotificationFacade, times(1)) 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 92b1a151e..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 @@ -21,15 +21,15 @@ import java.util.Map; import java.util.Optional; import org.jeasy.random.EasyRandom; -import org.junit.Test; -import org.junit.runner.RunWith; +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.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; -@RunWith(MockitoJUnitRunner.class) -public class DeleteInactiveSessionsAndUserServiceTest { +@ExtendWith(MockitoExtension.class) +class DeleteInactiveSessionsAndUserServiceTest { @InjectMocks private DeleteInactiveSessionsAndUserService deleteInactiveSessionsAndUserService; @@ -42,9 +42,8 @@ public class DeleteInactiveSessionsAndUserServiceTest { @Mock private InactivePrivateGroupsProvider inactivePrivateGroupsProvider; @Test - public void - deleteInactiveSessionsAndUsers_Should_SendWorkflowErrorsMail_When_userNotFoundReason() { - + void deleteInactiveSessionsAndUsers_Should_SendWorkflowErrorsMail_When_userNotFoundReason() { + // given EasyRandom easyRandom = new EasyRandom(); User user = easyRandom.nextObject(User.class); Session session = easyRandom.nextObject(Session.class); @@ -63,17 +62,19 @@ public class DeleteInactiveSessionsAndUserServiceTest { 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())); } @Test - public void + void deleteInactiveSessionsAndUsers_Should_DeleteEntireUserAccount_WhenUserHasOnlyInactiveSessions() { - + // given EasyRandom easyRandom = new EasyRandom(); User user = easyRandom.nextObject(User.class); Session session1 = easyRandom.nextObject(Session.class); @@ -90,15 +91,17 @@ public class DeleteInactiveSessionsAndUserServiceTest { .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 - public void + void deleteInactiveSessionsAndUsers_Should_DeleteSingleSession_WhenUserHasActiveAndInactiveSessions() { - + // given EasyRandom easyRandom = new EasyRandom(); User user = easyRandom.nextObject(User.class); Session session1 = easyRandom.nextObject(Session.class); @@ -115,15 +118,17 @@ public class DeleteInactiveSessionsAndUserServiceTest { .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 - public void + void deleteInactiveSessionsAndUsers_Should_logWorkflowErrorMail_WhenUserHasActiveAndInactiveSessionsAndHasErrors() { - + // given EasyRandom easyRandom = new EasyRandom(); User user = easyRandom.nextObject(User.class); Session session1 = easyRandom.nextObject(Session.class); @@ -150,8 +155,10 @@ public class DeleteInactiveSessionsAndUserServiceTest { 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)) @@ -159,9 +166,9 @@ public class DeleteInactiveSessionsAndUserServiceTest { } @Test - public void - deleteInactiveSessionsAndUsers_Should_logWorkflowErrorMail_WhenSessionCouldNotBeFound() { - + void + deleteInactiveSessionsAndUsers_Should_notLogError_WhenSessionCouldNotBeFound_BecauseItMayHaveBeenDeletedByPreviousWorkflowRun() { + // given EasyRandom easyRandom = new EasyRandom(); User user = easyRandom.nextObject(User.class); Session session1 = easyRandom.nextObject(Session.class); @@ -179,18 +186,17 @@ public class DeleteInactiveSessionsAndUserServiceTest { .thenReturn(Optional.of(user)); when(sessionRepository.findByUser(user)).thenReturn(Arrays.asList(session2, session3)); + // when deleteInactiveSessionsAndUserService.deleteInactiveSessionsAndUsers(); - verify(workflowErrorLogService, Mockito.times(1)) - .logWorkflowErrors(argThat(list -> !list.isEmpty())); - verify(workflowErrorMailService, Mockito.times(1)) - .buildAndSendErrorMail(Collections.emptyList()); + // then + verify(workflowErrorLogService, Mockito.never()).logWorkflowErrors(Mockito.anyList()); + verify(workflowErrorMailService, Mockito.never()).buildAndSendErrorMail(Mockito.anyList()); } @Test - public 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); @@ -205,10 +211,10 @@ public class DeleteInactiveSessionsAndUserServiceTest { 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); } }