Search in sources :

Example 1 with AttributeResponse

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

the class MetadataApiControllerTest method testGetAttribute.

@Test
void testGetAttribute() {
    String attributeId = "MyAttributeId";
    String entityTypeId = "entityTypeId";
    Attribute attribute = mock(Attribute.class);
    when(metadataApiService.findAttribute(entityTypeId, attributeId)).thenReturn(attribute);
    AttributeResponse attributeResponse = mock(AttributeResponse.class);
    when(attributeResponseMapper.toAttributeResponse(attribute, false)).thenReturn(attributeResponse);
    assertEquals(attributeResponse, metadataApiController.getAttribute(entityTypeId, attributeId));
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute) AttributeResponse(org.molgenis.api.metadata.v3.model.AttributeResponse) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 2 with AttributeResponse

use of org.molgenis.api.metadata.v3.model.AttributeResponse 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 3 with AttributeResponse

use of org.molgenis.api.metadata.v3.model.AttributeResponse 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)

Aggregations

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