use of org.molgenis.api.model.Query in project molgenis by molgenis.
the class MetadataApiControllerTest method testDeleteAttributes.
@Test
void testDeleteAttributes() {
String entityTypeId = "MyEntityTypeId";
Query query = Query.create("name", IN, asList("name1", "name2"));
DeleteAttributesRequest deleteAttributesRequest = new DeleteAttributesRequest();
deleteAttributesRequest.setQ(query);
MetadataUpsertJobExecution jobExecution = mockUpsertJobExecution();
when(metadataApiService.deleteAttributesAsync(entityTypeId, query)).thenReturn(jobExecution);
metadataApiController.deleteAttributes(entityTypeId, deleteAttributesRequest);
verify(metadataApiService).deleteAttributesAsync(entityTypeId, query);
}
use of org.molgenis.api.model.Query in project molgenis by molgenis.
the class MetadataApiControllerTest method testGetEntityTypes.
@Test
void testGetEntityTypes() {
int page = 2;
int size = 1;
Sort sort = mock(Sort.class);
Query query = mock(Query.class);
ReadEntityTypesRequest readEntityTypesRequest = new ReadEntityTypesRequest();
readEntityTypesRequest.setPage(page);
readEntityTypesRequest.setSize(size);
readEntityTypesRequest.setSort(sort);
readEntityTypesRequest.setQ(query);
EntityTypes entityTypes = mock(EntityTypes.class);
when(metadataApiService.findEntityTypes(query, sort, size, page)).thenReturn(entityTypes);
EntityTypesResponse entityTypesResponse = mock(EntityTypesResponse.class);
when(entityTypeResponseMapper.toEntityTypesResponse(entityTypes, size, page)).thenReturn(entityTypesResponse);
assertEquals(entityTypesResponse, metadataApiController.getEntityTypes(readEntityTypesRequest));
}
use of org.molgenis.api.model.Query in project molgenis by molgenis.
the class MetadataApiServiceImplTest method testFindAttributesNestedQuery.
@SuppressWarnings("unchecked")
@Test
void testFindAttributesNestedQuery() {
Repository<Attribute> attributeRepository = mock(Repository.class);
EntityType entityType = mock(EntityType.class);
when(attributeRepository.getEntityType()).thenReturn(entityType);
when(metadataService.getRepository("sys_md_Attribute", Attribute.class)).thenReturn(Optional.of(attributeRepository));
Query query = mock(Query.class);
org.molgenis.data.Query<Attribute> repositoryQuery = new QueryImpl<>(asList(new QueryRule("id", QueryRule.Operator.EQUALS, "id0"), new QueryRule(QueryRule.Operator.OR), new QueryRule("id", QueryRule.Operator.EQUALS, "id1")));
when(queryMapper.map(query, attributeRepository)).thenReturn(repositoryQuery);
Attribute attribute = mock(Attribute.class);
ArgumentCaptor<org.molgenis.data.Query<Attribute>> findQueryCaptor = ArgumentCaptor.forClass(org.molgenis.data.Query.class);
when(attributeRepository.findAll(findQueryCaptor.capture())).thenReturn(Stream.of(attribute));
long total = 100L;
ArgumentCaptor<org.molgenis.data.Query<Attribute>> countQueryCaptor = ArgumentCaptor.forClass(org.molgenis.data.Query.class);
when(attributeRepository.count(countQueryCaptor.capture())).thenReturn(total);
Sort sort = Sort.EMPTY_SORT;
org.molgenis.data.Sort repositorySort = mock(org.molgenis.data.Sort.class);
when(sortMapper.map(sort, entityType)).thenReturn(repositorySort);
int size = 5;
int number = 2;
Attributes expectedAttributes = Attributes.builder().setAttributes(ImmutableList.of(attribute)).setTotal((int) total).build();
String entityTypeId = "MyEntityTypeId";
when(metadataService.hasEntityType(entityTypeId)).thenReturn(true);
Attributes attributes = metadataApiService.findAttributes(entityTypeId, query, sort, size, number);
org.molgenis.data.Query<Attribute> findQuery = findQueryCaptor.getValue();
org.molgenis.data.Query<Attribute> countQuery = countQueryCaptor.getValue();
assertAll(() -> assertEquals(expectedAttributes, attributes), () -> assertEquals(asList(new QueryRule(asList(new QueryRule("id", QueryRule.Operator.EQUALS, "id0"), new QueryRule(QueryRule.Operator.OR), new QueryRule("id", QueryRule.Operator.EQUALS, "id1"))), new QueryRule(QueryRule.Operator.AND), new QueryRule("entity", QueryRule.Operator.EQUALS, entityTypeId)), findQuery.getRules()), () -> assertEquals(size * number, findQuery.getOffset()), () -> assertEquals(size, findQuery.getPageSize()), () -> assertEquals(repositorySort, findQuery.getSort()), () -> assertEquals(asList(new QueryRule(asList(new QueryRule("id", QueryRule.Operator.EQUALS, "id0"), new QueryRule(QueryRule.Operator.OR), new QueryRule("id", QueryRule.Operator.EQUALS, "id1"))), new QueryRule(QueryRule.Operator.AND), new QueryRule("entity", QueryRule.Operator.EQUALS, entityTypeId)), countQuery.getRules()), () -> assertEquals(0, countQuery.getOffset()), () -> assertEquals(Integer.MAX_VALUE, countQuery.getPageSize()));
}
use of org.molgenis.api.model.Query in project molgenis by molgenis.
the class MetadataApiServiceImplTest method testDeleteAttributesQuery.
@SuppressWarnings("unchecked")
@Test
void testDeleteAttributesQuery() {
String entityTypeId = "test_entity1";
EntityType entityType = mock(EntityType.class);
when(metadataService.getEntityType(entityTypeId)).thenReturn(Optional.of(entityType));
Query query = Query.create("identifier", IN, asList("attr1", "attr2"));
org.molgenis.data.Query<Attribute> dataServiceQuery = mock(org.molgenis.data.Query.class);
when(dataServiceQuery.eq(AttributeMetadata.ENTITY, entityTypeId)).thenReturn(dataServiceQuery);
Attribute attribute1 = mock(Attribute.class);
Attribute attribute2 = mock(Attribute.class);
when(dataServiceQuery.findAll()).thenReturn(Stream.of(attribute1, attribute2));
Repository<Attribute> attributeRepository = mock(Repository.class);
when(metadataService.getRepository(ATTRIBUTE_META_DATA, Attribute.class)).thenReturn(Optional.of(attributeRepository));
when(queryMapper.map(query, attributeRepository)).thenReturn(dataServiceQuery);
metadataApiService.deleteAttributesAsync(entityTypeId, query);
assertAll(() -> verify(entityType).removeAttribute(attribute1), () -> verify(entityType).removeAttribute(attribute2), () -> metadataApiJobService.scheduleUpdate(entityType));
}
use of org.molgenis.api.model.Query in project molgenis by molgenis.
the class MetadataApiServiceImplTest method testFindEntityTypes.
@Test
void testFindEntityTypes() {
EntityType entityType = mock(EntityType.class);
@SuppressWarnings("unchecked") Repository<EntityType> entityTypeRepository = mock(Repository.class);
when(entityTypeRepository.getEntityType()).thenReturn(entityType);
when(metadataService.getRepository("sys_md_EntityType", EntityType.class)).thenReturn(Optional.of(entityTypeRepository));
Query query = Query.builder().setItem("id").setOperator(Operator.EQUALS).setValue("id0").build();
when(queryMapper.map(query, entityTypeRepository)).thenReturn(new QueryImpl<>());
@SuppressWarnings("unchecked") ArgumentCaptor<org.molgenis.data.Query<EntityType>> findQueryCaptor = ArgumentCaptor.forClass(org.molgenis.data.Query.class);
when(entityTypeRepository.findAll(findQueryCaptor.capture())).thenReturn(Stream.of(entityType));
long total = 100L;
@SuppressWarnings("unchecked") ArgumentCaptor<org.molgenis.data.Query<EntityType>> countQueryCaptor = ArgumentCaptor.forClass(org.molgenis.data.Query.class);
when(entityTypeRepository.count(countQueryCaptor.capture())).thenReturn(total);
Sort sort = Sort.EMPTY_SORT;
org.molgenis.data.Sort repositorySort = mock(org.molgenis.data.Sort.class);
when(sortMapper.map(sort, entityType)).thenReturn(repositorySort);
int size = 5;
int number = 2;
EntityTypes expectedEntityTypes = EntityTypes.builder().setEntityTypes(ImmutableList.of(entityType)).setTotal((int) total).build();
EntityTypes entityTypes = metadataApiService.findEntityTypes(query, sort, size, number);
org.molgenis.data.Query<EntityType> findQuery = findQueryCaptor.getValue();
org.molgenis.data.Query<EntityType> countQuery = countQueryCaptor.getValue();
assertAll(() -> assertEquals(expectedEntityTypes, entityTypes), () -> assertEquals(size * number, findQuery.getOffset()), () -> assertEquals(size, findQuery.getPageSize()), () -> assertEquals(repositorySort, findQuery.getSort()), () -> assertEquals(0, countQuery.getOffset()), () -> assertEquals(Integer.MAX_VALUE, countQuery.getPageSize()));
}
Aggregations