package org.slc.sli.ingestion.util;

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();
        }
    }
}

@@ -30,16 +30,61 @@
 .util;%0A%0A
+import static org.junit.Assert.assertEquals;%0A
 import s
@@ -905,16 +905,28 @@
 sConfig,
+%0A           
  String 
@@ -1330,24 +1330,26 @@
 %7B%0A          
+  
 smooks.filte
@@ -1403,24 +1403,26 @@
 %7D finally %7B%0A
+  
           nr
@@ -1540,17 +1540,16 @@
 %0A    %7D%0A%0A
-%0A
     publ
@@ -1651,17 +1651,16 @@
 tor) %7B%0A%0A
-%0A
         
@@ -2065,11 +2065,534 @@
  %7D%0A    %7D
+%0A%0A    /**%0A     * Utility to make checking values in a map less verbose.%0A     *%0A     * @param map%0A     *            The map containing the entry we want to check.%0A     * @param key%0A     *            The string key for the entry%0A     * @param expectedValue%0A     *            The Object value we will assertEquals against%0A     */%0A    public static void assertObjectInMapEquals(Map map, String key, Object expectedValue) %7B%0A        assertEquals(%22Object value in map does not match expected.%22, expectedValue, map.get(key));%0A    %7D
 %0A%7D%0A

add utility method for checking values inside a map