ThirdPartyService { void mapMojitoAndThirdPartyTextUnits(Repository repository, String projectId) { List<ThirdPartyTextUnit> thirdPartyTextUnits = thirdPartyTMS.getThirdPartyTextUnits(repository, projectId); LoadingCache<String, Optional<Asset>> assetCache = getAssetCache(repository); logger.debug("Get the third party ids that have been already mapped"); Set<String> thirdPartyTextUnitIdsAlreadyMapped = thirdPartyTextUnitRepository.findThirdPartyIdsByRepository(repository); logger.debug("Batch by asset to optimize the import and exclude already mapped"); Map<Asset, List<ThirdPartyTextUnit>> notMappedGroupedByAsset = thirdPartyTextUnits.stream(). filter(t -> !thirdPartyTextUnitIdsAlreadyMapped.contains(t.getId())). collect(groupingBy(o -> assetCache.getUnchecked(o.getAssetPath()).orElse(null), LinkedHashMap::new, toList())); logger.debug("Perform mapping by asset (exclude null assset, that could appear if asset path didn't match)"); notMappedGroupedByAsset.entrySet().stream() .filter(e -> e.getKey() != null) .forEach(e -> mapThirdPartyTextUnitsToTextUnitDTOs(e.getKey(), e.getValue())); }  PollableFuture<Void> asyncSyncMojitoWithThirdPartyTMS(ThirdPartySync thirdPartySync);  }
@Test public void mapMojitoAndThirdPartyTextUnits() throws InterruptedException, ExecutionException { ThirdPartyServiceTestData thirdPartyServiceTestData = new ThirdPartyServiceTestData(testIdWatcher); Repository repository = thirdPartyServiceTestData.repository; Asset asset = thirdPartyServiceTestData.asset; String projectId = "someProjectIdForTest"; logger.debug("Create mocks and data for tests"); doAnswer(invocation -> Arrays.asList( createThirdPartyTextUnit(asset.getPath(), "3rd-hello", "hello"), createThirdPartyTextUnit(asset.getPath(), "3rd-bye", "bye"), createThirdPartyTextUnit(asset.getPath(), "3rd-plural_things", "plural_things", true) )).when(thirdPartyTMSMock).getThirdPartyTextUnits(any(), any()); doNothing().when(thirdPartyTMSMock).createImageToTextUnitMappings(any(), any()); logger.debug("Invoke function to test"); ThirdPartySync thirdPartySync = new ThirdPartySync(); thirdPartySync.setRepositoryId(repository.getId()); thirdPartySync.setProjectId(projectId); thirdPartySync.setActions(Arrays.asList(ThirdPartySyncAction.MAP_TEXTUNIT, ThirdPartySyncAction.PUSH_SCREENSHOT)); thirdPartySync.setPluralSeparator(" _"); thirdPartySync.setOptions(new ArrayList<>()); thirdPartyService.asyncSyncMojitoWithThirdPartyTMS(thirdPartySync).get(); logger.debug("Verify states"); thirdPartyTextUnitRepository.findAll().stream() .filter(thirdPartyTextUnit -> thirdPartyTextUnit.getAsset().getId().equals(asset.getId())) .forEach(t -> { logger.debug("id:{}, asset: {}, ttuid: {}, ttuname:{}, tpid:{}", t.getId(), t.getAsset().getPath(), t.getTmTextUnit().getId(), t.getTmTextUnit().getName(), t.getThirdPartyId()); }); List<com.box.l10n.mojito.entity.ThirdPartyTextUnit> thirdPartyTextUnits = thirdPartyTextUnitRepository.findAll().stream() .filter(thirdPartyTextUnit -> thirdPartyTextUnit.getAsset().getId().equals(asset.getId())) .collect(toList()); assertEquals(8, thirdPartyTextUnits.size()); thirdPartyTextUnits.forEach(t -> assertEquals(asset.getId(), t.getAsset().getId())); assertEquals(thirdPartyServiceTestData.tmTextUnitHello.getId(), thirdPartyTextUnits.get(0).getTmTextUnit().getId()); assertEquals("3rd-hello", thirdPartyTextUnits.get(0).getThirdPartyId()); assertEquals(thirdPartyServiceTestData.tmTextUnitBye.getId(), thirdPartyTextUnits.get(1).getTmTextUnit().getId()); assertEquals("3rd-bye", thirdPartyTextUnits.get(1).getThirdPartyId()); assertEquals(thirdPartyServiceTestData.tmTextUnitPluralThingsZero.getId(), thirdPartyTextUnits.get(2).getTmTextUnit().getId()); assertEquals("3rd-plural_things", thirdPartyTextUnits.get(2).getThirdPartyId()); assertEquals(thirdPartyServiceTestData.tmTextUnitPluralThingsOne.getId(), thirdPartyTextUnits.get(3).getTmTextUnit().getId()); assertEquals("3rd-plural_things", thirdPartyTextUnits.get(3).getThirdPartyId()); assertEquals(thirdPartyServiceTestData.tmTextUnitPluralThingsTwo.getId(), thirdPartyTextUnits.get(4).getTmTextUnit().getId()); assertEquals("3rd-plural_things", thirdPartyTextUnits.get(4).getThirdPartyId()); assertEquals(thirdPartyServiceTestData.tmTextUnitPluralThingsFew.getId(), thirdPartyTextUnits.get(5).getTmTextUnit().getId()); assertEquals("3rd-plural_things", thirdPartyTextUnits.get(5).getThirdPartyId()); assertEquals(thirdPartyServiceTestData.tmTextUnitPluralThingsMany.getId(), thirdPartyTextUnits.get(6).getTmTextUnit().getId()); assertEquals("3rd-plural_things", thirdPartyTextUnits.get(6).getThirdPartyId()); assertEquals(thirdPartyServiceTestData.tmTextUnitPluralThingsOther.getId(), thirdPartyTextUnits.get(7).getTmTextUnit().getId()); assertEquals("3rd-plural_things", thirdPartyTextUnits.get(7).getThirdPartyId()); logger.debug("Verify behavior"); verify(thirdPartyTMSMock, times(3)).createImageToTextUnitMappings( eq(projectId), thirdPartyImageToTextUnitsArgumentCaptor.capture() ); List<List<ThirdPartyImageToTextUnit>> allThirdPartyImageToTextUnits = thirdPartyImageToTextUnitsArgumentCaptor.getAllValues(); List<ThirdPartyImageToTextUnit> thirdPartyImageToTextUnits = allThirdPartyImageToTextUnits.get(0); assertEquals(2L, thirdPartyImageToTextUnits.size()); assertEquals("img-image1a.png", thirdPartyImageToTextUnits.get(0).getImageId()); assertEquals("3rd-bye", thirdPartyImageToTextUnits.get(0).getTextUnitId()); assertEquals("img-image1a.png", thirdPartyImageToTextUnits.get(1).getImageId()); assertEquals("3rd-hello", thirdPartyImageToTextUnits.get(1).getTextUnitId()); thirdPartyImageToTextUnits = allThirdPartyImageToTextUnits.get(1); assertEquals(1L, thirdPartyImageToTextUnits.size()); assertEquals("img-image2a.png", thirdPartyImageToTextUnits.get(0).getImageId()); assertEquals("3rd-hello", thirdPartyImageToTextUnits.get(0).getTextUnitId()); thirdPartyImageToTextUnits = allThirdPartyImageToTextUnits.get(2); assertEquals(2L, thirdPartyImageToTextUnits.size()); assertEquals("img-image3a.png", thirdPartyImageToTextUnits.get(0).getImageId()); assertEquals("3rd-plural_things", thirdPartyImageToTextUnits.get(0).getTextUnitId()); assertEquals("img-image3a.png", thirdPartyImageToTextUnits.get(1).getImageId()); assertEquals("3rd-plural_things", thirdPartyImageToTextUnits.get(1).getTextUnitId()); }