forked from comixed/comixed
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added REST APIs for retrieving the list of task log entries [comixed#413
- Loading branch information
Showing
10 changed files
with
296 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
comixed-rest-api/src/main/java/org/comixed/controller/ComiXedControllerException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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.comixed.controller; | ||
|
||
/** | ||
* <code>ComiXedControllerException</code> is throw when an error occurs during the processing of a | ||
* REST API request. | ||
* | ||
* @author Darryl L. Pierce | ||
*/ | ||
public class ComiXedControllerException extends Exception { | ||
public ComiXedControllerException(final String message, final Exception cause) { | ||
super(message, cause); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
comixed-rest-api/src/main/java/org/comixed/controller/tasks/TaskController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* 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.comixed.controller.tasks; | ||
|
||
import com.fasterxml.jackson.annotation.JsonView; | ||
import java.util.Date; | ||
import java.util.List; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.comixed.controller.ComiXedControllerException; | ||
import org.comixed.model.tasks.TaskAuditLogEntry; | ||
import org.comixed.repositories.tasks.TaskAuditLogRepository; | ||
import org.comixed.service.ComiXedServiceException; | ||
import org.comixed.service.task.TaskService; | ||
import org.comixed.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; | ||
|
||
/** | ||
* <code>TaskController</code> provides REST APIs for interacting with the tasks system. | ||
* | ||
* @author Darryl L. Pierce | ||
*/ | ||
@RestController | ||
@RequestMapping(value = "/api/tasks") | ||
@Log4j2 | ||
public class TaskController { | ||
@Autowired private TaskAuditLogRepository taskAuditLogRepository; | ||
@Autowired private TaskService taskService; | ||
|
||
/** | ||
* Retrieve the list of log entries after the cutoff time. | ||
* | ||
* @param cutoff the cutoff | ||
* @return the log entries | ||
*/ | ||
@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 { | ||
final Date cutoff = new Date(timestamp); | ||
log.debug("Getting all task audit log entries after: {}", cutoff); | ||
|
||
try { | ||
return this.taskService.getAuditLogEntriesAfter(cutoff); | ||
} catch (ComiXedServiceException error) { | ||
throw new ComiXedControllerException("unable to get task audit log entries", error); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
comixed-rest-api/src/test/java/org/comixed/controller/tasks/TaskControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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.comixed.controller.tasks; | ||
|
||
import static junit.framework.TestCase.assertNotNull; | ||
import static junit.framework.TestCase.assertSame; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
import org.comixed.controller.ComiXedControllerException; | ||
import org.comixed.model.tasks.TaskAuditLogEntry; | ||
import org.comixed.service.ComiXedServiceException; | ||
import org.comixed.service.task.TaskService; | ||
import org.comixed.service.user.UserService; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class TaskControllerTest { | ||
private static final Date TEST_LAST_UPDATED_DATE = new Date(); | ||
private static final String TEST_USER_EMAIL = "user@domain.tld"; | ||
|
||
@InjectMocks private TaskController taskController; | ||
@Mock private UserService userService; | ||
@Mock private TaskService taskService; | ||
@Mock private List<TaskAuditLogEntry> auditLogEntries; | ||
|
||
@Test | ||
public void testGetAllEntries() throws ComiXedServiceException, ComiXedControllerException { | ||
Mockito.when(taskService.getAuditLogEntriesAfter(Mockito.any(Date.class))) | ||
.thenReturn(auditLogEntries); | ||
|
||
final List<TaskAuditLogEntry> result = | ||
taskController.getAllAfterDate(TEST_LAST_UPDATED_DATE.getTime()); | ||
|
||
assertNotNull(result); | ||
assertSame(auditLogEntries, result); | ||
|
||
Mockito.verify(taskService, Mockito.times(1)).getAuditLogEntriesAfter(TEST_LAST_UPDATED_DATE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.