package com.razorpay;

import okhttp3.*;
import okio.Buffer;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.json.JSONObject;
import org.junit.Before;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.anyObject;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class BaseTest {

    private OkHttpClient okHttpClient;
    Response mockedResponse;

    static final String TEST_SECRET_KEY = "test";

    @Before
    public void setUp() throws Exception {

        MockitoAnnotations.initMocks(this);
        mockGetCall();

    }

    private void mockGetCall() throws IOException, IllegalAccessException {

        okHttpClient = mock(OkHttpClient.class);
        mockedResponse = mock(Response.class);
        Field clientField =FieldUtils.getDeclaredField(ApiUtils.class,"client",true);
        FieldUtils.writeField(clientField,new ApiUtils(),okHttpClient);

        Call call = mock(Call.class);
        when(call.execute()).thenReturn(mockedResponse);
        when(okHttpClient.newCall(anyObject())).thenReturn(call);

    }

    protected void mockResponseHTTPCodeFromExternalClient(int code)
    {
        when(mockedResponse.code()).thenReturn(code);
    }
    protected void mockURL(List<String> urlString)
    {
        HttpUrl url = mock(HttpUrl.class);
        when(url.pathSegments()).thenReturn(urlString);
        Request request = mock(Request.class);
        when(request.url()).thenReturn(url);
        when(mockedResponse.request()).thenReturn(request);
    }
    protected void mockResponseFromExternalClient(String response) throws IOException {
        JSONObject parse = new JSONObject(response);
        ResponseBody rb = mock(ResponseBody.class);
        when(rb.string()).thenReturn(parse.toString());
        when(mockedResponse.body()).thenReturn(rb);
    }

    protected OkHttpClient getOkHttpClient()
    {
        return okHttpClient;
    }

    public void verifySentRequest(boolean hasBody, String request, String requestPath) {
        ArgumentCaptor<Request> req = ArgumentCaptor.forClass(Request.class);
        Mockito.verify(getOkHttpClient()).newCall(req.capture());
        if(hasBody) {
            assertEquals(new JSONObject(request).toString(), new JSONObject(bodyToString(req.getAllValues().get(0))).toString());
        }
        assertEquals(requestPath, req.getValue().url().toString());
    }

    private static String bodyToString(final Request request) {

        try {
            final Request copy = request.newBuilder().build();
            final Buffer buffer = new Buffer();
            copy.body().writeTo(buffer);
            return buffer.readUtf8();
        } catch (final IOException e) {
            return "did not work";
        }
    }

    String getHost(String url) {
        return Constants.SCHEME + "://" + Constants.HOSTNAME + "/" + Constants.VERSION + "/" + url;
    }
}
@@ -38,28 +38,8 @@
 .*;%0A
-import okio.Buffer;%0A
 impo
@@ -82,24 +82,24 @@
 FieldUtils;%0A
+
 import org.j
@@ -143,43 +143,8 @@
 re;%0A
-import org.mockito.ArgumentCaptor;%0A
 impo
@@ -175,36 +175,8 @@
 ks;%0A
-import org.mockito.Mockito;%0A
 impo
@@ -299,53 +299,8 @@
 t;%0A%0A
-import static org.junit.Assert.assertEquals;%0A
 impo
@@ -2052,977 +2052,6 @@
    %7D
-%0A%0A    public void verifySentRequest(boolean hasBody, String request, String requestPath) %7B%0A        ArgumentCaptor%3CRequest%3E req = ArgumentCaptor.forClass(Request.class);%0A        Mockito.verify(getOkHttpClient()).newCall(req.capture());%0A        if(hasBody) %7B%0A            assertEquals(new JSONObject(request).toString(), new JSONObject(bodyToString(req.getAllValues().get(0))).toString());%0A        %7D%0A        assertEquals(requestPath, req.getValue().url().toString());%0A    %7D%0A%0A    private static String bodyToString(final Request request) %7B%0A%0A        try %7B%0A            final Request copy = request.newBuilder().build();%0A            final Buffer buffer = new Buffer();%0A            copy.body().writeTo(buffer);%0A            return buffer.readUtf8();%0A        %7D catch (final IOException e) %7B%0A            return %22did not work%22;%0A        %7D%0A    %7D%0A%0A    String getHost(String url) %7B%0A        return Constants.SCHEME + %22://%22 + Constants.HOSTNAME + %22/%22 + Constants.VERSION + %22/%22 + url;%0A    %7D
 %0A%7D

revert basetest