ThirdPartyTMSSmartling implements ThirdPartyTMS { @Override public void push(Repository repository, String projectId, String pluralSeparator, String skipTextUnitsWithPattern, String skipAssetsWithPathPattern, List<String> optionList) { SmartlingOptions options = SmartlingOptions.parseList(optionList); AndroidStringDocumentMapper mapper = new AndroidStringDocumentMapper(pluralSeparator, null); Stream<SmartlingFile> singularFiles = mapWithIndex( partitionSingulars(repository.getId(), LOCALE_EN, skipTextUnitsWithPattern, skipAssetsWithPathPattern), (batch, index) -> processPushBatch(batch, index, mapper, repository, projectId, options, Prefix.SINGULAR)); Stream<SmartlingFile> pluralFiles = mapWithIndex( partitionPlurals(repository.getId(), LOCALE_EN, skipTextUnitsWithPattern, skipAssetsWithPathPattern), (batch, index) -> processPushBatch(batch, index, mapper, repository, projectId, options, Prefix.PLURAL)); List<SmartlingFile> result = Stream.concat(singularFiles, pluralFiles).collect(Collectors.toList()); resultProcessor.processPush(result, options); } @Autowired  ThirdPartyTMSSmartling(SmartlingClient smartlingClient,
                                  TextUnitSearcher textUnitSearcher,
                                  AssetPathAndTextUnitNameKeys assetPathAndTextUnitNameKeys,
                                  TextUnitBatchImporterService textUnitBatchImporterService,
                                  SmartlingResultProcessor resultProcessor);  ThirdPartyTMSSmartling(SmartlingClient smartlingClient,
                                  TextUnitSearcher textUnitSearcher,
                                  AssetPathAndTextUnitNameKeys assetPathAndTextUnitNameKeys,
                                  TextUnitBatchImporterService textUnitBatchImporterService,
                                  SmartlingResultProcessor resultProcessor,
                                  int batchSize); @Override ThirdPartyTMSImage uploadImage(String projectId, String name, byte[] content); @Override List<ThirdPartyTextUnit> getThirdPartyTextUnits(Repository repository, String projectId); @Override void createImageToTextUnitMappings(String projectId, List<ThirdPartyImageToTextUnit> thirdPartyImageToTextUnits); @Override void push(Repository repository,
                     String projectId,
                     String pluralSeparator,
                     String skipTextUnitsWithPattern,
                     String skipAssetsWithPathPattern,
                     List<String> optionList); @Override void pull(Repository repository,
                     String projectId,
                     String pluralSeparator,
                     Map<String, String> localeMapping,
                     String skipTextUnitsWithPattern,
                     String skipAssetsWithPathPattern,
                     List<String> optionList); @Override void pushTranslations(Repository repository,
                                 String projectId,
                                 String pluralSeparator,
                                 Map<String, String> localeMapping,
                                 String skipTextUnitsWithPattern,
                                 String skipAssetsWithPathPattern,
                                 List<String> optionList);  }
@Test public void testPushWithoutSingularsNorPlurals() throws RepositoryNameAlreadyUsedException { List<SmartlingFile> result; Repository repository = repositoryService.createRepository(testIdWatcher.getEntityName("batchRepo")); tmsSmartling.push(repository, "projectId", "_", null, null, Collections.emptyList()); result = resultProcessor.pushFiles; assertThat(result).isEmpty(); verifyNoInteractions(smartlingClient); }
@Test public void testPushInBatchesWithSingularsAndNoPlurals() throws RepositoryNameAlreadyUsedException { int batchSize = 3; List<SmartlingFile> result; Repository repository = repositoryService.createRepository(testIdWatcher.getEntityName("batchRepo")); tmsSmartling = new ThirdPartyTMSSmartling(smartlingClient, textUnitSearcher, assetPathAndTextUnitNameKeys, textUnitBatchImporterService, resultProcessor, batchSize); TM tm = repository.getTm(); Asset asset = assetService.createAssetWithContent(repository.getId(), "fake_for_test", "fake for test"); AssetExtraction assetExtraction = new AssetExtraction(); assetExtraction.setAsset(asset); assetExtraction = assetExtractionRepository.save(assetExtraction); int singularTextUnits = 16; int singularBatches = 6; for (int i = 0; i < singularTextUnits; i++){ String name = "singular_message_test" + i; String content = "Singular Message Test " + i; String comment = "Singular Comment" + i; tmService.addTMTextUnit(tm.getId(), asset.getId(), name, content, comment); assetExtractionService.createAssetTextUnit(assetExtraction, name, content, comment); } prepareAssetAndTextUnits(assetExtraction, asset, tm); tmsSmartling.push(repository, "projectId", pluralSep, null, null, Collections.emptyList()); result = resultProcessor.pushFiles; assertThat(result).hasSize(singularBatches); assertThat(result).allSatisfy(file -> assertThat(file.getFileName()).endsWith("singular_source.xml")); assertThat(result.subList(0, 4)).allSatisfy(file -> assertThat(readTextUnits(file, pluralSep)).hasSize(batchSize)); assertThat(result.get(5)).satisfies(file -> assertThat(readTextUnits(file, pluralSep)).hasSize(singularTextUnits % batchSize)); }
@Test public void testPushInBatchesWithNoSingularsAndPlurals() throws RepositoryNameAlreadyUsedException { int batchSize = 3; List<SmartlingFile> result; Repository repository = repositoryService.createRepository(testIdWatcher.getEntityName("batchRepo")); tmsSmartling = new ThirdPartyTMSSmartling(smartlingClient, textUnitSearcher, assetPathAndTextUnitNameKeys, textUnitBatchImporterService, resultProcessor, batchSize); TM tm = repository.getTm(); Asset asset = assetService.createAssetWithContent(repository.getId(), "fake_for_test", "fake for test"); AssetExtraction assetExtraction = new AssetExtraction(); assetExtraction.setAsset(asset); assetExtraction = assetExtractionRepository.save(assetExtraction); PluralForm one = pluralFormService.findByPluralFormString("one"); int pluralTextUnits = 10; int pluralBatches = 4; for (int i = 0; i < pluralTextUnits; i++){ String name = "plural_message_test" + i; String content = "Plural Message Test " + i; String comment = "Plural Comment" + i; String pluralFormOther = "plural_form_other" + i; tmService.addTMTextUnit(tm, asset, name, content, comment, null, one, pluralFormOther); assetExtractionService.createAssetTextUnit(assetExtraction, name, content, comment); } prepareAssetAndTextUnits(assetExtraction, asset, tm); tmsSmartling.push(repository, "projectId", pluralSep, null, null, Collections.emptyList()); result = resultProcessor.pushFiles; assertThat(result).hasSize(pluralBatches); assertThat(result).allSatisfy(file -> assertThat(file.getFileName()).endsWith("plural_source.xml")); assertThat(result.subList(0, 2)).allSatisfy(file -> assertThat(readTextUnits(file, pluralSep)).hasSize(batchSize)); assertThat(result.get(3)).satisfies(file -> assertThat(readTextUnits(file, pluralSep)).hasSize(pluralTextUnits % batchSize)); }
@Test public void testPushInBatchesWithSingularsAndPlurals() throws RepositoryNameAlreadyUsedException { int batchSize = 3; List<SmartlingFile> result; Repository repository = repositoryService.createRepository(testIdWatcher.getEntityName("batchRepo")); tmsSmartling = new ThirdPartyTMSSmartling(smartlingClient, textUnitSearcher, assetPathAndTextUnitNameKeys, textUnitBatchImporterService, resultProcessor, batchSize); TM tm = repository.getTm(); Asset asset = assetService.createAssetWithContent(repository.getId(), "fake_for_test", "fake for test"); AssetExtraction assetExtraction = new AssetExtraction(); assetExtraction.setAsset(asset); assetExtraction = assetExtractionRepository.save(assetExtraction); PluralForm one = pluralFormService.findByPluralFormString("one"); int singularTextUnits = 14; int singularBatches = 5; for (int i = 0; i < singularTextUnits; i++){ String name = "singular_message_test" + i; String content = "Singular Message Test " + i; String comment = "Singular Comment" + i; tmService.addTMTextUnit(tm.getId(), asset.getId(), name, content, comment); assetExtractionService.createAssetTextUnit(assetExtraction, name, content, comment); } int pluralTextUnits = 8; int pluralBatches = 3; for (int i = 0; i < pluralTextUnits; i++){ String name = "plural_message_test" + i; String content = "Plural Message Test " + i; String comment = "Plural Comment" + i; String pluralFormOther = name + "_other"; tmService.addTMTextUnit(tm, asset, name, content, comment, null, one, pluralFormOther); assetExtractionService.createAssetTextUnit(assetExtraction, name, content, comment); } prepareAssetAndTextUnits(assetExtraction, asset, tm); tmsSmartling.push(repository, "projectId", pluralSep, null, null, Collections.emptyList()); result = resultProcessor.pushFiles; assertThat(result).hasSize(pluralBatches+singularBatches); List<SmartlingFile> singularFiles = result.stream() .filter(f -> f.getFileName().endsWith("singular_source.xml")) .collect(Collectors.toList()); List<SmartlingFile> pluralFiles = result.stream() .filter(f -> f.getFileName().endsWith("plural_source.xml")) .collect(Collectors.toList()); assertThat(singularFiles).hasSize(singularBatches); assertThat(singularFiles.subList(0, 3)).allSatisfy(file -> assertThat(readTextUnits(file, pluralSep)).hasSize(batchSize)); assertThat(singularFiles.get(4)).satisfies(file -> assertThat(readTextUnits(file, pluralSep)).hasSize(singularTextUnits % batchSize)); assertThat(pluralFiles).hasSize(pluralBatches); assertThat(pluralFiles.subList(0, 1)).allSatisfy(file -> assertThat(readTextUnits(file, pluralSep)).hasSize(batchSize)); assertThat(pluralFiles.get(2)).satisfies(file -> assertThat(readTextUnits(file, pluralSep)).hasSize(pluralTextUnits % batchSize)); }
@Test public void testPushDryRunNoBatches() { List<SmartlingFile> result; ThirdPartyServiceTestData testData = new ThirdPartyServiceTestData(testIdWatcher); Repository repository = testData.repository; tmsSmartling.push(repository, "projectId", "_", null, null, Arrays.asList("dry-run=true")); result = resultProcessor.pushFiles; assertThat(result).hasSize(2); assertThat(result.stream().map(SmartlingFile::getFileName)).containsOnly( repository.getName() + "/00000_singular_source.xml", repository.getName() + "/00000_plural_source.xml"); assertThat(result.stream().map(SmartlingFile::getFileContent)).containsOnly( singularContent(testData), pluralsContent(testData)); verifyNoInteractions(smartlingClient); }
@Test public void testPushWithUploadNoBatches() { List<SmartlingFile> result; ThirdPartyServiceTestData testData = new ThirdPartyServiceTestData(testIdWatcher); Repository repository = testData.repository; List<String> optionList = Arrays.asList("smartling-placeholder-format=NONE", "smartling-placeholder-format-custom=^some(.*)pattern$"); tmsSmartling.push(repository, "projectId", "_", null, null, optionList); result = resultProcessor.pushFiles; assertThat(result).hasSize(2); assertThat(result.stream().map(SmartlingFile::getFileName)).containsOnly( repository.getName() + "/00000_singular_source.xml", repository.getName() + "/00000_plural_source.xml"); assertThat(result.stream().map(SmartlingFile::getFileContent)).containsOnly( singularContent(testData), pluralsContent(testData)); verify(smartlingClient, times(1)).uploadFile( eq("projectId"), eq(repository.getName() + "/00000_singular_source.xml"), eq("android"), matches("(?s).*string name=.*"), eq("NONE"), eq("^some(.*)pattern$")); verify(smartlingClient, times(1)).uploadFile( eq("projectId"), eq(repository.getName() + "/00000_plural_source.xml"), eq("android"), matches("(?s).*plurals name=.*"), eq("NONE"), eq("^some(.*)pattern$")); }