Search in sources :

Example 1 with Selection

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

the class DataServiceV3ImplTest method testFindUnknownRepository.

@Test
void testFindUnknownRepository() {
    String entityTypeId = "MyEntityType";
    String entityId = "MyId";
    Selection filter = Selection.FULL_SELECTION;
    Selection expand = Selection.EMPTY_SELECTION;
    when(metaDataService.getRepository(entityTypeId)).thenReturn(Optional.empty());
    assertThrows(UnknownRepositoryException.class, () -> dataServiceV3Impl.find(entityTypeId, entityId, filter, expand));
}
Also used : Selection(org.molgenis.api.model.Selection) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 2 with Selection

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

the class DataServiceV3ImplTest method testFindAll.

@SuppressWarnings("unchecked")
@Test
void testFindAll() {
    String entityTypeId = "MyEntityType";
    Selection filter = Selection.FULL_SELECTION;
    Selection expand = Selection.EMPTY_SELECTION;
    EntityType entityType = mock(EntityType.class);
    Repository<Entity> repository = mock(Repository.class);
    when(repository.getEntityType()).thenReturn(entityType);
    Entity entity1 = mock(Entity.class);
    Entity entity2 = mock(Entity.class);
    Sort sort = Sort.create("field", Direction.ASC);
    Fetch fetch = new Fetch().field("id", new Fetch().field("refAttr"));
    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(repository.findAll(findQuery)).thenReturn(Stream.of(entity1, entity2));
    when(repository.count(countQuery)).thenReturn(100L);
    when(queryMapper.map(q, repository)).thenReturn(findAllQuery).thenReturn(countQuery);
    when(sortMapper.map(sort, entityType)).thenReturn(dataSort);
    when(metaDataService.getRepository(entityTypeId)).thenReturn(Optional.of(repository));
    Entities actual = dataServiceV3Impl.findAll(entityTypeId, 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) 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 3 with Selection

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

the class DataServiceV3ImplTest method testFindFieldUnsupportedAttributeType.

@SuppressWarnings("unchecked")
@Test
void testFindFieldUnsupportedAttributeType() {
    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);
    Attribute mrefAttribute = mock(Attribute.class, "mref");
    when(mrefAttribute.getDataType()).thenReturn(STRING);
    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"));
    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(UnsupportedAttributeTypeException.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 4 with Selection

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

the class DataServiceV3ImplTest method testFindUnknownEntity.

@SuppressWarnings("unchecked")
@Test
void testFindUnknownEntity() {
    String entityTypeId = "MyEntityType";
    String entityId = "MyId";
    Selection filter = Selection.FULL_SELECTION;
    Selection expand = Selection.EMPTY_SELECTION;
    Attribute idAttribute = mock(Attribute.class);
    when(idAttribute.getDataType()).thenReturn(STRING);
    EntityType entityType = mock(EntityType.class);
    when(entityType.getIdAttribute()).thenReturn(idAttribute);
    Repository<Entity> repository = mock(Repository.class);
    when(repository.getEntityType()).thenReturn(entityType);
    when(metaDataService.getRepository(entityTypeId)).thenReturn(Optional.of(repository));
    assertThrows(UnknownEntityException.class, () -> dataServiceV3Impl.find(entityTypeId, entityId, filter, expand));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Entity(org.molgenis.data.Entity) 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 5 with Selection

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

the class DataServiceV3ImplTest method testFind.

@SuppressWarnings("unchecked")
@Test
void testFind() {
    String entityTypeId = "sys_md_EntityType";
    String entityId = "MyId";
    Selection filter = Selection.FULL_SELECTION;
    Selection expand = Selection.EMPTY_SELECTION;
    Attribute idAttribute = mock(Attribute.class);
    when(idAttribute.getDataType()).thenReturn(STRING);
    EntityType entityType = mock(EntityType.class);
    when(entityType.getIdAttribute()).thenReturn(idAttribute);
    Repository<Entity> repository = mock(Repository.class);
    when(repository.getEntityType()).thenReturn(entityType);
    Fetch fetch = new Fetch().field("id");
    when(fetchMapper.toFetch(entityType, filter, expand)).thenReturn(fetch);
    Entity entity = mock(Entity.class);
    when(repository.findOneById(entityId, fetch)).thenReturn(entity);
    when(metaDataService.getRepository(entityTypeId)).thenReturn(Optional.of(repository));
    assertEquals(dataServiceV3Impl.find(entityTypeId, entityId, filter, expand), entity);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Fetch(org.molgenis.data.Fetch) Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) Selection(org.molgenis.api.model.Selection) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

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