diff --git a/src/main/java/de/caritas/cob/userservice/api/workflow/delete/action/asker/DeleteRoomsAndSessionAction.java b/src/main/java/de/caritas/cob/userservice/api/workflow/delete/action/asker/DeleteRoomsAndSessionAction.java index a94184e00..833512ac7 100644 --- a/src/main/java/de/caritas/cob/userservice/api/workflow/delete/action/asker/DeleteRoomsAndSessionAction.java +++ b/src/main/java/de/caritas/cob/userservice/api/workflow/delete/action/asker/DeleteRoomsAndSessionAction.java @@ -79,9 +79,9 @@ protected void deleteSession(Session session, List workfl void performSessionDeletion(Session session, List workflowErrors) { - deleteRocketChatGroup(session.getGroupId(), workflowErrors); - deleteRocketChatGroup(session.getFeedbackGroupId(), workflowErrors); deleteSessionData(session, workflowErrors); deleteSession(session, workflowErrors); + deleteRocketChatGroup(session.getGroupId(), workflowErrors); + deleteRocketChatGroup(session.getFeedbackGroupId(), workflowErrors); } } 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 fe133b27e..b14e11f34 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 @@ -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; @@ -78,14 +81,20 @@ private List performDeletionWorkflow( List workflowErrors = new ArrayList<>(); - Optional user = - userRepository.findByRcUserIdAndDeleteDateIsNull(userInactiveGroupEntry.getKey()); - user.ifPresentOrElse( - u -> workflowErrors.addAll(deleteInactiveGroupsOrUser(userInactiveGroupEntry, u)), - () -> - workflowErrors.addAll( - performUserSessionDeletionForNonExistingUser(userInactiveGroupEntry.getValue()))); - + try { + Optional 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; } @@ -138,7 +147,11 @@ private Collection performUserSessionDeletionFo String rcGroupId) { List workflowErrors = new ArrayList<>(); Optional 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; } diff --git a/src/test/java/de/caritas/cob/userservice/api/workflow/delete/action/asker/DeleteRoomsAndSessionActionTest.java b/src/test/java/de/caritas/cob/userservice/api/workflow/delete/action/asker/DeleteRoomsAndSessionActionTest.java new file mode 100644 index 000000000..e3f6e9042 --- /dev/null +++ b/src/test/java/de/caritas/cob/userservice/api/workflow/delete/action/asker/DeleteRoomsAndSessionActionTest.java @@ -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(); + } +} 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 ae6aa561f..cd78603fc 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 @@ -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; @@ -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; @@ -41,6 +51,16 @@ class DeleteInactiveSessionsAndUserServiceTest { @Mock private DeleteSessionService deleteSessionService; @Mock private InactivePrivateGroupsProvider inactivePrivateGroupsProvider; + private ListAppender 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 @@ -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> 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> 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 logEvents = listAppender.list; + assertTrue( + logEvents.stream() + .anyMatch( + event -> + event + .getFormattedMessage() + .contains( + "Non unique result for findByRcUserIdAndDeleteDateIsNull found"))); + } }