package focusedCrawler.util.parser;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import java.net.MalformedURLException;
import java.net.URL;

import org.junit.Test;

public class PaginaURLTest {

    @Test
    public void htmlEncodedLinksShouldBeEscaped() throws Exception {
        // given
        StringBuilder testPage = new StringBuilder();
        testPage.append("<!DOCTYPE html>");
        testPage.append("<html>");
        testPage.append("<body>");
        testPage.append("<a href = \"http://ex.com/index.php?p1=asdf&amp;p2=qwer\">Anchor text.</a>");
        testPage.append("</body>");
        testPage.append("</html>");
        String testString = testPage.toString();
        
        // when
        PaginaURL pageParser = new PaginaURL(new URL("http://ex.com/index.html"),testString);
        URL[] extractedLinks = pageParser.links();
        LinkNeighborhood[] neighborhood = pageParser.getLinkNeighboor();

        // then
        assertThat(extractedLinks[0].toString(), is("http://ex.com/index.php?p1=asdf&p2=qwer"));
        assertThat(neighborhood[0].getLink().toString(), is("http://ex.com/index.php?p1=asdf&p2=qwer"));
    }

    @Test
    public void linksShouldNotContainFragments() throws Exception {
        // given
        String testString = createTestPage();
        URL url = new URL("http://www.w3schools.com/html/tryit.asp?filename=tryhtml_basic_document");
        // when
        PaginaURL pageParser = new PaginaURL(url,testString);
        URL[] extractedLinks = pageParser.links();
        // then
        assertEquals("Extracted URL contains fragment.", false, hasFragments(extractedLinks));
    }

    
    @Test
    public void constructorsShouldWork() throws MalformedURLException {
        // given
        URL url = new URL("http://www.w3schools.com/html/tryit.asp?filename=tryhtml_basic_document");
        String testPage = createTestPage();
        // when
        PaginaURL paginaURL = new PaginaURL(url, testPage);
        // then
        assertEquals("Constructor not working properly !", false, paginaURL.getURL().equals(null));
    }
    
    @Test
    public void shouldNotExtractInvalidLinks() throws MalformedURLException {
        // given
        URL url = new URL("http://example.com/test.html");
        StringBuilder testPage = new StringBuilder();
        testPage.append("<!DOCTYPE html>");
        testPage.append("<html>");
        testPage.append("<body>");
        testPage.append("<h1>My First Heading</h1>");
        testPage.append("<a href = \"http://None/\">link 0</a>");
        testPage.append("<a href = \"http://12324/\">link 1</a>");
        testPage.append("<a href = \"/asdf.html\">link 2</a>");
        testPage.append("</body>");
        testPage.append("</html>");
        
        // when
        PaginaURL paginaURL = new PaginaURL(url, testPage.toString());
        URL[] links = paginaURL.links();
        LinkNeighborhood[] lns  = paginaURL.getLinkNeighboor();
        
        // then
        assertThat(links.length, is(1));
        assertThat(links[0].toString(), is("http://example.com/asdf.html"));

        assertThat(lns.length, is(1));
        assertThat(lns[0].getLink().toString(), is("http://example.com/asdf.html"));
    }
    
    @Test
    public void shouldExtractAnchoTextAndTextAroundLink() throws MalformedURLException {
        // given
        URL url = new URL("http://www.w3schools.com/html/tryit.asp?filename=tryhtml_basic_document");
        String testPage = createTestPage();
        // when
        PaginaURL paginaURL = new PaginaURL(url, testPage);
        LinkNeighborhood[] neighborhoods = paginaURL.getLinkNeighboor();
        // then
        assertThat(neighborhoods.length, is(1));
        
        assertThat(neighborhoods[0].getAroundString().trim(), is("my first heading"));
        assertThat(neighborhoods[0].getAround()[0], is("my"));
        assertThat(neighborhoods[0].getAround()[1], is("first"));
        assertThat(neighborhoods[0].getAround()[2], is("heading"));
        
        assertThat(neighborhoods[0].getAnchorString().trim(), is("my first paragraph"));
        assertThat(neighborhoods[0].getAnchor()[0], is("my"));
        assertThat(neighborhoods[0].getAnchor()[1], is("first"));
        assertThat(neighborhoods[0].getAnchor()[2], is("paragraph"));
    }

    private boolean hasFragments(URL[] urls) {
        for (URL url : urls) {
            if (url.getFile().toString().contains("#"))
                return true;
        }
        return false;
    }

    private String createTestPage() {
        StringBuilder testPage = new StringBuilder();
        testPage.append("<!DOCTYPE html>");
        testPage.append("<html>");
        testPage.append("<body>");
        testPage.append("<h1>My First Heading</h1>");
        testPage.append("<a href = \"https://en.wikipedia.org/wiki/Mouse_(computing)#Mechanical_mice\">My first paragraph.</a>");
        testPage.append("</body>");
        testPage.append("</html>");
        return testPage.toString();
    }
}

@@ -96,33 +96,42 @@
 org.
-junit.Assert.assertEquals
+hamcrest.CoreMatchers.notNullValue
 ;%0Aim
@@ -1646,22 +1646,20 @@
   assert
-Equals
+That
 (%22Extrac
@@ -1686,23 +1686,16 @@
 gment.%22,
- false,
  hasFrag
@@ -1715,16 +1715,26 @@
 edLinks)
+, is(true)
 );%0A    %7D
@@ -2095,90 +2095,50 @@
 sert
-Equals(%22Constructor not working properly !%22, false, paginaURL.getURL().equals(null
+That(paginaURL.getURL(), is(notNullValue()
 ));%0A

Use hamcrest matchers for assertions