This commit is contained in:
coach88 2015-08-10 10:51:08 +03:00
commit c41e8a6ac6
7 changed files with 496 additions and 198 deletions

View File

@ -12,8 +12,19 @@ public interface UserRepository extends MongoRepository<User, String>, QueryDslP
@Query("{ 'name' : ?0 }")
List<User> findUsersByName(String name);
@Query(value = "{'age':?0}", fields = "{ 'name' : 1}")
List<String> findUsersByAgeAndReturnNames(int age);
@Query("{ 'age' : { $gt: ?0, $lt: ?1 } }")
List<User> findUsersByAgeBetween(int ageGT, int ageLT);
@Query("{ 'name' : { $regex: ?0 } }")
List<User> findUsersByRegexpName(String regexp);
List<User> findByName(String name);
List<User> findByNameLikeOrderByAgeAsc(String name);
List<User> findByAgeBetween(int ageGT, int ageLT);
List<User> findByNameStartingWith(String regexp);
List<User> findByNameEndingWith(String regexp);
}

View File

@ -0,0 +1,179 @@
package org.baeldung.mongotemplate;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.List;
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.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
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.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MongoConfig.class)
public class DocumentQueryIntegrationTest {
@Autowired
private MongoTemplate mongoTemplate;
@Before
public void testSetup() {
mongoTemplate.createCollection(User.class);
}
@After
public void tearDown() {
mongoTemplate.dropCollection(User.class);
}
@Test
public void givenUsersExist_whenFindingUsersByName_thenUsersAreFound() {
User user = new User();
user.setName("Eric");
user.setAge(45);
mongoTemplate.insert(user);
user = new User();
user.setName("Antony");
user.setAge(55);
mongoTemplate.insert(user);
Query query = new Query();
query.addCriteria(Criteria.where("name").is("Eric"));
List<User> users = mongoTemplate.find(query, User.class);
assertThat(users.size(), is(1));
}
@Test
public void givenUsersExist_whenFindingUserWithAgeLessThan50AndGreateThan20_thenUsersAreFound() {
User user = new User();
user.setAge(20);
user.setName("Jon");
mongoTemplate.insert(user);
user = new User();
user.setAge(50);
user.setName("Jon");
mongoTemplate.insert(user);
user = new User();
user.setAge(33);
user.setName("Jim");
mongoTemplate.insert(user);
Query query = new Query();
query.addCriteria(Criteria.where("age").lt(40).gt(26));
List<User> users = mongoTemplate.find(query, User.class);
assertThat(users.size(), is(1));
}
@Test
public void givenUsersExist_whenFindingUserWithNameStartWithA_thenUsersAreFound() {
User user = new User();
user.setName("Eric");
user.setAge(45);
mongoTemplate.insert(user);
user = new User();
user.setName("Antony");
user.setAge(33);
mongoTemplate.insert(user);
user = new User();
user.setName("Alice");
user.setAge(35);
mongoTemplate.insert(user);
Query query = new Query();
query.addCriteria(Criteria.where("name").regex("^A"));
List<User> users = mongoTemplate.find(query, User.class);
assertThat(users.size(), is(2));
}
@Test
public void givenUsersExist_whenFindingUserWithNameEndWithC_thenUsersAreFound() {
User user = new User();
user.setName("Eric");
user.setAge(45);
mongoTemplate.insert(user);
user = new User();
user.setName("Antony");
user.setAge(33);
mongoTemplate.insert(user);
user = new User();
user.setName("Alice");
user.setAge(35);
mongoTemplate.insert(user);
Query query = new Query();
query.addCriteria(Criteria.where("name").regex("c$"));
List<User> users = mongoTemplate.find(query, User.class);
assertThat(users.size(), is(1));
}
@Test
public void givenUsersExist_whenFindingByPage_thenUsersAreFoundByPage() {
User user = new User();
user.setName("Eric");
user.setAge(45);
mongoTemplate.insert(user);
user = new User();
user.setName("Antony");
user.setAge(33);
mongoTemplate.insert(user);
user = new User();
user.setName("Alice");
user.setAge(35);
mongoTemplate.insert(user);
final Pageable pageableRequest = new PageRequest(0, 2);
Query query = new Query();
query.with(pageableRequest);
List<User> users = mongoTemplate.find(query, User.class);
assertThat(users.size(), is(2));
}
@Test
public void givenUsersExist_whenFindingUsersAndSortThem_thenUsersAreFoundAndSorted() {
User user = new User();
user.setName("Eric");
user.setAge(45);
mongoTemplate.insert(user);
user = new User();
user.setName("Antony");
user.setAge(33);
mongoTemplate.insert(user);
user = new User();
user.setName("Alice");
user.setAge(35);
mongoTemplate.insert(user);
Query query = new Query();
query.with(new Sort(Sort.Direction.ASC, "age"));
List<User> users = mongoTemplate.find(query, User.class);
assertThat(users.size(), is(3));
}
}

View File

@ -1,132 +0,0 @@
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() {
final User user = new User();
user.setName("Jon");
mongoTemplate.insert(user);
}
@Test
public void whenSavingNewUser_thenUserIsInserted() {
final User user = new User();
user.setName("Aaron");
mongoTemplate.save(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() {
final User user = new User();
user.setName("Benn");
mongoTemplate.insert(user);
mongoTemplate.remove(user, "user");
}
}

View File

@ -0,0 +1,27 @@
package org.baeldung.repository;
import org.baeldung.model.User;
import org.baeldung.repository.UserRepository;
import org.junit.After;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
public class BaseQueryIntegrationTest {
@Autowired
protected UserRepository userRepository;
@Autowired
protected MongoOperations mongoOps;
@Before
public void testSetup() {
mongoOps.createCollection(User.class);
}
@After
public void tearDown() {
mongoOps.dropCollection(User.class);
}
}

View File

@ -8,12 +8,8 @@ import java.util.List;
import org.baeldung.config.MongoConfig;
import org.baeldung.model.QUser;
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.MongoOperations;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ -21,60 +17,24 @@ import com.mysema.query.types.Predicate;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MongoConfig.class)
public class MongoRepositoryQueryIntegrationTest {
@Autowired
private UserRepository userRepository;
@Autowired
private MongoOperations mongoOps;
@Before
public void testSetup() {
mongoOps.createCollection(User.class);
}
@After
public void tearDown() {
mongoOps.dropCollection(User.class);
}
public class DSLQueryIntegrationTest extends BaseQueryIntegrationTest {
@Test
public void givenUsersExist_whenFindingUsersByName_thenUsersAreFound() {
public void givenUsersExist_whenFindingUsersByName_thenUserAreFound() {
User user = new User();
user.setName("Jon");
user.setName("Eric");
user.setAge(45);
mongoOps.insert(user);
user = new User();
user.setName("Jon");
user.setName("Antony");
user.setAge(55);
mongoOps.insert(user);
user = new User();
user.setName("Jim");
mongoOps.insert(user);
QUser qUser = new QUser("user");
List<User> users = userRepository.findUsersByName("Jon");
assertThat(users.size(), is(2));
}
Predicate predicate = qUser.name.eq("Eric");
List<User> users = (List<User>) userRepository.findAll(predicate);
@Test
public void givenUsersExist_whenFindingUsersByAgeAndReturningNames_thenUsersAreFoundAndReturnedNames() {
User user = new User();
user.setName("Jon");
user.setAge(20);
mongoOps.insert(user);
user = new User();
user.setName("Jon");
user.setAge(30);
mongoOps.insert(user);
user = new User();
user.setName("Jim");
user.setAge(40);
mongoOps.insert(user);
List<String> users = userRepository.findUsersByAgeAndReturnNames(30);
assertThat(users.size(), is(1));
}
@ -95,26 +55,59 @@ public class MongoRepositoryQueryIntegrationTest {
user.setName("Jim");
mongoOps.insert(user);
List<User> users = userRepository.findByAgeBetween(26, 40);
assertThat(users.size(), is(1));
}
@Test
public void givenUsersExist_whenFindingUsersAndCount_thenUserAreFound() {
User user = new User();
user.setName("Alex");
mongoOps.insert(user);
user = new User();
user.setName("Mat");
mongoOps.insert(user);
QUser qUser = new QUser("user");
Predicate predicate = qUser.name.eq("Alex");
Predicate predicate = qUser.age.between(26, 40);
List<User> users = (List<User>) userRepository.findAll(predicate);
assertThat(users.size(), is(1));
}
@Test
public void givenUsersExist_whenFindingUserWithNameStartWithA_thenUsersAreFound() {
User user = new User();
user.setName("Eric");
user.setAge(45);
mongoOps.insert(user);
user = new User();
user.setName("Antony");
user.setAge(33);
mongoOps.insert(user);
user = new User();
user.setName("Alice");
user.setAge(35);
mongoOps.insert(user);
QUser qUser = new QUser("user");
Predicate predicate = qUser.name.startsWith("A");
List<User> users = (List<User>) userRepository.findAll(predicate);
assertThat(users.size(), is(2));
}
@Test
public void givenUsersExist_whenFindingUserWithNameEndWithC_thenUsersAreFound() {
User user = new User();
user.setName("Eric");
user.setAge(45);
mongoOps.insert(user);
user = new User();
user.setName("Antony");
user.setAge(33);
mongoOps.insert(user);
user = new User();
user.setName("Alice");
user.setAge(35);
mongoOps.insert(user);
QUser qUser = new QUser("user");
Predicate predicate = qUser.name.endsWith("c");
List<User> users = (List<User>) userRepository.findAll(predicate);
assertThat(users.size(), is(1));
}
}

View File

@ -0,0 +1,98 @@
package org.baeldung.repository;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.List;
import org.baeldung.config.MongoConfig;
import org.baeldung.model.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MongoConfig.class)
public class JSONQueryIntegrationTest extends BaseQueryIntegrationTest {
@Test
public void givenUsersExist_whenFindingUsersByName_thenUsersAreFound() {
User user = new User();
user.setName("Eric");
user.setAge(45);
mongoOps.insert(user);
user = new User();
user.setName("Antony");
user.setAge(55);
mongoOps.insert(user);
List<User> users = userRepository.findUsersByName("Eric");
assertThat(users.size(), is(1));
}
@Test
public void givenUsersExist_whenFindingUsersWithAgeCreaterThanAndLessThan_thenUsersAreFound() {
User user = new User();
user.setAge(20);
user.setName("Jon");
mongoOps.insert(user);
user = new User();
user.setAge(50);
user.setName("Jon");
mongoOps.insert(user);
user = new User();
user.setAge(33);
user.setName("Jim");
mongoOps.insert(user);
List<User> users = userRepository.findUsersByAgeBetween(26, 40);
assertThat(users.size(), is(1));
}
@Test
public void givenUsersExist_whenFindingUserWithNameStartWithA_thenUsersAreFound() {
User user = new User();
user.setName("Eric");
user.setAge(45);
mongoOps.insert(user);
user = new User();
user.setName("Antony");
user.setAge(33);
mongoOps.insert(user);
user = new User();
user.setName("Alice");
user.setAge(35);
mongoOps.insert(user);
List<User> users = userRepository.findUsersByRegexpName("^A");
assertThat(users.size(), is(2));
}
@Test
public void givenUsersExist_whenFindingUserWithNameEndWithC_thenUsersAreFound() {
User user = new User();
user.setName("Eric");
user.setAge(45);
mongoOps.insert(user);
user = new User();
user.setName("Antony");
user.setAge(33);
mongoOps.insert(user);
user = new User();
user.setName("Alice");
user.setAge(35);
mongoOps.insert(user);
List<User> users = userRepository.findUsersByRegexpName("c$");
assertThat(users.size(), is(1));
}
}

View File

@ -0,0 +1,122 @@
package org.baeldung.repository;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.List;
import org.baeldung.config.MongoConfig;
import org.baeldung.model.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MongoConfig.class)
public class QueryMethodsIntegrationTest extends BaseQueryIntegrationTest {
@Test
public void givenUsersExist_whenFindingUsersByName_thenUsersAreFound() {
User user = new User();
user.setName("Eric");
user.setAge(45);
mongoOps.insert(user);
user = new User();
user.setName("Antony");
user.setAge(55);
mongoOps.insert(user);
List<User> users = userRepository.findByName("Eric");
assertThat(users.size(), is(1));
}
@Test
public void givenUsersExist_whenFindingUsersWithAgeCreaterThanAndLessThan_thenUsersAreFound() {
User user = new User();
user.setAge(20);
user.setName("Jon");
mongoOps.insert(user);
user = new User();
user.setAge(50);
user.setName("Jon");
mongoOps.insert(user);
user = new User();
user.setAge(33);
user.setName("Jim");
mongoOps.insert(user);
List<User> users = userRepository.findByAgeBetween(26, 40);
assertThat(users.size(), is(1));
}
@Test
public void givenUsersExist_whenFindingUserWithNameStartWithA_thenUsersAreFound() {
User user = new User();
user.setName("Eric");
user.setAge(45);
mongoOps.insert(user);
user = new User();
user.setName("Antony");
user.setAge(33);
mongoOps.insert(user);
user = new User();
user.setName("Alice");
user.setAge(35);
mongoOps.insert(user);
List<User> users = userRepository.findByNameStartingWith("A");
assertThat(users.size(), is(2));
}
@Test
public void givenUsersExist_whenFindingUserWithNameEndWithC_thenUsersAreFound() {
User user = new User();
user.setName("Eric");
user.setAge(45);
mongoOps.insert(user);
user = new User();
user.setName("Antony");
user.setAge(33);
mongoOps.insert(user);
user = new User();
user.setName("Alice");
user.setAge(35);
mongoOps.insert(user);
List<User> users = userRepository.findByNameEndingWith("c");
assertThat(users.size(), is(1));
}
@Test
public void givenUsersExist_whenFindingUsersAndSortThem_thenUsersAreFoundAndSorted() {
User user = new User();
user.setName("Eric");
user.setAge(45);
mongoOps.insert(user);
user = new User();
user.setName("Antony");
user.setAge(33);
mongoOps.insert(user);
user = new User();
user.setName("Alice");
user.setAge(35);
mongoOps.insert(user);
List<User> users = userRepository.findByNameLikeOrderByAgeAsc("A");
assertThat(users.size(), is(2));
}
}