From 759298a2bc45704887ecf8f11745dafaf63801e1 Mon Sep 17 00:00:00 2001 From: alex-semenyuk Date: Sun, 9 Aug 2015 18:21:14 +0200 Subject: [PATCH 1/5] 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)); + } +} + From 02ba4a560538e28424af85aacf65cf9210046873 Mon Sep 17 00:00:00 2001 From: alex-semenyuk Date: Sun, 9 Aug 2015 19:13:51 +0200 Subject: [PATCH 2/5] Formatting cleanup --- .../baeldung/repository/UserRepository.java | 2 +- .../DocumentQueryIntegrationTest.java | 4 +- .../repository/BaseQueryIntegrationTest.java | 1 - .../repository/DSLQueryIntegrationTest.java | 11 +- .../repository/JSONQueryIntegrationTest.java | 117 +++++++++--------- 5 files changed, 66 insertions(+), 69 deletions(-) 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 229c907b90..72d9a4dba0 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 @@ -25,6 +25,6 @@ public interface UserRepository extends MongoRepository, QueryDslP 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 index 8e264720d9..a4b09e1c70 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/DocumentQueryIntegrationTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/DocumentQueryIntegrationTest.java @@ -37,7 +37,7 @@ public class DocumentQueryIntegrationTest { public void tearDown() { mongoTemplate.dropCollection(User.class); } - + @Test public void givenUsersExist_whenFindingUsersByName_thenUsersAreFound() { User user = new User(); @@ -103,7 +103,7 @@ public class DocumentQueryIntegrationTest { List users = mongoTemplate.find(query, User.class); assertThat(users.size(), is(2)); } - + @Test public void givenUsersExist_whenFindingUserWithNameEndWithC_thenUsersAreFound() { User user = new 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 index 9e63121472..459716c7ca 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/repository/BaseQueryIntegrationTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/repository/BaseQueryIntegrationTest.java @@ -7,7 +7,6 @@ import org.junit.Before; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoOperations; - public class BaseQueryIntegrationTest { @Autowired diff --git a/spring-data-mongodb/src/test/java/org/baeldung/repository/DSLQueryIntegrationTest.java b/spring-data-mongodb/src/test/java/org/baeldung/repository/DSLQueryIntegrationTest.java index 48e53dfdf1..ca8d46a97a 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/repository/DSLQueryIntegrationTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/repository/DSLQueryIntegrationTest.java @@ -18,7 +18,7 @@ import com.mysema.query.types.Predicate; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = MongoConfig.class) public class DSLQueryIntegrationTest extends BaseQueryIntegrationTest { - + @Test public void givenUsersExist_whenFindingUsersByName_thenUserAreFound() { User user = new User(); @@ -37,7 +37,7 @@ public class DSLQueryIntegrationTest extends BaseQueryIntegrationTest { assertThat(users.size(), is(1)); } - + @Test public void givenUsersExist_whenFindingUsersWithAgeCreaterThanAndLessThan_thenUsersAreFound() { User user = new User(); @@ -62,7 +62,7 @@ public class DSLQueryIntegrationTest extends BaseQueryIntegrationTest { assertThat(users.size(), is(1)); } - + @Test public void givenUsersExist_whenFindingUserWithNameStartWithA_thenUsersAreFound() { User user = new User(); @@ -86,7 +86,7 @@ public class DSLQueryIntegrationTest extends BaseQueryIntegrationTest { List users = (List) userRepository.findAll(predicate); assertThat(users.size(), is(2)); } - + @Test public void givenUsersExist_whenFindingUserWithNameEndWithC_thenUsersAreFound() { User user = new User(); @@ -107,8 +107,7 @@ public class DSLQueryIntegrationTest extends BaseQueryIntegrationTest { 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 index a899e76659..ed88429792 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/repository/JSONQueryIntegrationTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/repository/JSONQueryIntegrationTest.java @@ -15,8 +15,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = MongoConfig.class) public class JSONQueryIntegrationTest extends BaseQueryIntegrationTest { - - @Test + + @Test public void givenUsersExist_whenFindingUsersByName_thenUsersAreFound() { User user = new User(); user.setName("Eric"); @@ -30,70 +30,69 @@ public class JSONQueryIntegrationTest extends BaseQueryIntegrationTest { 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); + @Test + public void givenUsersExist_whenFindingUsersWithAgeCreaterThanAndLessThan_thenUsersAreFound() { + User user = new User(); + user.setAge(20); + user.setName("Jon"); + mongoOps.insert(user); - user = new User(); - user.setAge(33); - user.setName("Jim"); - mongoOps.insert(user); + user = new User(); + user.setAge(50); + user.setName("Jon"); + 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.setAge(33); + user.setName("Jim"); + mongoOps.insert(user); - user = new User(); - user.setName("Antony"); - user.setAge(33); - mongoOps.insert(user); + List users = userRepository.findUsersByAgeBetween(26, 40); + assertThat(users.size(), is(1)); + } - user = new User(); - user.setName("Alice"); - user.setAge(35); - mongoOps.insert(user); + @Test + public void givenUsersExist_whenFindingUserWithNameStartWithA_thenUsersAreFound() { + User user = new User(); + user.setName("Eric"); + user.setAge(45); + 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("Antony"); - user.setAge(33); - mongoOps.insert(user); + user = new User(); + user.setName("Alice"); + user.setAge(35); + mongoOps.insert(user); - user = new User(); - user.setName("Alice"); - user.setAge(35); - mongoOps.insert(user); + List users = userRepository.findUsersByRegexpName("^A"); - List users = userRepository.findUsersByRegexpName("c$"); - - assertThat(users.size(), is(1)); - } + 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)); + } } - From 18c69a0b1375798515d33ca7350fc24777ac083e Mon Sep 17 00:00:00 2001 From: coach88 Date: Mon, 10 Aug 2015 10:50:02 +0300 Subject: [PATCH 3/5] Cleanup classpath --- spring-data-mongodb/.classpath | 68 +++++++++++++++++----------------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/spring-data-mongodb/.classpath b/spring-data-mongodb/.classpath index b049bd7d47..a86150fe21 100644 --- a/spring-data-mongodb/.classpath +++ b/spring-data-mongodb/.classpath @@ -1,39 +1,37 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 8481866d6a538e66c9e643043e50db785d2ed9ef Mon Sep 17 00:00:00 2001 From: coach88 Date: Mon, 10 Aug 2015 11:41:23 +0300 Subject: [PATCH 4/5] Cleanup for test --- .../mongotemplate/DocumentQueryIntegrationTest.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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 index a4b09e1c70..83867dd423 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/DocumentQueryIntegrationTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/DocumentQueryIntegrationTest.java @@ -3,6 +3,7 @@ package org.baeldung.mongotemplate; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; +import java.util.Iterator; import java.util.List; import org.baeldung.config.MongoConfig; @@ -101,6 +102,7 @@ public class DocumentQueryIntegrationTest { query.addCriteria(Criteria.where("name").regex("^A")); List users = mongoTemplate.find(query, User.class); + assertThat(users.size(), is(2)); } @@ -125,6 +127,7 @@ public class DocumentQueryIntegrationTest { query.addCriteria(Criteria.where("name").regex("c$")); List users = mongoTemplate.find(query, User.class); + assertThat(users.size(), is(1)); } @@ -150,6 +153,7 @@ public class DocumentQueryIntegrationTest { query.with(pageableRequest); List users = mongoTemplate.find(query, User.class); + assertThat(users.size(), is(2)); } @@ -174,6 +178,11 @@ public class DocumentQueryIntegrationTest { query.with(new Sort(Sort.Direction.ASC, "age")); List users = mongoTemplate.find(query, User.class); - assertThat(users.size(), is(3)); + + Iterator iter = users.iterator(); + assertThat(users.size(), is(3)); + assertThat(iter.next().getName(), is("Antony")); + assertThat(iter.next().getName(), is("Alice")); + assertThat(iter.next().getName(), is("Eric")); } } From 4e02612c1ca697b89ccec31db0ed1839dbef3912 Mon Sep 17 00:00:00 2001 From: alex-semenyuk Date: Mon, 10 Aug 2015 16:31:21 +0200 Subject: [PATCH 5/5] Formatting cleanup --- spring-data-mongodb/pom.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index 34ec7f0949..5b7c83482a 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -14,7 +14,7 @@ spring-data-mongodb ${org.springframework.data.version} - + org.springframework spring-core @@ -46,6 +46,7 @@ org.springframework spring-test ${org.springframework.version} + test