Newer
Older
fedex-api / src / test / java / io / ljunggren / fedex / api / service / AddressServiceTest.java
package io.ljunggren.fedex.api.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
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.address.request.ValidationRequest;
import io.ljunggren.fedex.api.model.address.response.ValidationResponse;

public class AddressServiceTest {

    private final AddressService addressService = new AddressService("https://example.com");

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

        ValidationResponse response = addressService.validate(new ValidationRequest(), "token", httpClient);

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

    @Test
    public void validateKnownErrorCodeIsParsedTest() throws Exception {
        CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
        CloseableHttpResponse httpResponse = HttpTestUtils.mockResponse(403,
                "{\"errors\":[{\"code\":\"FORBIDDEN\",\"message\":\"no access\"}]}");
        when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(httpResponse);

        ValidationResponse response = addressService.validate(new ValidationRequest(), "token", httpClient);

        assertEquals(403, response.getResponseCode());
        assertEquals("FORBIDDEN", response.getErrors().get(0).getCode());
    }

    @Test
    public void validateUnknownCodeProducesGenericErrorTest() throws Exception {
        CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
        CloseableHttpResponse httpResponse = HttpTestUtils.mockResponse(418, "teapot");
        when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(httpResponse);

        ValidationResponse response = addressService.validate(new ValidationRequest(), "token", httpClient);

        assertEquals(418, response.getResponseCode());
        assertNull(response.getErrors().get(0).getCode());
        assertEquals("teapot", response.getErrors().get(0).getMessage());
    }

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

        ValidationResponse response = addressService.validate(new ValidationRequest(), "token", httpClient);

        assertEquals("Validation Error", response.getErrors().get(0).getCode());
        assertEquals("reset", response.getErrors().get(0).getMessage());
    }

}