forked from comixed/comixed
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Issue comixed#137] Add the ConvertComicTaskEncoder class and tests.
- Loading branch information
Showing
2 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
comixed-tasks/src/main/java/org/comixed/task/encoders/ConvertComicTaskEncoder.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,92 @@ | ||
/* | ||
* 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.task.encoders; | ||
|
||
import org.comixed.adaptors.ArchiveType; | ||
import org.comixed.model.library.Comic; | ||
import org.comixed.model.tasks.Task; | ||
import org.comixed.repositories.tasks.TaskRepository; | ||
import org.comixed.task.model.ConvertComicWorkerTask; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.ObjectFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.config.ConfigurableBeanFactory; | ||
import org.springframework.context.annotation.Scope; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Component | ||
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) | ||
public class ConvertComicTaskEncoder extends AbstractTaskEncoder<ConvertComicWorkerTask> { | ||
private final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||
|
||
public static final String RENAME_PAGES = "rename-pages"; | ||
public static final String ARCHIVE_TYPE = "target-archive-type"; | ||
|
||
@Autowired private TaskRepository taskRepository; | ||
@Autowired private ObjectFactory<ConvertComicWorkerTask> convertComicWorkerTaskObjectFactory; | ||
|
||
private Comic comic; | ||
private ArchiveType archiveType; | ||
private boolean renamePages; | ||
|
||
@Override | ||
public Task encode() { | ||
this.logger.debug("Encoding save comic task"); | ||
|
||
Task result = new Task(); | ||
result.setComic(this.comic); | ||
result.setProperty(ARCHIVE_TYPE, this.archiveType.toString()); | ||
result.setProperty(RENAME_PAGES, String.valueOf(this.renamePages)); | ||
|
||
return result; | ||
} | ||
|
||
@Override | ||
@Transactional | ||
public ConvertComicWorkerTask decode(Task task) { | ||
this.logger.debug("Decoding save comic task: id={}", task.getId()); | ||
|
||
ConvertComicWorkerTask result = this.convertComicWorkerTaskObjectFactory.getObject(); | ||
result.setComic(task.getComic()); | ||
ArchiveType targetArchiveType = ArchiveType.forValue(task.getProperty(ARCHIVE_TYPE)); | ||
result.setTargetArchiveType(targetArchiveType); | ||
result.setRenamePages(Boolean.valueOf(task.getProperty(RENAME_PAGES))); | ||
|
||
this.logger.debug("Deleting persisted task"); | ||
this.taskRepository.delete(task); | ||
|
||
return result; | ||
} | ||
|
||
public void setComic(final Comic comic) { | ||
this.logger.debug("Setting comic: id={}", comic.isMissing()); | ||
this.comic = comic; | ||
} | ||
|
||
public void setTargetArchiveType(ArchiveType archiveType) { | ||
this.logger.debug("Setting target archive type: {}", archiveType); | ||
this.archiveType = archiveType; | ||
} | ||
|
||
public void setRenamePages(boolean renamePages) { | ||
this.renamePages = renamePages; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
comixed-tasks/src/test/java/org/comixed/task/encoders/ConvertComicTaskEncoderTest.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,90 @@ | ||
/* | ||
* 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.task.encoders; | ||
|
||
import static junit.framework.TestCase.assertEquals; | ||
import static junit.framework.TestCase.assertNotNull; | ||
import static org.junit.Assert.assertSame; | ||
|
||
import java.util.Random; | ||
import org.comixed.adaptors.ArchiveType; | ||
import org.comixed.model.library.Comic; | ||
import org.comixed.model.tasks.Task; | ||
import org.comixed.repositories.tasks.TaskRepository; | ||
import org.comixed.task.model.ConvertComicWorkerTask; | ||
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; | ||
import org.springframework.beans.factory.ObjectFactory; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class ConvertComicTaskEncoderTest { | ||
private static final ArchiveType TEST_ARCHIVE_TYPE = ArchiveType.CBZ; | ||
private static final Random RANDOM = new Random(); | ||
private static final boolean TEST_RENAME_PAGES = RANDOM.nextBoolean(); | ||
|
||
@InjectMocks private ConvertComicTaskEncoder encoder; | ||
@Mock private ObjectFactory<ConvertComicWorkerTask> convertComicWorkerTaskObjectFactory; | ||
@Mock private ConvertComicWorkerTask convertComicWorkerTask; | ||
@Mock private TaskRepository taskRepository; | ||
@Mock private Comic comic; | ||
@Mock private Task task; | ||
|
||
@Test | ||
public void testEncode() { | ||
encoder.setComic(comic); | ||
encoder.setTargetArchiveType(TEST_ARCHIVE_TYPE); | ||
encoder.setRenamePages(TEST_RENAME_PAGES); | ||
|
||
Task result = encoder.encode(); | ||
|
||
assertNotNull(result); | ||
assertSame(comic, result.getComic()); | ||
assertEquals( | ||
TEST_ARCHIVE_TYPE.toString(), result.getProperty(ConvertComicTaskEncoder.ARCHIVE_TYPE)); | ||
assertEquals( | ||
String.valueOf(TEST_RENAME_PAGES), | ||
result.getProperty(ConvertComicTaskEncoder.RENAME_PAGES)); | ||
} | ||
|
||
@Test | ||
public void testDecode() { | ||
Mockito.when(convertComicWorkerTaskObjectFactory.getObject()) | ||
.thenReturn(convertComicWorkerTask); | ||
Mockito.when(task.getComic()).thenReturn(comic); | ||
Mockito.when(task.getProperty(ConvertComicTaskEncoder.ARCHIVE_TYPE)) | ||
.thenReturn(TEST_ARCHIVE_TYPE.toString()); | ||
Mockito.when(task.getProperty(ConvertComicTaskEncoder.RENAME_PAGES)) | ||
.thenReturn(String.valueOf(TEST_RENAME_PAGES)); | ||
|
||
ConvertComicWorkerTask result = encoder.decode(task); | ||
|
||
assertNotNull(result); | ||
assertSame(convertComicWorkerTask, result); | ||
|
||
Mockito.verify(convertComicWorkerTask, Mockito.times(1)).setComic(comic); | ||
Mockito.verify(convertComicWorkerTask, Mockito.times(1)) | ||
.setTargetArchiveType(TEST_ARCHIVE_TYPE); | ||
Mockito.verify(convertComicWorkerTask, Mockito.times(1)).setRenamePages(TEST_RENAME_PAGES); | ||
Mockito.verify(taskRepository, Mockito.times(1)).delete(task); | ||
} | ||
} |