package com.baeldung.mongotemplate; import com.baeldung.config.MongoConfig; import com.baeldung.model.EmailAddress; import com.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.domain.Sort.Direction; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.index.Index; import org.springframework.data.mongodb.core.index.IndexInfo; 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; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = MongoConfig.class) public class MongoTemplateQueryLiveTest { @Autowired private MongoTemplate mongoTemplate; @Before public void testSetup() { if (!mongoTemplate.collectionExists(User.class)) { mongoTemplate.createCollection(User.class); } } @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 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_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)); } @Test public void givenUserExistsWithIndexAddedFromMapping_whenCheckingIndex_thenIndexIsExisted() { final User user = new User(); user.setName("Brendan"); EmailAddress emailAddress = new EmailAddress(); emailAddress.setValue("a@gmail.com"); user.setEmailAddress(emailAddress); mongoTemplate.insert(user); List indexInfos = mongoTemplate.indexOps("user").getIndexInfo(); assertThat(indexInfos.size(), is(1)); } @Test public void whenSavingUserWithEmailAddress_thenUserandEmailAddressSaved() { final User user = new User(); user.setName("Brendan"); 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() { final User user = new User(); user.setName("Brendan"); mongoTemplate.indexOps(User.class).ensureIndex(new Index().on("name", Direction.ASC)); mongoTemplate.insert(user); List indexInfos = mongoTemplate.indexOps("user").getIndexInfo(); assertThat(indexInfos.size(), is(2)); } @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"); user.setYearOfBirth(1985); mongoTemplate.insert(user); assertThat(mongoTemplate.findOne(Query.query(Criteria.where("name").is("Alex")), User.class).getYearOfBirth(), is(nullValue())); } }