package com.stolets.rxdiffutil.diffrequest;

import android.support.v7.util.DiffUtil;

import com.stolets.rxdiffutil.BaseTest;
import com.stolets.rxdiffutil.DefaultDiffCallback;
import com.stolets.rxdiffutil.RxDiffResult;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;

import io.reactivex.Single;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Predicate;
import io.reactivex.observers.TestObserver;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.mock;

public class DiffRequestManagerTest extends BaseTest {
    private static final String TEST_TAG = "TEST_TAG";
    private DiffRequestManager mDiffRequestManager;

    @Before
    public void setup() {
        mDiffRequestManager = new DiffRequestManager();
    }

    @Mock
    DiffUtil.Callback mCallback;

    @Test
    public void addPendingRequest_AddsRequestToMap() {
        // Given
        final DiffRequest diffRequest = new DiffRequest(true, TEST_TAG, mCallback);

        // When
        mDiffRequestManager.addPendingRequest(diffRequest);

        // Then
        assertThat(mDiffRequestManager.getPendingRequests().size(), is(1));
        assertThat(mDiffRequestManager.getPendingRequests().get(TEST_TAG), notNullValue());
    }

    @Test
    public void execute_GiveDiffUtilCallback_ReturnsNull() {
        // Given
        final DiffRequest diffRequest = new DiffRequest(true, TEST_TAG, mCallback);
        mDiffRequestManager.addPendingRequest(diffRequest);

        // When
        final Single<RxDiffResult> single = mDiffRequestManager.execute(TEST_TAG);

        // Then
        assertThat(single, notNullValue());

        final TestObserver<RxDiffResult> testObserver = new TestObserver<>();
        assert single != null;
        single.subscribe(testObserver);

        testObserver.assertValue(new Predicate<RxDiffResult>() {
            @Override
            public boolean test(@NonNull RxDiffResult rxDiffResult) throws Exception {
                return rxDiffResult.getTag().equals(TEST_TAG);
            }
        });
    }

    @Test
    public void execute_GivenDefaultDiffCallback_ReturnsSingle() {
        // Given
        final DefaultDiffCallback defaultDiffCallback = mock(DefaultDiffCallback.class);
        final DiffRequest diffRequest = new DiffRequest(true, TEST_TAG, defaultDiffCallback);

        mDiffRequestManager.addPendingRequest(diffRequest);

        // When
        final Single<RxDiffResult> single = mDiffRequestManager.execute(TEST_TAG);

        // Then
        assertThat(single, nullValue());
    }
}

@@ -124,59 +124,8 @@
 st;%0A
-import com.stolets.rxdiffutil.DefaultDiffCallback;%0A
 impo
@@ -164,16 +164,16 @@
 Result;%0A
+
 %0Aimport 
@@ -541,95 +541,9 @@
 ue;%0A
-import static org.hamcrest.Matchers.nullValue;%0Aimport static org.mockito.Mockito.mock;
 %0A
+
 %0Apub
@@ -649,16 +649,59 @@
 T_TAG%22;%0A
+    @Mock%0A    DiffUtil.Callback mCallback;%0A
     priv
@@ -850,52 +850,8 @@
  %7D%0A%0A
-    @Mock%0A    DiffUtil.Callback mCallback;%0A%0A
     
@@ -1319,40 +1319,412 @@
 ute_
-GiveDiffUtilCallback_ReturnsNull
+RemovesPendingRequest() %7B%0A        // Given%0A        final DiffRequest diffRequest = new DiffRequest(true, TEST_TAG, mCallback);%0A        mDiffRequestManager.addPendingRequest(diffRequest);%0A%0A        // When%0A        mDiffRequestManager.execute(TEST_TAG);%0A%0A        // Then%0A        assertThat(mDiffRequestManager.getPendingRequests().size(), is(0));%0A    %7D%0A%0A    @Test%0A    public void execute_ReturnsSharedSingle
 () %7B
@@ -2129,39 +2129,8 @@
 ();%0A
-        assert single != null;%0A
     
@@ -2417,16 +2417,16 @@
       %7D%0A
+
         
@@ -2438,510 +2438,7 @@
    %7D
-%0A%0A    @Test%0A    public void execute_GivenDefaultDiffCallback_ReturnsSingle() %7B%0A        // Given%0A        final DefaultDiffCallback defaultDiffCallback = mock(DefaultDiffCallback.class);%0A        final DiffRequest diffRequest = new DiffRequest(true, TEST_TAG, defaultDiffCallback);%0A%0A        mDiffRequestManager.addPendingRequest(diffRequest);%0A%0A        // When%0A        final Single%3CRxDiffResult%3E single = mDiffRequestManager.execute(TEST_TAG);%0A%0A        // Then%0A        assertThat(single, nullValue());%0A    %7D
 %0A%7D%0A

Add removing pending request unit test