RosieRepository implements ReadableDataSource<K, V>, WriteableDataSource<K, V> { @Override public Collection<V> getAll() throws Exception { return getAll(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 shouldReturnNullIfThereAreNoDataSourcesWithData() throws Exception { givenCacheDataSourceReturnsNull(); givenReadableDataSourceReturnsNull(); RosieRepository<?, ?> repository = givenAReadableAndCacheRepository(); Collection<?> values = repository.getAll(); assertNull(values); }
@Test public void shouldReturnDataFromCacheDataSourceIfDataIsValid() throws Exception { Collection<AnyRepositoryValue> cacheValues = givenCacheDataSourceReturnsValidValues(); givenReadableDataSourceReturnsNull(); RosieRepository<?, AnyRepositoryValue> repository = givenAReadableAndCacheRepository(); Collection<AnyRepositoryValue> values = repository.getAll(); assertEquals(cacheValues, values); }
@Test public void shouldReturnDataFromReadableDataSourceIfCacheDataSourceReturnsNullOnGetAll() throws Exception { givenCacheDataSourceReturnsNull(); Collection<AnyRepositoryValue> readableValues = givenReadableDataSourceReturnsValidValues(); RosieRepository<?, AnyRepositoryValue> repository = givenAReadableAndCacheRepository(); Collection<AnyRepositoryValue> values = repository.getAll(); Assert.assertEquals(readableValues, values); }
@Test public void shouldReturnDataFromReadableDataSourceIfTheCacheDataSourceReturnsNoValidData() throws Exception { givenCacheDataSourceReturnsNonValidValues(); Collection<AnyRepositoryValue> readableValues = givenReadableDataSourceReturnsValidValues(); RosieRepository<?, AnyRepositoryValue> repository = givenAReadableAndCacheRepository(); Collection<AnyRepositoryValue> values = repository.getAll(); Assert.assertEquals(readableValues, values); }
@Test(expected = Exception.class) public void shouldPropagateExceptionsThrownByAnyDataSource() throws Exception { givenReadableDataSourceThrowsException(); RosieRepository<?, ?> repository = givenARepository(EnumSet.of(DataSource.READABLE)); repository.getAll(); }
@Test public void shouldGetDataFromReadableDataSourceIfReadPolicyForcesOnlyReadable() throws Exception { givenCacheDataSourceReturnsValidValues(); givenReadableDataSourceReturnsValidValues(); RosieRepository<?, AnyRepositoryValue> repository = givenAReadableAndCacheRepository(); repository.getAll(ReadPolicy.READABLE_ONLY); verify(cacheDataSource, never()).getAll(); verify(readableDataSource).getAll(); }