Tuesday, January 17, 2012

Create sample web app project in Eclipse using m2eclipse

Make sure you have m2eclipse installed from the update site http://m2eclipse.sonatype.org/sites/m2e
Key here is to also install the m2e-extras which allows for WTP integration.
Install m2e-extras from http://m2eclipse.sonatype.org/sites/m2e-extras

In Eclipse
    File -- New -- Other -- Maven -- Maven Project -- Next

In the New Maven Project dialog select
Group Id: org.apache.maven.archetypes
Artifact Id: maven-archetype-webapp

Provide a GroupId and ArtifactId for your project and thats all there is to it.

Wednesday, January 4, 2012

A way to test REST resource

While looking around for an easy way to test REST APIs, I stumbled across rest-assured
This is a very simple and elegant way to test your code.

Add the following to your pom.xml
 <dependency>  
     <groupId>com.jayway.restassured</groupId>  
     <artifactId>rest-assured</artifactId>  
     <version>1.5</version>  
     <scope>test</scope>  
 </dependency>  

Your test will look something like ..
 public void testInvalidInputInvalidDelayTol()  
 {  
  with().  
   parameters(.....).  
  expect().  
   statusCode(400).  
   body(  
    equalToIgnoringWhiteSpace("....")  
   ).  
  when().  
   with().authentication().preemptive().basic("username", "password").  
   get("/network-adapter/network");  
 }  


Rest-assured supports various response formats like json, xml.  Explore the rest-assured wiki to learn more.