Search in sources :

Example 1 with EntityTypeResponse

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

the class EntityTypeResponseMapperTest method toEntityTypesResponse.

@Test
void toEntityTypesResponse() throws URISyntaxException {
    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
    mockHttpServletRequest.setMethod("GET");
    mockHttpServletRequest.setRequestURI("/api/metadata");
    mockHttpServletRequest.setQueryString("page=1");
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockHttpServletRequest));
    EntityType entityType = mock(EntityType.class);
    when(entityType.getId()).thenReturn("MyEntityTypeId");
    int total = 5;
    EntityTypes entityTypes = EntityTypes.builder().setEntityTypes(singletonList(entityType)).setTotal(total).build();
    int size = 1;
    int number = 1;
    EntityTypeResponseData entityTypeResponseData = EntityTypeResponseData.builder().setId("MyEntityTypeId").setAttributes(AttributesResponse.builder().setLinks(LinksResponse.create(null, new URI("http://localhost/api/metadata/MyEntityTypeId/attributes"), null)).build()).setAbstract(false).setIndexingDepth(0).build();
    EntityTypeResponse entityTypeResponse = EntityTypeResponse.builder().setLinks(LinksResponse.create(null, new URI("http://localhost/api/metadata/MyEntityTypeId"), null)).setData(entityTypeResponseData).build();
    EntityTypesResponse entityTypesResponse = EntityTypesResponse.builder().setLinks(LinksResponse.create(new URI("http://localhost/api/metadata?page=0"), new URI("http://localhost/api/metadata?page=1"), new URI("http://localhost/api/metadata?page=2"))).setItems(singletonList(entityTypeResponse)).setPage(PageResponse.create(size, total, number)).build();
    assertEquals(entityTypesResponse, entityTypeV3Mapper.toEntityTypesResponse(entityTypes, size, number));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) EntityTypeResponseData(org.molgenis.api.metadata.v3.model.EntityTypeResponseData) EntityTypesResponse(org.molgenis.api.metadata.v3.model.EntityTypesResponse) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) EntityTypeResponse(org.molgenis.api.metadata.v3.model.EntityTypeResponse) URI(java.net.URI) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 2 with EntityTypeResponse

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

the class EntityTypeResponseMapperTest method toEntityTypeResponse.

@Test
void toEntityTypeResponse() throws URISyntaxException {
    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
    mockHttpServletRequest.setMethod("GET");
    mockHttpServletRequest.setRequestURI("/api/metadata/MyEntityTypeId");
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockHttpServletRequest));
    EntityType entityType = mock(EntityType.class);
    when(entityType.getId()).thenReturn("MyEntityTypeId");
    doReturn("My Entity Type").when(entityType).getLabel();
    doReturn("My Entity Type").when(entityType).getLabel(any(String.class));
    doReturn("My Entity Type description").when(entityType).getDescription();
    doReturn("My Entity Type description").when(entityType).getDescription(any(String.class));
    when(entityType.getDescription()).thenReturn("My Entity Type description");
    when(entityType.getString("labelEn")).thenReturn("My Entity Type (en)");
    EntityTypeResponseData entityTypeResponseData = EntityTypeResponseData.builder().setId("MyEntityTypeId").setLabel("My Entity Type").setLabelI18n(I18nValue.builder().setDefaultValue("My Entity Type").setTranslations(ImmutableMap.of("en", "My Entity Type (en)")).build()).setDescription("My Entity Type description").setDescriptionI18n(I18nValue.builder().setDefaultValue("My Entity Type description").setTranslations(ImmutableMap.of()).build()).setAttributes(AttributesResponse.builder().setLinks(LinksResponse.create(null, new URI("http://localhost/api/metadata/MyEntityTypeId/attributes"), null)).setItems(ImmutableList.of()).build()).setAbstract(false).setIndexingDepth(0).build();
    EntityTypeResponse entityTypeResponse = EntityTypeResponse.builder().setLinks(LinksResponse.create(null, new URI("http://localhost/api/metadata/MyEntityTypeId"), null)).setData(entityTypeResponseData).build();
    assertEquals(entityTypeResponse, entityTypeV3Mapper.toEntityTypeResponse(entityType, true, true));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) EntityTypeResponseData(org.molgenis.api.metadata.v3.model.EntityTypeResponseData) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) EntityTypeResponse(org.molgenis.api.metadata.v3.model.EntityTypeResponse) URI(java.net.URI) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 3 with EntityTypeResponse

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

the class MetadataApiControllerTest method testGetEntityType.

@Test
void testGetEntityType() {
    String entityTypeId = "MyEntityTypeId";
    boolean flattenAttrs = true;
    boolean i18n = false;
    ReadEntityTypeRequest readEntityTypeRequest = new ReadEntityTypeRequest();
    readEntityTypeRequest.setFlattenAttributes(flattenAttrs);
    readEntityTypeRequest.setI18n(i18n);
    EntityType entityType = mock(EntityType.class);
    when(metadataApiService.findEntityType(entityTypeId)).thenReturn(entityType);
    EntityTypeResponse entityTypeResponse = mock(EntityTypeResponse.class);
    when(entityTypeResponseMapper.toEntityTypeResponse(entityType, flattenAttrs, i18n)).thenReturn(entityTypeResponse);
    assertEquals(entityTypeResponse, metadataApiController.getEntityType(entityTypeId, readEntityTypeRequest));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) EntityTypeResponse(org.molgenis.api.metadata.v3.model.EntityTypeResponse) ReadEntityTypeRequest(org.molgenis.api.metadata.v3.model.ReadEntityTypeRequest) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 4 with EntityTypeResponse

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

the class EntityTypeResponseMapperImpl method mapInternal.

private EntityTypeResponse mapInternal(EntityType entityType, boolean flattenAttrs, boolean includeData, boolean expandAttrs, boolean i18n) {
    EntityTypeResponse.Builder entityTypeResponseBuilder = EntityTypeResponse.builder();
    entityTypeResponseBuilder.setLinks(LinksResponse.create(null, createEntityTypeResponseUri(entityType), null));
    if (includeData) {
        EntityTypeResponseData.Builder builder = EntityTypeResponseData.builder();
        builder.setId(entityType.getId());
        Package pack = entityType.getPackage();
        if (pack != null) {
            builder.setPackage(PackageResponse.builder().setLinks(LinksResponse.create(null, createPackageResponseUri(pack), null)).build());
        }
        builder.setLabel(entityType.getLabel(LocaleContextHolder.getLocale().getLanguage()));
        builder.setDescription(entityType.getDescription(LocaleContextHolder.getLocale().getLanguage()));
        if (i18n) {
            builder.setLabelI18n(getI18nEntityTypeLabel(entityType));
            getI18nEntityTypeDesc(entityType).ifPresent(builder::setDescriptionI18n);
        }
        AttributesResponse.Builder attributesResponseBuilder = AttributesResponse.builder().setLinks(LinksResponse.create(null, createAttributesResponseUri(entityType), null));
        if (expandAttrs) {
            attributesResponseBuilder.setItems(flattenAttrs ? attributeResponseMapper.mapInternal(entityType.getAllAttributes(), i18n) : attributeResponseMapper.mapInternal(entityType.getOwnAllAttributes(), i18n));
        }
        builder.setAttributes(attributesResponseBuilder.build());
        builder.setAbstract(entityType.isAbstract());
        EntityType parent = entityType.getExtends();
        builder.setExtends(parent != null ? mapInternal(parent, false, false, false, i18n) : null);
        builder.setIndexingDepth(entityType.getIndexingDepth());
        entityTypeResponseBuilder.setData(builder.build());
    }
    return entityTypeResponseBuilder.build();
}
Also used : EntityTypeResponseData(org.molgenis.api.metadata.v3.model.EntityTypeResponseData) EntityType(org.molgenis.data.meta.model.EntityType) AttributesResponse(org.molgenis.api.metadata.v3.model.AttributesResponse) EntityTypeResponse(org.molgenis.api.metadata.v3.model.EntityTypeResponse) Package(org.molgenis.data.meta.model.Package)

Example 5 with EntityTypeResponse

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

the class EntityTypeResponseMapperImpl method toEntityTypesResponse.

@Override
public EntityTypesResponse toEntityTypesResponse(EntityTypes entityTypes, int pageSize, int pageNumber) {
    List<EntityTypeResponse> results = new ArrayList<>();
    for (EntityType entityType : entityTypes.getEntityTypes()) {
        results.add(mapInternal(entityType, false, true, false, false));
    }
    int total = entityTypes.getTotal();
    return EntityTypesResponse.create(LinksUtils.createLinksResponse(pageNumber, pageSize, total), results, PageUtils.getPageResponse(pageSize, pageNumber * pageSize, total));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) EntityTypeResponse(org.molgenis.api.metadata.v3.model.EntityTypeResponse) ArrayList(java.util.ArrayList)

Aggregations

EntityTypeResponse (org.molgenis.api.metadata.v3.model.EntityTypeResponse)5 EntityType (org.molgenis.data.meta.model.EntityType)5 Test (org.junit.jupiter.api.Test)3 EntityTypeResponseData (org.molgenis.api.metadata.v3.model.EntityTypeResponseData)3 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)3 URI (java.net.URI)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)2 ArrayList (java.util.ArrayList)1 AttributesResponse (org.molgenis.api.metadata.v3.model.AttributesResponse)1 EntityTypesResponse (org.molgenis.api.metadata.v3.model.EntityTypesResponse)1 ReadEntityTypeRequest (org.molgenis.api.metadata.v3.model.ReadEntityTypeRequest)1 Package (org.molgenis.data.meta.model.Package)1