package com.sleekbyte.tailor.output;

import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import com.sleekbyte.tailor.common.Rules;
import com.sleekbyte.tailor.common.Severity;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

/**
 * Tests for {@link ViolationMessage}.
 */
@RunWith(MockitoJUnitRunner.class)
public class ViolationMessageTest {

    private ViolationMessage violationMessage;

    @Before
    public void setUp() {
        this.violationMessage = new ViolationMessage(Rules.LOWER_CAMEL_CASE, "/usr/bin/local", 10, 1, Severity.ERROR,
            "errMsg");
    }

    @Test
    public void testCompareToMessageWithGreaterLineNumber() {
        // global message has lower line number than test message
        ViolationMessage messageWithGreaterLineNumber =
            new ViolationMessage(null, "/usr/bin/local", 12, 5, Severity.ERROR, "errMsg");
        int ret = this.violationMessage.compareTo(messageWithGreaterLineNumber);
        assertThat(ret, lessThan(0));
    }

    @Test
    public void testCompareToMessageWithLesserLineNumber() {
        // global message has lower line number than test message
        ViolationMessage messageWithGreaterLineNumber =
            new ViolationMessage(null, "/usr/bin/local", 12, 5, Severity.ERROR, "errMsg");
        int ret = this.violationMessage.compareTo(messageWithGreaterLineNumber);
        assertThat(ret, lessThan(0));
    }

    @Test
    public void testCompareToMessageWithGreaterColumnNumber() {
        // global message has equal line number but greater column number than test message
        ViolationMessage messageWithGreaterColumnNumber =
            new ViolationMessage(null, "/usr/bin/local", 10, 2, Severity.ERROR, "errMsg");
        int ret = this.violationMessage.compareTo(messageWithGreaterColumnNumber);
        assertThat(ret, lessThan(0));
    }

    @Test
    public void testCompareToMessageWithLesserColumnNumber() {
        // global message has equal line number but lesser column number than test message
        ViolationMessage messageWithLesserColumnNumber =
            new ViolationMessage(null, "/usr/bin/local", 10, 0, Severity.WARNING, "warningMsg");
        int ret = this.violationMessage.compareTo(messageWithLesserColumnNumber);
        assertThat(ret, greaterThan(0));
    }

    @Test
    public void testCompareToMessageWithEqualLineAndColumnNumbers() {
        // global message has equal line number and column number wrt test message
        ViolationMessage messageWithEqualLineColumnNumberButDifferentText =
            new ViolationMessage(null, "/usr/bin/local", 10, 1, Severity.ERROR, "warningMsg");
        int ret = this.violationMessage.compareTo(messageWithEqualLineColumnNumberButDifferentText);
        assertTrue(ret < 0);
        ret = this.violationMessage.compareTo(this.violationMessage);
        assertEquals(0, ret);
        ViolationMessage messageWithEqualLineColumnNumberAndSameTextButDifferentSeverity =
            new ViolationMessage(null, "/usr/bin/local", 10, 1, Severity.WARNING, "errorMsg");
        ret = this.violationMessage.compareTo(messageWithEqualLineColumnNumberAndSameTextButDifferentSeverity);
        assertTrue(ret < 0);
    }

    @Test
    public void testEqualsUnequalMessage() {
        // Unequal ViolationMessages, verify that each field is checked for differences
        ViolationMessage unequalViolationMessage =
            new ViolationMessage(null, "/usr/bin/local", 12, 1, Severity.ERROR, "errMsg");
        assertNotEquals(this.violationMessage, unequalViolationMessage);

        unequalViolationMessage = new ViolationMessage(null, "/usr/bin/local", 10, 5, Severity.ERROR, "errMsg");
        assertNotEquals(this.violationMessage, unequalViolationMessage);

        unequalViolationMessage = new ViolationMessage(null, "/usr/bin/local/diff", 10, 1, Severity.ERROR, "errMsg");
        assertNotEquals(this.violationMessage, unequalViolationMessage);

        unequalViolationMessage = new ViolationMessage(null, "/usr/bin/local", 10, 1, Severity.WARNING, "errMsg");
        assertNotEquals(this.violationMessage, unequalViolationMessage);

        unequalViolationMessage = new ViolationMessage(null, "/usr/bin/local", 10, 1, Severity.ERROR, "warningMsg");
        assertNotEquals(this.violationMessage, unequalViolationMessage);
    }

    @Test
    public void testEqualsUnequalRule() {
        ViolationMessage unequalViolationMessage =
            new ViolationMessage(null, "/usr/bin/local", 12, 1, Severity.ERROR, "errMsg");
        assertNotEquals(this.violationMessage, unequalViolationMessage);
    }

    @Test
    public void testEqualsSameMessage() {
        // Equal ViolationMessages
        ViolationMessage equalViolationMessage =
            new ViolationMessage(Rules.LOWER_CAMEL_CASE, "/usr/bin/local", 10, 1, Severity.ERROR, "errMsg");
        assertEquals(this.violationMessage, equalViolationMessage);
    }

    @Test
    public void testToString() {
        String expectedOutput = "/usr/bin/local:10:1: error: [lower-camel-case] errMsg";
        assertEquals(expectedOutput, this.violationMessage.toString());
    }
}

@@ -3704,36 +3704,54 @@
 iolationMessage(
-null
+Rules.LOWER_CAMEL_CASE
 , %22/usr/bin/loca
@@ -3909,36 +3909,54 @@
 iolationMessage(
-null
+Rules.LOWER_CAMEL_CASE
 , %22/usr/bin/loca
@@ -3973,32 +3973,44 @@
  Severity.ERROR,
+%0A           
  %22errMsg%22);%0A    
@@ -4126,36 +4126,54 @@
 iolationMessage(
-null
+Rules.LOWER_CAMEL_CASE
 , %22/usr/bin/loca
@@ -4179,32 +4179,44 @@
 al/diff%22, 10, 1,
+%0A           
  Severity.ERROR,
@@ -4348,36 +4348,54 @@
 iolationMessage(
-null
+Rules.LOWER_CAMEL_CASE
 , %22/usr/bin/loca
@@ -4396,32 +4396,44 @@
 n/local%22, 10, 1,
+%0A           
  Severity.WARNIN
@@ -4567,36 +4567,54 @@
 iolationMessage(
-null
+Rules.LOWER_CAMEL_CASE
 , %22/usr/bin/loca
@@ -4615,32 +4615,44 @@
 n/local%22, 10, 1,
+%0A           
  Severity.ERROR,
@@ -4744,91 +4744,16 @@
 e);%0A
+%0A   
     
-%7D%0A%0A    @Test%0A    public void testEqualsUnequalRule() %7B%0A        ViolationMessage
  une
@@ -4770,36 +4770,24 @@
 ionMessage =
-%0A           
  new Violati
@@ -4788,36 +4788,49 @@
 iolationMessage(
-null
+Rules.BRACE_STYLE
 , %22/usr/bin/loca
@@ -4831,32 +4831,44 @@
 n/local%22, 12, 1,
+%0A           
  Severity.ERROR,
@@ -4950,32 +4950,36 @@
 Message);%0A    %7D%0A
+    
 %0A    @Test%0A    p

Fix ViolationMessageTest