AIF: Testing Document services surpassing the AIF setup – Dynamics AX 2012

Testing document services is important to ensure that integrations go well. The hard part with testing AX services is that you will have to build SOAP xml’s and then configure AIF. Post that run AIF every time using a File system adapter or other adopter and verify the issues. The difficulties are

  • Running AIF every time
  • Modify the code to get the debugger call
  • If you are a first time user you run through a lot of AIF handling code before you land in the actual document logic.
  • Finding the error statement

Here is a quick job that will alleviate your pain completely.

The job directly feeds/exports the XML through document service class.It straight away starts the document service surpassing the entire AIF and can be used to validate the business logic.The advantages are

  • It is easy to run the test multiple times
  • The xml format is easy and doesn’t require any headers very easy and quick way to validate business logick
  • Doesn’t require you to run the AIF over and over
  • You don’t need to configure AIF
  • It works with AX 2009/AX 2012

End to End testing is very important but this way you can be pretty sure that your business logic works as desired before you go for the full flow testing.

How to use it ? Let us say that you are planning to test the sales order create service.

First you would need a XML to feed in. The best way to create a XML is to export data outside from AX  via the outbound document service.

public AifDocumentXml read(AifEntityKey                _aifEntityKey,
                           AifSchemaInfo               _xsdInfo,
                           AifEndpointActionPolicyInfo _actionPolicyInfo,
                           AifConstraintList           _constraintList,
                           AifPropertyBag              _aifPropertyBag)
{
    //throw error(strFmt("@SYS94920"));

    super(_aifEntityKey, 
         _xsdInfo, 
         _actionPolicyInfo, 
         _constraintListCollection, 
         _aifPropertyBag);
}

use this code to export the XML from a document service. You can modify this to make it work for any document service.

static void JobTestWebService_ExportXml(Args _args)
{
    AxdAddress address;
    AxdSalesOrder salesOrder;

    AifEntityKey    key;
    Map             map;

    //dummy variable
    AifPropertyBag bag;

    map = new Map(Types::Integer, Types::Container);
    //This is the unique index field(natural key based)
    //if nothing exists then a recid is used.
    //AxdBase.createEntityKeyFieldList() incase you struggle
    //finding the key.
    //map.insert(fieldnum(LogisticsPostalAddress, Recid), ['5637147252']);
    map.insert(fieldnum(SalesTable, SalesId), ['000001']);
    key = new AifEntityKey();
    key.parmTableId(tablenum(SalesTable));
    //key.parmTableId(tablenum(LogisticsPostalAddress));
    key.parmKeyDataMap(map);

    try
    {
        address = new AxdAddress();
        salesOrder = new AxdSalesOrder();
        info(salesOrder.read(key, null, new AifEndPointActionPolicyInfo(), new AifConstraintList(), bag));
       // info(address.read(key, null, new AifEndPointActionPolicyInfo(), new AifConstraintList(), bag));

    }
    catch
    {
        throw error('Error in document service outbound');
    }
}

Now modify the imported XML with the changes you need. E.g. Remove the SalesId, change the Item code or change the item line. Keep this minimal so what every item you want to test you could already enter in the sales line that you are planning to export and get it exported directly into XML.

All you need to do now to verify your import logic is just run the code mentioned here in a Job. What this code does is it directly calls the AX document service.

 

static void JobTestWebService_ImportXml(Args _args)
{
    //feed the xml that was modified after exporting through exportxml job
    XmlDocument xml = XmlDocument::newFile(@'C:\Test1.xml');
    AxdSalesOrder salesOrder;

    try
    {
        salesOrder = new AxdSalesOrder();
        salesOrder.create(xml.xml(),  new AifEndPointActionPolicyInfo(), new AifConstraintList());
    }
    catch
    {
        throw error('Error in document service');
    }
}

 

Sharpen your SSRS skills. Buy the Dynamics AX reporting cookbook

Keep yourself updated with Dynamics Ax – Check the weekly Must read section here