|
| 1 | +package org.springframework.data.elasticsearch.core; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.BeforeEach; |
| 4 | +import org.junit.jupiter.api.Order; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.data.annotation.Id; |
| 8 | +import org.springframework.data.elasticsearch.annotations.Document; |
| 9 | +import org.springframework.data.elasticsearch.annotations.Field; |
| 10 | +import org.springframework.data.elasticsearch.annotations.FieldType; |
| 11 | +import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; |
| 12 | +import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest; |
| 13 | +import org.springframework.data.elasticsearch.utils.IndexNameProvider; |
| 14 | +import org.springframework.lang.Nullable; |
| 15 | +import reactor.test.StepVerifier; |
| 16 | + |
| 17 | +import static org.springframework.data.elasticsearch.core.IndexOperationsAdapter.*; |
| 18 | +import static org.springframework.data.elasticsearch.utils.IdGenerator.*; |
| 19 | + |
| 20 | +/** |
| 21 | + * Integration tests for the count API. |
| 22 | + * |
| 23 | + * @author maryantocinn |
| 24 | + * @since 5.4 |
| 25 | + */ |
| 26 | +@SpringIntegrationTest |
| 27 | +public abstract class ReactiveCountIntegrationTests { |
| 28 | + |
| 29 | + @Autowired private ReactiveElasticsearchOperations operations; |
| 30 | + @Autowired private IndexNameProvider indexNameProvider; |
| 31 | + |
| 32 | + @BeforeEach |
| 33 | + public void beforeEach() { |
| 34 | + |
| 35 | + indexNameProvider.increment(); |
| 36 | + blocking(operations.indexOps(ReactiveReindexIntegrationTests.Entity.class)).createWithMapping(); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + @Order(java.lang.Integer.MAX_VALUE) |
| 41 | + void cleanup() { |
| 42 | + blocking(operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + '*'))).delete(); |
| 43 | + } |
| 44 | + |
| 45 | + @Test // GH-2749 |
| 46 | + void shouldCount() { |
| 47 | + String indexName = indexNameProvider.indexName(); |
| 48 | + |
| 49 | + Entity entity = new Entity(); |
| 50 | + entity.setId(nextIdAsString()); |
| 51 | + entity.setMessage("abc"); |
| 52 | + |
| 53 | + operations.save(entity) // |
| 54 | + .as(StepVerifier::create) // |
| 55 | + .expectNextCount(1) // |
| 56 | + .verifyComplete(); |
| 57 | + |
| 58 | + Entity entity2 = new Entity(); |
| 59 | + entity2.setId(nextIdAsString()); |
| 60 | + entity2.setMessage("def"); |
| 61 | + |
| 62 | + operations.save(entity2) // |
| 63 | + .as(StepVerifier::create) // |
| 64 | + .expectNextCount(1) // |
| 65 | + .verifyComplete(); |
| 66 | + |
| 67 | + operations.count(operations.matchAllQuery(), Entity.class, IndexCoordinates.of(indexName)) // |
| 68 | + .as(StepVerifier::create) // |
| 69 | + .expectNext(2L) // |
| 70 | + .verifyComplete(); |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + @Document(indexName = "#{@indexNameProvider.indexName()}") |
| 75 | + static class Entity { |
| 76 | + @Nullable |
| 77 | + @Id |
| 78 | + private String id; |
| 79 | + @Nullable |
| 80 | + @Field(type = FieldType.Text) private String message; |
| 81 | + |
| 82 | + @Nullable |
| 83 | + public String getId() { |
| 84 | + return id; |
| 85 | + } |
| 86 | + |
| 87 | + public void setId(@Nullable String id) { |
| 88 | + this.id = id; |
| 89 | + } |
| 90 | + |
| 91 | + @Nullable |
| 92 | + public String getMessage() { |
| 93 | + return message; |
| 94 | + } |
| 95 | + |
| 96 | + public void setMessage(@Nullable String message) { |
| 97 | + this.message = message; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments