Testing web services with test case support in WebSphere Integration Developer

This article will discuss how to test web service modules with test case support in WebSphere Integration Developer. It also explains how to use Java expressions to reference a field in another test variable in their test cases.

Flannan Lo (flannanl@ca.ibm.com), Staff Software Developer, IBM

Flannan Lo is a software developer at the IBM Canada Lab. Flannan is works primarily on the development of integration test client and test case support in WebSphere Integration Developer.



27 September 2010

Also available in

Summary

This article shows users how to test their web service modules with test case support in WebSphere Integration Developer. I have discovered that most users do not know why and how to use Java expressions to set their test variables in their test cases. This article will explain how to use Java expressions to reference a field in another variable. More importantly, this article will also expose some built-in Java utilities that allow users to reference a specific field in a SOAP message variable.

Introduction

One of the enhanced features offered in test case support in WebSphere Integration Developer v7.0 is the ability to test web service modules with Simple Object Access Protocol (SOAP) attachments. In a production environment, when clicking a button on a web page, it could involve a chain of invocations of different web service modules. After you have finished developing your web services, you may want to test them as if they are being invoked in a real production environment. The test case support allows you to create your test scenarios to verify that your web service modules work as expected.

This article shows you how to use the test case support in WebSphere Integration Developer to create your scenario-based test cases for your web services with SOAP attachments. In a scenario test case, you often want to use the response SOAP message returned from a web service to invoke another web service; you are shown in-depth how to do it using the Java expression feature in the test case support.

This article is prepared in WebSphere Process Server v7.0.0.3 and so you are strongly recommended to update your copy of WebSphere Integration Developer and WebSphere Process Server to v7.0.0.3. Some of the APIs mentioned in this article may not be available prior to v7.0.0.3.


Create a web service module

Before creating a scenario test case, we will briefly show you how to create a web service module in WebSphere Integration Developer v7.0. For the purpose of this article, you will create two web service exports so that you can test them in your scenario test case. Note: This sample is prepared in WebSphere Integration Developer v7.0.0.3 which supports WS-I compliant SOAP attachments.

The following steps guide you how to create the web services:

  1. Import the WebService_InitialPI.zip project interchange file.
  2. Open the Assembly Diagram of WebServiceModule.
Figure 1. Assembly Diagram of WebServiceModule
Assembly Diagram of WebServiceModule

In the Assembly Diagram, there are two mediation components. One is called UserMediation, which is a service for searching a user record for a given user ID. It takes a user ID and a picture attachment as parameters and returns the corresponding user record. The other component is called AttachmentMediation, which is a service for submitting a PDF file for a registered user. It takes the user record (returned by UserMediation) and a PDF attachment as parameters and returns the submission status and the submitted PDF attachment.

  1. Select UserMediation component and select Generate Export > Web Service Binding from the context menu.
  2. Select SOAP1.1/HTTP and click Finish.
Figure 2. Select transport protocol for UserInterfaceExport1
Select transport protocol for UserInterfaceExport1
  1. In the Assembly Diagram, select AttachmentMediation component and select Generate Export > Web Service Binding from the context menu.
  2. Select SOAP1.1/HTTP and click Next.
  3. Click Next on the next page to proceed to the final page.
  4. Select Use WS-I compliant SOAP message and click Finish.
Figure 3. Specify WS-I compliance for AttachmentInterfaceExport1
Specify WS-I compliance for AttachmentInterfaceExport1
  1. Save the Assembly Diagram.
  2. Select UserInterfaceExport1 and open the Properties view.
  3. Select the Name field and press Alt+Shift+R to rename the export name to UserWebService.
  4. Select AttachmentInterfaceExport1 and open the Properties view.
  5. Select the Name field and press Alt+Shift+R to rename the export name to AttachmentWebService.
Figure 4. Web services created in the Assembly Diagram
Web services created in the Assembly Diagram

You have now created two web services and you want to test them. The next section will show you how to test them using the test case support available in WebSphere Integration Developer.


Create a scenario-based test case

Using the test case support in WebSphere Integration Developer, you can create a scenario-based test case that involves multiple invocations of your modules. In this section, you will be shown how to create one for the web services you created in the previous section.

In the following steps, you will create a test case to test the web services:

  1. Select WebServiceTestProject in the Business Integration view.
  2. Select New > Component Test Suite from the context menu.
  3. Specify a name for the new test suite and click Next.
  4. Select Scenario-based testing and click Next.
Figure 5. Select test pattern
Select test pattern
  1. On the Define a Test Scenario page, you want to create a test case to test the two web services you created in the previous section. First, you want to invoke the getUser method in the UserWebService to find the user record for a user ID, then you want to invoke the submitDocument method in the AttachmentWebService to submit a PDF file for that user. To create the test case, do the following:
    • Specify a name for your test case.
    • Select WebServiceModule > UserWebService > UserInterface in the Available components and interfaces section.
    • Double-click getUser in the Testable operations section. You will then see the operation is added to the Test Scenario section.
    • Now, select WebServiceModule > AttachmentWebService > AttachmentInterface in the Available components and interfaces section.
    • Double-click submitDocument in the Testable operations section. You will then see the operation is added to the Test Scenario section.
Figure 6. Define a scenario test case
Define a scenario test case
  1. Click Finish to create the test scenario.

You will find the new test suite opened in the test suite editor. Now, it is time to set the parameter values for your invocations and the expected results.

Setting Values in Your Test Case

After the test suite is created, let's specify the test case details in the test suite editor. If you open the Test Data Table view, you will see a few variables defined and these variables are being used in the invocation tasks in the test case.

The following steps show you how to set the values in the test case variables:

  1. Switch to the Test Cases page in the test suite editor.
  2. Select 1. Invoke UserWebService:getUser task.
  3. Scroll to the bottom of the Detailed Properties section. You will see the Request tab. There are two variables defined in the Request tab.
  4. Click the request link. This will bring you to the associated variable in the Test Data Table view.
  5. Expand the SOAP body of the request variable.
  6. Type an arbitrary ID for the userId field. E.g. 12345.
  7. Select the picture field and click the cell in the Type column.
  8. Select image/gif from the drop-down menu.
Figure 7. Select the content type of swaRef attachment
Select the content type of swaRef attachment
  1. Right click the cell to launch the context menu and select Browse for File. Note: The application of the file that you select must also be deployed on the server when you run your test case. Therefore, it is highly recommended to store your attachment files in the test project where you create your test cases.
  2. In the Browse for File dialog, expand WebServiceTestProject.
  3. Select picture.gif and click Finish.
  4. Go back to the Test Cases page of the test suite editor.
  5. Switch to the Response tab.
  6. Click the response link. This will bring you to the associated variable in the Test Data Table view.
  7. Right click any column cell of the response variable and select Set To > Equals Default from the context menu.
  8. Expand the SOAP body and specify the following values:
    • Type the ID you specified in step 6 (E.g. 12345) in the userId field.
    • Type Joe in the firstname field.
    • Type Smith in the lastname field.
    • Type /WebServiceTestProject/picture.gif in the picture field.
Figure 8. The expected SOAP message returned from the web service
The expected SOAP message returned from the web service
  1. Save the test suite.

You have finished setting the values for testing the first web service invocation. You want to reference some values in the response SOAP message of the first invocation in your second invocation. The following section shows you how to do it with Java expressions.

Using Java Expressions

The test case support in WebSphere Integration Developer allows you to use Java expressions to reference a field from another variable. The Test Data Table view provides content assist to help you specify Java expression for the variables. This sample will focus on how to use Java expression to access a field in a SOAP message. When a variable is a SOAP message type, you can write your own Java code to use XPath expressions to retrieve a field in the SOAP message. Alternatively, you can also use the built-in utilities provided by the test case support to retrieve a specific SOAP message field. These built-in utilities are primarily used by the test case support to verify the SOAP message returned from the web services.

The following steps show you how to use the built-in utilities to reference a field from another SOAP message variable:

  1. Before you can call a Java method, you must import the Java class that defines the method in the Test Data Table view:
    • Go to the Imports page of the Test Data Table view.
    • Click Add. The Select Type to Import dialog opens.
    • Select com.ibm.wbit.comptest.ctnative.runtime.utils.DOMHelper and click OK. This utility class provides an API that allows you to retrieve a specific field in a SOAP message by specifying an XPath expression. You can find more information about this utility class in the API Reference section.
Figure 9. Import Java class to the test data table
Import Java class to the test data table
  1. Go back to the Default page.
  2. Go to the Test Cases page of the test suite editor.
  3. Select 2. Invoke AttachmentWebService:submitDocument.
  4. Scroll to the bottom of the Detailed Properties section and click the Request tab.
  5. Click the request variable to bring you to the associated variable in the Test Data Table view.
  6. Expand the SOAP Body of the variable.
  7. Select the following fields and set their format to Java expression by selecting Set Format > Java Expression from the context menu.
    • userId
    • firstname
    • lastname
    • picture
  8. The test case support defines a SOAP variable of type com.ibm.wbit.comptest.ct.soap.SoapWrapper. You can find more details about this Java type in the API Reference section. Type the following Java expression to each corresponding field:
Table 1. Setting Java expression in the SOAP message variable
SOAP message fieldJava Expression
userId(String) DOMHelper.getSelectedNode(response.getMessage(), "/soapenv:Envelope/soapenv:Body/in:getUserResponse/user/userId/text()", null)
firstname(String) DOMHelper.getSelectedNode(response.getMessage(), "/soapenv:Envelope/soapenv:Body/in:getUserResponse/user/firstname/text()", null)
lastname(String) DOMHelper.getSelectedNode(response.getMessage(), "/soapenv:Envelope/soapenv:Body/in:getUserResponse/user/lastname/text()", null)
pictureresponse.getAttachment((String) DOMHelper.getSelectedNode(response.getMessage(), "/soapenv:Envelope/soapenv:Body/in:getUserResponse/user/picture/text()", null))

The first 3 fields (i.e. userId, firstname, lastname) expect a String type value. You call DOMHelper, providing an XPath expression to the field you want to retrieve from the SOAP message (i.e. response.getMessage()). Note that the namespace prefix used in your XPath expression must be exactly the same as the namespace prefix defined in the actual SOAP message. Since the namespace prefixes used in the actual SOAP message may not be known when creating the test suite. You can instead provide a map that contains the namespaces and the prefixes you used in your XPath expression to the DOMHelper.getSelectedNode() so that the namespaces can be resolved properly at run time. You will be shown how to do this in the Create your own Java class section.

The picture field is a SOAP attachment field. By retrieving the content ID from the response SOAP message, we can call response.getAttachment(contentId) to retrieve the binary content of the attachment in the SOAP message.

  1. Select the pdf field, and use the Browse for File action from the context menu to set it to a file: /WebServiceTestProject/test.pdf.
  2. Go back to the Test Cases page of the test suite editor.
  3. Switch to the Response tab.
  4. Click the response link. This will bring you to the associated variable in the Test Data Table view.
  5. Right click a column cell of the variable and select Set To > Equals Default from the context menu.
  6. Expand the SOAP body in the variable and type OK for the submitDocumentResult0 field.
  7. Right click the pdf field in the response SOAP message variable and select Set Format > Java Expression from the context menu.
  8. Type request_2.getAttachmentByPartName("pdf") in the Expected column cell. This Java expression will retrieve the binary content of the pdf attachment in the request SOAP message.
Figure 10. Using Java expressions in test data variables
Using Java expressions in test data variables
  1. Save the test suite.

You have successfully created a scenario test case for your web services and you are now ready to run it in the integration test client.


Run your scenario test case

To run your scenario test case, right click your test suite in the Business Integration view and select Run Test from the context menu. This will open a test client editor. In the test client editor, click Continue to run your test. When the test completes, you can examine the test trace produced from the run.

Figure 11. Run the test suite in integration test client
Run the test suite in integration test client

You can select the Binding events to see the SOAP message and attachments being sent or returned from the web services.

Figure 12. Web service response binding event in integration test client
Web service response binding event in integration test client

Create your own Java class

Sometimes, you may want to create your own Java utility and use it in the Test Data Table view. You can create your Java utility in the component test project or in a shared library and then import it to the test data table like what you did in the Using Java Expressions section. In this section, you will be shown how to create a custom namespace map to be fed in the DOMHelper.getSelectedNode() API.

  1. Switch to the Java perspective.
  2. Select WebServiceTestProject and select File > New > Class from the workbench menu.
  3. Specify the package name and the class name as in the following screenshot. Click Finish to continue.
Figure 13. Create a new Java class
Create a new Java class
  1. The custom.NameSpaceMapManager class is opened in the Java editor.
  2. Create a static method called getNamespaceInfo in the Java class as below.
Listing 1. Source code for getNamespaceInfo() method
public class NameSpaceMapManager {

   public static NamespaceInfo getNamespaceInfo() {
      NamespaceInfo nsInfo = new NamespaceInfo();
      nsInfo.addNamespace("ns0", "http://schemas.xmlsoap.org/soap/envelope/");
      nsInfo.addNamespace("ns1", "http://WebServiceLibrary/UserInterface");
      return nsInfo;
   }
}
  1. Open WebServiceTest in the test suite editor.
  2. Import the custom.NameSpaceMapManager in the test data table.
Figure 14. Import your own Java class to test data table
Import your own Java class to test data table
  1. Go back to the Default page of the Test Data Table view and change the Java expressions in the userId, firstname, lastname, and picture fields in the request_2 variable:
Table 2. Change the Java expression in the SOAP message variable
SOAP message fieldJava Expression
userId(String) DOMHelper.getSelectedNode(response.getMessage(), "/ns0:Envelope/ns0:Body/ns1:getUserResponse/user/userId/text()", NameSpaceMapManager.getNamespaceInfo())
firstname(String) DOMHelper.getSelectedNode(response.getMessage(), "/ns0:Envelope/ns0:Body/ns1:getUserResponse/user/firstname/text()",NameSpaceMapManager.getNamespaceInfo())
lastname(String) DOMHelper.getSelectedNode(response.getMessage(), "/ns0:Envelope/ns0:Body/ns1:getUserResponse/user/lastname/text()",NameSpaceMapManager.getNamespaceInfo())
pictureresponse.getAttachment((String) DOMHelper.getSelectedNode(response.getMessage(), "/ns0:Envelope/ns0:Body/ns1:getUserResponse/user/picture/text()",NameSpaceMapManager.getNamespaceInfo()))

You can see that the new XPath expressions are using the namespace prefixes you defined in the NameSpaceMapManager class and the namespace map that you created in the NameSpaceMapManager class is passed to the DOMHelper.getSelectedNode() API.

  1. Save the test suite.
  2. You can rerun your test case again as outlined in Run your scenario test case section.

Create a shared library

If you want to reuse your Java utility in other component test projects, you can create it in a shared library. The following steps show you how to create a shared library.

  1. Select File > New > Library from the workbench menu.
Figure 15. Create a new library
Create a new library
  1. Type an arbitrary name for the new library and click Finish.
  2. To use the new library, you must add it as a dependency of the component test project that contains the test case. Double click Dependencies in WebServiceTestProject in the Business Integration view.
  3. The Dependencies editor opens. Click the Add button to add the library to the component test project.
Figure 16. Reference the shared library in component test project
Reference the shared library in component test project
  1. Click Save to save the Dependencies editor.
  2. The new library is now referenced by the component test project. You can then follow the steps in Create your own Java class to create your Java utility in this library.

API Reference

This section shows the javadoc of the built-in utilities provided by the test case support that will help you to access SOAP message variables in your test suite. Note that the following javadocs are generated from the WebSphere Process Server v7.0.0.3. Some methods may not be available prior to v7.0.0.3.

Listing 2. Javadoc of com.ibm.wbit.comptest.ct.soap.SoapWrapper
/**
 * @return the SOAP envelope in this wrapper.
 */
public Document getMessage();

/**
 * 
 * @param index
 *        the position of the attachment in this wrapper
 * @return the content of the attachment in raw bytes at the given position.
 */
public byte[] getAttachment(int index);

/**
 * 
 * @param contentID
 *       the corresponding content ID of the attachment in this wrapper
 * @return the content of the attachment in raw bytes.
 */
public byte[] getAttachment(String contentID);

/**
 * 
 * @param partName
 *      the corresponding part name of the attachment in this wrapper
 * @return the content of the attachment in raw bytes.
 */
public byte[] getAttachmentByPartName(String partName);

/**
 * @return the number of attachments found in this wrapper.
 */
public int getAttachmentCount();
Listing 3. Javadoc of com.ibm.wbit.comptest.ctnative.runtime.utils. DOMHelper
/**
 * Look up the object from the specified DOM document using the XPath
 * expression.
 * 
 * @param target
 *            the DOM Document node
 * @param xpathExpr
 *            the XPath expression for the lookup
 * @param nsInfo
 *            contains the prefix-namespace pair. Since the namespace
 *            prefixes used in the DOM document can be anything, it allows
 *            the specified XPath expression to use its own namespace
 *            prefixes and this API will try to use this object to map them
 *            to the namespace prefixes used in the actual DOM document. If
 *            this is <code
type="inline">null</code>, the XPath expression must use the
 *            same namespace prefixes as appeared in the DOM document.
 * @return the value or DOM node identified by the given XPath expression.
 * @throws Exception
 */
public static Object getSelectedNode(Object target, String xpathExpr,
        NamespaceInfo nsInfo) throws Exception;
Listing 4. Javadoc of com.ibm.wbit.comptest.ctnative.runtime.utils. NamespaceInfo
/**
 * Add a namespace pair to this map.
 * 
 * @param prefix
 *            the namespace prefix
 * @param namespace
 *            the namespace defined by the specified prefix
 */
public void addNamespace(String prefix, String namespace);

/**
 * Finds the namespace defined by the given prefix in this map.
 * 
 * @param prefix
 *            a namespace prefix cached in this map
 * @return the namespace paired with the given prefix or null if the prefix
 *         cannot be found in this map.
 */
public String getNamespace(String prefix);

/**
 * Finds the namespace prefix associated to the given namespace cached in
 * this map.
 * 
 * @param namespace
 *            the namespace cached in this map
 * @return the associated namespace prefix or null if the namespace is not
 *         found in this map.
 */
public String getPrefix(String namespace);

Conclusion

The test case support in WebSphere Integration Developer allows you to create comprehensive test cases for your web service modules. In this article, you have learned how to create a scenario test case for a web service module that involves multiple invocations. You are also shown how to use Java expressions to reference a field in a variable from another one. You can now go ahead and try creating your own test cases to ensure that your web service applications work solid.


Downloads

DescriptionNameSize
WebService_CompletePIWebService_CompletePI.zip10KB
WebService_InitialPIWebService_InitialPI.zip10KB
WebService_OwnJavaClass_CompletePIWebService_OwnJavaClass_CompletePI.zip10KB

Resources

Learn

Get products and technologies

  • Evaluate IBM products in the way that suits you best: Download a product trial, try a product online, use a product in a cloud environment, or spend a few hours in the SOA Sandbox learning how to implement Service Oriented Architecture efficiently.

Discuss

  • Get involved in the My developerWorks community. Connect with other developerWorks users while exploring the developer-driven blogs, forums, groups, and wikis.

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into SOA and web services on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=SOA and web services, WebSphere
ArticleID=547901
ArticleTitle=Testing web services with test case support in WebSphere Integration Developer
publish-date=09272010