/**
 * Copyright (c) 2016-2017, Mihai Emil Andronache
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * * Neither the name of charles nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.amihaiemil.charles;

import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

/**
 * Integration tests for {@link GraphCrawl}
 * @author Mihai Andronache (amihaiemil@gmail.com)
 *
 */
public class GraphCrawlITCase {
    private WebDriver driver;
    
    /**
     * Crawls the site as a graph, visiting all the linked pages.
     * @throws Exception If something goes wrong.
     */
    @Test
    public void crawlsAllPages() throws Exception {
        InMemoryRepository inmr = new InMemoryRepository();
        GraphCrawl graph = new GraphCrawl("http://www.amihaiemil.com", this.driver, inmr);
        graph.crawl();
        Set<WebPage> uniquePages = new HashSet<WebPage>();
        for(WebPage p : inmr.getCrawledPages()) {
            assertTrue("Page crawled 2 times!", uniquePages.add(p));
        }
    }
    
    /**
     * Crawls the website as a graph.
     * The index page does not have any links to other pages on the site.
     * @throws Exception If something goes wrong.
     */
    @Test
    public void crawlsWithNoMoreLinks() throws Exception {
        InMemoryRepository inmr = new InMemoryRepository();
        GraphCrawl graph = new GraphCrawl("http://eva.amihaiemil.com/index.html", this.driver, inmr);
        graph.crawl();
        assertTrue(inmr.getCrawledPages().size() == 1);
        WebPage index = inmr.getCrawledPages().get(0);
        assertTrue(index.getTextContent().contains("changing and combining the existing ones until finally, a solution is chosen to be the answer"));
        assertTrue(index.getName().equals("index"));
        assertTrue(index.getTitle().equals("EvA project"));
        
    }
    
    /**
     * Graph crawls all the links except the ignored ones.
     * @throws Exception If something goes wrong.
     */
    @Test
    public void crawlsAllPagesExceptIgnored() throws Exception {
        InMemoryRepository inmr = new InMemoryRepository();
        GraphCrawl graph = new GraphCrawl(
            "http://www.amihaiemil.com",
            this.driver,
            new IgnoredPatterns(Arrays.asList("http://www.amihaiemil.com/*/2016/04/*")),//ignore all pages (posts) from April 2016
            inmr
        );
        graph.crawl();
        for(WebPage p : inmr.getCrawledPages()) {
            assertTrue("Ignored page was crawled! " + p.getUrl(), !p.getUrl().contains("/2016/04/"));
        }
    }
    
    @Before
    public void initDriver() {
        this.driver = this.phantomJsDriver();
    }
    
    @After
    public void quitDriver() {
        this.driver.quit();
    }
    
    private WebDriver phantomJsDriver() {
        String phantomJsExecPath =  System.getProperty("phantomjsExec");
        if("".equals(phantomJsExecPath)) {
            phantomJsExecPath = "/usr/local/bin/phantomjs";
        }
        DesiredCapabilities dc = new DesiredCapabilities();
        dc.setJavascriptEnabled(true);
        dc.setCapability(
            PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
            phantomJsExecPath
        );
        return new PhantomJSDriver(dc);
    }
}

@@ -1625,16 +1625,17 @@
 rtTrue;%0A
+%0A
 import j
@@ -1694,24 +1694,90 @@
 va.util.Set;
+%0A%0Aimport org.hamcrest.MatcherAssert;%0Aimport org.hamcrest.Matchers;
 %0Aimport org.
@@ -3226,27 +3226,54 @@
         
-assertTrue(
+MatcherAssert.assertThat(%0A            
 inmr.get
@@ -3297,13 +3297,33 @@
 ze()
- == 1
+, Matchers.is(1)%0A        
 );%0A 
@@ -3388,41 +3388,99 @@
     
-assertTrue(index.getTextContent()
+%0A        MatcherAssert.assertThat(%0A            index.getTextContent(),%0A            Matchers
 .con
@@ -3484,16 +3484,22 @@
 contains
+String
 (%22changi
@@ -3536,161 +3536,239 @@
 ones
- until finally, a solution is chosen to be the answer%22));%0A        assertTrue(index.getName()
+%22)%0A        );        %0A        MatcherAssert.assertThat(%0A            index.getName(),%0A            Matchers
 .equal
-s
+To
 (%22index
-%22));%0A        assertTrue(index.getTitle()
+.html%22)%0A        );%0A        MatcherAssert.assertThat(%0A            index.getTitle(),%0A            Matchers
 .equal
-s
+To
 (%22Ev
@@ -3778,18 +3778,16 @@
 roject%22)
-);
 %0A       
@@ -3783,24 +3783,26 @@
 t%22)%0A        
+);
 %0A    %7D%0A    %0A

fix test