Skip to content

Commit

Permalink
[Issue comixed#137] Add the ConvertComicTaskEncoder class and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcpierce committed Mar 15, 2020
1 parent 4ac2e97 commit f90f6d3
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 0 deletions.
@@ -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;
}
}
@@ -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);
}
}

0 comments on commit f90f6d3

Please sign in to comment.