Added tests with MongoTemplate

This commit is contained in:
coach88 2015-07-31 22:18:25 +03:00
parent 0f43d06678
commit d1263934d3
2 changed files with 125 additions and 66 deletions

View File

@ -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<User> 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);
}
}

View File

@ -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");
}
}