package com.ricardo.casarez.flighsearch.data.repository.datasource.flights;

import android.content.Context;

import com.ricardo.casarez.flighsearch.data.cache.FlightsCache;
import com.ricardo.casarez.flighsearch.data.net.QpxExpressApi;
import com.ricardo.casarez.flighsearch.data.repository.datasource.flights.CloudFlightsDataStore;
import com.ricardo.casarez.flighsearch.data.repository.datasource.flights.DiskFlightsDataStore;
import com.ricardo.casarez.flighsearch.data.repository.datasource.flights.FlightsDataStore;
import com.ricardo.casarez.flighsearch.data.repository.datasource.flights.FlightsDataStoreFactory;

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

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

/**
 * Created by Ricardo Casarez on 7/28/17.
 */
@RunWith(MockitoJUnitRunner.class)
public class FlightsDataStoreFactoryTest {
    private FlightsDataStoreFactory factory;
    @Mock
    private Context context;
    @Mock
    private QpxExpressApi api;
    @Mock
    private FlightsCache cache;

    @Before
    public void setUp() {
        factory = new FlightsDataStoreFactory(api, cache);
    }

    @Test
    public void testCreateCloudStore() {
        FlightsDataStore cloudStore = factory.createCloudDataStore();
        assertTrue(cloudStore instanceof CloudFlightsDataStore);
    }

    @Test
    public void testCreateDiskStore() {
        FlightsDataStore diskStore = factory.createDiskDataStore();
        assertTrue(diskStore instanceof DiskFlightsDataStore);
    }

    @Test
    public void testCreateStoreNoCache() {
        when(cache.isCached()).thenReturn(false);
        FlightsDataStore store = factory.create();
        assertTrue(store instanceof CloudFlightsDataStore);
    }

    @Test
    public void testCreateStoreCacheExpired() {
        when(cache.isCached()).thenReturn(true);
        when(cache.isExpired()).thenReturn(true);
        FlightsDataStore store = factory.create();
        assertTrue(store instanceof CloudFlightsDataStore);
    }

    @Test
    public void testCreateStoreCacheValid() {
        when(cache.isCached()).thenReturn(true);
        when(cache.isExpired()).thenReturn(false);
        FlightsDataStore store = factory.create();
        assertTrue(store instanceof DiskFlightsDataStore);
    }
}
