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 static org.hamcrest.Matchers.startsWith;

import java.io.IOException;

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

import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.common.base.Charsets;
import com.google.common.io.Resources;

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()
            .statusCode(HttpStatus.BAD_REQUEST.value())
            .body("status", is(HttpStatus.BAD_REQUEST.value()))
            .body("exception", is(ValidationException.class.getCanonicalName()))
        ;
    }

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

    @Test
    public void httpRequestMethodNotSupportedException() {
        given()
            .log().all()
            .header("Content-Type", "application/json;charset=UTF-8")
            .body(new Facility())
        .when()
            .put(UrlSchema.FACILITIES)
        .then()
            .log().all()
            .statusCode(HttpStatus.BAD_REQUEST.value())
            .body("status", is(HttpStatus.BAD_REQUEST.value()))
            .body("exception", is(HttpRequestMethodNotSupportedException.class.getCanonicalName()))
        ;
    }

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

@@ -182,85 +182,8 @@
 .is;
-%0Aimport static org.hamcrest.Matchers.startsWith;%0A%0Aimport java.io.IOException;
 %0A%0Aim
@@ -409,134 +409,121 @@
 com.
-fasterxml.jackson.databind.JsonMappingException;%0Aimport com.google.common.base.Charsets;%0Aimport com.google.common.
+jayway.restassured.builder.ResponseSpecBuilder;%0Aimport com.jayway.restassured.specificat
 io
+n
 .Res
-ources
+ponseSpecification
 ;%0A%0Ai
@@ -1213,148 +1213,51 @@
   .s
-tatusCode(HttpStatus.BAD_REQUEST.value())%0A            .body(%22status%22, is(HttpStatus.BAD_REQUEST.value()))%0A            .body(%22exception%22, is(
+pec(assertResponse(HttpStatus.BAD_REQUEST, 
 Vali
@@ -1277,35 +1277,16 @@
 on.class
-.getCanonicalName()
 ))%0A     
@@ -1615,148 +1615,51 @@
   .s
-tatusCode(HttpStatus.BAD_REQUEST.value())%0A            .body(%22status%22, is(HttpStatus.BAD_REQUEST.value()))%0A            .body(%22exception%22, is(
+pec(assertResponse(HttpStatus.BAD_REQUEST, 
 Http
@@ -1691,35 +1691,16 @@
 on.class
-.getCanonicalName()
 ))%0A     
@@ -1800,33 +1800,8 @@
 n()%0A
-            .log().all()%0A
     
@@ -1988,174 +1988,52 @@
    .
-log().all()%0A            .statusCode(HttpStatus.BAD_REQUEST.value())%0A            .body(%22status%22, is(HttpStatus.BAD_REQUEST.value()))%0A            .body(%22exception%22, is(
+spec(assertResponse(HttpStatus.BAD_REQUEST, 
 Http
@@ -2076,27 +2076,8 @@
 lass
-.getCanonicalName()
 ))%0A 
@@ -2315,11 +2315,361 @@
  trigger
+%0A%0A    private static ResponseSpecification assertResponse(HttpStatus status, Class%3C?%3E exClass) %7B%0A        return new ResponseSpecBuilder()%0A                .expectStatusCode(status.value())%0A                .expectBody(%22status%22, is(status.value()))%0A                .expectBody(%22exception%22, is(exClass.getCanonicalName()))%0A                .build();%0A    %7D
 %0A%7D%0A

refactor response expectations