package lan.dk.podcastserver.service;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import javaslang.control.Option;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Tag;
import org.jsoup.select.Elements;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.stream.Collector;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.assertj.core.api.Assertions.assertThat;

/**
 * Created by kevin on 11/06/15 for HackerRank problem
 */
@RunWith(MockitoJUnitRunner.class)
public class HtmlServiceTest {

    @Spy
    UrlService urlService;
    @InjectMocks HtmlService htmlService;

    @Rule
    public WireMockRule wireMockRule = new WireMockRule(8089); // No-args constructor defaults to port 8080

    public static final String URL = "http://nowhere.anywhere";
    private static final String USER_AGENT = "User-Agent";
    private static final String REFERER = "Referer";

    @Test
    public void should_get_page() {
        /* Given */
        stubFor(get(urlEqualTo("/page/file.html"))
                .willReturn(aResponse()
                        .withStatus(200)
                        .withBodyFile("service/htmlService/jsoup.html")));

        /* When */
        Option<Document> document = htmlService.get("http://localhost:8089/page/file.html");

        /* Then */
        assertThat(document.isDefined()).isTrue();
        assertThat(document.map(d -> d.head().select("title").text())).contains("JSOUP Example");
    }

    @Test
    public void should_reject_get_with_empty() {
        /* Given */
        /* When */
        Option<Document> document = htmlService.get("http://localhost:8089/page/foo.html");

        /* Then */
        assertThat(document).isEmpty();
    }

    @Test
    public void should_parse_string() {
        /* Given */
        String html = "<div></div>";
        /* When */
        Document document = htmlService.parse(html);
        /* Then */
        assertThat(document).isNotNull().isInstanceOf(Document.class);
    }

    @Test
    public void should_generate_a_collector_for_html_element() {
        /* Given */
        Collector<Element, Elements, Elements> collector = HtmlService.toElements();
        Element divWithFooBar = new Element(Tag.valueOf("div"), "foo-bar");
        Element span = new Element(Tag.valueOf("span"), "Text");

        /* When */
        Elements listOfElements = collector.supplier().get();
        collector.accumulator().accept(listOfElements, divWithFooBar);
        Elements cobinedElements = collector.combiner().apply(listOfElements, new Elements(span));

        /* Then */
        assertThat(cobinedElements)
                .hasSize(2).contains(divWithFooBar, span)
                .isInstanceOf(Elements.class);
    }

}
@@ -81,24 +81,29 @@
 nit.WireMock
+Class
 Rule;%0Aimport
@@ -257,16 +257,44 @@
 ements;%0A
+import org.junit.ClassRule;%0A
 import o
@@ -766,20 +766,16 @@
     @Spy
-%0A   
  UrlServ
@@ -838,16 +838,21 @@
 ;%0A%0A    @
+Class
 Rule%0A   
@@ -859,24 +859,31 @@
  public 
+static 
 WireMock
 Rule wir
@@ -870,24 +870,29 @@
 tic WireMock
+Class
 Rule wireMoc
@@ -915,17 +915,22 @@
 Mock
+Class
 Rule(80
-8
 9
+0
 ); /
@@ -982,179 +982,394 @@
     
-public static final String URL = %22http://nowhere.anywhere%22;%0A    private static final String USER_AGENT = %22User-Agent%22;%0A    private static final String REFERER = %22Referer%22;
+@Rule%0A    public WireMockClassRule instanceRule = wireMockRule;%0A%0A    public static final String URL = %22http://nowhere.anywhere%22;%0A%0A    @Test%0A%0A    public void should_reject_get_with_empty() %7B%0A        /* Given */%0A        /* When */%0A        Option%3CDocument%3E document = htmlService.get(%22http://localhost:8090/page/foo.html%22);%0A%0A        /* Then */%0A        assertThat(document).isEmpty();%0A    %7D
 %0A%0A  
@@ -1409,16 +1409,44 @@
 t_page()
+ throws InterruptedException
  %7B%0A     
@@ -1755,26 +1755,26 @@
 localhost:80
-8
 9
+0
 /page/file.h
@@ -1960,265 +1960,8 @@
  %7D%0A%0A
-    @Test%0A    public void should_reject_get_with_empty() %7B%0A        /* Given */%0A        /* When */%0A        Option%3CDocument%3E document = htmlService.get(%22http://localhost:8089/page/foo.html%22);%0A%0A        /* Then */%0A        assertThat(document).isEmpty();%0A    %7D%0A%0A
     

change server start method to prevent error during build due to concurrent execution