package com.malmstein.yahnac.comments;

import android.content.ContentValues;

import com.malmstein.yahnac.comments.parser.CommentsParser;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Before;

import java.io.File;
import java.util.Vector;

import static org.junit.Assert.assertEquals;

public class CommentsParserTest {

    private static final String BASE_URI = "https://news.ycombinator.com/news";

    private static final String TITLE_SAMPLE = "This doesn't help my impression of Digital Ocean at all (even if I am a paying customer currently). A few years ago you could impersonate Digital Ocean staff on their support pages with no effort. They grabbed the username from your email, so whatever you put in front of the @ becamse your username on the forums, visible to everyone. And the avatar came from one of those email-&gt;avatar services where you can sign up and set it to anything. So when I signed up with a username like digitalocean@mydomain.com, I ended up being called \"digitalocean\" on the support forums, and if I had wanted I could just change the avatar to the Digital Ocean logo and impersonate DO or anyone else.<p>I tried reporting it but got pretty much the same answer as this guy (though I did not get banned). Luckily they fixed it like a year later.</p><p>Great write-up, and interesting problem! I wonder if more hosting providers are vulnerable to the same problem.</p>";
    private static final String AUTHOR_SAMPLE = "flexd";
    private static final String TIME_AGO_SAMPLE = "3 hours ago";
    private static final String QUESTION_SAMPLE = "I'm a student developer, @pravj on GitHub. I try to follow the best possible development practice when I'm working like documentation, respecting language's style guide etc.<p>But one thing I don't like with me is that I never write tests.</p><p>Why I never followed that aspect is because I always felt that the thing that I'm writing, sounds totally OK.</p><p>One another negative point is that I don't actually get it that How to do/start this and what type of tests I should write for any given project. Maybe this is because I have never done this before.</p><p>For example recently I wrote a identicons library, Penticons[1]. It generates GitHub contribution flavored identicons. You can read the development story here[2].</p><p>Now, the thing that stopped me from writing tests was, I thought that ultimately it's a image generation library, why this thing needs any tests.</p><p>I don't want to lost my 1 point in the Joel score. Whenever I see a <i>build passing</i> label on a GitHub repository, It makes me crazy.</p><p>I'm asking here because I just love HN, I know people will suggest some awesome things here.</p><p>1. https://github.com/penticons/penticons.go</p><p>2. http://pravj.github.io/blog/penticons-the-hash-game/</p>";
    private static final String EMPTY_QUESTION_SAMPLE = "";

    private static final long storyId = 9085563;

    CommentsParser askStoryCommentsParser;
    CommentsParser storyCommentsParser;

    Element topRowElement;
    Element secondRowElement;
    Element voteElement;

    @Before
    public void setUp() throws Exception {
        File newsStoryCommentsFile = new File(getClass().getResource("/comments_9989667.html").getPath());
        File askStoryCommentsFile = new File(getClass().getResource("/comments_ask_998917.html").getPath());
        Document askStoryComments = Jsoup.parse(askStoryCommentsFile, "UTF-8", BASE_URI);
        Document storyComments = Jsoup.parse(newsStoryCommentsFile, "UTF-8", BASE_URI);

        Elements tableRows = storyComments.select("table tr table tr:has(table)");

        topRowElement = tableRows.get(0).select("td:eq(2)").first();
        secondRowElement = tableRows.get(0).select("td:eq(0)").first();
        secondRowElement = tableRows.get(0).select("td:eq(0)").first();
        voteElement = tableRows.get(0).select("td:eq(1) a").first();

        askStoryCommentsParser = new CommentsParser(storyId, askStoryComments);
        storyCommentsParser = new CommentsParser(storyId, storyComments);
    }

    @org.junit.Test
    public void returnsAllStoryComments() {
        Vector<ContentValues> storyComments = storyCommentsParser.parse();
        assertEquals(58, storyComments.size());
    }

    @org.junit.Test
    public void returnsAllAskStoryComments() {
        Vector<ContentValues> storyComments = askStoryCommentsParser.parse();
        assertEquals(53, storyComments.size());
    }

    @org.junit.Test
    public void returnsCommentText() {
        String title = storyCommentsParser.parseText(topRowElement);
        assertEquals(TITLE_SAMPLE, title);
    }

    @org.junit.Test
    public void returnsCommentAuthor() {
        String author = storyCommentsParser.parseAuthor(topRowElement);
        assertEquals(AUTHOR_SAMPLE, author);
    }

    @org.junit.Test
    public void returnsCommentTimeAgo() {
        String timeAgo = storyCommentsParser.parseTimeAgo(topRowElement);
        assertEquals(TIME_AGO_SAMPLE, timeAgo);
    }

    @org.junit.Test
    public void returnsCommentLevel() {
        int level = storyCommentsParser.parseLevel(secondRowElement);
        assertEquals(0, level);
    }

    @org.junit.Test
    public void returnsCommentId() {
        String commentId = storyCommentsParser.parseCommentId(topRowElement);
        assertEquals("12364422", commentId);
    }

}

@@ -543,962 +543,268 @@
  = %22
-This doesn't help my impression of Digital Ocean at all (even if I am a paying customer currently). A few years ago you could impersonate Digital Ocean staff on their support pages with no effort. They grabbed the username from your email, so whatever you put in front of the @ becamse your username on the forums, visible to everyone. And the avatar came from one of those email-&gt;avatar services where you can sign up and set it to anything. So when I signed up with a username like digitalocean@mydomain.com, I ended up being called %5C%22digitalocean%5C%22 on the support forums, and if I had wanted I could just change the avatar to the Digital Ocean logo and impersonate DO or anyone else.%3Cp%3EI tried reporting it but got pretty much the same answer as this guy (though I did not get banned). Luckily they fixed it like a year later.%3C/p%3E%3Cp%3EGreat write-up, and interesting problem! I wonder if more hosting providers are vulnerable to the same problem.%3C/p%3E
+Makes you wonder how many cool things people would have written for the native Intel GPU instruction set if there existed supported interfaces for it. All the programming information has been out there in Intel-provided open source drivers for many many years.
 %22;%0A 
@@ -856,12 +856,14 @@
 = %22f
-lexd
+ulafel
 %22;%0A 
@@ -912,17 +912,17 @@
 MPLE = %22
-3
+2
  hours a
@@ -3641,10 +3641,9 @@
 als(
-58
+3
 , st
@@ -4756,14 +4756,14 @@
 (%2212
-364422
+424346
 %22, c

fix comments parser test