package uk.gov.register.presentation.functional;

import com.fasterxml.jackson.databind.JsonNode;
import io.dropwizard.jackson.Jackson;
import org.apache.commons.codec.digest.DigestUtils;
import org.json.JSONException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.Before;
import org.junit.Test;

import javax.ws.rs.core.Response;
import java.io.IOException;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;

public class EntryResourceFunctionalTest extends FunctionalTestBase {
    private static final String item1 = "{\"address\":\"6789\",\"street\":\"presley\"}";
    private static final String item2 = "{\"address\":\"6789\",\"street\":\"presley\"}";

    @Before
    public void publishTestMessages() throws Throwable {
        mintItems(item1, item2);
    }

    @Test
    public void getEntryByEntryNumber() throws JSONException, IOException {
        String sha256Hex = DigestUtils.sha256Hex(item1);

        Response response = getRequest("/entry/1.json");

        assertThat(response.getStatus(), equalTo(200));

        JsonNode res = Jackson.newObjectMapper().readValue(response.readEntity(String.class), JsonNode.class);

        assertThat(res.get("entry-number").textValue(), equalTo("1"));
        assertThat(res.get("item-hash").textValue(), equalTo("sha-256:" + sha256Hex));
        assertTrue(res.get("entry-timestamp").textValue().matches("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$"));
    }

    @Test
    public void entryView_itemHashIsRenderedAsALink() {
        String sha256Hex = DigestUtils.sha256Hex(item1);

        Response response = getRequest("/entry/1");
        Document doc = Jsoup.parse(response.readEntity(String.class));
        String text = doc.getElementsByTag("table").select("a[href=/item/sha-256:" + sha256Hex + "]").first().text();
        assertThat(text, equalTo("sha-256:" + sha256Hex));
    }


    @Test
    public void return404ResponseWhenEntryNotExist() {
        assertThat(getRequest("/entry/5001").getStatus(), equalTo(404));
    }

    @Test
    public void return404ResponseWhenEntryNumberIsNotAnIntegerValue() {
        assertThat(getRequest("/entry/a2").getStatus(), equalTo(404));
    }
}

@@ -379,16 +379,164 @@
 ception;
+%0Aimport java.time.Instant;%0Aimport java.time.ZoneId;%0Aimport java.time.ZonedDateTime;%0Aimport java.time.format.DateTimeFormatter;%0Aimport java.util.Map;
 %0A%0Aimport
@@ -2116,17 +2116,16 @@
 %0A    %7D%0A%0A
-%0A
     @Tes
@@ -2339,24 +2339,24 @@
 erValue() %7B%0A
-
         asse
@@ -2419,11 +2419,802 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void entryResource_retrieviesTimestampsInUTC() throws IOException %7B%0A        String dateTimeWithNoSeconds = %22yyyyMMddHHmm%22;%0A        String expectedDateTime = formatInstant(Instant.now(), dateTimeWithNoSeconds);%0A%0A        Response response = getRequest(%22/entry/1.json%22);%0A%0A        Map%3CString, String%3E responseData = response.readEntity(Map.class);%0A        Instant entryTimestamp = Instant.parse(responseData.get(%22entry-timestamp%22));%0A        String actualDateTime = formatInstant(entryTimestamp, dateTimeWithNoSeconds);%0A%0A        assertThat(actualDateTime, equalTo(expectedDateTime));%0A    %7D%0A%0A    private String formatInstant(Instant instant, String format)%7B%0A        return ZonedDateTime.ofInstant(instant, ZoneId.of(%22UTC%22)).format(DateTimeFormatter.ofPattern(format));%0A    %7D
 %0A%7D%0A

Add end to end functional test checking timezone for newly created entries