package io.ljunggren.fedex.api.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.junit.Test;

import io.ljunggren.fedex.api.model.rate.request.RateRequest;
import io.ljunggren.fedex.api.model.rate.response.RateResponse;

public class RateServiceTest {

    private final RateService rateService = new RateService("https://example.com");

    @Test
    public void requestSuccessTest() throws Exception {
        CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
        CloseableHttpResponse httpResponse = HttpTestUtils.mockResponse(200, "{\"transactionId\":\"tx-1\"}");
        when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(httpResponse);

        RateResponse response = rateService.request(new RateRequest(), "token", httpClient);

        assertEquals("tx-1", response.getTransactionId());
        assertEquals(200, response.getResponseCode());
    }

    @Test
    public void requestKnownErrorCodeIsParsedTest() throws Exception {
        CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
        CloseableHttpResponse httpResponse = HttpTestUtils.mockResponse(400,
                "{\"errors\":[{\"code\":\"BAD.REQUEST\",\"message\":\"bad input\"}]}");
        when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(httpResponse);

        RateResponse response = rateService.request(new RateRequest(), "token", httpClient);

        assertEquals(400, response.getResponseCode());
        assertEquals(1, response.getErrors().size());
        assertEquals("BAD.REQUEST", response.getErrors().get(0).getCode());
    }

    @Test
    public void requestUnknownCodeProducesGenericErrorTest() throws Exception {
        CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
        CloseableHttpResponse httpResponse = HttpTestUtils.mockResponse(404, "not found");
        when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(httpResponse);

        RateResponse response = rateService.request(new RateRequest(), "token", httpClient);

        assertEquals(404, response.getResponseCode());
        assertEquals(1, response.getErrors().size());
        assertNull(response.getErrors().get(0).getCode());
        assertEquals("not found", response.getErrors().get(0).getMessage());
    }

    @Test
    public void requestThrottlingCodeIsParsedTest() throws Exception {
        CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
        CloseableHttpResponse httpResponse = HttpTestUtils.mockResponse(429,
                "{\"errors\":[{\"code\":\"TOO.MANY.REQUESTS\",\"message\":\"slow down\"}]}");
        when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(httpResponse);

        RateResponse response = rateService.request(new RateRequest(), "token", httpClient);

        assertEquals(429, response.getResponseCode());
        assertEquals("TOO.MANY.REQUESTS", response.getErrors().get(0).getCode());
    }

    @Test
    public void requestExceptionIsCapturedAsErrorTest() throws Exception {
        CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
        when(httpClient.execute(any(HttpUriRequest.class))).thenThrow(new java.io.IOException("connection refused"));

        RateResponse response = rateService.request(new RateRequest(), "token", httpClient);

        assertTrue(response.getErrors().size() == 1);
        assertEquals("Rate Error", response.getErrors().get(0).getCode());
        assertEquals("connection refused", response.getErrors().get(0).getMessage());
    }

}
