Search in sources :

Example 6 with DataItem

use of org.hisp.dhis.dataitem.DataItem in project dhis2-core by dhis2.

the class PaginationHelperTest method testPaginateWhenFirstPage.

@Test
void testPaginateWhenFirstPage() {
    // Given
    final int pageSize = 5;
    final int firstPage = 1;
    final int totalOfItems = 13;
    final WebOptions theWebOptions = mockWebOptions(pageSize, firstPage);
    final List<DataItem> anyDimensionalItems = mockDimensionalItems(totalOfItems);
    // When
    final List<DataItem> resultingList = paginate(theWebOptions, anyDimensionalItems);
    // Then
    assertThat(resultingList, hasSize(5));
}
Also used : DataItem(org.hisp.dhis.dataitem.DataItem) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) Test(org.junit.jupiter.api.Test)

Example 7 with DataItem

use of org.hisp.dhis.dataitem.DataItem in project dhis2-core by dhis2.

the class DataItemQueryControllerTest method testGetWithSuccess.

@Test
void testGetWithSuccess() {
    // Given
    final Map<String, String> anyUrlParameters = new HashMap<>();
    final OrderParams anyOrderParams = new OrderParams();
    final User anyUser = new User();
    final Set<Class<? extends BaseIdentifiableObject>> targetEntities = new HashSet<>(singletonList(Indicator.class));
    final List<DataItem> itemsFound = singletonList(new DataItem());
    // When
    when(dataItemServiceFacade.extractTargetEntities(anySet())).thenReturn(targetEntities);
    when(aclService.canRead(anyUser, Indicator.class)).thenReturn(true);
    when(dataItemServiceFacade.retrieveDataItemEntities(anySet(), anySet(), any(WebOptions.class), any(OrderParams.class))).thenReturn(itemsFound);
    final ResponseEntity<RootNode> actualResponse = dataItemQueryController.getJson(anyUrlParameters, anyOrderParams, anyUser);
    // Then
    assertThat(actualResponse, is(not(nullValue())));
    assertThat(actualResponse.getStatusCode(), is(OK));
    verify(responseHandler, times(1)).addResultsToNode(any(RootNode.class), anyList(), anySet());
    verify(responseHandler, times(1)).addPaginationToNode(any(RootNode.class), anySet(), any(), any(), anySet());
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) User(org.hisp.dhis.user.User) HashMap(java.util.HashMap) DataItem(org.hisp.dhis.dataitem.DataItem) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) Indicator(org.hisp.dhis.indicator.Indicator) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) OrderParams(org.hisp.dhis.dxf2.common.OrderParams) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 8 with DataItem

use of org.hisp.dhis.dataitem.DataItem in project dhis2-core by dhis2.

the class DataItemQueryControllerTest method testGetWhenItemsAreNotFound.

@Test
void testGetWhenItemsAreNotFound() {
    // Given
    final Map<String, String> anyUrlParameters = new HashMap<>();
    final OrderParams anyOrderParams = new OrderParams();
    final User anyUser = new User();
    final Set<Class<? extends BaseIdentifiableObject>> targetEntities = new HashSet<>(singletonList(Indicator.class));
    final List<DataItem> itemsFound = emptyList();
    // When
    when(dataItemServiceFacade.extractTargetEntities(anySet())).thenReturn(targetEntities);
    when(aclService.canRead(anyUser, Indicator.class)).thenReturn(true);
    when(dataItemServiceFacade.retrieveDataItemEntities(anySet(), anySet(), any(WebOptions.class), any(OrderParams.class))).thenReturn(itemsFound);
    final ResponseEntity<RootNode> actualResponse = dataItemQueryController.getJson(anyUrlParameters, anyOrderParams, anyUser);
    // Then
    assertThat(actualResponse, is(not(nullValue())));
    assertThat(actualResponse.getStatusCode(), is(OK));
    verify(responseHandler, times(1)).addResultsToNode(any(), anyList(), anySet());
    verify(responseHandler, times(1)).addPaginationToNode(any(), anySet(), any(), any(), anySet());
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) User(org.hisp.dhis.user.User) HashMap(java.util.HashMap) DataItem(org.hisp.dhis.dataitem.DataItem) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) Indicator(org.hisp.dhis.indicator.Indicator) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) OrderParams(org.hisp.dhis.dxf2.common.OrderParams) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 9 with DataItem

use of org.hisp.dhis.dataitem.DataItem in project dhis2-core by dhis2.

the class DataItemQueryController method getDimensionalItems.

/**
 * Based on the informed arguments, this method will read the URL and based
 * on the give params will retrieve the respective data items.
 *
 * @param currentUser the logged user
 * @param urlParameters the request url params
 * @param orderParams the request order params
 * @return the complete root node
 */
private ResponseEntity<RootNode> getDimensionalItems(final User currentUser, final Map<String, String> urlParameters, final OrderParams orderParams) {
    // Defining the input params.
    final Set<String> fields = newHashSet(contextService.getParameterValues(FIELDS));
    final Set<String> filters = newHashSet(contextService.getParameterValues(FILTER));
    final WebOptions options = new WebOptions(urlParameters);
    if (fields.isEmpty()) {
        fields.addAll(Preset.ALL.getFields());
    }
    checkNamesAndOperators(filters);
    checkOrderParams(orderParams.getOrders());
    // Extracting the target entities to be queried.
    final Set<Class<? extends BaseIdentifiableObject>> targetEntities = dataItemServiceFacade.extractTargetEntities(filters);
    // Checking if the user can read all the target entities.
    checkAuthorization(currentUser, targetEntities);
    // Retrieving the data items based on the input params.
    final List<DataItem> dimensionalItems = dataItemServiceFacade.retrieveDataItemEntities(targetEntities, filters, options, orderParams);
    // Creating the response node.
    final RootNode rootNode = createMetadata();
    responseHandler.addResultsToNode(rootNode, dimensionalItems, fields);
    responseHandler.addPaginationToNode(rootNode, targetEntities, currentUser, options, filters);
    return new ResponseEntity<>(rootNode, OK);
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) ResponseEntity(org.springframework.http.ResponseEntity) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) DataItem(org.hisp.dhis.dataitem.DataItem) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions)

Example 10 with DataItem

use of org.hisp.dhis.dataitem.DataItem in project dhis2-core by dhis2.

the class DataItemServiceFacadeTest method testRetrieveDataItemEntitiesWhenTargetEntitiesIsEmpty.

@Test
void testRetrieveDataItemEntitiesWhenTargetEntitiesIsEmpty() {
    // Given
    final Set<Class<? extends BaseIdentifiableObject>> anyTargetEntities = emptySet();
    final Set<String> anyFilters = newHashSet("anyFilter");
    final WebOptions anyWebOptions = mockWebOptions(10, 1);
    final Set<String> anyOrdering = new HashSet<>(asList("name:desc"));
    final OrderParams anyOrderParams = new OrderParams(anyOrdering);
    // When
    final List<DataItem> actualDimensionalItems = dataItemServiceFacade.retrieveDataItemEntities(anyTargetEntities, anyFilters, anyWebOptions, anyOrderParams);
    // Then
    assertThat(actualDimensionalItems, is(empty()));
}
Also used : BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) DataItem(org.hisp.dhis.dataitem.DataItem) OrderParams(org.hisp.dhis.dxf2.common.OrderParams) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) HashSet(java.util.HashSet) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) Test(org.junit.jupiter.api.Test)

Aggregations

DataItem (org.hisp.dhis.dataitem.DataItem)14 WebOptions (org.hisp.dhis.webapi.webdomain.WebOptions)11 Test (org.junit.jupiter.api.Test)11 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)5 HashSet (java.util.HashSet)4 OrderParams (org.hisp.dhis.dxf2.common.OrderParams)4 RootNode (org.hisp.dhis.node.types.RootNode)4 User (org.hisp.dhis.user.User)4 Indicator (org.hisp.dhis.indicator.Indicator)3 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)2 CollectionNode (org.hisp.dhis.node.types.CollectionNode)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 ResponseEntity (org.springframework.http.ResponseEntity)1