package org.loklak.api.search;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import org.junit.Test;
import org.loklak.api.search.GithubProfileScraper;
import org.loklak.data.DAO;
import org.loklak.harvester.Post;
import org.loklak.http.ClientConnection;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

/**
 * These unit-tests test org.loklak.api.search.GithubProfileScraper.java
 */
public class GithubProfileScraperTest {

    @Test
    public void githubProfileScraperOrgTest() {

        GithubProfileScraper githubScraper = new GithubProfileScraper();
        String url = "https://github.com/fossasia/";
        BufferedReader br = null;
        githubScraper.termsList = new ArrayList<String>();
        githubScraper.termsList.add("all");

        String profile = "fossasia";
        String shortDescription = "Open Technologies in Asia";
        String userName = "fossasia";
        String userId = "6295529";
        String location = "Singapore";
        String specialLink = "http://fossasia.org";

        try {
            ClientConnection connection = new ClientConnection(url);
            //Check Network issue
            assertThat(connection.getStatusCode(), is(200));

            br = githubScraper.getHtml(connection);
        } catch (IOException e) {
            DAO.log("GithubProfileScraperTest.githubProfileScraperUserTest() failed to connect to network. url:" + url);
        }

        Post fetchedProfile = githubScraper.scrapeGithub(profile, br);

        assertEquals(fetchedProfile.getString("short_description"), shortDescription);
        assertEquals(fetchedProfile.getString("user"), userName);
        assertEquals(fetchedProfile.getString("user_id"), userId);
        assertEquals(fetchedProfile.getString("location"), location);
        assertEquals(fetchedProfile.getString("special_link"), specialLink);

    }

    @Test
    public void githubProfileScraperUserTest() {

        GithubProfileScraper githubScraper = new GithubProfileScraper();
        String url = "https://github.com/djmgit/";
        BufferedReader br = null;
        githubScraper.termsList = new ArrayList<String>();
        githubScraper.termsList.add("all");

        String profile = "djmgit";
        String userName = "djmgit";
        String fullName = "Deepjyoti Mondal";
        String specialLink = "http://djmgit.github.io";
        String userId = "16368427";

        try {
            ClientConnection connection = new ClientConnection(url);
            //Check Network issue
            assertThat(connection.getStatusCode(), is(200));

            br = githubScraper.getHtml(connection);
        } catch (IOException e) {
                DAO.log("GithubProfileScraperTest.githubProfileScraperUserTest() failed to connect to network. url:" + url);
        }

            Post fetchedProfile = githubScraper.scrapeGithub(profile, br);

            assertEquals(fetchedProfile.getString("user"), userName);
            assertEquals(fetchedProfile.getString("full_name"), fullName);
            assertEquals(fetchedProfile.getString("special_link"), specialLink);
            assertEquals(fetchedProfile.getString("user_id"), userId);
    }

}

@@ -1109,16 +1109,17 @@
  = %22http
+s
 ://fossa
@@ -1128,16 +1128,16 @@
 a.org%22;%0A
-
 %0A       
@@ -1611,32 +1611,50 @@
    assertEquals(
+shortDescription, 
 fetchedProfile.g
@@ -1686,26 +1686,8 @@
 on%22)
-, shortDescription
 );%0A 
@@ -1698,32 +1698,42 @@
    assertEquals(
+userName, 
 fetchedProfile.g
@@ -1744,34 +1744,24 @@
 ring(%22user%22)
-, userName
 );%0A        a
@@ -1764,32 +1764,40 @@
    assertEquals(
+userId, 
 fetchedProfile.g
@@ -1811,32 +1811,24 @@
 g(%22user_id%22)
-, userId
 );%0A        a
@@ -1831,32 +1831,42 @@
    assertEquals(
+location, 
 fetchedProfile.g
@@ -1889,18 +1889,8 @@
 on%22)
-, location
 );%0A 
@@ -1901,32 +1901,45 @@
    assertEquals(
+specialLink, 
 fetchedProfile.g
@@ -1962,29 +1962,16 @@
 l_link%22)
-, specialLink
 );%0A%0A    
@@ -2999,32 +2999,42 @@
    assertEquals(
+userName, 
 fetchedProfile.g
@@ -3053,18 +3053,8 @@
 er%22)
-, userName
 );%0A 
@@ -3069,32 +3069,42 @@
    assertEquals(
+fullName, 
 fetchedProfile.g
@@ -3128,18 +3128,8 @@
 me%22)
-, fullName
 );%0A 
@@ -3144,32 +3144,45 @@
    assertEquals(
+specialLink, 
 fetchedProfile.g
@@ -3209,21 +3209,8 @@
 nk%22)
-, specialLink
 );%0A 
@@ -3233,16 +3233,24 @@
 tEquals(
+userId, 
 fetchedP
@@ -3280,16 +3280,8 @@
 id%22)
-, userId
 );%0A 

Fix #1556: Fix GithubProfileScraper test (#1557)