package com.wikia.mwapi;

import com.wikia.mwapi.domain.ApiResponse;

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 {
    MWApi mwApi = Mockito.mock(MWApi.class);
    Mockito.when(mwApi.get()).thenCallRealMethod();
    Mockito.when(mwApi.wikia(Mockito.anyString())).thenCallRealMethod();
    Mockito.when(mwApi.queryAction()).thenCallRealMethod();
    Mockito.when(mwApi.titles(Mockito.anyString())).thenCallRealMethod();

    InputStream jsonStream = new ByteArrayInputStream(
        FixtureHelpers.fixture("fixtures/kermit-the-frog-title.json").getBytes(StandardCharsets.UTF_8)
    );
    Mockito.when(mwApi.handleMWRequest(Mockito.anyString())).thenReturn(jsonStream);

    ApiResponse response = mwApi
        .wikia("muppet")
        .queryAction()
        .titles("Kermit the Frog")
        .get();
    assertEquals("Kermit the Frog", response.getQuery().getFirstPage().getTitle());
    assertEquals(50, response.getQuery().getFirstPage().getPageId());
  }


}
@@ -63,16 +63,266 @@
 ponse;%0A%0A
+import org.apache.http.HttpEntity;%0Aimport org.apache.http.HttpResponse;%0Aimport org.apache.http.client.HttpClient;%0Aimport org.apache.http.client.methods.HttpGet;%0Aimport org.apache.http.client.methods.HttpUriRequest;%0Aimport org.mockito.ArgumentCaptor;%0A
 import o
@@ -956,305 +956,425 @@
     
-MWApi mwApi = Mockito.mock(MWApi.class);%0A    Mockito.when(mwApi.get()).thenCallRealMethod
+String wikia = %22muppet%22;%0A    String title = %22Kermit the Frog%22;%0A    String url = MWApi.createBuilder
 ()
-;
 %0A    
-Mockito.when(mwApi.wikia(Mockito.anyString())).thenCallRealMethod();%0A    Mockito.when(mwApi.queryAction()).thenCallRealMethod();%0A    Mockito.when(mwApi.titles(Mockito.anyString())).thenCallRealMethod(
+    .wikia(wikia)%0A        .queryAction()%0A        .titles(title)%0A        .url();%0A    HttpUriRequest request = new HttpGet(url);%0A%0A    HttpClient httpClient = Mockito.mock(HttpClient.class);%0A    HttpResponse httpResponse = Mockito.mock(HttpResponse.class);%0A    HttpEntity httpEntity = Mockito.mock(HttpEntity.class
 );%0A%0A
@@ -1559,48 +1559,192 @@
 hen(
-mwApi.handleMWRequest(Mockito.anyString(
+httpEntity.getContent()).thenReturn(jsonStream);%0A    Mockito.when(httpResponse.getEntity()).thenReturn(httpEntity);%0A    Mockito.when(httpClient.execute(Mockito.any(HttpUriRequest.class
 ))).
@@ -1754,26 +1754,28 @@
 nReturn(
-jsonStream
+httpResponse
 );%0A%0A    
@@ -1801,13 +1801,39 @@
 e = 
-mw
+MW
 Api
+.createBuilder(httpClient)
 %0A   
@@ -1840,32 +1840,29 @@
      .wikia(
-%22muppet%22
+wikia
 )%0A        .q
@@ -1890,33 +1890,21 @@
 .titles(
-%22Kermit the Frog%22
+title
 )%0A      
@@ -2071,11 +2071,229 @@
 ));%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%7D
+%0A

Fix the MWApi test