RosieRepository implements ReadableDataSource<K, V>, WriteableDataSource<K, V> { @Override public V getByKey(K key) throws Exception { return getByKey(key, ReadPolicy.READ_ALL); }  @Override V getByKey(K key); V getByKey(K key, ReadPolicy policy); @Override Collection<V> getAll(); Collection<V> getAll(ReadPolicy policy); @Override V addOrUpdate(V value); V addOrUpdate(V value, WritePolicy policy); @Override Collection<V> addOrUpdateAll(Collection<V> values); Collection<V> addOrUpdateAll(Collection<V> values, WritePolicy policy); @Override void deleteByKey(K key); @Override void deleteAll();  }
@Test public void shouldLoadItemFromReadableDataSourceIfReadPolicyForcesOnlyReadable() throws Exception { givenCacheDataSourceReturnsValidValueWithKey(ANY_KEY); givenReadableDataSourceReturnsValidValueWithKey(ANY_KEY); RosieRepository<AnyRepositoryKey, AnyRepositoryValue> repository = givenAReadableAndCacheRepository(); repository.getByKey(ANY_KEY, ReadPolicy.READABLE_ONLY); verify(cacheDataSource, never()).getByKey(ANY_KEY); verify(readableDataSource).getByKey(ANY_KEY); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNullKeys() throws Exception { RosieRepository<?, ?> repository = givenAReadableAndCacheRepository(); repository.getByKey(null); }
@Test public void shouldReturnValueByKeyFromCacheDataSource() throws Exception { givenReadableDataSourceReturnsNull(); AnyRepositoryValue cacheValue = givenCacheDataSourceReturnsValidValueWithKey(ANY_KEY); RosieRepository<AnyRepositoryKey, AnyRepositoryValue> repository = givenAReadableAndCacheRepository(); AnyRepositoryValue value = repository.getByKey(ANY_KEY); Assert.assertEquals(cacheValue, value); }
@Test public void shouldReturnValueFromReadableDataSourceIfCacheDataSourceValueIsNull() throws Exception { givenCacheDataSourceReturnsNull(); AnyRepositoryValue readableValue = givenReadableDataSourceReturnsValidValueWithKey(ANY_KEY); RosieRepository<AnyRepositoryKey, AnyRepositoryValue> repository = givenAReadableAndCacheRepository(); AnyRepositoryValue value = repository.getByKey(ANY_KEY); Assert.assertEquals(readableValue, value); }
@Test public void shouldReturnItemFromReadableDataSourceIfCacheDataSourceValueIsNotValid() throws Exception { givenCacheDataSourceReturnsNonValidValueWithKey(ANY_KEY); AnyRepositoryValue readableValue = givenReadableDataSourceReturnsValidValueWithKey(ANY_KEY); RosieRepository<AnyRepositoryKey, AnyRepositoryValue> repository = givenAReadableAndCacheRepository(); AnyRepositoryValue value = repository.getByKey(ANY_KEY); Assert.assertEquals(readableValue, value); }