If you're using BizTalk Server maps, it is about 99.999% likely that you are using logic in them.  Testing this functionality in Visual Studio can be tedious as the only way to change the test document is in the properties of the map.  So if you have quite a few scenarios to test, this method sucks.  BizTalk Server 2000 and 2002 allowed you to select the test document at runtime, which was nice I suppose but it still meant that you had to do it manually all the time.

With the exception of either figuring out how to use BizUnit and actually deploying your solution, there are not too many ways to quickly test multiple scenarios of documents in your maps.  I wanted to create a series of tests for a project that I was working on so that I could very quickly test the outcome of my BizTalk Server maps and essentially 'tick some boxes' before moving in to system testing.  There is nothing worse than a BizTalk Server deploy just to find out you've forgotten something that is key in your map.

Using BizTalk Server Maps in Code

I use Test Projects in Visual Studio to do a majority of my unit tests have recently stopped using NUnit.  What I wanted was a nice way to simply perform multiple BizTalk Server map tests at the click of a button.  Saving me a great deal of time and helping me to ensure my deployments contain maps that fulfil the project requirements.

I'll not go in to how to create the actual test projects, the purpose of this article is to call a BizTalk Server map in code so that we can examine the output.

In order to reference BizTalk Server maps, you must first add a reference to Microsoft.XLANGs.BaseTypes which can be found in C:\Program Files\Microsoft BizTalk Server 2006\Microsoft.XLANGs.BaseTypes.dll or wherever you ended up installing BizTalk Server.  Once you have this, it's actually pretty simple (although it took me a while to figure everything out and I was trying to do stuff manually that was already done for me).

Below is the code I used to execute a map.

// Create an instance of the map from the BizTalk Server project
MyBizTalkProjects.Schemas.MapProduct map = new MyBizTalkProjects.Schemas.MapProduct();

// Create an XPath document based on the XML file that is our source
System.Xml.XPath.XPathDocument source;

// Open the XML document from the file system and load it in to the XPath document
using (
    System.IO.StreamReader sRead = new StreamReader(
        @"D:\BraitrimDotNet\BizTalk\Schemas TestProject\EBizDocuments\DespatchConfirmation.xml"
        )
    )
{
    source = new System.Xml.XPath.XPathDocument(sRead);
}

// This took a while to figure out.  Essentially the map is a fully declared XslTransform.  as such, the
// extension objects (i.e. functiod references) are already referenced and so we can use this reference
// rather than trying to build it ourselves.
System.Xml.Xsl.XsltArgumentList args = map.TransformArgs;

// Set the XmlReader to the transorm.
System.Xml.XmlReader xReader = map.Transform.Transform(source, args);

// Perform the transform and load it in to an XmlDocument for examination
System.Xml.XmlDocument result = new System.Xml.XmlDocument(xReader.NameTable);
result.Load(xReader);

Once I have the XmlDocument, I can simply use XPath queries to check values in the XML match my anticipated output.

In Visual Studio, I essentially create a test method for piece of map logic that I want to test.  That way, when I run my tests from the Test View window, I can quickly see what has worked and what has not.


Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist
posted @ Thursday, July 10, 2008 4:27 PM | in BizTalk Server .Net Framework Visual Studio

Comments

No comments posted yet.

Post Comment

Title *
Name *
Email
Url
Comment *  


Please add 1 and 8 and type the answer here: