/*
 * Copyright 2015 Adam Speakman
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package speakman.whatsshakingnz.network;

import android.test.AndroidTestCase;

import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.mockito.ArgumentCaptor;

import rx.Observable;
import speakman.whatsshakingnz.model.EarthquakeStore;
import speakman.whatsshakingnz.network.geonet.GeonetResponse;
import speakman.whatsshakingnz.network.geonet.GeonetService;

import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.notNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * Created by Adam on 15-06-07.
 */
public class RequestManagerTest extends AndroidTestCase {

    public void testRequestLastNDaysWhenNoMostRecentEventDateIsAvailable() {
        GeonetService service = mock(GeonetService.class);
        EarthquakeStore store = new EarthquakeStore();
        RequestTimeStore timeStore = mock(RequestTimeStore.class);
        RequestManager mgr = new RequestManager(store, service, timeStore);

        when(timeStore.getMostRecentRequestTime()).thenReturn(null);
        when(service.getEarthquakesSince(notNull(DateTime.class), eq(RequestManager.MAX_EVENTS_PER_REQUEST)))
                .thenReturn(Observable.<GeonetResponse>empty());

        mgr.retrieveNewEarthquakes();

        ArgumentCaptor<DateTime> argumentCaptor = ArgumentCaptor.forClass(DateTime.class);
        verify(service).getEarthquakesSince(argumentCaptor.capture(), eq(RequestManager.MAX_EVENTS_PER_REQUEST));
        LocalDate requestedDate = argumentCaptor.getValue().toLocalDate();
        LocalDate today = DateTime.now().toLocalDate();
        // We want to ensure that the request was for DAYS_BEFORE_TODAY days ago.
        assertEquals(RequestManager.DAYS_BEFORE_TODAY, Days.daysBetween(requestedDate, today).getDays());
    }

    public void testRequestOnlyEventsSinceLastEventDate() {

    }

    public void testAllEventsAreProvidedToStore() {

    }

    public void testSpecificEventIsRetrieved() { // We can request updated/detailed info on a specific event, maybe?

    }

    public void testAllPagedEventsAreRetrieved() {

    }

    public void testLastEventTimeIsStored() {

    }

    public void testLastEventTimeIsNotUpdatedWhenFirstPageFails() {

    }

    public void testLastEventTimeIsStoredWhenASubsequentPageFails() {

    }

}

@@ -2508,24 +2508,747 @@
 entDate() %7B%0A
+        GeonetService service = mock(GeonetService.class);%0A        EarthquakeStore store = mock(EarthquakeStore.class);%0A        RequestTimeStore timeStore = mock(RequestTimeStore.class);%0A        RequestManager mgr = new RequestManager(store, service, timeStore);%0A%0A        DateTime mostRecentRequestTime = new DateTime();%0A%0A        when(timeStore.getMostRecentRequestTime()).thenReturn(mostRecentRequestTime);%0A        when(service.getEarthquakesSince(mostRecentRequestTime, RequestManager.MAX_EVENTS_PER_REQUEST))%0A                .thenReturn(Observable.%3CGeonetResponse%3Eempty());%0A%0A        mgr.retrieveNewEarthquakes();%0A%0A        verify(service).getEarthquakesSince(mostRecentRequestTime, RequestManager.MAX_EVENTS_PER_REQUEST);
 %0A    %7D%0A%0A    

Add test for requesting only events since last event date