Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

BizUnit 4.0 TestStep for MQSC (Ibm Websphere Message Queue).

5.00/5 (1 vote)
21 Feb 2013CPOL10 min read 19.9K   210  
New TestStep in BizUnit 4.0 for MQSC

Introduction 

As we know BizTalk project is often a mission critical as middleware it always come in between different application and the BizTalk piece get down it lead to halt other system.So, it is a article about automation test of BizTalk using BizUnit4.0

BizUnit 4.0 - now available. This is a major release with significant changes making it much easier to create coded tests and also XAML tests. This new version 4.0 (BizUnit.TestSteps) don’t have support to MQSC by default, you need to add reference to BizUnit.MQSeriesSteps BUT It accept only XML base config not from C# code and BizUnit.MQSeriesSteps also been deprecated.So, I created new Test steps in BizUnit.TestSteps.

This article cover 

  • Very simple BizTalk project that read file and put message on MQSC.
  • How to created new MQSCStepHT test step in BizUnit 4.0 which facilate the access to MQSC just like other test steps (C# and XML/XMAL).
  • Test project to use MQSCStepHT 

Background  

BizUnit (Ref: http://bizunit.codeplex.com/) 

The adoption of an automated testing strategy is fundamental in reducing the risk associated with software development projects, it is key to ensuring that you deliver high quality software. Often, the overhead associated with developing automated tests is seen as excessive and a reason to not adopt automated testing.

BizUnit enables automated tests to be rapidly developed. BizUnit is a flexible and extensible declarative test framework targeted that rapidly enables the automated testing of distributed systems, for example it is widely used to test BizTalk solutions. BizUnit is fully extensible. Its approach is to enable test cases to be constructed from generic reusable test steps, test cases are defined in XML which allows them to be auto-generated and also enables the ‘fixing up’ of Url’s for different environments, e.g. test, staging and production environments. Defining test cases in XML enables test cases to be auto-generated.

This BizUnit test will do the following: 

  1. Copies the Input file into BizTalk project’s receive location.
  2. Read the Out data in MQ 
  3. Validates MQ data against the Out file format (XSD)
  4. Verify the content of the file (XPATH)  

Pre requesite

  1. You must be familiar with BizTalk
  2. Have Idea about BizUnit 
  3. Software needed:
  4. Visual Studio 2010 
    • BizTalk 2010 
    • BizUnit 4.0 (4.0.154.0) (http://bizunit.codeplex.com/releases/view/66850)
    • Message Queue on IBM webshere

Using the code 

Project: BizTalk MQSC 

This will be a project that we test using BizUnit. So we need to create this project first.

This will be going to be a very simple BizTalk project just enough to fulfill our immediate need. Here we brow the scenario of insurance company where we receiving basic enquire about policy. The policy details are stored into Mainframe system and we pass data to MF thru MQ. (Guys please don’t think about standardization of scenario. I know in real life this would be HIPPA)

I am attaching this BizTalk project with this article, so you can directly deploy it for test.

 Artifact of project: 

  • Two schema one for input and other for MF input (out put of BizTalk project)
  • One map to perform mapping between Input file and MF file
  • Orchestration to design the flow of message and bind the port

Input Schema 

This structure of input message. Where we are getting policy holder ID, name, address and employer Id.

Image 1

Instance of this Schema 

XML
<ns0:Ind xmlns:ns0="http://HT1_MQ.SchIN">
  <ID>ABC009</ID> 
  <FName>Himanshu</FName> 
  <LName>Thawait</LName> 
  <Address>KCMO</Address> 
  <EmployerID>iGate01</EmployerID> 
</ns0:Ind>
MF Schema

This structure of MF input message. Which need Group policy number and member number to pull all the details 

Image 2

Instance of this Schema

Will be the out put of BizTalk project

Map

A very simple map, which mapping incoming message’s ID filed to MF schema’s MemNum filed and EmployeeID to GrpPolicyNum filed by appending the “GRP:” using String Concatenate functoid

Image 3

Orchestration

Here is our orchestration which have one receive port that will use to pickup the message (input xml file) from folder and give it to Rec shape then it’s  make event log entry by using Expression share and orchestration variable (Trace Log) and construct the message for MF using above Map. It again make event log about completion of  process and send this message to send port (H1_MQ_SEN) which is banded with MQSC.

Image 4 

How to specify the MQSC binding:

Her is one catch : MQSC wont run on 64 bit BizTalk Host Instance so, you need to create 32 bit Host Instance for you Orchestration and Port which connect to MQSC 

Image 5

Deployment 

Project is ready to deploy, assign the snk and give the name to Application, set configurations and server and deploy.Details about deployment: (http://msdn.microsoft.com/en-us/library/aa559168.aspx)

Project: BizUnit.TestSteps-Ench-Create of MQSCStepHT test step 

Before going into detail I wan to highlight about pre requisite #2(Have Idea about BizUnit). I am assuming by this point you have downloaded and installed the BizUnit 4.0 and also unzipped it’s source code “BizUnit.Source” @C:\Program Files\BizUnit\BizUnit 4.0\Source (default location).

I am NOT attaching the entire BizUnit.TestStep in this article. You need to add following class in you existing BizUnit.TestStep.

Now it’s time to add new TestSteps for MQSC

  • Go under “BizTalk\BizUnit4.0_Src\Src\BizUnit.TestSteps” and Open “BizUnit.TestSteps”
  • Add the reference to “amqmdnet.dll” I am using ver 1.0.0.2. This IBM’s dll for Implementation WebSphere MQ Classes for .NET. References should looks like this:
Image 6
  • Add new folder “MQSCHT” to the project
  • Copy “MQSeriesHelper.cs” from C:\Program Files\BizUnit\BizUnit 4.0\Source\BizUnit4.0_Src\Src\BizUnit.MQSeriesSteps\Utilities to “MQSCHT”. This is helper class created by Kevin B. Smith to work with MQSC
  • Add new class file “MQSCGetStepHT.cs” in to “MQSCHT”

Image 7

Code of MQSCGetStepHT.cs 

The code is pretty self explanatory. However it’s good to explain it little bit. The heard of this class is  TestStepBase all the BizUnit test steps must be derive from this make this class to be a BizUnit TestStep. It has two abstract methods that we need to implement here. 

It has a constructor where we unitize the Sub Step. The subStep is “The list of sub-steps to be executed by the test step, there maybe zero or more sub-steps. Each sub-step is called, with the data being assed from one to the next typically.”

Execute(Context context): Where we actual logic and code is written for MQSC read. Executes the test steps logic.Parameters: context: The test context being used in the current TestCase

Validate (Context context): You can write validation logic here like check for some required properties and there valid values. This will run when test steps is being execute.

C#
///Himanshu Thawait
///Test step to work with MQSC. using amqmdnet.dll and MQSeriesHelper.cs

using System;
using System.Collections.ObjectModel;
using System.IO;
using BizUnit.TestSteps.Common;
using BizUnit.Xaml;

namespace BizUnit.TestSteps.MQSCStepHT
{

    public class MQSCGetStepHT : TestStepBase
    {
        private string message;

        /// <summary>
        /// Que Manager details server (port)
        /// </summary>
        public string queueManager { get; set; }
        /// <summary>
        /// Query name
        /// </summary>
        public string queue { get; set; }
        /// <summary>
        /// Wait time out
        /// </summary>
        public int waitTimeout { get; set; }

        /// <summary>
        /// Used for Sub steps
        /// </summary>
        public MQSCGetStepHT()
        {
            SubSteps = new Collection<SubStepBase>();
        }

        /// <summary>
        /// Read the data from MQ
        /// </summary>
        /// <param name="context"></param>
        public override void Execute(Context context)
        {
            context.LogInfo("Reading queue: {0}, search queueManager: {1}", queue, queueManager);
            message = MQSeriesHelper.ReadMessage(queueManager, queue, waitTimeout, context);
            context.LogData("MQSeries output message:", message);

            // SubSteps
            // Check it against the validate steps to see if it matches one of them
            foreach (var subStep in SubSteps)
            {
                try
                {
                    Stream fileData = StreamHelper.LoadMemoryStream(message);
                    // Try the validation and catch the exception
                    fileData = subStep.Execute(fileData, context);
                }
                catch (Exception ex)
                {
                    context.LogException(ex);
                    throw;
                }
            }
        }

        /// <summary>
        /// Validation on MQ and Data  data
        /// </summary>
        /// <param name="context"></param>
        public override void Validate(Context context)
        {
            if (string.IsNullOrEmpty(queueManager))
            {
                throw new StepValidationException("queueManager may not be null or empty", this);
            }

            if (string.IsNullOrEmpty(queue))
            {
                throw new StepValidationException("queue may not be null or empty", this);
            }

        }
    }
}

Code of MQSeriesHelper.cs

This is helper call for MQSC by Kevin B. Smith. No changes here, using as it is. 

Build

We are all set to build the project and use in Test project. So build he project, it should update the “BizUnit.TestSteps.dll” check for the Date Modified to verify it is correct one. It has new test steps “MQSCStepHT” 

Project: Test project 

Now we have veering the ready to test. BizTalk project is running picking up the file and putting into MQSC and our new TestStep is available in BizUnit to read from MQSC. Create new Test project and add the reference to BizUnit.dll and BizUnit.TestSteps.dll default location C:\Program Files\BizUnit\BizUnit 4.0\Bins\. References should looks like this: I am attaching this Test project with this article, so you can directly deploy it for test.

Image 8 

Finally we are all set to write test case for MQSC. This test project will going to use the same file drop location, Schema and XML file of BizTalk MQSC project (obviously as we going to test that project).

  • File drop location: \\FileServer\Share\HT1\IN_MQ\ same location that is being used by BizTalk MQSC project
  • Input files: IN_MQ.xml
  • Schma :  SchOUT.xsd same schema of out message (MF Schema) of BizTalk MQSC

Code of HT1_MQ.cs

We have two test cases on this class. One is for copying the xml file in BizTalk pickup location using BizUnit.TestSteps.File (out of the box test step) and other is to read data from MQSC using our BizUnit.TestSteps.MQSCStepHT. 

Things to remember here is:

RunTest() is a key here, what ever code you have written (in MQSCStepHT) is not going to actually execute until this RunTest() get called. So, if you going to debug the code then wait this function get call then your breakpoint will hit (in MQSCStepHT)

To know about the BizUnit Test steps read: “BizUnit Getting Started Guide.pdf” Which come with BizUnit 4.0 install. Default location C:\Program Files\BizUnit\BizUnit 4.0

In my test case I decided to label the code in 6 steps.  
  • Step 1: create object of TestCase, that is actually the test case which is going to have many thing.
  • Step 2: Preparation step where you setup thing to assign into Execution step. Like setup file drop location and assign source file path. 
  • Step 3: Execution step where you assign the step 2 to step 1 testcase.
  • Step 4: Is an optional step where you can save you test case in XMAL file format. As BizUnit can also work pretty well with XMAL base test steps.
  • Step 5: Create BizUnit instance to run. Assign the step 1 test case in to BizUnit object.
  • Step 6: Actually run the test by RunTest().

Here is the complete code: 

C#
using BizUnit.TestSteps.Common;
using BizUnit.TestSteps.DataLoaders.File;
using BizUnit.TestSteps.File;
using BizUnit.TestSteps.MQSCStepHT;
using BizUnit.TestSteps.ValidationSteps.Xml;
using BizUnit.Xaml;
using Microsoft.VisualStudio.TestTools.UnitTesting;


namespace HT1.TestData
{
    [TestClass]
    public class HT1_MQ
    {
        [TestMethod]
        public void MQ_TC1_CopyFileToProcess()
        {
            //1. Creat test case
            var TC1 = new TestCase { Name = "H31 TC2", Category = "In Run", Preconditions = "H31 TC1" };

            //2.0 Prepration for **Execution Step**- Copy file to IN folder
            var createStep = new CreateStep();
            createStep.CreationPath = @"\\FileServer\Share\HT1\IN_MQ\IN_MQ.xml";
            //2.3 Source File
            var dataLoder = new FileDataLoader { FilePath = @"\\FileServer\Share\HT1\IN_MQ.xml" };

            createStep.DataSource = dataLoder;


            //3 Execution Step- Copy file
            TC1.ExecutionSteps.Add(createStep);

            //4 Optional steps- Save Test case to XMAL file
            TestCase.SaveToFile(TC1, TC1.Name + ".xmal");

            //5.Create BizUnit instance to run
            var bizunit = new BizUnit.BizUnit(TC1);

            //6.Actually run the test
            bizunit.RunTest();
        }


        [TestMethod]
        public void MQ_TC2_Read()
        {
            //1. Create Test case
            var TC1 = new TestCase { Name = "H31.MQ.TC1", Category = "IN run", Purpose = "Verify MQData" };

            //2.0 Prepration for **Execution Step**- Setup MQSC setting
            var mqscStep = new MQSCGetStepHT();
            mqscStep.queueManager = "MQSCMger";
            mqscStep.queue="MQSC.TEST.IN";


            //2.1 Validate XML again Schems
            var xmlValidationStep = new XmlValidationStep();
            var schemaDefination = new SchemaDefinition
            {
                XmlSchemaPath = @"\\FileServer\Share\HT1\SchOUT.xsd",
                XmlSchemaNameSpace = "http://HT1_MQ.SchOUT"
            };

            //2.2 Adding schemt to validate
            xmlValidationStep.XmlSchemas.Add(schemaDefination);

            //2.3 Validate XML data Using XPath
            var xpathRecCount = new XPathDefinition
            {
                Description = "Checking the GrpPolicyNum",
                XPath = "/*[local-name()='Member' and namespace-uri()='http://HT1_MQ.SchOUT']/*[local-name()='GrpPolicyNum' and namespace-uri()='']",
                Value = "GRP:iGate01"
            };


            //3 Execution Step- check MQ data
            xmlValidationStep.XPathValidations.Add(xpathRecCount);
           
            //3.1 sub step
            mqscStep.SubSteps.Add(xmlValidationStep);

            //3.2 Main step
            TC1.ExecutionSteps.Add(mqscStep);

            //4 Optional steps- Save Test case to XMAL file
            TestCase.SaveToFile(TC1, TC1.Name + ".xmal");

            //5.Create BizUnit instance to run
            var bizunit = new BizUnit.BizUnit(TC1);

            //6.Actually run the test
            bizunit.RunTest();

           
        }
    }
}
MQ_TC2_Read() :

Has some extra thing on Step 2 (2.1, 2.2, 2.3) that is for validation of message format and data in it.

  • Step 2.0: we just step the MQSC details
  • Step 2.1 & 2.2: We know that data is in XML format and we have the schema of that xml. So, we want to verify MQ data against schema.
  • Step 2.3: We also want dig into the node level and verify the value in it. So we are using the xpath query to get node’s value. We got this xpath query from BizTalk schema editor “instance xpath” properties.

To get instance xpath 

Image 9

Now it’s time to run:

BizUnit test case will run just like any other test case. Jusr select your test case, right click and hit Run

Image 10 

As you can see you can select more then one test case to run. To specify the permanent order of test case execution you can add “Order test” and here can see the list of test case and select them in desire order. Then instead of select individual test case you can simply select and run this Order test file.

Image 11 

Things to remember: If you want to debug the code and hits the break point then right click on test case and select “Debug Selection”

Test Result:

When you run the test case “Test Result” tab will come in form in bottom of you VS. Here you can see the test case status like pass, failed, Abraded and running. To see in details right click on test result and select “View Test Result Details”

Image 12

Here is how test details looks like, and let see how to red it:

IT has two parts: 

  • Common Result will just give you information about who run when runs which computer etc.
  • Standard Console Output: Here you get most of the thing. I marked 4 place and lest see what each of it tell us.
  1. Tell us about MQSC config values
  2. Tell us what data we have in MQSC
  3. Going to verify the Schema and value
  4. 3.1: Saying value is as expected. So test got pass 
Image 13

Each test run will store the complete details in “.trx” file which is “Visual Studio Test Results File” format @ folder “TestResults” Name of file has user who run this test case plus computer name along with date time stamp.

Finally it’s complete hope it will help you.

Next Steps  

I have written test steps for REStfull service and for Oracle as well, will create similar article for that too.

References  

BizUnit4.0 : http://bizunit.codeplex.com/

BizUnit File step and I also like the article style : http://www.codeproject.com/Articles/243970/BizUnit-4-0-and-BizTalk-2010

History 

  • Version 1.0 Dt: 21-Feb-2013

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)