package benjamin.groehbiel.ch.e2e;

import benjamin.groehbiel.ch.ApplicationTest;
import benjamin.groehbiel.ch.SpringTest;
import benjamin.groehbiel.ch.shortener.ShortenerRepository;
import benjamin.groehbiel.ch.shortener.word.EnglishDictionary;
import benjamin.groehbiel.ch.shortener.word.WordDefinition;
import org.fluentlenium.adapter.FluentTest;
import org.fluentlenium.core.domain.FluentList;
import org.fluentlenium.core.domain.FluentWebElement;
import org.hamcrest.MatcherAssert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import java.util.LinkedList;
import java.util.List;

import static org.hamcrest.Matchers.*;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ApplicationTest.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
public class LandingPageTest extends FluentTest {

    @Autowired
    private ShortenerRepository shortenerRepository;

    @Autowired
    private EnglishDictionary englishDictionary;

    @Before
    public void clearRepository() {
        shortenerRepository.clear();

        List<WordDefinition> words = new LinkedList<>();
        words.add(new WordDefinition("fun", "enjoyment, amusement, or light-hearted pleasure."));
        words.add(new WordDefinition("eloquence", "fluent or persuasive speaking or writing."));
        words.add(new WordDefinition("elephant", "a very large plant-eating mammal with a prehensile trunk, long curved ivory tusks, and large ears, native to Africa and southern Asia."));
        englishDictionary.set(words);
    }

    @Value("${local.server.port}")
    int port;

    @Test
    public void showsSuccessAndWordDescription() {
        goTo("http://localhost:" + port);
        fill("#urlForm .url").with("http://www.pivotal.io");
        click("#urlForm button");

        await();

        FluentList<FluentWebElement> descriptions = find(".success .description");
        MatcherAssert.assertThat(descriptions, hasSize(1));
        MatcherAssert.assertThat(descriptions.get(0).isDisplayed(), equalTo(true));
        MatcherAssert.assertThat(descriptions.get(0).getText(), containsString("enjoyment, amusement, or light-hearted pleasure."));
    }

}

@@ -445,16 +445,111 @@
 lement;%0A
+import org.hamcrest.BaseMatcher;%0Aimport org.hamcrest.Description;%0Aimport org.hamcrest.Matcher;%0A
 import o
@@ -2683,10 +2683,1444 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void makeSuccessMessageClosable() %7B%0A        goTo(%22http://localhost:%22 + port);%0A        fill(%22#urlForm .url%22).with(%22http://www.pivotal.io%22);%0A        click(%22#urlForm button%22);%0A%0A        await();%0A%0A        FluentWebElement searchButton = findFirst(%22#urlForm button%22);%0A        MatcherAssert.assertThat(searchButton, hasAttribute(%22disabled%22));%0A%0A        FluentWebElement closeIcon = findFirst(%22.success .close%22);%0A        closeIcon.click();%0A%0A        MatcherAssert.assertThat(searchButton, not(hasAttribute(%22disabled%22)));%0A    %7D%0A%0A    private Matcher%3C? super FluentWebElement%3E hasAttribute(String attribute) %7B%0A        Matcher%3CFluentWebElement%3E matcher = new BaseMatcher%3CFluentWebElement%3E() %7B%0A            @Override%0A            public void describeTo(Description description) %7B%0A            %7D%0A%0A            @Override%0A            public boolean matches(Object item) %7B%0A                FluentWebElement button = (FluentWebElement) item;%0A                String disabledAttr = button.getAttribute(attribute);%0A                if (disabledAttr == null) %7B%0A                    return false;%0A                %7D else %7B%0A                    return true;%0A                %7D%0A            %7D%0A%0A            @Override%0A            public void describeMismatch(Object item, Description mismatchDescription) %7B%0A                mismatchDescription.appendText(%22Could not find attribute %22 + attribute);%0A            %7D%0A        %7D;%0A%0A        return matcher;%0A    %7D%0A%0A
 %7D%0A

introduce hasAttribute Matcher