package com.tobi.movies.posterdetails;

import android.support.annotation.NonNull;

import com.tobi.movies.Converter;

import org.junit.Before;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import rx.Observable;
import rx.observers.TestSubscriber;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class MovieDetailsRepositoryTest {

    private MovieDetailsApiDatasource apiDatasource = mock(MovieDetailsApiDatasource.class);
    private Converter<ApiMovieDetails, MovieDetails> conveter = mock(ApiMovieDetailsConverter.class);

    private final MovieDetailsRepository repository = new MovieDetailsRepository(
            apiDatasource,
            conveter
    );

    @Before
    public void setUp() throws Exception {
        doAnswer(new Answer() {
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                ApiMovieDetails apiMovieDetails = ((ApiMovieDetails) invocation.getArguments()[0]);

                return MovieDetails.builder().
                        movieId(apiMovieDetails.movieId).
                        originalTitle(apiMovieDetails.originalTitle).
                        posterPath(apiMovieDetails.posterPath).
                        overview(apiMovieDetails.overview).
                        releaseDate(apiMovieDetails.releaseDate).build();
            }
        }).when(conveter).convert(any(ApiMovieDetails.class));
    }

    @Test
    public void shouldReturnMovieDetailsForId() throws Exception {
        long movieId = 1L;
        String imagePath = "path";
        String overview = "overview";
        String title = "title";
        String releaseDate = "date";
        ApiMovieDetails apiMovieDetails = createApiMovieDetails(movieId, imagePath, overview, title, releaseDate);
        when(apiDatasource.getMovieDetails(movieId)).thenReturn(Observable.just(apiMovieDetails));

        Observable<MovieDetails> detailsObservable = repository.getMovieDetails(movieId);

        TestSubscriber<MovieDetails> testSubscriber = new TestSubscriber<>();
        detailsObservable.subscribe(testSubscriber);
        MovieDetails expectedMovieDetails = MovieDetails.builder().
                movieId(movieId).
                posterPath(imagePath).
                originalTitle(title).
                releaseDate(releaseDate).
                overview(overview).
                build();
        testSubscriber.assertValue(expectedMovieDetails);
    }

    @NonNull
    private ApiMovieDetails createApiMovieDetails(long movieId, String imagePath, String overview, String title, String releaseDate) {
        ApiMovieDetails apiMovieDetails = new ApiMovieDetails();
        apiMovieDetails.movieId = movieId;
        apiMovieDetails.posterPath = imagePath;
        apiMovieDetails.overview = overview;
        apiMovieDetails.originalTitle = title;
        apiMovieDetails.releaseDate = releaseDate;
        return apiMovieDetails;
    }
}

@@ -133,121 +133,12 @@
 nit.
-Before;%0Aimport org.junit.Test;%0Aimport org.mockito.invocation.InvocationOnMock;%0Aimport org.mockito.stubbing.Answer
+Test
 ;%0A%0Ai
@@ -199,92 +199,8 @@
 r;%0A%0A
-import static org.mockito.Matchers.any;%0Aimport static org.mockito.Mockito.doAnswer;%0A
 impo
@@ -331,16 +331,22 @@
 private 
+final 
 MovieDet
@@ -430,16 +430,22 @@
 private 
+final 
 Converte
@@ -488,21 +488,20 @@
 veter = 
-mock(
+new 
 ApiMovie
@@ -516,22 +516,17 @@
 onverter
-.class
+(
 );%0A%0A    
@@ -602,29 +602,16 @@
 ository(
-%0A            
 apiDatas
@@ -620,784 +620,19 @@
 rce,
-%0A            conveter%0A    );%0A%0A    @Before%0A    public void setUp() throws Exception %7B%0A        doAnswer(new Answer() %7B%0A            @Override%0A            public Object answer(InvocationOnMock invocation) throws Throwable %7B%0A                ApiMovieDetails apiMovieDetails = ((ApiMovieDetails) invocation.getArguments()%5B0%5D);%0A%0A                return MovieDetails.builder().%0A                        movieId(apiMovieDetails.movieId).%0A                        originalTitle(apiMovieDetails.originalTitle).%0A                        posterPath(apiMovieDetails.posterPath).%0A                        overview(apiMovieDetails.overview).%0A                        releaseDate(apiMovieDetails.releaseDate).build();%0A            %7D%0A        %7D).when(conveter).convert(any(ApiMovieDetails.class));%0A    %7D
+ conveter);
 %0A%0A  
@@ -761,16 +761,17 @@
 Path = %22
+/
 path%22;%0A 
@@ -1442,25 +1442,94 @@
 terPath(
-imagePath
+%22http://image.tmdb.org/t/p/w500/%22 + imagePath.substring(1, imagePath.length())
 ).%0A     

Use real converter instead of mock