package fi.hsl.parkandride.itest;

import static com.jayway.restassured.RestAssured.given;
import static com.jayway.restassured.RestAssured.when;
import static org.hamcrest.Matchers.is;

import org.junit.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;

import fi.hsl.parkandride.core.domain.Facility;
import fi.hsl.parkandride.core.service.ValidationException;

public class ErrorHandlingITest extends AbstractIntegrationTest {
    @Test
    public void notFound() {
        when()
            .get("api/v1/facilities/42")
        .then()
            .statusCode(HttpStatus.NOT_FOUND.value())
            .body(is(""))
        ;
    }

    @Test
    public void validationException() {
        given()
            .header("Content-Type", "application/json;charset=UTF-8")
            .body(new Facility())
        .when()
            .post("/api/v1/facilities")
        .then()
            .statusCode(HttpStatus.BAD_REQUEST.value())
            .body("status", is(HttpStatus.BAD_REQUEST.value()))
            .body("exception", is(ValidationException.class.getCanonicalName()))
        ;
    }

    @Test
    public void jsonException() {
        given()
            .header("Content-Type", "application/json;charset=UTF-8")
            .body("{ \"name\": \"foo\", \"location\": \"bar\"  }")
        .when()
            .post("/api/v1/facilities")
        .then()
            .statusCode(HttpStatus.BAD_REQUEST.value())
            .body("status", is(HttpStatus.BAD_REQUEST.value()))
            .body("exception", is(HttpMessageNotReadableException.class.getCanonicalName()))
        ;
    }

    // bindException
    // methodNotSupportedException
    // exception
}

@@ -178,16 +178,93 @@
 hers.is;
+%0Aimport static org.hamcrest.Matchers.startsWith;%0A%0Aimport java.io.IOException;
 %0A%0Aimport
@@ -390,32 +390,243 @@
 adableException;
+%0Aimport org.springframework.web.HttpRequestMethodNotSupportedException;%0A%0Aimport com.fasterxml.jackson.databind.JsonMappingException;%0Aimport com.google.common.base.Charsets;%0Aimport com.google.common.io.Resources;
 %0A%0Aimport fi.hsl.
@@ -1480,20 +1480,38 @@
 ic void 
-json
+httpMessageNotReadable
 Exceptio
@@ -1662,11 +1662,24 @@
 : %5C%22
-bar
+invalid location
 %5C%22  
@@ -1992,75 +1992,772 @@
     
-// bindException%0A    // methodNotSupportedException%0A    // exception
+@Test%0A    public void httpRequestMethodNotSupportedException() %7B%0A        given()%0A            .log().all()%0A            .header(%22Content-Type%22, %22application/json;charset=UTF-8%22)%0A            .body(new Facility())%0A        .when()%0A            .put(%22api/v1/facilities%22)%0A        .then()%0A            .log().all()%0A            .statusCode(HttpStatus.BAD_REQUEST.value())%0A            .body(%22status%22, is(HttpStatus.BAD_REQUEST.value()))%0A            .body(%22exception%22, is(HttpRequestMethodNotSupportedException.class.getCanonicalName()))%0A        ;%0A    %7D%0A%0A    // HttpMessageNotReadableException_jsonMappingException: unclear how to trigger%0A    // HttpMediaTypeException: unclear how to trigger%0A    // bindException: unclear how to trigger%0A    // exception: unclear how to trigger
 %0A%7D%0A

test error handling, HttpMessageNotReadableException