RosieRepository implements ReadableDataSource<K, V>, WriteableDataSource<K, V> { @Override public V addOrUpdate(V value) throws Exception { return addOrUpdate(value, 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(expected = IllegalArgumentException.class) public void shouldNotAddOrUpdateNullItems() throws Exception { RosieRepository<?, AnyRepositoryValue> repository = givenAWriteableAndCacheRepository(); repository.addOrUpdate(null); }
@Test public void shouldAddOrUpdateItemToWriteableDataSource() throws Exception { givenWriteableDataSourceWritesValue(ANY_VALUE); RosieRepository<?, AnyRepositoryValue> repository = givenAWriteableAndCacheRepository(); repository.addOrUpdate(ANY_VALUE); verify(writeableDataSource).addOrUpdate(ANY_VALUE); }
@Test public void shouldPopulateCacheDataSourceWithWriteableDataSourceResult() throws Exception { AnyRepositoryValue writeableValue = givenWriteableDataSourceWritesValue(ANY_VALUE); RosieRepository<?, AnyRepositoryValue> repository = givenAWriteableAndCacheRepository(); repository.addOrUpdate(ANY_VALUE); verify(cacheDataSource).addOrUpdate(writeableValue); }
@Test public void shouldNotPopulateCacheDataSourceIfResultIsNotSuccessful() throws Exception { givenWriteableDataSourceDoesNotWriteValue(ANY_VALUE); RosieRepository<?, AnyRepositoryValue> repository = givenAWriteableAndCacheRepository(); repository.addOrUpdate(ANY_VALUE); verify(cacheDataSource, never()).addOrUpdate(any(AnyRepositoryValue.class)); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNullValues() throws Exception { RosieRepository<?, ?> repository = givenAWriteableAndCacheRepository(); repository.addOrUpdate(null); }