Skip to content

Commit

Permalink
fix:
Browse files Browse the repository at this point in the history
* changed from email sending to logging the errors
  • Loading branch information
Leandro13Silva13 committed May 6, 2024
1 parent 9cfa4c5 commit 0c10354
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class DeleteInactiveSessionsAndUserService {
private final @NonNull UserRepository userRepository;
private final @NonNull SessionRepository sessionRepository;
private final @NonNull DeleteUserAccountService deleteUserAccountService;
private final @NonNull WorkflowErrorMailService workflowErrorMailService;
private final @NonNull WorkflowErrorLogService workflowErrorLogService;
private final @NonNull DeleteSessionService deleteSessionService;
private final @NonNull InactivePrivateGroupsProvider inactivePrivateGroupsProvider;

Expand All @@ -49,12 +49,12 @@ public void deleteInactiveSessionsAndUsers() {
.flatMap(Collection::stream)
.collect(Collectors.toList());

sendWorkflowErrorsMail(workflowErrors);
logWorkflowErrors(workflowErrors);
}

private void sendWorkflowErrorsMail(List<DeletionWorkflowError> workflowErrors) {
private void logWorkflowErrors(List<DeletionWorkflowError> workflowErrors) {
if (isNotEmpty(workflowErrors)) {
this.workflowErrorMailService.buildAndSendErrorMail(workflowErrors);
this.workflowErrorLogService.logWorkflowErrors(workflowErrors);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package de.caritas.cob.userservice.api.workflow.delete.service;

import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;

import de.caritas.cob.userservice.api.workflow.delete.model.DeletionWorkflowError;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/** Service class to log all deletion workflow errors. */
@Slf4j
@Service
@RequiredArgsConstructor
public class WorkflowErrorLogService {

public void logWorkflowErrors(List<DeletionWorkflowError> workflowErrors) {
if (isNotEmpty(workflowErrors)) {
workflowErrors.forEach(
workflowError ->
log.warn(
"Errors during deletion workflow:"
+ " SourceType = {}; "
+ "TargetType = {}; "
+ "Identifier = {}; "
+ "Reason = {}; "
+ "Timestamp = {}.",
workflowError.getDeletionSourceType(),
workflowError.getDeletionTargetType(),
workflowError.getIdentifier(),
workflowError.getReason(),
workflowError.getTimestamp()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class DeleteInactiveSessionsAndUserServiceTest {

@InjectMocks private DeleteInactiveSessionsAndUserService deleteInactiveSessionsAndUserService;

@Mock private WorkflowErrorMailService workflowErrorMailService;
@Mock private WorkflowErrorLogService workflowErrorLogService;
@Mock private UserRepository userRepository;
@Mock private SessionRepository sessionRepository;
@Mock private DeleteUserAccountService deleteUserAccountService;
Expand Down Expand Up @@ -60,7 +60,7 @@ public void deleteInactiveSessionsAndUsers_Should_SendWorkflowErrorsMail() {

deleteInactiveSessionsAndUserService.deleteInactiveSessionsAndUsers();

verify(workflowErrorMailService, Mockito.times(1)).buildAndSendErrorMail(any());
verify(workflowErrorLogService, Mockito.times(1)).logWorkflowErrors(any());
}

@Test
Expand Down Expand Up @@ -138,7 +138,7 @@ public void deleteInactiveSessionsAndUsers_Should_SendWorkflowErrorsMail() {

deleteInactiveSessionsAndUserService.deleteInactiveSessionsAndUsers();

verify(workflowErrorMailService, Mockito.times(1)).buildAndSendErrorMail(any());
verify(workflowErrorLogService, Mockito.times(1)).logWorkflowErrors(any());
}

@Test
Expand All @@ -164,7 +164,7 @@ public void deleteInactiveSessionsAndUsers_Should_SendWorkflowErrorsMail() {

deleteInactiveSessionsAndUserService.deleteInactiveSessionsAndUsers();

verify(workflowErrorMailService, Mockito.times(1)).buildAndSendErrorMail(any());
verify(workflowErrorLogService, Mockito.times(1)).logWorkflowErrors(any());
}

@Test
Expand All @@ -187,6 +187,6 @@ public void deleteInactiveSessionsAndUsers_Should_SendWorkflowErrorsMail() {

deleteInactiveSessionsAndUserService.deleteInactiveSessionsAndUsers();

verify(workflowErrorMailService, Mockito.times(1)).buildAndSendErrorMail(any());
verify(workflowErrorLogService, Mockito.times(1)).logWorkflowErrors(any());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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.ROCKET_CHAT;
import static java.util.Collections.emptyList;

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.workflow.delete.model.DeletionWorkflowError;
import java.util.ArrayList;
import java.util.List;
import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.LoggerFactory;

@RunWith(MockitoJUnitRunner.class)
public class WorkflowErrorLogServiceTest {

@InjectMocks private WorkflowErrorLogService workflowErrorLogService;

private ListAppender<ILoggingEvent> listAppender;

@Before
public void setUp() {
Logger logger = (Logger) LoggerFactory.getLogger(WorkflowErrorLogService.class);
listAppender = new ListAppender<>();
listAppender.start();
logger.addAppender(listAppender);
}

@Test
public void logWorkflowErrors_Should_logNoErrors_When_workflowErrorsAreNull() {

// when
this.workflowErrorLogService.logWorkflowErrors(null);

// then
Assertions.assertThat(listAppender.list).isEmpty();
}

@Test
public void logWorkflowErrors_Should_logNoErrors_When_workflowErrorsAreEmpty() {

// when
this.workflowErrorLogService.logWorkflowErrors(emptyList());

// then
Assertions.assertThat(listAppender.list).isEmpty();
}

@Test
public void logWorkflowErrors_Should_logErrors_When_workflowErrorsExists() {
// given
List<DeletionWorkflowError> workflowErrors = new ArrayList<>();
workflowErrors.add(
DeletionWorkflowError.builder()
.deletionSourceType(ASKER)
.deletionTargetType(ROCKET_CHAT)
.timestamp(nowInUtc())
.reason("reason")
.identifier("id")
.build());

// when
this.workflowErrorLogService.logWorkflowErrors(workflowErrors);

// then
Assertions.assertThat(listAppender.list).hasSize(1);
}
}

0 comments on commit 0c10354

Please sign in to comment.