package focusedCrawler.link.classifier.builder;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import focusedCrawler.util.Urls;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import focusedCrawler.config.Configuration;
import focusedCrawler.link.LinkStorageConfig.BackSurferConfig;
import focusedCrawler.link.backlink.BacklinkSurfer;
import focusedCrawler.util.parser.BackLinkNeighborhood;

public class BacklinkSurferTest {

    @Test
    public void backlinksShouldBeDownloadedFromMozApi() throws MalformedURLException, IOException {
        // given
        Map<String, String> props = new HashMap<>();
        props.put("link_storage.backsurfer.moz.access_id", "mozscape-4a1d0827fc");
        props.put("link_storage.backsurfer.moz.secret_key", "d6ea0c3b253ab44425769e422624a0f");
        
        BackSurferConfig config = new Configuration(props).getLinkStorageConfig().getBackSurferConfig();
        BacklinkSurfer surfer = new BacklinkSurfer(config);
        
        URL url = new URL("http://www.bbc.co.uk/news/health-30577776");

        // when
        BackLinkNeighborhood[] backlinks = surfer.getLNBacklinks(url);
        
        // then
        assertThat(backlinks, is(notNullValue()));
        assertThat(backlinks.length>0, is(true));
        assertTrue(backLinkSetIsValid(backlinks));
    }
    
    @Test
    public void backlinksShouldBeDownloadedFromGoogle() throws MalformedURLException, IOException {
        // given
        Map<String, String> props = new HashMap<>();
        BackSurferConfig config = new Configuration(props).getLinkStorageConfig().getBackSurferConfig();
        BacklinkSurfer surfer = new BacklinkSurfer(config);

        URL url = new URL("http://www.bbc.co.uk");

        // when
        BackLinkNeighborhood[] backlinks = surfer.getLNBacklinks(url);
        
        // then
        assertThat(backlinks, is(notNullValue()));
        assertThat(backlinks.length>0, is(true));
        assertTrue(backLinkSetIsValid(backlinks));

    }

    public boolean backLinkSetIsValid(BackLinkNeighborhood[] backlinks) {
        for (BackLinkNeighborhood backlink : backlinks) {
            if (Urls.isValid(backlink.getLink()))
                return true;
        }
        return false;
    }

}

@@ -397,16 +397,41 @@
 l.Map;%0A%0A
+import org.junit.Ignore;%0A
 import o
@@ -697,16 +697,28 @@
 Test %7B%0A%0A
+    @Ignore%0A
     @Tes
@@ -781,39 +781,16 @@
 ) throws
- MalformedURLException,
  IOExcep
@@ -1571,20 +1571,28 @@
 ;%0A    %7D%0A
+%0A
     
+@Ignore
 %0A    @Te
@@ -1660,31 +1660,8 @@
 rows
- MalformedURLException,
  IOE

Disable tests that use third-party APIs