package com.jenkins.plugins.rally.utils;

import com.jenkins.plugins.rally.connector.RallyDetailsDTO;
import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

public final class CommitMessageParserTest {

    @Test
    public void shouldParseWorkItemIdFromCommitMessage() {
        String commitMessage = "US12345: do a thing";

        RallyDetailsDTO details = CommitMessageParser.parse(commitMessage);

        assertThat(details.getId(), is(equalTo("US12345")));
    }

    @Test
    public void shouldParseDefectFromCommitMessage() {
        String commitMessage = "de12345: fix a bug";

        RallyDetailsDTO details = CommitMessageParser.parse(commitMessage);

        assertThat(details.getId(), is(equalTo("de12345")));
    }

    @Test
    public void shouldNotCatchOddballWorkItemCases() {
        String[] commitMessages = new String[]
                {
                        "HELLO BUS1, DO YOU READ?",
                        "IDE1 IS BETTER THAN IDE2",
                        "\"BASTA1!!\" él gritó"
                };

        for (String commitMessage : commitMessages) {
            RallyDetailsDTO details = CommitMessageParser.parse(commitMessage);
            assertThat(details.getId(), is(isEmptyString()));
        }
    }

    @Test
    public void shouldParseTaskIdFromCommitMessage() {
        String commitMessage = "US12345: (TA54321) do a thing";

        RallyDetailsDTO details = CommitMessageParser.parse(commitMessage);

        assertThat(details.getTaskID(), is(equalTo("TA54321")));
    }

    @Test
    public void shouldParseTaskIndexFromCommitMessage() {
        String commitMessage = "US12345: fixes #3";

        RallyDetailsDTO details = CommitMessageParser.parse(commitMessage);

        assertThat(details.getTaskIndex(), is(equalTo("3")));
    }

    @Test
    public void shouldParseTaskActualsFromCommitMessage() {
        String commitMessage = "US12345: fixes #3 with actuals: 15";

        RallyDetailsDTO details = CommitMessageParser.parse(commitMessage);

        assertThat(details.getTaskActuals(), is(equalTo("15")));
    }

    @Test
    public void shouldParseTaskStatusFromCommitMessage() {
        String[][] commitMessageAndStatusPairs = new String[][]
                {
                        { "US12345: #2 status: in progress", "In-Progress" },
                        { "US12345: #2 status: complete", "Completed" },
                        { "US12345: #2 status: define", "Defined" }
                };

        for (String[] pair : commitMessageAndStatusPairs) {
            RallyDetailsDTO details = CommitMessageParser.parse(pair[0]);

            assertThat(details.getTaskStatus(), is(equalTo(pair[1])));
        }
    }

    @Test
    public void shouldParseTaskToDoHoursFromCommitMessage() {
        String commitMessage = "US12345: fixes #3 with to do: 15";

        RallyDetailsDTO details = CommitMessageParser.parse(commitMessage);

        assertThat(details.getTaskToDO(), is(equalTo("15")));
    }

    @Test
    public void shouldMarkTaskToDoAsZeroWhenTaskStatusIsCompleted() {
        String commitMessage = "US12345: fixes #3 with status: completed";

        RallyDetailsDTO details = CommitMessageParser.parse(commitMessage);

        assertThat(details.getTaskToDO(), is(equalTo("0")));
    }

    @Test
    public void shouldParseTaskEstimationFromCommitMessage() {
        String commitMessage = "US12345: fixes #3 with estimate: 15";

        RallyDetailsDTO details = CommitMessageParser.parse(commitMessage);

        assertThat(details.getTaskEstimates(), is(equalTo("15")));
    }
}

@@ -783,32 +783,596 @@
 345%22)));%0A    %7D%0A%0A
+    @Test%0A    public void shouldParseWorkItemFromMultiLineCommitMessage() %7B%0A        String commitMessage = %22Do a thing%5Cnfixes US12345%22;%0A%0A        RallyDetailsDTO details = CommitMessageParser.parse(commitMessage);%0A%0A        assertThat(details.getId(), is(equalTo(%22US12345%22)));%0A    %7D%0A%0A    @Test%0A    public void shouldParseDefectFromMultiLineCommitMessage() %7B%0A        String commitMessage = %22fix a bug%5Cncorrects de12345%22;%0A%0A        RallyDetailsDTO details = CommitMessageParser.parse(commitMessage);%0A%0A        assertThat(details.getId(), is(equalTo(%22de12345%22)));%0A    %7D%0A%0A
     @Test%0A    pu
@@ -2886,32 +2886,34 @@
   %7B %22US12345: #2
+%5Cn
  status: in prog
@@ -2966,32 +2966,34 @@
   %7B %22US12345: #2
+%5Cn
  status: complet
@@ -3049,16 +3049,18 @@
 2345: #2
+%5Cn
  status:

add tests to ensure multiline commits are parsed correctly