use of org.molgenis.api.model.Query in project molgenis by molgenis.
the class DataServiceV3ImplTest method testDeleteAll.
@SuppressWarnings("unchecked")
@Test
void testDeleteAll() {
String entityTypeId = "MyEntityType";
Repository<Entity> repository = mock(Repository.class);
when(metaDataService.getRepository(entityTypeId)).thenReturn(Optional.of(repository));
Entity entity1 = mock(Entity.class);
Entity entity2 = mock(Entity.class);
Query query = Query.builder().setOperator(Operator.MATCHES).setValue("value").build();
org.molgenis.data.Query<Entity> molgenisQuery = mock(org.molgenis.data.Query.class);
when(queryMapper.map(query, repository)).thenReturn(molgenisQuery);
dataServiceV3Impl.deleteAll(entityTypeId, query);
ArgumentCaptor<Stream> captor = ArgumentCaptor.forClass(Stream.class);
verify(repository).delete(captor.capture());
Stream<Entity> entityStream = captor.getValue();
entityStream.collect(Collectors.toList()).containsAll(Arrays.asList(entity1, entity2));
}
use of org.molgenis.api.model.Query 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));
}
use of org.molgenis.api.model.Query 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());
}
use of org.molgenis.api.model.Query 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));
}
use of org.molgenis.api.model.Query 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));
}
Aggregations