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 not needed anymore so they are deleted, thus all tests are in sync again.
  • Loading branch information
wadimw committed Oct 4, 2023
1 parent 2238219 commit 561f6c2
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 137 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,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<DropFile> 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<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);
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;
}
}
}

@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());
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<BoxFile> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<DropFile> 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<String> bcp47Tags, int expectedNumberOfUnstranslated) {
Expand Down Expand Up @@ -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");
Expand All @@ -504,48 +521,35 @@ 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);
}
}

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

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);
}
}

public void checkImportedFilesContent(Drop drop, int round) throws BoxSDKServiceException, DropExporterException, 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);
checkImportedFilesContent(importedFile.getName(), importedContent, round);
checkImportedFilesContent(importedFile.getName(), importedFile.getContent(), round);
}
}

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

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

0 comments on commit 561f6c2

Please sign in to comment.