ApiKeyRepositoryWrapper implements ApiKeyRepository { @Override public Optional<ApiKey> findById(String apiKey) throws TechnicalException { return Optional .ofNullable(cache.get(apiKey)) .map(element -> (ApiKey) element.getObjectValue()); } ApiKeyRepositoryWrapper(ApiKeyRepository wrapped, Ehcache cache); @Override Optional<ApiKey> findById(String apiKey); @Override ApiKey create(ApiKey apiKey); @Override Set<ApiKey> findBySubscription(String subscription); @Override Set<ApiKey> findByPlan(String plan); @Override List<ApiKey> findByCriteria(ApiKeyCriteria filter); @Override ApiKey update(ApiKey apiKey);  }
@Test public void shouldFindById_empty() throws TechnicalException { String apiKey = "1234-4567-7890"; Mockito.when(cache.get(apiKey)).thenReturn(null); Optional<ApiKey> optApiKey = repository.findById(apiKey); Assert.assertNotNull(optApiKey); Assert.assertFalse(optApiKey.isPresent()); }
@Test public void shouldFindById_fromCache() throws TechnicalException { String apiKey = "1234-4567-7890"; ApiKey mockApiKey = Mockito.mock(ApiKey.class); Mockito.when(cache.get(apiKey)).thenReturn(new Element(apiKey, mockApiKey)); Optional<ApiKey> optApiKey = repository.findById(apiKey); Assert.assertNotNull(optApiKey); Assert.assertTrue(optApiKey.isPresent()); Assert.assertEquals(mockApiKey, optApiKey.get()); }