package uk.gov.register.functional;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.junit.ClassRule;
import org.junit.Test;
import uk.gov.register.functional.app.RegisterRule;

import javax.ws.rs.core.Response;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertThat;

public class HomePageFunctionalTest {

    private final String registerName = "postcode";

    @ClassRule
    public static final RegisterRule register = new RegisterRule();

    @Test
    public void homePageIsAvailableWhenNoDataInRegister() {
        Response response = register.getRequest(registerName, "/");
        assertThat(response.getStatus(), equalTo(200));
    }

    @Test
    public void homepageHasXhtmlLangAttributes() throws Throwable {
        Response response = register.getRequest(registerName, "/");

        Document doc = Jsoup.parse(response.readEntity(String.class));
        Elements htmlElement = doc.select("html");
        assertThat(htmlElement.size(), equalTo(1));
        assertThat(htmlElement.first().attr("lang"), equalTo("en"));
        assertThat(htmlElement.first().attr("xml:lang"), equalTo("en"));
    }

    @Test
    public void homepageHasCopyrightInMainBodyRenderedAsMarkdown() throws Throwable {
        // assumes that registers.yaml has a `postcode` entry with a copyright field containing markdown links
        // might be good to find a way to specify this in the test
        Response response = register.getRequest(registerName, "/");
        Document doc = Jsoup.parse(response.readEntity(String.class));

        Elements copyrightParagraph = doc.select("main .registry-copyright");
        Elements links = copyrightParagraph.select("a");
        assertThat(links.size(), greaterThan(0));
    }

    @Test
    public void homepageHasCopyrightInFooterRenderedAsMarkdown() throws Throwable {
        // assumes that registers.yaml has a `postcode` entry with a copyright field containing markdown links
        // might be good to find a way to specify this in the test
        Response response = register.getRequest(registerName, "/");
        Document doc = Jsoup.parse(response.readEntity(String.class));

        Elements copyrightParagraph = doc.select("footer .registry-copyright");
        Elements links = copyrightParagraph.select("a");
        assertThat(links.size(), greaterThan(0));
    }
}

@@ -453,16 +453,23 @@
 private 
+static 
 final St
@@ -473,28 +473,45 @@
  String 
-registerName
+REGISTER_WITH_COPYRIGHT_FIELD
  = %22post
@@ -716,36 +716,53 @@
 .getRequest(
-registerName
+REGISTER_WITH_COPYRIGHT_FIELD
 , %22/%22);%0A    
@@ -942,36 +942,53 @@
 .getRequest(
-registerName
+REGISTER_WITH_COPYRIGHT_FIELD
 , %22/%22);%0A%0A   
@@ -1625,36 +1625,53 @@
 .getRequest(
-registerName
+REGISTER_WITH_COPYRIGHT_FIELD
 , %22/%22);%0A    
@@ -2254,20 +2254,37 @@
 est(
-registerName
+REGISTER_WITH_COPYRIGHT_FIELD
 , %22/

use intension-revealing name