package io.github.hidroh.materialistic.data;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;
import dagger.Module;
import dagger.ObjectGraph;
import dagger.Provides;
import io.github.hidroh.materialistic.test.ParameterizedTestRunner;
import rx.Observable;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(ParameterizedTestRunner.class)
public class AlgoliaPopularClientTest {
private final String range;
@Inject RestServiceFactory factory;
private ItemManager hackerNewsClient = mock(ItemManager.class);
private AlgoliaPopularClient client;
public AlgoliaPopularClientTest(String range) {
this.range = range;
}
@ParameterizedTestRunner.Parameters
public static List<Object[]> provideParameters() {
return Arrays.asList(
new Object[]{AlgoliaPopularClient.LAST_24H},
new Object[]{AlgoliaPopularClient.PAST_WEEK},
new Object[]{AlgoliaPopularClient.PAST_MONTH},
new Object[]{AlgoliaPopularClient.PAST_YEAR}
);
}
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
ObjectGraph.create(new TestModule()).inject(this);
reset(TestRestServiceFactory.algoliaRestService);
client = new AlgoliaPopularClient(RuntimeEnvironment.application, factory);
client.mHackerNewsClient = hackerNewsClient;
}
@Test
public void testGetStories() {
when(TestRestServiceFactory.algoliaRestService.searchByMinTimestamp(any()))
.thenReturn(Observable.error(new IOException()));
client.getStories(range, ItemManager.MODE_DEFAULT, mock(ResponseListener.class));
verify(TestRestServiceFactory.algoliaRestService)
.searchByMinTimestamp(contains("created_at_i>"));
}
@Module(
injects = AlgoliaPopularClientTest.class,
overrides = true
)
static class TestModule {
@Provides
@Singleton
public RestServiceFactory provideRestServiceFactory() {
return new TestRestServiceFactory();
}
}
}