Skip to content

Commit a4520c7

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 a4520c7

File tree

5 files changed

+185
-15
lines changed

5 files changed

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