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());
}
Aggregations