A mock web service―The easy way (Part 2)

This article is written addressing a real situation that you might already have experienced―or are about to. WSO2 ESB is a very lightweight Enterprise Service Bus product that can be used to handle this situation at a level of perfection―especially, without taking more than a few minutes.
Earlier―with my previous article―I described a real world situation that you would deal with, as a software developer or a tester. It was totally written describing the use case, and at the end of the same article I mentioned that I would be publishing the second part of it (which is this article) describing how to do it. Therefore―while composing this―I intend to avoid long descriptions on the use case scenarios: rather, wish to give the instructions on building the working set-up that is relevant to use case. Therefore, this will help you if you are looking for “unzip, copy, paste, execute and try” kind of an article; allowing you to build a working set-up, without even reading the fisrt article, which has been written describing the use case.
Jessey Pinkman cooks 96% pure stuff,
even without knowing
the subject of matter.
I wish to make my articles "Copy, Paste, Execute and Try" (C-PET) friendly―which may be good for getting things done quickly―even if everybody would not agree with that technique. It sounds like kind of a "Jessey Pinkman style cook", making the name of this Blog―Teaching Bad―a meaningful one. However, I would separate the "Read, Understand, Explore and Learn" kind of stuff in to separate articles, similar to what I did with the Part 1 of this article.
While you keep reading, you will find the below listed elements and words in this post. Please note that these can be just symbols that I use while writing, which you may replace accordingly, and get things done.

[TOOL_HOME]The directory where you have placed the relevant tool.
Eg: [ESB_HOME] : The directory that you have placed the ESB installation. Or [JAVA_HOME] : The directory that you have installed Java—probably a directory such as '/usr/lib/jvm/jdk1.6.45'.
Admin consoleThe web based Administration user interface that the relevant product is equipped with.
Server consoleThe terminal (command prompt) application window that you used to run the relevant server product/application.

Download the following tools.
  1. Oracle JDK
  2. WSO2 Enterprise Service Bus
  3. SOAP UI
Install java and set/export $JAVA_HOME environment variable.
—If you haven't set this already, please do refer my blog post and get it done quickly.

Install the WSO2 Enterprise Service Bus.
Unzip the downloaded wso2esb-x.x.x.zip archive into a preferred directory. (By the time I write this article, the latest stable release of WSO2 Enterprise Service Bus is 4.8.1.)
For example, I would unzip it into a location like /home/chathura/demo/. Then it will be extracted into a new directory, and ultimately look alike /home/chathura/demo/wso2esb-4.8.1/. We can call this location as the [ESB_HOME], hereafter.

Install SOAP-UI.
You can either download the zipped version and extract or download the binary setup and install it.

Run WSO2 Enterprise Service Bus.

Goto [ESB_HOME]/bin
    cd /home/chathura/demo/wso2esb-4.8.1/bin
Start
    sh wso2server.sh 
(on Windows, it is wso2server.bat)

Open the WSO2 ESB Admin Console, on your browser. After you execute the start-up script (wso2server.sh/wso2server.bat) you will see that this Admin console URL is printed on the server console. It will be something similar to https://localhost:9443/carbon. You can open this URL on your browser. Both the username and the password will be 'admin'.


Create a proxy service

  1. Find the menu on the left side.
  2. On the 'Main' panel, click 'Proxy Service' under 'Services > Add'
  3. Click on 'Custom Proxy'
  4. Type a name for the 'Proxy Service Name' (I would type 'AccountsServiceProxy' in this sample), and click 'Next >'.
  5. Select 'Define Inline' option, under 'Define In Sequence'.
  6. Click '(+)Create' link (Which just appeared)
The above step will take you into another screen.
  1. Click (+)Add child, and select 'Core > Log' to add a log mediator.
    This Log mediator is something that is similar to 'System.out.println()' in java, 'cout' in 'C' or 'Console.Writeln()' in C#. In Log mediator properties, change the 'Log level' to 'Full', which will help you logging everything.
  2. Click 'Update'
  3. Click 'Save & Close'
  4. Click 'Next'
  5. Click 'Finish'
Now you can see the Proxy service (AccountsServiceProxy) that we created on the services list. Click on the 'WSDL1.1' or 'WSDL2.0', and copy the WSDL URL of the proxy service.
  1. Open SOAP-UI
  2. Select 'File > New SOAP Project' (A dialog will open)
  3. Paste the WSDL URL in the 'Initial WSDL' input field
  4. Press 'OK' (Now you will see the SOAP-UI project has been added.
  5. Expand the 'mediate' node, and double click the 'Request 1' node, which would open a special window.
  6. Copy the below mentioned payload, paste it on the left side Text Area field of the same window (the window titled 'Request 1'), and click the submit button (The green Triangular button, on Top-Left)
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
    <soapenv:Body>
        <t:getAccountInfo xmlns:t="http://www.teachingbad.com">
            <t:accountNumber>100153451092</t:accountNumber>
        </t:getAccountInfo>
    </soapenv:Body>
</soapenv:Envelope>
Now you will see that the payload which you just sent, has been logged on the WSO2 ESB server console (Terminal). This means, that you have done everything correctly up to this point.

Now we are going to proceed with the next step.
  1. Goto the WSO2 ESB Admin console on the browser.
  2. Click on 'List', under 'Manage > Services' on the Main menu palette.
  3. In the AccountServiceProxy row of the list, click on 'Design View'.
  4. Click 'Next'
  5. Click 'Edit' link of the 'Define Inline' option, under 'Define In Sequence'
  6. Click 'Add Child', and select 'Transform > PayloadFactory'
  7. On the 'PayloadFactory' mediator properties, select 'Define inline' option.
    (Make sure that 'XML' is selected as the 'Payload Media-Type')
  8. Copy the below mentioned XML code:the fake response payload, and paste on 'Define Inline' Teatarea field.
<t:account xmlns:t="http://www.teachingbad.com">
    <t:customerId>303101842</t:customerId>
    <t:accountNumber>$1</t:accountNumber>
    <t:accountType>Savings</t:accountType>
    <t:accountCurrency>LKR</t:accountCurrency>
    <t:accountBalance>250356.46</t:accountBalance>
</t:account>

Notice the '$1' in the above XML.
  1. Click the '(+)Add argument' link (This will add a new row)
  2. Change the 'Argument Type' into 'Expression'
  3. Click the 'Namespaces' link (a dialog will pop-up)
  4. Click 'Add'
  5. Type 't' in prefix, and type 'http://www.teachingbad.com' in URI fields, respectively and Save.
  6. In the 'Argument Value/Expression' field, paste '//t:accountNumber'
  7. Click 'Update'
  8. Add a Log mediator below the 'PayloadFactory' in the sequence.
  9. Click '(+)Add child', and select 'Core > Log'
  10. In the Log Mediator properties, change the 'Log level' to 'Full'
  11. Click 'Update'
  12. Click 'Save & close'
  13. Click 'Next'
  14. Click 'Finish'
Now resend the previous payload using the SOAP-UI and observe what is printed on the server console.

Additionally you will see the fake response logged on the server console.

In the place that you put '$1' in the fake payload, you will see the 'Account Number' value of the request payload which you just sent using the SOAP-UI. (<t:accountNumber>100153451092</t:accountNumber>)

Now, it is the time for a Celebration!

You are done with your Mock Service, without even touching much of code. :-D

If you want this scenario to look perfect by getting the response XML payload printed on SOAP-UI response box, you can do it by adding another Mediator, at the end of the same proxy service.

Edit the proxy service using the 'Design view' again, just like what you did while adding a 'PayloadFactory' mediator.

  1. Goto the WSO2 ESB Admin console on the browser.
  2. Click on 'List', under 'Manage > Services' on the Main menu palette.
  3. In the AccountServiceProxy row of the list, click on 'Design View'.
  4. Click 'Next'
  5. Click 'Edit' link of the 'Define Inline' option, under 'Define In Sequence'
  6. Click 'Add Child', and select 'Core > Respond'
  7. Click 'Save & close'
  8. Click 'Next'
  9. Click 'Finish'
Now try it again using SOAP-UI.

This time you don't essentially need to observe the server console, because; the response will be printed on the right side box of SOAP-UI 'Request1' window.

In this example, please do remember that the SOAP-UI application acts as your 'Application Δ'. Therefore, you can replace this SOAP-UI with your 'Application Δ' in your case.

Now you know whether this works or not, and how to get it working. If this works for you—then there is no risk in investing some more minutes learning what happened underneath while you were following the steps. If you believe and decided that it is worth to spend some more time on it, you can read the very next article of the same topic 'A mock web service―The easy way (part 3)'

Comments