Repositories { @NonNull public static <T> REventSource<T, T> repositoryWithInitialValue(@NonNull final T initialValue) { return RepositoryCompiler.repositoryWithInitialValue(initialValue); } private  Repositories(); @NonNull static Repository<T> repository(@NonNull final T object); @NonNull static REventSource<T, T> repositoryWithInitialValue(@NonNull final T initialValue); @NonNull static MutableRepository<T> mutableRepository(@NonNull final T object);  }
@Test public void shouldGetUpdateFromRepositoryChangedToNewValue() { final Repository<List<Integer>> repository = repositoryWithInitialValue(INITIAL_VALUE) .observe() .onUpdatesPerLoop() .thenGetFrom(listSource) .compile(); updatable.addToObservable(repository); listSource.accept(OTHER_LIST); assertThat(updatable, wasUpdated()); }
@Test public void shouldNotGetUpdateFromRepositoryChangedToSameValue() { final Repository<List<Integer>> repository = repositoryWithInitialValue(LIST) .observe() .onUpdatesPerLoop() .thenGetFrom(listSource) .compile(); updatable.addToObservable(repository); listSource.accept(LIST); assertThat(updatable, wasNotUpdated()); }
@Test public void shouldGetUpdateFromAlwaysNotifyRepositoryChangedToSameValue() { final Repository<List<Integer>> repository = repositoryWithInitialValue(LIST) .observe() .onUpdatesPerLoop() .thenGetFrom(listSource) .notifyIf(staticMerger(true)) .compile(); updatable.addToObservable(repository); assertThat(updatable, wasUpdated()); }
@Test public void shouldContainCorrectValueForLazyRepository() { final Repository<List<Integer>> repository = repositoryWithInitialValue(INITIAL_VALUE) .observe() .onUpdatesPerLoop() .goLazy() .thenGetFrom(listSource) .compile(); updatable.addToObservable(repository); assertThat(repository, has(LIST)); }
@Test public void shouldContainCorrectValueForTransformInExecutor() { final Repository<List<Integer>> repository = repositoryWithInitialValue(INITIAL_VALUE) .observe() .onUpdatesPerLoop() .goTo(new SyncExecutor()) .getFrom(listSource) .thenTransform(new AddTwoForEachFunction()) .compile(); updatable.addToObservable(repository); assertThat(repository, has(LIST_PLUS_TWO)); }
@Test public void shouldContainCorrectValueForTransformInExecutorMidFlow() { final Repository<List<Integer>> repository = repositoryWithInitialValue(INITIAL_VALUE) .observe() .onUpdatesPerLoop() .getFrom(listSource) .goTo(new SyncExecutor()) .thenTransform(new AddTwoForEachFunction()) .compile(); updatable.addToObservable(repository); assertThat(repository, has(LIST_PLUS_TWO)); }
@Test public void shouldContainCorrectValueForLazyTransformMidFlow() { final Repository<List<Integer>> repository = repositoryWithInitialValue(INITIAL_VALUE) .observe() .onUpdatesPerLoop() .getFrom(listSource) .goLazy() .thenTransform(new AddTwoForEachFunction()) .compile(); updatable.addToObservable(repository); assertThat(repository, has(LIST_PLUS_TWO)); }
@Test public void shouldBeAbleToObserveRepository() { final Repository<List<Integer>> repository = repositoryWithInitialValue(INITIAL_VALUE) .observe() .onUpdatesPerLoop() .thenGetFrom(listSource) .compile(); updatable.addToObservable(repository); }
@Test public void shouldMergeInSecondSource() { final Repository<List<Integer>> repository = repositoryWithInitialValue(INITIAL_VALUE) .observe() .onUpdatesPerLoop() .getFrom(listSource) .thenMergeIn(otherListSource, new ListMerger<Integer, Integer, Integer>()) .compile(); updatable.addToObservable(repository); assertThat(repository, has(LIST_AND_OTHER_LIST)); }
@Test public void shouldUpdateOnExplicitObservable() { final Repository<List<Integer>> repository = repositoryWithInitialValue(INITIAL_VALUE) .observe(updateDispatcher) .onUpdatesPerLoop() .thenGetFrom(mockIntegerListSupplier) .compile(); updatable.addToObservable(repository); updateDispatcher.update(); assertThat(repository, has(LIST)); verify(mockIntegerListSupplier, times(2)).get(); }
@Test public void shouldReturnDataWithGoLazyOnDemand() { final Repository<List<Integer>> repository = repositoryWithInitialValue(INITIAL_VALUE) .observe() .onUpdatesPerLoop() .goLazy() .thenGetFrom(mockIntegerListSupplier) .compile(); updatable.addToObservable(repository); verifyZeroInteractions(mockIntegerListSupplier); assertThat(repository, has(LIST)); verify(mockIntegerListSupplier).get(); }
@Test public void shouldCompileIntoNextRepository() throws Exception { final Repository<Integer> repository = repositoryWithInitialValue(INITIAL_VALUE) .observe() .onUpdatesPerLoop() .thenGetFrom(listSource) .compileIntoRepositoryWithInitialValue(INITIAL_INT_VALUE) .onUpdatesPerLoop() .thenTransform(mockIntegerListToIntValueFunction) .compile(); updatable.addToObservable(repository); verify(mockIntegerListToIntValueFunction, atLeastOnce()).apply(LIST); assertThat(repository, has(INT_VALUE)); }
@Test public void shouldSendTo() throws Exception { final Repository<List<Integer>> repository = repositoryWithInitialValue(INITIAL_VALUE) .observe() .onUpdatesPerLoop() .sendTo(mockIntegerListReceiver) .thenSkip() .compile(); updatable.addToObservable(repository); verify(mockIntegerListReceiver).accept(INITIAL_VALUE); assertThat(updatable, wasNotUpdated()); assertThat(repository, has(INITIAL_VALUE)); }
@Test public void shouldBindWith() throws Exception { final Repository<List<Integer>> repository = repositoryWithInitialValue(INITIAL_VALUE) .observe() .onUpdatesPerLoop() .bindWith(mockStringSupplier, mockIntegerListStringBinder) .thenSkip() .compile(); updatable.addToObservable(repository); verify(mockIntegerListStringBinder).bind(INITIAL_VALUE, STRING_VALUE); assertThat(updatable, wasNotUpdated()); assertThat(repository, has(INITIAL_VALUE)); }
@Test public void shouldGetSuccessfulValueFromThenAttemptGetFrom() { final Repository<String> repository = repositoryWithInitialValue(INITIAL_STRING_VALUE) .observe() .onUpdatesPerLoop() .thenAttemptGetFrom(mockResultStringSupplier).orSkip() .compile(); updatable.addToObservable(repository); assertThat(updatable, wasUpdated()); assertThat(repository, has(STRING_VALUE)); }
@Test public void shouldNotUpdateForSkippedFailedValueFromThenAttemptGetFrom() { final Repository<String> repository = repositoryWithInitialValue(INITIAL_STRING_VALUE) .observe() .onUpdatesPerLoop() .thenAttemptGetFrom(mockFailedResultStringSupplier).orSkip() .compile(); updatable.addToObservable(repository); assertThat(updatable, wasNotUpdated()); assertThat(repository, has(INITIAL_STRING_VALUE)); }
@Test public void shouldGetSuccessfulValueFromThenAttemptTransform() { final Repository<String> repository = repositoryWithInitialValue(INITIAL_STRING_VALUE) .observe() .onUpdatesPerLoop() .thenAttemptTransform(mockResultStringFunction).orSkip() .compile(); updatable.addToObservable(repository); assertThat(updatable, wasUpdated()); assertThat(repository, has(STRING_VALUE)); }
@Test public void shouldNotUpdateForSkippedFailedValueFromThenAttemptTransform() { final Repository<String> repository = repositoryWithInitialValue(INITIAL_STRING_VALUE) .observe() .onUpdatesPerLoop() .thenAttemptTransform(mockFailedResultStringFunction).orSkip() .compile(); updatable.addToObservable(repository); assertThat(updatable, wasNotUpdated()); assertThat(repository, has(INITIAL_STRING_VALUE)); }
@Test public void shouldGetSuccessfulValueFromThenAttemptMergeIn() { final Repository<String> repository = repositoryWithInitialValue(INITIAL_STRING_VALUE) .observe() .onUpdatesPerLoop() .thenAttemptMergeIn(mockStringSupplier, mockResultStringMerger).orSkip() .compile(); updatable.addToObservable(repository); assertThat(updatable, wasUpdated()); assertThat(repository, has(STRING_VALUE)); }
@Test public void shouldNotUpdateForSkippedFailedValueFromThenAttemptMergeIn() { final Repository<String> repository = repositoryWithInitialValue(INITIAL_STRING_VALUE) .observe() .onUpdatesPerLoop() .thenAttemptMergeIn(mockStringSupplier, mockFailedResultStringMerger).orSkip() .compile(); updatable.addToObservable(repository); assertThat(updatable, wasNotUpdated()); assertThat(repository, has(INITIAL_STRING_VALUE)); }