Merge pull request #234 from alex-semenyuk/master

Created different types of queries are doing the same and with the same states database
This commit is contained in:
Eugen 2015-08-10 07:44:16 -07:00
commit f2d083fa3d
9 changed files with 540 additions and 234 deletions

View File

@ -1,39 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry including="**/*.java" kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry including="**/*.java" kind="src" path="src/main/java"/>
<classpathentry including="**/*.java" kind="src" path="target/generated-sources/java"/>
<classpathentry excluding="**/*.java" kind="src" path="src/main/resources"/>
<classpathentry kind="var" path="M2_REPO/javax/inject/javax.inject/1/javax.inject-1.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="var" path="M2_REPO/org/springframework/data/spring-data-mongodb/1.7.1.RELEASE/spring-data-mongodb-1.7.1.RELEASE.jar" sourcepath="M2_REPO/org/springframework/data/spring-data-mongodb/1.7.1.RELEASE/spring-data-mongodb-1.7.1.RELEASE-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-tx/4.0.9.RELEASE/spring-tx-4.0.9.RELEASE.jar"/>
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-beans/4.0.9.RELEASE/spring-beans-4.0.9.RELEASE.jar"/>
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-core/4.0.9.RELEASE/spring-core-4.0.9.RELEASE.jar"/>
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-context/4.0.9.RELEASE/spring-context-4.0.9.RELEASE.jar" sourcepath="M2_REPO/org/springframework/spring-context/4.0.9.RELEASE/spring-context-4.0.9.RELEASE-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-aop/4.0.9.RELEASE/spring-aop-4.0.9.RELEASE.jar"/>
<classpathentry kind="var" path="M2_REPO/aopalliance/aopalliance/1.0/aopalliance-1.0.jar" sourcepath="M2_REPO/aopalliance/aopalliance/1.0/aopalliance-1.0-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-expression/4.0.9.RELEASE/spring-expression-4.0.9.RELEASE.jar"/>
<classpathentry kind="var" path="M2_REPO/org/springframework/data/spring-data-commons/1.10.1.RELEASE/spring-data-commons-1.10.1.RELEASE.jar" sourcepath="M2_REPO/org/springframework/data/spring-data-commons/1.10.1.RELEASE/spring-data-commons-1.10.1.RELEASE-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/org/slf4j/slf4j-api/1.7.12/slf4j-api-1.7.12.jar"/>
<classpathentry kind="var" path="M2_REPO/org/slf4j/jcl-over-slf4j/1.7.12/jcl-over-slf4j-1.7.12.jar"/>
<classpathentry kind="var" path="M2_REPO/org/mongodb/mongo-java-driver/2.13.0/mongo-java-driver-2.13.0.jar"/>
<classpathentry kind="var" path="M2_REPO/junit/junit/4.11/junit-4.11.jar" sourcepath="M2_REPO/junit/junit/4.11/junit-4.11-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar" sourcepath="M2_REPO/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/org/hamcrest/hamcrest-library/1.3/hamcrest-library-1.3.jar"/>
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-test/4.1.7.RELEASE/spring-test-4.1.7.RELEASE.jar"/>
<classpathentry kind="var" path="M2_REPO/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar"/>
<classpathentry kind="var" path="M2_REPO/ch/qos/logback/logback-core/1.1.3/logback-core-1.1.3.jar"/>
<classpathentry kind="var" path="M2_REPO/org/slf4j/log4j-over-slf4j/1.7.12/log4j-over-slf4j-1.7.12.jar"/>
<classpathentry kind="var" path="M2_REPO/com/mysema/querydsl/querydsl-mongodb/3.6.6/querydsl-mongodb-3.6.6.jar"/>
<classpathentry kind="var" path="M2_REPO/com/mysema/querydsl/querydsl-core/3.6.6/querydsl-core-3.6.6.jar" sourcepath="M2_REPO/com/mysema/querydsl/querydsl-core/3.6.6/querydsl-core-3.6.6-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/com/google/guava/guava/18.0/guava-18.0.jar" sourcepath="M2_REPO/com/google/guava/guava/18.0/guava-18.0-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar"/>
<classpathentry kind="var" path="M2_REPO/com/mysema/commons/mysema-commons-lang/0.2.4/mysema-commons-lang-0.2.4.jar"/>
<classpathentry kind="var" path="M2_REPO/com/infradna/tool/bridge-method-annotation/1.13/bridge-method-annotation-1.13.jar"/>
<classpathentry kind="var" path="M2_REPO/com/mysema/querydsl/querydsl-apt/3.6.6/querydsl-apt-3.6.6.jar"/>
<classpathentry kind="var" path="M2_REPO/com/mysema/querydsl/querydsl-codegen/3.6.6/querydsl-codegen-3.6.6.jar"/>
<classpathentry kind="var" path="M2_REPO/com/mysema/codegen/codegen/0.6.7/codegen-0.6.7.jar"/>
<classpathentry kind="var" path="M2_REPO/org/eclipse/jdt/core/compiler/ecj/4.3.1/ecj-4.3.1.jar"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/classes" path="target/generated-sources/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -14,7 +14,7 @@
<artifactId>spring-data-mongodb</artifactId>
<version>${org.springframework.data.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
@ -46,6 +46,7 @@
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
<dependency>

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,188 @@
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;
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);
Iterator<User> 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"));
}
}

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));
}
}