|
| 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.index; |
| 11 | + |
| 12 | + |
| 13 | +import static org.assertj.core.api.Assertions.assertThat; |
| 14 | + |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.AfterEach; |
| 19 | +import org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.api.condition.DisabledIf; |
| 22 | +import org.springframework.beans.factory.annotation.Autowired; |
| 23 | +import org.springframework.data.annotation.Id; |
| 24 | +import org.springframework.data.elasticsearch.annotations.Document; |
| 25 | +import org.springframework.data.elasticsearch.annotations.DynamicTemplates; |
| 26 | +import org.springframework.data.elasticsearch.annotations.Field; |
| 27 | +import org.springframework.data.elasticsearch.annotations.FieldType; |
| 28 | +import org.springframework.data.elasticsearch.core.ElasticsearchOperations; |
| 29 | +import org.springframework.data.elasticsearch.core.IndexOperations; |
| 30 | +import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; |
| 31 | +import org.springframework.data.elasticsearch.core.query.Query; |
| 32 | +import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest; |
| 33 | +import org.springframework.data.elasticsearch.utils.IndexNameProvider; |
| 34 | +import org.springframework.lang.Nullable; |
| 35 | + |
| 36 | +@SpringIntegrationTest |
| 37 | +public abstract class DynamicTemplatesContextBaseTests { |
| 38 | + @Autowired protected ElasticsearchOperations operations; |
| 39 | + @Autowired protected IndexNameProvider indexNameProvider; |
| 40 | + |
| 41 | + @BeforeEach |
| 42 | + void setUp() { |
| 43 | + indexNameProvider.increment(); |
| 44 | + } |
| 45 | + |
| 46 | + @AfterEach |
| 47 | + void cleanup() { |
| 48 | + operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*")).delete(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void shouldCreateDynamicTemplateOne() { |
| 53 | + IndexOperations indexOperations = operations.indexOps(SampleDynamicTemplatesEntity.class); |
| 54 | + assertThat(indexOperations.createWithMapping()).isTrue(); |
| 55 | + |
| 56 | + operations.save(new SampleDynamicTemplatesEntity(Map.of("John", "Smith"))); |
| 57 | + assertThat(operations.search(Query.findAll(), SampleDynamicTemplatesEntity.class).get().count()).isEqualTo(1L); |
| 58 | + } |
| 59 | + |
| 60 | + private boolean isOSCTest() { |
| 61 | + return this instanceof DynamicTemplatesOSCIntegrationTests; |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + @DisabledIf(value = "isOSCTest", disabledReason = "https://github.com/opensearch-project/opensearch-java/issues/1513") |
| 66 | + void shouldCreateDynamicTemplateTwo() { |
| 67 | + IndexOperations indexOperations = operations.indexOps(SampleDynamicTemplatesEntityTwo.class); |
| 68 | + assertThat(indexOperations.createWithMapping()).isTrue(); |
| 69 | + |
| 70 | + operations.save(new SampleDynamicTemplatesEntityTwo(Map.of("A.B", "1"))); |
| 71 | + assertThat(operations.search(Query.findAll(), SampleDynamicTemplatesEntityTwo.class).get().count()).isEqualTo(1L); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @author Petr Kukral |
| 76 | + */ |
| 77 | + @Document(indexName = "#{@indexNameProvider.indexName()}") |
| 78 | + @DynamicTemplates(mappingPath = "/mappings/test-dynamic_templates_mappings.json") |
| 79 | + static class SampleDynamicTemplatesEntity { |
| 80 | + |
| 81 | + @Nullable |
| 82 | + @Id private String id; |
| 83 | + |
| 84 | + @Nullable |
| 85 | + @Field(type = FieldType.Object) private final Map<String, String> names; |
| 86 | + |
| 87 | + public SampleDynamicTemplatesEntity() { |
| 88 | + this(new HashMap<>()); |
| 89 | + } |
| 90 | + |
| 91 | + public SampleDynamicTemplatesEntity(final Map<String, String> names) { |
| 92 | + this.names = names; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @author Petr Kukral |
| 98 | + */ |
| 99 | + @Document(indexName = "#{@indexNameProvider.indexName()}") |
| 100 | + @DynamicTemplates(mappingPath = "/mappings/test-dynamic_templates_mappings_two.json") |
| 101 | + static class SampleDynamicTemplatesEntityTwo { |
| 102 | + |
| 103 | + @Nullable |
| 104 | + @Id private String id; |
| 105 | + |
| 106 | + @Nullable |
| 107 | + @Field(type = FieldType.Object) private final Map<String, String> test; |
| 108 | + |
| 109 | + public SampleDynamicTemplatesEntityTwo() { |
| 110 | + this(new HashMap<>()); |
| 111 | + } |
| 112 | + |
| 113 | + public SampleDynamicTemplatesEntityTwo(final Map<String, String> test) { |
| 114 | + this.test = test; |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments