From 759298a2bc45704887ecf8f11745dafaf63801e1 Mon Sep 17 00:00:00 2001 From: alex-semenyuk Date: Sun, 9 Aug 2015 18:21:14 +0200 Subject: [PATCH] Created quires are doing the same and with same db with different types --- .../baeldung/repository/UserRepository.java | 15 +- .../DocumentQueryIntegrationTest.java | 179 ++++++++++++++++++ .../MongoTemplateIntegrationTest.java | 132 ------------- .../repository/BaseQueryIntegrationTest.java | 28 +++ ...Test.java => DSLQueryIntegrationTest.java} | 126 ++++++------ .../repository/JSONQueryIntegrationTest.java | 99 ++++++++++ .../QueryMethodsIntegrationTest.java | 122 ++++++++++++ 7 files changed, 501 insertions(+), 200 deletions(-) create mode 100644 spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/DocumentQueryIntegrationTest.java delete mode 100644 spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateIntegrationTest.java create mode 100644 spring-data-mongodb/src/test/java/org/baeldung/repository/BaseQueryIntegrationTest.java rename spring-data-mongodb/src/test/java/org/baeldung/repository/{MongoRepositoryQueryIntegrationTest.java => DSLQueryIntegrationTest.java} (57%) create mode 100644 spring-data-mongodb/src/test/java/org/baeldung/repository/JSONQueryIntegrationTest.java create mode 100644 spring-data-mongodb/src/test/java/org/baeldung/repository/QueryMethodsIntegrationTest.java diff --git a/spring-data-mongodb/src/main/java/org/baeldung/repository/UserRepository.java b/spring-data-mongodb/src/main/java/org/baeldung/repository/UserRepository.java index 3af6ed4cec..229c907b90 100644 --- a/spring-data-mongodb/src/main/java/org/baeldung/repository/UserRepository.java +++ b/spring-data-mongodb/src/main/java/org/baeldung/repository/UserRepository.java @@ -12,8 +12,19 @@ public interface UserRepository extends MongoRepository, QueryDslP @Query("{ 'name' : ?0 }") List findUsersByName(String name); - @Query(value = "{'age':?0}", fields = "{ 'name' : 1}") - List findUsersByAgeAndReturnNames(int age); + @Query("{ 'age' : { $gt: ?0, $lt: ?1 } }") + List findUsersByAgeBetween(int ageGT, int ageLT); + + @Query("{ 'name' : { $regex: ?0 } }") + List findUsersByRegexpName(String regexp); + + List findByName(String name); + + List findByNameLikeOrderByAgeAsc(String name); List findByAgeBetween(int ageGT, int ageLT); + + List findByNameStartingWith(String regexp); + + List findByNameEndingWith(String regexp); } diff --git a/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/DocumentQueryIntegrationTest.java b/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/DocumentQueryIntegrationTest.java new file mode 100644 index 0000000000..8e264720d9 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/DocumentQueryIntegrationTest.java @@ -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 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 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 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 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 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 users = mongoTemplate.find(query, User.class); + assertThat(users.size(), is(3)); + } +} 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 deleted file mode 100644 index 8330b8d0b3..0000000000 --- a/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateIntegrationTest.java +++ /dev/null @@ -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"); - } - -} diff --git a/spring-data-mongodb/src/test/java/org/baeldung/repository/BaseQueryIntegrationTest.java b/spring-data-mongodb/src/test/java/org/baeldung/repository/BaseQueryIntegrationTest.java new file mode 100644 index 0000000000..9e63121472 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/baeldung/repository/BaseQueryIntegrationTest.java @@ -0,0 +1,28 @@ +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); + } +} \ No newline at end of file diff --git a/spring-data-mongodb/src/test/java/org/baeldung/repository/MongoRepositoryQueryIntegrationTest.java b/spring-data-mongodb/src/test/java/org/baeldung/repository/DSLQueryIntegrationTest.java similarity index 57% rename from spring-data-mongodb/src/test/java/org/baeldung/repository/MongoRepositoryQueryIntegrationTest.java rename to spring-data-mongodb/src/test/java/org/baeldung/repository/DSLQueryIntegrationTest.java index b58981a840..48e53dfdf1 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/repository/MongoRepositoryQueryIntegrationTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/repository/DSLQueryIntegrationTest.java @@ -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,63 +17,27 @@ 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 users = userRepository.findUsersByName("Jon"); - assertThat(users.size(), is(2)); - } + Predicate predicate = qUser.name.eq("Eric"); + List users = (List) 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 users = userRepository.findUsersByAgeAndReturnNames(30); assertThat(users.size(), is(1)); } - + @Test public void givenUsersExist_whenFindingUsersWithAgeCreaterThanAndLessThan_thenUsersAreFound() { User user = new User(); @@ -95,26 +55,60 @@ public class MongoRepositoryQueryIntegrationTest { user.setName("Jim"); mongoOps.insert(user); - List 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 users = (List) 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 users = (List) 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 users = (List) userRepository.findAll(predicate); + + assertThat(users.size(), is(1)); + } } + diff --git a/spring-data-mongodb/src/test/java/org/baeldung/repository/JSONQueryIntegrationTest.java b/spring-data-mongodb/src/test/java/org/baeldung/repository/JSONQueryIntegrationTest.java new file mode 100644 index 0000000000..a899e76659 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/baeldung/repository/JSONQueryIntegrationTest.java @@ -0,0 +1,99 @@ +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 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 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 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 users = userRepository.findUsersByRegexpName("c$"); + + assertThat(users.size(), is(1)); + } +} + diff --git a/spring-data-mongodb/src/test/java/org/baeldung/repository/QueryMethodsIntegrationTest.java b/spring-data-mongodb/src/test/java/org/baeldung/repository/QueryMethodsIntegrationTest.java new file mode 100644 index 0000000000..5bbf779821 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/baeldung/repository/QueryMethodsIntegrationTest.java @@ -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 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 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 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 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 users = userRepository.findByNameLikeOrderByAgeAsc("A"); + + assertThat(users.size(), is(2)); + } +} +