package com.canoo.dolphin.client.impl;

import com.canoo.dolphin.client.DummyUiThreadHandler;
import com.canoo.dolphin.util.DolphinRemotingException;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHttpResponse;
import org.opendolphin.core.client.ClientDolphin;
import org.opendolphin.core.comm.Command;
import org.opendolphin.core.comm.CreatePresentationModelCommand;
import org.opendolphin.core.comm.JsonCodec;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.net.MalformedURLException;
import java.net.URL;

public class TestDolphinPlatformHttpClientConnector {

    @Test
    public void testSimpleCall() throws MalformedURLException {
        HttpClient httpClient = new DefaultHttpClient() {

            @Override
            public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler) throws IOException, ClientProtocolException {
                return (T) "[{\"pmId\":\"p1\",\"clientSideOnly\":false,\"id\":\"CreatePresentationModel\",\"attributes\":[],\"pmType\":null,\"className\":\"org.opendolphin.core.comm.CreatePresentationModelCommand\"}]";
            }
        };

        final URL dummyURL = new URL("http://dummyURL");
        DolphinPlatformHttpClientConnector connector = new DolphinPlatformHttpClientConnector(new ClientDolphin(), new JsonCodec(), httpClient, dummyURL, new ForwardableCallback<>(), new DummyUiThreadHandler());

        CreatePresentationModelCommand command = new CreatePresentationModelCommand();
        command.setPmId("p1");
        List<Command> result = connector.transmit(Collections.singletonList(command));

        Assert.assertEquals(result.size(), 1);
        Assert.assertTrue(result.get(0) instanceof CreatePresentationModelCommand);
        Assert.assertEquals(((CreatePresentationModelCommand) result.get(0)).getPmId(), "p1");
    }

    @Test(expectedExceptions = DolphinRemotingException.class)
    public void testBadResponse() throws MalformedURLException {
        final CountDownLatch httpWasCalled = new CountDownLatch(1);

        HttpClient httpClient = new DefaultHttpClient() {

            @Override
            public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler) throws IOException, ClientProtocolException {
                StatusLine statusLine = new StatusLine() {
                    @Override
                    public ProtocolVersion getProtocolVersion() {
                        return new ProtocolVersion("Dummy-Protocol", 1, 1);
                    }

                    @Override
                    public int getStatusCode() {
                        return 500;
                    }

                    @Override
                    public String getReasonPhrase() {
                        return "Internal Server Error";
                    }
                };
                StringEntity entity = new StringEntity("failed");
                HttpResponse response = new BasicHttpResponse(statusLine);
                response.setEntity(entity);
                responseHandler.handleResponse(response);
                httpWasCalled.countDown();
                return (T) "[]";
            }
        };
        final URL dummyURL = new URL("http://dummyURL");
        DolphinPlatformHttpClientConnector connector = new DolphinPlatformHttpClientConnector(new ClientDolphin(), new JsonCodec(), httpClient, dummyURL, new ForwardableCallback<>(), new DummyUiThreadHandler());

        connector.transmit(Collections.singletonList(new Command()));
    }

    @Test(expectedExceptions = DolphinRemotingException.class)
    public void testCallWithException() throws MalformedURLException {
        final URL dummyURL = new URL("http://dummyURL");
        DolphinPlatformHttpClientConnector connector = new DolphinPlatformHttpClientConnector(new ClientDolphin(), new JsonCodec(), new DefaultHttpClient(), dummyURL, new ForwardableCallback<>(), new DummyUiThreadHandler());
        connector.transmit(Collections.singletonList(new Command()));
    }

}

@@ -1152,37 +1152,8 @@
 ll()
- throws MalformedURLException
  %7B%0A 
@@ -1630,65 +1630,8 @@
 %7D;%0A%0A
-        final URL dummyURL = new URL(%22http://dummyURL%22);%0A
     
@@ -1766,32 +1766,37 @@
 httpClient, 
-d
+getD
 ummyURL
+()
 , new Forwar
@@ -2383,37 +2383,8 @@
 se()
- throws MalformedURLException
  %7B%0A 
@@ -3602,65 +3602,8 @@
  %7D;%0A
-        final URL dummyURL = new URL(%22http://dummyURL%22);%0A
     
@@ -3742,24 +3742,29 @@
 Client, 
-d
+getD
 ummyURL
+()
 , new Fo
@@ -4000,95 +4000,9 @@
 n() 
-throws MalformedURLException %7B%0A        final URL dummyURL = new URL(%22http://dummyURL%22);
+%7B
 %0A   
@@ -4159,16 +4159,21 @@
 (), 
-d
+getD
 ummyURL
+()
 , ne
@@ -4305,10 +4305,247 @@
 %0A    %7D%0A%0A
+    private URL getDummyURL() %7B%0A        try %7B%0A            return new URL(%22http://dummyURL%22);%0A        %7D catch (MalformedURLException e) %7B%0A            throw new RuntimeException(%22Exception occurred while creating URL%22, e);%0A        %7D%0A    %7D%0A
 %7D%0A

create helper method for creating URL