Search in sources :

Example 21 with MappingProject

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

the class AlgorithmServiceImplIT method testWhenSourceHasMultipleMatchesThenFirstMappingGetsCreated.

@Test
public void testWhenSourceHasMultipleMatchesThenFirstMappingGetsCreated() {
    EntityType targetEntityType = entityTypeFactory.create("target");
    Attribute targetAttribute = attrMetaFactory.create().setName("targetHeight");
    targetAttribute.setDescription("height");
    targetEntityType.addAttribute(targetAttribute);
    EntityType sourceEntityType = entityTypeFactory.create("source");
    Attribute sourceAttribute1 = attrMetaFactory.create().setName("sourceHeight1");
    sourceAttribute1.setDescription("height");
    Attribute sourceAttribute2 = attrMetaFactory.create().setName("sourceHeight2");
    sourceAttribute2.setDescription("height");
    sourceEntityType.addAttributes(Arrays.asList(sourceAttribute1, sourceAttribute2));
    User owner = userFactory.create();
    owner.setUsername("flup");
    owner.setPassword("geheim");
    owner.setId("12345");
    owner.setActive(true);
    owner.setEmail("flup@blah.com");
    owner.setFirstName("Flup");
    owner.setLastName("de Flap");
    MappingProject project = new MappingProject("project", owner);
    project.addTarget(targetEntityType);
    EntityMapping mapping = project.getMappingTarget("target").addSource(sourceEntityType);
    Map<Attribute, ExplainedAttribute> mappings = ImmutableMap.of(sourceAttribute1, ExplainedAttribute.create(sourceAttribute1), sourceAttribute2, ExplainedAttribute.create(sourceAttribute2));
    LinkedHashMultimap<Relation, OntologyTerm> ontologyTermTags = LinkedHashMultimap.create();
    when(semanticSearchService.decisionTreeToFindRelevantAttributes(sourceEntityType, targetAttribute, ontologyTermTags.values(), null)).thenReturn(mappings);
    when(ontologyTagService.getTagsForAttribute(targetEntityType, targetAttribute)).thenReturn(ontologyTermTags);
    algorithmService.autoGenerateAlgorithm(sourceEntityType, targetEntityType, mapping, targetAttribute);
    assertEquals(mapping.getAttributeMapping("targetHeight").getSourceAttributes().get(0), sourceAttribute1);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) EntityMapping(org.molgenis.semanticmapper.mapping.model.EntityMapping) MappingProject(org.molgenis.semanticmapper.mapping.model.MappingProject) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) Relation(org.molgenis.data.semantic.Relation) User(org.molgenis.data.security.auth.User) Attribute(org.molgenis.data.meta.model.Attribute) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 22 with MappingProject

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

the class MappingServiceImplTest method createMappingProjectWithMappings.

private MappingProject createMappingProjectWithMappings() {
    MappingProject mappingProject = mappingService.addMappingProject("TestRun", user, hopMetaData.getId());
    MappingTarget target = mappingProject.getMappingTarget(hopMetaData.getId());
    when(mappingProjectRepo.getMappingProject("TestRun")).thenReturn(mappingProject);
    EntityMapping mapping = target.addSource(geneMetaData);
    AttributeMapping idMapping = mapping.addAttributeMapping("identifier");
    idMapping.setAlgorithm("$('id').value()");
    AttributeMapping attrMapping = mapping.addAttributeMapping("height");
    attrMapping.setAlgorithm("$('length').value()");
    return mappingProject;
}
Also used : EntityMapping(org.molgenis.semanticmapper.mapping.model.EntityMapping) MappingProject(org.molgenis.semanticmapper.mapping.model.MappingProject) AttributeMapping(org.molgenis.semanticmapper.mapping.model.AttributeMapping) MappingTarget(org.molgenis.semanticmapper.mapping.model.MappingTarget)

Example 23 with MappingProject

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

the class MappingServiceImplTest method testUpdateMappingProject.

@Test
public void testUpdateMappingProject() {
    MappingProject mappingProject = Mockito.mock(MappingProject.class);
    mappingService.updateMappingProject(mappingProject);
    Mockito.verifyZeroInteractions(mappingProject);
    Mockito.verify(mappingProjectRepo).update(mappingProject);
}
Also used : MappingProject(org.molgenis.semanticmapper.mapping.model.MappingProject) Test(org.testng.annotations.Test)

Example 24 with MappingProject

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

Example 25 with MappingProject

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

the class MappingServiceImplTest method testCloneMappingProjectString.

@Test
public void testCloneMappingProjectString() {
    MappingProject mappingProject = Mockito.mock(MappingProject.class);
    when(mappingProjectRepo.getMappingProject("0")).thenReturn(mappingProject);
    mappingService.cloneMappingProject("0");
    Mockito.verify(mappingProject).removeIdentifiers();
    Mockito.verify(mappingProjectRepo).add(mappingProject);
}
Also used : MappingProject(org.molgenis.semanticmapper.mapping.model.MappingProject) 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