Merge pull request #236 from alex-semenyuk/master
Introduction to Spring Data MongoDB (continue)
This commit is contained in:
@@ -7,6 +7,7 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.config.MongoConfig;
|
||||
import org.baeldung.model.EmailAddress;
|
||||
import org.baeldung.model.User;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
@@ -31,11 +32,14 @@ public class DocumentQueryIntegrationTest {
|
||||
|
||||
@Before
|
||||
public void testSetup() {
|
||||
mongoTemplate.createCollection(User.class);
|
||||
if (!mongoTemplate.collectionExists(User.class)) {
|
||||
mongoTemplate.createCollection(User.class);
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
mongoTemplate.dropCollection(EmailAddress.class);
|
||||
mongoTemplate.dropCollection(User.class);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import static org.junit.Assert.assertThat;
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.config.MongoConfig;
|
||||
import org.baeldung.model.EmailAddress;
|
||||
import org.baeldung.model.User;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
@@ -15,7 +16,10 @@ 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;
|
||||
@@ -30,7 +34,9 @@ public class MongoTemplateQueryIntegrationTest {
|
||||
|
||||
@Before
|
||||
public void testSetup() {
|
||||
mongoTemplate.createCollection(User.class);
|
||||
if (!mongoTemplate.collectionExists(User.class)) {
|
||||
mongoTemplate.createCollection(User.class);
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -128,4 +134,42 @@ public class MongoTemplateQueryIntegrationTest {
|
||||
List<User> 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<IndexInfo> indexInfos = mongoTemplate.indexOps("user").getIndexInfo();
|
||||
|
||||
assertThat(indexInfos.size(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSavingUserWithEmailAddress_thenUserandEmailAddressSaved() {
|
||||
final User user = new User();
|
||||
user.setName("Brendan");
|
||||
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<IndexInfo> indexInfos = mongoTemplate.indexOps("user").getIndexInfo();
|
||||
|
||||
assertThat(indexInfos.size(), is(2));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,9 @@ public class BaseQueryIntegrationTest {
|
||||
|
||||
@Before
|
||||
public void testSetup() {
|
||||
mongoOps.createCollection(User.class);
|
||||
if (!mongoOps.collectionExists(User.class)) {
|
||||
mongoOps.createCollection(User.class);
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
|
||||
@@ -34,7 +34,9 @@ public class UserRepositoryIntegrationTest {
|
||||
|
||||
@Before
|
||||
public void testSetup() {
|
||||
mongoOps.createCollection(User.class);
|
||||
if (!mongoOps.collectionExists(User.class)) {
|
||||
mongoOps.createCollection(User.class);
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -67,12 +69,11 @@ public class UserRepositoryIntegrationTest {
|
||||
mongoOps.insert(user);
|
||||
|
||||
user = mongoOps.findOne(Query.query(Criteria.where("name").is("Jack")), User.class);
|
||||
final String id = user.getId();
|
||||
|
||||
user.setName("Jim");
|
||||
userRepository.save(user);
|
||||
|
||||
assertThat(mongoOps.findOne(Query.query(Criteria.where("id").is(id)), User.class).getName(), is("Jim"));
|
||||
assertThat(mongoOps.findAll(User.class).size(), is(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -145,4 +146,5 @@ public class UserRepositoryIntegrationTest {
|
||||
assertThat(users.size(), is(1));
|
||||
assertThat(page.getTotalPages(), is(2));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user