Showing posts with label REST. Show all posts
Showing posts with label REST. Show all posts

Saturday, March 10, 2012

Another MVC Framework - Play

Recently, I have found a little down time and thought I would investigate some other frameworks. I have been working mostly with Spring MVC this last year. I stumbled upon  Play Framework recently. It reminded me of a promotion of Ruby on Rails a few years ago comparing a several feet high stack of books on J2EE with the one simple single Rails book. Play framework is taking the same approach. Simplicity and immediacy.

It uses a command line similar to Rails (and Spring Roo by the way) to create initial constructs based on archetypes/patterns. It incorporates the web server so every change is instantly view-able on the web page and it will immediately display errors right in the web being developed. Further, it includes a simple template engine integrating the framework for creating pages to deliver to the client.

Watch a a nice tutorial posted on YouTube.

One premise play is based on is of more interest than its Rails like simplicity, that is its assumption of a stateless "share nothing'' REST first approach. Keeping state synchronized on the client, in the session and on persistent store can be annoying and difficult. It makes clustering more difficult and tends to produce complexity and as a result bug potential.


Friday, March 9, 2012

Looking at Google Plus API

I'm jumping around technologies right now but I am playing around with the new google plus restful APIs.

After watching an interesting youtube video of the API by jenny murphy, I explored the API interactively using the google plus api developers site.

Clicking on REST API, you can go to each api resource (people, activities, comments, oauth) and explore the API including running an example request and seeing the results. I have a google plus identity (+Ed)  so I posted a couple of public messages to play with.

I was able to download the google plus java client api and google plus java starter project.

I followed these steps to get started

Copy the google java starter project web app example (./google-plus-java-starter/web-app) to a new directory to begin playing. The instructions are in the readme.txt to get started.

In order to access the API, you first have to register your application using the google api console and get a token for oauth and access to the specific api. The steps

  1. go to  https://code.google.com/apis/console/b/0/?api=plus 
  2. click on 'create project'
  3. agree to the terms of service (and read :)) (two of them)
  4. Select the google+API (if not already selected (note the url I supplied should select this)
  5. CLick on API access (left side menu)
  6. Click on Create an OAuth 2.0 client ID
  7. describe your project (and upload logo if you have it) and press next
  8. select application type=web app, and site host name to run locally, select http: and enter localhost
  9. click 'create client ID'
  10. there are 4 pieces of info that you'll need to copy to your project: client id, client secret, redirect URI and API key (from the Simple API access)
in your new project (copied from starter) open src/main/resources/config.properties

paste in the 4 bits of information from the registration process (the redirect URI may already be correct)

now from the project root, use maven to launch the project using the built in jetty:

mvn compile jetty:run

go to your browser (chrome naturally) to check out 

http://localhost:8080/

This may fail (the version I had downloaded _v5 did fail) 

If you get an exception thrown when first accessing with error: 

Exception initializing page context java.lang.VerifyError: (class: org/apache/jasper/runtime/PageContextImpl, method: getELResolver signature: ()Ljavax/el/ELResolver;) Incompatible argument to function
Then you may need to fix the pom.xml. Open pom.xml and add scope provided element for the jsp 2.1 dependency:

<dependency>
     <groupid>org.mortbay.jetty</groupid>
     <artifactid>jsp-2.1</artifactid>
     <version>6.1.14</version>
     <scope>provided</scope>
</dependency>

Then restart and access again. Then it works!

You should see 'connect me' button.

This will require you to authorize access to your account from this program. Once authorized, it presents your profile information and a list of public activities.

Now you can play with the API

Friday, July 8, 2011

Testing my RESTful controllers with Spring RESTtemplate

I have been playing with Spring's RESTtemplate as an approach to integration testing on my project.

Overview of Testing a RIA with Spring and Spring MVC



I feel I should create a set of blog posts related to how I am doing testing with this technology stack. Sometimes this aggregation is helpful. In my team, we are testing at the following levels:

  • unit testing of everyting - pure tests, typically single class only, with mock (using Mockito)
  • integration testing of our services - out of container (web server) testing, but with db
  • integration testing of our controllers - in container testing, with db
  • functional testing of our ui (TBD) - full in container testing


This blog will focus on integration testing.

Integration testing, different from unit testing, tests the code as it is wired up to other components, the server and the data base as well.

Testing the controller layer in Spring MVC



To set the stage, I am using spring 3 with Spring MVC for my application. In particular, I am leveraging a lot of Ajax, accessing the controller from the browser and transporting JSON. I am using the Jackson JSON mapping implementation to marshal to from beans to JSON.

I need to create good integration tests of my controller classes so I can run JUnit tests that connect with the deployed web app and interact RESTfully like the client UI, passing JSON objects back and forth.

A helpful blog post I have used for guidance: is ralf.schaeftlein s blog detailing an approach to integration testing of restful MVC controllers with JSON. Exactly what I was interested in.
The basic steps seem to be:
  1. create a unit test with java4 leveraging Spring 3 Integration Test Annotations
  2. configure your application context XML to define your mappers that will convert between JSON and beans
  3. write your tests pojo style
Integration Testing with Spring uses annotations to supply a spring context to your test code. Further, the annotations specify what transaction manager to use and if a rollback should be performed after the test is complete. The tests itself are written with JUnit4. Here is a snippet showing the annotations required placed on your test class.


In this case, I am testing my Data Repository, which is essentially my Data Access Object (DAO) for you old schoolers. I will post why Repositories and not DAO in a future blog.
My application context file configures spring with the required setup for my application to run this test. First, the usual name spaces:

Then, we use automatic annotation discovery of out project

Here is wshere we configure the restTemplate to be used for this integration test of our controller
Finally the required configuration for our persistence. Notice we are using JPA configuration here


Finnaly the test code.

We write an integration test for our controller that connects remotely to the deployed application, sends beans mapped from Pojo to json for the call, and then maps back json to Pojo to assert results.