xmlschematransformbiztalk

How to unit test a BizTalk flat file map?


In the Visual Studio map editor, you can tell a BizTalk map to accept a flat file as input by setting its TestMap Input property to Native, and setting its TestMap Input Instance to the flat file in question, then choose Test Map. Under the hood, the map will use the flat file schema to transform the flat file to an XML file, putting it in the user's temp directory, and then use the map to transform that generated input XML to the required output XML.

Unfortunately, I can't seem to get this to work programmatically as a unit test. The test throws an XmlException about the input not being XML. Obviously it isn't, but I would hope that the behavior of running the map programmatically would be the same as when running it from the VS interface, in other words I would expect it to transform the input flat file to XML, and then execute the transform.

Can anyone tell me one of the following:

-- Is there a way to programmatically use a flat file as input to a map, maybe by setting some hidden property? In our generic code for executing maps programmatically, we use XslCompiledTransform, which takes the XSL representation of the supplied map to do the transform, but maybe I need to use a different method of applying the map?

-- If this is not possible, is there an easy way to programmatically do the same thing Visual Studio does when I select Test Map, ie create a temp XML file from the flat file according to the schema? It is easy to programmatically validate an XML file against a schema, but this works by loading the input into an XDocument and holding the schema against it, which method is impossible with a flat file. But Visual Studio is doing it, so there should be a way :-).

Thanks for your time and any tips or insights!


Solution

  • Have another test before it that tests the schema, or rather the flat file pipeline, and you use the output of that to test the map.

    Using the Unit Testing Feature with Pipelines

    [TestMethod()]
    public void FFReceivePipelineUnitTest()
    {
        //=== Pipeline class derived from TestableReceivePipeline ===//
        FFReceivePipeline target = new FFReceivePipeline();
    
        //=== Collection of messages to test the flat file pipeline ===//
        StringCollection documents = new StringCollection();
        string strSourcePO_XML = @".\..\..\..\FlatFileReceive_in.txt";
        Assert.IsTrue(File.Exists(strSourcePO_XML));
        documents.Add(strSourcePO_XML);
    
        //=== Only a body part for this test message so an empty ===//
        //=== collection will be passed.                         ===//
        StringCollection parts = new StringCollection();
    
        //=== Dictionary mapping the schema to the namespace and type ===//
        //=== as displayed in the properties window for the *.xsd     ===//
        Dictionary<string, string> schemas = new Dictionary<string, string>();
        string SchemaFile = @".\..\..\..\PO.xsd";
        Assert.IsTrue(File.Exists(SchemaFile));
        schemas.Add("Microsoft.Samples.BizTalk.FlatFileReceive.PO", SchemaFile);
    
        //=== Test the execution of the pipeline using the inputs ===//
        target.TestPipeline(documents, parts, schemas);
    
        //=== Validate that the pipeline test produced the message ===//
        //=== which conforms to the schema.                        ===//
        string[] strMessages = Directory.GetFiles(testContextInstance.TestDir + "\\out","Message*.out");
        Assert.IsTrue(strMessages.Length > 0);
        PO PO_target = new PO();
        foreach(string outFile in strMessages)
        {
          Assert.IsTrue(PO_target.ValidateInstance(outFile,Microsoft.BizTalk.TestTools.Schema.OutputInstanceType.XML));
        }
    }