From d1263934d3b1b5561aebe329769e16e9d35f9940 Mon Sep 17 00:00:00 2001 From: coach88 Date: Fri, 31 Jul 2015 22:18:25 +0300 Subject: [PATCH] Added tests with MongoTemplate --- .../org/baeldung/service/UserService.java | 66 --------- .../MongoTemplateIntegrationTest.java | 125 ++++++++++++++++++ 2 files changed, 125 insertions(+), 66 deletions(-) delete mode 100644 spring-data-mongodb/src/main/java/org/baeldung/service/UserService.java create mode 100644 spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateIntegrationTest.java diff --git a/spring-data-mongodb/src/main/java/org/baeldung/service/UserService.java b/spring-data-mongodb/src/main/java/org/baeldung/service/UserService.java deleted file mode 100644 index 06a6735340..0000000000 --- a/spring-data-mongodb/src/main/java/org/baeldung/service/UserService.java +++ /dev/null @@ -1,66 +0,0 @@ -package org.baeldung.service; - -import java.util.List; - -import org.baeldung.model.User; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.data.mongodb.core.query.Criteria; -import org.springframework.data.mongodb.core.query.Query; -import org.springframework.data.mongodb.core.query.Update; - -import com.mongodb.WriteResult; - -public class UserService { - - @Autowired - private MongoTemplate mongoTemplate; - - public void insertUser(final User user) { - mongoTemplate.insert(user, "user"); - } - - public List listUser() { - return mongoTemplate.findAll(User.class, "user"); - } - - public void removeUser(final User user) { - mongoTemplate.remove(user, "user"); - } - - public void saveUser(final User user) { - mongoTemplate.save(user, "user"); - } - - public User findAndModifyUser(final String name, final String newName) { - final Query query = new Query(); - query.addCriteria(Criteria.where("name").is(name)); - final Update update = new Update(); - update.set("name", newName); - return mongoTemplate.findAndModify(query, update, User.class); - } - - public void updateFirstUser(final String name, final String newName) { - final Query query = new Query(); - query.addCriteria(Criteria.where("name").is(name)); - final Update update = new Update(); - update.set("name", newName); - mongoTemplate.updateFirst(query, update, User.class); - } - - public WriteResult upsertUser(final String name, final String newName) { - final Query query = new Query(); - query.addCriteria(Criteria.where("name").is(name)); - final Update update = new Update(); - update.set("name", newName); - return mongoTemplate.upsert(query, update, User.class); - } - - public void updateMultiUser(final String name, final String newName) { - final Query query = new Query(); - query.addCriteria(Criteria.where("name").is(name)); - final Update update = new Update(); - update.set("name", newName); - mongoTemplate.updateMulti(query, update, User.class); - } -} diff --git a/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateIntegrationTest.java b/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateIntegrationTest.java new file mode 100644 index 0000000000..3868715714 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateIntegrationTest.java @@ -0,0 +1,125 @@ +package org.baeldung.mongotemplate; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.baeldung.config.MongoConfig; +import org.baeldung.model.User; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.data.mongodb.core.query.Update; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = MongoConfig.class) +public class MongoTemplateIntegrationTest { + + @Autowired + private MongoTemplate mongoTemplate; + + @Before + public void testSetup() { + mongoTemplate.createCollection(User.class); + } + + @After + public void tearDown() { + mongoTemplate.dropCollection(User.class); + } + + @Test + public void whenInsertingUser_thenUserIsInserted() { + User user = new User(); + user.setName("Jon"); + mongoTemplate.insert(user); + } + + @Test + public void givenUserExists_whenSavingExistUser_thenUserIsUpdated() { + User user = new User(); + user.setName("Jack"); + mongoTemplate.insert(user); + + user = new User(); + user.setName("Jim"); + mongoTemplate.save(user); + } + + @Test + public void givenUsersExist_whenUpdatingFirstUser_thenFirstUserIsUpdated() { + User user = new User(); + user.setName("Alex"); + mongoTemplate.insert(user); + + user = new User(); + user.setName("Alex"); + mongoTemplate.insert(user); + + Query query = new Query(); + query.addCriteria(Criteria.where("name").is("Alex")); + Update update = new Update(); + update.set("name", "James"); + mongoTemplate.updateFirst(query, update, User.class); + } + + @Test + public void givenUsersExist_whenUpdatingMultiUser_thenAllUserAreUpdated() { + User user = new User(); + user.setName("Eugen"); + mongoTemplate.insert(user); + + user = new User(); + user.setName("Eugen"); + mongoTemplate.insert(user); + + Query query = new Query(); + query.addCriteria(Criteria.where("name").is("Eugen")); + Update update = new Update(); + update.set("name", "Victor"); + mongoTemplate.updateMulti(query, update, User.class); + } + + @Test + public void givenUserExists_whenFindingAndModifyingUser_thenUserIsReturnedAndUpdated() { + User user = new User(); + user.setName("Markus"); + mongoTemplate.insert(user); + + Query query = new Query(); + query.addCriteria(Criteria.where("name").is("Markus")); + Update update = new Update(); + update.set("name", "Nick"); + user = mongoTemplate.findAndModify(query, update, User.class); + assertThat(user.getName(),is("Markus")); + } + + @Test + public void givenUserExists_whenUpsertingUser_thenUserIsUpdated() { + User user = new User(); + user.setName("Markus"); + mongoTemplate.insert(user); + + Query query = new Query(); + query.addCriteria(Criteria.where("name").is("Markus")); + Update update = new Update(); + update.set("name", "Nick"); + mongoTemplate.upsert(query, update, User.class); + } + + @Test + public void givenUserExists_whenRemovingUser_thenUserIsRemoved() { + User user = new User(); + user.setName("Benn"); + mongoTemplate.insert(user); + + mongoTemplate.remove(user, "user"); + } + +}