Skip to content

Commit

Permalink
Changed the task audit list retrieval code [#424]
Browse files Browse the repository at this point in the history
 * Changed the response to use ApiResponse.
 * Included the latest date to reduce frontend calculation.
  • Loading branch information
mcpierce authored and BRUCELLA2 committed Aug 18, 2020
1 parent b08de88 commit 59b3eeb
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 15 deletions.
Expand Up @@ -67,5 +67,5 @@ public interface LibraryUpdate {}
public interface PluginList {}

/** Uses when viewing the list of task audit log entries. */
public interface TaskAuditLogEntryList {}
public interface TaskAuditLogEntryList extends ApiResponse {}
}
Expand Up @@ -38,5 +38,5 @@ public interface TaskAuditLogRepository extends JpaRepository<TaskAuditLogEntry,
* @param startTime the earliest startTime date
* @return the log entries
*/
List<TaskAuditLogEntry> findAllByStartTimeGreaterThan(Date startTime);
List<TaskAuditLogEntry> findAllByStartTimeGreaterThanOrderByStartTime(Date startTime);
}
Expand Up @@ -63,7 +63,7 @@ public class TaskAuditLogRepositoryTest {
@Test
public void testGetEntriesEarliestStartTime() {
final List<TaskAuditLogEntry> result =
this.repository.findAllByStartTimeGreaterThan(new Date(0L));
this.repository.findAllByStartTimeGreaterThanOrderByStartTime(new Date(0L));

assertNotNull(result);
assertFalse(result.isEmpty());
Expand All @@ -78,7 +78,7 @@ public void testGetEntriesEarliestStartTime() {
@Test
public void testGetEntriesAfterSpecifiedDate() {
final List<TaskAuditLogEntry> result =
this.repository.findAllByStartTimeGreaterThan(TEST_START_TIMESTAMP);
this.repository.findAllByStartTimeGreaterThanOrderByStartTime(TEST_START_TIMESTAMP);

assertNotNull(result);
assertFalse(result.isEmpty());
Expand Down Expand Up @@ -108,7 +108,7 @@ public void testSaveEntry() {
assertEquals(TEST_DESCRIPTION, result.getDescription());

final List<TaskAuditLogEntry> entries =
repository.findAllByStartTimeGreaterThan(TEST_START_TIMESTAMP);
repository.findAllByStartTimeGreaterThanOrderByStartTime(TEST_START_TIMESTAMP);

assertTrue(entries.contains(result));
}
Expand Down
Expand Up @@ -25,6 +25,7 @@
import org.comixedproject.controller.ComiXedControllerException;
import org.comixedproject.model.tasks.TaskAuditLogEntry;
import org.comixedproject.net.ApiResponse;
import org.comixedproject.net.GetTaskAuditLogResponse;
import org.comixedproject.repositories.tasks.TaskAuditLogRepository;
import org.comixedproject.service.ComiXedServiceException;
import org.comixedproject.service.task.TaskService;
Expand Down Expand Up @@ -55,16 +56,26 @@ public class TaskController {
@GetMapping(value = "/entries/{cutoff}", produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasRole('ADMIN')")
@JsonView(View.TaskAuditLogEntryList.class)
public List<TaskAuditLogEntry> getAllAfterDate(@PathVariable("cutoff") final Long timestamp)
throws ComiXedControllerException {
public ApiResponse<GetTaskAuditLogResponse> getAllAfterDate(
@PathVariable("cutoff") final Long timestamp) throws ComiXedControllerException {
ApiResponse<GetTaskAuditLogResponse> result = new ApiResponse<>();

final Date cutoff = new Date(timestamp);
log.debug("Getting all task audit log entries after: {}", cutoff);

try {
return this.taskService.getAuditLogEntriesAfter(cutoff);
final List<TaskAuditLogEntry> entries = this.taskService.getAuditLogEntriesAfter(cutoff);
result.setResult(new GetTaskAuditLogResponse());
result.getResult().setEntries(entries);
result.getResult().setLatest(entries.get(entries.size() - 1).getStartTime());
result.setSuccess(true);
} catch (ComiXedServiceException error) {
throw new ComiXedControllerException("unable to get task audit log entries", error);
log.error("Failed to load task audit log entries", error);
result.setSuccess(false);
result.setError(error.getMessage());
}

return result;
}

/**
Expand Down
@@ -0,0 +1,33 @@
package org.comixedproject.net;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonView;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
import org.comixedproject.model.tasks.TaskAuditLogEntry;
import org.comixedproject.views.View;

/**
* <code>GetTaskAuditLogResponse</code> represents the response body for a task audit log entry
* request.
*
* @author Darryl L. Pierce
*/
public class GetTaskAuditLogResponse {
@Getter
@Setter
@JsonProperty("entries")
@JsonView(View.TaskAuditLogEntryList.class)
private List<TaskAuditLogEntry> entries = new ArrayList<>();

@Getter
@Setter
@JsonProperty("latest")
@JsonView(View.TaskAuditLogEntryList.class)
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
private Date latest;
}
Expand Up @@ -20,14 +20,17 @@

import static junit.framework.TestCase.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.comixedproject.controller.ComiXedControllerException;
import org.comixedproject.model.tasks.TaskAuditLogEntry;
import org.comixedproject.net.ApiResponse;
import org.comixedproject.net.GetTaskAuditLogResponse;
import org.comixedproject.service.ComiXedServiceException;
import org.comixedproject.service.task.TaskService;
import org.comixedproject.service.user.UserService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
Expand All @@ -43,18 +46,28 @@ public class TaskControllerTest {
@InjectMocks private TaskController taskController;
@Mock private UserService userService;
@Mock private TaskService taskService;
@Mock private List<TaskAuditLogEntry> auditLogEntries;
@Mock private TaskAuditLogEntry taskAuditLogEntry;

private List<TaskAuditLogEntry> auditLogEntries = new ArrayList<>();

@Before
public void setUp() {
this.auditLogEntries.add(taskAuditLogEntry);
Mockito.when(taskAuditLogEntry.getStartTime()).thenReturn(TEST_LAST_UPDATED_DATE);
}

@Test
public void testGetAllEntries() throws ComiXedServiceException, ComiXedControllerException {
Mockito.when(taskService.getAuditLogEntriesAfter(Mockito.any(Date.class)))
.thenReturn(auditLogEntries);

final List<TaskAuditLogEntry> result =
final ApiResponse<GetTaskAuditLogResponse> result =
taskController.getAllAfterDate(TEST_LAST_UPDATED_DATE.getTime());

assertNotNull(result);
assertSame(auditLogEntries, result);
assertTrue(result.isSuccess());
assertSame(auditLogEntries, result.getResult().getEntries());
assertEquals(TEST_LAST_UPDATED_DATE, result.getResult().getLatest());

Mockito.verify(taskService, Mockito.times(1)).getAuditLogEntriesAfter(TEST_LAST_UPDATED_DATE);
}
Expand Down
Expand Up @@ -73,7 +73,7 @@ public List<TaskAuditLogEntry> getAuditLogEntriesAfter(final Date cutoff)
boolean done = false;
long started = System.currentTimeMillis();
while (!done) {
result = this.taskAuditLogRepository.findAllByStartTimeGreaterThan(cutoff);
result = this.taskAuditLogRepository.findAllByStartTimeGreaterThanOrderByStartTime(cutoff);

if (result.isEmpty()) {
log.debug("Waiting for task audit log entries");
Expand Down
Expand Up @@ -60,7 +60,9 @@ public void testGetTaskCount() {

@Test
public void testGetAuditLogEntriesAfter() throws ComiXedServiceException {
Mockito.when(taskAuditLogRepository.findAllByStartTimeGreaterThan(Mockito.any(Date.class)))
Mockito.when(
taskAuditLogRepository.findAllByStartTimeGreaterThanOrderByStartTime(
Mockito.any(Date.class)))
.thenReturn(auditLogEntryList);

final List<TaskAuditLogEntry> result =
Expand All @@ -70,7 +72,7 @@ public void testGetAuditLogEntriesAfter() throws ComiXedServiceException {
assertSame(auditLogEntryList, result);

Mockito.verify(taskAuditLogRepository, Mockito.times(1))
.findAllByStartTimeGreaterThan(TEST_AUDIT_LOG_CUTOFF_DATE);
.findAllByStartTimeGreaterThanOrderByStartTime(TEST_AUDIT_LOG_CUTOFF_DATE);
}

@Test
Expand Down

0 comments on commit 59b3eeb

Please sign in to comment.