package com.grayben.riskExtractor.htmlScorer;

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.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static junit.framework.TestCase.*;

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

    private ScoredText scoredTextSUT;

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

    @Mock
    public ScoredTextElement scoredTextElementMock;

    @Before
    public void setUp() throws Exception {
        scoredTextSUT = new ScoredText();
    }

    @After
    public void tearDown() throws Exception {
        scoredTextSUT = null;
    }

    @Test
    public void
    test_ToStringReturnsNonNull_WhenNothingWasAdded
            () throws Exception {
        scoredTextSUT = new ScoredText();

        assertNotNull(scoredTextSUT.toString());
    }

    @Test
    public void
    test_AddChangesWhatToStringReturns
            () throws Exception {
        String beforeAdd = scoredTextSUT.toString();
        scoredTextSUT.add(scoredTextElementMock);
        String afterAdd = scoredTextSUT.toString();

        assertNotSame(beforeAdd, afterAdd);
    }

    @Test
    public void
    test_AddThrowsNullPointerException_WhenParamIsNull
            () throws Exception {
        ScoredTextElement element = null;

        thrown.expect(NullPointerException.class);

        scoredTextSUT.add(element);
    }

    @Test
    public void
    test_AddThrowsIllegalArgumentException_WhenParamScoresInNull
            () throws Exception {
        Mockito.when(scoredTextElementMock.getScores())
                .thenReturn(null);

        thrown.expect(IllegalArgumentException.class);

        scoredTextSUT.add(scoredTextElementMock);
    }

    @Test
    public void
    test_AddThrowsIllegalArgumentException_WhenParamTextIsNull
            () throws Exception {
        Mockito.when(scoredTextElementMock.getTextElement()).thenReturn(null);

        thrown.expect(IllegalArgumentException.class);

        scoredTextSUT.add(scoredTextElementMock);
    }

    @Test
    public void
    test_AddThrowsIllegalArgumentException_WhenParamHasEmptyText
            () throws Exception {
        Mockito.when(scoredTextElementMock.getTextElement()).thenReturn("");

        thrown.expect(IllegalArgumentException.class);

        scoredTextSUT.add(scoredTextElementMock);
    }

    @Test
    public void
    test_AddThrowsIllegalArgumentException_WhenParamHasEmptyScores
            () throws Exception {
        Mockito.when(scoredTextElementMock.getScores())
                .thenReturn(new HashMap());

        thrown.expect(IllegalArgumentException.class);

        scoredTextSUT.add(scoredTextElementMock);
    }

    @Test
    public void
    test_GetTextElementReturnsExpectedOutput_OnSimpleInputCase
            () throws Exception {
        List<ScoredTextElement> elementMocks = new ArrayList<>();

        for (int i = 0; i < 5; i++) {
            ScoredTextElement elementMock
                    = Mockito.mock(ScoredTextElement.class);
            Mockito.when(elementMock.getTextElement()).thenReturn("foo");
            elementMocks.add(elementMock);
        }

        String expectedOutput = "foo foo foo foo foo";

        for (ScoredTextElement elementMock : elementMocks) {
            scoredTextSUT.add(elementMock);
        }

        String output = scoredTextSUT.toString();

        assertEquals(expectedOutput, output);
    }
}
@@ -263,16 +263,59 @@
 ockito;%0A
+import org.mockito.internal.matchers.Null;%0A
 import o
@@ -349,24 +349,24 @@
 UnitRunner;%0A
-
 %0Aimport java
@@ -430,16 +430,38 @@
 il.List;
+%0Aimport java.util.Map;
 %0A%0Aimport
@@ -782,16 +782,102 @@
 tMock;%0A%0A
+    public Map%3CString, Integer%3E scoresPrototype;%0A%0A    public String stringPrototype;%0A%0A
     @Bef
@@ -961,24 +961,379 @@
 oredText();%0A
+        scoresPrototype = new HashMap%3C%3E();%0A        scoresPrototype.put(%22swag%22, 9001);%0A        stringPrototype = %22foo%22;%0A        Mockito.when(scoredTextElementMock.getScores())%0A                .thenReturn(new HashMap%3C%3E(scoresPrototype));%0A        Mockito.when(scoredTextElementMock.getTextElement())%0A                .thenReturn(new String(stringPrototype));%0A
     %7D%0A%0A    @
@@ -2231,39 +2231,35 @@
 st_AddThrows
-IllegalArgument
+NullPointer
 Exception_Wh
@@ -2422,39 +2422,35 @@
 rown.expect(
-IllegalArgument
+NullPointer
 Exception.cl
@@ -2553,39 +2553,35 @@
 st_AddThrows
-IllegalArgument
+NullPointer
 Exception_Wh
@@ -2730,39 +2730,35 @@
 rown.expect(
-IllegalArgument
+NullPointer
 Exception.cl
@@ -3867,25 +3867,183 @@
 t())
-.thenReturn(%22foo%22
+%0A                    .thenReturn(new String(stringPrototype));%0A            Mockito.when(elementMock.getScores())%0A                    .thenReturn(new HashMap%3C%3E(scoresPrototype)
 );%0A 

Correct tests