Tag Archives: java

Sniff Request Parameters

If you need to debug request parameters for a HttpServletRequest:

 

TestNG with Parameters

Often we want to test a function against a set of variables.

TestNG offers @DataProvider annotation.

My goal is to test a service class method: findByType of class StateMachineObjectService. This method accepts two ENUM arguments: SmProcess process and SmState state.

I need to feed a collection of different processes and states. Best way is to create an object array with desired combinations and feed it to test function:

Output is like:

ADVERTISE INCOMPLETE
Hibernate: select statemachi0_.id as id1_22_, statemachi0_.process as process2_22_, statemachi0_.state as state3_22_ from sm_map statemachi0_ where statemachi0_.process=0 and statemachi0_.state=0

ADVERTISE ACTIVE
Hibernate: select statemachi0_.id as id1_22_, statemachi0_.process as process2_22_, statemachi0_.state as state3_22_ from sm_map statemachi0_ where statemachi0_.process=0 and statemachi0_.state=1

===============================================
Custom suite
Total tests run: 8, Failures: 0, Skips: 0
===============================================

Criteria Query – find by id

Example of DAO implementation for Hibernate Criteria Query -> Find By Id

 

Model class has attribute: id

Random number

This code generates a random number between low (inclusive) and high (exclusive):

 

 

Spring Data Mongo DB

Spring MVC Project with maven, mongodb, spring-data

This will contain only snippets of code. There are small dependencies that are not listed in these blocks of code.

I am running mongoDB in a remote Centos 6.2 Server.

Main Database is a PostgreSQL. Upper layers in MVC Service classes will try to fetch data from mongoDB. If is not available, will use SQL DB and update mongoDB. My application uses mongo for caching purposes and writes denormalized relationships in mongoDB.

pom.xml

 

web.xml

 

applicationContext.xml

 

A very simple entity:

A ultra simple Dao Repository:

 

Service class for Repository (contains JPA code for data transfer from JPA Repository to Mongo Repository, can be ignored):

Unit test for this implementation: