Skip to content

Commit

Permalink
Refactored the import REST APIs to match the new feature set [#539]
Browse files Browse the repository at this point in the history
  • Loading branch information
mcpierce committed Mar 1, 2021
1 parent 0733145 commit 8a044c6
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 43 deletions.
Expand Up @@ -22,7 +22,7 @@

import org.junit.Test;

public class FileDetailsTest {
public class ComicFileTest {
private static final long TEST_FILE_SIZE = 32767L;
private static final String TEST_WINDOWS_FILENAME =
"C:\\Users\\comixeduser\\Downloads\\MyComicFile.cbz";
Expand All @@ -34,19 +34,19 @@ public class FileDetailsTest {
@Test
public void testCreateInstanceConvertsWindowsFilenames()
throws IllegalArgumentException, IllegalAccessException {
FileDetails fileDetails = new FileDetails(TEST_WINDOWS_FILENAME, TEST_FILE_SIZE);
ComicFile comicFile = new ComicFile(TEST_WINDOWS_FILENAME, TEST_FILE_SIZE);

assertEquals(TEST_ENCODED_WINDOWS_FILENAME, fileDetails.getFilename());
assertEquals(TEST_FILE_SIZE, fileDetails.getSize());
assertEquals(TEST_BASE_FILENAME, fileDetails.getBaseFilename());
assertEquals(TEST_ENCODED_WINDOWS_FILENAME, comicFile.getFilename());
assertEquals(TEST_FILE_SIZE, comicFile.getSize());
assertEquals(TEST_BASE_FILENAME, comicFile.getBaseFilename());
}

@Test
public void testCreateInstance() throws IllegalArgumentException, IllegalAccessException {
FileDetails fileDetails = new FileDetails(TEST_FILENAME, TEST_FILE_SIZE);
ComicFile comicFile = new ComicFile(TEST_FILENAME, TEST_FILE_SIZE);

assertEquals(TEST_FILENAME, fileDetails.getFilename());
assertEquals(TEST_FILE_SIZE, fileDetails.getSize());
assertEquals(TEST_BASE_FILENAME, fileDetails.getBaseFilename());
assertEquals(TEST_FILENAME, comicFile.getFilename());
assertEquals(TEST_FILE_SIZE, comicFile.getSize());
assertEquals(TEST_BASE_FILENAME, comicFile.getBaseFilename());
}
}
Expand Up @@ -19,35 +19,40 @@
package org.comixedproject.model.file;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonView;
import liquibase.util.file.FilenameUtils;
import lombok.Getter;
import org.comixedproject.views.View;

/**
* <code>FileDetails</code> contains the basic information for a comic file during the import
* process.
* <code>ComicFile</code> contains the basic information for a comic file during the import process.
*
* @author Darryl L. Pierce
*/
public class FileDetails {
public class ComicFile {
private static int count = 0;

@JsonProperty("id")
@Getter
@JsonView(View.ComicFileList.class)
private int id = ++count;

@JsonProperty("filename")
@Getter
@JsonView(View.ComicFileList.class)
private String filename;

@JsonProperty("baseFilename")
@Getter
@JsonView(View.ComicFileList.class)
private String baseFilename;

@JsonProperty("size")
@Getter
@JsonView(View.ComicFileList.class)
private long size;

public FileDetails(final String filename, final long size) {
public ComicFile(final String filename, final long size) {
this.filename = filename.replace("\\", "/");
this.baseFilename = FilenameUtils.getName(this.filename);
this.size = size;
Expand Down
Expand Up @@ -66,6 +66,9 @@ public interface LibraryUpdate {}
/** Used when viewing the list of plugins. */
public interface PluginList {}

/** Uses when viewing a list of audit log entries. */
/** Used when viewing a list of audit log entries. */
public interface AuditLogEntryList extends ApiResponse {}

/** Used when viewing a list of comic files. */
public interface ComicFileList extends ApiResponse {}
}
Expand Up @@ -18,6 +18,7 @@

package org.comixedproject.controller.file;

import com.fasterxml.jackson.annotation.JsonView;
import java.io.File;
import java.io.IOException;
import java.util.List;
Expand All @@ -27,16 +28,17 @@
import org.comixedproject.auditlog.AuditableEndpoint;
import org.comixedproject.controller.comic.ComicController;
import org.comixedproject.handlers.ComicFileHandlerException;
import org.comixedproject.model.file.FileDetails;
import org.comixedproject.model.file.ComicFile;
import org.comixedproject.model.net.ApiResponse;
import org.comixedproject.model.net.GetAllComicsUnderRequest;
import org.comixedproject.model.net.ImportComicFilesRequest;
import org.comixedproject.model.net.comicfiles.LoadComicFilesResponse;
import org.comixedproject.service.comic.ComicService;
import org.comixedproject.service.file.FileService;
import org.comixedproject.task.model.QueueComicsWorkerTask;
import org.comixedproject.task.runner.TaskManager;
import org.comixedproject.utils.ComicFileUtils;
import org.json.JSONException;
import org.comixedproject.views.View;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -73,8 +75,9 @@ public class FileController {
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasRole('ADMIN')")
@JsonView(View.ComicFileList.class)
@AuditableEndpoint
public ApiResponse<List<FileDetails>> getAllComicsUnder(
public ApiResponse<LoadComicFilesResponse> loadComicFiles(
@RequestBody() final GetAllComicsUnderRequest request) throws IOException {
String directory = request.getDirectory();
Integer maximum = request.getMaximum();
Expand All @@ -84,11 +87,11 @@ public ApiResponse<List<FileDetails>> getAllComicsUnder(
directory,
maximum > 0 ? maximum : "UNLIMITED");

return new ApiResponse<List<FileDetails>>(
this.fileService.getAllComicsUnder(directory, maximum));
return new ApiResponse<>(
new LoadComicFilesResponse(this.fileService.getAllComicsUnder(directory, maximum)));
}

private void getAllFilesUnder(File root, List<FileDetails> result) throws IOException {
private void getAllFilesUnder(File root, List<ComicFile> result) throws IOException {
for (File file : root.listFiles()) {
if (file.isDirectory()) {
log.debug("Searching directory: " + file.getAbsolutePath());
Expand All @@ -98,7 +101,7 @@ private void getAllFilesUnder(File root, List<FileDetails> result) throws IOExce
if (ComicFileUtils.isComicFile(file)
&& (this.comicService.findByFilename(file.getCanonicalPath()) == null)) {
log.debug("Adding file: " + file.getCanonicalPath());
result.add(new FileDetails(file.getCanonicalPath(), file.length()));
result.add(new ComicFile(file.getCanonicalPath(), file.length()));
}
}
}
Expand Down
@@ -0,0 +1,38 @@
/*
* 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.model.net.comicfiles;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonView;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.comixedproject.model.file.ComicFile;
import org.comixedproject.views.View;

@AllArgsConstructor
public class LoadComicFilesResponse {
@JsonProperty("files")
@Getter
@Setter
@JsonView(View.ComicFileList.class)
private List<ComicFile> files = new ArrayList<>();
}
Expand Up @@ -27,10 +27,11 @@
import java.util.Random;
import org.comixedproject.adaptors.archive.ArchiveAdaptorException;
import org.comixedproject.handlers.ComicFileHandlerException;
import org.comixedproject.model.file.FileDetails;
import org.comixedproject.model.file.ComicFile;
import org.comixedproject.model.net.ApiResponse;
import org.comixedproject.model.net.GetAllComicsUnderRequest;
import org.comixedproject.model.net.ImportComicFilesRequest;
import org.comixedproject.model.net.comicfiles.LoadComicFilesResponse;
import org.comixedproject.service.file.FileService;
import org.comixedproject.task.model.QueueComicsWorkerTask;
import org.comixedproject.task.model.WorkerTask;
Expand Down Expand Up @@ -68,7 +69,7 @@ public class FileControllerTest {

@InjectMocks private FileController controller;
@Mock private FileService fileService;
@Mock private List<FileDetails> fileDetailsList;
@Mock private List<ComicFile> comicFileList;
@Mock private ObjectFactory<QueueComicsWorkerTask> queueComicsWorkerTaskObjectFactory;
@Mock private QueueComicsWorkerTask queueComicsWorkerTask;
@Mock private TaskManager taskManager;
Expand All @@ -88,27 +89,27 @@ public void testGetImportFileCover() throws ArchiveAdaptorException, ComicFileHa
@Test
public void testGetAllComicsUnderNoLimit() throws IOException, JSONException {
Mockito.when(fileService.getAllComicsUnder(Mockito.anyString(), Mockito.anyInt()))
.thenReturn(fileDetailsList);
.thenReturn(comicFileList);

final ApiResponse<List<FileDetails>> response =
controller.getAllComicsUnder(new GetAllComicsUnderRequest(TEST_DIRECTORY, TEST_NO_LIMIT));
final ApiResponse<LoadComicFilesResponse> response =
controller.loadComicFiles(new GetAllComicsUnderRequest(TEST_DIRECTORY, TEST_NO_LIMIT));

assertNotNull(response);
assertSame(fileDetailsList, response.getResult());
assertSame(comicFileList, response.getResult().getFiles());

Mockito.verify(fileService, Mockito.times(1)).getAllComicsUnder(TEST_DIRECTORY, TEST_NO_LIMIT);
}

@Test
public void testGetAllComicsUnder() throws IOException, JSONException {
Mockito.when(fileService.getAllComicsUnder(Mockito.anyString(), Mockito.anyInt()))
.thenReturn(fileDetailsList);
.thenReturn(comicFileList);

final ApiResponse<List<FileDetails>> response =
controller.getAllComicsUnder(new GetAllComicsUnderRequest(TEST_DIRECTORY, TEST_LIMIT));
final ApiResponse<LoadComicFilesResponse> response =
controller.loadComicFiles(new GetAllComicsUnderRequest(TEST_DIRECTORY, TEST_LIMIT));

assertNotNull(response);
assertSame(fileDetailsList, response.getResult());
assertSame(comicFileList, response.getResult().getFiles());

Mockito.verify(fileService, Mockito.times(1)).getAllComicsUnder(TEST_DIRECTORY, TEST_LIMIT);
}
Expand Down
Expand Up @@ -28,7 +28,7 @@
import org.comixedproject.handlers.ComicFileHandler;
import org.comixedproject.handlers.ComicFileHandlerException;
import org.comixedproject.model.comic.Comic;
import org.comixedproject.model.file.FileDetails;
import org.comixedproject.model.file.ComicFile;
import org.comixedproject.model.tasks.TaskType;
import org.comixedproject.repositories.comic.ComicRepository;
import org.comixedproject.service.task.TaskService;
Expand Down Expand Up @@ -70,12 +70,12 @@ public byte[] getImportFileCover(final String comicArchive)
return result;
}

public List<FileDetails> getAllComicsUnder(final String rootDirectory, final Integer maximum)
public List<ComicFile> getAllComicsUnder(final String rootDirectory, final Integer maximum)
throws IOException {
log.debug("Getting comics below root: {}", rootDirectory);

final File rootFile = new File(rootDirectory);
final List<FileDetails> result = new ArrayList<>();
final List<ComicFile> result = new ArrayList<>();

if (rootFile.exists()) {
if (rootFile.isDirectory()) {
Expand All @@ -91,8 +91,7 @@ public List<FileDetails> getAllComicsUnder(final String rootDirectory, final Int
}

private void loadFilesUnder(
final List<FileDetails> files, final File directory, final Integer maximum)
throws IOException {
final List<ComicFile> files, final File directory, final Integer maximum) throws IOException {
log.debug("Loading files in directory: {}", directory);
if (directory.listFiles() != null) {
for (File file : directory.listFiles()) {
Expand All @@ -109,7 +108,7 @@ private void loadFilesUnder(

log.debug("Adding file: {} ({} bytes)", file.getAbsolutePath(), file.length());

files.add(new FileDetails(filePath, fileSize));
files.add(new ComicFile(filePath, fileSize));
}
}
}
Expand Down
Expand Up @@ -28,7 +28,7 @@
import org.comixedproject.handlers.ComicFileHandler;
import org.comixedproject.handlers.ComicFileHandlerException;
import org.comixedproject.model.comic.Comic;
import org.comixedproject.model.file.FileDetails;
import org.comixedproject.model.file.ComicFile;
import org.comixedproject.repositories.comic.ComicRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -103,7 +103,7 @@ public void testGetImportFileCover() throws ComicFileHandlerException, ArchiveAd

@Test
public void testGetAllComicsUnderInvalidDirectory() throws IOException {
final List<FileDetails> result =
final List<ComicFile> result =
service.getAllComicsUnder(TEST_ROOT_DIRECTORY + "/nonexistent", TEST_LIMIT);

assertNotNull(result);
Expand All @@ -112,7 +112,7 @@ public void testGetAllComicsUnderInvalidDirectory() throws IOException {

@Test
public void testGetAllComicsUnderWithFileSupplied() throws IOException {
final List<FileDetails> result = service.getAllComicsUnder(TEST_COMIC_ARCHIVE, TEST_LIMIT);
final List<ComicFile> result = service.getAllComicsUnder(TEST_COMIC_ARCHIVE, TEST_LIMIT);

assertNotNull(result);
assertTrue(result.isEmpty());
Expand All @@ -122,7 +122,7 @@ public void testGetAllComicsUnderWithFileSupplied() throws IOException {
public void testGetAllComicsAlreadyImported() throws IOException {
Mockito.when(comicRepository.findByFilename(Mockito.anyString())).thenReturn(comic);

final List<FileDetails> result = service.getAllComicsUnder(TEST_ROOT_DIRECTORY, TEST_LIMIT);
final List<ComicFile> result = service.getAllComicsUnder(TEST_ROOT_DIRECTORY, TEST_LIMIT);

assertNotNull(result);
assertTrue(result.isEmpty());
Expand All @@ -133,7 +133,7 @@ public void testGetAllComicsAlreadyImported() throws IOException {

@Test
public void testGetAllComicsUnderWithLimit() throws IOException {
final List<FileDetails> result = service.getAllComicsUnder(TEST_ROOT_DIRECTORY, TEST_LIMIT);
final List<ComicFile> result = service.getAllComicsUnder(TEST_ROOT_DIRECTORY, TEST_LIMIT);

assertNotNull(result);
assertFalse(result.isEmpty());
Expand All @@ -142,7 +142,7 @@ public void testGetAllComicsUnderWithLimit() throws IOException {

@Test
public void testGetAllComicsUnder() throws IOException {
final List<FileDetails> result = service.getAllComicsUnder(TEST_ROOT_DIRECTORY, TEST_NO_LIMIT);
final List<ComicFile> result = service.getAllComicsUnder(TEST_ROOT_DIRECTORY, TEST_NO_LIMIT);

assertNotNull(result);
assertFalse(result.isEmpty());
Expand Down

0 comments on commit 8a044c6

Please sign in to comment.