Search in sources :

Example 31 with FieldFilterParams

use of org.hisp.dhis.fieldfilter.FieldFilterParams in project dhis2-core by dhis2.

the class DataElementOperandControllerTest method verifyPaginationGetFirstPage.

@Test
void verifyPaginationGetFirstPage() throws Exception {
    int pageSize = 15;
    long totalSize = 150;
    // Given
    final List<DataElement> dataElements = rnd.objects(DataElement.class, 1).collect(Collectors.toList());
    when(manager.getAllSorted(DataElement.class)).thenReturn(dataElements);
    final List<DataElementOperand> dataElementOperands = rnd.objects(DataElementOperand.class, (int) totalSize).collect(Collectors.toList());
    when(dataElementCategoryService.getOperands(dataElements, true)).thenReturn(rnd.objects(DataElementOperand.class, (int) totalSize).collect(Collectors.toList()));
    final List<DataElementOperand> first50elements = dataElementOperands.subList(0, pageSize);
    ArgumentCaptor<FieldFilterParams> filterParamsArgumentCaptor = ArgumentCaptor.forClass(FieldFilterParams.class);
    when(fieldFilterService.toCollectionNode(eq(DataElementOperand.class), filterParamsArgumentCaptor.capture())).thenReturn(buildResponse(first50elements));
    doReturn(totalSize).when(queryService).count(any(Query.class));
    // Then
    ResultActions resultActions = mockMvc.perform(get(ENDPOINT).param("totals", "true").param("pageSize", Integer.toString(pageSize))).andExpect(status().isOk()).andExpect(content().contentType("application/json")).andExpect(jsonPath("$.pager.pageSize").value(Integer.toString(pageSize))).andExpect(jsonPath("$.pager.page").value("1")).andExpect(jsonPath("$.pager.total").value(Long.toString(totalSize))).andExpect(jsonPath("$.pager.pageCount").value(Long.toString(totalSize / pageSize))).andExpect(jsonPath("$.dataElementOperands", hasSize(pageSize)));
    final FieldFilterParams fieldFilterParams = filterParamsArgumentCaptor.getValue();
    assertThat(fieldFilterParams.getObjects(), hasSize(pageSize));
    assertThat(fieldFilterParams.getFields(), Matchers.is(Lists.newArrayList("*")));
    // Make sure that the first and last element in the page matches with
    // the
    // original list
    List<Map<String, Object>> jsonDataElementOperands = convertResponse(resultActions);
    assertThat(jsonDataElementOperands.get(0).get("id"), Matchers.is(dataElementOperands.get(0).getDimensionItem()));
    assertThat(jsonDataElementOperands.get(pageSize - 1).get("id"), Matchers.is(dataElementOperands.get(pageSize - 1).getDimensionItem()));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) Query(org.hisp.dhis.query.Query) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) ResultActions(org.springframework.test.web.servlet.ResultActions) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 32 with FieldFilterParams

use of org.hisp.dhis.fieldfilter.FieldFilterParams in project dhis2-core by dhis2.

the class DataElementOperandControllerTest method verifyPaginationGetThirdPage.

@Test
void verifyPaginationGetThirdPage() throws Exception {
    int pageSize = 25;
    long totalSize = 100;
    // Given
    final List<DataElement> dataElements = rnd.objects(DataElement.class, 1).collect(Collectors.toList());
    when(manager.getAllSorted(DataElement.class)).thenReturn(dataElements);
    final List<DataElementOperand> dataElementOperands = rnd.objects(DataElementOperand.class, (int) totalSize).collect(Collectors.toList());
    when(dataElementCategoryService.getOperands(dataElements, true)).thenReturn(rnd.objects(DataElementOperand.class, (int) totalSize).collect(Collectors.toList()));
    final List<DataElementOperand> thirdPageElements = dataElementOperands.subList(50, 50 + pageSize);
    ArgumentCaptor<FieldFilterParams> filterParamsArgumentCaptor = ArgumentCaptor.forClass(FieldFilterParams.class);
    when(fieldFilterService.toCollectionNode(eq(DataElementOperand.class), filterParamsArgumentCaptor.capture())).thenReturn(buildResponse(thirdPageElements));
    doReturn(totalSize).when(queryService).count(any(Query.class));
    // Then
    ResultActions resultActions = mockMvc.perform(get(ENDPOINT).param("totals", "true").param("pageSize", Integer.toString(pageSize)).param("page", Integer.toString(3))).andExpect(status().isOk()).andExpect(content().contentType("application/json")).andExpect(jsonPath("$.pager.pageSize").value(Integer.toString(pageSize))).andExpect(jsonPath("$.pager.page").value("3")).andExpect(jsonPath("$.pager.total").value(Long.toString(totalSize))).andExpect(jsonPath("$.pager.pageCount").value(Long.toString(totalSize / pageSize))).andExpect(jsonPath("$.dataElementOperands", hasSize(pageSize)));
    final FieldFilterParams fieldFilterParams = filterParamsArgumentCaptor.getValue();
    assertThat(fieldFilterParams.getObjects(), hasSize(pageSize));
    assertThat(fieldFilterParams.getFields(), Matchers.is(Lists.newArrayList("*")));
    // Make sure that the first and last element in the page matches with
    // the
    // original list
    List<Map<String, Object>> jsonDataElementOperands = convertResponse(resultActions);
    assertThat(jsonDataElementOperands.get(0).get("id"), Matchers.is(dataElementOperands.get(50).getDimensionItem()));
    assertThat(jsonDataElementOperands.get(pageSize - 1).get("id"), Matchers.is(dataElementOperands.get(74).getDimensionItem()));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) Query(org.hisp.dhis.query.Query) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) ResultActions(org.springframework.test.web.servlet.ResultActions) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Aggregations

FieldFilterParams (org.hisp.dhis.fieldfilter.FieldFilterParams)32 RootNode (org.hisp.dhis.node.types.RootNode)28 GetMapping (org.springframework.web.bind.annotation.GetMapping)25 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)23 Pager (org.hisp.dhis.common.Pager)10 CollectionNode (org.hisp.dhis.node.types.CollectionNode)10 WebOptions (org.hisp.dhis.webapi.webdomain.WebOptions)8 ArrayList (java.util.ArrayList)7 Query (org.hisp.dhis.query.Query)6 WebMetadata (org.hisp.dhis.webapi.webdomain.WebMetadata)5 DataElement (org.hisp.dhis.dataelement.DataElement)4 DataSet (org.hisp.dhis.dataset.DataSet)4 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)4 Test (org.junit.jupiter.api.Test)4 List (java.util.List)3 Map (java.util.Map)3 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)3 Order (org.hisp.dhis.query.Order)3 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2