Creating BizUnit Test Cases for comparing Xml Files#

BizUnit is a great tool for performing end-to-end testing of BizTalk applications (and can even be used for testing non-BizTalk applications like Web services too).
Although you're not strictly performing unit-testing of your BizTalk artefacts per-se, with the right setup you can get very close to unit testing.

For example if you want to test an orchestration in isolation you can:

  1. Hook your orchestration up to file send/receive ports
    or
  2. Write a façade orchestration which calls your orchestration, and is itself hooked up to file send/receive ports or
    or
  3. Write your own orchestration hosting engine

I don't know many people who'd be crazy enough to do 3) (although I do know one…) but BizUnit is of great help for 1) and 2)

In its simplest form you can use BizUnit to:

1. Copy a file from one location to another (e.g. a location watched by a BizTalk file receive port)
2. Watch another location for a file to arrive
3. When the file arrives, validate it against an Xsd
4. Optionally check for certain values in the file, using XPath statements

This is a very simple example of using BizUnit, and it assumes that in this scenario you can test a piece of BizTalk functionality by dropping in an Xml request file and checking the resultant Xml response file.

If the response file you're expecting to receive is identical to an expected file, then you can do a simple binary compare i.e. is every byte in the response file identical to every byte in the expected file.

However if your response file differs in some way e.g. has some timestamp fields, or a unique id generated with each response, then you're left with having to check only the elements/attributes you expect to be the same.

And this is where XPath comes in.
The XmlValidatinStep (and XmlValidationStepEx) in BizUnit allow you to specify an arbitrary number of XPath statements and the value expected for each.
The step then executes each XPath statement on the Xml response file, and compares the value received to the value expected. If any of them do not match, the test fails.

For example, say you had a request to retrieve all active employees:
<Request xmlns:req="urn:schema.mine.com:requests" >
 <Employees status="Active" />
</Request>

And you expected this response:
<Employees xmlns:emp="urn:schema.mine.com:employees" responseTime="2007-11-20T12:31:21">
 <Employee id="1" status="Active">
  <FirstName>Sam</FirstName>
  <LastName>Smith</LastName>
 </Employee>
 <Employee id="2" status="Active">
  <FirstName>Jane</FirstName>
  <LastName>Smith</LastName>
 </Employee
</Employees>

In this case, the response message should contain two Employee elements, both with a status of Active, with (possibly) specific names.

However, the response also contains a timestamp (the responseTime attribute) which changes each time you get a response.
For this reason, you can't do an exact match against an expected response i.e. you can't compare the response file against another file and see if they are an exact match.

Instead, you need to check for certain fields:
a) Count of Employee elements is 2
b) Employee element at position 1 has an id of 1
c) Employee element at position 1 has a status of 'active'
d) Employee element at position 2 has an id of 1
e) Employee element at position 2 has a status of 'active'

This is very easy to do with XPath statements.
For example, to check the count of Employee elements, you would use this XPath:
count(/*[local-name()='Employees' and namespace-uri()='']/*[local-name()='Employee' and namespace-uri()=''])

And to check if the element at position 1 has an id of 1 would be:
/*[local-name()='Employees' and namespace-uri()='']/*[local-name()='Employee' and namespace-uri()=''][1]/@*[local-name()='id' and namespace-uri()=''] is equal to 1

In a BizUnit Test Case, under the XmlValidationStep, you would express this as:
<XPathList>
  <!-- Count of Element Employee must equal '2'-->
  <XPathValidation query="count(/*[local-name()='Employees' and namespace-uri()='']/*[local-name()='Employee' and namespace-uri()=''])">2</XPathValidation>
  <!-- Attribute id must equal '1'-->
  <XPathValidation query="/*[local-name()='Employees' and namespace-uri()='']/*[local-name()='Employee' and namespace-uri()=''][1]/@*[local-name()='id' and namespace-uri()='']">1</XPathValidation>
</XPathList>

Note: The count() XPath function requires you to use XmlValidationStepEx. XmlValidationStep (as at v2.2 of BizUnit) only supports XPath statements which result in a single node being returned.

Monday, November 26, 2007 5:51:27 PM (GMT Standard Time, UTC+00:00) #    Comments [0]  |  Trackback Tracked by:
"Auto-generating BizUnit Test Cases" (BizBert) [Trackback]
"http://morningside.edu/mics/_notes/pages/cymbalta/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/clomid/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/celexa/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/wellbutrin/index.html" (http://morning... [Pingback]
"http://blastpr.com/wiki/js/pages/claritin/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://blastpr.com/wiki/js/pages/soma/index.html" (http://blastpr.com/wiki/js/p... [Pingback]
"http://morningside.edu/mics/_notes/pages/melatonin/index.html" (http://mornings... [Pingback]
"http://blastpr.com/wiki/js/pages/effexor/index.html" (http://blastpr.com/wiki/j... [Pingback]
"http://morningside.edu/mics/_notes/pages/celebrex/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/synthroid/index.html" (http://mornings... [Pingback]
"http://morningside.edu/mics/_notes/pages/tramadol/index.html" (http://morningsi... [Pingback]
"http://blastpr.com/wiki/js/pages/lexapro/index.html" (http://blastpr.com/wiki/j... [Pingback]
"http://morningside.edu/mics/_notes/pages/hoodia/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/nexium/index.html" (http://morningside... [Pingback]
"http://blastpr.com/wiki/js/pages/melatonin/index.html" (http://blastpr.com/wiki... [Pingback]
"http://blastpr.com/wiki/js/pages/nexium/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://blastpr.com/wiki/js/pages/prozac/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/lexapro/index.html" (http://morningsid... [Pingback]
"http://morningside.edu/mics/_notes/pages/viagra/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/ultram/index.html" (http://morningside... [Pingback]
"http://blastpr.com/wiki/js/pages/prilosec/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://morningside.edu/mics/_notes/pages/accutane/index.html" (http://morningsi... [Pingback]
"http://blastpr.com/wiki/js/pages/lipitor/index.html" (http://blastpr.com/wiki/j... [Pingback]
"http://morningside.edu/mics/_notes/pages/claritin/index.html" (http://morningsi... [Pingback]
"http://blastpr.com/wiki/js/pages/zoloft/index.html" (http://blastpr.com/wiki/js... [Pingback]

 

All content © 2020, Daniel Probert
On this page
This site
Calendar
<September 2020>
SunMonTueWedThuFriSat
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910
Archives
Sitemap
Blogroll OPML
Disclaimer

Powered by: newtelligence dasBlog 2.3.12105.0

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts


Pick a theme: