Search in sources :

Example 26 with Selection

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

Example 27 with Selection

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

Example 28 with Selection

use of org.molgenis.api.model.Selection in project molgenis by molgenis.

the class EntityMapperImplTest method testMapEntityFilter.

@Test
void testMapEntityFilter() throws URISyntaxException {
    EntityType entityType = when(mock(EntityType.class).getId()).thenReturn("EntityType").getMock();
    Attribute attribute0 = when(mock(Attribute.class).getName()).thenReturn("attr0").getMock();
    doReturn(STRING).when(attribute0).getDataType();
    Attribute attribute1 = when(mock(Attribute.class).getName()).thenReturn("attr1").getMock();
    doReturn(STRING).when(attribute1).getDataType();
    doReturn(asList(attribute0, attribute1)).when(entityType).getAtomicAttributes();
    Entity entity = mock(Entity.class);
    doReturn("id0").when(entity).getIdValue();
    doReturn(entityType).when(entity).getEntityType();
    doReturn("string0").when(entity).getString("attr0");
    doReturn("string1").when(entity).getString(("attr1"));
    URI self = new URI("http://localhost/api/data/EntityType/id0");
    EntityResponse expectedEntityResponse = EntityResponse.builder().setLinks(LinksResponse.create(null, self, null)).setData(singletonMap("attr1", "string1")).build();
    assertEquals(expectedEntityResponse, entityMapper.map(entity, new Selection(singletonMap("attr1", null)), EMPTY_SELECTION));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) EntityResponse(org.molgenis.api.data.v3.model.EntityResponse) Selection(org.molgenis.api.model.Selection) URI(java.net.URI) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 29 with Selection

use of org.molgenis.api.model.Selection in project molgenis by molgenis.

the class FetchMapperTest method testToFetchEmpty.

@Test
void testToFetchEmpty() {
    EntityType entityType = mock(EntityType.class);
    Attribute attribute = mock(Attribute.class);
    when(attribute.getName()).thenReturn("test");
    when(entityType.getAtomicAttributes()).thenReturn(singletonList(attribute));
    Selection expand = Selection.EMPTY_SELECTION;
    Selection filter = Selection.FULL_SELECTION;
    Fetch expected = new Fetch().field("test");
    assertEquals(expected, fetchMapper.toFetch(entityType, filter, expand));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Fetch(org.molgenis.data.Fetch) Attribute(org.molgenis.data.meta.model.Attribute) Selection(org.molgenis.api.model.Selection) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 30 with Selection

use of org.molgenis.api.model.Selection in project molgenis by molgenis.

the class EntityMapperImpl method mapReferences.

private EntitiesResponse mapReferences(Entity entity, Attribute attribute, Selection filter, Selection expand, int depth) {
    URI uri = createEntityResponseUri(entity, attribute.getName());
    if (expand.hasItem(attribute.getName())) {
        String refEntityTypeId = attribute.getRefEntity().getId();
        List<Entity> refEntities = stream(entity.getEntities(attribute.getName())).collect(toList());
        EntityCollection entityCollection = EntityCollection.builder().setEntityTypeId(refEntityTypeId).setEntities(refEntities).setEntityId(entity.getIdValue().toString()).build();
        Selection refFilter = getReferenceFilter(attribute, filter, expand);
        Selection refExpand = getReferenceExpand(attribute, expand);
        return mapRecursive(entityCollection, refFilter, refExpand, depth).setLinks(LinksResponse.create(null, uri, null)).build();
    } else {
        return EntitiesResponse.builder().setLinks(LinksResponse.create(null, uri, null)).build();
    }
}
Also used : Entity(org.molgenis.data.Entity) Selection(org.molgenis.api.model.Selection) URI(java.net.URI)

Aggregations

Selection (org.molgenis.api.model.Selection)32 Test (org.junit.jupiter.api.Test)24 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)19 Entity (org.molgenis.data.Entity)18 Attribute (org.molgenis.data.meta.model.Attribute)15 EntityType (org.molgenis.data.meta.model.EntityType)13 Fetch (org.molgenis.data.Fetch)12 Sort (org.molgenis.api.model.Sort)11 Query (org.molgenis.api.model.Query)9 QueryImpl (org.molgenis.data.support.QueryImpl)7 URI (java.net.URI)5 EntityResponse (org.molgenis.api.data.v3.model.EntityResponse)5 CheckForNull (javax.annotation.CheckForNull)4 Nullable (javax.annotation.Nullable)4 EntitiesResponse (org.molgenis.api.data.v3.model.EntitiesResponse)4 HashMap (java.util.HashMap)3 Transactional (org.springframework.transaction.annotation.Transactional)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 Streams.stream (com.google.common.collect.Streams.stream)2 LinkedHashMap (java.util.LinkedHashMap)2