package org.slc.sli.ingestion.util;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import javax.xml.transform.stream.StreamSource;

import org.junit.Assert;
import org.milyn.Smooks;
import org.xml.sax.SAXException;

import org.slc.sli.domain.Entity;
import org.slc.sli.ingestion.NeutralRecordFileReader;
import org.slc.sli.ingestion.NeutralRecordFileWriter;
import org.slc.sli.ingestion.smooks.SmooksEdFiVisitor;
import org.slc.sli.validation.EntityValidationException;
import org.slc.sli.validation.EntityValidator;
import org.slc.sli.validation.ValidationError;

/**
 *
 * @author ablum
 *
 */
public class EntityTestUtils {

    public static NeutralRecordFileReader getNeutralRecords(InputStream dataSource, String smooksConfig,
            String targetSelector) throws IOException, SAXException {
        File outputFile = File.createTempFile("test", ".dat");
        outputFile.deleteOnExit();
        NeutralRecordFileWriter nrfWriter = new NeutralRecordFileWriter(outputFile);

        Smooks smooks = new Smooks(smooksConfig);

        smooks.addVisitor(SmooksEdFiVisitor.createInstance("record", nrfWriter), targetSelector);

        try {
            smooks.filterSource(new StreamSource(dataSource));
        } finally {
            nrfWriter.close();
        }

        return new NeutralRecordFileReader(new File(outputFile.getAbsolutePath()));
    }

    public static void mapValidation(Map<String, Object> obj, String schemaName, EntityValidator validator) {

        Entity e = mock(Entity.class);
        when(e.getBody()).thenReturn(obj);
        when(e.getType()).thenReturn(schemaName);

        try {
            Assert.assertTrue(validator.validate(e));
        } catch (EntityValidationException ex) {
            for (ValidationError err : ex.getValidationErrors()) {
                System.err.println(err);
            }
            Assert.fail();
        }
    }

    /**
     * Utility to make checking values in a map less verbose.
     *
     * @param map
     *            The map containing the entry we want to check.
     * @param key
     *            The string key for the entry
     * @param expectedValue
     *            The Object value we will assertEquals against
     */
    public static void assertObjectInMapEquals(Map map, String key, Object expectedValue) {
        assertEquals("Object value in map does not match expected.", expectedValue, map.get(key));
    }
}

@@ -152,24 +152,61 @@
 kito.when;%0A%0A
+import java.io.ByteArrayInputStream;%0A
 import java.
@@ -460,16 +460,60 @@
 Entity;%0A
+import org.slc.sli.ingestion.NeutralRecord;%0A
 import o
@@ -2473,16 +2473,50 @@
      */%0A
+    @SuppressWarnings(%22rawtypes%22)%0A
     publ
@@ -2703,11 +2703,1191 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Utility to run smooks with provided configurations and return the first NeutralRecord (if%0A     * there is one)%0A     *%0A     * @param smooksXmlConfigFilePath%0A     *            path to smooks config xml%0A     * @param targetSelector%0A     *            selector for this run%0A     * @param testData%0A     *            string we want to use as the input for smooks run%0A     * @return first NeutralRecord%0A     * @throws IOException%0A     * @throws SAXException%0A     */%0A    public static NeutralRecord smooksGetSingleNeutralRecord(String smooksXmlConfigFilePath, String targetSelector,%0A            String testData) throws IOException, SAXException %7B%0A%0A        ByteArrayInputStream testDataStream = new ByteArrayInputStream(testData.getBytes());%0A%0A        NeutralRecord neutralRecord = null;%0A        NeutralRecordFileReader nrfr = null;%0A        try %7B%0A            nrfr = EntityTestUtils.getNeutralRecords(testDataStream, smooksXmlConfigFilePath, targetSelector);%0A%0A            Assert.assertTrue(nrfr.hasNext());%0A%0A            neutralRecord = nrfr.next();%0A        %7D finally %7B%0A            if (nrfr != null)%0A                nrfr.close();%0A        %7D%0A%0A        return neutralRecord;%0A    %7D
 %0A%7D%0A

add utility for running smooks and getting a NeutralRecord for entity tests