package be.iminds.ilabt.jfed.lowlevel.api;

import be.iminds.ilabt.jfed.fedmon.webapi.service.json.*;
import be.iminds.ilabt.jfed.lowlevel.connection.ConnectionConfig;
import be.iminds.ilabt.jfed.lowlevel.connection.HttpConnection;
import be.iminds.ilabt.jfed.lowlevel.connection.JFedConnection;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicStatusLine;
import org.apache.http.protocol.HttpContext;

import java.io.ByteArrayInputStream;
import java.io.IOException;

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

/**
 * User: twalcari
 * Date: 1/8/2016
 * Time: 15:31
 */
public abstract class RestApiTest {

    public HttpConnection mockHttpConnection(String answer) throws IOException {

        ConnectionConfig conConfig = mock(ConnectionConfig.class);

        HttpResponse response =
                new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK"));
        final BasicHttpEntity httpEntity = new BasicHttpEntity();

        httpEntity.setContent(new ByteArrayInputStream(answer.getBytes()));
        response.setEntity(httpEntity);

        HttpClient client = mock(HttpClient.class);
        when(client.execute(any(HttpHost.class), any(HttpRequest.class), any(HttpContext.class))).thenReturn(response);


        JFedConnection.DebugInfo debugInfo = mock(JFedConnection.DebugInfo.class);
        Service service = mock(Service.class);
        when(debugInfo.getService()).thenReturn(service);

        HttpConnection con = mock(HttpConnection.class);
        when(con.getConnectionConfig()).thenReturn(conConfig);
        when(con.getHttpClient()).thenReturn(client);
        when(con.getHttpContext()).thenReturn(null);
        when(con.getServerUrl()).thenReturn("http://test/");
        when(con.getDebugInfo()).thenReturn(debugInfo);

        return con;

    }


}
