1
0
Fork 0

unit tests in da house

This commit is contained in:
Xavier Fontanet 2024-05-25 18:03:58 +02:00
parent d093e0cc35
commit 95e75de815
8 changed files with 108 additions and 18 deletions

14
pom.xml
View File

@ -49,6 +49,20 @@
<version>2.1</version>
</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>
<build>

View File

@ -2,11 +2,15 @@ package tv.mangrana.config;
import tv.mangrana.exception.IncorrectWorkingReferencesException;
import static tv.mangrana.config.ConfigLoader.ProjectConfiguration.TEST_MODE;
public class ConfigLoader extends CommonConfigFileLoader<ConfigLoader.ProjectConfiguration> {
private static final String CONFIG_FILE = "SonarrFixerConfig.yml";
private static ConfigLoader service;
private static boolean isUnitTesting = false;
private ConfigLoader() throws IncorrectWorkingReferencesException {
super(ProjectConfiguration.class);
}
@ -25,6 +29,13 @@ public class ConfigLoader extends CommonConfigFileLoader<ConfigLoader.ProjectCon
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 {
UPLOADS_PATHS,
TEST_MODE

View File

@ -1,5 +1,6 @@
package tv.mangrana.worker;
import tv.mangrana.config.ConfigLoader;
import tv.mangrana.exception.IncorrectWorkingReferencesException;
import tv.mangrana.sonarr.api.schema.queue.Record;
import tv.mangrana.sonarr.api.schema.series.SonarrSerie;
@ -74,6 +75,7 @@ class FailedImportFixer {
return path.toFile().isFile();
}
private boolean isAVideo(Path path) {
if (ConfigLoader.isTestMode()) return true;
return path.toFile().length() > MINIMUM_FILE_SIZE_TO_BE_CONSIDERED_A_VIDEO;
}

View File

@ -6,7 +6,6 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import static tv.mangrana.config.ConfigLoader.ProjectConfiguration.TEST_MODE;
import static tv.mangrana.config.ConfigLoader.ProjectConfiguration.UPLOADS_PATHS;
class FileCopier {
@ -14,7 +13,7 @@ class FileCopier {
try {
createDestinationFolderIfApply(destination);
if (!ConfigLoader.isEnabled(TEST_MODE))
if (!ConfigLoader.isTestMode())
Files.createLink(destination, source);
} catch (IOException e) {
System.out.printf("error when creating hardlink with destination %s, error: %s%n",
@ -27,7 +26,7 @@ class FileCopier {
var destinationFolder = destinationFile.getParent();
if (isTemporaryDestination(destinationFolder) && !Files.exists(destinationFolder)) {
System.out.printf("destination folder %s will be created", destinationFolder);
if (!ConfigLoader.isEnabled(TEST_MODE))
if (!ConfigLoader.isTestMode())
Files.createDirectories(destinationFile);
}
}

View File

@ -3,28 +3,32 @@ package tv.mangrana.worker;
import tv.mangrana.config.ConfigLoader;
import tv.mangrana.exception.IncorrectWorkingReferencesException;
import tv.mangrana.sonarr.Sonarr;
import static tv.mangrana.config.ConfigLoader.ProjectConfiguration.TEST_MODE;
import tv.mangrana.sonarr.api.client.gateway.SonarrApiGateway;
public class MainWorker {
private final QueueFixer queueFixer;
public static void main(String[] args) throws IncorrectWorkingReferencesException {
var worker = new MainWorker();
SonarrApiGateway sonarrApiGateway = configureSonarApiGateway();
var worker = new MainWorker(sonarrApiGateway);
worker.work();
}
private MainWorker() throws IncorrectWorkingReferencesException {
private static SonarrApiGateway configureSonarApiGateway() {
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);
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();
}
}

View File

@ -1,7 +1,6 @@
package tv.mangrana.worker;
import tv.mangrana.config.ConfigLoader;
import tv.mangrana.sonarr.Sonarr;
import tv.mangrana.sonarr.api.client.gateway.SonarrApiGateway;
import tv.mangrana.sonarr.api.schema.queue.Record;
import tv.mangrana.sonarr.api.schema.series.SonarrSerie;
@ -11,16 +10,14 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import static tv.mangrana.config.ConfigLoader.ProjectConfiguration.TEST_MODE;
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.";
private final SonarrApiGateway sonarrApiGateway;
private final FailedImportFixer.Factory fixerFactory;
QueueFixer() {
sonarrApiGateway = Sonarr.api();
QueueFixer(SonarrApiGateway sonarGateway) {
sonarrApiGateway = sonarGateway;
fixerFactory = FailedImportFixer.factory();
}
@ -67,7 +64,7 @@ public class QueueFixer {
private void cleanWorkedElementsFromQueue(List<Record> sonarQueue, List<Record> recordsToFix) {
List<String> workedTitles = mapRecord2Title(recordsToFix);
List<Integer> recordIds2Delete = filterPresentTitlesFromQueue(sonarQueue, workedTitles);
if (!ConfigLoader.isEnabled(TEST_MODE))
if (!ConfigLoader.isTestMode())
sonarrApiGateway.deleteQueueElements(recordIds2Delete);
}

View File

@ -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);
}
}