package uk.org.sappho.code.change.management.data;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import uk.org.sappho.string.mapping.Mapper;
import uk.org.sappho.warnings.SimpleWarningList;
import uk.org.sappho.warnings.WarningList;

public class RawDataTest {

    private RawData rawData;

    @Before
    public void setupRawData() {

        rawData = getFakeRawData();
    }

    @Test
    public void shouldValidate() {

        reWireFakeRawData(rawData);
        assertTrue(rawData.isValid());
    }

    @Test
    public void shouldFailToValidateDueToInvalidWarning() {

        reWireFakeRawData(rawData);
        rawData.getWarnings().add(null, null);
        boolean valid = rawData.isValid();
        assertFalse(valid);
    }

    public static RawData getFakeRawData() {

        String revisionKey = "42";
        String project = "LIFE";
        String issueKey = project + "-" + revisionKey;
        String issueSummary = "Add meaning to life.";
        Date revisionDate = new GregorianCalendar().getTime();
        String commitComment = issueKey + ": " + issueSummary;
        String committer = "sappho";
        String changedFile = "/sappho/fragments/Hymn to Aphrodite.txt";
        List<String> changedFiles = new ArrayList<String>();
        changedFiles.add(changedFile);
        List<String> badPaths = new ArrayList<String>();
        RevisionData revisionData = new RevisionData(revisionKey, revisionDate, commitComment, committer, changedFiles,
                badPaths);
        String issueType = "change";
        String component = "artistry";
        List<String> components = new ArrayList<String>();
        components.add(component);
        String release = "Fragments 0.1";
        List<String> releases = new ArrayList<String>();
        releases.add(release);
        IssueData issueData = new IssueData(issueKey, issueType, issueSummary, revisionDate, revisionDate, committer,
                project, components, releases);
        String warningCategory = "Loss";
        String warning = "Papyrus decays with time";
        WarningList warningList = new SimpleWarningList();
        warningList.add(warningCategory, warning);
        RawData rawData = new RawData();
        rawData.putRevisionData(revisionData);
        rawData.putIssueData(issueData);
        rawData.putWarnings(warningList);
        return rawData;
    }

    public static void reWireFakeRawData(RawData rawData) {

        Mapper issueKeyMapper = new Mapper() {
            public String map(String commitComment) {
                return commitComment.substring(0, 7);
            }
        };
        rawData.reWire(issueKeyMapper);
    }
}

@@ -731,32 +731,291 @@
 d());%0D%0A    %7D%0D%0A%0D%0A
+    @Test%0D%0A    public void shouldFailToValidateDueToMissingIssueKey() %7B%0D%0A%0D%0A        // the issueKey on the RevisionData will be missing because we haven't done a re-wire so this should always fail to be valid%0D%0A        assertFalse(rawData.isValid());%0D%0A    %7D%0D%0A%0D%0A
     @Test%0D%0A    p
@@ -1066,24 +1066,24 @@
 arning() %7B%0D%0A
-
 %0D%0A        re
@@ -1169,24 +1169,20 @@
     
-boolean valid = 
+assertFalse(
 rawD
@@ -1198,36 +1198,8 @@
 id()
-;%0D%0A        assertFalse(valid
 );%0D%0A

Check that raw data is invalid before a re-wire.