Follow my blog with Bloglovin

Sunday, December 29, 2013

Play Behavior Driven Development with Cucumber

Cucumber is an behavior driven testing framework. In this post I will describe how to use this Play application.

To use Cucumber we need to install (add dependency to Play) four Cucumber modules :

  • cucumber-core Core Cucumber module
  • cucumberjava Cucumber Java language module.
  • cucumber-spring Cucumber Spring module for dependency injection of test related classes. Will not use this in this tutorial.
  • cucumber-junit Cucumer JUnit runner to run tests.
Add following dependencies in /project/Build.scala :

  val appDependencies = Seq(
    // Add your project dependencies here
    "info.cukes" % "cucumber-jvm" % "1.1.5",
    "info.cukes" % "cucumber-java" % "1.1.5",
    "info.cukes" % "cucumber-spring" % "1.1.5",
    "info.cukes" % "cucumber-junit" % "1.1.5"
  )

Note: If editing in Eclipse or other IDE the classpath maintained by IDE will not update with Spring dependency added. For this, run "play eclipse" (or corresponding IDE) command from Play console and then import project in IDE.

Create a class CucumberTest.java in /test folder of Play application:

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(format = "pretty")
public class CucumberTest {

}

This empty class will launch Cucumber tests.

Write step definition (behaviors) as described here.

Friday, December 27, 2013

Spring Framework integration with Play Framework

Spring is one of the most popular Java framework, mainly known for dependency injection. Play is popular web development Java framework, but lacks dependency injection. Spring can be used for this. Integrating Spring requires following:

1. Create ApplicationContext (using Spring XML and Java config) on application start-up.

2. @Autowire fields as annotated. This will be done automatically by Spring by configuring component scan.

3. Instantiating Play controllers using Spring.

Step #0: Add Spring dependency in Play application by adding them to /project/Build.scala file:

val appDependencies = Seq(
    // Add your project dependencies here,
    javaCore,
    javaJdbc,
    javaEbean,
    "org.springframework" % "spring-context" % "3.2.6.RELEASE"
....
)

Note: If editing in Eclipse or other IDE the classpath maintained by IDE will not update with Spring dependency added. For this, run "play eclipse" (or corresponding IDE) command from Play console and then import project in IDE.
In Play 2.2.x and higher this need to be added to build.sbt in root directory of application.

Step #1: Put Spring context XMLs in /conf directory of Play application.

Sample XML with package scan:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:component-scan base-package="controllers,services,dao,config"/>


</beans>

Step #2: Add Global.java is default package in /app directory of Play application.
Global.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import constants.ApplicationConstants;

import play.Application;
import play.GlobalSettings;
import play.Play;

public class Global extends GlobalSettings{

      private ApplicationContext applicationContext;
     
      @Override
      public void onStart(Application arg0) {
            String configLocation = Play.application().configuration().getString(“spring.context.location”);
            applicationContext = new ClassPathXmlApplicationContext(configLocation);
      }
     
      @Override
      public <A> A getControllerInstance(Class<A> type) throws Exception {
            return applicationContext.getBean(type);
      }
     
}

Also, add this property in /conf/application.conf file:

# Spring configuration
spring.context.location="application-context.xml"

This is optional and can be hard coded.

Step #3: In /conf/routes file add routes configuration, prefixed with @ as :

GET / @controllers.Application.home()
GET /tracks @controllers.Application.getTrack(artist: String, genre: String, frame: String)

Also, annotate controllers.Application class with @Component Spring annotation to be scanned by Spring and add controllers package in Spring's package scan.

Sample controller class:
@Component
public class Application extends Controller {

@Autowired
private AppDAO appDAO;

public Result home(){
return ok("Hi!");
}
}

Note: 
1. No need to make controller methods static. This is helpful in mocking instances for testing.
2. Super class Controller is Play specific (play.mvc package) and annotations are Spring specific.

Annotate any class with Spring annotations like, @Component or other stereotype annotation, and @Autowire them throughout application. Beans will be created and autowired as in usual Spring application. Only controllers need spatial configuration.

Let me know if there is any issues.






Elasticsearch Indexing using Talend ETL



Talend Open Stutio is open source ETL tool, supporting 450+ components (read/writre/transorm). Support for most of the databases, file ormats and other data sources, like web service, FTP, SMTP and some NoSQL, HDFS/Hadoop (in Talend BigData). But, no component to work with full text search engines like Lucene, Solr or Elasticsearch.
Often, we need full text search support in our application along with analytics. Having a component will allow indexing created/updated along with data loaded in warehouse. So, I have created a component for indexing (create/update) for Elasticsearch. Elasticsearch, is distributed, open source, full text engine built on top of Lucene. It is well documented, supports REST API over JSON and many other native language API for indexing and querying.
NOTE: This component is tested with Elasticsearch version 0.90.7. And, to support add or update, at least one field in schema must be designated as key to identify rows as unique. Multiple columns can be designated as key (in case of composite primary keys).

Download component from Talend Exchange
Download sample jobs for your talend version from https://github.com/sylentprayer/sample-projects/releases. Two sample jobs are there for talend version v5.0.3 and v5.3.1.

A tutorial for walk through these jobs are here.

Monday, December 16, 2013

Sentiment Analysis and Text Processing with Python : TextBlob

Sentiment analysis is used to find sentiment of author from a text, like tweets, posts or blog. Lets do this in Python:

1. Python is already installed on Linux distributions and can be install on windows using installer http://www.python.org/download/releases/2.7.6/

2. Install easy install and pip package manager:
Download python script from https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py and execute using "python <path/to/ez_setup.py>"

Install pip using "sudo easy_install pip"

3. Install TextBlob using "pip install -U textblob"

Python script to do sentiment analysis:


from textblob import TextBlob

class TextTool:



    def analyseText(self, text,  sourceLang='en', translateTo='en', spellCorrection=False):

        blob = TextBlob(text)

        if spellCorrection: #correct spelling

            correctedTxt = blob.correct()

        if translateTo!=None and sourceLang!=translateTo: #translate text

            if sourceLang!=None:

                translatedTxt = blob.translate(sourceLang, translateTo)

            else:

                translatedTxt = blob.translate(to=translateTo)

        #return sentiment score, 0-10 (negative to positive, 5 as neutral)

        return blob.sentiment, correctedTxt.__str__() if spellCorrection else None, translatedTxt.__str__() if translateTo!=None and sourceLang!=translateTo else None



textTool = TextTool()

analysis = textTool.analyseText(“Its great to see this again … wow”)


This Python script,
Corrects spelling or can suggest correction (check other methods for that).
Translates text using Google translate.
Analyzes sentiment of text (authors' feeling).


This function can do sentiment analysis, translation and spelling corrections. For more text processing see TextBlob website.

Popular Posts