package com.xamoom.android.xamoomsdk.xamoomsdk.Storage;

import com.xamoom.android.xamoomsdk.BuildConfig;
import com.xamoom.android.xamoomsdk.Storage.DownloadTask;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.concurrent.Semaphore;

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

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class DownloadTaskTest {
  private HttpURLConnection mMockedURLConnection;
  private InputStream mMockedInputStream;
  private URL mURL;

  @Before
  public void setup() throws IOException {
    mMockedURLConnection = Mockito.mock(HttpURLConnection.class);
    mMockedInputStream = Mockito.mock(InputStream.class);
    Mockito.stub(mMockedURLConnection.getInputStream()).toReturn(mMockedInputStream);

    URLStreamHandler handler = new URLStreamHandler() {
      @Override
      protected URLConnection openConnection(final URL arg0)
          throws IOException {
        return mMockedURLConnection;
      }
    };

    mURL = new URL("http://foo.bar", "foo.bar", 80, "", handler);
  }

  @Test
  public void testDownloadTask() throws InterruptedException, IOException {
    Mockito.stub(mMockedURLConnection.getResponseCode()).toReturn(HttpURLConnection.HTTP_OK);
    Mockito.stub(mMockedURLConnection.getContentLength()).toReturn(1000);
    Mockito.stub(mMockedInputStream.read(any(byte[].class))).toReturn(1000).toReturn(-1);

    final Semaphore semaphore = new Semaphore(0);
    DownloadTask task = new DownloadTask(new DownloadTask.OnDownloadTaskCompleted() {
      @Override
      public void completed(ByteArrayOutputStream byteArrayOutputStream) {
        Assert.assertEquals(byteArrayOutputStream.size(), 1000);
        semaphore.release();
      }

      @Override
      public void failed() {
        Assert.fail();
      }
    });

    task.execute(mURL);
    semaphore.acquire();

    Mockito.verify(mMockedInputStream, times(2)).read(any(byte[].class));
    Mockito.verify(mMockedURLConnection).getResponseCode();
    Mockito.verify(mMockedURLConnection.getInputStream());
  }
}

@@ -2325,24 +2325,53 @@
 ert.fail();%0A
+        semaphore.release();%0A
       %7D%0A    
@@ -2590,32 +2590,33 @@
 kedURLConnection
+)
 .getInputStream(
@@ -2616,17 +2616,16 @@
 Stream()
-)
 ;%0A  %7D%0A%7D%0A

Fix wrong bracket