Search in sources :

Example 16 with Sort

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

the class DataServiceV3ImplTest method testFindField.

@SuppressWarnings("unchecked")
@Test
void testFindField() {
    String entityTypeId = "MyEntityType";
    String refEntityTypeId = "refEntityType";
    String entityId = "MyEntity";
    String fieldId = "MyField";
    Selection filter = Selection.FULL_SELECTION;
    Selection expand = Selection.EMPTY_SELECTION;
    Attribute refIdAttribute = mock(Attribute.class, "mrefId");
    when(refIdAttribute.getName()).thenReturn("id");
    EntityType refEntityType = mock(EntityType.class, "refEntityType");
    when(refEntityType.getId()).thenReturn(refEntityTypeId);
    when(refEntityType.getIdAttribute()).thenReturn(refIdAttribute);
    Attribute idAttribute = mock(Attribute.class, "id");
    when(idAttribute.getDataType()).thenReturn(STRING);
    Attribute mrefAttribute = mock(Attribute.class, "mref");
    when(mrefAttribute.getName()).thenReturn("MyField");
    when(mrefAttribute.getDataType()).thenReturn(MREF);
    when(mrefAttribute.getRefEntity()).thenReturn(refEntityType);
    EntityType entityType = mock(EntityType.class, "entityType");
    when(entityType.getIdAttribute()).thenReturn(idAttribute);
    when(entityType.getAttribute(fieldId)).thenReturn(mrefAttribute);
    Repository<Entity> repository = mock(Repository.class);
    when(repository.getEntityType()).thenReturn(entityType);
    Fetch fetch = new Fetch().field("MyField", new Fetch().field("id"));
    Entity entity = mock(Entity.class);
    doReturn(entity).when(repository).findOneById(entityId, fetch);
    Repository<Entity> refRepository = mock(Repository.class);
    when(refRepository.getEntityType()).thenReturn(refEntityType);
    Entity entity1 = mock(Entity.class);
    doReturn("entity1").when(entity1).getIdValue();
    Entity entity2 = mock(Entity.class);
    doReturn("entity2").when(entity2).getIdValue();
    Entity entity3 = mock(Entity.class);
    doReturn("entity3").when(entity3).getIdValue();
    doReturn(Arrays.asList(entity1, entity2, entity3)).when(entity).getEntities("MyField");
    Sort sort = Sort.create("field", Direction.ASC);
    Query q = Query.builder().setOperator(Operator.MATCHES).setValue("value").build();
    org.molgenis.data.Query<Entity> findAllQuery = mock(org.molgenis.data.Query.class);
    org.molgenis.data.Sort dataSort = mock(org.molgenis.data.Sort.class);
    org.molgenis.data.Query<Entity> findQuery = new QueryImpl<>(findAllQuery);
    findQuery.in("id", Arrays.asList("entity1", "entity2", "entity3"));
    findQuery.fetch(fetch);
    findQuery.offset(10);
    findQuery.pageSize(10);
    findQuery.sort(dataSort);
    org.molgenis.data.Query<Entity> countQuery = new QueryImpl<>(findAllQuery);
    countQuery.offset(0);
    countQuery.pageSize(Integer.MAX_VALUE);
    when(queryMapper.map(q, refRepository)).thenReturn(findAllQuery).thenReturn(countQuery);
    countQuery.in("id", Arrays.asList("entity1", "entity2", "entity3"));
    when(refRepository.count(countQuery)).thenReturn(100L);
    when(refRepository.findAll(findQuery)).thenReturn(Stream.of(entity1, entity2));
    when(sortMapper.map(sort, refEntityType)).thenReturn(dataSort);
    doReturn(Optional.of(repository)).when(metaDataService).getRepository(entityTypeId);
    doReturn(Optional.of(refRepository)).when(metaDataService).getRepository(refEntityTypeId);
    Entities actual = dataServiceV3Impl.findSubresources(entityTypeId, entityId, fieldId, q, filter, expand, sort, 10, 1);
    assertEquals(actual, Entities.builder().setEntities(asList(entity1, entity2)).setTotal(100).build());
}
Also used : Entity(org.molgenis.data.Entity) Query(org.molgenis.api.model.Query) Attribute(org.molgenis.data.meta.model.Attribute) Selection(org.molgenis.api.model.Selection) EntityType(org.molgenis.data.meta.model.EntityType) Fetch(org.molgenis.data.Fetch) QueryImpl(org.molgenis.data.support.QueryImpl) Sort(org.molgenis.api.model.Sort) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 17 with Sort

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

the class DataServiceV3ImplTest method testFindFieldWithQuery.

@SuppressWarnings("unchecked")
@Test
void testFindFieldWithQuery() {
    String entityTypeId = "MyEntityType";
    String refEntityTypeId = "refEntityType";
    String entityId = "MyEntity";
    String fieldId = "MyField";
    Selection filter = Selection.FULL_SELECTION;
    Selection expand = Selection.EMPTY_SELECTION;
    Attribute refIdAttribute = mock(Attribute.class, "mrefId");
    when(refIdAttribute.getName()).thenReturn("id");
    EntityType refEntityType = mock(EntityType.class, "refEntityType");
    when(refEntityType.getId()).thenReturn(refEntityTypeId);
    when(refEntityType.getIdAttribute()).thenReturn(refIdAttribute);
    Attribute idAttribute = mock(Attribute.class, "id");
    when(idAttribute.getDataType()).thenReturn(STRING);
    Attribute mrefAttribute = mock(Attribute.class, "mref");
    when(mrefAttribute.getName()).thenReturn("MyField");
    when(mrefAttribute.getDataType()).thenReturn(MREF);
    when(mrefAttribute.getRefEntity()).thenReturn(refEntityType);
    EntityType entityType = mock(EntityType.class, "entityType");
    when(entityType.getIdAttribute()).thenReturn(idAttribute);
    when(entityType.getAttribute(fieldId)).thenReturn(mrefAttribute);
    Repository<Entity> repository = mock(Repository.class);
    when(repository.getEntityType()).thenReturn(entityType);
    Fetch fetch = new Fetch().field("MyField", new Fetch().field("id"));
    Entity entity = mock(Entity.class);
    doReturn(entity).when(repository).findOneById(entityId, fetch);
    Repository<Entity> refRepository = mock(Repository.class);
    when(refRepository.getEntityType()).thenReturn(refEntityType);
    Entity entity1 = mock(Entity.class);
    doReturn("entity1").when(entity1).getIdValue();
    Entity entity2 = mock(Entity.class);
    doReturn("entity2").when(entity2).getIdValue();
    Entity entity3 = mock(Entity.class);
    doReturn("entity3").when(entity3).getIdValue();
    doReturn(Arrays.asList(entity1, entity2, entity3)).when(entity).getEntities("MyField");
    Sort sort = Sort.create("field", Direction.ASC);
    Query q = Query.builder().setOperator(Operator.MATCHES).setValue("value").build();
    org.molgenis.data.Query<Entity> findAllQuery = new QueryImpl<>();
    findAllQuery.eq("field1", "value1").or().eq("field2", "value2");
    org.molgenis.data.Sort dataSort = mock(org.molgenis.data.Sort.class);
    org.molgenis.data.Query<Entity> findQuery = new QueryImpl<>();
    findQuery.nest().eq("field1", "value1").or().eq("field2", "value2").unnest().and();
    findQuery.in("id", Arrays.asList("entity1", "entity2", "entity3"));
    findQuery.fetch(fetch);
    findQuery.offset(10);
    findQuery.pageSize(10);
    findQuery.sort(dataSort);
    org.molgenis.data.Query<Entity> countQuery = new QueryImpl<>();
    countQuery.nest().eq("field1", "value1").or().eq("field2", "value2").unnest().and();
    countQuery.offset(0);
    countQuery.pageSize(Integer.MAX_VALUE);
    when(queryMapper.map(q, refRepository)).thenReturn(findAllQuery).thenReturn(countQuery);
    countQuery.in("id", Arrays.asList("entity1", "entity2", "entity3"));
    when(refRepository.count(countQuery)).thenReturn(100L);
    when(refRepository.findAll(findQuery)).thenReturn(Stream.of(entity1, entity2));
    when(sortMapper.map(sort, refEntityType)).thenReturn(dataSort);
    doReturn(Optional.of(repository)).when(metaDataService).getRepository(entityTypeId);
    doReturn(Optional.of(refRepository)).when(metaDataService).getRepository(refEntityTypeId);
    Entities actual = dataServiceV3Impl.findSubresources(entityTypeId, entityId, fieldId, q, filter, expand, sort, 10, 1);
    assertEquals(actual, Entities.builder().setEntities(asList(entity1, entity2)).setTotal(100).build());
}
Also used : Entity(org.molgenis.data.Entity) Query(org.molgenis.api.model.Query) Attribute(org.molgenis.data.meta.model.Attribute) Selection(org.molgenis.api.model.Selection) EntityType(org.molgenis.data.meta.model.EntityType) Fetch(org.molgenis.data.Fetch) QueryImpl(org.molgenis.data.support.QueryImpl) Sort(org.molgenis.api.model.Sort) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 18 with Sort

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

the class DataServiceV3ImplTest method testFindFieldUnknownAttributeException.

@SuppressWarnings("unchecked")
@Test
void testFindFieldUnknownAttributeException() {
    String entityTypeId = "MyEntityType";
    String entityId = "MyEntity";
    String fieldId = "MyField";
    Selection filter = Selection.FULL_SELECTION;
    Selection expand = Selection.EMPTY_SELECTION;
    Attribute idAttribute = mock(Attribute.class, "id");
    when(idAttribute.getDataType()).thenReturn(STRING);
    EntityType entityType = mock(EntityType.class, "entityType");
    when(entityType.getIdAttribute()).thenReturn(idAttribute);
    when(entityType.getAttribute(fieldId)).thenReturn(null);
    Repository<Entity> repository = mock(Repository.class);
    when(repository.getEntityType()).thenReturn(entityType);
    Fetch fetch = new Fetch().field("MyField", new Fetch().field("id"));
    Sort sort = Sort.create("field", Direction.ASC);
    Query q = Query.builder().setOperator(Operator.MATCHES).setValue("value").build();
    org.molgenis.data.Query<Entity> findAllQuery = mock(org.molgenis.data.Query.class);
    org.molgenis.data.Sort dataSort = mock(org.molgenis.data.Sort.class);
    org.molgenis.data.Query<Entity> findQuery = new QueryImpl<>(findAllQuery);
    findQuery.fetch(fetch);
    findQuery.offset(10);
    findQuery.pageSize(10);
    findQuery.sort(dataSort);
    org.molgenis.data.Query<Entity> countQuery = new QueryImpl<>(findAllQuery);
    countQuery.offset(0);
    countQuery.pageSize(Integer.MAX_VALUE);
    doReturn(Optional.of(repository)).when(metaDataService).getRepository(entityTypeId);
    assertThrows(UnknownAttributeException.class, () -> dataServiceV3Impl.findSubresources(entityTypeId, entityId, fieldId, q, filter, expand, sort, 10, 1));
}
Also used : Entity(org.molgenis.data.Entity) Query(org.molgenis.api.model.Query) Attribute(org.molgenis.data.meta.model.Attribute) Selection(org.molgenis.api.model.Selection) EntityType(org.molgenis.data.meta.model.EntityType) Fetch(org.molgenis.data.Fetch) QueryImpl(org.molgenis.data.support.QueryImpl) Sort(org.molgenis.api.model.Sort) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 19 with Sort

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

the class DataServiceV3ImplTest method testFindFieldNoEntities.

@SuppressWarnings("unchecked")
@Test
void testFindFieldNoEntities() {
    String entityTypeId = "MyEntityType";
    String refEntityTypeId = "refEntityType";
    String entityId = "MyEntity";
    String fieldId = "MyField";
    Selection filter = Selection.FULL_SELECTION;
    Selection expand = Selection.EMPTY_SELECTION;
    Attribute refIdAttribute = mock(Attribute.class, "mrefId");
    when(refIdAttribute.getName()).thenReturn("id");
    EntityType refEntityType = mock(EntityType.class, "refEntityType");
    when(refEntityType.getId()).thenReturn(refEntityTypeId);
    when(refEntityType.getIdAttribute()).thenReturn(refIdAttribute);
    Attribute idAttribute = mock(Attribute.class, "id");
    when(idAttribute.getDataType()).thenReturn(STRING);
    Attribute mrefAttribute = mock(Attribute.class, "mref");
    when(mrefAttribute.getName()).thenReturn("MyField");
    when(mrefAttribute.getDataType()).thenReturn(MREF);
    when(mrefAttribute.getRefEntity()).thenReturn(refEntityType);
    EntityType entityType = mock(EntityType.class, "entityType");
    when(entityType.getIdAttribute()).thenReturn(idAttribute);
    when(entityType.getAttribute(fieldId)).thenReturn(mrefAttribute);
    Repository<Entity> repository = mock(Repository.class);
    when(repository.getEntityType()).thenReturn(entityType);
    Fetch fetch = new Fetch().field("MyField", new Fetch().field("id"));
    Entity entity = mock(Entity.class);
    doReturn(entity).when(repository).findOneById(entityId, fetch);
    Repository<Entity> refRepository = mock(Repository.class);
    Sort sort = Sort.create("field", Direction.ASC);
    Query q = Query.builder().setOperator(Operator.MATCHES).setValue("value").build();
    org.molgenis.data.Query<Entity> findAllQuery = mock(org.molgenis.data.Query.class);
    org.molgenis.data.Sort dataSort = mock(org.molgenis.data.Sort.class);
    org.molgenis.data.Query<Entity> findQuery = new QueryImpl<>(findAllQuery);
    findQuery.fetch(fetch);
    findQuery.offset(10);
    findQuery.pageSize(10);
    findQuery.sort(dataSort);
    org.molgenis.data.Query<Entity> countQuery = new QueryImpl<>(findAllQuery);
    countQuery.offset(0);
    countQuery.pageSize(Integer.MAX_VALUE);
    when(queryMapper.map(q, refRepository)).thenReturn(findAllQuery).thenReturn(countQuery);
    doReturn(Optional.of(repository)).when(metaDataService).getRepository(entityTypeId);
    doReturn(Optional.of(refRepository)).when(metaDataService).getRepository(refEntityTypeId);
    Entities actual = dataServiceV3Impl.findSubresources(entityTypeId, entityId, fieldId, q, filter, expand, sort, 10, 1);
    assertEquals(actual, Entities.builder().setEntities(Collections.emptyList()).setTotal(0).build());
}
Also used : Entity(org.molgenis.data.Entity) Query(org.molgenis.api.model.Query) Attribute(org.molgenis.data.meta.model.Attribute) Selection(org.molgenis.api.model.Selection) EntityType(org.molgenis.data.meta.model.EntityType) Fetch(org.molgenis.data.Fetch) QueryImpl(org.molgenis.data.support.QueryImpl) Sort(org.molgenis.api.model.Sort) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 20 with Sort

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

the class DataServiceV3ImplTest method testFindFieldUnknownEntity.

@SuppressWarnings("unchecked")
@Test
void testFindFieldUnknownEntity() {
    String entityTypeId = "MyEntityType";
    String refEntityTypeId = "refEntityType";
    String entityId = "MyEntity";
    String fieldId = "MyField";
    Selection filter = Selection.FULL_SELECTION;
    Selection expand = Selection.EMPTY_SELECTION;
    Attribute refIdAttribute = mock(Attribute.class, "mrefId");
    when(refIdAttribute.getName()).thenReturn("id");
    EntityType refEntityType = mock(EntityType.class, "refEntityType");
    when(refEntityType.getId()).thenReturn(refEntityTypeId);
    when(refEntityType.getIdAttribute()).thenReturn(refIdAttribute);
    Attribute idAttribute = mock(Attribute.class, "id");
    when(idAttribute.getDataType()).thenReturn(STRING);
    Attribute mrefAttribute = mock(Attribute.class, "mref");
    when(mrefAttribute.getName()).thenReturn("MyField");
    when(mrefAttribute.getDataType()).thenReturn(MREF);
    when(mrefAttribute.getRefEntity()).thenReturn(refEntityType);
    EntityType entityType = mock(EntityType.class, "entityType");
    when(entityType.getIdAttribute()).thenReturn(idAttribute);
    when(entityType.getAttribute(fieldId)).thenReturn(mrefAttribute);
    Repository<Entity> repository = mock(Repository.class);
    when(repository.getEntityType()).thenReturn(entityType);
    Fetch fetch = new Fetch().field("MyField", new Fetch().field("id"));
    doReturn(null).when(repository).findOneById(entityId, fetch);
    Sort sort = Sort.create("field", Direction.ASC);
    Query q = Query.builder().setOperator(Operator.MATCHES).setValue("value").build();
    org.molgenis.data.Query<Entity> findAllQuery = mock(org.molgenis.data.Query.class);
    org.molgenis.data.Sort dataSort = mock(org.molgenis.data.Sort.class);
    org.molgenis.data.Query<Entity> findQuery = new QueryImpl<>(findAllQuery);
    findQuery.fetch(fetch);
    findQuery.offset(10);
    findQuery.pageSize(10);
    findQuery.sort(dataSort);
    org.molgenis.data.Query<Entity> countQuery = new QueryImpl<>(findAllQuery);
    countQuery.offset(0);
    countQuery.pageSize(Integer.MAX_VALUE);
    doReturn(Optional.of(repository)).when(metaDataService).getRepository(entityTypeId);
    assertThrows(UnknownEntityException.class, () -> dataServiceV3Impl.findSubresources(entityTypeId, entityId, fieldId, q, filter, expand, sort, 10, 1));
}
Also used : Entity(org.molgenis.data.Entity) Query(org.molgenis.api.model.Query) Attribute(org.molgenis.data.meta.model.Attribute) Selection(org.molgenis.api.model.Selection) EntityType(org.molgenis.data.meta.model.EntityType) Fetch(org.molgenis.data.Fetch) QueryImpl(org.molgenis.data.support.QueryImpl) Sort(org.molgenis.api.model.Sort) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Aggregations

Sort (org.molgenis.api.model.Sort)22 Test (org.junit.jupiter.api.Test)17 Query (org.molgenis.api.model.Query)14 EntityType (org.molgenis.data.meta.model.EntityType)14 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)14 Selection (org.molgenis.api.model.Selection)11 Attribute (org.molgenis.data.meta.model.Attribute)11 QueryImpl (org.molgenis.data.support.QueryImpl)8 Entity (org.molgenis.data.Entity)7 Fetch (org.molgenis.data.Fetch)7 Transactional (org.springframework.transaction.annotation.Transactional)4 GetMapping (org.springframework.web.bind.annotation.GetMapping)4 AttributeMetadata (org.molgenis.data.meta.model.AttributeMetadata)3 EntitiesResponse (org.molgenis.api.data.v3.model.EntitiesResponse)2 QueryRule (org.molgenis.data.QueryRule)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 Maps (com.google.common.collect.Maps)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1