Thursday, January 26, 2017

Install Genymotion on ubuntu 16.04

I wanted to install Genymotion to my ubuntu 16.04 to run Nativescript  sample apps on Android simulator. As mentioned in the documentation first I installed the Oracle Virtualbox 5.1.0  then Genymotion.

But when I start the Genymotion it failed with following error.



This error message doesn't provide many details about the issue.  But when I open the genymotion.log (vi  ~/.Genymobile/genymotion.log ), that has the root cause for this issue.


VBoxManage: error: Failed to create the host-only adapter
VBoxManage: error: VBoxNetAdpCtl: Error while adding new interface: failed to open /dev/vboxnetctl: No such file or directory
VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component HostNetworkInterfaceWrap, interface IHostNetworkInterface
VBoxManage: error: Context: "RTEXITCODE handleCreate(HandlerArg*)" at line 71 of file VBoxManageHostonly.cpp"
Jan 25 22:32:40 [Genymotion] [critical] [VBox] [createHostOnlyInterface] Failed to create interface


So, When you start Genymotion very first time, it is trying to create a "Host-only Network" in Virtualbox. That process going to fail if your system has enabled the "Secure Boot".



So as a solution:

1. Restart the machine and logged into the BIOS settings (Press F1 while rebooting the machine).
2. Under the "Security" tab  "Disabled" the "Secure Boot".

After that, you will be able to start the Genymotion on Ubuntu.




Wednesday, February 17, 2016

[WSO2 ESB]Invoke Contentful API

Lets say we want to invoke Contentful API which contains the response with Content-Type:application/vnd.contentful.delivery.v2+json

1. First we tried setting the Accept header before send the message. But that didn't change the Content-Type of the  server response.
<header name="Accept" scope="transport" value="application/json"/>
2. The second attempt was to set the BinaryRelayBuilder and ExpandingMessageFormatter in the axis2.xml file under <messageBuilders> and <messageFormatters> sections.
<messageBuilder class="org.wso2.carbon.relay.BinaryRelayBuilder" contentType="application/vnd.contentful.delivery.v2+json"/>
<messageFormatter class="org.wso2.carbon.relay.ExpandingMessageFormatter" contentType="application/vnd.contentful.delivery.v2+json"/>
Now the message building error solved. But the response contains in binary format.
Eg:
<ns:binary xmlns:ns="http://ws.apache.org/commons/ns/payload">-------CAgIH0KICAgICAgICBdCiAgICAg--------</ns:binary>
3. Finally we used the default json builder and formatter with our contentful media type(application/vnd.contentful.delivery.v2+json) to resolve the issue. (make sure to remove the configuration added in above 1 and 2 steps)
Eg:
<messageBuilder class="org.apache.synapse.commons.json.JsonStreamBuilder" contentType="application/vnd.contentful.delivery.v2+json"/>
<messageFormatter class="org.apache.synapse.commons.json.JsonStreamFormatter" contentType="application/vnd.contentful.delivery.v2+json"/>

Thursday, February 11, 2016

[WSO2 ESB] How to remove default namespace at message enrich.

1.Lets say we need to add new element (eg: foo) to the message body using enrich mediator.

<enrich>
 <source type="inline" clone="true">
 <foo>test</foo>
</source>
<target type="body" action="child"/>
</enrich>
2. After enriched, the  message will be as bellow.
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
    <soapenv:Body>
        <foo xmlns="http://ws.apache.org/ns/synapse">test</foo>
    </soapenv:Body>
</soapenv:Envelope>
3. You can see foo element contains with default synapse namespace.
4. You can avoid this adding empty namespace (xmlns="") to foo element in enrich mediator configuration.
<enrich>
 <source type="inline" clone="true">
 <foo xmlns="">test</foo>
</source>
<target type="body" action="child"/>
</enrich>

Friday, February 5, 2016

[WSO2 ESB] How to remove jsonObject root element from message body

Eg: Sample SOAP message

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soap:Header/>
    <soap:Body>
        <jsonObject>
            <foo>
                <aaa>Ajith</aaa>
                <bbb>Vitharana</bbb>
            </foo>
        </jsonObject>
    </soap:Body>
</soap:Envelope>

Eg: Sample ESB configuration.

<enrich>
        <source clone="true" xpath="$body/jsonObject/foo"/>
        <target type="body"/>
</enrich>

source : https://docs.wso2.com/display/ESB480/JSON+Support#JSONSupport-XMLrepresentationofJSONpayloads

Friday, January 22, 2016

[WSO2 ESB] Iterate and Aggregate mediators

Lets assume we have two services.

First : This returns list of market details
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <marketDetails xmlns="http://market.test.com">
            <marketDetail>
                <market>
                    <id>100</id>
                    <openTime>10.00AM</openTime>
                    <closeTime>4.00PM</closeTime>
                    <name>New York</name>
                </market>
                <market>
                    <id>200</id>
                    <openTime>9.00AM</openTime>
                    <closeTime>5.PM</closeTime>
                    <name>London</name>
                </market>
            </marketDetail>
        </marketDetails>
    </soapenv:Body>
</soapenv:Envelope>
Second: This returns the symbols of each market.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <symbols xmlns="http://market.test.com">
            <symbol>
                <id>20</id>
                <name>Google</name>
                <qty>2000</qty>
                <unitPrice>6</unitPrice>
            </symbol>
            <symbol>
                <id>30</id>
                <name>IBM</name>
                <qty>1000</qty>
                <unitPrice>5</unitPrice>
            </symbol>
            <symbol>
                <id>30</id>
                <name>WSO2</name>
                <qty>1000</qty>
                <unitPriceice>5.5</unitPriceice>
            </symbol>
        </symbols>
    </soapenv:Body>
</soapenv:Envelope>
So we need to build a new service which returns all the symbols of all markets.

1. We need to iterate through the first response to get the market id and invoke the second service.
<property xmlns:m0="http://market.test.com" name="MARKET_COUNT"
          expression="count(//m0:marketDetails/m0:marketDetail/m0:market)"/>
<iterate xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://org.apache.synapse/xsd"
         preservePayload="true"
         attachPath="//m0:marketDetails/m0:marketDetail/m0:market"
         expression="//m0:marketDetails/m0:marketDetail/m0:market/m0:id"
         id="MARKET_ITERATOR">
<target>
    <sequence>
        <payloadFactory media-type="xml">
            <format>
                <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                    <soap:Body>
                        <getMarketSymbols xmlns="http://market.test.com">
                            <marketId>$1</marketId>
                        </getMarketSymbols>
                    </soap:Body>
                </soap:Envelope>
            </format>
            <args>
                <arg xmlns:m0="http://market.test.com" 
expression="//m0:marketDetails/m0:marketDetail/m0:market/m0:id" evaluator="xml"/>
            </args>
        </payloadFactory>
        <property name="messageType" value="application/soap+xml" scope="axis2"/>
        <call>
            <endpoint xmlns="http://ws.apache.org/ns/synapse" name="SymbolEndpoint">
                <address uri="https://test.com/services/market">
                    <timeout>
                        <duration>30000</duration>
                        <responseAction>fault</responseAction>
                    </timeout>
                    <suspendOnFailure>
                        <errorCodes>-1</errorCodes>
                        <initialDuration>0</initialDuration>
                        <progressionFactor>1.0</progressionFactor>
                        <maximumDuration>0</maximumDuration>
                    </suspendOnFailure>
                    <markForSuspension>
                        <errorCodes>-1</errorCodes>
                    </markForSuspension>
                </address>
            </endpoint>
        </call>
       <loopback/>
    </sequence>
</target>
</iterate>
2. Use the aggregate mediator to collect all the symbols.
<property name="ECNCLOSING_ELEMENT" scope="default">
    <wrapper xmlns=""/>
</property>

<aggregate id="MARKET_ITERATOR">
<completeCondition>
    <messageCount min="{get-property('MARKET_COUNT')}" max="-1"/>
</completeCondition>
<onComplete xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns:ns="http://org.apache.synapse/xsd"
            xmlns:m0="http://market.test.com"
            expression="//m0:symbols/m0:symbol"
            enclosingElementProperty="ECNCLOSING_ELEMENT">
</onComplete>
</aggregate>
</iterate>

Monday, January 18, 2016

[WSO2 ESB]Add new element to message body.


Sometime we need to add static content to the message response. Eg: privacy statement. So we can store the note in the registry and include to message body using the enrich mediator.

<property  expression="get-property('registry','gov:/note/PrivacyStatement_v1.0.0.xml')"
name="PrivacyStatement" scope="default" type="OM"/>

<enrich>
  <source xpath="$ctx:PrivacyStatement"/>
  <target action="sibling" type="body"/>
</enrich>

Monday, December 14, 2015

[JMeter] Invoke API chain using JMeter

I have created two ESB APIs. You can download these two files and copy to wso2esb-4.9.0/repository/deployment/server/synapse-configs/default/api directory.

1. First API (API-1) will  return the TV channels (tvstations.xml).

GET http://localhost:8280/channels

{
  "details":{
    "totalChannelCount":4,
    "channelDetails":{
      "channelDetail":[
        {
          "name":"TV-01",
          "owner":"ajith",
          "location":"Colombo 05"
        },
        {
          "name":"TV-02",
          "owner":"vajira",
          "location":"Colombo 05"
        },
        {
          "name":"TV-03",
          "owner":"ranjith",
          "location":"Colombo 05"
        },
        {
          "name":"TV-04",
          "owner":"lakmali",
          "location":"Colombo 05"
        }
      ]
    }
  }
}


2. The second API (API-2) will return the programs available in each channel (tvprograms.xml).

GET http://localhost:8280/programs?channel=TV-01

{
  "details": {
    "totalPrograms": 4,
    "channelName": "TV-01",
    "programsDetails": {
      "program": [
        {
          "name": "Morning Show",
          "presenter": "ajith",
          "duration": "1h"        },
        {
          "name": "News",
          "presenter": "vajira",
          "duration": "2h"        },
        {
          "name": "Lunch Time Music",
          "presenter": "nishatha",
          "duration": "3h"        },
        {
          "name": "movie",
          "presenter": "lakmali",
          "duration": "4h"        }
      ]
    }
  }
}
Now I'm going to create a JMeter test plan. First invoke  API-1 to get  TV channels, then iterate through each channel to get TV programs invoking  API-2.

Steps:

1. Download and install the JMeter plugins from http://jmeter-plugins.org/downloads/all/

(Download the "Extras with Libs Set")

2. This plugin provides "Json Xpath extractor" feature, which can be used to filter the TV channel names  from first API response.

3. Create a new "Thread Group".

"Test Plan" ----> "Add" ---> "Threads (users)" -----> "Thread Group"

4. Add "HTTP Header Manager", this is  useful to set the HTTP headers which is required to invoke the API.

 "Thread Group" ---> "Add" ---->  "Config Element" --->  "HTTP Header Manager"

5.  Add HTTP Request, then define a host , protocol, path ..etc.

"Thread Group ----> "Add"  ---> "Sampler" -----> HTTP Request"


5. Add JSON path extractor to filter the channel names from the first response.

"HTTP Request" ---> "Add" ---> "Post Processors" ---> "Json path extractor"

variable name : channelNames
Json path expression: $.details.channelDetails.channelDetail[*].name


6.  Add another JSON path extractor to assign channel count to a variable.

variable name : channelCount
Json path expression:$.details.totalChannelCount


7.  Add "Loop Controller" -  set the Loop Count as ${channelCount}

"Thread Group" ---> "Add" ---> "Logic Controller" ---> "Loop Controller"


8. Add counter to construct the for loop.

"Loop Controller" ---> "Add" ----> "Config Element" ----> "Counter"

Start : 0
Increment : 1
Maximum : ${channelCount}
Reference Name : loopCounter


9.  Add "BeanShell Processor" to set channel name as a variable.

"Loop Controller" --> "Add" ---> "Post Processors" ---> "BeanShell Processor"



// channelNames is the Json array as string.

String tvChannels = vars.get("channelNames");

// Create channle list as array

tvChannels = tvChannels.replace("[","").replace("]","").replace("\"","");

String[] channelArray = tvChannels.split(",");

// "loopCounter" is reference variable used in Counter

idx = Integer.parseInt(vars.get("loopCounter"));

// Set the chanle name to a variable

vars.put("channelName",channelArray[idx]);

10. Add the second API as HTTP Request.

The channelName  is variable defined in the BeanShell processor.
Path : /programs?channel=${channelName}

11. Add a listener to monitor the results.

Eg:

"HTTP Request"  ---> "Add" ---> "Listeners" ---> "Summary Report"


11. You can download the .jmx file  here and import to JMeter.