Merge remote-tracking branch 'upstream/master'

Conflicts:
	spring-data-mongodb/src/test/java/org/baeldung/repository/UserRepositoryIntegrationTest.java
This commit is contained in:
coach88 2015-07-31 21:16:59 +03:00
commit 0f43d06678
4 changed files with 44 additions and 43 deletions

View File

@ -7,19 +7,6 @@
<name>spring-data-mongodb</name> <name>spring-data-mongodb</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.data.version>1.7.1.RELEASE</org.springframework.data.version>
<org.springframework.version>4.1.7.RELEASE</org.springframework.version>
<org.hamcrest.version>1.3</org.hamcrest.version>
<junit.version>4.11</junit.version>
<rest-assured.version>2.4.1</rest-assured.version>
<org.slf4j.version>1.7.12</org.slf4j.version>
<logback.version>1.1.3</logback.version>
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
@ -80,6 +67,7 @@
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
@ -92,4 +80,18 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.data.version>1.7.1.RELEASE</org.springframework.data.version>
<org.springframework.version>4.1.7.RELEASE</org.springframework.version>
<org.hamcrest.version>1.3</org.hamcrest.version>
<junit.version>4.11</junit.version>
<rest-assured.version>2.4.1</rest-assured.version>
<org.slf4j.version>1.7.12</org.slf4j.version>
<logback.version>1.1.3</logback.version>
</properties>
</project> </project>

View File

@ -1,8 +1,8 @@
package org.baeldung.repository; package org.baeldung.repository;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.baeldung.model.User; import org.baeldung.model.User;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface UserRepository extends MongoRepository<User, String> { public interface UserRepository extends MongoRepository<User, String> {
//
} }

View File

@ -11,13 +11,12 @@ import org.springframework.data.mongodb.core.query.Update;
import com.mongodb.WriteResult; import com.mongodb.WriteResult;
public class UserService { public class UserService {
@Autowired @Autowired
private MongoTemplate mongoTemplate; private MongoTemplate mongoTemplate;
public void insertUser(User user) { public void insertUser(final User user) {
mongoTemplate.insert(user, "user"); mongoTemplate.insert(user, "user");
} }
@ -25,42 +24,42 @@ public class UserService {
return mongoTemplate.findAll(User.class, "user"); return mongoTemplate.findAll(User.class, "user");
} }
public void removeUser(User user) { public void removeUser(final User user) {
mongoTemplate.remove(user, "user"); mongoTemplate.remove(user, "user");
} }
public void saveUser(User user) { public void saveUser(final User user) {
mongoTemplate.save(user, "user"); mongoTemplate.save(user, "user");
} }
public User findAndModifyUser(String name, String newName) { public User findAndModifyUser(final String name, final String newName) {
Query query = new Query(); final Query query = new Query();
query.addCriteria(Criteria.where("name").is(name)); query.addCriteria(Criteria.where("name").is(name));
Update update = new Update(); final Update update = new Update();
update.set("name", newName); update.set("name", newName);
return mongoTemplate.findAndModify(query, update, User.class); return mongoTemplate.findAndModify(query, update, User.class);
} }
public void updateFirstUser(String name, String newName) { public void updateFirstUser(final String name, final String newName) {
Query query = new Query(); final Query query = new Query();
query.addCriteria(Criteria.where("name").is(name)); query.addCriteria(Criteria.where("name").is(name));
Update update = new Update(); final Update update = new Update();
update.set("name", newName); update.set("name", newName);
mongoTemplate.updateFirst(query, update, User.class); mongoTemplate.updateFirst(query, update, User.class);
} }
public WriteResult upsertUser(String name, String newName) { public WriteResult upsertUser(final String name, final String newName) {
Query query = new Query(); final Query query = new Query();
query.addCriteria(Criteria.where("name").is(name)); query.addCriteria(Criteria.where("name").is(name));
Update update = new Update(); final Update update = new Update();
update.set("name", newName); update.set("name", newName);
return mongoTemplate.upsert(query, update, User.class); return mongoTemplate.upsert(query, update, User.class);
} }
public void updateMultiUser(String name, String newName) { public void updateMultiUser(final String name, final String newName) {
Query query = new Query(); final Query query = new Query();
query.addCriteria(Criteria.where("name").is(name)); query.addCriteria(Criteria.where("name").is(name));
Update update = new Update(); final Update update = new Update();
update.set("name", newName); update.set("name", newName);
mongoTemplate.updateMulti(query, update, User.class); mongoTemplate.updateMulti(query, update, User.class);
} }

View File

@ -44,7 +44,7 @@ public class UserRepositoryIntegrationTest {
@Test @Test
public void whenInsertingUser_thenUserIsInserted() { public void whenInsertingUser_thenUserIsInserted() {
User user = new User(); final User user = new User();
user.setName("Jon"); user.setName("Jon");
userRepository.insert(user); userRepository.insert(user);
@ -58,7 +58,7 @@ public class UserRepositoryIntegrationTest {
mongoOps.insert(user); mongoOps.insert(user);
user = mongoOps.findOne(Query.query(Criteria.where("name").is("Jack")), User.class); user = mongoOps.findOne(Query.query(Criteria.where("name").is("Jack")), User.class);
String id = user.getId(); final String id = user.getId();
user.setName("Jim"); user.setName("Jim");
userRepository.save(user); userRepository.save(user);
@ -68,7 +68,7 @@ public class UserRepositoryIntegrationTest {
@Test @Test
public void givenUserExists_whenDeletingUser_thenUserIsDeleted() { public void givenUserExists_whenDeletingUser_thenUserIsDeleted() {
User user = new User(); final User user = new User();
user.setName("Benn"); user.setName("Benn");
mongoOps.insert(user); mongoOps.insert(user);
@ -84,7 +84,7 @@ public class UserRepositoryIntegrationTest {
mongoOps.insert(user); mongoOps.insert(user);
user = mongoOps.findOne(Query.query(Criteria.where("name").is("Chris")), User.class); user = mongoOps.findOne(Query.query(Criteria.where("name").is("Chris")), User.class);
User foundUser = userRepository.findOne(user.getId()); final User foundUser = userRepository.findOne(user.getId());
assertThat(user.getName(), is(foundUser.getName())); assertThat(user.getName(), is(foundUser.getName()));
} }
@ -96,7 +96,7 @@ public class UserRepositoryIntegrationTest {
mongoOps.insert(user); mongoOps.insert(user);
user = mongoOps.findOne(Query.query(Criteria.where("name").is("Harris")), User.class); user = mongoOps.findOne(Query.query(Criteria.where("name").is("Harris")), User.class);
boolean isExists = userRepository.exists(user.getId()); final boolean isExists = userRepository.exists(user.getId());
assertThat(isExists, is(true)); assertThat(isExists, is(true));
} }
@ -111,7 +111,7 @@ public class UserRepositoryIntegrationTest {
user.setName("Adam"); user.setName("Adam");
mongoOps.insert(user); mongoOps.insert(user);
List<User> users = userRepository.findAll(new Sort(Sort.Direction.ASC, "name")); final List<User> users = userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));
assertThat(users.size(), is(2)); assertThat(users.size(), is(2));
assertThat(users.get(0).getName(), is("Adam")); assertThat(users.get(0).getName(), is("Adam"));
@ -128,9 +128,9 @@ public class UserRepositoryIntegrationTest {
user.setName("Adam"); user.setName("Adam");
mongoOps.insert(user); mongoOps.insert(user);
Pageable pageableRequest = new PageRequest(0, 2); final Pageable pageableRequest = new PageRequest(0, 2);
Page<User> users = userRepository.findAll(pageableRequest); final Page<User> users = userRepository.findAll(pageableRequest);
assertThat(users.getTotalPages(), is(1)); assertThat(users.getTotalPages(), is(1));
assertThat(users.iterator().next().getName(), is("Brendan")); assertThat(users.iterator().next().getName(), is("Brendan"));