@Test @SuppressWarnings("unchecked") void testApplyMappingsToRepoUpsert() { Repository<Entity> targetRepo = mock(Repository.class); Repository<Entity> sourceRepo = mock(Repository.class); EntityMapping sourceMapping = mock(EntityMapping.class); when(sourceMapping.getLabel()).thenReturn("sourceMappingLabel"); when(sourceMapping.getName()).thenReturn("sourceMappingID"); when(dataService.getRepository("sourceMappingID")).thenReturn(sourceRepo); when(targetRepo.count()).thenReturn(3L); EntityType targetEntityType = mock(EntityType.class); when(targetEntityType.getId()).thenReturn("targetEntityType"); when(targetEntityType.getAtomicAttributes()).thenReturn(newArrayList()); Attribute targetID = mock(Attribute.class); when(targetID.getName()).thenReturn("targetID"); when(targetEntityType.getIdAttribute()).thenReturn(targetID); when(targetRepo.getEntityType()).thenReturn(targetEntityType); List<Entity> batch = newArrayList(mock(Entity.class), mock(Entity.class)); Mockito.doAnswer( invocationOnMock -> { Consumer<List<Entity>> consumer = (Consumer<List<Entity>>) invocationOnMock.<Consumer>getArgument(0); consumer.accept(batch); consumer.accept(batch); return null; }) .when(sourceRepo) .forEachBatched(ArgumentMatchers.any(Consumer.class), eq(MAPPING_BATCH_SIZE)); mappingService.applyMappingToRepo(sourceMapping, targetRepo, progress, 3); Mockito.verify(targetRepo, Mockito.times(2)).upsertBatch(ArgumentMatchers.any(List.class)); Mockito.verify(progress, Mockito.times(2)).increment(1); Mockito.verify(progress).status("Mapping source [sourceMappingLabel]..."); Mockito.verify(progress).status("Mapped 4 [sourceMappingLabel] entities."); Mockito.verifyNoMoreInteractions(progress); }
long applyMappingToRepo( EntityMapping sourceMapping, Repository<Entity> targetRepo, Progress progress, int depth) { progress.status(format("Mapping source [%s]...", sourceMapping.getLabel())); AtomicLong counter = new AtomicLong(); boolean canAdd = targetRepo.count() == 0; dataService .getRepository(sourceMapping.getName()) .forEachBatched( entities -> processBatch(sourceMapping, targetRepo, progress, counter, canAdd, entities, depth), MAPPING_BATCH_SIZE); progress.status(format("Mapped %s [%s] entities.", counter, sourceMapping.getLabel())); return counter.get(); }
MappingServiceImpl implements MappingService { long applyMappingToRepo( EntityMapping sourceMapping, Repository<Entity> targetRepo, Progress progress, int depth) { progress.status(format("Mapping source [%s]...", sourceMapping.getLabel())); AtomicLong counter = new AtomicLong(); boolean canAdd = targetRepo.count() == 0; dataService .getRepository(sourceMapping.getName()) .forEachBatched( entities -> processBatch(sourceMapping, targetRepo, progress, counter, canAdd, entities, depth), MAPPING_BATCH_SIZE); progress.status(format("Mapped %s [%s] entities.", counter, sourceMapping.getLabel())); return counter.get(); } }
MappingServiceImpl implements MappingService { long applyMappingToRepo( EntityMapping sourceMapping, Repository<Entity> targetRepo, Progress progress, int depth) { progress.status(format("Mapping source [%s]...", sourceMapping.getLabel())); AtomicLong counter = new AtomicLong(); boolean canAdd = targetRepo.count() == 0; dataService .getRepository(sourceMapping.getName()) .forEachBatched( entities -> processBatch(sourceMapping, targetRepo, progress, counter, canAdd, entities, depth), MAPPING_BATCH_SIZE); progress.status(format("Mapped %s [%s] entities.", counter, sourceMapping.getLabel())); return counter.get(); } MappingServiceImpl(
      DataService dataService,
      AlgorithmService algorithmService,
      MappingProjectRepository mappingProjectRepository,
      PermissionSystemService permissionSystemService,
      AttributeFactory attrMetaFactory,
      EntityManager entityManager); }
MappingServiceImpl implements MappingService { long applyMappingToRepo( EntityMapping sourceMapping, Repository<Entity> targetRepo, Progress progress, int depth) { progress.status(format("Mapping source [%s]...", sourceMapping.getLabel())); AtomicLong counter = new AtomicLong(); boolean canAdd = targetRepo.count() == 0; dataService .getRepository(sourceMapping.getName()) .forEachBatched( entities -> processBatch(sourceMapping, targetRepo, progress, counter, canAdd, entities, depth), MAPPING_BATCH_SIZE); progress.status(format("Mapped %s [%s] entities.", counter, sourceMapping.getLabel())); return counter.get(); } MappingServiceImpl(
      DataService dataService,
      AlgorithmService algorithmService,
      MappingProjectRepository mappingProjectRepository,
      PermissionSystemService permissionSystemService,
      AttributeFactory attrMetaFactory,
      EntityManager entityManager); @Override @Transactional MappingProject addMappingProject(String projectName, String target, int depth); @Override @Transactional void deleteMappingProject(String mappingProjectId); @Override @Transactional MappingProject cloneMappingProject(String mappingProjectId); @Override @Transactional MappingProject cloneMappingProject(
      String mappingProjectId, String clonedMappingProjectName); @Override List<MappingProject> getAllMappingProjects(); @Override @Transactional void updateMappingProject(MappingProject mappingProject); @Override MappingProject getMappingProject(String identifier); @Override @Transactional long applyMappings(
      String mappingProjectId,
      String entityTypeId,
      Boolean addSourceAttribute,
      String packageId,
      String label,
      Progress progress); Stream<EntityType> getCompatibleEntityTypes(EntityType target); }
MappingServiceImpl implements MappingService { long applyMappingToRepo( EntityMapping sourceMapping, Repository<Entity> targetRepo, Progress progress, int depth) { progress.status(format("Mapping source [%s]...", sourceMapping.getLabel())); AtomicLong counter = new AtomicLong(); boolean canAdd = targetRepo.count() == 0; dataService .getRepository(sourceMapping.getName()) .forEachBatched( entities -> processBatch(sourceMapping, targetRepo, progress, counter, canAdd, entities, depth), MAPPING_BATCH_SIZE); progress.status(format("Mapped %s [%s] entities.", counter, sourceMapping.getLabel())); return counter.get(); } MappingServiceImpl(
      DataService dataService,
      AlgorithmService algorithmService,
      MappingProjectRepository mappingProjectRepository,
      PermissionSystemService permissionSystemService,
      AttributeFactory attrMetaFactory,
      EntityManager entityManager); @Override @Transactional MappingProject addMappingProject(String projectName, String target, int depth); @Override @Transactional void deleteMappingProject(String mappingProjectId); @Override @Transactional MappingProject cloneMappingProject(String mappingProjectId); @Override @Transactional MappingProject cloneMappingProject(
      String mappingProjectId, String clonedMappingProjectName); @Override List<MappingProject> getAllMappingProjects(); @Override @Transactional void updateMappingProject(MappingProject mappingProject); @Override MappingProject getMappingProject(String identifier); @Override @Transactional long applyMappings(
      String mappingProjectId,
      String entityTypeId,
      Boolean addSourceAttribute,
      String packageId,
      String label,
      Progress progress); Stream<EntityType> getCompatibleEntityTypes(EntityType target); static final int MAPPING_BATCH_SIZE; }