Skip to content

Commit

Permalink
Merge pull request #35 from virtualidentityag/DIAKONIE-375-fix-deleti…
Browse files Browse the repository at this point in the history
…on-routine-to-delete-rc-rooms-if-session-is-already-deleted-from-mariadb

Diakonie 375 fix deletion routine to delete rc rooms if session is already deleted from mariadb
  • Loading branch information
tkuzynow authored Sep 27, 2024
2 parents 10d2119 + c4d5095 commit 2d4ad0b
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/dockerImage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
run: mvn -B -Pprod clean package -DskipTests=true
- name: Maven Verify
run: mvn -B -Pprod clean verify -DskipTests=true
- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
with:
name: targetfiles
path: target/*.jar
Expand Down Expand Up @@ -76,7 +76,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Download buildfiles artifact
uses: actions/download-artifact@v2
uses: actions/download-artifact@v4
with:
name: targetfiles
- name: Get current time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ public class RocketChatService implements MessageClient {
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 static final String COULD_NOT_REMOVE_USER_FROM_ROCKET_CHAT_GROUP =
"Could not remove user %s from Rocket.Chat group with id %s";
private final LocalDateTime localDateTime1900 = LocalDateTime.of(1900, 1, 1, 0, 0);

private final LocalDateTime localDateTimeFuture = nowInUtc().plusYears(1L);
Expand Down Expand Up @@ -662,12 +664,11 @@ public void removeUserFromGroup(String rcUserId, String rcGroupId)
rcGroupId,
ex);
throw new RocketChatRemoveUserFromGroupException(
String.format(
"Could not remove user %s from Rocket.Chat group with id %s", rcUserId, rcGroupId));
String.format(COULD_NOT_REMOVE_USER_FROM_ROCKET_CHAT_GROUP, rcUserId, rcGroupId));
}

if (response != null && !response.isSuccess()) {
var error = "Could not remove user %s from Rocket.Chat group with id %s";
var error = COULD_NOT_REMOVE_USER_FROM_ROCKET_CHAT_GROUP;
throw new RocketChatRemoveUserFromGroupException(String.format(error, rcUserId, rcGroupId));
}
}
Expand Down Expand Up @@ -802,6 +803,7 @@ public List<GroupMemberDTO> getChatUsers(String chatId) {
* @return al members of the group
* @deprecated use getChatUsers
*/
@Deprecated
public List<GroupMemberDTO> getMembersOfGroup(String rcGroupId)
throws RocketChatGetGroupMembersException {

Expand Down Expand Up @@ -1365,12 +1367,11 @@ public void removeUserFromGroupIgnoreGroupNotFound(String rcUserId, String rcGro
rcGroupId,
ex);
throw new RocketChatRemoveUserFromGroupException(
String.format(
"Could not remove user %s from Rocket.Chat group with id %s", rcUserId, rcGroupId));
String.format(COULD_NOT_REMOVE_USER_FROM_ROCKET_CHAT_GROUP, rcUserId, rcGroupId));
}

if (response != null && !response.isSuccess()) {
var error = "Could not remove user %s from Rocket.Chat group with id %s";
var error = COULD_NOT_REMOVE_USER_FROM_ROCKET_CHAT_GROUP;
throw new RocketChatRemoveUserFromGroupException(String.format(error, rcUserId, rcGroupId));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private boolean shouldEnrichTopic(SessionDTO session) {
}

private void enrichSession(Map<Long, TopicDTO> availableTopics, SessionDTO sessionDTO) {
var topicData = availableTopics.get(Long.valueOf(sessionDTO.getTopic().getId()));
var topicData = availableTopics.get(sessionDTO.getTopic().getId());
if (topicData != null) {
log.debug("Enriching session with id {} with topicData {}", sessionDTO.getId(), topicData);
sessionDTO.setTopic(convertToSessionTopicDTO(topicData));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
@RequiredArgsConstructor
abstract class DeleteRoomsAndSessionAction {

private static final String USER_SERVICE_DELETE_WORKFLOW_ERROR =
"UserService delete workflow error: ";
protected final @NonNull SessionRepository sessionRepository;
protected final @NonNull SessionDataRepository sessionDataRepository;
protected final @NonNull RocketChatService rocketChatService;
Expand All @@ -29,7 +31,7 @@ void deleteRocketChatGroup(String rcGroupId, List<DeletionWorkflowError> workflo
try {
this.rocketChatService.deleteGroupAsTechnicalUser(rcGroupId);
} catch (RocketChatDeleteGroupException e) {
log.error("UserService delete workflow error: ", e);
log.error(USER_SERVICE_DELETE_WORKFLOW_ERROR, e);
workflowErrors.add(
DeletionWorkflowError.builder()
.deletionSourceType(ASKER)
Expand All @@ -47,7 +49,7 @@ void deleteSessionData(Session session, List<DeletionWorkflowError> workflowErro
var sessionData = this.sessionDataRepository.findBySessionId(session.getId());
this.sessionDataRepository.deleteAll(sessionData);
} catch (Exception e) {
log.error("UserService delete workflow error: ", e);
log.error(USER_SERVICE_DELETE_WORKFLOW_ERROR, e);
workflowErrors.add(
DeletionWorkflowError.builder()
.deletionSourceType(ASKER)
Expand All @@ -63,7 +65,7 @@ protected void deleteSession(Session session, List<DeletionWorkflowError> workfl
try {
this.sessionRepository.delete(session);
} catch (Exception e) {
log.error("UserService delete workflow error: ", e);
log.error(USER_SERVICE_DELETE_WORKFLOW_ERROR, e);
workflowErrors.add(
DeletionWorkflowError.builder()
.deletionSourceType(ASKER)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package de.caritas.cob.userservice.api.workflow.delete.action.asker;

import static de.caritas.cob.userservice.api.helper.CustomLocalDateTime.nowInUtc;
import static de.caritas.cob.userservice.api.workflow.delete.model.DeletionSourceType.ASKER;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

import de.caritas.cob.userservice.api.actions.ActionCommand;
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.workflow.delete.model.DeletionTargetType;
import de.caritas.cob.userservice.api.workflow.delete.model.DeletionWorkflowError;
import de.caritas.cob.userservice.api.workflow.delete.model.RocketchatRoomDeletionWorkflowDTO;
import de.caritas.cob.userservice.api.workflow.delete.model.SessionDeletionWorkflowDTO;
import java.util.List;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class DeleteSingleRoomAction implements ActionCommand<RocketchatRoomDeletionWorkflowDTO> {

protected final @NonNull RocketChatService rocketChatService;

public DeleteSingleRoomAction(@NonNull RocketChatService rocketChatService) {
this.rocketChatService = rocketChatService;
}

void deleteRocketChatGroup(String rcGroupId, List<DeletionWorkflowError> workflowErrors) {
if (isNotBlank(rcGroupId)) {
try {
this.rocketChatService.deleteGroupAsTechnicalUser(rcGroupId);
} catch (RocketChatDeleteGroupException e) {
log.error("UserService delete workflow error: ", e);
workflowErrors.add(
DeletionWorkflowError.builder()
.deletionSourceType(ASKER)
.deletionTargetType(DeletionTargetType.ROCKET_CHAT)
.identifier(rcGroupId)
.reason("Deletion of Rocket.Chat group failed")
.timestamp(nowInUtc())
.build());
}
}
}

/**
* Deletes the given {@link Session} in the database with the related Rocket.Chat room containing
* all messages and uploads.
*
* @param actionTarget the {@link SessionDeletionWorkflowDTO} with the session to delete
*/
@Override
public void execute(RocketchatRoomDeletionWorkflowDTO actionTarget) {
deleteRocketChatGroup(
actionTarget.getRocketchatRoomId(), actionTarget.getDeletionWorkflowErrors());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.caritas.cob.userservice.api.workflow.delete.model;

import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class RocketchatRoomDeletionWorkflowDTO {

private String rocketchatRoomId;
private List<DeletionWorkflowError> deletionWorkflowErrors;
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ 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)));
session.ifPresentOrElse(
s -> workflowErrors.addAll(deleteSessionService.performSessionDeletion(s)),
() ->
workflowErrors.addAll(
deleteSessionService.performRocketchatSessionDeletion(rcGroupId)));

return workflowErrors;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

import de.caritas.cob.userservice.api.actions.registry.ActionsRegistry;
import de.caritas.cob.userservice.api.model.Session;
import de.caritas.cob.userservice.api.workflow.delete.action.asker.DeleteSingleRoomAction;
import de.caritas.cob.userservice.api.workflow.delete.action.asker.DeleteSingleRoomAndSessionAction;
import de.caritas.cob.userservice.api.workflow.delete.model.DeletionWorkflowError;
import de.caritas.cob.userservice.api.workflow.delete.model.RocketchatRoomDeletionWorkflowDTO;
import de.caritas.cob.userservice.api.workflow.delete.model.SessionDeletionWorkflowDTO;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import lombok.NonNull;
Expand Down Expand Up @@ -42,4 +45,20 @@ public List<DeletionWorkflowError> performSessionDeletion(Session session) {

return deletionWorkflowDTO.getDeletionWorkflowErrors();
}

public Collection<? extends DeletionWorkflowError> performRocketchatSessionDeletion(
String rcGroupId) {
if (isNull(rcGroupId)) {
return Collections.emptyList();
}

var deletionWorkflowDTO = new RocketchatRoomDeletionWorkflowDTO(rcGroupId, new ArrayList<>());

this.actionsRegistry
.buildContainerForType(RocketchatRoomDeletionWorkflowDTO.class)
.addActionToExecute(DeleteSingleRoomAction.class)
.executeActions(deletionWorkflowDTO);

return deletionWorkflowDTO.getDeletionWorkflowErrors();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package de.caritas.cob.userservice.api.workflow.delete.action.asker;

import static java.util.Collections.emptyList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.powermock.reflect.Whitebox.setInternalState;

import de.caritas.cob.userservice.api.adapters.rocketchat.RocketChatService;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatDeleteGroupException;
import de.caritas.cob.userservice.api.workflow.delete.model.DeletionWorkflowError;
import de.caritas.cob.userservice.api.workflow.delete.model.RocketchatRoomDeletionWorkflowDTO;
import java.util.ArrayList;
import java.util.List;
import org.jeasy.random.EasyRandom;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.Logger;

@RunWith(MockitoJUnitRunner.class)
public class DeleteSingleRoomActionTest {

@InjectMocks private DeleteSingleRoomAction deleteSingleRoomAction;

@Mock private RocketChatService rocketChatService;

@Mock private Logger logger;

@Before
public void setup() {
setInternalState(DeleteSingleRoomAction.class, "log", logger);
}

@Test
public void
execute_Should_returnEmptyListAndPerformAllDeletions_When_userSessionIsDeletedSuccessful()
throws Exception {
String rocketChatId = new EasyRandom().nextObject(String.class);
RocketchatRoomDeletionWorkflowDTO workflowDTO =
new RocketchatRoomDeletionWorkflowDTO(rocketChatId, emptyList());

this.deleteSingleRoomAction.execute(workflowDTO);
List<DeletionWorkflowError> workflowErrors = workflowDTO.getDeletionWorkflowErrors();

assertThat(workflowErrors, hasSize(0));
verifyNoMoreInteractions(this.logger);
verify(this.rocketChatService, times(1)).deleteGroupAsTechnicalUser(any());
}

@Test
public void
execute_Should_returnExpectedWorkflowErrors_When_noUserSessionDeletedStepIsSuccessful()
throws Exception {
doThrow(new RocketChatDeleteGroupException(new RuntimeException()))
.when(this.rocketChatService)
.deleteGroupAsTechnicalUser(any());

RocketchatRoomDeletionWorkflowDTO workflowDTO =
new RocketchatRoomDeletionWorkflowDTO("rocketChatId", new ArrayList<>());

this.deleteSingleRoomAction.execute(workflowDTO);
List<DeletionWorkflowError> workflowErrors = workflowDTO.getDeletionWorkflowErrors();

assertThat(workflowErrors, hasSize(1));
verify(logger, times(1)).error(anyString(), any(Exception.class));
}
}

0 comments on commit 2d4ad0b

Please sign in to comment.