@Test
  public void
      getTopUpResultShouldDelegateToTopUpServiceAndNotCacheResultWhenPlayerHasNoCachedResultAndHasBeenToppedUpButNotAcknowledgedTopUp() {
    final TopUpResult topUpResult = new TopUpResult(PLAYER_ID, CREDITED, new DateTime());
    when(dailyAwardPromotionService.getTopUpResult(PLAYER_ID, WEB)).thenReturn(topUpResult);

    final TopUpResult actualTopUpResult = underTest.getTopUpResult(PLAYER_ID, WEB);

    // check that result wasn't cached before
    assertThat(topUpStatusCache.getStatistics().getCacheHits(), is(0L));
    // check that result wasn't cached after
    assertThat(topUpStatusCache.getStatistics().getMemoryStoreObjectCount(), is(0L));

    assertThat(actualTopUpResult, is(topUpResult));
  }
    private void runBasicPinningTest(Cache cache) {
      for (int i = 0; i < ELEMENT_COUNT; i++) {
        cache.put(new Element(i, i));
      }

      Assert.assertEquals(ELEMENT_COUNT, cache.getSize());

      for (int i = 0; i < ELEMENT_COUNT; i++) {
        Assert.assertNotNull(cache.get(i));
      }

      Assert.assertEquals(ELEMENT_COUNT, cache.getStatistics().getInMemoryHits());
      Assert.assertEquals(0, cache.getStatistics().getInMemoryMisses());
      Assert.assertEquals(0, cache.getStatistics().getOnDiskHits());
      Assert.assertEquals(0, cache.getStatistics().getOnDiskMisses());
      Assert.assertEquals(0, cache.getStatistics().getEvictionCount());
    }
  @Test
  public void getTopUpResultShouldReturnCachedResultIfPlayerHasCacheEntry() {
    topUpStatusCache.setStatisticsEnabled(true);
    long cacheHitsPreRequest = topUpStatusCache.getStatistics().getCacheHits();
    final TopUpResult expectedTopUpResult =
        new TopUpResult(PLAYER_ID, ACKNOWLEDGED, new DateTime());
    Element element =
        new Element(
            PLAYER_ID,
            new TopUpResultService.TopUpStatusCacheEntry(
                ACKNOWLEDGED, expectedTopUpResult.getLastTopUpDate()));
    topUpStatusCache.put(element);

    final TopUpResult actualTopUpResult = underTest.getTopUpResult(PLAYER_ID, WEB);

    assertThat(topUpStatusCache.getStatistics().getCacheHits(), is(cacheHitsPreRequest + 1));
    assertThat(actualTopUpResult, is(expectedTopUpResult));
    verify(dailyAwardPromotionService, never()).getTopUpResult(PLAYER_ID, WEB);
  }
  @Test
  public void
      getTopUpResultShouldCacheResultWhenPlayerHasNoCachedResultAndLastTopUpIsAcknowledged() {
    final TopUpResult topUpResult = new TopUpResult(PLAYER_ID, ACKNOWLEDGED, new DateTime());
    when(dailyAwardPromotionService.getTopUpResult(PLAYER_ID, WEB)).thenReturn(topUpResult);

    final TopUpResult actualTopUpResult = underTest.getTopUpResult(PLAYER_ID, WEB);

    // check that result wasn't cached before
    assertThat(topUpStatusCache.getStatistics().getCacheHits(), is(0L));
    // check that result was cached after
    assertThat(topUpStatusCache.getStatistics().getMemoryStoreObjectCount(), is(1L));
    // check cache entry
    TopUpResultService.TopUpStatusCacheEntry entry =
        (TopUpResultService.TopUpStatusCacheEntry) topUpStatusCache.get(PLAYER_ID).getObjectValue();
    assertThat(entry.getTopUpStatus(), is(ACKNOWLEDGED));
    assertThat(entry.getLastTopUpDate(), is(topUpResult.getLastTopUpDate()));

    assertThat(actualTopUpResult, is(topUpResult));
  }
  @Test
  public void acknowledgeTopUpResultShouldPutAcknowledgedResultIntoCache() {
    final TopUpAcknowledgeRequest topUpAcknowledgeRequest =
        new TopUpAcknowledgeRequest(PLAYER_ID, new DateTime());

    underTest.acknowledgeTopUpResult(topUpAcknowledgeRequest);

    assertThat(topUpStatusCache.getStatistics().getMemoryStoreObjectCount(), is(1L));
    TopUpResultService.TopUpStatusCacheEntry entry =
        (TopUpResultService.TopUpStatusCacheEntry) topUpStatusCache.get(PLAYER_ID).getObjectValue();
    assertThat(entry.getTopUpStatus(), is(ACKNOWLEDGED));
    assertThat(entry.getLastTopUpDate(), is(topUpAcknowledgeRequest.getTopUpDate()));
  }
  public static void main(String args[]) {

    Map<Object, Element> map = new HashMap<Object, Element>();
    List<String> list = new ArrayList<String>();

    // Create a cache manager
    CacheManager cacheManager = CacheManager.getInstance();

    // Creates a cache called newCache
    cacheManager.addCache("newCache");

    // Get cache called newCache
    Cache cache = cacheManager.getCache("newCache");
    StatisticsGateway stats = cache.getStatistics();

    // put into cache
    cache.put(new Element("1", "Monday"));
    list.add("1");
    cache.put(new Element("2", "Tuesday"));
    list.add("2");
    cache.put(new Element("3", "Wednesday"));
    list.add("3");
    cache.put(new Element("4", "Thursday"));
    list.add("4");

    // Displaying all elements
    System.out.println("All elements");
    map = cache.getAll(list);
    Iterator it = map.entrySet().iterator();
    while (it.hasNext()) {
      Map.Entry pair = (Map.Entry) it.next();
      System.out.println(pair.getKey() + " = " + pair.getValue());
    }

    // Displaying elements and size of cache
    Element element = cache.get("1");
    System.out.println("Value of key 1 :" + element.getObjectValue().toString());
    System.out.println("Cache Size " + cache.getSize());
    element = cache.get("2");
    System.out.println("Value of key 2 :" + element.getObjectValue().toString());
    System.out.println("Cache Size " + cache.getSize());
    cache.removeElement(element);
    System.out.println("Cache Size after removing an element : " + cache.getSize());
    cache.flush();
    System.out.println("Removed Cache with key 3 :" + cache.remove("3"));
    System.out.println("Size after remove : " + cache.getSize());
  }
  @Test
  public void acknowledgeTopUpResultShouldNotSendAcknowledgeRequestIfAlreadyCached() {
    final TopUpAcknowledgeRequest topUpAcknowledgeRequest =
        new TopUpAcknowledgeRequest(PLAYER_ID, new DateTime());
    Element element =
        new Element(
            PLAYER_ID,
            new TopUpResultService.TopUpStatusCacheEntry(
                ACKNOWLEDGED, topUpAcknowledgeRequest.getTopUpDate()));
    topUpStatusCache.put(element);

    underTest.acknowledgeTopUpResult(topUpAcknowledgeRequest);

    assertThat(topUpStatusCache.getStatistics().getMemoryStoreObjectCount(), is(1L));
    TopUpResultService.TopUpStatusCacheEntry entry =
        (TopUpResultService.TopUpStatusCacheEntry) topUpStatusCache.get(PLAYER_ID).getObjectValue();
    assertThat(entry.getTopUpStatus(), is(ACKNOWLEDGED));
    assertThat(entry.getLastTopUpDate(), is(topUpAcknowledgeRequest.getTopUpDate()));
    verify(queuePublishingService, never()).send(topUpAcknowledgeRequest);
  }
  @Test
  public void testParallelAndSequentialAreEquivalent() {
    CacheManager manager =
        new CacheManager(
            new Configuration()
                .name("testEqualsDriver")
                .cache(
                    new CacheConfiguration()
                        .name("sequential")
                        .maxBytesLocalHeap(64, MemoryUnit.MEGABYTES))
                .cache(
                    new CacheConfiguration()
                        .name("parallel")
                        .maxBytesLocalHeap(64, MemoryUnit.MEGABYTES)));

    Cache sequential = manager.getCache("sequential");
    Cache parallel = manager.getCache("parallel");

    CacheDriver loadSequential =
        CacheLoader.load(ehcache(sequential))
            .using(StringGenerator.integers(), ByteArrayGenerator.fixedSize(128))
            .sequentially()
            .untilFilled();
    SequentialDriver.inSequence(loadSequential).run();
    long seqHeapSize = sequential.getStatistics().getLocalHeapSize();
    long seqOffHeapSize = sequential.getStatistics().getLocalOffHeapSize();
    long seqDiskSize = sequential.getStatistics().getLocalDiskSize();

    CacheDriver loadParallel =
        CacheLoader.load(ehcache(parallel))
            .using(StringGenerator.integers(), ByteArrayGenerator.fixedSize(128))
            .sequentially()
            .untilFilled();
    ParallelDriver.inParallel(4, loadParallel).run();
    long parHeapSize = parallel.getStatistics().getLocalHeapSize();
    long parOffHeapSize = parallel.getStatistics().getLocalOffHeapSize();
    long parDiskSize = parallel.getStatistics().getLocalDiskSize();

    Assert.assertEquals(seqHeapSize, parHeapSize);
    Assert.assertEquals(seqOffHeapSize, parOffHeapSize);
    Assert.assertEquals(seqDiskSize, parDiskSize);

    manager.shutdown();
  }
 public Statistics getStatistics() {
   return cache.getStatistics();
 }