Technical Blog

2 Posts tagged with the soapui tag

Elastic Path supports web services via the Simple Object Access Protocol (SOAP), and provides support for obtaining and updating information for orders. The problem is, how do we actually test the web services component? One could write a custom SOAP client that interacts with the the SOAP server. However, there is an excellent tool available currently (and free) that can simulate sending a SOAP request, getting a response from the server, and then examining the data.

 

*Drum roll*

 

Introducing SoapUI. SoapUI is a Java-based program that aims to automate web service calls and can integrate into your testing framework. In this article we will go through the steps and create a test case that can be run automatically after the initial setup. We will introduce scripting in Groovy, a derivative of Java language.

 

The goal of this exercise to get used to the SoapUI framework and making a test suite consisting of updating a SKU, then checking to make sure that SKU has been updated. It can be run from the command line, and can generate reports/results, which could potentially be used in build reports.

 

To get started, you will need to have the following things ready:

 

  1. SoapUI software. To get SoapUI, please visit http://www.soapui.org and get the latest version (at the time of writing, 2.5 is the most current version and will be used in this article)

  2. The address of the Web Service Description Language file (WSDL). In this example the address is https://<your_domain>/webservices/catalogwebservice?wsdl where <your_domain> refers to the domain of your deployment

  3. Permission priliveges to access web services. To create permissions you have to select the Web Service Role when creating a new CM user in the Commerce Manager client.

  4. Have available SKU code information for use by the web services.

 

Step 1 - Creating a project

  1. Open SoapUI, and then click on File->New soapUI Project.

  2. In the Project Name text box, put in example1

  3. In the Initial WSDL/WADL, type in the URL of the web service definition file (bullet number 2 in the previous section)

  4. Press OK

     

    1.png

A list of operations should appear on the left hand column. For the purposes of this demonstration, we will only test two operations, getSku and updateSku. Next, we will need to create a new TestSuite. Right click on the project name and then click on New TestSuite. Name it TestSuite 1 for the purposes of this test.

 

newTestSuite.png

 

Now create a new TestCase by right clicking on TestSuite1 and click on New TestCase. Name it TestCase 1 for the purposes of this test.

 

newTestCase.png

 

 

Step 2 - Configuring the test case

The next step is to configure the test case so we can run the test. First we need to set the global settings. Click on the key icon and set the credentials for the username and password.

 

auth.png

 

This username/password combination should match the user that has Web Services permission set in the Commerce Manager. Leave the Domain blank in this case.

 

The next step involves setting a property that can be accessed by the whole test case. Click on the Properties button.

 

properties.png

 

Now add a property name called 'start' by click on the left-most icon. For the purposes of this test, we will name it 'date'. Give it a value of '2050-01-01T12:12:12.643-08:00'.

 

 

 

Step 3 - Adding the first SOAP request

We need to add a few SOAP requests first. Add the first SOAP request by clicking the little SOAP icon.

 

 

Name that request 'getSku', press Ok and click on CatalogWebServicePort->getSku from the dropdown box in the next dialog. Finish by clicking 'OK'. Now request window will open. Now on the left pane, the XML generated using the schema from the WSDL will be present. For this test case, use a SKU code that exists in the system and enter it in the <SkuCode> element. I chose '1YESPE' for this particular test. If you know the catalog that the SKU belongs to, put it between the catalog tag. Next we have add an assertion that the response is successful. We do that by clicking on the Assertions button and then clicking on the '+' button, then select 'Xquery Match' from the dropdown box. Fill in the details based on the values from below.

 

assertion.png

 

Press Save. By doing this, every time the test step is run, a validation check will occur. It will pass the check only if we see that the status of the test is succesful.

 

Queries using XPath are invaluable because they test can expected node values coming from the response. Since EP's Web Services returns a result status, it is ideal to evaluate the result using XPath queries. For more information on how to use XPath queries, please goto http://msdn.microsoft.com/en-us/library/ms256086.aspx

 

 

 

Step 4 - Adding the first Groovy script

SoapUI uses Groovy, an agile dynamic language for the Java platform. I won't go into details about Groovy, but basically if you know Java, then you know Groovy. To learn more about Groovy, please visit http://groovy.codehaus.org/.

 

For the first step of this part, you will need to add a test step to the test case. Right click on the test step on the left hand column and then click on Add Step -> Groovy Script.

 

A new window will appear. Fill in the following information in the box.

 

//gets the xml utilities

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

//get the reponse part

def holder = groovyUtils.getXmlHolder( "getSku#Response" )

//get the original start date

def startDate = holder.getNodeValue( "//StartDate" )

//assign it to a session variable to be used later

testRunner.testCase.setPropertyValue('temp', startDate);

//now update the Request of the second request

def holder2 = groovyUtils.getXmlHolder( "updateSku#Request" )

//gets the start date node and set it to our date

def startDate2 = holder2.setNodeValue( "//StartDate", testRunner.testCase.getPropertyValue('date'))

//update the change

holder2.updateProperty()

 

 

What does this do? Basically this will retrieve the startDate timestamp from the previous soap request and assign it to a variable, which will be used later at the end. This script also sets the variable of the next request so that it can be sent to the server. To read more on how to script your tests with Groovy in SoapUI, please visit http://www.soapui.org/userguide/functional/groovystep.html.

 

 

 

Step 5 - Configuring the rest of the test

Next we configure the rest of the steps. The rest of the test consists of:

 

Adding a SOAP request to update the SKU, and then checking the results.
  1. For this request, we once again use assertions to help us identify potential problems. Create a new Xquery Match assertion and copy the data below into the boxes and press Ok.

 

     assertion2.png

 

  2.   Fill in the SKU code (the same SKU code as before) in the SkuCode node. In addition, since we are changing the data in StartDate element, we should let the server know by putting 'STARTDATE' in the UpdatedField element.
startdate.png
Adding a Groovy script to retrieve the time stamp that was saved previously, and set it in the subsequent request.
  1. This step is similar to the one before, and it will update the next SOAP request with original value that we retrieved (and saved) in the first request. The code is listed below.

 

    //gets the xml utilities

    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

    //now update the Request of the second request

    def holder = groovyUtils.getXmlHolder( "updateSku2#Request" )

    //gets the start date node and set it to our date

    def startDate = holder.setNodeValue( "//StartDate", testRunner.testCase.getPropertyValue('temp'))

    //update the changes

    holder.updateProperty()

Adding a SOAP request to update the SKU again, this time we replace the start date with the original date we retrieved from the first request (so that we don't have actually change any data in this test).
  1. This step is identical to the previous updateSku request. We can actually make a clone of the last request and run that. The only difference is the value of StartDate, which actually will be changed by the Groovy script from above. To make a clone of the previous step, right click on the last SOAP request, and then click 'Clone TestStep'. Name the new step updateSku2.

 

Running the test case and making sure they pass

 

  1. Run the test case by pressing the arrow button in the Test Case 1 window.
  2. If you have done everything correctly, all the tests should be green when you run the test. Your screen should look similar to the following.

finished3.png

 

 

SoapUI is a powerful tool that is designed to test web services. It can be used as a functional testing tool, a regression testing tool and an unit testing tool. Furthermore, it has built in capabilities to run using command line and can generate reports after each run. This tutorial is just a fraction of the things that SoapUI can provide a QA team when it comes to testing web services.

1 Comments 0 References Permalink

If you've always dreamed of integrating Facebook/Flickr/WordPress/Mediawiki with Elastic Path, read on. Elastic Path's web services let you access some EP functionality remotely from the client platform of your choice.

First, make sure you've deployed the com.elasticpath.connect web app properly. See the section on deploying Elastic Path web apps in the Elastic Path 6.1 Deployment Guide.

Next, if you don't already know, find out what web services are available in your version of Elastic Path. Take a look at WEB-INF/web.xml in the directory where you deployed the webservices webapp. The <servlet-mapping> elements show you the URL patterns to use to access each of supported web services. For example, in my setup, I have the following:

 

<!-- Servlet mappings -->
   <servlet-mapping>
      <servlet-name>shoppingcart_web_service</servlet-name>
      <url-pattern>/shoppingcartwebservice</url-pattern>
   </servlet-mapping>

   <servlet-mapping>
      <servlet-name>order_web_service</servlet-name>
      <url-pattern>/orderwebservice</url-pattern>
   </servlet-mapping>

   <servlet-mapping>
      <servlet-name>association_web_service</servlet-name>
      <url-pattern>/associationwebservice</url-pattern>
   </servlet-mapping>

    <servlet-mapping>
       <servlet-name>import_web_service</servlet-name>
       <url-pattern>/importwebservice</url-pattern>
    </servlet-mapping>

   <servlet-mapping>
      <servlet-name>inventory_web_service</servlet-name>
      <url-pattern>/inventorywebservice</url-pattern>
   </servlet-mapping>

   <servlet-mapping>
      <servlet-name>catalog_web_service</servlet-name>
      <url-pattern>/catalogwebservice</url-pattern>
   </servlet-mapping>

   <servlet-mapping>
      <servlet-name>customer_web_service</servlet-name>
      <url-pattern>/customerwebservice</url-pattern>
   </servlet-mapping>

So, the available web services are:

  • shoppingcartwebservice (displays shopping cart items)
  • orderwebservice (view, hold, release orders)
  • associationwebservice (get product associations)
  • importwebservice (run import jobs, like from the Commerce Manager)
  • inventorywebservice (view and adjust inventory)
  • catalogwebservice (access the catalog, add products)
  • customerwebservice (view and add customers)

For our purposes, you'll want to disable the secure channel redirect in webservices\WEB-INF\conf\spring\security\acegi.xml:

 

<bean id="channelProcessingFilter" class="org.acegisecurity.securechannel.ChannelProcessingFilter">
      <property name="channelDecisionManager"><ref bean="channelDecisionManager"/></property>
      <property name="filterInvocationDefinitionSource">
            <!-- Comment this out:
<value>
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
                                \A/.*\Z=REQUIRES_SECURE_CHANNEL
</value>
     -->
            <!-- Add this: -->
<value>
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
</value>
</property>
    </bean>

Note that if you want to use SSL, there is an issue with PHP not being able to retrieve the WSDL over SSL. As a workaround, download the WSDL (and all referenced files) to your PHP host and repoint all references to the local versions.

Now is a good time to test if web services are deployed and configured properly on your system. A quick way to do this is to try a web service URL in a browser. For example:

http://localhost:11080/webservices/shoppingcartwebservice?wsdl

Note that I tested this using the Elastic Path trial installer, so you can try this out without having to do a full-blown EP deployment. You should get back an XML document that looks similar to the following.

wsdl.jpg

 

Next, install soapUI. soapUI is a great tool for inspecting WSDLs and testing web services. After it's installed, start it and create a project. (In the File menu, choose New soapUI Project.)

In the dialog, enter a project and enter the URL of the web service.

 

soapui_01.jpg


Click OK. When it's done, you'll see that the project has been added to the navigator pane on the left. Under the project, you can see the methods available in the web service. If you expand one of the methods, you'll see a Request 1 node beneath it.

soapui_02.jpg

 

Select Request 1. In the Request Properties pane (under the navigator), enter the user name and password of the Commerce Manager administrator user (or use the login of a user with web services permissions, if one exists.)

 

soapui_03.jpg


Double-click Request 1. You'll see the XML that would be sent if you called the method from a web service client.

 

soapui_04.jpg

 

You can even call that method by replacing the question marks with actual values and clicking the the green arrow button. The results are displayed in the pane on the right. (Don't forget to set up your environment properly before trying out web service calls. For example, if you want to try the shopping cart service as shown here, you'll need to log a user in to the store front and add items to the cart.)

 

I find this a great way to experiment with a service, without having to worry about the details of setting up your particular client platform. Once you've got a feel for how it works, the next step is to set up PHP and write some code.

  1. Install Apache and PHP 5.1 or later. For convenience, you can install Xampplite, which includes a complete LAMP/WAMP suite (Apache, PHP, Perl, MySQL, ...).
  2. In the php.ini file, do the following:
    1. Set soap.wsdl_cache_enabled to 0. For development purposes, you don't want to cache the WSDLs.
    2. Make sure the PHP SOAP extension is enabled (i.e., you have the following line with no semi-colon (;) at the beginning):
      extension=php_soap.dll
  3. Restart Apache.
  4. Write PHP code. Here's an example that uses the shopping cart web service to display items in a user's shopping cart:
    <html>
    <head><title>Elastic Path Web Services Test</title></head>
    <body>
    <h1>Elastic Path Web Services Test</h1>
    <?php
    // construct a SOAP client
    $wsdl = 'http://ep-ww-pmonk:7001/webservices/shoppingcartwebservice?wsdl';

    // if SSL is used, must download wsdl and xsd and refer to that
    //$wsdl = 'shoppingcartwebservice.wsdl';

    // set client options, including CM admin or web service user name and password
    $options = array(
    'trace' => 1,
    'login' => 'admin',
    'password' => 'password'
    );

    try{
    // create the service proxy
    $cartService = new SoapClient($wsdl, $options);

    // fill an object with the parameters
    $getCartRequestObj->UserId = 'paul.monk@example.org';
    $getCartRequestObj->StoreCode = 'SNAPITUP';

    // wrap that object (container property name must be the xxxRequest type in the WSDL (e.g., GetCartRequest))
    $getCartObj->GetCartRequest = $getCartRequestObj;

    // call the service method
    $result = $cartService->getCart($getCartObj);

    // display the results
    print '<p>Your shopping cart contains these items:</p><table><tr><th>Quantity</th><th colspan="2">Item</th></tr>';
    $cart = $result->return->shoppingCart;
    foreach($cart->CartItems->CartItem as $cartItem){
    $tr ='<tr>';
    $qty = $cartItem->Quantity;
    $tr .= '<td>' . $qty . '</td>';
    $productSku = $cartItem->ProductSku;
    $tr .= '<td>' . $productSku->Code . '</td>';
    $tr .= '<td><img src="http://ep-ww-pmonk.elasticpath.net:7001/storefront/renderImage.image?imageName=' .
    $productSku->Image->FileName . '&width=200&height=200&padding=0"/></td></tr>';
    print $tr;
    }
    print '</table>';

    }
    catch(SoapFault $fault){
    print '<pre>';
    var_dump($fault);
    var_dump(htmlspecialchars($cartService->__getLastRequest()));
    var_dump(htmlspecialchars($cartService->__getLastResponse()));
    print '</pre>';
    }
    ?>
    </body>
    </html>

For more info on PHP's SOAP extension, see the PHP SOAP documentation.

Once you've experimented a bit, you may want to start looking at exposing more of Elastic Path's functionality as web services. You'll want to look at the section on web services architecture in the Elastic Path 6.1 Developer Guide.

0 Comments 0 References Permalink