
package net.contargo.iris.api;

import org.junit.Test;

import org.springframework.http.HttpStatus;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import org.springframework.web.client.HttpClientErrorException;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.hamcrest.MatcherAssert.assertThat;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;


/**
 * Unit test for {@link PublicAPIExceptionHandler}.
 *
 * @author  Aljona Murygina - murygina@synyx.de
 */
public class PublicAPIExceptionHandlerUnitTest {

    private final PublicAPIExceptionHandler sut;

    private final HttpServletRequest request;
    private final MockHttpServletResponse response;

    public PublicAPIExceptionHandlerUnitTest() {

        sut = new PublicAPIExceptionHandler();

        request = new MockHttpServletRequest();
        response = new MockHttpServletResponse();
    }

    @Test
    public void resolveIllegalArgumentException() {

        String exMessage = "Invalid ID for Foo";

        sut.resolveException(request, response, sut, new IllegalArgumentException(exMessage));

        assertThat(response.getErrorMessage(), containsString(exMessage));
        assertThat(response.getStatus(), equalTo(HttpServletResponse.SC_BAD_REQUEST));
    }


    @Test
    public void resolveIllegalStateException() {

        String exMessage = "Invalid state foo";

        sut.resolveException(request, response, sut, new IllegalStateException(exMessage));

        assertThat(response.getErrorMessage(), containsString(exMessage));
        assertThat(response.getStatus(), equalTo(HttpServletResponse.SC_BAD_REQUEST));
    }


    @Test
    public void resolveHttpClientErrorException() {

        String exMessage = "Nominatim down";

        sut.resolveException(request, response, sut,
            new HttpClientErrorException(HttpStatus.SERVICE_UNAVAILABLE, exMessage));

        assertThat(response.getErrorMessage(), containsString(exMessage));
        assertThat(response.getErrorMessage(),
            containsString("Service is temporary not available, please try again later"));
        assertThat(response.getStatus(), equalTo(HttpServletResponse.SC_SERVICE_UNAVAILABLE));
    }


    @Test
    public void resolveNullPointerException() {

        String exMessage = "This should never happen";

        // NullpointerException must be thrown for test purposes
        sut.resolveException(request, response, sut, new NullPointerException(exMessage));

        assertThat(response.getErrorMessage(), containsString(exMessage));
        assertThat(response.getStatus(), equalTo(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
    }


    @Test
    public void resolveRuntimeException() {

        String exMessage = "This should never happen";

        // Runtimeexception must be thrown for test purposes
        sut.resolveException(request, response, sut, new RuntimeException(exMessage));

        assertThat(response.getErrorMessage(), containsString(exMessage));
        assertThat(response.getStatus(), equalTo(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
    }


    @Test
    public void exceptionWhileResolvingGetsCaught() throws IOException {

        String exMessage = "Invalid state foo";
        HttpServletResponse responseMock = mock(HttpServletResponse.class);

        doThrow(new IOException()).when(responseMock)
            .sendError(HttpServletResponse.SC_BAD_REQUEST, "Bad request. " + exMessage);
        sut.resolveException(request, responseMock, sut, new IllegalStateException(exMessage));
    }
}

@@ -641,16 +641,242 @@
 .mock;%0A%0A
+import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;%0Aimport static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;%0Aimport static javax.servlet.http.HttpServletResponse.SC_SERVICE_UNAVAILABLE;%0A%0A
 %0A/**%0A * 
@@ -1714,36 +1714,16 @@
 equalTo(
-HttpServletResponse.
 SC_BAD_R
@@ -2066,36 +2066,16 @@
 equalTo(
-HttpServletResponse.
 SC_BAD_R
@@ -2603,36 +2603,16 @@
 equalTo(
-HttpServletResponse.
 SC_SERVI
@@ -3033,36 +3033,16 @@
 equalTo(
-HttpServletResponse.
 SC_INTER
@@ -3453,36 +3453,16 @@
 equalTo(
-HttpServletResponse.
 SC_INTER
@@ -3755,52 +3755,19 @@
 ock)
-%0A            .sendError(HttpServletResponse.
+.sendError(
 SC_B

add static imports for HttpServletResponse