package it.gvnn.slackcast;

import android.test.InstrumentationTestCase;
import android.util.Log;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NoCache;
import com.google.common.io.ByteStreams;

import java.util.concurrent.CountDownLatch;

import it.gvnn.slackcast.mock.MockNetwork;
import it.gvnn.slackcast.search.GPodderPodcastSearchClient;
import it.gvnn.slackcast.search.SearchResultListener;

public class GPodderPodcastSearchClientTest extends InstrumentationTestCase {
    private static final String TAG = "GPodderPodcastSearchClientTest";
    final CountDownLatch signal = new CountDownLatch(1);

    @Override
    protected void setUp() throws Exception {
        super.setUp();
    }

    public void testSearch() throws Exception {
        MockNetwork network = new MockNetwork();

        network.setDataToReturn(ByteStreams.toByteArray(getInstrumentation().getTargetContext().getAssets().open("mock/searchResult.json")));
        RequestQueue queue = new RequestQueue(new NoCache(), network);

        // Start the queue
        queue.start();

        GPodderPodcastSearchClient client = GPodderPodcastSearchClient.getInstance(queue);
        client.search("test", new SearchResultListener() {

            @Override
            public void onResult(PodcastSearchResponse response) {
                Log.d(TAG, response.toString());
                assertEquals(response.size(), 2);
                signal.countDown();
            }

            @Override
            public void onError(VolleyError error) {
                Log.d(TAG, error.toString());
                signal.countDown();
                assertNull(error); // never true
            }

        });
        signal.await();
    }

}

@@ -69,33 +69,8 @@
 ase;
-%0Aimport android.util.Log;
 %0A%0Aim
@@ -230,16 +230,73 @@
 treams;%0A
+import com.google.gson.Gson;%0A%0Aimport java.io.InputStream;
 %0Aimport 
@@ -573,80 +573,8 @@
 e %7B%0A
-    private static final String TAG = %22GPodderPodcastSearchClientTest%22;%0A
     
@@ -765,24 +765,25 @@
 ption %7B%0A
+%0A
         
 MockNetw
@@ -778,58 +778,125 @@
     
-MockNetwork network = new MockNetwork(
+InputStream mockStream = getInstrumentation().getTargetContext().getAssets().open(%22mock/searchResult.json%22
 );%0A
-%0A
         
 netw
@@ -895,139 +895,309 @@
     
-network.setDataToReturn(ByteStreams.toByteArray(getInstrumentation().getTargetContext().getAssets().ope
+byte%5B%5D mockResult = ByteStreams.toByteArray(mockStream);%0A%0A        Gson gson = new Gson();%0A        final PodcastSearchResponse mockResponse = gson.fromJson(new String(mockResult), PodcastSearchResponse.class);%0A%0A        MockNetwork network = new MockNetwork();%0A        network.setDataToRetur
 n(
-%22
 mock
-/searchResult.json%22))
+Result
 );%0A 
@@ -1271,35 +1271,8 @@
 );%0A%0A
-        // Start the queue%0A
     
@@ -1551,90 +1551,485 @@
     
-Log.d(TAG, response.toString());%0A                assertEquals(response.size(), 2);
+assertEquals(response.size(), mockResponse.size());%0A                for (int i = 0; i %3C response.size(); i++) %7B%0A                    assertEquals(response.get(i).description, mockResponse.get(i).description);%0A                    assertEquals(response.get(i).title, mockResponse.get(i).title);%0A                    assertEquals(response.get(i).url, mockResponse.get(i).url);%0A                    assertEquals(response.get(i).website, mockResponse.get(i).website);%0A                %7D
 %0A   
@@ -2155,54 +2155,8 @@
 ) %7B%0A
-                Log.d(TAG, error.toString());%0A
     
@@ -2295,9 +2295,8 @@
     %7D%0A%0A%7D
-%0A

Improve GPodderPodcastSearchClientTest