From 83af8c33a16ff56f7d09d103028af9e4542f9345 Mon Sep 17 00:00:00 2001 From: Wadim Wawrzenczak Date: Thu, 5 Oct 2023 00:14:12 +0200 Subject: [PATCH] Fix DropServiceBoxTests out-of-sync testing logic DropServiceBoxTest logic was out of sync with base DropServiceTest due to overrides in testing logic. It was done this way likely because DropServiceTest logic was coupled with java.io.File. Drop file access is now isolated from testing logic and overriden in DropServiceBoxTest. Testing logic overrides are deleted, so these tests are in sync again. --- .../l10n/mojito/service/drop/DropFile.java | 10 ++ .../service/drop/DropServiceBoxTest.java | 123 +++++------------- .../mojito/service/drop/DropServiceTest.java | 113 ++++++---------- 3 files changed, 86 insertions(+), 160 deletions(-) create mode 100644 webapp/src/test/java/com/box/l10n/mojito/service/drop/DropFile.java 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..49935cdad9 --- /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; +} 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 56899063d2..b201c03c35 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,25 +1,21 @@ package com.box.l10n.mojito.service.drop; +import static com.box.l10n.mojito.service.drop.exporter.DropExporterDirectories.*; import static org.junit.Assume.assumeTrue; -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 java.util.List; import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; import org.junit.Before; -import org.junit.Test; import org.junit.experimental.categories.Category; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,101 +43,48 @@ public void checkBoxConfig() { } @Override - public List getDropSourceFileNames(Drop drop) throws Exception { - BoxDropExporter boxDropExporter = - (BoxDropExporter) dropExporterService.recreateDropExporter(drop); - BoxDropExporterConfig boxDropExporterConfig = boxDropExporter.getBoxDropExporterConfig(); - - List sourceFiles = boxSDKService.listFiles(boxDropExporterConfig.getSourceFolderId()); - - return sourceFiles.stream().map(file -> file.getInfo().getName()).collect(Collectors.toList()); + public List getDropFiles(Drop drop, String dropFolder) + throws DropExporterException, BoxSDKServiceException { + return boxSDKService.listFiles(getDropFolderIdForName(drop, dropFolder)).stream() + .map( + boxFile -> + new DropFile() { + public String getName() { + return boxFile.getInfo().getName(); + } + + public String getContent() throws BoxSDKServiceException { + return boxSDKService.getFileContent(boxFile).getContent(); + } + }) + .collect(Collectors.toList()); } @Override - public void localizeDropFiles(Drop drop, int round) + public void writeDropFile(Drop drop, String dropFolder, String fileName, String content) throws BoxSDKServiceException, DropExporterException { - - logger.debug("Localize files in a drop for testing"); - - 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()); - - boxSDKService.uploadFile( - boxDropExporterConfig.getLocalizedFolderId(), - sourceFile.getInfo().getName(), - reviewedContent); - } - } - - @Override - public void checkImportedFilesContent(Drop drop, int round) - throws BoxSDKServiceException, DropExporterException { - - logger.debug("Check imported files contains text unit variant ids"); - + public String getDropFolderIdForName(Drop drop, String dropFolderName) + throws DropExporterException { 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); + switch (dropFolderName) { + case DROP_FOLDER_SOURCE_FILES_NAME: + return boxDropExporterConfig.getSourceFolderId(); + case DROP_FOLDER_LOCALIZED_FILES_NAME: + return boxDropExporterConfig.getLocalizedFolderId(); + case DROP_FOLDER_IMPORTED_FILES_NAME: + return boxDropExporterConfig.getImportedFolderId(); + case DROP_FOLDER_QUERIES_NAME: + return boxDropExporterConfig.getQueriesFolderId(); + case DROP_FOLDER_QUOTES_NAME: + return boxDropExporterConfig.getQuotesFolderId(); + default: + return null; } } } 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 752156ee0f..50e0ba6588 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 @@ -48,7 +48,6 @@ 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; @@ -448,20 +447,45 @@ public void forNoEmptyXliffs() throws Exception { Drop drop = startExportProcess.get(); // Make sure no French xliff was generated - assertFalse(getDropSourceFileNames(drop).contains("fr-FR.xliff")); + assertFalse( + getDropFiles(drop, DROP_FOLDER_SOURCE_FILES_NAME).stream() + .anyMatch(dropFile -> dropFile.getName().equals("fr-FR.xliff"))); } - public List getDropSourceFileNames(Drop drop) throws Exception { + public List getDropFiles(Drop drop, String dropFolder) + throws DropExporterException, BoxSDKServiceException { FileSystemDropExporter fileSystemDropExporter = (FileSystemDropExporter) dropExporterService.recreateDropExporter(drop); FileSystemDropExporterConfig fileSystemDropExporterConfig = fileSystemDropExporter.getFileSystemDropExporterConfig(); - File sourceDirectory = - Paths.get(fileSystemDropExporterConfig.getDropFolderPath(), DROP_FOLDER_SOURCE_FILES_NAME) - .toFile(); - File[] sourceFiles = sourceDirectory.listFiles(); - assertNotNull(sourceFiles); - return Arrays.stream(sourceFiles).map(File::getName).collect(Collectors.toList()); + + File folder = Paths.get(fileSystemDropExporterConfig.getDropFolderPath(), dropFolder).toFile(); + File[] files = folder.listFiles(); + assertNotNull(files); + return Arrays.stream(files) + .map( + file -> + new DropFile() { + public String getName() { + return file.getName(); + } + + public String getContent() throws IOException { + return Files.toString(file, StandardCharsets.UTF_8); + } + }) + .collect(Collectors.toList()); + } + + 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( @@ -499,19 +523,9 @@ public void localizeDropFiles( 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( @@ -539,12 +553,7 @@ public void localizeDropFiles( // 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); } } @@ -553,30 +562,15 @@ public void reviewDropFiles(Drop drop) 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); } } @@ -585,24 +579,13 @@ public void checkImportedFilesContent(Drop drop, int round) 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); } } @@ -734,23 +717,13 @@ 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")) {