RosieRepository implements ReadableDataSource<K, V>, WriteableDataSource<K, V> { @Override public Collection<V> addOrUpdateAll(Collection<V> values) throws Exception { return addOrUpdateAll(values, WritePolicy.WRITE_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 shouldAddItemsToWriteableDataSource() throws Exception { Collection<AnyRepositoryValue> values = getSomeValues(); givenWriteableDataSourceWritesValues(values); RosieRepository<?, AnyRepositoryValue> repository = givenAWriteableAndCacheRepository(); repository.addOrUpdateAll(values); verify(writeableDataSource).addOrUpdateAll(values); }
@Test public void shouldPopulateCacheDataSourceWithWriteableDataSourceResults() throws Exception { Collection<AnyRepositoryValue> values = getSomeValues(); Collection<AnyRepositoryValue> writeableValues = givenWriteableDataSourceWritesValues(values); RosieRepository<?, AnyRepositoryValue> repository = givenAWriteableAndCacheRepository(); repository.addOrUpdateAll(values); verify(cacheDataSource).addOrUpdateAll(writeableValues); }
@Test public void shouldNotPopulateCacheDataSourceIfWriteableDataSourceResultIsNotSuccessful() throws Exception { Collection<AnyRepositoryValue> values = getSomeValues(); givenWriteableDataSourceDoesNotWriteValues(values); RosieRepository<?, AnyRepositoryValue> repository = givenAWriteableAndCacheRepository(); repository.addOrUpdateAll(values); verify(cacheDataSource, never()).addOrUpdateAll(values); }