package de.philipphager.disclosure.feature.sync.api.usecases;

import de.philipphager.disclosure.api.DisclosureApi;
import de.philipphager.disclosure.database.library.model.Library;
import de.philipphager.disclosure.database.mocks.MockLibrary;
import de.philipphager.disclosure.service.LibraryService;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.threeten.bp.OffsetDateTime;
import org.threeten.bp.ZoneOffset;
import rx.Observable;
import rx.observers.TestSubscriber;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class) public class SyncLibrariesShould {
  @Mock protected DisclosureApi disclosureApi;
  @Mock protected LibraryService libraryService;
  @InjectMocks protected SyncLibraries syncLibraries;

  @Test @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
  public void fetchFeaturesFromApiAndSaveThem() {
    OffsetDateTime lastUpdated = OffsetDateTime.of(2016, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
    when(libraryService.lastUpdated()).thenReturn(Observable.just(lastUpdated));

    List<Library> newLibraries = Collections.singletonList(MockLibrary.TEST);
    when(disclosureApi.allLibraries(any())).thenReturn(Observable.just(newLibraries));

    TestSubscriber<List<Library>> testSubscriber = new TestSubscriber<>();
    syncLibraries.run().subscribe(testSubscriber);

    verify(disclosureApi).allLibraries(lastUpdated);
    verify(libraryService).insertOrUpdate(newLibraries);

    testSubscriber.assertReceivedOnNext(Collections.singletonList(newLibraries));
    testSubscriber.assertNoErrors();
    testSubscriber.assertCompleted();
  }

  @Test @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
  public void callOnErrorIfApiReturnsAnError() {
    OffsetDateTime lastUpdated = OffsetDateTime.of(2016, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
    when(libraryService.lastUpdated()).thenReturn(Observable.just(lastUpdated));

    Throwable apiError = new Throwable();
    when(disclosureApi.allLibraries(any())).thenReturn(Observable.error(apiError));

    TestSubscriber<List<Library>> testSubscriber = new TestSubscriber<>();
    syncLibraries.run().subscribe(testSubscriber);

    testSubscriber.assertError(apiError);
  }
}

@@ -681,16 +681,59 @@
 rs.any;%0A
+import static org.mockito.Matchers.anyInt;%0A
 import s
@@ -766,16 +766,16 @@
 verify;%0A
-
 import s
@@ -1426,32 +1426,52 @@
 lLibraries(any()
+, anyInt(), anyInt()
 )).thenReturn(Ob
@@ -1678,16 +1678,22 @@
 tUpdated
+, 0, 0
 );%0A    v
@@ -2231,24 +2231,24 @@
 hrowable();%0A
-
     when(dis
@@ -2276,16 +2276,36 @@
 es(any()
+, anyInt(), anyInt()
 )).thenR

Fix api tests