|
6 | 6 | import java.util.List; |
7 | 7 |
|
8 | 8 | import org.baeldung.config.MongoConfig; |
| 9 | +import org.baeldung.model.EmailAddress; |
9 | 10 | import org.baeldung.model.User; |
10 | 11 | import org.junit.After; |
11 | 12 | import org.junit.Before; |
|
16 | 17 | import org.springframework.data.domain.PageRequest; |
17 | 18 | import org.springframework.data.domain.Pageable; |
18 | 19 | import org.springframework.data.domain.Sort; |
| 20 | +import org.springframework.data.domain.Sort.Direction; |
19 | 21 | import org.springframework.data.mongodb.core.MongoOperations; |
| 22 | +import org.springframework.data.mongodb.core.index.Index; |
| 23 | +import org.springframework.data.mongodb.core.index.IndexInfo; |
20 | 24 | import org.springframework.data.mongodb.core.query.Criteria; |
21 | 25 | import org.springframework.data.mongodb.core.query.Query; |
22 | 26 | import org.springframework.test.context.ContextConfiguration; |
@@ -145,4 +149,30 @@ public void givenUsersExist_whenFindingAllUsersWithPagination_thenUsersAreFoundA |
145 | 149 | assertThat(users.size(), is(1)); |
146 | 150 | assertThat(page.getTotalPages(), is(2)); |
147 | 151 | } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void givenUserExistsWithIndexAddedFromMapping_whenCheckingIndex_thenIndexIsExisted() { |
| 155 | + User user = new User(); |
| 156 | + user.setName("Brendan"); |
| 157 | + EmailAddress emailAddress = new EmailAddress("a@gmail.com"); |
| 158 | + mongoOps.insert(emailAddress); |
| 159 | + user.setEmailAddress(emailAddress); |
| 160 | + mongoOps.insert(user); |
| 161 | + |
| 162 | + List<IndexInfo> indexInfos = mongoOps.indexOps("user").getIndexInfo(); |
| 163 | + |
| 164 | + assertThat(indexInfos.size(), is(1)); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + public void givenUserExistsWithIndexAddedFromCode_whenCheckingIndex_thenIndexIsExisted() { |
| 169 | + User user = new User(); |
| 170 | + user.setName("Brendan"); |
| 171 | + mongoOps.indexOps(User.class).ensureIndex(new Index().on("name", Direction.ASC)); |
| 172 | + mongoOps.insert(user); |
| 173 | + |
| 174 | + List<IndexInfo> indexInfos = mongoOps.indexOps("user").getIndexInfo(); |
| 175 | + |
| 176 | + assertThat(indexInfos.size(), is(2)); |
| 177 | + } |
148 | 178 | } |
0 commit comments