Skip to content

Commit

Permalink
Fix DropServiceBoxTests out-of-sync testing logic
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wadimw committed Oct 4, 2023
1 parent 499f58f commit 83af8c3
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 160 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -47,101 +43,48 @@ public void checkBoxConfig() {
}

@Override
public List<String> getDropSourceFileNames(Drop drop) throws Exception {
BoxDropExporter boxDropExporter =
(BoxDropExporter) dropExporterService.recreateDropExporter(drop);
BoxDropExporterConfig boxDropExporterConfig = boxDropExporter.getBoxDropExporterConfig();

List<BoxFile> sourceFiles = boxSDKService.listFiles(boxDropExporterConfig.getSourceFolderId());

return sourceFiles.stream().map(file -> file.getInfo().getName()).collect(Collectors.toList());
public List<DropFile> 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<BoxFile> 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(
"</body>",
"<trans-unit id=\"badid\" resname=\"TEST2\">\n"
+ "<source xml:lang=\"en\">Content2</source>\n"
+ "<target xml:lang=\"ko-kr\">Import Drop"
+ round
+ " - Content2 ko-kr</target>\n"
+ "</trans-unit>\n"
+ "</body>");
} 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("</body>", "</bod");
} else {
localizedContent = XliffUtils.localizeTarget(localizedContent, "Import Drop" + round);
}

boxSDKService.uploadFile(
boxDropExporterConfig.getLocalizedFolderId(),
sourceFile.getInfo().getName(),
localizedContent);
}
boxSDKService.uploadFile(getDropFolderIdForName(drop, dropFolder), fileName, content);
}

@Override
public void reviewDropFiles(Drop drop) throws DropExporterException, BoxSDKServiceException {

logger.debug("Review files in a drop for testing");

BoxDropExporter boxDropExporter =
(BoxDropExporter) dropExporterService.recreateDropExporter(drop);
BoxDropExporterConfig boxDropExporterConfig = boxDropExporter.getBoxDropExporterConfig();

List<BoxFile> 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<BoxFile> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> getDropSourceFileNames(Drop drop) throws Exception {
public List<DropFile> 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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
}

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

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

Expand Down Expand Up @@ -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")) {

Expand Down

0 comments on commit 83af8c3

Please sign in to comment.