use of org.molgenis.jobs.Progress in project molgenis by molgenis.
the class MappingServiceImplTest method testApplyMappingsAdd.
/**
* New entities in the source should be added to the target when a new mapping to the same target is performed.
*/
@Test
@SuppressWarnings("unchecked")
public void testApplyMappingsAdd() {
String entityTypeId = "addEntity";
@SuppressWarnings("unchecked") Repository<Entity> addEntityRepo = Mockito.mock(Repository.class);
when(addEntityRepo.getName()).thenReturn(entityTypeId);
EntityType targetMeta = entityTypeFactory.create(TARGET_HOP_ENTITY).setPackage(package_);
targetMeta.addAttribute(attrMetaFactory.create().setName("identifier"), ROLE_ID);
targetMeta.addAttribute(attrMetaFactory.create().setName("height").setDataType(DECIMAL).setNillable(false));
targetMeta.addAttribute(attrMetaFactory.create().setName("source"));
when(addEntityRepo.getEntityType()).thenReturn(targetMeta);
when(metaDataService.createRepository(argThat(obj -> obj.getId().equals(entityTypeId)))).thenReturn(addEntityRepo);
// add an entity to the source
List<Entity> sourceGeneEntities = newArrayList();
List<Entity> expectedEntities = newArrayList();
createEntities(targetMeta, sourceGeneEntities, expectedEntities);
Mockito.doAnswer(invocationOnMock -> {
@SuppressWarnings("unchecked") Consumer<List<Entity>> consumer = (Consumer<List<Entity>>) invocationOnMock.<Consumer>getArgument(0);
consumer.accept(sourceGeneEntities);
return null;
}).when(geneRepo).forEachBatched(ArgumentMatchers.any(Consumer.class), ArgumentMatchers.eq(MAPPING_BATCH_SIZE));
// make project and apply mappings once
MappingProject project = createMappingProjectWithMappings();
Entity mappedEntity = Mockito.mock(Entity.class);
when(entityManager.create(targetMeta, EntityManager.CreationMode.POPULATE)).thenReturn(mappedEntity);
// apply mapping again
assertEquals(mappingService.applyMappings("TestRun", entityTypeId, true, "packageId", "label", progress), 4);
Mockito.verify(geneRepo).forEachBatched(ArgumentMatchers.any(Consumer.class), ArgumentMatchers.any(Integer.class));
ArgumentCaptor<EntityType> entityTypeCaptor = ArgumentCaptor.forClass(EntityType.class);
Mockito.verify(permissionSystemService).giveUserWriteMetaPermissions(entityTypeCaptor.capture());
assertEquals(entityTypeCaptor.getValue().getId(), entityTypeId);
Mockito.verify(progress).setProgressMax(ArgumentMatchers.anyInt());
Mockito.verify(progress).progress(0, "Checking target repository [addEntity]...");
Mockito.verify(progress).status("Applying mappings to repository [HopEntity]");
Mockito.verify(progress).status("Mapping source [Genes]...");
Mockito.verify(progress).increment(1);
Mockito.verify(progress).status("Mapped 4 [Genes] entities.");
Mockito.verify(progress).status("Done applying mappings to repository [HopEntity]");
Mockito.verifyNoMoreInteractions(progress);
}
use of org.molgenis.jobs.Progress in project molgenis by molgenis.
the class OneClickImportJobTest method testGetEntityTypeWithCsv.
@Test
public void testGetEntityTypeWithCsv() throws UnknownFileTypeException, InvalidFormatException, IOException, URISyntaxException, EmptySheetException {
Progress progress = mock(Progress.class);
String filename = "simple-valid.csv";
when(oneClickImporterNamingService.createValidIdFromFileName(filename)).thenReturn("simple_valid");
File file = loadFile(OneClickImportJobTest.class, "/" + filename);
when(fileStore.getFile(filename)).thenReturn(file);
List<String[]> lines = new ArrayList<>();
lines.add(new String[] { "name,age", "piet,25" });
when(csvService.buildLinesFromFile(file)).thenReturn(lines);
DataCollection dataCollection = mock(DataCollection.class);
when(dataCollection.getName()).thenReturn("file_1");
when(oneClickImporterService.buildDataCollectionFromCsv("simple_valid", lines)).thenReturn(dataCollection);
EntityType entityType = mock(EntityType.class);
when(entityService.createEntityType(dataCollection, "simple_valid")).thenReturn(entityType);
oneClickImporterJob = new OneClickImportJob(excelService, csvService, oneClickImporterService, oneClickImporterNamingService, entityService, fileStore);
oneClickImporterJob.getEntityType(progress, filename);
verify(progress).status("Preparing import");
verify(csvService).buildLinesFromFile(file);
verify(oneClickImporterService).buildDataCollectionFromCsv("simple_valid", lines);
verify(progress).status("Importing [file_1] into package [simple_valid]");
verify(entityService).createEntityType(dataCollection, "simple_valid");
}
use of org.molgenis.jobs.Progress in project molgenis by molgenis.
the class MappingServiceImplTest method testApplyMappingsUpdate.
/**
* Applying a mapping multiple times to the same target should update the existing entities.
*/
@SuppressWarnings("unchecked")
@Test
public void testApplyMappingsUpdate() {
String entityTypeId = "updateEntity";
@SuppressWarnings("unchecked") Repository<Entity> updateEntityRepo = Mockito.mock(Repository.class);
when(updateEntityRepo.getName()).thenReturn(entityTypeId);
EntityType targetMeta = entityTypeFactory.create(TARGET_HOP_ENTITY).setPackage(package_);
targetMeta.addAttribute(attrMetaFactory.create().setName("identifier"), ROLE_ID);
targetMeta.addAttribute(attrMetaFactory.create().setName("height").setDataType(DECIMAL).setNillable(false));
targetMeta.addAttribute(attrMetaFactory.create().setName("source"));
when(dataService.hasRepository(entityTypeId)).thenReturn(true);
when(dataService.getRepository(entityTypeId)).thenReturn(updateEntityRepo);
when(updateEntityRepo.getEntityType()).thenReturn(targetMeta);
when(metaDataService.createRepository(argThat(obj -> obj.getLabel().equals(entityTypeId)))).thenReturn(updateEntityRepo);
// add an entity to the source
List<Entity> sourceGeneEntities = newArrayList();
List<Entity> expectedEntities = newArrayList();
createEntities(targetMeta, sourceGeneEntities, expectedEntities);
when(updateEntityRepo.count()).thenReturn(4L);
when(updateEntityRepo.findAll(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(expectedEntities.stream());
Mockito.doAnswer(invocationOnMock -> {
@SuppressWarnings("unchecked") Consumer<List<Entity>> consumer = (Consumer<List<Entity>>) invocationOnMock.<Consumer>getArgument(0);
consumer.accept(sourceGeneEntities);
return null;
}).when(geneRepo).forEachBatched(ArgumentMatchers.any(Consumer.class), ArgumentMatchers.eq(MAPPING_BATCH_SIZE));
// make project and apply mappings once
MappingProject project = createMappingProjectWithMappings();
when(entityManager.create(targetMeta, EntityManager.CreationMode.POPULATE)).thenAnswer(invocation -> new DynamicEntity(targetMeta));
// apply mapping again
assertEquals(mappingService.applyMappings("TestRun", entityTypeId, false, "packageId", "label", progress), 4);
Mockito.verify(geneRepo).forEachBatched(ArgumentMatchers.any(Consumer.class), ArgumentMatchers.any(Integer.class));
Mockito.verify(updateEntityRepo).upsertBatch(batchCaptor.capture());
Assert.assertTrue(EntityUtils.equalsEntities(batchCaptor.getValue(), expectedEntities));
Mockito.verify(progress).status("Applying mappings to repository [HopEntity]");
Mockito.verify(progress).status("Mapping source [Genes]...");
Mockito.verify(progress).increment(1);
Mockito.verify(progress).status("Mapped 4 [Genes] entities.");
Mockito.verify(progress).status("Done applying mappings to repository [HopEntity]");
Mockito.verifyZeroInteractions(permissionSystemService);
}
Aggregations