Search in sources :

Example 6 with MappingProject

use of org.molgenis.semanticmapper.mapping.model.MappingProject in project molgenis by molgenis.

the class MappingServiceImplTest method testIncompatibleMetaDataUnknownAttribute.

@Test(expectedExceptions = MolgenisDataException.class, expectedExceptionsMessageRegExp = "Target repository does not contain the following attribute: COUNTRY_1")
public void testIncompatibleMetaDataUnknownAttribute() {
    String targetRepositoryName = "targetRepository";
    @SuppressWarnings("unchecked") Repository<Entity> targetRepository = Mockito.mock(Repository.class);
    EntityType targetRepositoryMetaData = entityTypeFactory.create(targetRepositoryName);
    targetRepositoryMetaData.addAttribute(attrMetaFactory.create().setName("ID").setDataType(STRING), ROLE_ID);
    targetRepositoryMetaData.addAttribute(attrMetaFactory.create().setName("COUNTRY").setDataType(STRING));
    targetRepositoryMetaData.setPackage(package_);
    when(dataService.hasRepository(targetRepositoryName)).thenReturn(true);
    when(dataService.getRepository(targetRepositoryName)).thenReturn(targetRepository);
    when(targetRepository.getEntityType()).thenReturn(targetRepositoryMetaData);
    EntityType mappingTargetMetaData = entityTypeFactory.create("mapping_target");
    mappingTargetMetaData.addAttribute(attrMetaFactory.create().setName("ID").setDataType(STRING), ROLE_ID);
    mappingTargetMetaData.addAttribute(attrMetaFactory.create().setName("COUNTRY_1").setDataType(STRING));
    mappingTargetMetaData.setPackage(package_);
    MappingTarget mappingTarget = new MappingTarget(mappingTargetMetaData);
    MappingProject mappingProject = Mockito.mock(MappingProject.class);
    when(mappingProject.getMappingTargets()).thenReturn(newArrayList(mappingTarget));
    when(mappingProjectRepo.getMappingProject("TestRun")).thenReturn(mappingProject);
    mappingService.applyMappings("TestRun", targetRepositoryName, false, "packageId", "label", progress);
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) MappingProject(org.molgenis.semanticmapper.mapping.model.MappingProject) MappingTarget(org.molgenis.semanticmapper.mapping.model.MappingTarget) Test(org.testng.annotations.Test)

Example 7 with MappingProject

use of org.molgenis.semanticmapper.mapping.model.MappingProject in project molgenis by molgenis.

the class MappingServiceImplTest method testAddMappingProject.

@Test
public void testAddMappingProject() {
    String projectName = "test_project";
    MappingProject mappingProject = mappingService.addMappingProject(projectName, user, hopMetaData.getId());
    Mockito.verify(mappingProjectRepo, Mockito.times(1)).add(mappingProject);
    assertEquals(mappingProject.getName(), projectName);
    assertEquals(mappingProject.getOwner(), user);
    List<MappingTarget> mappingTargets = mappingProject.getMappingTargets();
    assertEquals(mappingTargets.size(), 1);
    assertEquals(mappingTargets.get(0).getTarget(), hopMetaData);
}
Also used : MappingProject(org.molgenis.semanticmapper.mapping.model.MappingProject) MappingTarget(org.molgenis.semanticmapper.mapping.model.MappingTarget) Test(org.testng.annotations.Test)

Example 8 with MappingProject

use of org.molgenis.semanticmapper.mapping.model.MappingProject in project molgenis by molgenis.

the class MappingServiceImplTest method testGetMappingProject.

@Test
public void testGetMappingProject() {
    String projectName = "test_project";
    MappingProject actualAddedMappingProject = mappingService.addMappingProject(projectName, user, hopMetaData.getId());
    String mappingTargetIdentifier = actualAddedMappingProject.getMappingTarget(hopMetaData.getId()).getIdentifier();
    MappingTarget expectedMappingTarget = getManualMappingTarget(mappingTargetIdentifier, emptyList());
    String mappingProjectIdentifier = actualAddedMappingProject.getIdentifier();
    MappingProject expectedMappingProject = getManualMappingProject(mappingProjectIdentifier, projectName, expectedMappingTarget);
    Entity mappingTargetEntity = Mockito.mock(Entity.class);
    when(mappingTargetEntity.getString(MappingTargetMetaData.IDENTIFIER)).thenReturn(mappingTargetIdentifier);
    when(mappingTargetEntity.getString(MappingTargetMetaData.TARGET)).thenReturn(hopMetaData.getId());
    when(mappingTargetEntity.getEntities(MappingTargetMetaData.ENTITY_MAPPINGS)).thenReturn(null);
    when(dataService.getEntityType(mappingTargetEntity.getString(MappingTargetMetaData.TARGET))).thenReturn(hopMetaData);
    Entity mappingProjectEntity = Mockito.mock(Entity.class);
    when(mappingProjectEntity.get(IDENTIFIER)).thenReturn(mappingProjectIdentifier);
    when(mappingProjectEntity.get(NAME)).thenReturn(projectName);
    when(mappingProjectEntity.get(OWNER)).thenReturn(user);
    when(mappingProjectEntity.getEntities(MAPPING_TARGETS)).thenReturn(singletonList(mappingTargetEntity));
    when(mappingProjectEntity.get(MAPPING_TARGETS)).thenReturn(singletonList(expectedMappingTarget));
    when(dataService.findOneById(MAPPING_PROJECT, mappingProjectIdentifier)).thenReturn(mappingProjectEntity);
    when(mappingProjectRepo.getMappingProject(mappingProjectIdentifier)).thenReturn(actualAddedMappingProject);
    MappingProject retrievedMappingProject = mappingService.getMappingProject(mappingProjectIdentifier);
    assertEquals(retrievedMappingProject, expectedMappingProject);
}
Also used : MappingProject(org.molgenis.semanticmapper.mapping.model.MappingProject) DynamicEntity(org.molgenis.data.support.DynamicEntity) MappingTarget(org.molgenis.semanticmapper.mapping.model.MappingTarget) Test(org.testng.annotations.Test)

Example 9 with MappingProject

use of org.molgenis.semanticmapper.mapping.model.MappingProject 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 10 with MappingProject

use of org.molgenis.semanticmapper.mapping.model.MappingProject in project molgenis by molgenis.

the class MappingServiceImplTest method testIncompatibleMetaDataDifferentType.

@Test(expectedExceptions = MolgenisDataException.class, expectedExceptionsMessageRegExp = "attribute COUNTRY in the mapping target is type INT while attribute " + "COUNTRY in the target repository is type STRING. Please make sure the types are the same")
public void testIncompatibleMetaDataDifferentType() {
    String targetRepositoryName = "target_repository";
    @SuppressWarnings("unchecked") Repository<Entity> targetRepository = Mockito.mock(Repository.class);
    EntityType targetRepositoryMetaData = entityTypeFactory.create(targetRepositoryName);
    targetRepositoryMetaData.addAttribute(attrMetaFactory.create().setName("ID").setDataType(STRING), ROLE_ID);
    targetRepositoryMetaData.addAttribute(attrMetaFactory.create().setName("COUNTRY").setDataType(STRING));
    targetRepositoryMetaData.setPackage(package_);
    when(dataService.hasRepository(targetRepositoryName)).thenReturn(true);
    when(dataService.getRepository(targetRepositoryName)).thenReturn(targetRepository);
    when(targetRepository.getEntityType()).thenReturn(targetRepositoryMetaData);
    EntityType mappingTargetMetaData = entityTypeFactory.create("mapping_target");
    mappingTargetMetaData.addAttribute(attrMetaFactory.create().setName("ID").setDataType(STRING), ROLE_ID);
    mappingTargetMetaData.addAttribute(attrMetaFactory.create().setName("COUNTRY").setDataType(INT));
    mappingTargetMetaData.setPackage(package_);
    MappingTarget mappingTarget = new MappingTarget(mappingTargetMetaData);
    MappingProject mappingProject = Mockito.mock(MappingProject.class);
    when(mappingProject.getMappingTargets()).thenReturn(newArrayList(mappingTarget));
    when(mappingProjectRepo.getMappingProject("TestRun")).thenReturn(mappingProject);
    mappingService.applyMappings("TestRun", targetRepositoryName, false, "packageId", "label", progress);
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) MappingProject(org.molgenis.semanticmapper.mapping.model.MappingProject) MappingTarget(org.molgenis.semanticmapper.mapping.model.MappingTarget) Test(org.testng.annotations.Test)

Aggregations

MappingProject (org.molgenis.semanticmapper.mapping.model.MappingProject)26 Test (org.testng.annotations.Test)18 MappingTarget (org.molgenis.semanticmapper.mapping.model.MappingTarget)14 DynamicEntity (org.molgenis.data.support.DynamicEntity)9 EntityMapping (org.molgenis.semanticmapper.mapping.model.EntityMapping)9 User (org.molgenis.data.security.auth.User)7 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)6 AttributeMapping (org.molgenis.semanticmapper.mapping.model.AttributeMapping)6 EntityType (org.molgenis.data.meta.model.EntityType)5 BeforeMethod (org.testng.annotations.BeforeMethod)4 Menu (org.molgenis.core.ui.menu.Menu)3 Attribute (org.molgenis.data.meta.model.Attribute)3 Transactional (org.springframework.transaction.annotation.Transactional)3 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