Search in sources :

Example 6 with EntityResponse

use of org.molgenis.api.data.v3.model.EntityResponse in project molgenis by molgenis.

the class EntityMapperImplTest method testMapEntityLong.

@Test
void testMapEntityLong() throws URISyntaxException {
    Entity entity = createMockEntity(LONG);
    doReturn(Long.MAX_VALUE).when(entity).getLong("attr");
    URI self = new URI("http://localhost/api/data/EntityType/id0");
    EntityResponse expectedEntityResponse = EntityResponse.builder().setLinks(LinksResponse.create(null, self, null)).setData(singletonMap("attr", Long.MAX_VALUE)).build();
    assertEquals(expectedEntityResponse, entityMapper.map(entity, FULL_SELECTION, EMPTY_SELECTION));
}
Also used : Entity(org.molgenis.data.Entity) EntityResponse(org.molgenis.api.data.v3.model.EntityResponse) URI(java.net.URI) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with EntityResponse

use of org.molgenis.api.data.v3.model.EntityResponse in project molgenis by molgenis.

the class EntityMapperImplTest method testMapEntityStringType.

@ParameterizedTest
@MethodSource("stringTypeProvider")
void testMapEntityStringType(AttributeType attributeType) throws URISyntaxException {
    Entity entity = createMockEntity(attributeType);
    doReturn("string").when(entity).getString("attr");
    URI self = new URI("http://localhost/api/data/EntityType/id0");
    EntityResponse expectedEntityResponse = EntityResponse.builder().setLinks(LinksResponse.create(null, self, null)).setData(singletonMap("attr", "string")).build();
    assertEquals(expectedEntityResponse, entityMapper.map(entity, FULL_SELECTION, EMPTY_SELECTION));
}
Also used : Entity(org.molgenis.data.Entity) EntityResponse(org.molgenis.api.data.v3.model.EntityResponse) URI(java.net.URI) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 8 with EntityResponse

use of org.molgenis.api.data.v3.model.EntityResponse in project molgenis by molgenis.

the class EntityMapperImplTest method testMapEntityDecimal.

@Test
void testMapEntityDecimal() throws URISyntaxException {
    Entity entity = createMockEntity(DECIMAL);
    doReturn(3.14).when(entity).getDouble("attr");
    URI self = new URI("http://localhost/api/data/EntityType/id0");
    EntityResponse expectedEntityResponse = EntityResponse.builder().setLinks(LinksResponse.create(null, self, null)).setData(singletonMap("attr", 3.14)).build();
    assertEquals(expectedEntityResponse, entityMapper.map(entity, FULL_SELECTION, EMPTY_SELECTION));
}
Also used : Entity(org.molgenis.data.Entity) EntityResponse(org.molgenis.api.data.v3.model.EntityResponse) URI(java.net.URI) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with EntityResponse

use of org.molgenis.api.data.v3.model.EntityResponse in project molgenis by molgenis.

the class EntityMapperImpl method mapRecursive.

private EntityResponse mapRecursive(Entity entity, Selection filter, Selection expand, int depth) {
    if (depth > MAX_DEPTH) {
        throw new IllegalArgumentException("max_depth exceeded: " + depth);
    }
    EntityResponse.Builder builder = EntityResponse.builder();
    if (filter.hasItems()) {
        Map<String, Object> dataMap = new LinkedHashMap<>();
        stream(entity.getEntityType().getAtomicAttributes()).filter(attribute -> filter.hasItem(attribute.getName())).forEach(attribute -> dataMap.put(attribute.getName(), mapRecursive(entity, attribute, filter, expand, depth)));
        builder.setData(dataMap);
    }
    URI uri = createEntityResponseUri(entity);
    return builder.setLinks(LinksResponse.create(null, uri, null)).build();
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) AttributeType(org.molgenis.data.meta.AttributeType) Builder(org.molgenis.api.data.v3.model.EntitiesResponse.Builder) MolgenisServletUriComponentsBuilder(org.molgenis.api.support.MolgenisServletUriComponentsBuilder) IllegalAttributeTypeException(org.molgenis.data.meta.IllegalAttributeTypeException) Selection(org.molgenis.api.model.Selection) LinksUtils(org.molgenis.api.support.LinksUtils) LinksResponse(org.molgenis.api.model.response.LinksResponse) Attribute(org.molgenis.data.meta.model.Attribute) EntitiesResponse(org.molgenis.api.data.v3.model.EntitiesResponse) Streams.stream(com.google.common.collect.Streams.stream) LinkedHashMap(java.util.LinkedHashMap) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Component(org.springframework.stereotype.Component) EntityResponse(org.molgenis.api.data.v3.model.EntityResponse) Map(java.util.Map) PageUtils.getPageResponse(org.molgenis.api.support.PageUtils.getPageResponse) URI(java.net.URI) CheckForNull(javax.annotation.CheckForNull) Nullable(javax.annotation.Nullable) Page(org.molgenis.api.data.v3.EntityCollection.Page) Entity(org.molgenis.data.Entity) EntityResponse(org.molgenis.api.data.v3.model.EntityResponse) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap)

Example 10 with EntityResponse

use of org.molgenis.api.data.v3.model.EntityResponse in project molgenis by molgenis.

the class EntityControllerTest method testGetEntity.

@Test
void testGetEntity() {
    String entityTypeId = "MyEntityTypeId";
    String entityId = "MyId";
    Selection filter = Selection.FULL_SELECTION;
    Selection expand = Selection.FULL_SELECTION;
    ReadEntityRequest entityRequest = new ReadEntityRequest();
    entityRequest.setEntityTypeId(entityTypeId);
    entityRequest.setEntityId(entityId);
    entityRequest.setFilter(filter);
    entityRequest.setExpand(expand);
    Entity entity = mock(Entity.class);
    when(dataServiceV3.find(entityTypeId, entityId, filter, expand)).thenReturn(entity);
    EntityResponse entityResponse = mock(EntityResponse.class);
    when(entityMapper.map(entity, filter, expand)).thenReturn(entityResponse);
    assertEquals(entityResponse, entityController.getEntity(entityRequest));
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Entity(org.molgenis.data.Entity) EntityResponse(org.molgenis.api.data.v3.model.EntityResponse) Selection(org.molgenis.api.model.Selection) ReadEntityRequest(org.molgenis.api.data.v3.model.ReadEntityRequest) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Aggregations

EntityResponse (org.molgenis.api.data.v3.model.EntityResponse)17 Entity (org.molgenis.data.Entity)17 URI (java.net.URI)16 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 Test (org.junit.jupiter.api.Test)12 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)12 EntitiesResponse (org.molgenis.api.data.v3.model.EntitiesResponse)6 Selection (org.molgenis.api.model.Selection)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 Attribute (org.molgenis.data.meta.model.Attribute)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)3 Streams.stream (com.google.common.collect.Streams.stream)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 Collectors.toList (java.util.stream.Collectors.toList)2 CheckForNull (javax.annotation.CheckForNull)2 Nullable (javax.annotation.Nullable)2