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 org.springframework.web.HttpRequestMethodNotSupportedException;

import com.jayway.restassured.builder.ResponseSpecBuilder;
import com.jayway.restassured.specification.ResponseSpecification;

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

public class ErrorHandlingITest extends AbstractIntegrationTest {
    @Test
    public void notFound() {
        when()
            .get(UrlSchema.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(UrlSchema.FACILITIES)
        .then()
            .spec(assertResponse(HttpStatus.BAD_REQUEST, ValidationException.class))
        ;
    }

    @Test
    public void httpMessageNotReadableException() {
        given()
            .header("Content-Type", "application/json;charset=UTF-8")
            .body("{ \"name\": \"foo\", \"location\": \"invalid location\"  }")
        .when()
            .post(UrlSchema.FACILITIES)
        .then()
            .spec(assertResponse(HttpStatus.BAD_REQUEST, HttpMessageNotReadableException.class))
        ;
    }

    @Test
    public void httpRequestMethodNotSupportedException() {
        given()
            .header("Content-Type", "application/json;charset=UTF-8")
            .body(new Facility())
        .when()
            .put(UrlSchema.FACILITIES)
        .then()
            .spec(assertResponse(HttpStatus.BAD_REQUEST, HttpRequestMethodNotSupportedException.class))
        ;
    }

    // HttpMessageNotReadableException_jsonMappingException: unclear how to trigger
    // HttpMediaTypeException: unclear how to trigger
    // bindException: unclear how to trigger
    // exception: unclear how to trigger

    private static ResponseSpecification assertResponse(HttpStatus status, Class<?> exClass) {
        return new ResponseSpecBuilder()
                .expectStatusCode(status.value())
                .expectBody("status", is(status.value()))
                .expectBody("exception", is(exClass.getCanonicalName()))
                .build();
    }
}

@@ -434,33 +434,157 @@
 ilder.Re
-sponseSpecBuilder
+questSpecBuilder;%0Aimport com.jayway.restassured.builder.ResponseSpecBuilder;%0Aimport com.jayway.restassured.specification.RequestSpecification
 ;%0Aimport
@@ -1144,79 +1144,20 @@
 iven
-()%0A            .header(%22Content-Type%22, %22application/json;charset=UTF-8%22
+WithContent(
 )%0A  
@@ -1441,79 +1441,20 @@
 iven
-()%0A            .header(%22Content-Type%22, %22application/json;charset=UTF-8%22
+WithContent(
 )%0A  
@@ -1803,79 +1803,20 @@
 iven
-()%0A            .header(%22Content-Type%22, %22application/json;charset=UTF-8%22
+WithContent(
 )%0A  
@@ -2014,32 +2014,103 @@
 ception.class))%0A
+            .body(%22message%22, is(%22Request method 'PUT' not supported%22))%0A
         ;%0A    %7D%0A
@@ -2289,24 +2289,24 @@
  to trigger%0A
-
     // excep
@@ -2335,16 +2335,241 @@
 rigger%0A%0A
+    private static RequestSpecification givenWithContent() %7B%0A        return given().spec(new RequestSpecBuilder()%0A                .addHeader(%22Content-Type%22, %22application/json;charset=UTF-8%22)%0A                .build());%0A    %7D%0A%0A
     priv

refactor request headers