Thursday 6 April 2017

SoapUI, HermesJMS, IBM Websphere MQ automation with Maven

I was working on the project where I need to automate tests with interact with IBM websphere MQ queues. I have been able to achieve it from SoapUI and HermesJMS (I have already talked about the steps in my old post here) soon to face the next challenge to integrate with Jenkins and run the tests with scheduled builds. 
In my hope to find an existing solution on net, I looked for it on google. Unfortunately it didn't help that much as the solutions available did not help me but they gave me an idea about how to achieve the same. I am using maven for integration of my solution here.


Step 1 : HeremesJMS Configuration File

I would suggest to simplify the HermesJMS configuration file which was created in the steps performed in exercise earlier. Remove all the unnecessary elements and keep only those that you would need for your test.  Here below is my configuration file which I've used



You would notice that I've used a keyword "MQ_LIB_LOCATION" while providing the path of the IBM websphere MQ dependency jar files which are used by HermesJMS in order to create connection. While my experiment I realized that in order to be able to run my build I'll have to keep my hermes configuration file autonomous. Hence, I needed to update the file dynamically with help of "com.google.code.maven-replacer-plugin" of maven. With help of this plugin I change the path of the libraries used by HermesJMS.


Step 2 : Create Maven Project skeleton

As explained above we will have to update the Hermes configuration files dynamically, hence a separate maven project module would be needed for achieving this and another module will be used to execute the SoapUI project.

Create the master project using below command and then the modules under the master project
mvn archetype:generate -DgroupId=myorg.com -DartifactId=myapp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This command will create a directory "myapp" with the master project, now edit the pom file and change "packaging" parameter from "jar" to "pom" and remove the directory "src" from the master project as you will not be using it.
Once done, you can create the modules. Go to the master project directory and create the two modules with below commands
mvn archetype:generate -DgroupId=myorg.com -DartifactId=SoapProject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
mvn archetype:generate -DgroupId=myorg.com -DartifactId=UpdateHermesJMS -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Now the two projects are ready, you can update the pom file to meet your need. We will start with the project to update the Hermes configuration.


Step 3 : Module UpdateHermesJMS

Now is the time to setup your module to update the hermesJMS configuration to be able to run in any environment. For this add a "com.google.code.maven-replacer-plugin" plugin to the project. I have created a folder "resources" under "src/main/" where I have put the folder for hermes configuration "hermes", the package of hermes "HermesJMS" and the library files for IBM Websphere MQ which is needed by HermesJMS for connecting to websphere MQ.
Below is example from my project : 


Step 4 : Module SoapProject

You are now set to work on the final module which is going to execute the SoapUI tests. There are few elements you should focus on and we will discuss about them one by one. 

Dependencies : Since the module UpdateHermesJMS should be executed before the SoapProject in order to have your configuration ready, this project needs to be prioritized. For that we need to define this module as dependency in the SoapProject module

Another level of dependencies has to be defined at plugin level for HermesJMS and websphere MQ library. 


I hope you are already aware of the SoapUI project execution and related plugin. The only thing extra you may have to do is to define two additional global variable for your project that will be used by SoapUI to configure HermesJMS
<globalProperties>
    <value>HERMES_JMS=${basedir}/../updateHermesConf/src/main/resources/HermesJMS</value>
    <value>hermesConfig=${basedir}/../updateHermesConf/src/main/resources/hermes</value>
</globalProperties>

Step 5 : Project LoadScript

Your maven project should be ready, however your project still needs one more tweeks in order to be able to use the right HermesJMS and corresponding configuration file.
Open your SoapUI workspace, right click on the project click on "Show Project View" and go on "Load Script" on the window of the project and insert the below script to configure HermesJMS and its configuration from the variables we have defined in previous step.



Now you are all set with the project and you can integrate your maven project on Jenkins with a simple maven build.

Monday 27 March 2017

Creating a simple Maven Project

When I was first introduced to maven, I was introduced as a build tool, an alternative to ant build which I was using for my SoapUI project builds. However, once I dwell deeper into maven and built my interest because of the wide range of features supported by Maven, only then I realized that maven is much more than just a build tool. It is a complete project management tool which allows the developers to have synergy in the project structure. It provides a defined structure to the project which allows developers to easily understand and help in case there is any transition on the project. The new developers who are familiar with maven structure can easily determine the class file, resources etc on the project.

In this post I am going to start with creating a simple maven project and then in later part I will explain how you can create a complex project in maven with multiple modules.

You can create a simple project with below command (here I am assuming that you already have configured JDK and Maven).
mvn archetype:generate -DgroupId=myProject.myCompany.com
                       -DartifactId=myProject
                       -DarchetypeArtifactId=maven-archetype-quickstart
                       -DinteractiveMode=false
 the above command will create a maven project structure where you will have
  • pom.xml : This is the main and single configuration file that contains majority of the information used by maven to build the project. 
  • Directory structure as :
    • src -
      • main - 
        • java - 
          • myProject -
            • myCompany -
              • com -
                • App.java : It is the class file
      • test -
        • java - 
          • myProject -
            • myCompany -
              • com -
                • App.java : It is the JUnit test class file for testing the class created by maven automatically.
To run this maven project file you need to run a simple command as below :
mvn install
Above command will compile your source code, perform test and then create a jar package in the directory as below :
  • target
    • myProject-1.0-SNAPSHOT.jar
    • classes
      • myProject
        • myCompany
          • com
            • App.class
    • maven-archiver
      • pom.properties
    • maven-status
      • maven-compiler-plugin
        • compile
          • default-compile
            • createdFiles.lst
            • inputFiles.lst
        • testCompile 
          • default-testCompile
            • createdFiles.lst
            • inputFiles.lst
    • surefire-reports
      • myProject.myCompany.com.AppTest.txt
      • TEST-myProject.myCompany.com.AppTest.xml
    • test-classes
      • myProject
        • myCompany
          • com
            • AppTest.class

As you can see that maven has compiled and packaged the project into a jar file (if it is web project then a war will be generated). You can run your package with following command from your project directory
java -cp target/myProject-1.0-SNAPSHOT.jar myProject.myCompany.com.App
Hello world!
So, as you can see in the above example that it is very easy to create a maven project. Now we look into the pom.xml file that has been generated by maven




Following element in the pom file together are known as co-ordinates of the projct
  • groupId
  • artifactId
  • packaging
  • version
Creating a complex project with multiple child elements or modules is even easier. Use the same command as above to create the maven master project. Now go to the project directory and remove the "src" directory created inside and edit the "pom.xml" and change the value of element "packaging" to "pom"
<packaging>pom</packaging>
Now your master project is almost ready and you can create the modules inside using the similar command as you used for creating the master project file.
mvn archetype:generate -DgroupId=myProject.myCompany.com
                       -DartifactId=module1
                       -DarchetypeArtifactId=maven-archetype-quickstart
                       -DinteractiveMode=false
mvn archetype:generate -DgroupId=myProject.myCompany.com
                       -DartifactId=module2
                       -DarchetypeArtifactId=maven-archetype-quickstart
                       -DinteractiveMode=false
The above two commands will create two modules or child projects within the master project and you will realize that the pom file inside the master project is updated to have below elements

And the child project will have reference to the master project as below



For a complex project you only need to compile the master project and child projects will be launched on their own.
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] myProject .......................................... SUCCESS [  0.757 s]
[INFO] module1 ............................................ SUCCESS [  5.211 s]
[INFO] module2 ............................................ SUCCESS [  0.748 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.909 s
[INFO] Finished at: 2017-03-26T19:26:54+05:30
[INFO] Final Memory: 15M/37M
[INFO] -----------------------------------------------------------------------

Thursday 23 March 2017

Maven : Compilation of Groovy Class files

There are various ways to build and use your groovy class files, you can use Gradle, or Goovyc Ant or using maven. In maven we have two main possibilities, one with GMaven and the other with Groovy eclipse.

I will elaborate an example with GMaven. GMaven Plugin definition requires the groovy dependency (groovy-all).

Below is an example of a pom file which compiles all the groovy class files located in a specific location.



In this example, my scripts are making connection to an oracle 12g database, hence I had to add dependency for ojdbc and since the DB is in 12G I was facing further errors related to xdb.war. So I downloaded the war file and installed it on maven using command below.

mvn install:install-file -Dfile=xdb6.jar -DgroupId=com.oracle -DartifactId=xmlparserv2 -Dversion=12.1.0.2 -Dpackaging=jar

After installing the war to my maven repository and added the same to dependencies as shown in example above.

Now I can use the above maven pom in my master file and other child pom build can use the classes compiled from it.

Dependency Error : SoapUI 5.2.1

In our project we observed a strange error because of which the maven build was failing. Further analysis led to a conclusion that the problem was specific to the version of Soap-UI plugin we were using in our project.

Here below is the error that we observed while executing the build


There are two possible ways to manage this error, one is to degrade the plugin version for Soap-UI. We saw that this error did not exist with Soap-UI plugin 4.6.? however with 5.?.? we have the problem.

Another way to fix the problem was to add "com.jgoodies:forms:1.0.7"  dependency to your pom file.


Tuesday 7 March 2017

Maven : Integration of tests

In the changing environment, the way tests are executed are also changing. Once we moved to agile mode from a traditional waterfall or v-Cycle mode of development we saw the need of automation. However, there is always a next step to be done, and in this case the obvious next step is to integrate them with development builds. There could be various ways to achive the same among which the most common one is deploying your tests on Jenkins. It provides a common platform where the development builds are done and at the same time the test builds can be done. In this article we will explore the possibility to integrate the various automated tests.

Maven : Integration of SoapUI automated Tests

SoapUI includes a Maven plugin for running SoapUI tests and mocks during Maven build. To use the correct version of SoapUI, you will have to see under the Soap Maven Repository. Maven should know about the soap repository and dependencies related to your project and it can be achieved by updating the maven settings which would be available at "conf" directory under maven binary location.
Edit the settings.xml and update the below elements as needed.
  • Local Repository : Location of the directory where dependency repository would be stored locally for compilation
  • Mirror : Location of maven mirror repository.
  • Repository : maven repositories and other repository where dependent tools and configuration can be found

  • PluginRepository : maven repositories and other repository where dependency plugins can be found
Attached an example settings.xml which is being used for one of the project.



Once the settings are configured your maven can execute soapUI tests.
The second step is to create pom files for the execution of the SoapUI tests. You may have two scenario here, one where you only have one project file and the other where you have multiple project files. For single project file based execution it is easier as you have to create only one pom file which can be located at the same place as the SoapUI project file. For multiple project file however, we need to organize our project to be in the way below :
  • Soap-Project
    • Project-1
      • Soap-Project-file.xml (Soap UI project file)
      • pom.xml (Child pom file configured for this project)
    • Project-2
      • Soap-Project-file.xml (Soap UI project file)
      • pom.xml (Child pom file configured for this project)
    • Project-n
      • Soap-Project-file.xml (Soap UI project file)
      • pom.xml (Child pom file configured for this project)
    • pom.xml (master pom file)
Prefer to use names of the files or directories without spaces or special characters to avoid referencing issues.

The master pom file would look as below :


A typical child pom file would look as below :


You can configure additional details for the project execution by further addition following parameters
  • projectFile : Specified the name of the SoapUI project file to use, default value is ${pom.artifactId}-soapui-project.xml
  • testSuite : Specifies the name of the TestSuite to run
  • testCase : Specifies the name of the TestCase to run
  • endpoint : Overrides the service endpoint to be invoked by any TestRequests
  • host : Overrides the target host:port to be invoked by any TestRequests
  • username : Overrides the username used by any TestRequests run
  • password : Overrides the password used by any TestRequests run
  • domain : Overrides the domain used by any TestRequests run
  • printReport : Controls if a small test report should be printed to the console (true/false)
  • outputFolder : Set which folder results/reports are saved to
  • junitReport : Turns on creation of JUnit-reports, (true/false)
  • exportAll : Controls if all test requests should be exported (default only exports errors), (true/false)
  • settingsFile : Specifies SoapUI settings file to use
  • wssPasswordType : Specifies WSS password type
  • project.password : Specifies password for encrypted project
  • settingsFile.password : Specifies password for encrypted settings file
  • globalProperties : Sets global properties
  • projectProperties : Sets project properties
  • saveAfterRun : Saves project file after run
  • testFailIgnore : Ignore failed tests.
  • reportFormat : Sets formats for created report. Pro only.
  • reportName : Sets which report to create. Pro only.
  • coverage : Sets the output to include Coverage HTML reports. Pro only.
  • environment : Sets the active environment. Pro only.
You will have to replicate the above configuration for all the child projects. Child project names in the pom, the directory name and the name mentioned under the master pom file should be same. Once the pom configuration is done you can test your execution with below command.
 > mvn -U clean install
In case of integration of mock service you will have to do some minor modification in the phase and goal of the execution section. The execution should appear as below :




 You can also define the following properties in order to customize your mock service on run time
  • projectFile : Specified the name of the SoapUI project file to use, default value is ${pom.artifactId}-soapui-project.xml
  • mockService : Specified the MockService to run
  • port : The local port to listen on, overrides the port configured for the MockService
  • path : The local path to listen on, overrides the path configured for the MockService
  • noBlock : Turns off blocking when MockRunner has started
  • settingsFile : Specifies SoapUI settings file to use, will contain path to integrated tool to run
  • project.password : Specifies password for encrypted project file
  • settingsFile.password : Specifies password for encrypted soapui settings file
  • saveAfterRun : Saves project file after run.
For more details following links can be referenced