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.observers.TestObserver;

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

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

    @Mock
    DefaultDiffCallback mDefaultDiffCallback;

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

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

        final DiffRequestManagerWrapper diffRequestManagerWrapper = new DiffRequestManagerWrapper(mDiffRequestManager, TEST_TAG);

        // When
        final Single<RxDiffResult> single = diffRequestManagerWrapper.calculate();

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

    @Test
    public void calculate_WhenDefaultDiffCallbackNotSet_ReturnsSingle() {
        // Given
        final DiffUtil.Callback callback = mock(DiffUtil.Callback.class);
        final DiffRequest diffRequest = new DiffRequest(true, TEST_TAG, callback);
        mDiffRequestManager.addPendingRequest(diffRequest);

        final DiffRequestManagerWrapper diffRequestManagerWrapper = new DiffRequestManagerWrapper(mDiffRequestManager, TEST_TAG);

        // When
        final Single<RxDiffResult> single = diffRequestManagerWrapper.calculate();

        assertThat(single, notNullValue());

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

        testObserver.assertNoErrors();
    }
}

@@ -42,50 +42,8 @@
 t;%0A%0A
-import android.support.v7.util.DiffUtil;%0A%0A
 impo
@@ -276,16 +276,98 @@
 Single;%0A
+import io.reactivex.annotations.NonNull;%0Aimport io.reactivex.functions.Predicate;%0A
 import i
@@ -528,35 +528,31 @@
 org.
-hamcrest.Matchers.nullValue
+mockito.BDDMockito.then
 ;%0Aim
@@ -587,13 +587,54 @@
 ito.
-mock;
+spy;%0Aimport static org.mockito.Mockito.times;%0A
 %0A%0Apu
@@ -999,46 +999,27 @@
 ate_
-WhenDefaultDiffCallbackSet_ReturnsNull
+ReturnsSharedSingle
 () %7B
@@ -1054,36 +1054,35 @@
 nal DiffRequest 
-diff
+spy
 Request = new Di
@@ -1067,32 +1067,36 @@
 st spyRequest = 
+spy(
 new DiffRequest(
@@ -1124,32 +1124,33 @@
 ultDiffCallback)
+)
 ;%0A        mDiffR
@@ -1173,36 +1173,35 @@
 dPendingRequest(
-diff
+spy
 Request);%0A%0A     
@@ -1470,16 +1470,19 @@
 ingle, n
+otN
 ullValue
@@ -1490,342 +1490,223 @@
 ));%0A
+%0A
     
-%7D%0A%0A
     
-@Test%0A    public void calculate_WhenDefaultDiffCallbackNotSet_ReturnsSingle() %7B%0A        // Given%0A
+for(int i = 0; i %3C 5; ++i) %7B%0A     
        
- 
 final 
-DiffUtil.Callback callback = mock(DiffUtil.Callback.class);%0A        final DiffRequest diffRequest = new DiffRequest(true, TEST_TAG, callback);%0A        mDiffRequestManager.addPendingRequest(diffRequest
+TestObserver%3CRxDiffResult%3E testObserver = new TestObserver%3C%3E();%0A            single.subscribe(testObserver);%0A            testObserver.assertNoErrors(
 );%0A
-%0A
         
 fina
@@ -1705,351 +1705,250 @@
     
-final DiffRequestManagerWrapper diffRequestManagerWrapper = new DiffRequestManagerWrapper(mDiffRequestManager, TEST_TAG);%0A%0A        // When%0A        final Single%3CRxDiffResult%3E single = diffRequestManagerWrapper.calculate();%0A%0A        assertThat(single, notNullValue());%0A%0A        final TestObserver%3CRxDiffResult%3E testObserver = new TestObserver%3C%3E(
+    testObserver.assertValue(new Predicate%3CRxDiffResult%3E() %7B%0A                @Override%0A                public boolean test(@NonNull RxDiffResult rxDiffResult) throws Exception %7B%0A                    return rxDiffResult.getTag().equals(TEST_TAG
 );%0A 
@@ -1958,110 +1958,109 @@
     
+ 
-assert single != null;%0A        single.subscribe(testObserver);%0A%0A        testObserver.assertNoErrors();
+       %7D%0A            %7D);%0A            then(spyRequest).should(times(1)).isDetectingMoves();%0A        %7D
 %0A   

Test whether the returned Single is hot