Search in sources :

Example 1 with PermissionSystemService

use of org.molgenis.data.security.permission.PermissionSystemService 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);
}
Also used : DefaultPackage(org.molgenis.data.meta.DefaultPackage) Progress(org.molgenis.jobs.Progress) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Autowired(org.springframework.beans.factory.annotation.Autowired) Test(org.testng.annotations.Test) AlgorithmService(org.molgenis.semanticmapper.service.AlgorithmService) PermissionSystemService(org.molgenis.data.security.permission.PermissionSystemService) MetaDataService(org.molgenis.data.meta.MetaDataService) Collections.singletonList(java.util.Collections.singletonList) User(org.molgenis.data.security.auth.User) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) JsMagmaScriptEvaluator(org.molgenis.js.magma.JsMagmaScriptEvaluator) MappingProject(org.molgenis.semanticmapper.mapping.model.MappingProject) SOURCE(org.molgenis.semanticmapper.service.impl.MappingServiceImpl.SOURCE) Collectors.toSet(java.util.stream.Collectors.toSet) UserFactory(org.molgenis.data.security.auth.UserFactory) UserTestConfig(org.molgenis.data.security.config.UserTestConfig) org.mockito(org.mockito) org.molgenis.data(org.molgenis.data) AttributeMapping(org.molgenis.semanticmapper.mapping.model.AttributeMapping) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) MappingProjectMetaData(org.molgenis.semanticmapper.meta.MappingProjectMetaData) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) Stream(java.util.stream.Stream) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) EntityBaseTestConfig(org.molgenis.data.config.EntityBaseTestConfig) Strictness(org.mockito.quality.Strictness) EntityUtils(org.molgenis.data.util.EntityUtils) Assert.assertEquals(org.testng.Assert.assertEquals) Lists(com.google.common.collect.Lists) DynamicEntity(org.molgenis.data.support.DynamicEntity) Assert(org.testng.Assert) MAPPING_BATCH_SIZE(org.molgenis.semanticmapper.service.impl.MappingServiceImpl.MAPPING_BATCH_SIZE) MappingProjectRepository(org.molgenis.semanticmapper.repository.MappingProjectRepository) EntityMapping(org.molgenis.semanticmapper.mapping.model.EntityMapping) AttributeType(org.molgenis.data.meta.AttributeType) Import(org.springframework.context.annotation.Import) Mockito.when(org.mockito.Mockito.when) MappingTargetMetaData(org.molgenis.semanticmapper.meta.MappingTargetMetaData) Consumer(java.util.function.Consumer) ROLE_ID(org.molgenis.data.meta.model.EntityType.AttributeRole.ROLE_ID) org.molgenis.data.meta.model(org.molgenis.data.meta.model) MappingTarget(org.molgenis.semanticmapper.mapping.model.MappingTarget) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Package(org.molgenis.data.meta.model.Package) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) DynamicEntity(org.molgenis.data.support.DynamicEntity) MappingProject(org.molgenis.semanticmapper.mapping.model.MappingProject) Consumer(java.util.function.Consumer) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Test(org.testng.annotations.Test)

Example 2 with PermissionSystemService

use of org.molgenis.data.security.permission.PermissionSystemService 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);
}
Also used : DefaultPackage(org.molgenis.data.meta.DefaultPackage) Progress(org.molgenis.jobs.Progress) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Autowired(org.springframework.beans.factory.annotation.Autowired) Test(org.testng.annotations.Test) AlgorithmService(org.molgenis.semanticmapper.service.AlgorithmService) PermissionSystemService(org.molgenis.data.security.permission.PermissionSystemService) MetaDataService(org.molgenis.data.meta.MetaDataService) Collections.singletonList(java.util.Collections.singletonList) User(org.molgenis.data.security.auth.User) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) JsMagmaScriptEvaluator(org.molgenis.js.magma.JsMagmaScriptEvaluator) MappingProject(org.molgenis.semanticmapper.mapping.model.MappingProject) SOURCE(org.molgenis.semanticmapper.service.impl.MappingServiceImpl.SOURCE) Collectors.toSet(java.util.stream.Collectors.toSet) UserFactory(org.molgenis.data.security.auth.UserFactory) UserTestConfig(org.molgenis.data.security.config.UserTestConfig) org.mockito(org.mockito) org.molgenis.data(org.molgenis.data) AttributeMapping(org.molgenis.semanticmapper.mapping.model.AttributeMapping) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) MappingProjectMetaData(org.molgenis.semanticmapper.meta.MappingProjectMetaData) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) Stream(java.util.stream.Stream) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) EntityBaseTestConfig(org.molgenis.data.config.EntityBaseTestConfig) Strictness(org.mockito.quality.Strictness) EntityUtils(org.molgenis.data.util.EntityUtils) Assert.assertEquals(org.testng.Assert.assertEquals) Lists(com.google.common.collect.Lists) DynamicEntity(org.molgenis.data.support.DynamicEntity) Assert(org.testng.Assert) MAPPING_BATCH_SIZE(org.molgenis.semanticmapper.service.impl.MappingServiceImpl.MAPPING_BATCH_SIZE) MappingProjectRepository(org.molgenis.semanticmapper.repository.MappingProjectRepository) EntityMapping(org.molgenis.semanticmapper.mapping.model.EntityMapping) AttributeType(org.molgenis.data.meta.AttributeType) Import(org.springframework.context.annotation.Import) Mockito.when(org.mockito.Mockito.when) MappingTargetMetaData(org.molgenis.semanticmapper.meta.MappingTargetMetaData) Consumer(java.util.function.Consumer) ROLE_ID(org.molgenis.data.meta.model.EntityType.AttributeRole.ROLE_ID) org.molgenis.data.meta.model(org.molgenis.data.meta.model) MappingTarget(org.molgenis.semanticmapper.mapping.model.MappingTarget) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Package(org.molgenis.data.meta.model.Package) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) DynamicEntity(org.molgenis.data.support.DynamicEntity) MappingProject(org.molgenis.semanticmapper.mapping.model.MappingProject) DynamicEntity(org.molgenis.data.support.DynamicEntity) Consumer(java.util.function.Consumer) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Test(org.testng.annotations.Test)

Aggregations

Lists (com.google.common.collect.Lists)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Collections.emptyList (java.util.Collections.emptyList)2 Collections.singletonList (java.util.Collections.singletonList)2 List (java.util.List)2 Set (java.util.Set)2 Consumer (java.util.function.Consumer)2 Collectors.toSet (java.util.stream.Collectors.toSet)2 Stream (java.util.stream.Stream)2 org.mockito (org.mockito)2 ArgumentMatchers.argThat (org.mockito.ArgumentMatchers.argThat)2 Mockito.when (org.mockito.Mockito.when)2 Strictness (org.mockito.quality.Strictness)2 org.molgenis.data (org.molgenis.data)2 EntityBaseTestConfig (org.molgenis.data.config.EntityBaseTestConfig)2 AttributeType (org.molgenis.data.meta.AttributeType)2 DefaultPackage (org.molgenis.data.meta.DefaultPackage)2