Search in sources :

Example 1 with AttributeResponseData

use of org.molgenis.api.metadata.v3.model.AttributeResponseData in project molgenis by molgenis.

the class AttributeResponseMapperTest method mapAttribute.

@Test
void mapAttribute() throws URISyntaxException {
    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockHttpServletRequest));
    EntityType entityType = mock(EntityType.class);
    when(entityType.getId()).thenReturn("MyEntityTypeId");
    Attribute attribute = mock(Attribute.class);
    when(attribute.getIdentifier()).thenReturn("MyAttributeId");
    when(attribute.getName()).thenReturn("MyAttributeName");
    doReturn("My Attribute").when(attribute).getLabel();
    doReturn(null).when(attribute).getLabel(any(String.class));
    doReturn("My Attribute description").when(attribute).getDescription();
    doReturn(null).when(attribute).getDescription(any(String.class));
    when(attribute.getSequenceNumber()).thenReturn(2);
    when(attribute.getEntity()).thenReturn(entityType);
    when(attribute.getDataType()).thenReturn(AttributeType.ENUM);
    when(attribute.getMaxLength()).thenReturn(255);
    when(attribute.getEnumOptions()).thenReturn(asList("option0", "option1"));
    when(attribute.getLookupAttributeIndex()).thenReturn(null);
    when(attribute.getCascadeDelete()).thenReturn(null);
    AttributeResponseData attributeResponseData = AttributeResponseData.builder().setId("MyAttributeId").setName("MyAttributeName").setSequenceNr(2).setType("enum").setMaxLength(255).setEnumOptions(ImmutableList.of("option0", "option1")).setIdAttribute(false).setLabelAttribute(false).setNullable(false).setAuto(false).setVisible(false).setUnique(false).setReadOnly(false).setAggregatable(false).setLabelI18n(I18nValue.builder().setDefaultValue("My Attribute").setTranslations(ImmutableMap.of()).build()).setDescriptionI18n(I18nValue.builder().setDefaultValue("My Attribute description").setTranslations(ImmutableMap.of()).build()).build();
    AttributeResponse attributeResponse = AttributeResponse.builder().setLink(LinksResponse.create(null, new URI("http://localhost/api/metadata/MyEntityTypeId/attributes/MyAttributeId"), null)).setData(attributeResponseData).build();
    assertEquals(attributeResponse, attributeResponseMapper.toAttributeResponse(attribute, true));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Attribute(org.molgenis.data.meta.model.Attribute) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) AttributeResponseData(org.molgenis.api.metadata.v3.model.AttributeResponseData) AttributeResponse(org.molgenis.api.metadata.v3.model.AttributeResponse) URI(java.net.URI) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 2 with AttributeResponseData

use of org.molgenis.api.metadata.v3.model.AttributeResponseData in project molgenis by molgenis.

the class AttributeResponseMapperTest method mapAttributes.

@Test
void mapAttributes() throws URISyntaxException {
    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
    mockHttpServletRequest.setRequestURI("/api/metadata/MyEntityType/attributes");
    mockHttpServletRequest.setQueryString("page=1");
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockHttpServletRequest));
    EntityType entityType = mock(EntityType.class);
    when(entityType.getId()).thenReturn("MyEntityTypeId");
    Attribute attribute = mock(Attribute.class);
    when(attribute.getIdentifier()).thenReturn("MyAttributeId");
    when(attribute.getName()).thenReturn("MyAttributeName");
    when(attribute.getSequenceNumber()).thenReturn(2);
    when(attribute.getEntity()).thenReturn(entityType);
    when(attribute.getDataType()).thenReturn(AttributeType.STRING);
    when(attribute.getMaxLength()).thenReturn(255);
    when(attribute.getLookupAttributeIndex()).thenReturn(null);
    when(attribute.getCascadeDelete()).thenReturn(null);
    AttributeResponseData attributeResponseData = AttributeResponseData.builder().setId("MyAttributeId").setName("MyAttributeName").setSequenceNr(2).setType("string").setMaxLength(255).setIdAttribute(false).setLabelAttribute(false).setNullable(false).setAuto(false).setVisible(false).setUnique(false).setReadOnly(false).setAggregatable(false).build();
    AttributeResponse attributeResponse = AttributeResponse.builder().setLink(LinksResponse.create(null, new URI("http://localhost/api/metadata/MyEntityTypeId/attributes/MyAttributeId"), null)).setData(attributeResponseData).build();
    AttributesResponse attributesResponse = AttributesResponse.builder().setLinks(LinksResponse.create(new URI("http://localhost/api/metadata/MyEntityType/attributes?page=0"), new URI("http://localhost/api/metadata/MyEntityType/attributes?page=1"), new URI("http://localhost/api/metadata/MyEntityType/attributes?page=2"))).setItems(ImmutableList.of(attributeResponse)).setPage(PageResponse.create(1, 5, 1)).build();
    assertEquals(attributesResponse, attributeResponseMapper.toAttributesResponse(Attributes.create(ImmutableList.of(attribute), 5), 1, 1));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) AttributesResponse(org.molgenis.api.metadata.v3.model.AttributesResponse) Attribute(org.molgenis.data.meta.model.Attribute) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) AttributeResponseData(org.molgenis.api.metadata.v3.model.AttributeResponseData) AttributeResponse(org.molgenis.api.metadata.v3.model.AttributeResponse) URI(java.net.URI) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 3 with AttributeResponseData

use of org.molgenis.api.metadata.v3.model.AttributeResponseData in project molgenis by molgenis.

the class AttributeResponseMapperImpl method mapInternal.

private AttributeResponseData mapInternal(Attribute attr, boolean i18n) {
    Builder builder = AttributeResponseData.builder();
    builder.setId(attr.getIdentifier());
    builder.setName(attr.getName());
    builder.setSequenceNr(attr.getSequenceNumber());
    builder.setType(getValueString(attr.getDataType()));
    Optional.ofNullable(attr.getMaxLength()).ifPresent(builder::setMaxLength);
    builder.setIdAttribute(attr.isIdAttribute());
    builder.setLabelAttribute(attr.isLabelAttribute());
    builder.setLookupAttributeIndex(attr.getLookupAttributeIndex());
    if (EntityTypeUtils.isReferenceType(attr)) {
        builder.setRefEntityType(LinksResponse.create(null, createEntityTypeResponseUri(attr.getRefEntity()), null));
    }
    builder.setCascadeDelete(attr.getCascadeDelete());
    builder.setMappedBy(attr.getMappedBy() != null ? toAttributeResponse(attr.getMappedBy(), i18n) : null);
    if (attr.getDataType() == ONE_TO_MANY && attr.isMappedBy()) {
        builder.setOrderBy(map(attr));
    }
    builder.setLabel(attr.getLabel(LocaleContextHolder.getLocale().getLanguage()));
    builder.setDescription(attr.getDescription(LocaleContextHolder.getLocale().getLanguage()));
    if (i18n) {
        builder.setLabelI18n(getI18nAttrLabel(attr));
        getI18nAttrDesc(attr).ifPresent(builder::setDescriptionI18n);
    }
    builder.setNullable(attr.isNillable());
    builder.setAuto(attr.isAuto());
    builder.setVisible(attr.isVisible());
    builder.setUnique(attr.isUnique());
    builder.setReadOnly(attr.isReadOnly());
    builder.setAggregatable(attr.isAggregatable());
    builder.setExpression(attr.getExpression());
    if (attr.getDataType() == AttributeType.ENUM) {
        builder.setEnumOptions(attr.getEnumOptions());
    }
    if (attr.getDataType() == AttributeType.CATEGORICAL || attr.getDataType() == AttributeType.CATEGORICAL_MREF) {
        builder.setCategoricalOptions(getCategories(attr.getRefEntity()));
    }
    org.molgenis.data.Range range = attr.getRange();
    if (range != null) {
        builder.setRange(Range.create(range.getMin(), range.getMax()));
    }
    Attribute parent = attr.getParent();
    builder.setParentAttributeId(parent != null ? parent.getIdentifier() : null);
    builder.setNullableExpression(attr.getNullableExpression());
    builder.setVisibleExpression(attr.getVisibleExpression());
    builder.setValidationExpression(attr.getValidationExpression());
    builder.setDefaultValue(attr.getDefaultValue());
    return builder.build();
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) Builder(org.molgenis.api.metadata.v3.model.AttributeResponseData.Builder)

Aggregations

Attribute (org.molgenis.data.meta.model.Attribute)3 URI (java.net.URI)2 Test (org.junit.jupiter.api.Test)2 AttributeResponse (org.molgenis.api.metadata.v3.model.AttributeResponse)2 AttributeResponseData (org.molgenis.api.metadata.v3.model.AttributeResponseData)2 EntityType (org.molgenis.data.meta.model.EntityType)2 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)2 Builder (org.molgenis.api.metadata.v3.model.AttributeResponseData.Builder)1 AttributesResponse (org.molgenis.api.metadata.v3.model.AttributesResponse)1 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)1