Search in sources :

Example 1 with SimpleCacheBuilder

use of org.hisp.dhis.cache.SimpleCacheBuilder in project dhis2-core by dhis2.

the class AnalyticsCacheTest method returnSameObjectAfterModifyCachedObject.

@Test
void returnSameObjectAfterModifyCachedObject() {
    // arrange
    final AnalyticsCacheSettings settings = new AnalyticsCacheSettings(systemSettingManager);
    final CacheBuilder<Grid> cacheBuilder = new SimpleCacheBuilder<>();
    cacheBuilder.expireAfterWrite(1L, TimeUnit.MINUTES);
    final Cache<Grid> cache = new LocalCache<>(cacheBuilder);
    Mockito.<Cache<Grid>>when(cacheProvider.createAnalyticsResponseCache(any(Duration.class))).thenReturn(cache);
    final AnalyticsCache analyticsCache = new AnalyticsCache(cacheProvider, settings);
    final Grid grid = new ListGrid();
    grid.addHeader(new GridHeader("Header1")).addHeader(new GridHeader("Header2")).addRow().addValue("Value11").addValue("Value12").addRow().addValue("Value21").addValue("Value22");
    DataQueryParams params = DataQueryParams.newBuilder().withDataElements(Lists.newArrayList(new DataElement("dataElementA"), new DataElement("dataElementB"))).build();
    // act, assert
    analyticsCache.put(params.getKey(), grid, 60);
    Optional<Grid> optCachedGrid = analyticsCache.get(params.getKey());
    assertTrue(optCachedGrid.isPresent());
    assertEquals(2, optCachedGrid.get().getHeaderWidth());
    assertEquals(2, optCachedGrid.get().getRows().size());
    // when the cachedGrid is not the clone of grid, next actions will
    // modify it
    grid.addHeader(new GridHeader("Header3")).addRow().addValue("31").addValue("32");
    optCachedGrid = analyticsCache.get(params.getKey());
    assertTrue(optCachedGrid.isPresent());
    assertEquals(2, optCachedGrid.get().getHeaderWidth());
    assertEquals(2, optCachedGrid.get().getRows().size());
}
Also used : LocalCache(org.hisp.dhis.cache.LocalCache) ListGrid(org.hisp.dhis.system.grid.ListGrid) Grid(org.hisp.dhis.common.Grid) SimpleCacheBuilder(org.hisp.dhis.cache.SimpleCacheBuilder) Duration(java.time.Duration) ListGrid(org.hisp.dhis.system.grid.ListGrid) GridHeader(org.hisp.dhis.common.GridHeader) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DataElement(org.hisp.dhis.dataelement.DataElement) Test(org.junit.jupiter.api.Test)

Aggregations

Duration (java.time.Duration)1 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)1 LocalCache (org.hisp.dhis.cache.LocalCache)1 SimpleCacheBuilder (org.hisp.dhis.cache.SimpleCacheBuilder)1 Grid (org.hisp.dhis.common.Grid)1 GridHeader (org.hisp.dhis.common.GridHeader)1 DataElement (org.hisp.dhis.dataelement.DataElement)1 ListGrid (org.hisp.dhis.system.grid.ListGrid)1 Test (org.junit.jupiter.api.Test)1