Android Open Source - android-model-record Model Record Test






From Project

Back to project page android-model-record.

License

The source code is released under:

MIT License

If you think the Android project android-model-record listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.fanpics.opensource.android.modelrecord;
/*  www.  ja  v a  2s  .c  om*/
import android.os.Handler;

import com.fanpics.opensource.android.modelrecord.callback.CreateCallback;
import com.fanpics.opensource.android.modelrecord.callback.DeleteCallback;
import com.fanpics.opensource.android.modelrecord.callback.LoadCallback;
import com.fanpics.opensource.android.modelrecord.callback.LoadListCallback;
import com.fanpics.opensource.android.modelrecord.callback.UpdateCallback;
import com.fanpics.opensource.android.modelrecord.configuration.MultiRecordConfiguration;
import com.fanpics.opensource.android.modelrecord.configuration.SingleRecordConfiguration;
import com.fanpics.opensource.android.modelrecord.event.EventProcessor;
import com.fanpics.opensource.android.modelrecord.event.FailureEvent;
import com.fanpics.opensource.android.modelrecord.event.SuccessEvent;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.robolectric.RobolectricTestRunner;

import java.util.ArrayList;
import java.util.List;

import static org.fest.assertions.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(RobolectricTestRunner.class)
public class ModelRecordTest {

    private ModelRecord modelRecord;
    private RecordCache cache;
    private SingleRecordConfiguration singleRecordConfiguration;
    private MultiRecordConfiguration multiRecordConfiguration;

    @Before
    public void createModelRecord() {
        modelRecord = mock(ModelRecord.class);
        modelRecord.handler = new Handler();
    }

    @Before
    public void createConfiguration() {
        singleRecordConfiguration = mock(SingleRecordConfiguration.class);
        multiRecordConfiguration = mock(MultiRecordConfiguration.class);
        cache = mock(RecordCache.class);

        setupConfiguration(singleRecordConfiguration);
        setupConfiguration(multiRecordConfiguration);
    }

    private void setupConfiguration(SingleRecordConfiguration configuration) {
        when(configuration.getCache()).thenReturn(cache);
        when(configuration.getSuccessEvent()).thenReturn(mock(SuccessEvent.class));
        when(configuration.getFailureEvent()).thenReturn(mock(FailureEvent.class));
        when(configuration.shouldRunSynchronously()).thenReturn(false);
        when(configuration.shouldRunAsynchronously()).thenReturn(true);
    }

    private void setupConfiguration(MultiRecordConfiguration configuration) {
        when(configuration.getCache()).thenReturn(cache);
        when(configuration.getSuccessEvent()).thenReturn(mock(SuccessEvent.class));
        when(configuration.getFailureEvent()).thenReturn(mock(FailureEvent.class));
        when(configuration.shouldRunSynchronously()).thenReturn(false);
        when(configuration.shouldRunAsynchronously()).thenReturn(true);
    }

    @Test
    @SuppressWarnings("unchecked")
    public void testCreate() {
        final Object object = new Object();
        doCallRealMethod().when(modelRecord).create(object);
        when(modelRecord.setupCreateConfiguration(any(SingleRecordConfiguration.class), eq(object))).thenReturn(singleRecordConfiguration);
        modelRecord.create(object);

        verify(modelRecord).setupCreateConfiguration(any(SingleRecordConfiguration.class), eq(object));
        verify(singleRecordConfiguration).performAsynchronousNetworkCall(eq(object), any(CreateCallback.class));
    }

    @Test
    @SuppressWarnings("unchecked")
    public void testUpdate() {
        final Object object = new Object();
        when(modelRecord.setupUpdateConfiguration(any(SingleRecordConfiguration.class), eq(object))).thenReturn(singleRecordConfiguration);
        doCallRealMethod().when(modelRecord).update(object);
        modelRecord.update(object);

        verify(modelRecord).setupUpdateConfiguration(any(SingleRecordConfiguration.class), eq(object));
        verify(singleRecordConfiguration).performAsynchronousNetworkCall(eq(object), any(UpdateCallback.class));
    }

    @Test
    @SuppressWarnings("unchecked")
    public void testDelete() {
        final Object object = new Object();
        when(modelRecord.setupDeleteConfiguration(any(SingleRecordConfiguration.class), eq(object))).thenReturn(singleRecordConfiguration);
        doCallRealMethod().when(modelRecord).delete(object);
        modelRecord.delete(object);

        verify(modelRecord).setupDeleteConfiguration(any(SingleRecordConfiguration.class), eq(object));
        verify(singleRecordConfiguration).performAsynchronousNetworkCall(eq(object), any(DeleteCallback.class));
    }

    @Test
    public void testLoad() {
        modelRecord = mock(ModelRecord.class);
        doCallRealMethod().when(modelRecord).load();

        modelRecord.load();
        verify(modelRecord).load(null);
    }

    @Test
    public void testLoadingWithNetworkAndNoCache() {
        final Object object = new Object();

        when(modelRecord.setupLoadConfiguration(any(SingleRecordConfiguration.class), any(Object.class))).thenReturn(singleRecordConfiguration);

        when(singleRecordConfiguration.shouldLoadFromNetwork()).thenReturn(true);
        when(singleRecordConfiguration.shouldLoadFromCache()).thenReturn(false);

        doCallRealMethod().when(modelRecord).load(any(Object.class));
        doCallRealMethod().when(modelRecord).load(any(Object.class), any(SingleRecordConfiguration.class));
        modelRecord.load(object, new SingleRecordConfiguration(SingleRecordConfiguration.Type.LOAD));

        assertThat(singleRecordConfiguration.getSuccessEvent().hasFinished());
        verify(modelRecord).setupLoadConfiguration(any(SingleRecordConfiguration.class), eq(object));
        verify(singleRecordConfiguration).performAsynchronousNetworkCall(eq(object), any(LoadCallback.class));
        verify(cache, never()).load(eq(object));
    }

    @Test
    public void testLoadingWithNetworkAndCache() {
        final Object object = new Object();
        final Object result = new Object();

        when(modelRecord.setupLoadConfiguration(any(SingleRecordConfiguration.class), any(Object.class))).thenReturn(singleRecordConfiguration);
        when(singleRecordConfiguration.getCache()).thenReturn(cache);
        when(cache.load(object)).thenReturn(result);
        modelRecord.eventProcessor = mock(EventProcessor.class);

        when(singleRecordConfiguration.shouldLoadFromNetwork()).thenReturn(true);
        when(singleRecordConfiguration.shouldLoadFromCache()).thenReturn(true);

        doCallRealMethod().when(modelRecord).load(any(Object.class));
        doCallRealMethod().when(modelRecord).load(any(Object.class), any(SingleRecordConfiguration.class));
        modelRecord.load(object, new SingleRecordConfiguration(SingleRecordConfiguration.Type.LOAD));

        assertThat(singleRecordConfiguration.getSuccessEvent().hasFinished());
        verify(cache).load(object);
        verify(modelRecord.eventProcessor).process(singleRecordConfiguration.getSuccessEvent());
        verify(singleRecordConfiguration).performAsynchronousNetworkCall(eq(object), any(LoadCallback.class));
    }

    @Test
    public void testLoadingWithCacheAndNoNetwork() {
        final Object object = new Object();
        final Object result = new Object();

        when(modelRecord.setupLoadConfiguration(any(SingleRecordConfiguration.class), any(Object.class))).thenReturn(singleRecordConfiguration);
        when(singleRecordConfiguration.getCache()).thenReturn(cache);
        when(cache.load(object)).thenReturn(result);
        modelRecord.eventProcessor = mock(EventProcessor.class);

        when(singleRecordConfiguration.shouldLoadFromNetwork()).thenReturn(false);
        when(singleRecordConfiguration.shouldLoadFromCache()).thenReturn(true);

        doCallRealMethod().when(modelRecord).load(any(Object.class));
        doCallRealMethod().when(modelRecord).load(any(Object.class), any(SingleRecordConfiguration.class));
        modelRecord.load(object, new SingleRecordConfiguration(SingleRecordConfiguration.Type.LOAD));

        assertThat(singleRecordConfiguration.getSuccessEvent().hasFinished());
        verify(cache).load(object);
        verify(modelRecord.eventProcessor).process(singleRecordConfiguration.getSuccessEvent());
        verify(singleRecordConfiguration, never()).performAsynchronousNetworkCall(eq(object), any(LoadCallback.class));
    }

    @Test
    public void testLoadList() {
        modelRecord = mock(ModelRecord.class);
        doCallRealMethod().when(modelRecord).loadList();

        modelRecord.loadList();
        verify(modelRecord).loadList(null);
    }

    @Test
    public void testLoadingListWithNetworkAndNoCache() {
        final Object object = new Object();

        when(modelRecord.setupLoadListConfiguration(any(MultiRecordConfiguration.class), any(Object.class))).thenReturn(multiRecordConfiguration);
        when(multiRecordConfiguration.shouldLoadFromNetwork()).thenReturn(true);
        when(multiRecordConfiguration.shouldLoadFromCache()).thenReturn(false);

        doCallRealMethod().when(modelRecord).loadList(any(Object.class));
        doCallRealMethod().when(modelRecord).loadList(any(Object.class), any(MultiRecordConfiguration.class));
        modelRecord.loadList(object, new MultiRecordConfiguration(MultiRecordConfiguration.Type.LOAD));

        verify(modelRecord).setupLoadListConfiguration(any(MultiRecordConfiguration.class), any(Object.class));
        verify(multiRecordConfiguration).performAsynchronousNetworkCall(eq(object), any(LoadListCallback.class));
        verify(cache, never()).loadList(eq(object));
    }

    @Test
    public void testLoadingListWithCacheAndNetwork() {
        final Object object = new Object();
        final List<Object> result = new ArrayList<>();

        when(modelRecord.setupLoadListConfiguration(any(MultiRecordConfiguration.class), any(Object.class))).thenReturn(multiRecordConfiguration);
        when(cache.loadList(object)).thenReturn(result);
        modelRecord.eventProcessor = mock(EventProcessor.class);

        when(multiRecordConfiguration.shouldLoadFromNetwork()).thenReturn(true);
        when(multiRecordConfiguration.shouldLoadFromCache()).thenReturn(true);

        doCallRealMethod().when(modelRecord).loadList(any(Object.class));
        doCallRealMethod().when(modelRecord).loadList(any(Object.class), any(MultiRecordConfiguration.class));
        modelRecord.loadList(object, new MultiRecordConfiguration(MultiRecordConfiguration.Type.LOAD));

        verify(cache).loadList(object);
        verify(modelRecord.eventProcessor).process(multiRecordConfiguration.getSuccessEvent());
        verify(multiRecordConfiguration).performAsynchronousNetworkCall(eq(object), any(LoadListCallback.class));
    }

    @Test
    public void testLoadingListWithCacheAndNoNetwork() {
        final Object object = new Object();
        final List<Object> result = new ArrayList<>();

        when(modelRecord.setupLoadListConfiguration(any(MultiRecordConfiguration.class), any(Object.class))).thenReturn(multiRecordConfiguration);
        when(cache.loadList(object)).thenReturn(result);
        modelRecord.eventProcessor = mock(EventProcessor.class);

        when(multiRecordConfiguration.shouldLoadFromNetwork()).thenReturn(false);
        when(multiRecordConfiguration.shouldLoadFromCache()).thenReturn(true);

        doCallRealMethod().when(modelRecord).loadList(any(Object.class));
        doCallRealMethod().when(modelRecord).loadList(any(Object.class), any(MultiRecordConfiguration.class));
        modelRecord.loadList(object, new MultiRecordConfiguration(MultiRecordConfiguration.Type.LOAD));

        verify(cache).loadList(object);
        verify(modelRecord.eventProcessor).process(multiRecordConfiguration.getSuccessEvent());
        verify(multiRecordConfiguration, never()).performAsynchronousNetworkCall(eq(object), any(LoadListCallback.class));
    }

    @Test
    public void testRefresh() {
        modelRecord = mock(ModelRecord.class);
        doCallRealMethod().when(modelRecord).refresh();

        modelRecord.refresh();
        verify(modelRecord).refresh(null);
    }

    @Test
    public void testRefreshWithKey() {
        final Object key = new Object();
        ArgumentCaptor<SingleRecordConfiguration> settings = ArgumentCaptor.forClass(SingleRecordConfiguration.class);

        when(modelRecord.setupLoadConfiguration(any(SingleRecordConfiguration.class), any(Object.class))).thenReturn(this.singleRecordConfiguration);
        doCallRealMethod().when(modelRecord).refresh(any(Object.class));
        modelRecord.refresh(key);

        verify(modelRecord).loadAsynchronously(eq(key), settings.capture());
        assertThat(settings.getValue().getType()).isEqualTo(SingleRecordConfiguration.Type.REFRESH);
    }

    @Test
    public void testRefreshList() {
        modelRecord = mock(ModelRecord.class);
        doCallRealMethod().when(modelRecord).refreshList();

        modelRecord.refreshList();
        verify(modelRecord).refreshList(null);
    }

    @Test
    public void testRefreshListWithKey() {
        final Object key = new Object();
        ArgumentCaptor<MultiRecordConfiguration> settings = ArgumentCaptor.forClass(MultiRecordConfiguration.class);

        when(modelRecord.setupLoadListConfiguration(any(MultiRecordConfiguration.class), any(Object.class))).thenReturn(multiRecordConfiguration);
        doCallRealMethod().when(modelRecord).refreshList(any(Object.class));
        modelRecord.refreshList(key);

        verify(modelRecord).loadListAsynchronously(eq(key), settings.capture());
        assertThat(settings.getValue().getType()).isEqualTo(SingleRecordConfiguration.Type.REFRESH);
    }

    @Test
    public void testGetPreLoaded() {
        modelRecord = mock(ModelRecord.class);
        doCallRealMethod().when(modelRecord).getPreLoaded();

        modelRecord.getPreLoaded();
        verify(modelRecord).getPreLoaded(null);
    }

    @Test
    public void testGetPreLoadedWithKey() {
        final Object key = new Object();
        ArgumentCaptor<SingleRecordConfiguration> settings = ArgumentCaptor.forClass(SingleRecordConfiguration.class);

        when(modelRecord.setupLoadConfiguration(any(SingleRecordConfiguration.class), any(Object.class))).thenReturn(this.singleRecordConfiguration);
        doCallRealMethod().when(modelRecord).getPreLoaded(any(Object.class));
        modelRecord.getPreLoaded(key);

        verify(modelRecord).loadAsynchronously(eq(key), settings.capture());
        assertThat(settings.getValue().getType()).isEqualTo(SingleRecordConfiguration.Type.CACHE_ONLY);
    }

    @Test
    public void testGetPreLoadedList() {
        modelRecord = mock(ModelRecord.class);
        doCallRealMethod().when(modelRecord).getPreLoadedList();

        modelRecord.getPreLoadedList();
        verify(modelRecord).getPreLoadedList(null);
    }

    @Test
    public void testGetPreLoadedListWithKey() {
        final Object key = new Object();
        ArgumentCaptor<MultiRecordConfiguration> settings = ArgumentCaptor.forClass(MultiRecordConfiguration.class);

        when(modelRecord.setupLoadListConfiguration(any(MultiRecordConfiguration.class), any(Object.class))).thenReturn(multiRecordConfiguration);
        doCallRealMethod().when(modelRecord).getPreLoadedList(any(Object.class));
        modelRecord.getPreLoadedList(key);

        verify(modelRecord).loadListAsynchronously(eq(key), settings.capture());
        assertThat(settings.getValue().getType()).isEqualTo(SingleRecordConfiguration.Type.CACHE_ONLY);
    }

}




Java Source Code List

com.fanpics.opensource.android.modelrecord.HttpReport.java
com.fanpics.opensource.android.modelrecord.ModelRecordTest.java
com.fanpics.opensource.android.modelrecord.ModelRecord.java
com.fanpics.opensource.android.modelrecord.RecordCache.java
com.fanpics.opensource.android.modelrecord.ResponseParser.java
com.fanpics.opensource.android.modelrecord.Result.java
com.fanpics.opensource.android.modelrecord.callback.BaseRecordCallback.java
com.fanpics.opensource.android.modelrecord.callback.CreateCallback.java
com.fanpics.opensource.android.modelrecord.callback.DeleteCallbackTest.java
com.fanpics.opensource.android.modelrecord.callback.DeleteCallback.java
com.fanpics.opensource.android.modelrecord.callback.FailureCallback.java
com.fanpics.opensource.android.modelrecord.callback.LoadCallback.java
com.fanpics.opensource.android.modelrecord.callback.LoadListCallback.java
com.fanpics.opensource.android.modelrecord.callback.RecordCallbackTest.java
com.fanpics.opensource.android.modelrecord.callback.RecordCallback.java
com.fanpics.opensource.android.modelrecord.callback.SuccessCallback.java
com.fanpics.opensource.android.modelrecord.callback.UpdateCallback.java
com.fanpics.opensource.android.modelrecord.configuration.BaseRecordConfigurationTest.java
com.fanpics.opensource.android.modelrecord.configuration.BaseRecordConfiguration.java
com.fanpics.opensource.android.modelrecord.configuration.MultiRecordConfigurationTest.java
com.fanpics.opensource.android.modelrecord.configuration.MultiRecordConfiguration.java
com.fanpics.opensource.android.modelrecord.configuration.SingleRecordConfigurationTest.java
com.fanpics.opensource.android.modelrecord.configuration.SingleRecordConfiguration.java
com.fanpics.opensource.android.modelrecord.event.EventProcessor.java
com.fanpics.opensource.android.modelrecord.event.FailureEvent.java
com.fanpics.opensource.android.modelrecord.event.OttoProcessorTest.java
com.fanpics.opensource.android.modelrecord.event.OttoProcessor.java
com.fanpics.opensource.android.modelrecord.event.SuccessEventTest.java
com.fanpics.opensource.android.modelrecord.event.SuccessEvent.java
com.fanpics.opensource.android.modelrecord.sample.data.cache.ImgurDataCache.java
com.fanpics.opensource.android.modelrecord.sample.data.model.ImgurData.java
com.fanpics.opensource.android.modelrecord.sample.data.model.ImgurItem.java
com.fanpics.opensource.android.modelrecord.sample.data.model.record.ImgurDataRecord.java
com.fanpics.opensource.android.modelrecord.sample.data.network.ImgurItemService.java
com.fanpics.opensource.android.modelrecord.sample.event.ImgurDataLoadFailedEvent.java
com.fanpics.opensource.android.modelrecord.sample.event.ImgurDataLoadSucceededEvent.java
com.fanpics.opensource.android.modelrecord.sample.ui.activity.MainActivity.java
com.fanpics.opensource.android.modelrecord.sample.ui.adapter.ImgurAdapter.java