package com.garymcgowan.moviepedia;

import com.garymcgowan.moviepedia.model.Movie;
import com.garymcgowan.moviepedia.model.MovieRepository;
import com.garymcgowan.moviepedia.view.MovieListActivityPresenter;
import com.garymcgowan.moviepedia.view.MovieListActivityView;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.schedulers.Schedulers;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * Created by garymcgowan on 08/05/2017.
 */
public class MovieListActivityPresenterTest {

    @Rule public MockitoRule mockitoRule = MockitoJUnit.rule();

    @Mock MovieListActivityView view;
    @Mock MovieRepository movieRepository;
    MovieListActivityPresenter presenter;

    private final List<Movie> THREE_MOVIES = Arrays.asList(new Movie(), new Movie(), new Movie());


    @Before
    public void setUp() {
        presenter = new MovieListActivityPresenter(view, movieRepository, Schedulers.trampoline());
        RxJavaPlugins.setIoSchedulerHandler(scheduler -> Schedulers.trampoline());
    }

    @After
    public void cleanUp() {
        RxJavaPlugins.reset();
    }

    @Test
    public void shouldPassMoviesToView() {
        //given

        //when
        when(movieRepository.getMovieSearch("123")).thenReturn(Flowable.just(THREE_MOVIES));
//        presenter.loadMovies("");
        presenter.setSearchTermObservable(Flowable.just("123"));

        //then
        verify(view).displayMovies(THREE_MOVIES);
    }


    @Test
    public void shouldPassMoviesToViewNull() {

        //given
        //Flowable<List<Movie>> flowableMovies = null;

        //when
        when(movieRepository.getMovieSearch("123")).thenReturn(null);
//        presenter.loadMovies("");
        presenter.setSearchTermObservable(Flowable.just("123"));


        //then
        verify(view).displayError(Mockito.anyString());

    }

    @Test
    public void shouldPassMoviesToViewEmpty() {
        //given
        //Flowable<List<Movie>> flowableMovies = Flowable.just(Collections.EMPTY_LIST);

        //when
        when(movieRepository.getMovieSearch("123")).thenReturn(Flowable.just(Collections.EMPTY_LIST));
        presenter.setSearchTermObservable(Flowable.just("123"));

//        presenter.loadMovies("");

        //then
        verify(view).displayMovies(Collections.EMPTY_LIST);
    }

    @Test
    public void shouldHandleException() {
        when(movieRepository.getMovieSearch("123")).thenThrow(new RuntimeException("Something when wrong"));

        presenter.setSearchTermObservable(Flowable.just("123"));

        verify(view).displayError(Mockito.anyString());
    }


    @Test
    public void multipleRequests() {
        when(movieRepository.getMovieSearch(Mockito.anyString())).thenReturn(Flowable.just(THREE_MOVIES));

        presenter.setSearchTermObservable(Flowable.just("t", "ta", "tak", "take", "taken"));


        verify(view, times(5)).displayMovies(THREE_MOVIES);
    }
}
@@ -1558,39 +1558,8 @@
 ) %7B%0A
-        //given%0A%0A        //when
 %0A   
@@ -1652,43 +1652,8 @@
 ));%0A
-//        presenter.loadMovies(%22%22);
 %0A   
@@ -1711,39 +1711,24 @@
 t(%22123%22));%0A%0A
-        //then%0A
         veri
@@ -1833,96 +1833,8 @@
 () %7B
-%0A%0A        //given%0A        //Flowable%3CList%3CMovie%3E%3E flowableMovies = null;%0A%0A        //when
 %0A   
@@ -1904,43 +1904,8 @@
 l);%0A
-//        presenter.loadMovies(%22%22);
 %0A   
@@ -1967,32 +1967,16 @@
 23%22));%0A%0A
-%0A        //then%0A
         
@@ -2019,25 +2019,24 @@
 yString());%0A
-%0A
     %7D%0A%0A    @
@@ -2092,128 +2092,8 @@
 ) %7B%0A
-        //given%0A        //Flowable%3CList%3CMovie%3E%3E flowableMovies = Flowable.just(Collections.EMPTY_LIST);%0A%0A        //when%0A
     
@@ -2187,24 +2187,25 @@
 PTY_LIST));%0A
+%0A
         pres
@@ -2262,60 +2262,8 @@
 );%0A%0A
-//        presenter.loadMovies(%22%22);%0A%0A        //then%0A
     
@@ -2862,25 +2862,24 @@
 %22taken%22));%0A%0A
-%0A
         veri

test cleanup