Skip to content

Commit 699aa7f

Browse files
pstetsukreta
andauthored
Fixed IndexOperations.getIndexTemplate() logic (#417)
* Fixed IndexOperations.getIndexTemplate() logic Signed-off-by: Pavel Stetsuk <[email protected]> * code review Signed-off-by: Pavel Stetsuk <[email protected]> * Update spring-data-opensearch/src/test/java/org/opensearch/data/client/core/indices/IndexTemplateOperationsOSCIntegrationTests.java Co-authored-by: Andriy Redko <[email protected]> Signed-off-by: Pavlo Stetsiuk <[email protected]> --------- Signed-off-by: Pavel Stetsuk <[email protected]> Signed-off-by: Pavlo Stetsiuk <[email protected]> Co-authored-by: Andriy Redko <[email protected]>
1 parent eee926e commit 699aa7f

File tree

3 files changed

+108
-7
lines changed

3 files changed

+108
-7
lines changed

spring-data-opensearch/src/main/java/org/opensearch/data/client/osc/ResponseConverter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.opensearch.data.client.osc;
1717

1818
import static org.opensearch.data.client.osc.JsonUtils.toJson;
19-
import static org.opensearch.data.client.osc.TypeUtils.removePrefixFromJson;
2019
import static org.opensearch.data.client.osc.TypeUtils.typeMapping;
2120

2221
import java.util.*;
@@ -126,7 +125,7 @@ private TemplateResponseData clusterGetComponentTemplateData(ComponentTemplateSu
126125
var mapping = typeMapping(componentTemplateSummary.mappings());
127126
var settings = new Settings();
128127
componentTemplateSummary.settings().forEach((key, indexSettings) -> {
129-
settings.put(key, Settings.parse(removePrefixFromJson(indexSettings.toString())));
128+
settings.put(key, Settings.parse(indexSettings.toJsonString()));
130129
});
131130

132131
Function<? super Map.Entry<String, AliasDefinition>, String> keyMapper = Map.Entry::getKey;

spring-data-opensearch/src/main/java/org/opensearch/data/client/osc/TypeUtils.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -468,11 +468,7 @@ static TypeMapping typeMapping(@Nullable Document mapping) {
468468

469469
@Nullable
470470
static Document typeMapping(@Nullable TypeMapping typeMapping) {
471-
return (typeMapping != null) ? Document.parse(removePrefixFromJson(typeMapping.toString())) : null;
472-
}
473-
474-
public static String removePrefixFromJson(String jsonWithPrefix) {
475-
return jsonWithPrefix.substring(jsonWithPrefix.indexOf("{"));
471+
return (typeMapping != null) ? Document.parse(typeMapping.toJsonString()) : null;
476472
}
477473

478474
@Nullable
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

Comments
 (0)