|
28 | 28 | import java.lang.Long;
|
29 | 29 | import java.util.ArrayList;
|
30 | 30 | import java.util.Arrays;
|
| 31 | +import java.util.Collections; |
31 | 32 | import java.util.List;
|
32 | 33 | import java.util.Optional;
|
33 | 34 |
|
34 | 35 | import org.elasticsearch.action.ActionRequestValidationException;
|
35 | 36 | import org.junit.Before;
|
| 37 | +import org.junit.ClassRule; |
36 | 38 | import org.junit.Test;
|
37 | 39 | import org.junit.runner.RunWith;
|
38 | 40 | import org.springframework.beans.factory.annotation.Autowired;
|
|
42 | 44 | import org.springframework.data.domain.PageRequest;
|
43 | 45 | import org.springframework.data.domain.Sort;
|
44 | 46 | import org.springframework.data.domain.Sort.Order;
|
| 47 | +import org.springframework.data.elasticsearch.RestElasticsearchTestConfiguration; |
| 48 | +import org.springframework.data.elasticsearch.TestNodeResource; |
45 | 49 | import org.springframework.data.elasticsearch.annotations.Document;
|
46 | 50 | import org.springframework.data.elasticsearch.annotations.Field;
|
47 |
| -import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; |
| 51 | +import org.springframework.data.elasticsearch.core.ElasticsearchOperations; |
48 | 52 | import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
49 | 53 | import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
50 | 54 | import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
| 55 | +import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; |
51 | 56 | import org.springframework.data.elasticsearch.utils.IndexInitializer;
|
52 | 57 | import org.springframework.test.context.ContextConfiguration;
|
53 | 58 | import org.springframework.test.context.junit4.SpringRunner;
|
|
62 | 67 | * @author Murali Chevuri
|
63 | 68 | */
|
64 | 69 | @RunWith(SpringRunner.class)
|
65 |
| -@ContextConfiguration("classpath:/simple-repository-test.xml") |
| 70 | +@ContextConfiguration(classes = { SimpleElasticsearchRepositoryTests.class, RestElasticsearchTestConfiguration.class }) |
| 71 | +@EnableElasticsearchRepositories(considerNestedRepositories = true) |
66 | 72 | public class SimpleElasticsearchRepositoryTests {
|
67 | 73 |
|
| 74 | + @ClassRule public static TestNodeResource testNodeResource = new TestNodeResource(); |
| 75 | + |
68 | 76 | @Autowired private SampleElasticsearchRepository repository;
|
69 | 77 |
|
70 |
| - @Autowired private ElasticsearchTemplate elasticsearchTemplate; |
| 78 | + @Autowired private ElasticsearchOperations elasticsearchOperations; |
71 | 79 |
|
72 | 80 | @Before
|
73 | 81 | public void before() {
|
74 |
| - IndexInitializer.init(elasticsearchTemplate, SampleEntity.class); |
| 82 | + IndexInitializer.init(elasticsearchOperations, SampleEntity.class); |
75 | 83 | }
|
76 | 84 |
|
77 | 85 | @Test
|
@@ -636,6 +644,34 @@ public void shouldReturnSimilarEntities() {
|
636 | 644 | assertThat(results.getTotalElements()).isGreaterThanOrEqualTo(1L);
|
637 | 645 | }
|
638 | 646 |
|
| 647 | + @Test // DATAES-142 |
| 648 | + public void shouldIndexNotEmptyList() { |
| 649 | + // given |
| 650 | + List<SampleEntity> list = new ArrayList<>(); |
| 651 | + String documentId = randomNumeric(5); |
| 652 | + SampleEntity sampleEntity1 = new SampleEntity(); |
| 653 | + sampleEntity1.setId(documentId); |
| 654 | + sampleEntity1.setMessage("world"); |
| 655 | + list.add(sampleEntity1); |
| 656 | + |
| 657 | + String documentId2 = randomNumeric(5); |
| 658 | + SampleEntity sampleEntity2 = new SampleEntity(); |
| 659 | + sampleEntity2.setId(documentId2); |
| 660 | + sampleEntity2.setMessage("hello"); |
| 661 | + list.add(sampleEntity2); |
| 662 | + |
| 663 | + Iterable<SampleEntity> savedEntities = repository.saveAll(list); |
| 664 | + |
| 665 | + assertThat(savedEntities).containsExactlyElementsOf(list); |
| 666 | + } |
| 667 | + |
| 668 | + @Test // DATAES-142 |
| 669 | + public void shouldNotFailOnIndexingEmptyList() { |
| 670 | + Iterable<SampleEntity> savedEntities = repository.saveAll(Collections.emptyList()); |
| 671 | + |
| 672 | + assertThat(savedEntities).hasSize(0); |
| 673 | + } |
| 674 | + |
639 | 675 | private static List<SampleEntity> createSampleEntitiesWithMessage(String message, int numberOfEntities) {
|
640 | 676 | List<SampleEntity> sampleEntities = new ArrayList<>();
|
641 | 677 | for (int i = 0; i < numberOfEntities; i++) {
|
|
0 commit comments