Skip to content

Commit

Permalink
Added the ability to clear the task audit log [comixed#424]
Browse files Browse the repository at this point in the history
 * Added a REST API and service method.
 * Added a standardized response type for API calls.
  • Loading branch information
mcpierce committed Aug 14, 2020
1 parent a0017f0 commit a90c520
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 6 deletions.
Expand Up @@ -25,6 +25,9 @@
* @author Darryl L. Pierce
*/
public class View {
/** The parent type for all views. */
public interface ApiResponse {}

/** Show full details on a single comic. */
public interface ComicDetails extends ComicList {}

Expand Down
Expand Up @@ -24,17 +24,15 @@
import lombok.extern.log4j.Log4j2;
import org.comixedproject.controller.ComiXedControllerException;
import org.comixedproject.model.tasks.TaskAuditLogEntry;
import org.comixedproject.net.ApiResponse;
import org.comixedproject.repositories.tasks.TaskAuditLogRepository;
import org.comixedproject.service.ComiXedServiceException;
import org.comixedproject.service.task.TaskService;
import org.comixedproject.views.View;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

/**
* <code>TaskController</code> provides REST APIs for interacting with the tasks system.
Expand Down Expand Up @@ -68,4 +66,28 @@ public List<TaskAuditLogEntry> getAllAfterDate(@PathVariable("cutoff") final Lon
throw new ComiXedControllerException("unable to get task audit log entries", error);
}
}

/**
* Endpoint for clearing the task log.
*
* @return the success of the operation
*/
@DeleteMapping(value = "/entries", produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasRole('ADMIN')")
@JsonView(View.ApiResponse.class)
public ApiResponse<Void> clearTaskAuditLog() {
log.debug("Clearing task audit log");
final ApiResponse<Void> response = new ApiResponse<>();

try {
this.taskService.clearTaskAuditLog();
response.setSuccess(true);
} catch (Exception error) {
log.error("Failed to clear audit log", error);
response.setSuccess(false);
response.setError(error.getMessage());
}

return response;
}
}
@@ -0,0 +1,51 @@
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2020, The ComiXed Project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/

package org.comixedproject.net;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonView;
import lombok.Getter;
import lombok.Setter;
import org.comixedproject.views.View;

/**
* <code>ApiResponse</code> represents a standardized response object for a REST API call.
*
* @param <T> the response data
* @author Darryl L. Pierce
*/
public class ApiResponse<T> {
@Getter
@Setter
@JsonProperty("success")
@JsonView(View.ApiResponse.class)
private boolean success;

@Getter
@Setter
@JsonProperty("error")
@JsonView(View.ApiResponse.class)
private String error;

@Getter
@Setter
@JsonProperty("result")
@JsonView(View.ApiResponse.class)
private T result;
}
Expand Up @@ -18,13 +18,13 @@

package org.comixedproject.controller.tasks;

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

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.service.ComiXedServiceException;
import org.comixedproject.service.task.TaskService;
import org.comixedproject.service.user.UserService;
Expand Down Expand Up @@ -58,4 +58,26 @@ public void testGetAllEntries() throws ComiXedServiceException, ComiXedControlle

Mockito.verify(taskService, Mockito.times(1)).getAuditLogEntriesAfter(TEST_LAST_UPDATED_DATE);
}

@Test
public void testClearTaskLogServiceRaisesException() {
Mockito.doThrow(new RuntimeException("Test")).when(taskService).clearTaskAuditLog();

final ApiResponse<Void> result = taskController.clearTaskAuditLog();

assertNotNull(result);
assertFalse(result.isSuccess());
assertNotNull(result.getError());
}

@Test
public void testClearTaskLog() {
Mockito.doNothing().when(taskService).clearTaskAuditLog();

final ApiResponse<Void> result = taskController.clearTaskAuditLog();

assertNotNull(result);
assertTrue(result.isSuccess());
assertNull(result.getError());
}
}
Expand Up @@ -123,6 +123,7 @@ public Task save(final Task task) {
* @return the tasks
*/
public List<Task> getTasksToRun(final int max) {
log.debug("Fetching next task to run");
return this.taskRepository.getTasksToRun(PageRequest.of(0, max));
}

Expand All @@ -133,6 +134,14 @@ public List<Task> getTasksToRun(final int max) {
*/
@Transactional
public void saveAuditLogEntry(final TaskAuditLogEntry entry) {
log.debug("Adding task audit log entry: {}", entry.getDescription());
this.taskAuditLogRepository.save(entry);
}

/** Clears the task audit log. */
@Transactional
public void clearTaskAuditLog() {
log.debug("Clearing task audit log");
this.taskAuditLogRepository.deleteAll();
}
}
Expand Up @@ -110,4 +110,13 @@ public void testGetTasksToRun() {
Mockito.verify(taskRepository, Mockito.times(1))
.getTasksToRun(pageRequestArgumentCaptor.getValue());
}

@Test
public void testClearTaskAuditLog() {
Mockito.doNothing().when(taskAuditLogRepository).deleteAll();

taskService.clearTaskAuditLog();

Mockito.verify(taskAuditLogRepository, Mockito.times(1)).deleteAll();
}
}

0 comments on commit a90c520

Please sign in to comment.