Search in sources :

Example 16 with DynamicEntity

use of org.molgenis.data.support.DynamicEntity in project molgenis by molgenis.

the class OntologyTermRepositoryTest method testGetChildOntologyTermsByNodePath.

@Test
public void testGetChildOntologyTermsByNodePath() {
    Entity ontologyEntity = new DynamicEntity(ontologyMetaData);
    ontologyEntity.set(OntologyMetaData.ONTOLOGY_IRI, "http://www.molgenis.org");
    ontologyEntity.set(OntologyMetaData.ONTOLOGY_NAME, "molgenis");
    Entity nodePathEntity_1 = new DynamicEntity(ontologyTermNodePathMetaData);
    nodePathEntity_1.set(OntologyTermNodePathMetaData.NODE_PATH, "0[0].1[1]");
    Entity nodePathEntity_2 = new DynamicEntity(ontologyTermNodePathMetaData);
    nodePathEntity_2.set(OntologyTermNodePathMetaData.NODE_PATH, "0[0].1[1].0[2]");
    Entity nodePathEntity_3 = new DynamicEntity(ontologyTermNodePathMetaData);
    nodePathEntity_3.set(OntologyTermNodePathMetaData.NODE_PATH, "0[0].1[1].1[2]");
    Entity ontologyTerm_2 = new DynamicEntity(ontologyTermMetaData);
    ontologyTerm_2.set(ONTOLOGY, ontologyEntity);
    ontologyTerm_2.set(ONTOLOGY_TERM_IRI, "iri 2");
    ontologyTerm_2.set(ONTOLOGY_TERM_NAME, "name 2");
    ontologyTerm_2.set(OntologyTermMetaData.ONTOLOGY_TERM_NODE_PATH, asList(nodePathEntity_1, nodePathEntity_2));
    ontologyTerm_2.set(ONTOLOGY_TERM_SYNONYM, emptyList());
    Entity ontologyTerm_3 = new DynamicEntity(ontologyTermMetaData);
    ontologyTerm_3.set(ONTOLOGY, ontologyEntity);
    ontologyTerm_3.set(ONTOLOGY_TERM_IRI, "iri 3");
    ontologyTerm_3.set(ONTOLOGY_TERM_NAME, "name 3");
    ontologyTerm_3.set(OntologyTermMetaData.ONTOLOGY_TERM_NODE_PATH, singletonList(nodePathEntity_3));
    ontologyTerm_3.set(ONTOLOGY_TERM_SYNONYM, emptyList());
    when(dataService.findAll(ONTOLOGY_TERM, new QueryImpl<>(new QueryRule(OntologyTermMetaData.ONTOLOGY_TERM_NODE_PATH, QueryRule.Operator.FUZZY_MATCH, "\"0[0].1[1]\"")).and().eq(ONTOLOGY, ontologyEntity))).thenReturn(Stream.of(ontologyTerm_2, ontologyTerm_3));
    List<OntologyTerm> childOntologyTermsByNodePath = ontologyTermRepository.getChildOntologyTermsByNodePath(ontologyEntity, nodePathEntity_1);
    assertEquals(childOntologyTermsByNodePath.size(), 2);
    assertEquals(childOntologyTermsByNodePath.get(0), OntologyTerm.create("iri 2", "name 2", null, singletonList("name 2")));
    assertEquals(childOntologyTermsByNodePath.get(1), OntologyTerm.create("iri 3", "name 3", null, singletonList("name 3")));
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) DynamicEntity(org.molgenis.data.support.DynamicEntity) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 17 with DynamicEntity

use of org.molgenis.data.support.DynamicEntity in project molgenis by molgenis.

the class RestControllerTest method beforeMethod.

@BeforeMethod
public void beforeMethod() {
    MockitoAnnotations.initMocks(this);
    reset(permissionService, dataService, metaDataService, tokenService);
    when(dataService.getMeta()).thenReturn(metaDataService);
    @SuppressWarnings("unchecked") Repository<Entity> repo = mock(Repository.class);
    // test entity meta data
    EntityType entityType = mock(EntityType.class);
    Attribute attrId = when(mock(Attribute.class).getName()).thenReturn("id").getMock();
    when(attrId.getLabel()).thenReturn("id");
    when(attrId.getLabel("en")).thenReturn("id");
    when(attrId.getDataType()).thenReturn(STRING);
    when(attrId.isReadOnly()).thenReturn(true);
    when(attrId.isUnique()).thenReturn(true);
    when(attrId.isNillable()).thenReturn(false);
    when(attrId.isVisible()).thenReturn(false);
    when(attrId.getChildren()).thenReturn(emptyList());
    when(attrId.getEnumOptions()).thenReturn(emptyList());
    Attribute attrName = when(mock(Attribute.class).getName()).thenReturn("name").getMock();
    when(attrName.getLabel()).thenReturn("name");
    when(attrName.getLabel("en")).thenReturn("name");
    when(attrName.getDataType()).thenReturn(STRING);
    when(attrName.isNillable()).thenReturn(true);
    when(attrName.isVisible()).thenReturn(true);
    when(attrName.getChildren()).thenReturn(emptyList());
    when(attrName.getEnumOptions()).thenReturn(emptyList());
    Attribute attrEnum = when(mock(Attribute.class).getName()).thenReturn("enum").getMock();
    when(attrEnum.getLabel()).thenReturn("enum");
    when(attrEnum.getLabel("en")).thenReturn("enum");
    when(attrEnum.getDataType()).thenReturn(AttributeType.ENUM);
    when(attrEnum.getEnumOptions()).thenReturn(singletonList("enum0, enum1"));
    when(attrEnum.isNillable()).thenReturn(true);
    when(attrEnum.isVisible()).thenReturn(true);
    when(attrEnum.getChildren()).thenReturn(emptyList());
    Attribute attrInt = when(mock(Attribute.class).getName()).thenReturn("int").getMock();
    when(attrInt.getLabel()).thenReturn("int");
    when(attrInt.getLabel("en")).thenReturn("int");
    when(attrInt.getDataType()).thenReturn(AttributeType.INT);
    when(attrInt.isNillable()).thenReturn(true);
    when(attrInt.isVisible()).thenReturn(true);
    when(attrInt.getChildren()).thenReturn(emptyList());
    when(entityType.getAttribute("id")).thenReturn(attrId);
    when(entityType.getAttribute("name")).thenReturn(attrName);
    when(entityType.getAttribute("enum")).thenReturn(attrEnum);
    when(entityType.getAttribute("int")).thenReturn(attrInt);
    when(entityType.getMappedByAttributes()).thenReturn(Stream.empty());
    when(entityType.getIdAttribute()).thenReturn(attrId);
    // TODO: This upgrades the test to mockito 2 but actually shows an error in the test
    when(entityType.getLookupAttributes()).thenReturn(null);
    when(entityType.getAttributes()).thenReturn(asList(attrName, attrId, attrEnum, attrInt));
    when(entityType.getAtomicAttributes()).thenReturn(asList(attrName, attrId, attrEnum, attrInt));
    when(entityType.getId()).thenReturn(ENTITY_NAME);
    when(entityType.getLabel("en")).thenReturn(null);
    when(repo.getEntityType()).thenReturn(entityType);
    when(repo.getName()).thenReturn(ENTITY_NAME);
    when(dataService.getEntityType(ENTITY_NAME)).thenReturn(entityType);
    when(entityManager.create(entityType, POPULATE)).thenReturn(new DynamicEntity(entityType));
    // test entities
    Entity entityXref = new DynamicEntity(entityType);
    entityXref.set("id", ENTITY_UNTYPED_ID);
    entityXref.set("name", "PietXREF");
    Entity entity = new DynamicEntity(entityType);
    entity.set("id", ENTITY_UNTYPED_ID);
    entity.set("name", "Piet");
    entity.set("enum", "enum1");
    entity.set("int", 1);
    Entity entity2 = new DynamicEntity(entityType);
    entity2.set("id", "p2");
    entity2.set("name", "Klaas");
    entity2.set("int", 2);
    when(dataService.getEntityTypeIds()).thenReturn(Stream.of(ENTITY_NAME));
    when(dataService.getRepository(ENTITY_NAME)).thenReturn(repo);
    when(dataService.findOneById(ArgumentMatchers.eq(ENTITY_NAME), ArgumentMatchers.eq(ENTITY_UNTYPED_ID), any(Fetch.class))).thenReturn(entity);
    when(dataService.findOneById(ENTITY_NAME, ENTITY_UNTYPED_ID)).thenReturn(entity);
    Query<Entity> q = new QueryImpl<>().eq("name", "Piet").pageSize(10).offset(5);
    when(dataService.findAll(ENTITY_NAME, q)).thenReturn(Stream.of(entity));
    Query<Entity> q2 = new QueryImpl<>().sort(new Sort().on("name", Sort.Direction.DESC)).pageSize(100).offset(0);
    when(dataService.findAll(ENTITY_NAME, q2)).thenReturn(Stream.of(entity2, entity));
    when(localeResolver.resolveLocale(any())).thenReturn(ENGLISH);
    mockMvc = MockMvcBuilders.standaloneSetup(restController).setMessageConverters(gsonHttpMessageConverter, new CsvHttpMessageConverter()).setCustomArgumentResolvers(new TokenExtractor()).setControllerAdvice(new GlobalControllerExceptionHandler(), new FallbackExceptionHandler(), new SpringExceptionHandler()).setLocaleResolver(localeResolver).build();
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) Attribute(org.molgenis.data.meta.model.Attribute) DynamicEntity(org.molgenis.data.support.DynamicEntity) SpringExceptionHandler(org.molgenis.web.exception.SpringExceptionHandler) CsvHttpMessageConverter(org.molgenis.core.ui.messageconverter.CsvHttpMessageConverter) EntityType(org.molgenis.data.meta.model.EntityType) TokenExtractor(org.molgenis.security.token.TokenExtractor) GlobalControllerExceptionHandler(org.molgenis.web.exception.GlobalControllerExceptionHandler) FallbackExceptionHandler(org.molgenis.web.exception.FallbackExceptionHandler) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 18 with DynamicEntity

use of org.molgenis.data.support.DynamicEntity in project molgenis by molgenis.

the class RestControllerTest method retrieveEntityAttributeXref.

@Test
public void retrieveEntityAttributeXref() throws Exception {
    reset(dataService);
    @SuppressWarnings("unchecked") Repository<Entity> repo = mock(Repository.class);
    when(dataService.getRepository(ENTITY_NAME)).thenReturn(repo);
    when(dataService.getEntityTypeIds()).thenReturn(Stream.of(ENTITY_NAME));
    // entity meta data
    EntityType refEntityType = when(mock(EntityType.class).getId()).thenReturn("refEntity").getMock();
    Attribute attrId = when(mock(Attribute.class).getName()).thenReturn("id").getMock();
    when(attrId.getLabel()).thenReturn("id");
    when(attrId.getLabel(ArgumentMatchers.anyString())).thenReturn("id");
    when(attrId.getDataType()).thenReturn(STRING);
    when(attrId.isReadOnly()).thenReturn(true);
    when(attrId.isUnique()).thenReturn(true);
    when(attrId.isNillable()).thenReturn(false);
    when(attrId.isVisible()).thenReturn(false);
    when(attrId.getChildren()).thenReturn(emptyList());
    when(attrId.getEnumOptions()).thenReturn(emptyList());
    Attribute attrName = when(mock(Attribute.class).getName()).thenReturn("name").getMock();
    when(attrName.getLabel()).thenReturn("name");
    when(attrName.getLabel(ArgumentMatchers.anyString())).thenReturn("name");
    when(attrName.getDataType()).thenReturn(STRING);
    when(attrName.isNillable()).thenReturn(true);
    when(attrName.isVisible()).thenReturn(true);
    when(attrName.getChildren()).thenReturn(emptyList());
    when(attrName.getEnumOptions()).thenReturn(emptyList());
    when(refEntityType.getAttribute("id")).thenReturn(attrId);
    when(refEntityType.getAttribute("name")).thenReturn(attrName);
    when(refEntityType.getIdAttribute()).thenReturn(attrId);
    when(refEntityType.getAttributes()).thenReturn(asList(attrId, attrName));
    when(refEntityType.getAtomicAttributes()).thenReturn(asList(attrId, attrName));
    when(refEntityType.getId()).thenReturn("refEntity");
    EntityType entityType = when(mock(EntityType.class).getId()).thenReturn(ENTITY_NAME).getMock();
    Attribute attrXref = when(mock(Attribute.class).getName()).thenReturn("xrefValue").getMock();
    when(attrXref.getLabel()).thenReturn("xrefValue");
    when(attrXref.getLabel(ArgumentMatchers.anyString())).thenReturn("xrefValue");
    when(attrXref.getDataType()).thenReturn(AttributeType.XREF);
    when(attrXref.isNillable()).thenReturn(true);
    when(attrXref.isVisible()).thenReturn(true);
    when(attrXref.getChildren()).thenReturn(emptyList());
    when(attrXref.getEnumOptions()).thenReturn(emptyList());
    when(attrXref.getRefEntity()).thenReturn(refEntityType);
    when(entityType.getAttribute("id")).thenReturn(attrId);
    when(entityType.getAttribute("xrefValue")).thenReturn(attrXref);
    when(entityType.getIdAttribute()).thenReturn(attrId);
    when(entityType.getAttributes()).thenReturn(asList(attrId, attrXref));
    when(entityType.getAtomicAttributes()).thenReturn(asList(attrId, attrXref));
    when(entityType.getId()).thenReturn(ENTITY_NAME);
    Entity entityXref = new DynamicEntity(refEntityType);
    entityXref.set("id", ENTITY_UNTYPED_ID);
    entityXref.set("name", "Piet");
    Entity entity = new DynamicEntity(entityType);
    entity.set("id", ENTITY_UNTYPED_ID);
    entity.set("xrefValue", entityXref);
    when(dataService.findOneById(ENTITY_NAME, ENTITY_UNTYPED_ID)).thenReturn(entity);
    when(dataService.findOneById("refEntity", ENTITY_UNTYPED_ID)).thenReturn(entityXref);
    when(dataService.getEntityType(ENTITY_NAME)).thenReturn(entityType);
    when(dataService.getEntityType("refEntity")).thenReturn(refEntityType);
    mockMvc = MockMvcBuilders.standaloneSetup(restController).setMessageConverters(gsonHttpMessageConverter).build();
    mockMvc.perform(get(HREF_ENTITY_ID + "/xrefValue")).andExpect(status().isOk()).andExpect(content().contentType(APPLICATION_JSON_UTF8)).andExpect(content().string("{\"href\":\"/api/v1/Person/p1/xrefValue\",\"id\":\"p1\",\"name\":\"Piet\"}"));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) DynamicEntity(org.molgenis.data.support.DynamicEntity) Attribute(org.molgenis.data.meta.model.Attribute) DynamicEntity(org.molgenis.data.support.DynamicEntity) Test(org.testng.annotations.Test)

Example 19 with DynamicEntity

use of org.molgenis.data.support.DynamicEntity in project molgenis by molgenis.

the class MappingProjectRepositoryImplTest method beforeMethod.

@BeforeMethod
public void beforeMethod() {
    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");
    EntityType target1 = entityTypeFactory.create("target1");
    target1.addAttribute(attrMetaFactory.create().setName("id"), ROLE_ID);
    EntityType target2 = entityTypeFactory.create("target2");
    target2.addAttribute(attrMetaFactory.create().setName("id"), ROLE_ID);
    mappingProject = new MappingProject("My first mapping project", owner);
    mappingTarget1 = mappingProject.addTarget(target1);
    mappingTarget2 = mappingProject.addTarget(target2);
    Entity mappingTargetEntity = new DynamicEntity(mappingTargetMeta);
    mappingTargetEntity.set(MappingTargetMetaData.TARGET, "target1");
    mappingTargetEntity.set(MappingTargetMetaData.IDENTIFIER, "mappingTargetID1");
    Entity mappingTargetEntity2 = new DynamicEntity(mappingTargetMeta);
    mappingTargetEntity2.set(MappingTargetMetaData.TARGET, "target2");
    mappingTargetEntity2.set(MappingTargetMetaData.IDENTIFIER, "mappingTargetID2");
    mappingTargetEntities = asList(mappingTargetEntity, mappingTargetEntity2);
    mappingProjectEntity = new DynamicEntity(mappingProjectMeta);
    mappingProjectEntity.set(IDENTIFIER, "mappingProjectID");
    mappingProjectEntity.set(MAPPING_TARGETS, mappingTargetEntities);
    mappingProjectEntity.set(OWNER, owner);
    mappingProjectEntity.set(NAME, "My first mapping project");
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) MappingProject(org.molgenis.semanticmapper.mapping.model.MappingProject) DynamicEntity(org.molgenis.data.support.DynamicEntity) DynamicEntity(org.molgenis.data.support.DynamicEntity) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 20 with DynamicEntity

use of org.molgenis.data.support.DynamicEntity in project molgenis by molgenis.

the class AlgorithmServiceImplIT method testGetXrefScript.

@Test
public void testGetXrefScript() throws ParseException {
    // xref entities
    EntityType entityTypeXref = entityTypeFactory.create("xrefEntity1");
    entityTypeXref.addAttribute(attrMetaFactory.create().setName("id").setDataType(INT), ROLE_ID);
    entityTypeXref.addAttribute(attrMetaFactory.create().setName("field1"));
    Entity xref1a = new DynamicEntity(entityTypeXref);
    xref1a.set("id", 1);
    xref1a.set("field1", "Test");
    EntityType entityTypeXref2 = entityTypeFactory.create("xrefEntity2");
    entityTypeXref2.addAttribute(attrMetaFactory.create().setName("id").setDataType(INT), ROLE_ID);
    entityTypeXref2.addAttribute(attrMetaFactory.create().setName("field2"));
    Entity xref2a = new DynamicEntity(entityTypeXref2);
    xref2a.set("id", 2);
    xref2a.set("field2", "Test");
    // source Entity
    EntityType entityTypeSource = entityTypeFactory.create("Source");
    entityTypeSource.addAttribute(attrMetaFactory.create().setName("id").setDataType(INT), ROLE_ID);
    entityTypeSource.addAttribute(attrMetaFactory.create().setName("xref").setDataType(XREF));
    Entity source = new DynamicEntity(entityTypeSource);
    source.set("id", 1);
    source.set("xref", xref2a);
    Attribute targetAttribute = attrMetaFactory.create().setName("field1");
    targetAttribute.setDataType(XREF);
    targetAttribute.setRefEntity(entityTypeXref);
    AttributeMapping attributeMapping = new AttributeMapping(targetAttribute);
    attributeMapping.setAlgorithm("$('xref').map({'1':'2', '2':'1'}).value();");
    when(entityManager.getReference(entityTypeXref, 1)).thenReturn(xref1a);
    Entity result = (Entity) algorithmService.apply(attributeMapping, source, entityTypeSource);
    assertEquals(result.get("field1"), xref2a.get("field2"));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) DynamicEntity(org.molgenis.data.support.DynamicEntity) Attribute(org.molgenis.data.meta.model.Attribute) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) AttributeMapping(org.molgenis.semanticmapper.mapping.model.AttributeMapping) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Aggregations

DynamicEntity (org.molgenis.data.support.DynamicEntity)161 Entity (org.molgenis.data.Entity)123 Test (org.testng.annotations.Test)104 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)50 EntityType (org.molgenis.data.meta.model.EntityType)48 Attribute (org.molgenis.data.meta.model.Attribute)38 AttributeMapping (org.molgenis.semanticmapper.mapping.model.AttributeMapping)14 BeforeMethod (org.testng.annotations.BeforeMethod)14 ExplainedAttribute (org.molgenis.semanticsearch.explain.bean.ExplainedAttribute)7 EntityMapping (org.molgenis.semanticmapper.mapping.model.EntityMapping)5 BeforeClass (org.testng.annotations.BeforeClass)5 ArrayList (java.util.ArrayList)4 MolgenisDataException (org.molgenis.data.MolgenisDataException)4 EntityWithComputedAttributes (org.molgenis.data.support.EntityWithComputedAttributes)4 ExplainedQueryString (org.molgenis.semanticsearch.explain.bean.ExplainedQueryString)4 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)4 StringWriter (java.io.StringWriter)3 List (java.util.List)3 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)3 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)3