package doctester;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.hamcrest.core.StringContains.containsString;

import com.google.common.io.Files;

import org.doctester.testbrowser.Request;
import org.doctester.testbrowser.Response;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset;

import model.BaseModel;

import ninja.NinjaDocTester;

public class ResourceControllerTest extends NinjaDocTester {

  String RESOURCE_CONTROLLER_BASE_URL = "/api/resource/";
  String POST_UPLOAD_RESOURCE_URL = RESOURCE_CONTROLLER_BASE_URL + "upload";
  String GET_RESOURCE_URL = RESOURCE_CONTROLLER_BASE_URL + "{id}/{format}";
  String THUMBNAIL_FORMAT = "thumbnail";
  String ORIGINAL_FORMAT = "original";

  URL url;
  File file;

  @Before
  public void setup() throws URISyntaxException {
    url = this.getClass().getResource("/fileupload.png");
    file = new File(url.toURI());
  }

  @Test
  public void testResourceUpload() throws URISyntaxException {
    sayNextSection("Upload a resource");
    say("Uploading a resource is a POST request to " + POST_UPLOAD_RESOURCE_URL);

    Response response =
        sayAndMakeRequest(Request.POST().url(testServerUrl().path(POST_UPLOAD_RESOURCE_URL))
            .addFileToUpload("testfile", file));
    BaseModel baseModel = response.payloadJsonAs(BaseModel.class);

    sayAndAssertThat("We get back an json containing the id of the uploaded resource",
        baseModel.getId(), notNullValue());
  }

  @Test
  public void testGetResourceInOriginalFormat() throws URISyntaxException, IOException {
    sayNextSection("Download a resource in " + ORIGINAL_FORMAT + " format");
    say("On upload of a file the resource id of the file is returned.");
    Response uploadResponse = uploadFile(file);
    BaseModel baseModel = uploadResponse.payloadJsonAs(BaseModel.class);
    String id = baseModel.getId().toString();

    say("Downloading a resource in " + ORIGINAL_FORMAT + " fileformat is a GET request to "
        + GET_RESOURCE_URL);
    Response response =
        sayAndMakeRequest(Request.GET().url(
            testServerUrl().path(
                GET_RESOURCE_URL.replace("{id}", id).replace("{format}", ORIGINAL_FORMAT))));

    String content = response.payload;
    String expected = Files.toString(file, Charset.defaultCharset());

    sayAndAssertThat("We get the resource back in " + ORIGINAL_FORMAT + " format", content,
        equalTo(expected));
  }

  @Test
  public void testGetResourceInThumbnailFormat() throws URISyntaxException, IOException {
    sayNextSection("Download a resource in " + THUMBNAIL_FORMAT + " format");
    say("On upload of a file the resource id of the file is returned.");
    Response uploadResponse = uploadFile(file);
    BaseModel baseModel = uploadResponse.payloadJsonAs(BaseModel.class);
    String id = baseModel.getId().toString();

    say("Downloading a resource in " + THUMBNAIL_FORMAT + " fileformat is a GET request to "
        + GET_RESOURCE_URL);
    Response response =
        sayAndMakeRequest(Request.GET().url(
            testServerUrl().path(
                GET_RESOURCE_URL.replace("{id}", id).replace("{format}", THUMBNAIL_FORMAT))));

    sayAndAssertThat("We get the resource back in " + THUMBNAIL_FORMAT + " format",
        response.payload, containsString("PNG"));
  }

  private Response uploadFile(File file) throws URISyntaxException {

    Response response =
        sayAndMakeRequest(Request.POST().url(testServerUrl().path(POST_UPLOAD_RESOURCE_URL))
            .addFileToUpload("testfile", file));

    return response;
  }
}

@@ -215,16 +215,64 @@
 Files;%0A%0A
+import org.doctester.testbrowser.HttpConstants;%0A
 import o
@@ -944,16 +944,57 @@
 iginal%22;
+%0A  String CONTENT_TYPE_PNG = %22image/png%22;
 %0A%0A  URL 
@@ -3435,24 +3435,106 @@
 _FORMAT))));
+%0A    String contentType = response.headers.get(HttpConstants.HEADER_CONTENT_TYPE);
 %0A%0A    sayAnd
@@ -3653,24 +3653,152 @@
 ng(%22PNG%22));%0A
+    sayAndAssertThat(%22We get a response header with png content type%22, contentType,%0A        containsString(CONTENT_TYPE_PNG));%0A%0A
   %7D%0A%0A  priva

check for content type