minor cleanup
This commit is contained in:
parent
d09c981353
commit
5de9fb3d70
|
@ -15,7 +15,7 @@ public class User {
|
|||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
public void setId(final String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,7 @@ public class User {
|
|||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
@ -31,7 +31,7 @@ public class User {
|
|||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
public void setAge(final Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ public class UserRepository {
|
|||
@Autowired
|
||||
private MongoTemplate mongoTemplate;
|
||||
|
||||
public void insertUser(User user) {
|
||||
public void insertUser(final User user) {
|
||||
mongoTemplate.insert(user, "user");
|
||||
}
|
||||
|
||||
|
@ -26,42 +26,42 @@ public class UserRepository {
|
|||
return mongoTemplate.findAll(User.class, "user");
|
||||
}
|
||||
|
||||
public void removeUser(User user) {
|
||||
public void removeUser(final User user) {
|
||||
mongoTemplate.remove(user, "user");
|
||||
}
|
||||
|
||||
public void saveUser(User user) {
|
||||
public void saveUser(final User user) {
|
||||
mongoTemplate.save(user, "user");
|
||||
}
|
||||
|
||||
public User findAndModifyUser(String name, String newName) {
|
||||
Query query = new Query();
|
||||
public User findAndModifyUser(final String name, final String newName) {
|
||||
final Query query = new Query();
|
||||
query.addCriteria(Criteria.where("name").is(name));
|
||||
Update update = new Update();
|
||||
final Update update = new Update();
|
||||
update.set("name", newName);
|
||||
return mongoTemplate.findAndModify(query, update, User.class);
|
||||
}
|
||||
|
||||
public void updateFirstUser(String name, String newName) {
|
||||
Query query = new Query();
|
||||
public void updateFirstUser(final String name, final String newName) {
|
||||
final Query query = new Query();
|
||||
query.addCriteria(Criteria.where("name").is(name));
|
||||
Update update = new Update();
|
||||
final Update update = new Update();
|
||||
update.set("name", newName);
|
||||
mongoTemplate.updateFirst(query, update, User.class);
|
||||
}
|
||||
|
||||
public WriteResult upsertUser(String name, String newName) {
|
||||
Query query = new Query();
|
||||
public WriteResult upsertUser(final String name, final String newName) {
|
||||
final Query query = new Query();
|
||||
query.addCriteria(Criteria.where("name").is(name));
|
||||
Update update = new Update();
|
||||
final Update update = new Update();
|
||||
update.set("name", newName);
|
||||
return mongoTemplate.upsert(query, update, User.class);
|
||||
}
|
||||
|
||||
public void updateMultiUser(String name, String newName) {
|
||||
Query query = new Query();
|
||||
public void updateMultiUser(final String name, final String newName) {
|
||||
final Query query = new Query();
|
||||
query.addCriteria(Criteria.where("name").is(name));
|
||||
Update update = new Update();
|
||||
final Update update = new Update();
|
||||
update.set("name", newName);
|
||||
mongoTemplate.updateMulti(query, update, User.class);
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ import static org.junit.Assert.assertThat;
|
|||
|
||||
import org.baeldung.config.MongoConfig;
|
||||
import org.baeldung.model.User;
|
||||
import org.baeldung.repository.UserRepository;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -18,8 +17,9 @@ import org.springframework.test.context.ContextConfiguration;
|
|||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes={MongoConfig.class, UserRepository.class})
|
||||
@ContextConfiguration(classes = { MongoConfig.class, UserRepository.class })
|
||||
public class UserRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
|
@ -36,9 +36,11 @@ public class UserRepositoryIntegrationTest {
|
|||
mongoOps.dropCollection(User.class);
|
||||
}
|
||||
|
||||
// tests
|
||||
|
||||
@Test
|
||||
public void testInsert() {
|
||||
User user = new User();
|
||||
final User user = new User();
|
||||
user.setName("Jon");
|
||||
userRepository.insertUser(user);
|
||||
|
||||
|
@ -52,7 +54,7 @@ public class UserRepositoryIntegrationTest {
|
|||
userRepository.insertUser(user);
|
||||
|
||||
user = mongoOps.findOne(Query.query(Criteria.where("name").is("Jack")), User.class);
|
||||
String id = user.getId();
|
||||
final String id = user.getId();
|
||||
|
||||
user.setName("Jim");
|
||||
userRepository.saveUser(user);
|
||||
|
@ -107,7 +109,7 @@ public class UserRepositoryIntegrationTest {
|
|||
|
||||
@Test
|
||||
public void testUpsert() {
|
||||
User user = new User();
|
||||
final User user = new User();
|
||||
user.setName("Markus");
|
||||
mongoOps.insert(user);
|
||||
|
||||
|
@ -119,7 +121,7 @@ public class UserRepositoryIntegrationTest {
|
|||
|
||||
@Test
|
||||
public void testRemove() {
|
||||
User user = new User();
|
||||
final User user = new User();
|
||||
user.setName("Benn");
|
||||
mongoOps.insert(user);
|
||||
|
||||
|
|
Loading…
Reference in New Issue