196 lines
6.2 KiB
Java
Raw Normal View History

2018-06-02 23:14:27 +03:00
package com.baeldung.mongotemplate;
2015-08-07 10:46:27 +03:00
2018-06-02 23:14:27 +03:00
import com.baeldung.config.MongoConfig;
import com.baeldung.model.EmailAddress;
import com.baeldung.model.User;
2015-08-07 10:46:27 +03:00
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.domain.Sort.Direction;
2015-08-07 10:46:27 +03:00
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.index.Index;
import org.springframework.data.mongodb.core.index.IndexInfo;
2015-08-07 10:46:27 +03:00
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;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
2015-08-07 10:46:27 +03:00
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MongoConfig.class)
2017-01-10 16:13:20 +02:00
public class MongoTemplateQueryLiveTest {
2015-08-07 10:46:27 +03:00
@Autowired
private MongoTemplate mongoTemplate;
@Before
public void testSetup() {
if (!mongoTemplate.collectionExists(User.class)) {
mongoTemplate.createCollection(User.class);
}
2015-08-07 10:46:27 +03:00
}
@After
public void tearDown() {
mongoTemplate.dropCollection(User.class);
}
@Test
public void givenUsersExist_whenFindingUserWithAgeLessThan50AndGreateThan20_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("age").lt(50).gt(20));
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_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));
}
@Test
public void givenUserExistsWithIndexAddedFromMapping_whenCheckingIndex_thenIndexIsExisted() {
2015-08-15 20:30:19 +02:00
final User user = new User();
user.setName("Brendan");
EmailAddress emailAddress = new EmailAddress();
emailAddress.setValue("a@gmail.com");
user.setEmailAddress(emailAddress);
mongoTemplate.insert(user);
List<IndexInfo> indexInfos = mongoTemplate.indexOps("user").getIndexInfo();
assertThat(indexInfos.size(), is(1));
}
@Test
public void whenSavingUserWithEmailAddress_thenUserandEmailAddressSaved() {
2015-08-15 20:30:19 +02:00
final User user = new User();
user.setName("Brendan");
2015-08-16 14:10:05 +02:00
final EmailAddress emailAddress = new EmailAddress();
emailAddress.setValue("b@gmail.com");
user.setEmailAddress(emailAddress);
mongoTemplate.insert(user);
assertThat(mongoTemplate.findOne(Query.query(Criteria.where("name").is("Brendan")), User.class).getEmailAddress().getValue(), is("b@gmail.com"));
}
@Test
public void givenUserExistsWithIndexAddedFromCode_whenCheckingIndex_thenIndexIsExisted() {
2015-08-15 20:30:19 +02:00
final User user = new User();
user.setName("Brendan");
mongoTemplate.indexOps(User.class).ensureIndex(new Index().on("name", Direction.ASC));
mongoTemplate.insert(user);
List<IndexInfo> indexInfos = mongoTemplate.indexOps("user").getIndexInfo();
assertThat(indexInfos.size(), is(2));
}
2015-08-16 14:10:05 +02:00
@Test
public void whenSavingUserWithoutSettingAge_thenAgeIsSetByDefault() {
final User user = new User();
user.setName("Alex");
mongoTemplate.insert(user);
assertThat(mongoTemplate.findOne(Query.query(Criteria.where("name").is("Alex")), User.class).getAge(), is(0));
}
@Test
public void whenSavingUser_thenYearOfBirthIsCalculated() {
final User user = new User();
user.setName("Alex");
2015-08-22 08:43:06 +02:00
user.setYearOfBirth(1985);
2015-08-16 14:10:05 +02:00
mongoTemplate.insert(user);
2015-08-22 12:22:13 +02:00
assertThat(mongoTemplate.findOne(Query.query(Criteria.where("name").is("Alex")), User.class).getYearOfBirth(), is(nullValue()));
2015-08-16 14:10:05 +02:00
}
2015-08-07 10:46:27 +03:00
}