Tuesday, March 25, 2014

SoapUI, Groovy Script tutorial

Groovy is built on JVM, so does the SoapUI, it’s no surprise that SoapUI offers the Groovy script support by default. Let’s do a simple test, basically send out a Http request with dynamic parameters, and parse the result using the regular expression capability of Groovy and IO capablity to save the result back to a file. for the parameter we can use the groorvy to poll a file and return some randomized keywords.

so the keywords file looks like
image

we create one project with one test suite, then under the suite, we can add test case, and for the test step, let’s add first step called GetKeyword as a groovy script.


image

then we put some script here, which will assign a keyword property to the context randomly.
image

then we can access this keyword using context.keyword in next step, now, let’s create a http request to bing.com

image

Click extract params,now if we run this test

image

the query keyword is hardcoded, we can replace it with a groovy expression

image

then add the 3rd step, that we read the response in step 2 and save it to a file

image

if we run the test case, you will see file named keyword.txt with the http response from bing.com search

also you can just copy and save the following xml as a project file, you open in in SoapUI

<?xml version="1.0" encoding="UTF-8"?>
<con:soapui-project activeEnvironment="Default" name="tst" soapui-version="4.6.4" xmlns:con="http://eviware.com/soapui/config"><con:settings/><con:testSuite name="testgroovy"><con:settings/><con:runType>SEQUENTIAL</con:runType><con:testCase failOnError="true" failTestCaseOnErrors="true" keepSession="false" maxResults="0" name="TestCase 1" searchProperties="true" id="cdd185b8-b6ae-4455-9713-ec70b5404569"><con:settings/><con:testStep type="groovy" name="GetKeyword"><con:settings/><con:config><script>def file=new File("c:\\a.txt")
def lines=file.readLines();
def randomindex=new Random()
def keyword=lines[randomindex.nextInt(lines.size())];
context.keyword=keyword
log.info(keyword)</script></con:config></con:testStep><con:testStep type="httprequest" name="Req2Bing"><con:settings/><con:config method="GET" xsi:type="con:HttpRequest" name="Req2Bing" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><con:settings><con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers">&lt;xml-fragment/></con:setting></con:settings><con:endpoint>http://www.bing.com/search</con:endpoint><con:request/><con:credentials><con:authType>Global HTTP Settings</con:authType></con:credentials><con:jmsConfig JMSDeliveryMode="PERSISTENT"/><con:jmsPropertyConfig/><con:parameters><con:parameter><con:name>q</con:name><con:value>${=context.keyword}</con:value><con:style>QUERY</con:style><con:default>androidyou</con:default><con:path xsi:nil="true"/><con:description xsi:nil="true"/></con:parameter></con:parameters></con:config></con:testStep><con:testStep type="groovy" name="SaveitToFile"><con:settings/><con:config><script>def resp=testRunner.getTestCase().getTestStepByName("Req2Bing").httpRequest.response
def raw=resp.getContentAsString()
//save it fo file
def resultFile=new File("c:\\" + context.keyword + "t.txt")
resultFile.write(raw);</script></con:config></con:testStep><con:properties/></con:testCase><con:properties/></con:testSuite><con:properties/><con:wssContainer/></con:soapui-project>

Monday, March 24, 2014

WMIC Tips, Export format and get hint about callable Actions

On windows , you can use wmic to list all process running by type process in the wmic console.
image

Also you can run some filter, like listing all  notepad.exe
procecss where name=”notepad.exe” list brief

image

if we don’t know which field are available to get for this Process object, try get /?

image

so we can call process where name=”notepad.exe” get commandline, name, processid

image

if we want to kill those processes, we can call call /? to see available actions

image

So we can call terninate method to kill all notepad.exe

image

then all process are gone,

we can do this same thing on service,

image

to export all the service you can run wmic /output:file  service where state=”running” /format:htable

image

image

Thursday, March 13, 2014

Apache Archiva, yet another simple Maven repository in your enterprise

Before I got a chance to play Archiva, I’ve always used the Nexus as the my maven repository across different machines. Now it turns out the Archiva is very easy to use and pretty powerful for most features I used in Nexus.
  Apache archiva is s standard jsp application, we can hosted in any container or just run as standalone,
image

the major usage of Archiva for me is to use it as a central repository to store all my artifacts. Basically when you are done with your project, run a “maven deploy” it will deploy it to your central repository, then across the computers you work, you can find those jars both on runtime or compile time. 

To do that, we need config a setting on the ~/.m2/settings.xml, put the server credentials there , and one your project pom file, put the repository as one of the distributionManagement subnode.

on the settings.xml,
add my own repositories that are proxyed and cached to remote,
image

then on the client pom.xml. just add the repository to the distributionmgmt

image

then once we run the mvn deploy, it will be deployed to the centorl internal repo.

Monday, March 10, 2014

Apache Camel tutorial : Quick start from scratch (Console App using Xml as the routes definition , without Spring)

Following the last post Apache Camel tutorial : Quick start from scratch (Console App , without Spring), if you don’t want to hard code the routes, we can simply use xml as keep the routes definition.

to do that , let’s change the code a little bit.

image

then in the route.xml, add the route definition there,
image

when run, you see the similar results,

image

for this code, check the branch demoxml

https://github.com/ryandh/apacheCamelTutorial/tree/demoxml

Apache Camel tutorial : Quick start from scratch (Console App , without Spring)

Apache Camel is one great implementation of the EIP pattern, If you are not a seasoned Spring/Java developer , you might found it’s hard to get started, the goal of this post is simplify that process and show you how easy to get started without remember any code snippet. we will use plain console app to host and start one camel route.

to get started, simply open eclipse and create one maven project (the purpose of maven here is to add those dependency automatically), if you don’t have any maven experience, you can simply download the required jar and add to you build path.

Create one Maven project , using the simple project type, we get one project with maven ready,
image

Create one simple class with main method,
image

Now , right click pom.xml, click maven menu,  add a dependency to camel-core, you can simply search camel-core

image

Now, we can start coding,
image

we’ve initialized the camelcontext, next step is adding our routes by xml or java code.

let’s start from java code first, since it has intelligence and life is little bit easier,

image

it looks like we can just add one RoutesBuilder to the context, is there any existing one, or we have to build our own.
ask the eclipse create a local variable for the routesbuilder, let’s see whether there is any existing one,

image

it turns out Routesbuilder is a interface, and have RoutesBuilder as an implementation, so let’s extends that one,
image

then the code looks like this,

image

in the configure , we just inject the code snippets listed on the camel component page, here is one demo, that dump out some message every 2 seconds,

image

if we run this, we can see the message get shown in the console every 2 seconds.

for the log component, we can’t see the message because it is NOP for slf4j by default, so lets add a slf4j-simple to the pom.xml

image

run again, you can see the message is listed in the console window.
image

if we want to export this as a executable jar, go to the pom.xml ,right click maven menu to add a mvn-assemly-plugin
image

Change the pom.xml a little big , pint to the mainclass , you can copy the xml here
image

then go to the project folder, run mvn compile assemlby : Single

image

you will see a jar in the target folder,
image

Now, we can run the jar directly, by typing java –jar pathtothejar

image

image

for the source code, check it out here, https://github.com/ryandh/apacheCamelTutorial/tree/DemoConsole

Maven Tutorial: how to create a maven plugin that rename the packaged jar files

It turns out it is super easy to create one plugin by using the maven archetype in eclipse , here is quick tutorial to create one plugin that we can add to any project, the plugin will rename the packaged jars to our desired format. I will put a data in the jar names as an example.

for the demo, I will create one parent Project named Container, and two modules under this container, one is named MyPlugin, another module will be Client which reference the Plugin.
so one project which is the parent, pom as the target, and two modules.

image

for the Client, it’s just a regular simple Maven module.
image

for the MyPlugin project, we want it to use the archetype maven-arechetype-mojo as the base type.
image

once done, we can right click the pom.xml in client library, and plugin dependency, you can see our new plugin is there.
image

Click ok to add the MyPlugin dependency.
here is the pom.xml for Client project
image

Now, the plugin is ready, we can go the client folder, and run our plugin,
before we run this, run a “mvn install” under Container folder to install the plugins to local repository.

on the Client folder, run “mvn MyPlugin:touch”
you can see the touch.txt is there, which is created by our plugin.
image

go back to demo for the file renaming, we want the plugin to rename the jar file from Client-0.0.1-SNAPSHOT.jar to Client-0.0.1-SNAPSHOT-20140310.jar

so let’s rewrite our plugin execution logic,

image

run this target again, we can see the file is renamed to our format

image

if we want add this plugin to our package target, we can change the execution for this plugin to package.

image

then once we run the package , this plugin will be invoked automatically.

image

 
Locations of visitors to this page