package com.nuts.test.controller;

import android.test.AndroidTestCase;

import java.lang.reflect.Method;
import java.util.TreeMap;

import com.google.common.reflect.Reflection;
import com.google.gson.Gson;
import com.nuts.lib.controller.ProxyInvokeHandler;
import com.nuts.lib.net.ApiInvokeHandler;
import com.nuts.lib.net.INet;
import com.nuts.test.api.BaseResponse;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;

public class ControllerApiTestCase extends AndroidTestCase {

    MockWebServer mServer;

    TestApi2 mApi;

    TestController mController = new ProxyInvokeHandler<>(TestController.IMPL).createProxy();

    @Override
    public void setUp() throws Exception {
        mServer = new MockWebServer();
        mServer.start();
        mApi = Reflection.newProxy(TestApi2.class, new ApiInvokeHandler(new INet() {
            @Override
            protected String onCreateUrl(final String url, final Method method, final Object[] args) {
                return mServer.getUrl(url)
                        .toString();
            }

            @Override
            protected void onCreateParams(final TreeMap<String, String> params, final TreeMap<String, String>
                    headers, final Method method, final Object[] args) {
            }
        }, new Gson()));
    }

    @Override
    public void tearDown() throws Exception {
        mServer.shutdown();
    }

    public void testSingle() throws Exception {
        BaseResponse firstResponse = new BaseResponse();
        firstResponse.code = 101;
        mServer.enqueue(new MockResponse().setBody(new Gson().toJson(firstResponse)));

        BaseResponse resultResponse = mController.single(mApi, 100)
                .sync();

        assertEquals(101, resultResponse.code);
    }
}

@@ -129,81 +129,265 @@
 ap;%0A
-%0Aimport com.google.common.reflect.Reflection;%0Aimport com.google.gson.Gson
+import java.util.concurrent.CountDownLatch;%0Aimport java.util.concurrent.TimeUnit;%0A%0Aimport com.google.common.reflect.Reflection;%0Aimport com.google.gson.Gson;%0Aimport com.nuts.lib.controller.ControllerCallback;%0Aimport com.nuts.lib.controller.ControllerListener
 ;%0Aim
@@ -2009,11 +2009,2574 @@
 );%0A    %7D
+%0A%0A    public void testMultiTask() throws Exception %7B%0A        final int count = 5000;%0A%0A        final CountDownLatch latch = new CountDownLatch(count);%0A%0A        for (int i = 0; i %3C count; ++i) %7B%0A            BaseResponse firstResponse = new BaseResponse();%0A            firstResponse.code = i + 1;%0A            mServer.enqueue(new MockResponse().setBody(new Gson().toJson(firstResponse))%0A                    .setBodyDelay(200, TimeUnit.MILLISECONDS));%0A%0A            switch (i %25 3) %7B%0A                case 0:%0A                    mController.single(mApi, 100)%0A                            .asyncUI(new ControllerCallback%3CBaseResponse%3E() %7B%0A                                @Override%0A                                public void onResult(final BaseResponse baseResponse) %7B%0A                                    assertNotNull(baseResponse);%0A                                    assertNotSame(0, baseResponse.code);%0A                                    assertTrue(baseResponse.getErrorCode() == 0);%0A                                    latch.countDown();%0A                                %7D%0A                            %7D);%0A                    break;%0A                case 1:%0A                    mController.single(mApi, 100)%0A                            .addListener(new ControllerListener%3CBaseResponse%3E() %7B%0A                                @Override%0A                                public void onBegin() %7B%0A%0A                                %7D%0A%0A                                @Override%0A                                public void onEnd(final BaseResponse response) %7B%0A                                    assertNotNull(response);%0A                                    assertNotSame(0, response.code);%0A                                    assertTrue(response.getErrorCode() == 0);%0A                                    latch.countDown();%0A                                %7D%0A%0A                                @Override%0A                                public void onException(final Throwable throwable) %7B%0A%0A                                %7D%0A                            %7D);%0A                    break;%0A                case 2:%0A                    BaseResponse response = mController.single(mApi, 100)%0A                            .sync();%0A                    assertNotNull(response);%0A                    assertNotSame(0, response.code);%0A                    assertTrue(response.getErrorCode() == 0);%0A                    latch.countDown();%0A                    break;%0A            %7D%0A%0A        %7D%0A%0A        //latch.await(200, TimeUnit.SECONDS);%0A        latch.await();%0A        assertEquals(0, latch.getCount());%0A    %7D
 %0A%7D%0A

add test case