package net.avantica.xinef.dapp.data.net;

import android.support.annotation.NonNull;

import net.avantica.xinef.dapp.data.entity.AddressComponent;
import net.avantica.xinef.dapp.data.entity.ReverseGeocodingEntity;
import net.avantica.xinef.dapp.data.entity.ReverseGeocodingResult;
import net.avantica.xinef.dapp.data.entity.mapper.ReverseGeocodingEntityJsonMapper;
import net.avantica.xinef.dapp.data.exception.NetworkConnectionException;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.net.MalformedURLException;

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

import static java.util.Collections.singletonList;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;

@RunWith(MockitoJUnitRunner.class)
public class GeolocationApiImplTest {

    private GeolocationApiImpl geolocationApi;

    @Mock
    private ReverseGeocodingEntityJsonMapper jsonMapper;

    @Before
    public void setUp() throws Exception {
        geolocationApi = spy(new GeolocationApiImpl(null, jsonMapper));
    }

    @Test
    public void shouldNotifyAnErrorWhenThereIsNoInternetConnection() {
        doReturn(false).when(geolocationApi).isThereInternetConnection();
        TestSubscriber<String> testSubscriber = new TestSubscriber<>();
        geolocationApi.getDepartmentFromlatitudeLongitude(14, 15)
            .subscribe(testSubscriber);

        testSubscriber.assertError(NetworkConnectionException.class);
    }

    @Test
    public void shouldReturnDepartmentWhenRetrievingSuccessfullyFromApi() throws MalformedURLException {
        doReturn(true).when(geolocationApi).isThereInternetConnection();

        ReverseGeocodingEntity entity = preparateReverseGeocodingEntity();

        given(jsonMapper.transformReverseGeocodingEntity(anyString())).willReturn(entity);
        given(geolocationApi.getDepartmentFromLatitudeLongitudeFromApi(14, 15)).willReturn("{}");

        TestSubscriber<String> testSubscriber = new TestSubscriber<>();
        geolocationApi.getDepartmentFromlatitudeLongitude(14, 15).subscribe(testSubscriber);

        testSubscriber.assertReceivedOnNext(singletonList("LAMBAYEQUE"));
    }

    @NonNull
    private ReverseGeocodingEntity preparateReverseGeocodingEntity() {
        AddressComponent addressComponent = new AddressComponent();
        addressComponent.setLongName("Lambayeque");
        addressComponent.setShortName("Lambayeque");
        addressComponent.setTypes(singletonList("administrative_area_level_1"));

        ReverseGeocodingResult reverseGeocodingResult = new ReverseGeocodingResult();
        reverseGeocodingResult.setAddressComponents(singletonList(addressComponent));

        ReverseGeocodingEntity entity = new ReverseGeocodingEntity();
        entity.setResults(singletonList(reverseGeocodingResult));
        entity.setStatus("Ok");
        return entity;
    }
}
@@ -631,30 +631,59 @@
 on;%0A
-%0A
 import 
-rx.Observable;
+java.util.ArrayList;%0Aimport java.util.List;%0A
 %0Aimp
@@ -1570,16 +1570,20 @@
         
+    
 .subscri
@@ -3090,10 +3090,820 @@
 y;%0A    %7D
+%0A%0A    @Test%0A    public void shouldNotifyAnErrorWhenListOfResultFromReverseGeocodingEntityIsEmpty() throws MalformedURLException %7B%0A        doReturn(true).when(geolocationApi).isThereInternetConnection();%0A%0A        ReverseGeocodingEntity entity = new ReverseGeocodingEntity();%0A        List%3CReverseGeocodingResult%3E results = new ArrayList%3C%3E();%0A        entity.setResults(results);%0A%0A        given(jsonMapper.transformReverseGeocodingEntity(anyString())).willReturn(entity);%0A        given(geolocationApi.getDepartmentFromLatitudeLongitudeFromApi(14, 15)).willReturn(%22%7B%7D%22);%0A%0A        TestSubscriber%3CString%3E testSubscriber = new TestSubscriber%3C%3E();%0A        geolocationApi.getDepartmentFromlatitudeLongitude(14, 15).subscribe(testSubscriber);%0A%0A        testSubscriber.assertError(NetworkConnectionException.class);%0A%0A%0A    %7D
 %0A%7D

Unit test: should notify an error when list of result from reverse geocoding entity is empty