package com.grayben.riskExtractor.htmlScorer;

import org.jsoup.HttpStatusException;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import java.io.File;
import java.net.UnknownHostException;

import static junit.framework.TestCase.fail;

/**
 * Created by beng on 28/11/2015.
 */
@RunWith(MockitoJUnitRunner.class)
public abstract class HtmlScorerTest {

    private HtmlScorer htmlScorerSUT;

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    protected void setHtmlScorerSUT(HtmlScorer htmlScorerSUT){
        this.htmlScorerSUT = htmlScorerSUT;
    }

    @Before
    public void setUp() throws Exception {
        assert htmlScorerSUT != null;
    }

    @After
    public void tearDown() throws Exception {
        this.htmlScorerSUT = null;
    }

    @Test
    public void
    test_ScoreHtmlThrowsNullPointerException_WhenURLIsNull
            () throws Exception {
        String url = null;

        thrown.expect(NullPointerException.class);

        htmlScorerSUT.scoreHtml(url);
    }

    @Test
    public void
    test_ScoreHtmlThrowsNullPointerException_WhenHtmlFileIsNull
            () throws Exception {
        File file = null;

        String charsetName = "ASCII";

        thrown.expect(NullPointerException.class);

        htmlScorerSUT.scoreHtml(file, charsetName);
    }

    @Test
    public void
    test_ScoreHtmlThrowsNullPointerException_WhenCharsetNameIsNull
            () throws Exception {
        File file = new File("src/test/resources/sample.html");

        String charsetName = null;

        thrown.expect(NullPointerException.class);

        htmlScorerSUT.scoreHtml(file, charsetName);
    }

    @Test
    public void
    test_ScoreHtmlThrowsIllegalArgumentException_WhenCharsetNameIsNotRecognised
            () throws Exception {
        File file = new File("src/test/resources/sample.html");

        String charsetName = "foo bar baz";

        thrown.expect(IllegalArgumentException.class);

        htmlScorerSUT.scoreHtml(file, charsetName);
    }


    @Test
    public void
    test_ScoreHtmlThrowsIllegalArgumentException_WhenCharsetNameIsEmptyString
            () throws Exception {
        File file = new File("src/test/resources/sample.html");

        String charsetName = "";

        thrown.expect(IllegalArgumentException.class);

        htmlScorerSUT.scoreHtml(file, charsetName);
    }

    @Test
    public void
    test_ScoreHtmlThrowsIllegalArgumentException_WhenUrlIsNotValid
            () throws Exception {
        String url = "ggggg:%//l";

        thrown.expect(IllegalArgumentException.class);

        htmlScorerSUT.scoreHtml(url);
    }

    @Test
    public void
    test_ScoreHtmlThrowsUnknownHostException_WhenUrlCanNotBeFound
            () throws Exception {
        String url = "http://ggggggggggggggggggggggggggggggggggg.com.au";

        thrown.expect(UnknownHostException.class);

        htmlScorerSUT.scoreHtml(url);
    }

    @Test
    public void
    test_ScoreHtmlThrowsHttpStatusException_IfResponseIsNotOK
            () throws Exception {
        String url = "http://www.google.com/sfadgadbdbddb";

        thrown.expect(HttpStatusException.class);

        htmlScorerSUT.scoreHtml(url);
    }

    @Test
    public void
    test_ScoreHtmlReturnsNonNull_WhenSimpleInput
            () throws Exception {
        fail("Test not implemented");
    }

    @Test
    public void
    test_ScoreHtmlReturnsNonNull_WhenLengthyInput
            () throws Exception {
        File file = new File("src/test/resources/sample.html");

        String charsetName = null;

        thrown.expect(NullPointerException.class);

        htmlScorerSUT.scoreHtml(file, charsetName);
    }

    @Test
    public void
    test_ScoreHtmlReturnsNonNull_WhenEmptyFile
            () throws Exception {
        fail("Test not implemented");
    }

    @Test
    public void
    test_ScoreHtmlReturnsSameFromAnySignature_WhenTextInputIsSameSimple
            () throws Exception {
        fail("Test not implemented");
    }
    
    @Test
    abstract public void
    test_ScoreHtmlReturnsExpected_WhenTextInputIsSimple
            () throws Exception;

    @Test
    abstract public void
    test_ScoreHtmlReturnsExpected_WhenTextInputIsComplicated
            () throws Exception;
}
@@ -356,16 +356,70 @@
 ption;%0A%0A
+import static junit.framework.TestCase.assertNotNull;%0A
 import s
@@ -3512,16 +3512,21 @@
 enSimple
+Local
 Input%0A  
@@ -3569,35 +3569,469 @@
     
-fa
+File file = new F
 il
+e
 (%22
-Test not implemented%22
+src/test/resources/simple.html%22);%0A%0A        String charsetName = %22UTF-8%22;%0A%0A        ScoredText returned = htmlScorerSUT.scoreHtml(file, charsetName);%0A%0A        assertNotNull(returned);%0A    %7D%0A%0A    @Test%0A    public void%0A    test_ScoreHtmlReturnsNonNull_WhenSimpleRemoteInput%0A            () throws Exception %7B%0A        String url = %22http://grayben.com%22;%0A%0A        ScoredText returned = htmlScorerSUT.scoreHtml(url);%0A%0A        assertNotNull(returned
 );%0A 
@@ -4106,16 +4106,21 @@
 nLengthy
+Local
 Input%0A  
@@ -4355,32 +4355,320 @@
 etName);%0A    %7D%0A%0A
+    @Test%0A    public void%0A    test_ScoreHtmlReturnsNonNull_WhenLengthyRemoteInput%0A            () throws Exception %7B%0A        String url = %22https://en.wikipedia.org/wiki/Albert_Einstein%22;%0A%0A        ScoredText returned = htmlScorerSUT.scoreHtml(url);%0A%0A        assertNotNull(returned);%0A    %7D%0A%0A
     @Test%0A    pu

Test for non-null returned on good input