package com.wikia.mwapi;

import com.wikia.mwapi.domain.ApiResponse;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import io.dropwizard.testing.FixtureHelpers;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import static org.junit.Assert.*;

public class MWApiTest {

  @Test
  public void testBuildUrl() {
    String url = MWApi.createBuilder()
        .wikia("muppet")
        .queryAction()
        .titles("Kermit the Frog")
        .url();
    assertEquals(url, "http://muppet.wikia.com/api.php?action=query&titles=Kermit+the+Frog&format=json");
  }

  @Test
  public void testGet() throws IOException {
    String wikia = "muppet";
    String title = "Kermit the Frog";
    String url = MWApi.createBuilder()
        .wikia(wikia)
        .queryAction()
        .titles(title)
        .url();
    HttpUriRequest request = new HttpGet(url);

    HttpClient httpClient = Mockito.mock(HttpClient.class);
    HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
    HttpEntity httpEntity = Mockito.mock(HttpEntity.class);

    InputStream jsonStream = new ByteArrayInputStream(
        FixtureHelpers.fixture("fixtures/kermit-the-frog-title.json").getBytes(StandardCharsets.UTF_8)
    );
    Mockito.when(httpEntity.getContent()).thenReturn(jsonStream);
    Mockito.when(httpResponse.getEntity()).thenReturn(httpEntity);
    Mockito.when(httpClient.execute(Mockito.any(HttpUriRequest.class))).thenReturn(httpResponse);

    ApiResponse response = MWApi.createBuilder(httpClient)
        .wikia(wikia)
        .queryAction()
        .titles(title)
        .get();
    assertEquals("Kermit the Frog", response.getQuery().getFirstPage().getTitle());
    assertEquals(50, response.getQuery().getFirstPage().getPageId());

    ArgumentCaptor<HttpUriRequest> argument = ArgumentCaptor.forClass(HttpUriRequest.class);
    Mockito.verify(httpClient).execute(argument.capture());
    assertEquals(url, argument.getValue().getURI().toString());
  }

}

@@ -924,16 +924,25 @@
  testGet
+WithTitle
 () throw
@@ -1147,24 +1147,25 @@
 .url();%0A
+%0A
     Http
 UriReque
@@ -1160,47 +1160,716 @@
 Http
-UriRequest request = new HttpGet(url);%0A
+Client httpClient = mockHttpClientForHandleMwRequest(%22fixtures/kermit-the-frog-title.json%22);%0A%0A    ApiResponse response = MWApi.createBuilder(httpClient)%0A        .wikia(wikia)%0A        .queryAction()%0A        .titles(title)%0A        .get();%0A    assertEquals(%22Kermit the Frog%22, response.getQuery().getFirstPage().getTitle());%0A    assertEquals(50, response.getQuery().getFirstPage().getPageId());%0A%0A    ArgumentCaptor%3CHttpUriRequest%3E argument = ArgumentCaptor.forClass(HttpUriRequest.class);%0A    Mockito.verify(httpClient).execute(argument.capture());%0A    assertEquals(url, argument.getValue().getURI().toString());%0A  %7D%0A%0A%0A  public HttpClient mockHttpClientForHandleMwRequest(String fixturePath) throws IOException %7B
 %0A   
@@ -2142,45 +2142,19 @@
 ure(
-%22
 fixture
-s/kermit-the-frog-title.json%22
+Path
 ).ge
@@ -2191,16 +2191,17 @@
 %0A    );%0A
+%0A
     Mock
@@ -2432,517 +2432,25 @@
     
-ApiResponse response = MWApi.createBuilder(httpClient)%0A        .wikia(wikia)%0A        .queryAction()%0A        .titles(title)%0A        .get();%0A    assertEquals(%22Kermit the Frog%22, response.getQuery().getFirstPage().getTitle());%0A    assertEquals(50, response.getQuery().getFirstPage().getPageId());%0A%0A    ArgumentCaptor%3CHttpUriRequest%3E argument = ArgumentCaptor.forClass(HttpUriRequest.class);%0A    Mockito.verify(httpClient).execute(argument.capture());%0A    assertEquals(url, argument.getValue().getURI().toString())
+return httpClient
 ;%0A  

Split the HttpClient mock creation into helper