Skip to content

Commit 7b58d7a

Browse files
retarursprung
authored andcommitted
Add test cases for dynamic_templates mappings
Note that one of the test cases currently fails and requires opensearch-java#1513 to be resolved first, thus it has been disabled for the OSC client. Signed-off-by: Andriy Redko <drreta@gmail.com> Signed-off-by: Ralph Ursprung <Ralph.Ursprung@avaloq.com>
1 parent 766e25a commit 7b58d7a

File tree

5 files changed

+186
-15
lines changed

5 files changed

+186
-15
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
}
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
{
44
"with_custom_analyzer": {
55
"mapping": {
6-
"type": "string",
7-
"analyzer": "standard_lowercase_asciifolding"
6+
"type": "text",
7+
"analyzer": "standard"
88
},
99
"path_match": "names.*"
1010
}

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": "test.*"
1915
}
2016
}
2117
]

0 commit comments

Comments
 (0)