diff --git a/src/main/java/com/sitepark/ies/publisher/channel/sync/service/analyser/TranslationsDirectory.java b/src/main/java/com/sitepark/ies/publisher/channel/sync/service/analyser/TranslationsDirectory.java index 4b77ce2..9e3ddd5 100644 --- a/src/main/java/com/sitepark/ies/publisher/channel/sync/service/analyser/TranslationsDirectory.java +++ b/src/main/java/com/sitepark/ies/publisher/channel/sync/service/analyser/TranslationsDirectory.java @@ -2,11 +2,10 @@ import com.sitepark.ies.publisher.channel.sync.domain.entity.AnalyserResult; import com.sitepark.ies.publisher.channel.sync.domain.entity.AnalyserResultFactory; +import com.sitepark.ies.publisher.channel.sync.domain.entity.Publication; import com.sitepark.ies.publisher.channel.sync.domain.entity.PublishedPath; import com.sitepark.ies.publisher.channel.sync.domain.entity.ResultType; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; public class TranslationsDirectory implements PublishedPathAnalyser { @@ -24,15 +23,12 @@ public AnalyserResult analyse(AnalyserContext ctx, PublishedPath path) throws IO return AnalyserResult.OK; } - Path parent = path.absolutePath().getParent(); - if (parent == null) { - throw new IllegalArgumentException("The parent path must not be null"); - } - String translationBaseName = name.substring(0, name.lastIndexOf(SUFFIX)); - Path translationBase = parent.resolve(translationBaseName); - if (Files.exists(translationBase)) { - return AnalyserResult.OK_AND_INTERRUPT; + + for (Publication p : ctx.getPublicationDirectory().getPublications(translationBaseName)) { + if (p.isPublished()) { + return AnalyserResult.OK_AND_INTERRUPT; + } } AnalyserResultFactory resultFactory = ctx.getAnalyserResultFactory(); diff --git a/src/test/java/com/sitepark/ies/publisher/channel/sync/service/analyser/TranslationsDirectoryTest.java b/src/test/java/com/sitepark/ies/publisher/channel/sync/service/analyser/TranslationsDirectoryTest.java index 705ed09..9174509 100644 --- a/src/test/java/com/sitepark/ies/publisher/channel/sync/service/analyser/TranslationsDirectoryTest.java +++ b/src/test/java/com/sitepark/ies/publisher/channel/sync/service/analyser/TranslationsDirectoryTest.java @@ -1,15 +1,17 @@ package com.sitepark.ies.publisher.channel.sync.service.analyser; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import com.sitepark.ies.publisher.channel.sync.domain.entity.AnalyserResult; +import com.sitepark.ies.publisher.channel.sync.domain.entity.Publication; +import com.sitepark.ies.publisher.channel.sync.domain.entity.PublicationDirectory; import com.sitepark.ies.publisher.channel.sync.domain.entity.PublishedPath; import com.sitepark.ies.publisher.channel.sync.domain.entity.ResultType; import java.io.IOException; import java.nio.file.Path; +import java.util.Arrays; import org.junit.jupiter.api.Test; class TranslationsDirectoryTest extends AnalyserTest { @@ -33,6 +35,16 @@ void testWhenPublishedPathIsFile() throws IOException { void testWithDirectoryNonTranslationsSuffix() throws IOException { AnalyserContext ctx = this.mockAnalyserContext(); + PublicationDirectory directory = mock(); + when(ctx.getPublicationDirectory()).thenReturn(directory); + + Publication depublishedPublication = mock(); + when(depublishedPublication.isPublished()).thenReturn(false); + Publication publishedPublication = mock(); + when(publishedPublication.isPublished()).thenReturn(true); + when(directory.getPublications("baseName")) + .thenReturn(Arrays.asList(depublishedPublication, publishedPublication)); + PublishedPath path = mock(); when(path.isDirectory()).thenReturn(true); when(path.baseName()).thenReturn("baseName"); @@ -41,30 +53,22 @@ void testWithDirectoryNonTranslationsSuffix() throws IOException { } @Test - void testPublicationDirectoryWithoutParent() throws IOException { + void testWithExistsPublicationDirectory() throws IOException { AnalyserContext ctx = this.mockAnalyserContext(); - PublishedPath path = mock(); - when(path.isDirectory()).thenReturn(true); - when(path.baseName()).thenReturn("baseName.translations"); - when(path.absolutePath()).thenReturn(Path.of("baseName.translations")); - - assertThrows( - IllegalArgumentException.class, - () -> { - this.analyser.analyse(ctx, path); - }); - } + PublicationDirectory directory = mock(); + when(ctx.getPublicationDirectory()).thenReturn(directory); - @Test - void testWithExistsPublicationDirectory() throws IOException { - AnalyserContext ctx = this.mockAnalyserContext(); + Publication depublishedPublication = mock(); + when(depublishedPublication.isPublished()).thenReturn(false); + Publication publishedPublication = mock(); + when(publishedPublication.isPublished()).thenReturn(true); + when(directory.getPublications("existsDir")) + .thenReturn(Arrays.asList(depublishedPublication, publishedPublication)); PublishedPath path = mock(); when(path.isDirectory()).thenReturn(true); when(path.baseName()).thenReturn("existsDir.translations"); - when(path.absolutePath()) - .thenReturn(this.resourceDir.resolve("existsDir.translations").toAbsolutePath()); assertEquals( AnalyserResult.OK_AND_INTERRUPT,