Search in sources :

Example 96 with QueryImpl

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

the class RestControllerV2Test method beforeMethod.

@BeforeMethod
public void beforeMethod() {
    reset(dataService);
    reset(repoCopier);
    EntityType refRefEntityType = entityTypeFactory.create(REF_REF_ENTITY_NAME).setLabel(REF_REF_ENTITY_NAME).addAttribute(attributeFactory.create().setName(REF_REF_ATTR_ID_NAME), ROLE_ID, ROLE_LOOKUP).addAttribute(attributeFactory.create().setName(REF_REF_ATTR_VALUE_NAME), ROLE_LABEL);
    EntityType selfRefEntityType = entityTypeFactory.create(SELF_REF_ENTITY_NAME).setLabel(SELF_REF_ENTITY_NAME).addAttribute(attributeFactory.create().setName("id"), ROLE_ID, ROLE_LABEL, ROLE_LOOKUP);
    selfRefEntityType.addAttribute(attributeFactory.create().setName("selfRef").setDataType(XREF).setRefEntity(selfRefEntityType));
    Entity selfRefEntity = new DynamicEntity(selfRefEntityType);
    selfRefEntity.set("id", "0");
    selfRefEntity.set("selfRef", selfRefEntity);
    EntityType refEntityType = entityTypeFactory.create(REF_ENTITY_NAME).setLabel(REF_ENTITY_NAME).addAttribute(attributeFactory.create().setName(REF_ATTR_ID_NAME), ROLE_ID, ROLE_LOOKUP).addAttribute(attributeFactory.create().setName(REF_ATTR_VALUE_NAME), ROLE_LABEL).addAttribute(attributeFactory.create().setName(REF_ATTR_REF_NAME).setDataType(XREF).setRefEntity(refRefEntityType));
    // required
    String attrIdName = "id";
    attrBoolName = "bool";
    String attrCategoricalName = "categorical";
    String attrCategoricalMrefName = "categorical_mref";
    attrCompoundName = "compound";
    attrCompoundAttr0Name = "compound_attr0";
    attrCompoundCategorical = "compound_categorical";
    attrCompoundAttrCompoundName = "compound_attrcompound";
    attrCompoundAttrCompoundAttr0Name = "compound_attrcompound_attr0";
    String attrDateName = "date";
    String attrDateTimeName = "date_time";
    String attrDecimalName = "decimal";
    String attrEmailName = "email";
    String attrEnumName = "enum";
    String attrHtmlName = "html";
    String attrHyperlinkName = "hyperlink";
    String attrIntName = "int";
    String attrLongName = "long";
    String attrMrefName = "mref";
    String attrScriptName = "script";
    attrStringName = "string";
    String attrTextName = "text";
    attrXrefName = "xref";
    // optional
    String attrBoolOptionalName = "boolOptional";
    String attrCategoricalOptionalName = "categoricalOptional";
    String attrCategoricalMrefOptionalName = "categorical_mrefOptional";
    String attrCompoundAttr0OptionalName = "compound_attr0Optional";
    String attrCompoundAttrCompoundAttr0OptionalName = "compound_attrcompound_attr0Optional";
    String attrDateOptionalName = "dateOptional";
    String attrDateTimeOptionalName = "date_timeOptional";
    String attrDecimalOptionalName = "decimalOptional";
    String attrEmailOptionalName = "emailOptional";
    String attrEnumOptionalName = "enumOptional";
    String attrHtmlOptionalName = "htmlOptional";
    String attrHyperlinkOptionalName = "hyperlinkOptional";
    String attrIntOptionalName = "intOptional";
    String attrLongOptionalName = "longOptional";
    String attrMrefOptionalName = "mrefOptional";
    String attrScriptOptionalName = "scriptOptional";
    String attrStringOptionalName = "stringOptional";
    String attrTextOptionalName = "textOptional";
    String attrXrefOptionalName = "xrefOptional";
    String enum0 = "enum0";
    String enum1 = "enum1";
    String enum2 = "enum2";
    // required
    entityType = entityTypeFactory.create(ENTITY_NAME).setLabel(ENTITY_NAME);
    Attribute attrId = attributeFactory.create().setName(attrIdName);
    entityType.addAttribute(attrId, ROLE_ID, ROLE_LABEL, ROLE_LOOKUP);
    Attribute attrBool = createAttributeMeta(entityType, attrBoolName, BOOL).setNillable(false);
    Attribute attrCategorical = createAttributeMeta(entityType, attrCategoricalName, CATEGORICAL, refEntityType).setNillable(false);
    Attribute attrCategoricalMref = createAttributeMeta(entityType, attrCategoricalMrefName, CATEGORICAL_MREF, refEntityType).setNillable(false);
    Attribute attrCompound = createAttributeMeta(entityType, attrCompoundName, COMPOUND);
    Attribute compoundAttr0 = createAttributeMeta(entityType, attrCompoundAttr0Name, STRING).setNillable(false).setParent(attrCompound);
    Attribute compoundAttrCategorical = createAttributeMeta(entityType, attrCompoundCategorical, CATEGORICAL, refEntityType).setNillable(false).setParent(attrCompound);
    Attribute compoundAttr0Optional = createAttributeMeta(entityType, attrCompoundAttr0OptionalName, STRING).setNillable(true).setParent(attrCompound);
    Attribute compoundAttrCompound = createAttributeMeta(entityType, attrCompoundAttrCompoundName, COMPOUND).setParent(attrCompound);
    Attribute compoundAttrCompoundAttr0 = createAttributeMeta(entityType, attrCompoundAttrCompoundAttr0Name, STRING).setNillable(false).setParent(compoundAttrCompound);
    Attribute compoundAttrCompoundAttr0Optional = createAttributeMeta(entityType, attrCompoundAttrCompoundAttr0OptionalName, STRING).setNillable(true).setParent(compoundAttrCompound);
    Attribute attrDate = createAttributeMeta(entityType, attrDateName, DATE).setNillable(false);
    Attribute attrDateTime = createAttributeMeta(entityType, attrDateTimeName, DATE_TIME).setNillable(false);
    Attribute attrDecimal = createAttributeMeta(entityType, attrDecimalName, DECIMAL, null).setReadOnly(true).setNillable(false);
    Attribute attrEmail = createAttributeMeta(entityType, attrEmailName, EMAIL).setNillable(false);
    Attribute attrEnum = createAttributeMeta(entityType, attrEnumName, ENUM).setEnumOptions(asList(enum0, enum1, enum2)).setNillable(false);
    Attribute attrHtml = createAttributeMeta(entityType, attrHtmlName, HTML).setNillable(false);
    Attribute attrHyperlink = createAttributeMeta(entityType, attrHyperlinkName, HYPERLINK).setNillable(false);
    Attribute attrInt = createAttributeMeta(entityType, attrIntName, INT).setNillable(false);
    Attribute attrLong = createAttributeMeta(entityType, attrLongName, LONG).setNillable(false);
    Attribute attrMref = createAttributeMeta(entityType, attrMrefName, MREF, refEntityType).setNillable(false);
    Attribute attrScript = createAttributeMeta(entityType, attrScriptName, SCRIPT).setNillable(false);
    Attribute attrString = createAttributeMeta(entityType, attrStringName, STRING).setNillable(false);
    Attribute attrText = createAttributeMeta(entityType, attrTextName, TEXT).setNillable(false);
    Attribute attrXref = createAttributeMeta(entityType, attrXrefName, XREF, refEntityType).setNillable(false);
    // optional
    Attribute attrBoolOptional = createAttributeMeta(entityType, attrBoolOptionalName, BOOL);
    Attribute attrCategoricalOptional = createAttributeMeta(entityType, attrCategoricalOptionalName, CATEGORICAL, refEntityType);
    Attribute attrCategoricalMrefOptional = createAttributeMeta(entityType, attrCategoricalMrefOptionalName, CATEGORICAL_MREF, refEntityType);
    Attribute attrDateOptional = createAttributeMeta(entityType, attrDateOptionalName, DATE);
    Attribute attrDateTimeOptional = createAttributeMeta(entityType, attrDateTimeOptionalName, DATE_TIME);
    Attribute attrDecimalOptional = createAttributeMeta(entityType, attrDecimalOptionalName, DECIMAL, null);
    Attribute attrEmailOptional = createAttributeMeta(entityType, attrEmailOptionalName, EMAIL);
    Attribute attrEnumOptional = createAttributeMeta(entityType, attrEnumOptionalName, ENUM).setEnumOptions(asList(enum0, enum1, enum2));
    Attribute attrHtmlOptional = createAttributeMeta(entityType, attrHtmlOptionalName, HTML);
    Attribute attrHyperlinkOptional = createAttributeMeta(entityType, attrHyperlinkOptionalName, HYPERLINK);
    Attribute attrIntOptional = createAttributeMeta(entityType, attrIntOptionalName, INT);
    Attribute attrLongOptional = createAttributeMeta(entityType, attrLongOptionalName, LONG);
    Attribute attrMrefOptional = createAttributeMeta(entityType, attrMrefOptionalName, MREF, refEntityType);
    Attribute attrScriptOptional = createAttributeMeta(entityType, attrScriptOptionalName, SCRIPT);
    Attribute attrStringOptional = createAttributeMeta(entityType, attrStringOptionalName, STRING);
    Attribute attrTextOptional = createAttributeMeta(entityType, attrTextOptionalName, TEXT);
    Attribute attrXrefOptional = createAttributeMeta(entityType, attrXrefOptionalName, XREF, refEntityType);
    Entity refRefEntity = new DynamicEntity(refRefEntityType);
    refRefEntity.set(REF_REF_ATTR_ID_NAME, REF_REF_ENTITY_ID);
    refRefEntity.set(REF_REF_ATTR_VALUE_NAME, "value");
    Entity refEntity0 = new DynamicEntity(refEntityType);
    refEntity0.set(REF_ATTR_ID_NAME, REF_ENTITY0_ID);
    refEntity0.set(REF_ATTR_VALUE_NAME, REF_ENTITY0_LABEL);
    refEntity0.set(REF_ATTR_REF_NAME, refRefEntity);
    Entity refEntity1 = new DynamicEntity(refEntityType);
    refEntity1.set(REF_ATTR_ID_NAME, REF_ENTITY1_ID);
    refEntity1.set(REF_ATTR_VALUE_NAME, REF_ENTITY1_LABEL);
    refEntity1.set(REF_ATTR_REF_NAME, refRefEntity);
    Entity entity = new DynamicEntity(entityType);
    // required
    entity.set(attrIdName, ENTITY_ID);
    entity.set(attrBoolName, true);
    entity.set(attrCategoricalName, refEntity0);
    entity.set(attrCategoricalMrefName, asList(refEntity0, refEntity1));
    entity.set(attrCompoundAttr0Name, "compoundAttr0Str");
    entity.set(attrCompoundCategorical, refEntity0);
    entity.set(attrCompoundAttrCompoundAttr0Name, "compoundAttrCompoundAttr0Str");
    entity.set(attrDateName, LocalDate.parse("2015-05-22"));
    entity.set(attrDateTimeName, Instant.parse("2015-05-22T06:12:13Z"));
    entity.set(attrDecimalName, 3.14);
    entity.set(attrEmailName, "my@mail.com");
    entity.set(attrEnumName, enum0);
    entity.set(attrHtmlName, "<h1>html</h1>");
    entity.set(attrHyperlinkName, "http://www.molgenis.org/");
    entity.set(attrIntName, 123);
    entity.set(attrLongName, Long.MAX_VALUE);
    entity.set(attrMrefName, asList(refEntity0, refEntity1));
    entity.set(attrScriptName, "print \"Hello world\"");
    entity.set(attrStringName, "str");
    String textValue = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam consectetur auctor lectus sed tincidunt. Fusce sodales quis mauris non aliquam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Integer maximus imperdiet velit quis vehicula. Mauris pulvinar amet.";
    entity.set(attrTextName, textValue);
    entity.set(attrXrefName, refEntity0);
    // optional
    entity.set(attrBoolOptionalName, null);
    entity.set(attrCategoricalOptionalName, null);
    entity.set(attrCategoricalMrefOptionalName, null);
    entity.set(attrCompoundAttr0OptionalName, null);
    entity.set(attrCompoundAttrCompoundAttr0OptionalName, null);
    entity.set(attrDateOptionalName, null);
    entity.set(attrDateTimeOptionalName, null);
    entity.set(attrDecimalOptionalName, null);
    entity.set(attrEmailOptionalName, null);
    entity.set(attrEnumOptionalName, null);
    entity.set(attrHtmlOptionalName, null);
    entity.set(attrHyperlinkOptionalName, null);
    entity.set(attrIntOptionalName, null);
    entity.set(attrLongOptionalName, null);
    entity.set(attrMrefOptionalName, null);
    entity.set(attrScriptOptionalName, null);
    entity.set(attrStringOptionalName, null);
    entity.set(attrTextOptionalName, null);
    entity.set(attrXrefOptionalName, null);
    Query<Entity> q = new QueryImpl<>().offset(0).pageSize(100);
    when(dataService.findOneById(ENTITY_NAME, ENTITY_ID)).thenReturn(entity);
    when(dataService.findOneById(eq(ENTITY_NAME), eq(ENTITY_ID), any(Fetch.class))).thenReturn(entity);
    when(dataService.findOneById(eq(SELF_REF_ENTITY_NAME), eq("0"), any(Fetch.class))).thenReturn(selfRefEntity);
    when(dataService.count(ENTITY_NAME, new QueryImpl<>())).thenReturn(2L);
    when(dataService.findAll(ENTITY_NAME, q)).thenReturn(Stream.of(entity));
    when(dataService.findAll(REF_ENTITY_NAME)).thenAnswer(invocation -> Stream.of(refEntity0, refEntity1));
    when(dataService.findOneById(REF_ENTITY_NAME, REF_ENTITY0_ID)).thenReturn(refEntity0);
    when(dataService.findOneById(REF_ENTITY_NAME, REF_ENTITY1_ID)).thenReturn(refEntity1);
    when(dataService.findOneById(REF_REF_ENTITY_NAME, REF_REF_ENTITY_ID)).thenReturn(refRefEntity);
    when(dataService.getEntityType(ENTITY_NAME)).thenReturn(entityType);
    when(dataService.getEntityType(REF_ENTITY_NAME)).thenReturn(refEntityType);
    when(dataService.getEntityType(REF_REF_ENTITY_NAME)).thenReturn(refRefEntityType);
    when(dataService.getEntityType(SELF_REF_ENTITY_NAME)).thenReturn(selfRefEntityType);
    assertEquals(entity.getIdValue(), ENTITY_ID);
    assertEquals(refEntity0.getIdValue(), REF_ENTITY0_ID);
    assertEquals(refEntity0.getLabelValue(), REF_ENTITY0_LABEL);
    assertEquals(refEntity1.getIdValue(), REF_ENTITY1_ID);
    assertEquals(refEntity1.getLabelValue(), REF_ENTITY1_LABEL);
    assertEquals(refRefEntity.getIdValue(), REF_REF_ENTITY_ID);
    assertEquals(selfRefEntity.getIdValue(), "0");
    when(entityManager.create(entityType, POPULATE)).thenAnswer(invocation -> new DynamicEntity(entityType));
    when(localeResolver.resolveLocale(any())).thenReturn(Locale.ENGLISH);
    mockMvc = MockMvcBuilders.standaloneSetup(restControllerV2).setLocaleResolver(localeResolver).setMessageConverters(gsonHttpMessageConverter).setControllerAdvice(new GlobalControllerExceptionHandler(), new SpringExceptionHandler(), new FallbackExceptionHandler()).setConversionService(conversionService).build();
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) QueryImpl(org.molgenis.data.support.QueryImpl) DynamicEntity(org.molgenis.data.support.DynamicEntity) SpringExceptionHandler(org.molgenis.web.exception.SpringExceptionHandler) GlobalControllerExceptionHandler(org.molgenis.web.exception.GlobalControllerExceptionHandler) FallbackExceptionHandler(org.molgenis.web.exception.FallbackExceptionHandler)

Example 97 with QueryImpl

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

the class AlgorithmGeneratorServiceImplTest method testGenerateTemplateBasedAlgorithm.

@Test
public void testGenerateTemplateBasedAlgorithm() {
    EntityType targetEntityType = entityTypeFactory.create("target");
    Attribute targetBMIAttribute = attrMetaFactory.create().setName("targetHeight");
    targetBMIAttribute.setLabel("BMI kg/m²");
    targetBMIAttribute.setDataType(DECIMAL);
    targetEntityType.addAttribute(targetBMIAttribute);
    EntityType sourceEntityType = entityTypeFactory.create("source");
    Attribute heightSourceAttribute = attrMetaFactory.create().setName("sourceHeight");
    heightSourceAttribute.setDataType(DECIMAL);
    heightSourceAttribute.setLabel("body length in cm");
    Attribute weightSourceAttribute = attrMetaFactory.create().setName("sourceWeight");
    weightSourceAttribute.setDataType(DECIMAL);
    weightSourceAttribute.setLabel("weight in kg");
    sourceEntityType.addAttribute(heightSourceAttribute);
    sourceEntityType.addAttribute(weightSourceAttribute);
    Map<Attribute, ExplainedAttribute> sourceAttributes = ImmutableMap.of(heightSourceAttribute, ExplainedAttribute.create(heightSourceAttribute, singletonList(ExplainedQueryString.create("height", "height", "height", 100)), true), weightSourceAttribute, ExplainedAttribute.create(heightSourceAttribute, Collections.singletonList(ExplainedQueryString.create("weight", "weight", "weight", 100)), true));
    Script script = mock(Script.class);
    ScriptParameter heightParameter = mock(ScriptParameter.class);
    when(heightParameter.getName()).thenReturn("height");
    ScriptParameter weightParameter = mock(ScriptParameter.class);
    when(weightParameter.getName()).thenReturn("weight");
    when(script.getParameters()).thenReturn(asList(heightParameter, weightParameter));
    when(script.getContent()).thenReturn("$('weight').div($('height').pow(2)).value()");
    when(dataService.findAll(SCRIPT, new QueryImpl<Script>().eq(TYPE, JsMagmaScriptRunner.NAME), Script.class)).thenReturn(Stream.of(script));
    GeneratedAlgorithm generate = algorithmGeneratorService.generate(targetBMIAttribute, sourceAttributes, targetEntityType, sourceEntityType);
    assertEquals(generate.getAlgorithm(), "$('sourceWeight').div($('sourceHeight').div(100.0).pow(2)).value()");
    assertEquals(generate.getAlgorithmState(), AttributeMapping.AlgorithmState.GENERATED_HIGH);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Script(org.molgenis.script.core.Script) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) QueryImpl(org.molgenis.data.support.QueryImpl) Attribute(org.molgenis.data.meta.model.Attribute) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) ScriptParameter(org.molgenis.script.core.ScriptParameter) GeneratedAlgorithm(org.molgenis.semanticmapper.algorithmgenerator.bean.GeneratedAlgorithm) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 98 with QueryImpl

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

the class SemanticSearchServiceHelperTest method testGetAttributeIdentifiers.

@Test
public void testGetAttributeIdentifiers() {
    EntityType sourceEntityType = entityTypeFactory.create("sourceEntityType");
    Entity entityTypeEntity = mock(Entity.class);
    when(dataService.findOne(ENTITY_TYPE_META_DATA, new QueryImpl<>().eq(EntityTypeMetadata.ID, sourceEntityType.getId()))).thenReturn(entityTypeEntity);
    Attribute attributeEntity1 = attrMetaFactory.create();
    attributeEntity1.setIdentifier("1");
    attributeEntity1.setDataType(STRING);
    Attribute attributeEntity2 = attrMetaFactory.create();
    attributeEntity2.setIdentifier("2");
    attributeEntity2.setDataType(STRING);
    when(entityTypeEntity.getEntities(EntityTypeMetadata.ATTRIBUTES)).thenReturn(asList(attributeEntity1, attributeEntity2));
    List<String> expactedAttributeIdentifiers = asList("1", "2");
    assertEquals(semanticSearchServiceHelper.getAttributeIdentifiers(sourceEntityType), expactedAttributeIdentifiers);
}
Also used : Entity(org.molgenis.data.Entity) QueryImpl(org.molgenis.data.support.QueryImpl) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Aggregations

QueryImpl (org.molgenis.data.support.QueryImpl)98 Test (org.testng.annotations.Test)70 DynamicEntity (org.molgenis.data.support.DynamicEntity)37 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)36 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)36 EntityType (org.molgenis.data.meta.model.EntityType)28 Attribute (org.molgenis.data.meta.model.Attribute)25 Entity (org.molgenis.data.Entity)15 WithMockUser (org.springframework.security.test.context.support.WithMockUser)8 Stream (java.util.stream.Stream)7 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)7 AggregateQueryImpl (org.molgenis.data.support.AggregateQueryImpl)7 Objects.requireNonNull (java.util.Objects.requireNonNull)6 QueryRule (org.molgenis.data.QueryRule)6 AggregateQuery (org.molgenis.data.aggregation.AggregateQuery)6 EntityTypeIdentity (org.molgenis.data.security.EntityTypeIdentity)6 BeforeMethod (org.testng.annotations.BeforeMethod)6 Instant (java.time.Instant)5 LocalDate (java.time.LocalDate)5 Operator (org.molgenis.data.QueryRule.Operator)5