I am building a web service for one of my clients and another company is using this web service for an aspx web site. I realized that I needed to test my web service before I can deliver it to that company. So, I created a test codeunit for the job.
First I downloaded the universal XML import/export tool from Mibuso. Then I added a function to the table 60000 XML Buffer that is in the above tool.
[code htmlscript=”false”]Read(VAR DOMDoc : Automation "’Microsoft XML, v6.0′.DOMDocument")
DELETEALL;
DOMNode := DOMDoc.documentElement;
Import2(DOMNode,1);
IF FINDFIRST THEN;[/code]
Next I create a read function in my test codeunit for every function in the web service. Here is an example.
[code htmlscript=”false”]GetFarmerTankEntryAverageYW(FarmerID : Integer;MinYear : Integer;MaxYear : Integer;MinWeekNo : Integer;MaxWeekNo : Integer)
GetSetup;
CREATE(XMLDoc,TRUE,FALSE);
XMLProsInstr := XMLDoc.createProcessingInstruction(‘xml’,’version="1.0" encoding="utf-8"’);
XMLDoc.appendChild(XMLProsInstr);
CreateEnvelope(XMLElement1);
XMLElement2 := XMLDoc.createElement(‘soap:Body’);
XMLElement3 := XMLDoc.createElement(‘GetFarmerTankEntryAverageYW’);
XMLElement3.setAttribute(‘xmlns’,’urn:microsoft-dynamics-schemas/codeunit/RMWeb’);
CreateElement(XMLElement3, ‘farmerID’, FORMAT(FarmerID,0,9), ”, ”);
CreateElement(XMLElement3, ‘minYear’, FORMAT(MinYear,0,9), ”, ”);
CreateElement(XMLElement3, ‘maxYear’, FORMAT(MaxYear,0,9), ”, ”);
CreateElement(XMLElement3, ‘minWeekNo’, FORMAT(MinWeekNo,0,9), ”, ”);
CreateElement(XMLElement3, ‘maxWeekNo’, FORMAT(MaxWeekNo,0,9), ”, ”);
CreateElement(XMLElement3, ‘tankEntryXML’, ”, ”, ”);
XMLElement2.appendChild(XMLElement3);
XMLElement1.appendChild(XMLElement2);
XMLDoc.appendChild(XMLElement1);
WinHTTP.open(‘POST’,ServiceURL,FALSE,UserName,Password);
WinHTTP.setRequestHeader(‘Content-Type’,’text/xml; charset=utf-8′);
WinHTTP.setRequestHeader(‘SOAPAction’,’GetFarmerTankEntryAverageYW’);
WinHTTP.send(XMLDoc);
IF WinHTTP.status <> 200 THEN
ERROR(Text003,WinHTTP.status,WinHTTP.statusText);
XMLResponseDoc.load(WinHTTP.responseXML);
DisplayDocument(XMLResponseDoc);[/code]
This will use the XML Buffer to read the response document and display the result. The Text Constant Text003 contains
[code htmlscript=”false”]ENU=Status error %1 %2;ISL=Stöðuvilla %1 %2[/code]
and the four functions used here contain
[code htmlscript=”false”]DisplayDocument(VAR XMLDoc : Automation "’Microsoft XML, v6.0′.DOMDocument")
XMLBuffer.Read(XMLDoc);
COMMIT;
FORM.RUNMODAL(FORM::"XML Buffer");
CreateEnvelope(VAR InElement : Automation "’Microsoft XML, v6.0′.IXMLDOMElement")
InElement := XMLRequestDoc.createElement(‘soap:Envelope’);
InElement.setAttribute(‘xmlns:soap’,’http://schemas.xmlsoap.org/soap/envelope/’);
InElement.setAttribute(‘xmlns:xsi’,’http://www.w3.org/2001/XMLSchema-instance’);
InElement.setAttribute(‘xmlns:xsd’,’http://www.w3.org/2001/XMLSchema’);
CreateElement(VAR InElement : Automation "’Microsoft XML, v6.0′.IXMLDOMElement";InNodeName : Text[50];InNodeValue : Text[250];InAttribu
TempElement := XMLRequestDoc.createElement(InNodeName);
TempElement.nodeTypedValue(InNodeValue);
IF InAttributeName <> ” THEN
TempElement.setAttribute(InAttributeName,InAttributeValue);
InElement.appendChild(TempElement);
GetSetup()
ServiceURL := ‘http://gunnar.dynamics.is:7047/DynamicsNAV/WS/CRONUS/Codeunit/WebService’;
UserName := ‘<Domain\User>’;
Password := ‘<Password>’;
IF ISCLEAR(WinHTTP) THEN
CREATE(WinHTTP,TRUE,FALSE);
IF ISCLEAR(XMLResponseDoc) THEN
CREATE(XMLResponseDoc,TRUE,FALSE);[/code]