Search in sources :

Example 26 with Query

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);
}
Also used : MetadataUpsertJobExecution(org.molgenis.api.metadata.v3.job.MetadataUpsertJobExecution) Query(org.molgenis.api.model.Query) DeleteAttributesRequest(org.molgenis.api.metadata.v3.model.DeleteAttributesRequest) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 27 with 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));
}
Also used : EntityTypesResponse(org.molgenis.api.metadata.v3.model.EntityTypesResponse) Query(org.molgenis.api.model.Query) Sort(org.molgenis.api.model.Sort) ReadEntityTypesRequest(org.molgenis.api.metadata.v3.model.ReadEntityTypesRequest) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 28 with Query

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()));
}
Also used : Query(org.molgenis.api.model.Query) AttributeMetadata(org.molgenis.data.meta.model.AttributeMetadata) Attribute(org.molgenis.data.meta.model.Attribute) EntityType(org.molgenis.data.meta.model.EntityType) QueryImpl(org.molgenis.data.support.QueryImpl) Sort(org.molgenis.api.model.Sort) QueryRule(org.molgenis.data.QueryRule) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 29 with Query

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));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Query(org.molgenis.api.model.Query) AttributeMetadata(org.molgenis.data.meta.model.AttributeMetadata) Attribute(org.molgenis.data.meta.model.Attribute) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 30 with Query

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()));
}
Also used : Query(org.molgenis.api.model.Query) AttributeMetadata(org.molgenis.data.meta.model.AttributeMetadata) EntityType(org.molgenis.data.meta.model.EntityType) Sort(org.molgenis.api.model.Sort) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Aggregations

Query (org.molgenis.api.model.Query)58 Test (org.junit.jupiter.api.Test)52 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)52 Entity (org.molgenis.data.Entity)27 Attribute (org.molgenis.data.meta.model.Attribute)22 QueryImpl (org.molgenis.data.support.QueryImpl)22 ComparisonNode (cz.jirutka.rsql.parser.ast.ComparisonNode)17 ComparisonOperator (cz.jirutka.rsql.parser.ast.ComparisonOperator)17 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)16 EntityType (org.molgenis.data.meta.model.EntityType)15 Sort (org.molgenis.api.model.Sort)14 Selection (org.molgenis.api.model.Selection)9 Fetch (org.molgenis.data.Fetch)7 AttributeMetadata (org.molgenis.data.meta.model.AttributeMetadata)5 MethodSource (org.junit.jupiter.params.provider.MethodSource)4 EntitiesResponse (org.molgenis.api.data.v3.model.EntitiesResponse)2 QueryRule (org.molgenis.data.QueryRule)2 AndNode (cz.jirutka.rsql.parser.ast.AndNode)1 OrNode (cz.jirutka.rsql.parser.ast.OrNode)1 Stream (java.util.stream.Stream)1