Skip to content

Commit 89c9965

Browse files
committed
Add test cases for dynamic_templates mappings
Signed-off-by: Andriy Redko <drreta@gmail.com>
1 parent b5381a5 commit 89c9965

File tree

5 files changed

+154
-14
lines changed

5 files changed

+154
-14
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 java.util.HashMap;
14+
import java.util.Map;
15+
import org.junit.jupiter.api.AfterEach;
16+
import org.junit.jupiter.api.BeforeEach;
17+
import org.junit.jupiter.api.Test;
18+
import org.springframework.beans.factory.annotation.Autowired;
19+
import org.springframework.data.annotation.Id;
20+
import org.springframework.data.elasticsearch.annotations.Document;
21+
import org.springframework.data.elasticsearch.annotations.DynamicTemplates;
22+
import org.springframework.data.elasticsearch.annotations.Field;
23+
import org.springframework.data.elasticsearch.annotations.FieldType;
24+
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
25+
import org.springframework.data.elasticsearch.core.IndexOperations;
26+
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
27+
import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest;
28+
import org.springframework.data.elasticsearch.utils.IndexNameProvider;
29+
import org.springframework.lang.Nullable;
30+
31+
@SpringIntegrationTest
32+
public abstract class DynamicTemplatesContextBaseTests {
33+
@Autowired protected ElasticsearchOperations operations;
34+
@Autowired protected IndexNameProvider indexNameProvider;
35+
36+
@BeforeEach
37+
void setUp() {
38+
indexNameProvider.increment();
39+
}
40+
41+
@AfterEach
42+
void cleanup() {
43+
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*")).delete();
44+
}
45+
46+
@Test
47+
void shouldCreateDynamicTemplateOne() {
48+
IndexOperations indexOperations = operations.indexOps(SampleDynamicTemplatesEntity.class);
49+
indexOperations.createWithMapping();
50+
}
51+
52+
53+
@Test
54+
void shouldCreateDynamicTemplateTwo() {
55+
IndexOperations indexOperations = operations.indexOps(SampleDynamicTemplatesEntityTwo.class);
56+
indexOperations.createWithMapping();
57+
}
58+
59+
/**
60+
* @author Petr Kukral
61+
*/
62+
@Document(indexName = "#{@indexNameProvider.indexName()}")
63+
@DynamicTemplates(mappingPath = "/mappings/test-dynamic_templates_mappings.json")
64+
static class SampleDynamicTemplatesEntity {
65+
66+
@Nullable
67+
@Id private String id;
68+
69+
@Nullable
70+
@Field(type = FieldType.Object) private final Map<String, String> names = new HashMap<>();
71+
}
72+
73+
/**
74+
* @author Petr Kukral
75+
*/
76+
@Document(indexName = "#{@indexNameProvider.indexName()}")
77+
@DynamicTemplates(mappingPath = "/mappings/test-dynamic_templates_mappings_two.json")
78+
static class SampleDynamicTemplatesEntityTwo {
79+
80+
@Nullable
81+
@Id private String id;
82+
83+
@Nullable
84+
@Field(type = FieldType.Object) private final Map<String, String> names = new HashMap<>();
85+
}
86+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
import org.opensearch.data.client.junit.jupiter.OpenSearchRestTemplateConfiguration;
13+
import org.springframework.context.annotation.Bean;
14+
import org.springframework.context.annotation.Configuration;
15+
import org.springframework.context.annotation.Import;
16+
import org.springframework.data.elasticsearch.utils.IndexNameProvider;
17+
import org.springframework.test.context.ContextConfiguration;
18+
19+
@ContextConfiguration(classes = {DynamicTemplatesORHLCIntegrationTests.Config.class})
20+
public class DynamicTemplatesORHLCIntegrationTests extends DynamicTemplatesContextBaseTests {
21+
@Configuration
22+
@Import({ OpenSearchRestTemplateConfiguration.class })
23+
static class Config {
24+
@Bean
25+
IndexNameProvider indexNameProvider() {
26+
return new IndexNameProvider("dynamic-templates-os");
27+
}
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
import org.opensearch.data.client.junit.jupiter.OpenSearchTemplateConfiguration;
13+
import org.springframework.context.annotation.Bean;
14+
import org.springframework.context.annotation.Configuration;
15+
import org.springframework.context.annotation.Import;
16+
import org.springframework.data.elasticsearch.utils.IndexNameProvider;
17+
import org.springframework.test.context.ContextConfiguration;
18+
19+
@ContextConfiguration(classes = {DynamicTemplatesOSCIntegrationTests.Config.class})
20+
public class DynamicTemplatesOSCIntegrationTests extends DynamicTemplatesContextBaseTests {
21+
@Configuration
22+
@Import({OpenSearchTemplateConfiguration.class})
23+
static class Config {
24+
@Bean
25+
IndexNameProvider indexNameProvider() {
26+
return new IndexNameProvider("dynamic-templates-os");
27+
}
28+
}
29+
}

spring-data-opensearch/src/test/resources/mappings/test-dynamic_templates_mappings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
"with_custom_analyzer": {
55
"mapping": {
6-
"type": "string",
6+
"type": "text",
77
"analyzer": "standard_lowercase_asciifolding"
88
},
99
"path_match": "names.*"

spring-data-opensearch/src/test/resources/mappings/test-dynamic_templates_mappings_two.json

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
{
22
"dynamic_templates": [
33
{
4-
"with_custom_analyzer": {
4+
"keywords_without_doc_values": {
5+
"match_mapping_type": "string",
56
"mapping": {
6-
"type": "string",
7-
"analyzer": "standard_lowercase_asciifolding"
7+
"fields": {
8+
"keyword": {
9+
"type": "keyword",
10+
"doc_values": false
11+
}
12+
}
813
},
9-
"path_match": "names.*"
10-
}
11-
},
12-
{
13-
"participantA1_with_custom_analyzer": {
14-
"mapping": {
15-
"type": "string",
16-
"analyzer": "standard_lowercase_asciifolding"
17-
},
18-
"path_match": "participantA1.*"
14+
"path_match": "others.*"
1915
}
2016
}
2117
]

0 commit comments

Comments
 (0)