Skip to content

Commit

Permalink
Added creating the audit log entry to TaskManager [#204]
Browse files Browse the repository at this point in the history
  • Loading branch information
mcpierce authored and BRUCELLA2 committed Jul 18, 2020
1 parent 628d01e commit 9b3e8a6
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

package org.comixed.task.runner;

import java.util.Date;
import lombok.extern.log4j.Log4j2;
import org.comixed.task.adaptors.TaskAdaptor;
import org.comixed.model.tasks.TaskAuditLogEntry;
import org.comixed.repositories.tasks.TaskAuditLogRepository;
import org.comixed.task.model.MonitorTaskQueue;
import org.comixed.task.model.WorkerTask;
import org.comixed.task.model.WorkerTaskException;
import org.springframework.beans.factory.InitializingBean;
Expand All @@ -31,19 +34,33 @@
@Log4j2
public class TaskManager implements InitializingBean {
@Autowired private ThreadPoolTaskExecutor taskExecutor;
@Autowired private TaskAdaptor taskAdaptor;
@Autowired private TaskAuditLogRepository auditLogRepository;

public void runTask(final WorkerTask task) {
this.taskExecutor.execute(
() -> {
log.debug("Preparing to run task: {}", task.getDescription());
final String description = task.getDescription();
log.debug("Preparing to run task: {}", description);
final Date started = new Date();
boolean success = false;
try {
task.startTask();
success = true;
} catch (WorkerTaskException error) {
log.error("Error executing task: {}" + task.getDescription(), error);
log.error("Error executing task: {}" + description, error);
} finally {
task.afterExecution();
}
final Date ended = new Date();
// do not log MonitorTaskQueue events
if (!(task instanceof MonitorTaskQueue)) {
final TaskAuditLogEntry entry = new TaskAuditLogEntry();
entry.setStartTime(started);
entry.setEndTime(ended);
entry.setSuccessful(success);
entry.setDescription(description);
TaskManager.this.auditLogRepository.save(entry);
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

package org.comixed.task.runner;

import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.*;

import org.comixed.model.tasks.TaskAuditLogEntry;
import org.comixed.repositories.tasks.TaskAuditLogRepository;
import org.comixed.task.model.MonitorTaskQueue;
import org.comixed.task.model.WorkerTask;
import org.comixed.task.model.WorkerTaskException;
import org.junit.Test;
Expand All @@ -30,15 +33,73 @@

@RunWith(MockitoJUnitRunner.class)
public class TaskManagerTest {
private static final String TEST_DESCRIPTION = "The task description";

@InjectMocks private TaskManager taskManager;
@Mock private ThreadPoolTaskExecutor taskExecutor;
@Mock private WorkerTask workerTask;
@Captor private ArgumentCaptor<Runnable> runnableArgumentCaptor;
@Mock private TaskAuditLogRepository taskAuditLogRepository;
@Captor private ArgumentCaptor<TaskAuditLogEntry> logEntryArgumentCaptor;
@Mock private TaskAuditLogEntry logEntryRecord;
@Mock private MonitorTaskQueue monitorTaskQueue;

@Test
public void testRunTask() throws WorkerTaskException {
Mockito.when(workerTask.getDescription()).thenReturn(TEST_DESCRIPTION);
Mockito.doNothing().when(taskExecutor).execute(runnableArgumentCaptor.capture());
Mockito.when(taskAuditLogRepository.save(logEntryArgumentCaptor.capture()))
.thenReturn(logEntryRecord);

taskManager.runTask(workerTask);

assertNotNull(runnableArgumentCaptor.getValue());

Mockito.verify(taskExecutor, Mockito.times(1)).execute(runnableArgumentCaptor.getValue());

runnableArgumentCaptor.getValue().run();

Mockito.verify(workerTask, Mockito.times(1)).getDescription();
Mockito.verify(workerTask, Mockito.times(1)).startTask();
Mockito.verify(workerTask, Mockito.times(1)).afterExecution();

final TaskAuditLogEntry logEntry = logEntryArgumentCaptor.getValue();
assertNotNull(logEntry);
assertNotNull(logEntry.getStartTime());
assertNotNull(logEntry.getEndTime());
assertTrue(logEntry.getSuccessful());
assertEquals(TEST_DESCRIPTION, logEntry.getDescription());

Mockito.verify(taskAuditLogRepository, Mockito.times(1)).save(logEntry);
}

@Test
public void testRunTaskDoNotAuditMonitorTaskQueue() throws WorkerTaskException {
Mockito.doNothing().when(taskExecutor).execute(runnableArgumentCaptor.capture());

taskManager.runTask(monitorTaskQueue);

assertNotNull(runnableArgumentCaptor.getValue());

Mockito.verify(taskExecutor, Mockito.times(1)).execute(runnableArgumentCaptor.getValue());

runnableArgumentCaptor.getValue().run();

Mockito.verify(monitorTaskQueue, Mockito.times(1)).getDescription();
Mockito.verify(monitorTaskQueue, Mockito.times(1)).startTask();
Mockito.verify(monitorTaskQueue, Mockito.times(1)).afterExecution();

Mockito.verify(taskAuditLogRepository, Mockito.never()).save(Mockito.any());
}

@Test
public void testRunTaskThrowsException() throws WorkerTaskException {
Mockito.when(workerTask.getDescription()).thenReturn(TEST_DESCRIPTION);
Mockito.doThrow(WorkerTaskException.class).when(workerTask).startTask();
Mockito.doNothing().when(taskExecutor).execute(runnableArgumentCaptor.capture());
Mockito.when(taskAuditLogRepository.save(logEntryArgumentCaptor.capture()))
.thenReturn(logEntryRecord);

taskManager.runTask(workerTask);

assertNotNull(runnableArgumentCaptor.getValue());
Expand All @@ -50,6 +111,15 @@ public void testRunTask() throws WorkerTaskException {
Mockito.verify(workerTask, Mockito.times(1)).getDescription();
Mockito.verify(workerTask, Mockito.times(1)).startTask();
Mockito.verify(workerTask, Mockito.times(1)).afterExecution();

final TaskAuditLogEntry logEntry = logEntryArgumentCaptor.getValue();
assertNotNull(logEntry);
assertNotNull(logEntry.getStartTime());
assertNotNull(logEntry.getEndTime());
assertFalse(logEntry.getSuccessful());
assertEquals(TEST_DESCRIPTION, logEntry.getDescription());

Mockito.verify(taskAuditLogRepository, Mockito.times(1)).save(logEntry);
}

@Test
Expand Down

0 comments on commit 9b3e8a6

Please sign in to comment.