package org.slc.sli.ingestion.smooks;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slc.sli.ingestion.FaultsReport;
import org.slc.sli.ingestion.FileFormat;
import org.slc.sli.ingestion.FileType;
import org.slc.sli.ingestion.IngestionTest;
import org.slc.sli.ingestion.handler.SmooksFileHandler;
import org.slc.sli.ingestion.landingzone.IngestionFileEntry;
import org.slc.sli.ingestion.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.xml.sax.SAXException;

/**
 * tests for SmooksFileHandler
 *
 * @author dduran
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/spring/applicationContext-test.xml" })
public class SmooksFileHandlerTest {

    @Autowired
    SmooksFileHandler smooksFileHandler;

    @Test
    public void firstRecordMissingColumnCsv() throws IOException, SAXException {

        // Get Input File
        File inputFile = IngestionTest.getFile("fileLevelTestData/invalidCSV/firstRecordMissingColumn/student.csv");

        // Create Ingestion File Entry
        IngestionFileEntry inputFileEntry = new IngestionFileEntry(FileFormat.CSV, FileType.CSV_STUDENT,
                inputFile.getName(), MD5.calculate(inputFile));
        inputFileEntry.setFile(inputFile);

        FaultsReport errorReport = new FaultsReport();
        smooksFileHandler.handle(inputFileEntry, errorReport);

        assertTrue("Missing column should give error.", errorReport.getFaults().size() != 0);
    }

    @Test
    public void incorrectColumnNameCsv() throws IOException, SAXException {

        // Get Input File
        File inputFile = IngestionTest.getFile("fileLevelTestData/invalidCSV/incorrectColumnName/student.csv");

        // Create Ingestion File Entry
        IngestionFileEntry inputFileEntry = new IngestionFileEntry(FileFormat.CSV, FileType.CSV_STUDENT,
                inputFile.getName(), MD5.calculate(inputFile));
        inputFileEntry.setFile(inputFile);

        FaultsReport errorReport = new FaultsReport();
        smooksFileHandler.handle(inputFileEntry, errorReport);

        assertTrue("Incorrect column name should give error.", errorReport.getFaults().size() != 0);
    }

    /*
     * XML TESTS
     */

    @Test
    public void incorrectAttributeXml() throws IOException, SAXException {

        // Get Input File
        File inputFile = IngestionTest
                .getFile("fileLevelTestData/invalidXML/firstRecordHasIncorrectAttribute/student.xml");

        // Create Ingestion File Entry
        IngestionFileEntry inputFileEntry = new IngestionFileEntry(FileFormat.EDFI_XML, FileType.XML_STUDENT,
                inputFile.getName(), MD5.calculate(inputFile));
        inputFileEntry.setFile(inputFile);

        FaultsReport errorReport = new FaultsReport();
        smooksFileHandler.handle(inputFileEntry, errorReport);

        assertTrue("Incorrect attribute should give error.", errorReport.getFaults().size() != 0);
    }

    @Test
    public void validXml() throws IOException, SAXException {

        // Get Input File
        File inputFile = IngestionTest.getFile("fileLevelTestData/validXml/student.xml");

        // Create Ingestion File Entry
        IngestionFileEntry inputFileEntry = new IngestionFileEntry(FileFormat.EDFI_XML, FileType.XML_STUDENT,
                inputFile.getName(), MD5.calculate(inputFile));
        inputFileEntry.setFile(inputFile);

        FaultsReport errorReport = new FaultsReport();
        smooksFileHandler.handle(inputFileEntry, errorReport);

        assertTrue("Valid XML should give no errors.", errorReport.getFaults().size() == 0);
    }
}

@@ -1686,39 +1686,27 @@
 rReport.
-getFaults().size() != 0
+hasErrors()
 );%0A    %7D
@@ -2379,39 +2379,27 @@
 rReport.
-getFaults().size() != 0
+hasErrors()
 );%0A    %7D
@@ -3141,31 +3141,751 @@
 ort.
-getFaults().size() != 0
+hasErrors());%0A    %7D%0A%0A    @Test%0A    public void valueTypeNotMatchAttributeType() throws IOException, SAXException %7B%0A%0A        // Get Input File%0A        File inputFile = IngestionTest%0A                .getFile(%22fileLevelTestData/invalidXML/valueTypeNotMatchAttributeType/student.xml%22);%0A%0A        // Create Ingestion File Entry%0A        IngestionFileEntry inputFileEntry = new IngestionFileEntry(FileFormat.EDFI_XML, FileType.XML_STUDENT,%0A                inputFile.getName(), MD5.calculate(inputFile));%0A        inputFileEntry.setFile(inputFile);%0A%0A        FaultsReport errorReport = new FaultsReport();%0A        smooksFileHandler.handle(inputFileEntry, errorReport);%0A%0A        assertTrue(%22Value type mismatch should give error.%22, errorReport.hasErrors()
 );%0A 
@@ -4507,24 +4507,25 @@
 o errors.%22, 
+!
 errorReport.
@@ -4528,31 +4528,19 @@
 ort.
-getFaults().size() == 0
+hasErrors()
 );%0A 

add file type mismatch test. utilize ErrorReport.hasErrors() interface method in tests.