package com.jagsaund.rxuploader.rx;

import com.jagsaund.rxuploader.job.Status;
import java.io.InputStream;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.BufferedSink;
import okio.Source;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import rx.Emitter;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
public class RxRequestBodyTest {

    @Test
    public void testWrite() throws Exception {
        final String jobId = "test-job-id";
        final long length = RxRequestBody.BUFFER_SIZE * 3;
        final BufferedSink sink = mock(BufferedSink.class);
        final InputStream inputStream = mock(InputStream.class);
        final Emitter<Status> emitter = mock(Emitter.class);

        final RequestBody requestBody =
                new RxRequestBody(emitter, jobId, MediaType.parse("text/plain"), inputStream,
                        length);

        requestBody.writeTo(sink);

        verify(sink, times(3)).write(any(Source.class), anyLong());

        verify(emitter).onNext(Status.createSending(jobId, 0));
        verify(emitter).onNext(Status.createSending(jobId, 33));
        verify(emitter).onNext(Status.createSending(jobId, 66));
        verify(emitter).onNext(Status.createSending(jobId, 100));
    }

    @Test
    public void testWriteEmpty() throws Exception {
        final String jobId = "test-job-id";
        final long length = 0;
        final BufferedSink sink = mock(BufferedSink.class);
        final InputStream inputStream = mock(InputStream.class);
        final Emitter<Status> emitter = mock(Emitter.class);

        final RequestBody requestBody =
                new RxRequestBody(emitter, jobId, MediaType.parse("text/plain"), inputStream,
                        length);

        requestBody.writeTo(sink);

        verify(sink, times(0)).write(any(Source.class), anyLong());

        verify(emitter).onNext(Status.createSending(jobId, 0));
    }

    @Test
    public void testWriteSmall() throws Exception {
        final String jobId = "test-job-id";
        final long length = 1;
        final BufferedSink sink = mock(BufferedSink.class);
        final InputStream inputStream = mock(InputStream.class);
        final Emitter<Status> emitter = mock(Emitter.class);

        final RequestBody requestBody =
                new RxRequestBody(emitter, jobId, MediaType.parse("text/plain"), inputStream,
                        length);

        requestBody.writeTo(sink);

        verify(sink, times(1)).write(any(Source.class), anyLong());

        verify(emitter).onNext(Status.createSending(jobId, 0));
        verify(emitter).onNext(Status.createSending(jobId, 100));
    }
}
@@ -73,16 +73,44 @@
 Status;%0A
+import java.io.IOException;%0A
 import j
@@ -370,36 +370,172 @@
 tic org.
-mockito.Matchers.any
+hamcrest.MatcherAssert.assertThat;%0Aimport static org.hamcrest.Matchers.is;%0Aimport static org.mockito.Matchers.any;%0Aimport static org.mockito.Matchers.anyInt
 ;%0Aimport
@@ -694,16 +694,56 @@
 .verify;
+%0Aimport static org.mockito.Mockito.when;
 %0A%0A@RunWi
@@ -3093,9 +3093,1956 @@
 ;%0A    %7D%0A
-%7D
+%0A    @Test(expected = IOException.class)%0A    public void testWriteError() throws Exception %7B%0A        final String jobId = %22test-job-id%22;%0A        final long length = RxRequestBody.BUFFER_SIZE * 3;%0A        final BufferedSink sink = mock(BufferedSink.class);%0A        final InputStream inputStream = mock(InputStream.class);%0A        final Emitter%3CStatus%3E emitter = mock(Emitter.class);%0A%0A        final RequestBody requestBody =%0A                new RxRequestBody(emitter, jobId, MediaType.parse(%22text/plain%22), inputStream,%0A                        length);%0A%0A        when(sink.write(any(Source.class), anyInt())).thenThrow(new IOException(%22Error%22));%0A%0A        // expect this to throw an IOException%0A        requestBody.writeTo(sink);%0A%0A        verify(sink, times(3)).write(any(Source.class), anyLong());%0A        verify(emitter).onNext(Status.createSending(jobId, 0));%0A    %7D%0A%0A    @Test%0A    public void testContentLength() throws Exception %7B%0A        final String jobId = %22test-job-id%22;%0A        final long length = RxRequestBody.BUFFER_SIZE * 3;%0A        final InputStream inputStream = mock(InputStream.class);%0A        final Emitter%3CStatus%3E emitter = mock(Emitter.class);%0A        final RequestBody requestBody =%0A                new RxRequestBody(emitter, jobId, MediaType.parse(%22text/plain%22), inputStream,%0A                        length);%0A%0A        assertThat(requestBody.contentLength(), is(length));%0A    %7D%0A%0A    @Test%0A    public void testContentType() throws Exception %7B%0A        final String jobId = %22test-job-id%22;%0A        final long length = RxRequestBody.BUFFER_SIZE * 3;%0A        final InputStream inputStream = mock(InputStream.class);%0A        final Emitter%3CStatus%3E emitter = mock(Emitter.class);%0A        final MediaType mediaType = MediaType.parse(%22text/plain%22);%0A        final RequestBody requestBody =%0A                new RxRequestBody(emitter, jobId, mediaType, inputStream, length);%0A%0A        assertThat(requestBody.contentType(), is(mediaType));%0A    %7D%0A%7D%0A

Add Unit Tests to RxUploader