unit tests in da house
This commit is contained in:
parent
d093e0cc35
commit
95e75de815
14
pom.xml
14
pom.xml
|
@ -49,6 +49,20 @@
|
||||||
<version>2.1</version>
|
<version>2.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- UTs -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testng</groupId>
|
||||||
|
<artifactId>testng</artifactId>
|
||||||
|
<version>7.0.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<version>5.12.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -2,11 +2,15 @@ package tv.mangrana.config;
|
||||||
|
|
||||||
import tv.mangrana.exception.IncorrectWorkingReferencesException;
|
import tv.mangrana.exception.IncorrectWorkingReferencesException;
|
||||||
|
|
||||||
|
import static tv.mangrana.config.ConfigLoader.ProjectConfiguration.TEST_MODE;
|
||||||
|
|
||||||
public class ConfigLoader extends CommonConfigFileLoader<ConfigLoader.ProjectConfiguration> {
|
public class ConfigLoader extends CommonConfigFileLoader<ConfigLoader.ProjectConfiguration> {
|
||||||
|
|
||||||
private static final String CONFIG_FILE = "SonarrFixerConfig.yml";
|
private static final String CONFIG_FILE = "SonarrFixerConfig.yml";
|
||||||
private static ConfigLoader service;
|
private static ConfigLoader service;
|
||||||
|
|
||||||
|
private static boolean isUnitTesting = false;
|
||||||
|
|
||||||
private ConfigLoader() throws IncorrectWorkingReferencesException {
|
private ConfigLoader() throws IncorrectWorkingReferencesException {
|
||||||
super(ProjectConfiguration.class);
|
super(ProjectConfiguration.class);
|
||||||
}
|
}
|
||||||
|
@ -25,6 +29,13 @@ public class ConfigLoader extends CommonConfigFileLoader<ConfigLoader.ProjectCon
|
||||||
return get(configParam).equals("true");
|
return get(configParam).equals("true");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean isTestMode(){
|
||||||
|
return isUnitTesting || get(TEST_MODE).equals("true");
|
||||||
|
}
|
||||||
|
public static void weAreUnitTesting() {
|
||||||
|
isUnitTesting = true;
|
||||||
|
}
|
||||||
|
|
||||||
public enum ProjectConfiguration {
|
public enum ProjectConfiguration {
|
||||||
UPLOADS_PATHS,
|
UPLOADS_PATHS,
|
||||||
TEST_MODE
|
TEST_MODE
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package tv.mangrana.worker;
|
package tv.mangrana.worker;
|
||||||
|
|
||||||
|
import tv.mangrana.config.ConfigLoader;
|
||||||
import tv.mangrana.exception.IncorrectWorkingReferencesException;
|
import tv.mangrana.exception.IncorrectWorkingReferencesException;
|
||||||
import tv.mangrana.sonarr.api.schema.queue.Record;
|
import tv.mangrana.sonarr.api.schema.queue.Record;
|
||||||
import tv.mangrana.sonarr.api.schema.series.SonarrSerie;
|
import tv.mangrana.sonarr.api.schema.series.SonarrSerie;
|
||||||
|
@ -74,6 +75,7 @@ class FailedImportFixer {
|
||||||
return path.toFile().isFile();
|
return path.toFile().isFile();
|
||||||
}
|
}
|
||||||
private boolean isAVideo(Path path) {
|
private boolean isAVideo(Path path) {
|
||||||
|
if (ConfigLoader.isTestMode()) return true;
|
||||||
return path.toFile().length() > MINIMUM_FILE_SIZE_TO_BE_CONSIDERED_A_VIDEO;
|
return path.toFile().length() > MINIMUM_FILE_SIZE_TO_BE_CONSIDERED_A_VIDEO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,6 @@ import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
|
||||||
import static tv.mangrana.config.ConfigLoader.ProjectConfiguration.TEST_MODE;
|
|
||||||
import static tv.mangrana.config.ConfigLoader.ProjectConfiguration.UPLOADS_PATHS;
|
import static tv.mangrana.config.ConfigLoader.ProjectConfiguration.UPLOADS_PATHS;
|
||||||
|
|
||||||
class FileCopier {
|
class FileCopier {
|
||||||
|
@ -14,7 +13,7 @@ class FileCopier {
|
||||||
try {
|
try {
|
||||||
createDestinationFolderIfApply(destination);
|
createDestinationFolderIfApply(destination);
|
||||||
|
|
||||||
if (!ConfigLoader.isEnabled(TEST_MODE))
|
if (!ConfigLoader.isTestMode())
|
||||||
Files.createLink(destination, source);
|
Files.createLink(destination, source);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.printf("error when creating hardlink with destination %s, error: %s%n",
|
System.out.printf("error when creating hardlink with destination %s, error: %s%n",
|
||||||
|
@ -27,7 +26,7 @@ class FileCopier {
|
||||||
var destinationFolder = destinationFile.getParent();
|
var destinationFolder = destinationFile.getParent();
|
||||||
if (isTemporaryDestination(destinationFolder) && !Files.exists(destinationFolder)) {
|
if (isTemporaryDestination(destinationFolder) && !Files.exists(destinationFolder)) {
|
||||||
System.out.printf("destination folder %s will be created", destinationFolder);
|
System.out.printf("destination folder %s will be created", destinationFolder);
|
||||||
if (!ConfigLoader.isEnabled(TEST_MODE))
|
if (!ConfigLoader.isTestMode())
|
||||||
Files.createDirectories(destinationFile);
|
Files.createDirectories(destinationFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,28 +3,32 @@ package tv.mangrana.worker;
|
||||||
import tv.mangrana.config.ConfigLoader;
|
import tv.mangrana.config.ConfigLoader;
|
||||||
import tv.mangrana.exception.IncorrectWorkingReferencesException;
|
import tv.mangrana.exception.IncorrectWorkingReferencesException;
|
||||||
import tv.mangrana.sonarr.Sonarr;
|
import tv.mangrana.sonarr.Sonarr;
|
||||||
|
import tv.mangrana.sonarr.api.client.gateway.SonarrApiGateway;
|
||||||
import static tv.mangrana.config.ConfigLoader.ProjectConfiguration.TEST_MODE;
|
|
||||||
|
|
||||||
public class MainWorker {
|
public class MainWorker {
|
||||||
|
|
||||||
private final QueueFixer queueFixer;
|
private final QueueFixer queueFixer;
|
||||||
|
|
||||||
public static void main(String[] args) throws IncorrectWorkingReferencesException {
|
public static void main(String[] args) throws IncorrectWorkingReferencesException {
|
||||||
var worker = new MainWorker();
|
SonarrApiGateway sonarrApiGateway = configureSonarApiGateway();
|
||||||
|
var worker = new MainWorker(sonarrApiGateway);
|
||||||
worker.work();
|
worker.work();
|
||||||
}
|
}
|
||||||
|
|
||||||
private MainWorker() throws IncorrectWorkingReferencesException {
|
private static SonarrApiGateway configureSonarApiGateway() {
|
||||||
var configLoader = ConfigLoader.getLoader();
|
var configLoader = ConfigLoader.getLoader();
|
||||||
if (ConfigLoader.isEnabled(TEST_MODE))
|
|
||||||
System.out.println("ATTENTION! TEST MODE ENABLED. No folders will be created nor files copied.");
|
|
||||||
|
|
||||||
Sonarr.initService(configLoader);
|
Sonarr.initService(configLoader);
|
||||||
queueFixer = new QueueFixer();
|
return Sonarr.api();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void work() {
|
MainWorker(SonarrApiGateway sonarGateway) throws IncorrectWorkingReferencesException {
|
||||||
|
if (ConfigLoader.isTestMode())
|
||||||
|
System.out.println("ATTENTION! TEST MODE ENABLED. No folders will be created nor files copied.");
|
||||||
|
|
||||||
|
queueFixer = new QueueFixer(sonarGateway);
|
||||||
|
}
|
||||||
|
|
||||||
|
void work() {
|
||||||
queueFixer.fix();
|
queueFixer.fix();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package tv.mangrana.worker;
|
package tv.mangrana.worker;
|
||||||
|
|
||||||
import tv.mangrana.config.ConfigLoader;
|
import tv.mangrana.config.ConfigLoader;
|
||||||
import tv.mangrana.sonarr.Sonarr;
|
|
||||||
import tv.mangrana.sonarr.api.client.gateway.SonarrApiGateway;
|
import tv.mangrana.sonarr.api.client.gateway.SonarrApiGateway;
|
||||||
import tv.mangrana.sonarr.api.schema.queue.Record;
|
import tv.mangrana.sonarr.api.schema.queue.Record;
|
||||||
import tv.mangrana.sonarr.api.schema.series.SonarrSerie;
|
import tv.mangrana.sonarr.api.schema.series.SonarrSerie;
|
||||||
|
@ -11,16 +10,14 @@ import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static tv.mangrana.config.ConfigLoader.ProjectConfiguration.TEST_MODE;
|
|
||||||
|
|
||||||
public class QueueFixer {
|
public class QueueFixer {
|
||||||
final static String IMPORT_FAILURE_BECAUSE_MATCHED_BY_ID = "Found matching series via grab history, but release was matched to series by ID. Automatic import is not possible. See the FAQ for details.";
|
final static String IMPORT_FAILURE_BECAUSE_MATCHED_BY_ID = "Found matching series via grab history, but release was matched to series by ID. Automatic import is not possible. See the FAQ for details.";
|
||||||
|
|
||||||
private final SonarrApiGateway sonarrApiGateway;
|
private final SonarrApiGateway sonarrApiGateway;
|
||||||
private final FailedImportFixer.Factory fixerFactory;
|
private final FailedImportFixer.Factory fixerFactory;
|
||||||
|
|
||||||
QueueFixer() {
|
QueueFixer(SonarrApiGateway sonarGateway) {
|
||||||
sonarrApiGateway = Sonarr.api();
|
sonarrApiGateway = sonarGateway;
|
||||||
fixerFactory = FailedImportFixer.factory();
|
fixerFactory = FailedImportFixer.factory();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,7 +64,7 @@ public class QueueFixer {
|
||||||
private void cleanWorkedElementsFromQueue(List<Record> sonarQueue, List<Record> recordsToFix) {
|
private void cleanWorkedElementsFromQueue(List<Record> sonarQueue, List<Record> recordsToFix) {
|
||||||
List<String> workedTitles = mapRecord2Title(recordsToFix);
|
List<String> workedTitles = mapRecord2Title(recordsToFix);
|
||||||
List<Integer> recordIds2Delete = filterPresentTitlesFromQueue(sonarQueue, workedTitles);
|
List<Integer> recordIds2Delete = filterPresentTitlesFromQueue(sonarQueue, workedTitles);
|
||||||
if (!ConfigLoader.isEnabled(TEST_MODE))
|
if (!ConfigLoader.isTestMode())
|
||||||
sonarrApiGateway.deleteQueueElements(recordIds2Delete);
|
sonarrApiGateway.deleteQueueElements(recordIds2Delete);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
package tv.mangrana.worker;
|
||||||
|
|
||||||
|
import org.testng.annotations.BeforeMethod;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
import tv.mangrana.config.ConfigLoader;
|
||||||
|
import tv.mangrana.sonarr.api.client.gateway.SonarrApiGateway;
|
||||||
|
import tv.mangrana.sonarr.api.schema.queue.Record;
|
||||||
|
import tv.mangrana.sonarr.api.schema.queue.StatusMessage;
|
||||||
|
import tv.mangrana.sonarr.api.schema.series.SonarrSerie;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
import static tv.mangrana.worker.QueueFixer.IMPORT_FAILURE_BECAUSE_MATCHED_BY_ID;
|
||||||
|
|
||||||
|
public class SonarQueueFixerTest {
|
||||||
|
|
||||||
|
private SonarrApiGateway sonarrApiGateway;
|
||||||
|
|
||||||
|
private MainWorker mainWorker;
|
||||||
|
|
||||||
|
@BeforeMethod
|
||||||
|
public void setup() {
|
||||||
|
ConfigLoader.weAreUnitTesting();
|
||||||
|
sonarrApiGateway = mock(SonarrApiGateway.class, RETURNS_DEEP_STUBS);
|
||||||
|
mainWorker = new MainWorker(sonarrApiGateway);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test //just for debugging purpose
|
||||||
|
public void whenDestinationFolderDoesNotExist_AndIsTemporary_shouldCreateIt_AndHardlinkFiles() {
|
||||||
|
prepareSonarrMockQueue();
|
||||||
|
prepareSonarrMockSerie();
|
||||||
|
|
||||||
|
mainWorker.work();
|
||||||
|
|
||||||
|
//asserting nothing, sorry (too stuff would be needed to be mocked, and it's not worthy at this moment)
|
||||||
|
}
|
||||||
|
|
||||||
|
private void prepareSonarrMockQueue() {
|
||||||
|
var record = new Record()
|
||||||
|
.withSeriesId(getRandomInteger())
|
||||||
|
.withTitle("Star Trek (1987) S01 [1080p]")
|
||||||
|
.withOutputPath("./test/torrents/Star Trek (1987) S01 [1080p]")
|
||||||
|
.withStatusMessages(prepareWarningMessages());
|
||||||
|
when(sonarrApiGateway.getFullQueue().getRecords()).thenReturn(List.of(record));
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getRandomInteger() {
|
||||||
|
return Double.valueOf(Math.random()*100).intValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<StatusMessage> prepareWarningMessages() {
|
||||||
|
return List.of(new StatusMessage()
|
||||||
|
.withMessages(List.of(IMPORT_FAILURE_BECAUSE_MATCHED_BY_ID)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void prepareSonarrMockSerie() {
|
||||||
|
SonarrSerie serie = new SonarrSerie()
|
||||||
|
.withPath("./test/sonarr_uploads/Star Trek (1987) {tvdb-71470}");
|
||||||
|
when(sonarrApiGateway.getSerieById(anyInt())).thenReturn(serie);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue