use of org.molgenis.data.meta.model.EntityType.AttributeRole.ROLE_ID 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.data.meta.model.EntityType.AttributeRole.ROLE_ID 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