package fr.insee.pogues.metadata;

import fr.insee.pogues.metadata.client.MetadataClient;
import fr.insee.pogues.metadata.client.MetadataClientImpl;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.entity.StringEntity;
import org.json.simple.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;

public class TestMetadataClient {

    @Mock
    HttpClient httpClient;

    @InjectMocks
    MetadataClient metadataClient;

    @Before
    public void beforeEach(){
        metadataClient = spy(new MetadataClientImpl());
        initMocks(this);
    }

    @Test
    public void testClientGet() throws Exception {
        String expectedResponse = "{\"id\":\"foo\"}";
        HttpResponse response = mock(HttpResponse.class);
        StatusLine statusLine = mock(StatusLine.class);
        when(statusLine.getStatusCode()).thenReturn(200);
        when(response.getStatusLine()).thenReturn(statusLine);
        when(response.getEntity()).thenReturn(new StringEntity(expectedResponse));
        when(httpClient.execute(any())).thenReturn(response);
        JSONObject actualResponse = metadataClient.getItem("foo");
        assertEquals("foo", actualResponse.get("id").toString());
    }
}

@@ -165,16 +165,75 @@
 he.http.
+StatusLine;%0Aimport org.apache.http.client.methods.Closeable
 HttpResp
@@ -257,34 +257,43 @@
 apache.http.
-StatusLine
+entity.StringEntity
 ;%0Aimport org
@@ -305,23 +305,37 @@
 he.http.
+impl.
 client.
+Closeable
 HttpClie
@@ -361,35 +361,45 @@
 he.http.
-entity.StringEntity
+impl.client.HttpClientBuilder
 ;%0Aimport
@@ -775,19 +775,29 @@
 ient
- httpClient
+Builder clientFactory
 ;%0A%0A 
@@ -1097,24 +1097,33 @@
 %7D%22;%0A        
+Closeable
 HttpResponse
@@ -1139,16 +1139,25 @@
  = mock(
+Closeable
 HttpResp
@@ -1217,32 +1217,183 @@
 tusLine.class);%0A
+        CloseableHttpClient httpClient = mock(CloseableHttpClient.class);%0A        when(clientFactory.build())%0A                .thenReturn(httpClient);%0A
         when(sta

Fix unit test metadata client