Search in sources :

Example 1 with EntitiesResponse

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

the class EntityControllerTest method testGetEntities.

@Test
void testGetEntities() {
    String entityTypeId = "MyEntityTypeId";
    Selection filter = Selection.FULL_SELECTION;
    Selection expand = Selection.FULL_SELECTION;
    Query query = Query.builder().setOperator(Operator.MATCHES).setValue("value").build();
    Sort sort = Sort.create("field", Direction.ASC);
    ReadEntitiesRequest entityRequest = new ReadEntitiesRequest();
    entityRequest.setEntityTypeId(entityTypeId);
    entityRequest.setQ(query);
    entityRequest.setSort(sort);
    entityRequest.setFilter(filter);
    entityRequest.setExpand(expand);
    entityRequest.setSize(10);
    entityRequest.setPage(2);
    Entities entities = Entities.create(emptyList(), 30);
    when(dataServiceV3.findAll(entityTypeId, query, filter, expand, sort, 10, 2)).thenReturn(entities);
    EntityCollection entityCollection = EntityCollection.builder().setEntityTypeId(entityTypeId).setEntities(emptyList()).setPage(Page.builder().setOffset(20).setPageSize(10).setTotal(30).build()).build();
    EntitiesResponse entitiesResponse = mock(EntitiesResponse.class);
    when(entityMapper.map(entityCollection, filter, expand, 10, 2, 30)).thenReturn(entitiesResponse);
    assertEquals(entitiesResponse, entityController.getEntities(entityRequest));
}
Also used : Query(org.molgenis.api.model.Query) Selection(org.molgenis.api.model.Selection) EntitiesResponse(org.molgenis.api.data.v3.model.EntitiesResponse) Sort(org.molgenis.api.model.Sort) ReadEntitiesRequest(org.molgenis.api.data.v3.model.ReadEntitiesRequest) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 2 with EntitiesResponse

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

the class EntityMapperImplTest method testMapEntityCollectionExpand.

@Test
void testMapEntityCollectionExpand() throws URISyntaxException {
    HttpServletRequest request = mock(HttpServletRequest.class);
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
    when(request.getRequestURI()).thenReturn("/api/data/EntityType");
    when(request.getScheme()).thenReturn("http");
    when(request.getServerName()).thenReturn("localhost");
    when(request.getServerPort()).thenReturn(80);
    Entity refEntity = createMockEntity(STRING, "RefEntityType", "refId0");
    Entity entity = createMockEntity(MREF);
    doReturn("refString").when(refEntity).getString("attr");
    doReturn(singletonList(refEntity)).when(entity).getEntities("attr");
    EntityCollection entityCollection = EntityCollection.builder().setEntityTypeId("EntityType").setEntities(singletonList(entity)).setPage(Page.builder().setOffset(0).setPageSize(1).setTotal(2).build()).build();
    URI refSelf = new URI("http://localhost/api/data/RefEntityType/refId0");
    EntityResponse expectedRefEntityResponse = EntityResponse.builder().setLinks(LinksResponse.create(null, refSelf, null)).setData(singletonMap("attr", "refString")).build();
    URI entitiesRefSelf = new URI("http://localhost/api/data/EntityType/id0/attr");
    EntitiesResponse expectedRefEntitiesResponse = EntitiesResponse.builder().setLinks(LinksResponse.create(null, entitiesRefSelf, null)).setItems(singletonList(expectedRefEntityResponse)).build();
    URI self = new URI("http://localhost/api/data/EntityType/id0");
    EntityResponse expectedEntityResponse = EntityResponse.builder().setLinks(LinksResponse.create(null, self, null)).setData(singletonMap("attr", expectedRefEntitiesResponse)).build();
    URI entitiesPrevious = new URI("http://localhost/api/data/EntityType?page=0");
    URI entitiesSelf = new URI("http://localhost/api/data/EntityType");
    URI entitiesNext = new URI("http://localhost/api/data/EntityType?page=2");
    EntitiesResponse expectedEntitiesResponse = EntitiesResponse.builder().setLinks(LinksResponse.create(entitiesPrevious, entitiesSelf, entitiesNext)).setItems(singletonList(expectedEntityResponse)).setPage(PageResponse.create(1, 2, 0)).build();
    assertEquals(expectedEntitiesResponse, entityMapper.map(entityCollection, FULL_SELECTION, FULL_SELECTION, 10, 1, 100));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Entity(org.molgenis.data.Entity) EntityResponse(org.molgenis.api.data.v3.model.EntityResponse) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) EntitiesResponse(org.molgenis.api.data.v3.model.EntitiesResponse) URI(java.net.URI) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with EntitiesResponse

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

the class EntityMapperImplTest method testMapEntityRefsType.

@ParameterizedTest
@MethodSource("refsTypeProvider")
void testMapEntityRefsType(AttributeType attributeType) throws URISyntaxException {
    Entity entity = createMockEntity(attributeType);
    doReturn(emptyList()).when(entity).getEntities("attr");
    URI refSelf = new URI("http://localhost/api/data/EntityType/id0/attr");
    EntitiesResponse expectedRefEntitiesResponse = EntitiesResponse.builder().setLinks(LinksResponse.create(null, refSelf, null)).build();
    URI self = new URI("http://localhost/api/data/EntityType/id0");
    EntityResponse expectedEntityResponse = EntityResponse.builder().setLinks(LinksResponse.create(null, self, null)).setData(singletonMap("attr", expectedRefEntitiesResponse)).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) EntitiesResponse(org.molgenis.api.data.v3.model.EntitiesResponse) URI(java.net.URI) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with EntitiesResponse

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

the class EntityMapperImpl method map.

@Override
public EntitiesResponse map(EntityCollection entityCollection, Selection filter, Selection expand, int size, int number, int total) {
    EntitiesResponse.Builder builder = mapRecursive(entityCollection, filter, expand, 0);
    LinksResponse linksResponse = LinksUtils.createLinksResponse(number, size, total);
    setPageResponse(entityCollection, builder);
    return builder.setLinks(linksResponse).build();
}
Also used : Builder(org.molgenis.api.data.v3.model.EntitiesResponse.Builder) LinksResponse(org.molgenis.api.model.response.LinksResponse) EntitiesResponse(org.molgenis.api.data.v3.model.EntitiesResponse)

Example 5 with EntitiesResponse

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

the class EntityControllerTest method testGetField.

@Test
void testGetField() {
    String entityTypeId = "MyEntityTypeId";
    String entityId = "EntityId";
    String fieldId = "Field";
    Selection filter = Selection.FULL_SELECTION;
    Selection expand = Selection.FULL_SELECTION;
    Query query = Query.builder().setOperator(Operator.MATCHES).setValue("value").build();
    Sort sort = Sort.create("field", Direction.ASC);
    ReadSubresourceRequest readSubResourceRequest = new ReadSubresourceRequest();
    readSubResourceRequest.setEntityTypeId(entityTypeId);
    readSubResourceRequest.setEntityId(entityId);
    readSubResourceRequest.setFieldId(fieldId);
    readSubResourceRequest.setQ(query);
    readSubResourceRequest.setSort(sort);
    readSubResourceRequest.setFilter(filter);
    readSubResourceRequest.setExpand(expand);
    readSubResourceRequest.setSize(10);
    readSubResourceRequest.setPage(2);
    Entities entities = Entities.create(emptyList(), 30);
    when(dataServiceV3.findSubresources(entityTypeId, entityId, fieldId, query, filter, expand, sort, 10, 2)).thenReturn(entities);
    EntityCollection entityCollection = EntityCollection.builder().setEntityTypeId(entityTypeId).setEntities(emptyList()).setPage(Page.builder().setOffset(20).setPageSize(10).setTotal(30).build()).build();
    EntitiesResponse entitiesResponse = mock(EntitiesResponse.class);
    when(entityMapper.map(entityCollection, filter, expand, 10, 2, 30)).thenReturn(entitiesResponse);
    assertEquals(entitiesResponse, entityController.getReferencedEntities(readSubResourceRequest));
}
Also used : Query(org.molgenis.api.model.Query) Selection(org.molgenis.api.model.Selection) EntitiesResponse(org.molgenis.api.data.v3.model.EntitiesResponse) Sort(org.molgenis.api.model.Sort) ReadSubresourceRequest(org.molgenis.api.data.v3.model.ReadSubresourceRequest) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Aggregations

EntitiesResponse (org.molgenis.api.data.v3.model.EntitiesResponse)7 Test (org.junit.jupiter.api.Test)5 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)5 URI (java.net.URI)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 EntityResponse (org.molgenis.api.data.v3.model.EntityResponse)4 Entity (org.molgenis.data.Entity)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)3 Query (org.molgenis.api.model.Query)2 Selection (org.molgenis.api.model.Selection)2 Sort (org.molgenis.api.model.Sort)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)1 Builder (org.molgenis.api.data.v3.model.EntitiesResponse.Builder)1 ReadEntitiesRequest (org.molgenis.api.data.v3.model.ReadEntitiesRequest)1 ReadSubresourceRequest (org.molgenis.api.data.v3.model.ReadSubresourceRequest)1 LinksResponse (org.molgenis.api.model.response.LinksResponse)1