package fr.cph.chicago.data;

import android.content.Context;

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import fr.cph.chicago.entity.BusArrival;

import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class FavoritesDataTest {

    private static final String ROUTE_ID = "4";

    @Mock
    private Preferences preferences;

    @Mock
    private Context context;

    private FavoritesData favoritesData;

    @Before
    public void setUp() {
        favoritesData = FavoritesData.INSTANCE;
        favoritesData.setBusArrivals(createBusArrivals());
        favoritesData.setBusFavorites(createListFavorites());
        favoritesData.setPreferences(preferences);

        when(preferences.getBusStopNameMapping(context, "2893")).thenReturn("111th Street & Vernon");
    }

    @Test
    public void testGetBusArrivalsMappedEmptyNoFavorites() {
        favoritesData.setBusArrivals(new ArrayList<>());
        favoritesData.setBusFavorites(new ArrayList<>());

        Map<String, Map<String, List<BusArrival>>> actual = favoritesData.getBusArrivalsMapped(ROUTE_ID, context);
        assertNotNull(actual);
        assertThat(actual.size(), is(0));
    }

    @Test
    public void testGetBusArrivalsMappedEmptyFavorites() {
        favoritesData.setBusArrivals(new ArrayList<>());

        Map<String, Map<String, List<BusArrival>>> actual = favoritesData.getBusArrivalsMapped(ROUTE_ID, context);
        assertNotNull(actual);
        assertThat(actual.size(), is(1));
    }

    @Test
    public void testGetBusArrivalsMappedWithResult() {
        Map<String, Map<String, List<BusArrival>>> actual = favoritesData.getBusArrivalsMapped(ROUTE_ID, context);
        assertNotNull(actual);
        assertThat(actual.size(), is(1));
        assertThat(actual, hasKey("111th Street & Vernon"));
    }


    private List<BusArrival> createBusArrivals() {
        final BusArrival busArrival = BusArrival.builder()
            .timeStamp(new Date())
            .stopName("111th Street & Vernon")
            .stopId(2893)
            .busId(1293)
            .routeId(ROUTE_ID)
            .routeDirection("Northbound")
            .busDestination("Illinois Center")
            .predictionTime(new Date())
            .isDly(false)
            .build();
        final List<BusArrival> busArrivals = new ArrayList<>();
        busArrivals.add(busArrival);
        return busArrivals;
    }

    //4_2893_Northbound

    private List<String> createListFavorites() {
        return Arrays.asList("4_2893_Northbound");
    }
}

@@ -231,41 +231,18 @@
 til.
-ArrayList;%0Aimport java.util.Array
+Collection
 s;%0Ai
@@ -553,16 +553,19 @@
 ito.
+BDD
 Mockito.
 when
@@ -560,18 +560,19 @@
 Mockito.
-wh
+giv
 en;%0A%0A@Ru
@@ -850,24 +850,41 @@
 d setUp() %7B%0A
+        // Given%0A
         favo
@@ -1091,25 +1091,24 @@
 ences);%0A
-%0A
         
 when(pre
@@ -1103,10 +1103,11 @@
     
-wh
+giv
 en(p
@@ -1161,12 +1161,12 @@
 %22)).
-then
+will
 Retu
@@ -1264,32 +1264,49 @@
 NoFavorites() %7B%0A
+        // Given%0A
         favorite
@@ -1322,39 +1322,45 @@
 BusArrivals(
-new Arra
+Collections.empt
 yList
-%3C%3E
 ());%0A       
@@ -1390,37 +1390,59 @@
 vorites(
-new Arra
+Collections.empt
 yList
-%3C%3E
 ());%0A%0A
+        // When%0A
         
@@ -1539,32 +1539,49 @@
 TE_ID, context);
+%0A%0A        // Then
 %0A        assertN
@@ -1706,32 +1706,49 @@
 tyFavorites() %7B%0A
+        // Given%0A
         favorite
@@ -1772,29 +1772,51 @@
 als(
-new Arra
+Collections.empt
 yList
-%3C%3E
 ());%0A%0A
+        // When%0A
     
@@ -1917,32 +1917,49 @@
 TE_ID, context);
+%0A%0A        // Then
 %0A        assertN
@@ -2084,24 +2084,40 @@
 hResult() %7B%0A
+        // When%0A
         Map%3C
@@ -2218,16 +2218,33 @@
 ontext);
+%0A%0A        // Then
 %0A       
@@ -2377,17 +2377,16 @@
 %0A    %7D%0A%0A
-%0A
     priv
@@ -2840,116 +2840,41 @@
     
-final List%3CBusArrival%3E busArrivals = new ArrayList%3C%3E();%0A        busArrivals.add(busArrival);%0A        return 
+return Collections.singletonList(
 busA
@@ -2879,17 +2879,17 @@
 sArrival
-s
+)
 ;%0A    %7D%0A
@@ -2893,33 +2893,8 @@
  %7D%0A%0A
-    //4_2893_Northbound%0A%0A
     
@@ -2957,17 +2957,29 @@
 urn 
-Arrays.as
+Collections.singleton
 List

Update unit testing