
package net.contargo.iris.api;

import org.hamcrest.Matchers;

import org.junit.Assert;
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.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));

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


    @Test
    public void resolveIllegalStateException() {

        String exMessage = "Invalid state foo";

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

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


    @Test
    public void resolveHttpClientErrorException() {

        String exMessage = "Nominatim down";

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

        Assert.assertThat(response.getErrorMessage(), Matchers.containsString(exMessage));
        Assert.assertThat(response.getErrorMessage(),
            Matchers.containsString("Service is temporary not available, please try again later"));
        Assert.assertThat(response.getStatus(), Matchers.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));

        Assert.assertThat(response.getErrorMessage(), Matchers.containsString(exMessage));
        Assert.assertThat(response.getStatus(), Matchers.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));

        Assert.assertThat(response.getErrorMessage(), Matchers.containsString(exMessage));
        Assert.assertThat(response.getStatus(), Matchers.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));
    }
}

@@ -30,64 +30,8 @@
 i;%0A%0A
-import org.hamcrest.Matchers;%0A%0Aimport org.junit.Assert;%0A
 impo
@@ -49,16 +49,16 @@
 t.Test;%0A
+
 %0Aimport 
@@ -401,24 +401,176 @@
 tResponse;%0A%0A
+import static org.hamcrest.MatcherAssert.assertThat;%0A%0Aimport static org.hamcrest.Matchers.containsString;%0Aimport static org.hamcrest.Matchers.equalTo;%0A%0A
 import stati
@@ -1045,24 +1045,25 @@
 nHandler();%0A
+%0A
         requ
@@ -1364,39 +1364,32 @@
 age));%0A%0A        
-Assert.
 assertThat(respo
@@ -1407,33 +1407,24 @@
 rMessage(), 
-Matchers.
 containsStri
@@ -1439,39 +1439,32 @@
 sage));%0A        
-Assert.
 assertThat(respo
@@ -1476,33 +1476,24 @@
 etStatus(), 
-Matchers.
 equalTo(Http
@@ -1736,39 +1736,32 @@
 age));%0A%0A        
-Assert.
 assertThat(respo
@@ -1779,33 +1779,24 @@
 rMessage(), 
-Matchers.
 containsStri
@@ -1811,39 +1811,32 @@
 sage));%0A        
-Assert.
 assertThat(respo
@@ -1848,33 +1848,24 @@
 etStatus(), 
-Matchers.
 equalTo(Http
@@ -2155,39 +2155,32 @@
 age));%0A%0A        
-Assert.
 assertThat(respo
@@ -2198,33 +2198,24 @@
 rMessage(), 
-Matchers.
 containsStri
@@ -2230,39 +2230,32 @@
 sage));%0A        
-Assert.
 assertThat(respo
@@ -2289,25 +2289,16 @@
         
-Matchers.
 contains
@@ -2368,39 +2368,32 @@
 ter%22));%0A        
-Assert.
 assertThat(respo
@@ -2405,33 +2405,24 @@
 etStatus(), 
-Matchers.
 equalTo(Http
@@ -2743,39 +2743,32 @@
 age));%0A%0A        
-Assert.
 assertThat(respo
@@ -2786,33 +2786,24 @@
 rMessage(), 
-Matchers.
 containsStri
@@ -2818,39 +2818,32 @@
 sage));%0A        
-Assert.
 assertThat(respo
@@ -2855,33 +2855,24 @@
 etStatus(), 
-Matchers.
 equalTo(Http
@@ -3183,39 +3183,32 @@
 age));%0A%0A        
-Assert.
 assertThat(respo
@@ -3230,25 +3230,16 @@
 sage(), 
-Matchers.
 contains
@@ -3266,23 +3266,16 @@
         
-Assert.
 assertTh
@@ -3299,25 +3299,16 @@
 atus(), 
-Matchers.
 equalTo(
@@ -3625,16 +3625,29 @@
 nseMock)
+%0A            
 .sendErr
@@ -3684,28 +3684,16 @@
 REQUEST,
-%0A           
  %22Bad re

use hemcrest at test and static imports for better readability