diff --git a/webapp/src/test/java/com/box/l10n/mojito/service/drop/DropFile.java b/webapp/src/test/java/com/box/l10n/mojito/service/drop/DropFile.java new file mode 100644 index 0000000000..d65f5dd5be --- /dev/null +++ b/webapp/src/test/java/com/box/l10n/mojito/service/drop/DropFile.java @@ -0,0 +1,10 @@ +package com.box.l10n.mojito.service.drop; + +import com.box.l10n.mojito.boxsdk.BoxSDKServiceException; +import java.io.IOException; + +public interface DropFile { + String getName(); + + String getContent() throws BoxSDKServiceException, IOException; +} \ No newline at end of file diff --git a/webapp/src/test/java/com/box/l10n/mojito/service/drop/DropServiceBoxTest.java b/webapp/src/test/java/com/box/l10n/mojito/service/drop/DropServiceBoxTest.java index 857027cb9d..0fc4ba1912 100644 --- a/webapp/src/test/java/com/box/l10n/mojito/service/drop/DropServiceBoxTest.java +++ b/webapp/src/test/java/com/box/l10n/mojito/service/drop/DropServiceBoxTest.java @@ -1,148 +1,103 @@ package com.box.l10n.mojito.service.drop; -import com.box.l10n.mojito.boxsdk.BoxFileWithContent; import com.box.l10n.mojito.boxsdk.BoxSDKService; +import com.box.l10n.mojito.boxsdk.BoxSDKServiceConfigFromProperties; import com.box.l10n.mojito.boxsdk.BoxSDKServiceException; import com.box.l10n.mojito.entity.Drop; -import com.box.l10n.mojito.okapi.XliffState; import com.box.l10n.mojito.service.drop.exporter.BoxDropExporter; import com.box.l10n.mojito.service.drop.exporter.BoxDropExporterConfig; import com.box.l10n.mojito.service.drop.exporter.DropExporterException; -import com.box.l10n.mojito.test.XliffUtils; import com.box.l10n.mojito.test.category.BoxSDKTest; import com.box.l10n.mojito.test.category.SlowTest; import com.box.sdk.BoxFile; -import org.junit.Test; +import org.apache.commons.lang.StringUtils; import org.junit.experimental.categories.Category; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; + import java.util.List; -import org.junit.Assume; +import java.util.stream.Collectors; + import org.junit.Before; import org.junit.Ignore; import org.springframework.boot.test.WebIntegrationTest; +import static com.box.l10n.mojito.service.drop.exporter.DropExporterDirectories.*; +import static org.junit.Assume.assumeTrue; + /** * @author jaurambault */ @Ignore("won't work without @DirtiesContext which makes the build to slow") @WebIntegrationTest(randomPort = true, value = {"l10n.dropexporter.type=BOX"}) +@Category({BoxSDKTest.class, SlowTest.class}) public class DropServiceBoxTest extends DropServiceTest { static Logger logger = LoggerFactory.getLogger(DropServiceBoxTest.class); + @Autowired + BoxSDKServiceConfigFromProperties boxSDKServiceConfigFromProperties; + @Autowired BoxSDKService boxSDKService; @Before public void checkBoxConfig() { - //TODO(P1) here we'll need to check if Box credential are available, if not don't run tests - Assume.assumeTrue(true); - } - - @Test - @Category({BoxSDKTest.class, SlowTest.class}) - @Override - public void testCreateDrop() throws Exception { - super.testCreateDrop(); - } - - @Test - @Category({BoxSDKTest.class, SlowTest.class}) - @Override - public void forTranslation() throws Exception { - super.forTranslation(); - } - - @Test - @Category({BoxSDKTest.class, SlowTest.class}) - @Override - public void forTranslationWithTranslationAddedAfterExport() throws Exception { - super.forTranslationWithTranslationAddedAfterExport(); + assumeTrue(StringUtils.isNotEmpty(boxSDKServiceConfigFromProperties.getRootFolderId())); } - @Test - @Category({BoxSDKTest.class, SlowTest.class}) @Override - public void forReview() throws Exception { - super.forTranslation(); + public List getDropFiles(Drop drop, String dropFolder) throws DropExporterException, BoxSDKServiceException { + return boxSDKService.listFiles(getDropFolderIdForName(drop, dropFolder)).stream() + .map(boxFile -> + new BoxDropFile(boxFile, boxSDKService)) + .collect(Collectors.toList()); } - @Test - @Category({BoxSDKTest.class, SlowTest.class}) @Override - public void allWithSevereError() throws Exception { - super.allWithSevereError(); + public void writeDropFile(Drop drop, String dropFolder, String fileName, String content) + throws BoxSDKServiceException, DropExporterException { + boxSDKService.uploadFile(getDropFolderIdForName(drop, dropFolder), fileName, content); } - @Override - public void localizeDropFiles(Drop drop, int round) throws BoxSDKServiceException, DropExporterException { - - logger.debug("Localize files in a drop for testing"); - - BoxDropExporter boxDropExporter = (BoxDropExporter) dropExporterService.recreateDropExporter(drop); + public String getDropFolderIdForName(Drop drop, String dropFolderName) + throws DropExporterException { + BoxDropExporter boxDropExporter = + (BoxDropExporter) dropExporterService.recreateDropExporter(drop); BoxDropExporterConfig boxDropExporterConfig = boxDropExporter.getBoxDropExporterConfig(); - List sourceFiles = boxSDKService.listFiles(boxDropExporterConfig.getSourceFolderId()); - - for (BoxFile sourceFile : sourceFiles) { - BoxFileWithContent fileContent = boxSDKService.getFileContent(sourceFile); - String localizedContent = fileContent.getContent(); - - if (sourceFile.getInfo().getName().startsWith("ko-KR")) { - logger.debug("For the Korean file, don't translate but add a corrupted text unit (invalid id) at the end"); - localizedContent = localizedContent.replaceAll("", - "\n" - + "Content2\n" - + "Import Drop" + round + " - Content2 ko-kr\n" - + "\n" - + ""); - } else if (sourceFile.getInfo().getName().startsWith("it-IT")) { - logger.debug("For the Italien file, don't translate but add a corrupted xml"); - localizedContent = localizedContent.replaceAll("", " sourceFiles = boxSDKService.listFiles(boxDropExporterConfig.getSourceFolderId()); - - for (BoxFile sourceFile : sourceFiles) { - BoxFileWithContent fileContent = boxSDKService.getFileContent(sourceFile); - String reviewedContent = fileContent.getContent(); - - reviewedContent = XliffUtils.replaceTargetState(reviewedContent, XliffState.SIGNED_OFF.toString()); +class BoxDropFile implements DropFile { + private final BoxFile boxFile; + private final BoxSDKService boxSDKService; - boxSDKService.uploadFile(boxDropExporterConfig.getLocalizedFolderId(), sourceFile.getInfo().getName(), reviewedContent); - } + BoxDropFile(BoxFile boxFile, BoxSDKService boxSDKService) { + this.boxFile = boxFile; + this.boxSDKService = boxSDKService; } - @Override - public void checkImportedFilesContent(Drop drop, int round) throws BoxSDKServiceException, DropExporterException { - - logger.debug("Check imported files contains text unit variant ids"); - - BoxDropExporter boxDropExporter = (BoxDropExporter) dropExporterService.recreateDropExporter(drop); - BoxDropExporterConfig boxDropExporterConfig = boxDropExporter.getBoxDropExporterConfig(); - - List importedFiles = boxSDKService.listFiles(boxDropExporterConfig.getImportedFolderId()); - - for (BoxFile importedFile : importedFiles) { - BoxFileWithContent fileWithContent = boxSDKService.getFileContent(importedFile); - checkImportedFilesContent(importedFile.getInfo().getName(), fileWithContent.getContent(), round); - } + public String getName() { + return boxFile.getInfo().getName(); } -} + public String getContent() throws BoxSDKServiceException { + return boxSDKService.getFileContent(boxFile).getContent(); + } +} \ No newline at end of file diff --git a/webapp/src/test/java/com/box/l10n/mojito/service/drop/DropServiceTest.java b/webapp/src/test/java/com/box/l10n/mojito/service/drop/DropServiceTest.java index 6bcb283d09..8b0c144bc7 100644 --- a/webapp/src/test/java/com/box/l10n/mojito/service/drop/DropServiceTest.java +++ b/webapp/src/test/java/com/box/l10n/mojito/service/drop/DropServiceTest.java @@ -45,16 +45,15 @@ import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; -import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; import java.util.List; import java.util.concurrent.ExecutionException; +import java.util.stream.Collectors; -import static com.box.l10n.mojito.service.drop.exporter.DropExporterDirectories.DROP_FOLDER_IMPORTED_FILES_NAME; -import static com.box.l10n.mojito.service.drop.exporter.DropExporterDirectories.DROP_FOLDER_LOCALIZED_FILES_NAME; -import static com.box.l10n.mojito.service.drop.exporter.DropExporterDirectories.DROP_FOLDER_SOURCE_FILES_NAME; +import static com.box.l10n.mojito.service.drop.exporter.DropExporterDirectories.*; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -437,12 +436,35 @@ public void forNoEmptyXliffs() throws Exception { Drop drop = startExportProcess.get(); // Make sure no French xliff was generated - FileSystemDropExporter fileSystemDropExporter = (FileSystemDropExporter) dropExporterService.recreateDropExporter(drop); - FileSystemDropExporterConfig fileSystemDropExporterConfig = fileSystemDropExporter.getFileSystemDropExporterConfig(); + assertFalse( + getDropFiles(drop, DROP_FOLDER_SOURCE_FILES_NAME).stream() + .anyMatch(dropFile -> dropFile.getName().equals("fr-FR.xliff"))); + } - File frFR = new File(Paths.get(fileSystemDropExporterConfig.getDropFolderPath(), DROP_FOLDER_SOURCE_FILES_NAME, "fr-FR.xliff").toString()); + public List getDropFiles(Drop drop, String dropFolder) + throws DropExporterException, BoxSDKServiceException { + FileSystemDropExporter fileSystemDropExporter = + (FileSystemDropExporter) dropExporterService.recreateDropExporter(drop); + FileSystemDropExporterConfig fileSystemDropExporterConfig = + fileSystemDropExporter.getFileSystemDropExporterConfig(); + + File folder = Paths.get(fileSystemDropExporterConfig.getDropFolderPath(), dropFolder).toFile(); + File[] files = folder.listFiles(); + assertNotNull(files); + return Arrays.stream(files) + .map(FileSystemDropFile::new) + .collect(Collectors.toList()); + } - assertFalse(frFR.exists()); + public void writeDropFile(Drop drop, String dropFolder, String fileName, String content) + throws BoxSDKServiceException, IOException, DropExporterException { + FileSystemDropExporter fileSystemDropExporter = + (FileSystemDropExporter) dropExporterService.recreateDropExporter(drop); + FileSystemDropExporterConfig fileSystemDropExporterConfig = + fileSystemDropExporter.getFileSystemDropExporterConfig(); + File file = + Paths.get(fileSystemDropExporterConfig.getDropFolderPath(), dropFolder, fileName).toFile(); + Files.write(content, file, StandardCharsets.UTF_8); } public void checkNumberOfUntranslatedTextUnit(Repository repository, List bcp47Tags, int expectedNumberOfUnstranslated) { @@ -475,14 +497,9 @@ public void localizeDropFiles(Drop drop, int round, String xliffState, boolean i logger.debug("Localize files in a drop for testing"); - FileSystemDropExporter fileSystemDropExporter = (FileSystemDropExporter) dropExporterService.recreateDropExporter(drop); - FileSystemDropExporterConfig fileSystemDropExporterConfig = fileSystemDropExporter.getFileSystemDropExporterConfig(); - - File[] sourceFiles = Paths.get(fileSystemDropExporterConfig.getDropFolderPath(), DROP_FOLDER_SOURCE_FILES_NAME).toFile().listFiles(); + for (DropFile sourceFile : getDropFiles(drop, DROP_FOLDER_SOURCE_FILES_NAME)) { - for (File sourceFile : sourceFiles) { - - String localizedContent = Files.toString(sourceFile, StandardCharsets.UTF_8); + String localizedContent = sourceFile.getContent(); if (sourceFile.getName().startsWith("ko-KR")) { logger.debug("For the Korean file, don't translate but add a corrupted text unit (invalid id) at the end"); @@ -504,8 +521,7 @@ public void localizeDropFiles(Drop drop, int round, String xliffState, boolean i localizedContent = XliffUtils.replaceTargetState(localizedContent, xliffState); //TODO(P1) this logic is being duplicated everywhere maybe it should go back into the config or service. - Path localizedFolderPath = Paths.get(fileSystemDropExporterConfig.getDropFolderPath(), DROP_FOLDER_LOCALIZED_FILES_NAME, sourceFile.getName()); - Files.write(localizedContent, localizedFolderPath.toFile(), StandardCharsets.UTF_8); + writeDropFile(drop, DROP_FOLDER_LOCALIZED_FILES_NAME, sourceFile.getName(), localizedContent); } } @@ -513,19 +529,13 @@ public void reviewDropFiles(Drop drop) throws DropExporterException, BoxSDKServi logger.debug("Review files in a drop for testing"); - FileSystemDropExporter fileSystemDropExporter = (FileSystemDropExporter) dropExporterService.recreateDropExporter(drop); - FileSystemDropExporterConfig fileSystemDropExporterConfig = fileSystemDropExporter.getFileSystemDropExporterConfig(); - - File[] sourceFiles = Paths.get(fileSystemDropExporterConfig.getDropFolderPath(), DROP_FOLDER_SOURCE_FILES_NAME).toFile().listFiles(); + for (DropFile sourceFile : getDropFiles(drop, DROP_FOLDER_SOURCE_FILES_NAME)) { - for (File sourceFile : sourceFiles) { - - String reviewedContent = Files.toString(sourceFile, StandardCharsets.UTF_8); + String reviewedContent = sourceFile.getContent(); reviewedContent = XliffUtils.replaceTargetState(reviewedContent, XliffState.SIGNED_OFF.toString()); //TODO(P1) this logic is being duplicated everywhere maybe it should go back into the config or service. - Path localizedFolderPath = Paths.get(fileSystemDropExporterConfig.getDropFolderPath(), DROP_FOLDER_LOCALIZED_FILES_NAME, sourceFile.getName()); - Files.write(reviewedContent, localizedFolderPath.toFile(), StandardCharsets.UTF_8); + writeDropFile(drop, DROP_FOLDER_LOCALIZED_FILES_NAME, sourceFile.getName(), reviewedContent); } } @@ -533,19 +543,13 @@ public void checkImportedFilesContent(Drop drop, int round) throws BoxSDKService logger.debug("Check imported files contains text unit variant ids"); - FileSystemDropExporter fileSystemDropExporter = (FileSystemDropExporter) dropExporterService.recreateDropExporter(drop); - FileSystemDropExporterConfig fileSystemDropExporterConfig = fileSystemDropExporter.getFileSystemDropExporterConfig(); - - File[] importedFiles = Paths.get(fileSystemDropExporterConfig.getDropFolderPath(), DROP_FOLDER_IMPORTED_FILES_NAME).toFile().listFiles(); - - for (File importedFile : importedFiles) { + for (DropFile importedFile : getDropFiles(drop, DROP_FOLDER_IMPORTED_FILES_NAME)) { if (!importedFile.getName().endsWith("xliff")) { continue; } - String importedContent = Files.toString(importedFile, StandardCharsets.UTF_8); - checkImportedFilesContent(importedFile.getName(), importedContent, round); + checkImportedFilesContent(importedFile.getName(), importedFile.getContent(), round); } } @@ -660,18 +664,13 @@ public void checkImportedFilesContent(String filename, String importedContent, i public void checkImportedFilesForReviewContent(Drop drop) throws DropExporterException, BoxSDKServiceException, IOException { logger.debug("Check imported files contains text unit variant ids"); - FileSystemDropExporter fileSystemDropExporter = (FileSystemDropExporter) dropExporterService.recreateDropExporter(drop); - FileSystemDropExporterConfig fileSystemDropExporterConfig = fileSystemDropExporter.getFileSystemDropExporterConfig(); - - File[] importedFiles = Paths.get(fileSystemDropExporterConfig.getDropFolderPath(), DROP_FOLDER_IMPORTED_FILES_NAME).toFile().listFiles(); - - for (File importedFile : importedFiles) { + for (DropFile importedFile : getDropFiles(drop, DROP_FOLDER_IMPORTED_FILES_NAME)) { if (!importedFile.getName().endsWith("xliff")) { continue; } - String importedContent = Files.toString(importedFile, StandardCharsets.UTF_8); + String importedContent = importedFile.getContent(); if (importedFile.getName().startsWith("fr-FR")) { @@ -838,4 +837,20 @@ public void testCancelDropException() throws DropExporterException, ExecutionExc logger.debug("Wait for cancellation to finish"); pollableTaskService.waitForPollableTask(cancelDropPollableTask.getId(), 600000L); } +} + +class FileSystemDropFile implements DropFile { + private final File file; + + FileSystemDropFile(File file) { + this.file = file; + } + + public String getName() { + return file.getName(); + } + + public String getContent() throws IOException { + return Files.toString(file, StandardCharsets.UTF_8); + } } \ No newline at end of file