miércoles, enero 02, 2013

Testing Spring Data MongoDB Applications with NoSQLUnit

Spring Data MongoDB


Spring Data MongoDB is the project within Spring Data project which provides an extension to the Spring programming model for writing applications that uses MongoDB as database.

To write tests using NoSQLUnit for Spring Data MongoDB applications, you do need nothing special apart from considering that Spring Data MongoDB uses a special property called _class for storing type information alongside the document.

_class property stores the fully qualified classname inside the document for the top-level document as well as for every value if it is a complex type.

Note Type mapping
MappingMongoConverter is used as default type mapping implementation but you can customize even more using @TypeAlias or implementing TypeInformationMapper interface.

Application


Starfleet has asked us to develop an application for storing all logs of starship crew members into their systems.

To implement this requirement we are going to use MongoDB database as backend system and Spring Data MongoDB at persistence layer.

Log documents have next json format:

Example of Log Document

{
        "_class" : "com.lordofthejars.nosqlunit.springdata.mongodb.log.Log" ,
        "_id" : 1 ,
        "owner" : "Captain" ,
        "stardate" : {
                "century" : 4 ,
                "season" : 3 ,
                "sequence" : 125 ,
                "day" : 8
        } ,
        "messages" : [
                        "We have entered a spectacular binary star system in the Kavis Alpha sector on a most critical mission of astrophysical research. Our eminent guest, Dr. Paul Stubbs, will attempt to study the decay of neutronium expelled at relativistic speeds from a massive stellar explosion which will occur here in a matter of hours." ,
                        "Our computer core has clearly been tampered with and yet there is no sign of a breach of security on board. We have engines back and will attempt to complete our mission. But without a reliable computer, Dr. Stubbs' experiment is in serious jeopardy."
        ]
}

This document is modelized into two Java classes, one for whole document and another one for stardate part.

Stardate class

@Document
public class Stardate {

        private int century;
        private int season;
        private int sequence;
        private int day;

        public static final Stardate createStardate(int century, int season, int sequence, int day) {

                Stardate stardate = new Stardate();

                stardate.setCentury(century);
                stardate.setSeason(season);
                stardate.setSequence(sequence);
                stardate.setDay(day);

                return stardate;

        }

        //Getters and Setters
}
Log class

@Document
public class Log {

        @Id
        private int logId;

        private String owner;
        private Stardate stardate;

        private List<String> messages = new ArrayList<String>();

        //Getters and Setters
}

Apart from model classes, we also need a DAO class for implementing CRUD operations, and spring application context file.

MongoLogManager class

@Repository
public class MongoLogManager implements LogManager {

        private MongoTemplate mongoTemplate;

        public void create(Log log) {
                this.mongoTemplate.insert(log);
        }

        public List<Log> findAll() {
                return this.mongoTemplate.findAll(Log.class);
        }

        @Autowired
        public void setMongoTemplate(MongoTemplate mongoTemplate) {
                this.mongoTemplate = mongoTemplate;
        }

}
application-context file

<?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.1.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.1.xsd">

     <context:component-scan base-package="com.lordofthejars.nosqlunit.springdata.mongodb"/>
     <context:annotation-config/>

</beans>
Tip
For this example we have used MongoTemplate class for accessing to MongoDB to not create an overcomplicated example, but in a bigger project I recommend use Spring Data Repository approach by implementing CrudRepository interface on manager classes.

Testing


As has been told previously, you don’t have to do anything special beyond using class property correctly. Let’s see the dataset used to test the findAll method by seeding _log
collection of logs database.

all-logs file

{
        "log":[
                {
                        "_class" : "com.lordofthejars.nosqlunit.springdata.mongodb.log.Log" ,
                        "_id" : 1 ,
                        "owner" : "Captain" ,
                        "stardate" : {
                                "century" : 4 ,
                                "season" : 3 ,
                                "sequence" : 125 ,
                                "day" : 8
                        } ,
                        "messages" : [
                                "We have entered a spectacular binary star system in the Kavis Alpha sector on a most critical mission of astrophysical research. Our eminent guest, Dr. Paul Stubbs, will attempt to study the decay of neutronium expelled at relativistic speeds from a massive stellar explosion which will occur here in a matter of hours." ,
                                "Our computer core has clearly been tampered with and yet there is no sign of a breach of security on board. We have engines back and will attempt to complete our mission. But without a reliable computer, Dr. Stubbs' experiment is in serious jeopardy."
                        ]
                }
                ,
                {
                        "_class" : "com.lordofthejars.nosqlunit.springdata.mongodb.log.Log" ,
                        "_id" : 2 ,
                        "owner" : "Captain" ,
                        "stardate" : {
                                "century" : 4 ,
                                "season" : 3 ,
                                "sequence" : 152 ,
                                "day" : 4
                        } ,
                        "messages" : [
                                "We are cautiously entering the Delta Rana star system three days after receiving a distress call from the Federation colony on its fourth planet. The garbled transmission reported the colony under attack from an unidentified spacecraft. Our mission is one of rescue and, if necessary, confrontation with a hostile force."
                        ]
                }
                ...
}

See that _class property is set to full qualified name of Log class.
Next step is configuring MongoTemplate for test execution.

LocalhostMongoAppConfig

@Configuration
@Profile("test")
public class LocalhostMongoAppConfig {

        private static final String DATABASE_NAME = "logs";

        public @Bean Mongo mongo() throws UnknownHostException, MongoException {
                Mongo mongo = new Mongo("localhost");
                return mongo;
        }

        public @Bean MongoTemplate mongoTemplate() throws UnknownHostException, MongoException {
                MongoTemplate mongoTemplate = new MongoTemplate(mongo(), DATABASE_NAME);
                return mongoTemplate;
        }

}

Notice that this MongoTemplate object will be instantiated only when test profile is active.
And now we can write the JUnit test case:

WhenAlmiralWantsToReadLogs

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:com/lordofthejars/nosqlunit/springdata/mongodb/log/application-context-test.xml")
@ActiveProfiles("test")
@UsingDataSet(locations = "all-logs.json", loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public class WhenAlmiralWantsToReadLogs {

        @ClassRule
        public static ManagedMongoDb managedMongoDb = newManagedMongoDbRule()
                        .mongodPath(
                                        "/Users/alexsotobueno/Applications/mongodb-osx-x86_64-2.0.5")
                        .build();

        @Rule
        public MongoDbRule mongoDbRule = newMongoDbRule().defaultManagedMongoDb("logs");

        @Autowired
        private LogManager logManager;

        @Test
        public void all_entries_should_be_loaded() {

                List<Log> allLogs = logManager.findAll();
                assertThat(allLogs, hasSize(3));

        }

}

There are some important points in the previous class to take a look:
  1. Because NoSQLUnit uses JUnit Rules you can use @RunWith(SpringJUnit4ClassRunner) freely.
  2. Using @ActiveProfiles we are loading the test configuration instead of the production ones.
  3. You can use Spring annotations like @Autowired without any problem.

Conclusions


There is not much difference between writing tests for none Spring Data MongoDB and applications that use it. Only keep in mind to define correctly the _class property.

We keep learning,
Alex.
Els astronautes volen baix, Els núvols passen com qui no diu res. Amb les butxaques a les mans, Caminarem els passos d’altres peus. (Pa amb Oli i Sal - Blaumut)
Music: http://www.youtube.com/watch?v=Hkc5piclElg

3 comentarios:

Libin Huang dijo...

20160509libinmulberry outlet store
true religion outlet
herve leger dresses
pandora jewelry
cartier watches
polo ralph lauren
cheap oakley sunglasses
tory burch outlet online
nfl jersey wholesale
swarovski jewelry
ray ban sunglasses
michael kors online
michael kors outlet
snapbacks wholesale
lebron james shoes
nike blazer pas cher
michael kors outlet online
mcm backpack
coach outlet
tory burch outlet online
michael kors handbags
calvin klein underwear
kate spade outlet
oakley sunglasses
michael kors outlet
louis vuitton handbags
true religion canada
asics,asics israel,asics shoes,asics running shoes,asics israel,asics gel,asics running,asics gel nimbus,asics gel kayano
gucci outlet online
michael kors outlet
air max 2015
nike air force 1
swarovski crystal
fitflops shoes
timberland shoes

chenlina dijo...

chenlina20160716
beats solo
louis vuitton outlet
christian louboutin outlet
mont blanc fountain pens
gucci handbags
coach factory outlet
cheap jordans
true religion outlet
supra sneakers
fitflops shoes
lebron james shoes 13
nike air max 90
polo ralph lauren outlet
louis vuitton handbags
cheap oakley sunglasses
nike blazers
true religion outlet
michael kors outlet
coach canada
michael kors outlet clearance
burberry outlet
michael kors outlet
louis vuitton purses
toms outlet
celine handbags
michael kors handbags
louis vuitton outlet stores
coach outlet
coach outlet
ghd hair straighteners
louis vuitton outlet
adidas nmd
toms shoes
true religion outlet
ray ban sunglasses discount
oakley sunglasses
louis vuitton bags
michael kors outlet
louis vuitton
hermes handbags
as

Gege Dai dijo...

football shirts
cheap oakley sunglasses
gucci outlet online
michael kors outlet
swarovski outlet
michael kors outlet
michael kors outlet
nike outlet store
tiffany and co
celine outlet online
nhl jerseys
true religion outlet
ralph lauren polo
true religion jeans
toms outlet
michael kors handbags outlet
michael kors outlet
hermes birkin bag
rolex orologi
juicy couture outlet
tory burch sandals
fitflops sale
polo ralph lauren
michael kors outlet
herve leger dresses
nike trainers
valentino outlet
lebron james shoes
hollister uk
ray ban sunglasses
fitflops sale
michael kors outlet
fitflops shoes
coach outlet
louis vuitton sunglasses for women
czq20160722

Donate If You Can and Find Post Useful