@Test public void loadRepoWithNEvent() { when(repo.getModificationId()).thenReturn("modif id"); IEvent first = mock(IEvent.class); IEvent second = mock(IEvent.class); IEvent third = mock(IEvent.class); // stub sorting order when(first.compareTo(second)).thenReturn(-1); when(first.compareTo(third)).thenReturn(-1); when(second.compareTo(first)).thenReturn(1); when(second.compareTo(third)).thenReturn(-1); when(third.compareTo(first)).thenReturn(1); when(third.compareTo(second)).thenReturn(1); // pass the events not sorted: List<IEvent> events = Arrays.asList(second, first, third); when(repo.getEventsSubscribedOnProperScheduler()).thenReturn(Observable.from(events)); sut.loadEvents(false, callback); // verify we received them in order List<IEvent> sortedEvents = Arrays.asList(first, second, third); verify(callback).onEventsLoadingStarted("modif id"); verify(callback).onEventsLoadingFinished(sortedEvents, "modif id"); }
@Test public void loadRepoEventsObservableThrowsError() { when(repo.getModificationId()).thenReturn("modif id"); when(repo.getEventsSubscribedOnProperScheduler()).thenReturn(Observable.error(new Throwable())); sut.loadEvents(false, callback); verify(callback).onEventsLoadingStarted("modif id"); verify(callback).onEventsLoadingCancelled("modif id"); }
@Test public void loadEmptyRepo() { when(repo.getModificationId()).thenReturn("modif id"); when(repo.getEventsSubscribedOnProperScheduler()).thenReturn(Observable.empty()); sut.loadEvents(false, callback); List<IEvent> emptyList = new ArrayList<>(); verify(callback).onEventsLoadingStarted("modif id"); verify(callback).onEventsLoadingFinished(emptyList, "modif id"); }
@Override public void onDataSetChanged(IEventRepo repo) { Timber.d("startLoadingData"); Observable<IEvent> events = repo.getEventsSubscribedOnProperScheduler(); Timber.i("archive"); events.subscribe( new EventRepoSerializerToFileDecorator(repoAccessor, new EventRepoSerializer(clock))); Timber.i("set next alarm on %s at %d", alarmGenerator, clock.hour()); alarmGenerator.generate(repo.getEventsSubscribedOnProperScheduler(), clock.now(), clock.hour()); }
@Test public void loadRepoWithOneEvent() { when(repo.getModificationId()).thenReturn("modif id"); List<IEvent> events = Arrays.asList(mock(IEvent.class)); when(repo.getEventsSubscribedOnProperScheduler()).thenReturn(Observable.from(events)); sut.loadEvents(false, callback); verify(callback).onEventsLoadingStarted("modif id"); verify(callback).onEventsLoadingFinished(events, "modif id"); }
@Test public void cancelLoadingEvents() { when(repo.getModificationId()).thenReturn("modif id"); when(repo.getEventsSubscribedOnProperScheduler()).thenReturn(Observable.never()); sut.loadEvents(false, callback); sut.cancelLoadingEvents(); verify(callback).onEventsLoadingStarted("modif id"); verify(callback).onEventsLoadingCancelled("modif id"); }
public void setComponent(BaseYearlyAppComponent component) { Timber.d( "setComponent repo: %s fileAccessor: %s", repo == null ? "null" : repo.toString(), repoAccessor == null ? "null" : repoAccessor.toString()); this.component = component; this.component.inject(this); repo.addListener(this); Timber.d( "injected component repo: %s fileAccessor: %s", repo == null ? "null" : repo.toString(), repoAccessor == null ? "null" : repoAccessor.toString()); }
@Test public void secondLoadRepoIsNotCachedForceUpdateIsTrue() { when(repo.getModificationId()).thenReturn("modif id"); List<IEvent> events = Arrays.asList(mock(IEvent.class)); when(repo.getEventsSubscribedOnProperScheduler()).thenReturn(Observable.from(events)); sut.loadEvents(false, callback); verify(callback).onEventsLoadingStarted("modif id"); verify(callback).onEventsLoadingFinished(events, "modif id"); reset(callback, repo); when(repo.getModificationId()).thenReturn("modif id"); when(repo.getEventsSubscribedOnProperScheduler()).thenReturn(Observable.from(events)); sut.loadEvents(true, callback); verify(callback).onEventsLoadingStarted("modif id"); verify(callback).onEventsLoadingFinished(events, "modif id"); verify(repo).getEventsSubscribedOnProperScheduler(); }
@Test public void secondLoadRepoIsCachedIfNotModified() { when(repo.getModificationId()).thenReturn("modif id"); List<IEvent> events = Arrays.asList(mock(IEvent.class)); when(repo.getEventsSubscribedOnProperScheduler()).thenReturn(Observable.from(events)); sut.loadEvents(false, callback); verify(callback).onEventsLoadingStarted("modif id"); verify(callback).onEventsLoadingFinished(events, "modif id"); reset(callback, repo); when(repo.getModificationId()).thenReturn("modif id"); // second loadEvents should not touch the repo to retrieve events sut.loadEvents(false, callback); verify(callback).onEventsLoadingStarted("modif id"); verify(callback).onEventsLoadingFinished(events, "modif id"); verify(repo, never()).getEventsSubscribedOnProperScheduler(); verify(repo, never()).getEvents(); }
@Test public void ongoingLoadEventsShouldBeCancelledOnNextLoadEvents() { when(repo.getModificationId()).thenReturn("first modif id"); when(repo.getEventsSubscribedOnProperScheduler()).thenReturn(Observable.never()); sut.loadEvents(false, callback); verify(callback).onEventsLoadingStarted("first modif id"); verify(callback, never()).onEventsLoadingFinished(anyList(), eq("first modif id")); reset(callback, repo); // now we do a second loadEvents (the first one should now be cancelled) List<IEvent> events = Arrays.asList(mock(IEvent.class)); when(repo.getModificationId()).thenReturn("second modif id"); when(repo.getEventsSubscribedOnProperScheduler()).thenReturn(Observable.from(events)); sut.loadEvents(true, callback); verify(callback).onEventsLoadingCancelled("first modif id"); verify(callback).onEventsLoadingStarted("second modif id"); verify(callback).onEventsLoadingFinished(events, "second modif id"); verify(repo).getEventsSubscribedOnProperScheduler(); }
@Override public void onTerminate() { Timber.d("onTerminate"); super.onTerminate(); repo.removeListener(this); }