|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * The OpenSearch Contributors require contributions made to |
| 6 | + * this file be licensed under the Apache-2.0 license or a |
| 7 | + * compatible open source license. |
| 8 | + */ |
| 9 | + |
| 10 | +package org.opensearch.data.client.core.indices; |
| 11 | + |
| 12 | +import static org.assertj.core.api.Assertions.assertThat; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | +import org.assertj.core.api.SoftAssertions; |
| 16 | +import org.junit.jupiter.api.AfterEach; |
| 17 | +import org.junit.jupiter.api.BeforeEach; |
| 18 | +import org.junit.jupiter.api.DisplayName; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.opensearch.data.client.junit.jupiter.OpenSearchTemplateConfiguration; |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.context.annotation.Bean; |
| 23 | +import org.springframework.context.annotation.Configuration; |
| 24 | +import org.springframework.context.annotation.Import; |
| 25 | +import org.springframework.data.annotation.Id; |
| 26 | +import org.springframework.data.elasticsearch.annotations.Document; |
| 27 | +import org.springframework.data.elasticsearch.core.ElasticsearchOperations; |
| 28 | +import org.springframework.data.elasticsearch.core.IndexOperations; |
| 29 | +import org.springframework.data.elasticsearch.core.index.PutIndexTemplateRequest; |
| 30 | +import org.springframework.data.elasticsearch.core.index.TemplateResponse; |
| 31 | +import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest; |
| 32 | +import org.springframework.data.elasticsearch.utils.IndexNameProvider; |
| 33 | +import org.springframework.lang.Nullable; |
| 34 | +import org.springframework.test.context.ContextConfiguration; |
| 35 | + |
| 36 | +@SpringIntegrationTest |
| 37 | +@ContextConfiguration(classes = {IndexTemplateOperationsOSCIntegrationTests.Config.class}) |
| 38 | +public class IndexTemplateOperationsOSCIntegrationTests { |
| 39 | + |
| 40 | + @Configuration |
| 41 | + @Import({OpenSearchTemplateConfiguration.class}) |
| 42 | + static class Config { |
| 43 | + @Bean |
| 44 | + IndexNameProvider indexNameProvider() { |
| 45 | + return new IndexNameProvider("indextemplateoperations-os"); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Autowired private ElasticsearchOperations operations; |
| 50 | + private IndexOperations indexOperations; |
| 51 | + |
| 52 | + @Autowired protected IndexNameProvider indexNameProvider; |
| 53 | + private String indexTemplateName; |
| 54 | + |
| 55 | + @BeforeEach |
| 56 | + void setUp() { |
| 57 | + indexNameProvider.increment(); |
| 58 | + indexTemplateName = indexNameProvider.indexName() + "-template"; |
| 59 | + |
| 60 | + indexOperations = operations.indexOps(Entity.class); |
| 61 | + } |
| 62 | + |
| 63 | + @AfterEach |
| 64 | + void cleanup() { |
| 65 | + indexOperations.deleteIndexTemplate(indexTemplateName); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + @DisplayName("should return TemplateResponseData with getIndexTemplate method") |
| 70 | + void shouldReturnIndexTemplate() { |
| 71 | + var mapping = indexOperations.createMapping(); |
| 72 | + |
| 73 | + PutIndexTemplateRequest putIndexTemplateRequest = PutIndexTemplateRequest.builder() |
| 74 | + .withName(indexTemplateName) |
| 75 | + .withIndexPatterns(indexNameProvider.getPrefix() + "-*") |
| 76 | + .withMapping(mapping) |
| 77 | + .build(); |
| 78 | + indexOperations.putIndexTemplate(putIndexTemplateRequest); |
| 79 | + |
| 80 | + List<TemplateResponse> templateResponses = indexOperations.getIndexTemplate(indexTemplateName); |
| 81 | + assertThat(templateResponses).hasSize(1); |
| 82 | + |
| 83 | + TemplateResponse templateResponse = templateResponses.get(0); |
| 84 | + |
| 85 | + SoftAssertions softly = new SoftAssertions(); |
| 86 | + softly.assertThat(templateResponse).isNotNull(); |
| 87 | + softly.assertThat(templateResponse.name()).isEqualTo(indexTemplateName); |
| 88 | + softly.assertThat(templateResponse.templateData()).isNotNull(); |
| 89 | + softly.assertThat(templateResponse.templateData().mapping()).isEqualTo(mapping); |
| 90 | + } |
| 91 | + |
| 92 | + @Document(indexName = "#{@indexNameProvider.indexName()}") |
| 93 | + protected static class Entity { |
| 94 | + @Nullable |
| 95 | + private @Id String id; |
| 96 | + |
| 97 | + @Nullable |
| 98 | + public String getId() { |
| 99 | + return id; |
| 100 | + } |
| 101 | + |
| 102 | + public void setId(@Nullable String id) { |
| 103 | + this.id = id; |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments