Skip to content

Commit

Permalink
fix: check translation directory against the publication
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Aug 22, 2024
1 parent 5fb2ebf commit 15e245e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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");
Expand All @@ -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,
Expand Down

0 comments on commit 15e245e

Please sign in to comment.