Skip to content

Commit

Permalink
REF: wire model in implementing IndexTemplateAPIWrapper
Browse files Browse the repository at this point in the history
Signed-off-by: George Chen <[email protected]>
  • Loading branch information
chenqi0805 committed Aug 2, 2023
1 parent a2dd541 commit afb1c76
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,36 @@

package org.opensearch.dataprepper.plugins.sink.opensearch.index;

import org.opensearch.client.opensearch.indices.GetIndexTemplateResponse;
import org.opensearch.client.opensearch.indices.get_index_template.IndexTemplateItem;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
* A {@link TemplateStrategy} for the OpenSearch <a href="https://opensearch.org/docs/latest/im-plugin/index-templates/">index template</a>.
*/
class ComposableIndexTemplateStrategy implements TemplateStrategy {
private final IndexTemplateAPIWrapper<IndexTemplateItem> indexTemplateAPIWrapper;
private final IndexTemplateAPIWrapper<GetIndexTemplateResponse> indexTemplateAPIWrapper;

public ComposableIndexTemplateStrategy(final IndexTemplateAPIWrapper<IndexTemplateItem> indexTemplateAPIWrapper) {
public ComposableIndexTemplateStrategy(final IndexTemplateAPIWrapper<GetIndexTemplateResponse> indexTemplateAPIWrapper) {
this.indexTemplateAPIWrapper = indexTemplateAPIWrapper;
}

@Override
public Optional<Long> getExistingTemplateVersion(final String templateName) throws IOException {
return indexTemplateAPIWrapper.getTemplate(templateName)
.map(IndexTemplateItem::indexTemplate)
.map(indexTemplate -> indexTemplate.version());
.map(getIndexTemplateResponse -> {
final List<IndexTemplateItem> indexTemplateItems = getIndexTemplateResponse.indexTemplates();
if (indexTemplateItems.size() == 1) {
return indexTemplateItems.stream().findFirst().get().indexTemplate().version();
} else {
throw new RuntimeException(String.format("Found zero or multiple index templates result when querying for %s",
templateName));
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@
import org.opensearch.client.opensearch.indices.GetIndexTemplateRequest;
import org.opensearch.client.opensearch.indices.GetIndexTemplateResponse;
import org.opensearch.client.opensearch.indices.PutIndexTemplateRequest;
import org.opensearch.client.opensearch.indices.get_index_template.IndexTemplateItem;
import org.opensearch.client.opensearch.indices.put_index_template.IndexTemplateMapping;
import org.opensearch.client.transport.endpoints.BooleanResponse;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;

public class ComposableTemplateAPIWrapper implements IndexTemplateAPIWrapper<IndexTemplateItem> {
public class ComposableTemplateAPIWrapper implements IndexTemplateAPIWrapper<GetIndexTemplateResponse> {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private final OpenSearchClient openSearchClient;

Expand Down Expand Up @@ -52,7 +50,7 @@ public void putTemplate(final IndexTemplate indexTemplate) throws IOException {
}

@Override
public Optional<IndexTemplateItem> getTemplate(final String indexTemplateName) throws IOException {
public Optional<GetIndexTemplateResponse> getTemplate(final String indexTemplateName) throws IOException {
final ExistsIndexTemplateRequest existsRequest = new ExistsIndexTemplateRequest.Builder()
.name(indexTemplateName)
.build();
Expand All @@ -65,15 +63,7 @@ public Optional<IndexTemplateItem> getTemplate(final String indexTemplateName) t
final GetIndexTemplateRequest getRequest = new GetIndexTemplateRequest.Builder()
.name(indexTemplateName)
.build();
final GetIndexTemplateResponse indexTemplateResponse = openSearchClient.indices().getIndexTemplate(getRequest);

final List<IndexTemplateItem> indexTemplateItems = indexTemplateResponse.indexTemplates();
if (indexTemplateItems.size() == 1) {
return indexTemplateItems.stream().findFirst();
} else {
throw new RuntimeException(String.format("Found zero or multiple index templates result when querying for %s",
indexTemplateName));
}
return Optional.of(openSearchClient.indices().getIndexTemplate(getRequest));
}

private static class PutIndexTemplateRequestDeserializer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.opensearch.client.opensearch.indices.OpenSearchIndicesClient;
import org.opensearch.client.opensearch.indices.PutTemplateRequest;
import org.opensearch.client.opensearch.indices.PutTemplateResponse;
import org.opensearch.client.opensearch.indices.TemplateMapping;
import org.opensearch.client.transport.JsonEndpoint;
import org.opensearch.client.transport.endpoints.BooleanResponse;
import org.opensearch.client.transport.endpoints.SimpleEndpoint;
Expand All @@ -23,7 +22,7 @@
import java.util.Map;
import java.util.Optional;

public class Es6IndexTemplateAPIWrapper implements IndexTemplateAPIWrapper<TemplateMapping> {
public class Es6IndexTemplateAPIWrapper implements IndexTemplateAPIWrapper<GetTemplateResponse> {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

private final OpenSearchClient openSearchClient;
Expand Down Expand Up @@ -56,7 +55,7 @@ public void putTemplate(final IndexTemplate indexTemplate) throws IOException {
}

@Override
public Optional<TemplateMapping> getTemplate(final String templateName) throws IOException {
public Optional<GetTemplateResponse> getTemplate(final String templateName) throws IOException {
final ExistsTemplateRequest existsTemplateRequest = new ExistsTemplateRequest.Builder()
.name(templateName)
.build();
Expand All @@ -69,14 +68,7 @@ public Optional<TemplateMapping> getTemplate(final String templateName) throws I
final GetTemplateRequest getTemplateRequest = new GetTemplateRequest.Builder()
.name(templateName)
.build();
final GetTemplateResponse response = openSearchClient.indices().getTemplate(getTemplateRequest);

if (response.result().size() == 1) {
return response.result().values().stream().findFirst();
} else {
throw new RuntimeException(String.format("Found zero or multiple index templates result when querying for %s",
templateName));
}
return Optional.of(openSearchClient.indices().getTemplate(getTemplateRequest));
}

private JsonEndpoint<PutTemplateRequest, PutTemplateResponse, ErrorResponse> es6PutTemplateEndpoint(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.opensearch.client.opensearch.indices.GetTemplateResponse;
import org.opensearch.client.opensearch.indices.OpenSearchIndicesClient;
import org.opensearch.client.opensearch.indices.PutTemplateRequest;
import org.opensearch.client.opensearch.indices.TemplateMapping;
import org.opensearch.client.transport.endpoints.BooleanResponse;

import java.io.ByteArrayInputStream;
Expand All @@ -18,7 +17,7 @@
import java.util.Map;
import java.util.Optional;

public class OpenSearchLegacyTemplateAPIWrapper implements IndexTemplateAPIWrapper<TemplateMapping> {
public class OpenSearchLegacyTemplateAPIWrapper implements IndexTemplateAPIWrapper<GetTemplateResponse> {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

private final OpenSearchClient openSearchClient;
Expand Down Expand Up @@ -50,7 +49,7 @@ public void putTemplate(IndexTemplate indexTemplate) throws IOException {
}

@Override
public Optional<TemplateMapping> getTemplate(final String templateName) throws IOException {
public Optional<GetTemplateResponse> getTemplate(final String templateName) throws IOException {
final ExistsTemplateRequest existsTemplateRequest = new ExistsTemplateRequest.Builder()
.name(templateName)
.build();
Expand All @@ -63,13 +62,6 @@ public Optional<TemplateMapping> getTemplate(final String templateName) throws I
final GetTemplateRequest getTemplateRequest = new GetTemplateRequest.Builder()
.name(templateName)
.build();
final GetTemplateResponse response = openSearchClient.indices().getTemplate(getTemplateRequest);

if (response.result().size() == 1) {
return response.result().values().stream().findFirst();
} else {
throw new RuntimeException(String.format("Found zero or multiple index templates result when querying for %s",
templateName));
}
return Optional.of(openSearchClient.indices().getTemplate(getTemplateRequest));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,30 @@

package org.opensearch.dataprepper.plugins.sink.opensearch.index;

import org.opensearch.client.opensearch.indices.TemplateMapping;
import org.opensearch.client.opensearch.indices.GetTemplateResponse;

import java.io.IOException;
import java.util.Map;
import java.util.Optional;

class V1TemplateStrategy implements TemplateStrategy {
private final IndexTemplateAPIWrapper<TemplateMapping> indexTemplateAPIWrapper;
private final IndexTemplateAPIWrapper<GetTemplateResponse> indexTemplateAPIWrapper;

public V1TemplateStrategy(final IndexTemplateAPIWrapper<TemplateMapping> indexTemplateAPIWrapper) {
public V1TemplateStrategy(final IndexTemplateAPIWrapper<GetTemplateResponse> indexTemplateAPIWrapper) {
this.indexTemplateAPIWrapper = indexTemplateAPIWrapper;
}

@Override
public Optional<Long> getExistingTemplateVersion(final String templateName) throws IOException {
return indexTemplateAPIWrapper.getTemplate(templateName)
.map(TemplateMapping::version);
.map(response -> {
if (response.result().size() == 1) {
return response.result().values().stream().findFirst().get().version();
} else {
throw new RuntimeException(String.format("Found zero or multiple index templates result when querying for %s",
templateName));
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.client.opensearch.indices.GetIndexTemplateResponse;
import org.opensearch.client.opensearch.indices.get_index_template.IndexTemplate;
import org.opensearch.client.opensearch.indices.get_index_template.IndexTemplateItem;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
Expand All @@ -36,7 +40,9 @@
@ExtendWith(MockitoExtension.class)
class ComposableIndexTemplateStrategyTest {
@Mock
private IndexTemplateAPIWrapper<IndexTemplateItem> indexTemplateAPIWrapper;
private IndexTemplateAPIWrapper<GetIndexTemplateResponse> indexTemplateAPIWrapper;
@Mock
private GetIndexTemplateResponse getIndexTemplateResponse;
@Mock
private IndexTemplateItem indexTemplateItem;
@Mock
Expand All @@ -56,10 +62,13 @@ private ComposableIndexTemplateStrategy createObjectUnderTest() {

@Test
void getExistingTemplateVersion_should_calls_getTemplate_with_templateName() throws IOException {
when(indexTemplateAPIWrapper.getTemplate(any())).thenReturn(Optional.of(indexTemplateItem));
when(indexTemplateAPIWrapper.getTemplate(any())).thenReturn(Optional.of(getIndexTemplateResponse));
when(getIndexTemplateResponse.indexTemplates()).thenReturn(List.of(indexTemplateItem));
when(indexTemplateItem.indexTemplate()).thenReturn(indexTemplate);
createObjectUnderTest().getExistingTemplateVersion(indexTemplateName);

verify(indexTemplateAPIWrapper).getTemplate(eq(indexTemplateName));
verify(indexTemplate).version();
}

@Test
Expand All @@ -72,7 +81,8 @@ void getExistingTemplateVersion_should_return_empty_if_no_template_exists() thro
class WithExistingIndexTemplate {
@Test
void getExistingTemplateVersion_should_return_empty_if_index_template_exists_without_version() throws IOException {
when(indexTemplateAPIWrapper.getTemplate(any())).thenReturn(Optional.of(indexTemplateItem));
when(indexTemplateAPIWrapper.getTemplate(any())).thenReturn(Optional.of(getIndexTemplateResponse));
when(getIndexTemplateResponse.indexTemplates()).thenReturn(List.of(indexTemplateItem));
when(indexTemplateItem.indexTemplate()).thenReturn(indexTemplate);
when(indexTemplate.version()).thenReturn(null);

Expand All @@ -85,7 +95,8 @@ void getExistingTemplateVersion_should_return_empty_if_index_template_exists_wit
@Test
void getExistingTemplateVersion_should_return_template_version_if_template_exists() throws IOException {
final Long version = (long) (random.nextInt(10_000) + 100);
when(indexTemplateAPIWrapper.getTemplate(any())).thenReturn(Optional.of(indexTemplateItem));
when(indexTemplateAPIWrapper.getTemplate(any())).thenReturn(Optional.of(getIndexTemplateResponse));
when(getIndexTemplateResponse.indexTemplates()).thenReturn(List.of(indexTemplateItem));
when(indexTemplateItem.indexTemplate()).thenReturn(indexTemplate);
when(indexTemplate.version()).thenReturn(version);

Expand All @@ -95,6 +106,21 @@ void getExistingTemplateVersion_should_return_template_version_if_template_exist
assertThat(optionalVersion.isPresent(), equalTo(true));
assertThat(optionalVersion.get(), equalTo(version));
}

@ParameterizedTest
@ValueSource(ints = {0, 2})
void getExistingTemplateVersion_should_throw_if_get_template_returns_unexpected_number_of_templates(
final int numberOfTemplatesReturned) throws IOException {
when(indexTemplateAPIWrapper.getTemplate(any())).thenReturn(Optional.of(getIndexTemplateResponse));
final List<IndexTemplateItem> indexTemplateItems = mock(List.class);
when(indexTemplateItems.size()).thenReturn(numberOfTemplatesReturned);
when(getIndexTemplateResponse.indexTemplates()).thenReturn(indexTemplateItems);

assertThrows(RuntimeException.class,
() -> createObjectUnderTest().getExistingTemplateVersion(indexTemplateName));

verify(indexTemplateAPIWrapper).getTemplate(eq(indexTemplateName));
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
Expand All @@ -16,7 +14,6 @@
import org.opensearch.client.opensearch.indices.GetIndexTemplateResponse;
import org.opensearch.client.opensearch.indices.OpenSearchIndicesClient;
import org.opensearch.client.opensearch.indices.PutIndexTemplateRequest;
import org.opensearch.client.opensearch.indices.get_index_template.IndexTemplateItem;
import org.opensearch.client.transport.OpenSearchTransport;
import org.opensearch.client.transport.endpoints.BooleanResponse;
import org.opensearch.dataprepper.plugins.sink.opensearch.bulk.PreSerializedJsonpMapper;
Expand Down Expand Up @@ -116,44 +113,27 @@ void getExistingTemplate_should_return_empty_if_no_index_template_exists() throw
when(openSearchIndicesClient.existsIndexTemplate(any(ExistsIndexTemplateRequest.class)))
.thenReturn(booleanResponse);

final Optional<IndexTemplateItem> optionalIndexTemplateItem = objectUnderTest.getTemplate(indexTemplateName);
final Optional<GetIndexTemplateResponse> optionalGetIndexTemplateResponse = objectUnderTest.getTemplate(
indexTemplateName);

assertThat(optionalIndexTemplateItem, notNullValue());
assertThat(optionalIndexTemplateItem.isPresent(), equalTo(false));
}

@ParameterizedTest
@ValueSource(ints = {0, 2})
void getExistingTemplate_should_throw_if_get_template_returns_unexpected_number_of_templates(final int numberOfTemplatesReturned) throws IOException {
when(booleanResponse.value()).thenReturn(true);
when(openSearchIndicesClient.existsIndexTemplate(any(ExistsIndexTemplateRequest.class)))
.thenReturn(booleanResponse);
final List<IndexTemplateItem> indexTemplateItems = mock(List.class);
when(indexTemplateItems.size()).thenReturn(numberOfTemplatesReturned);
when(getIndexTemplateResponse.indexTemplates()).thenReturn(indexTemplateItems);
when(openSearchIndicesClient.getIndexTemplate(any(GetIndexTemplateRequest.class)))
.thenReturn(getIndexTemplateResponse);

assertThrows(RuntimeException.class, () -> objectUnderTest.getTemplate(indexTemplateName));

verify(openSearchIndicesClient).getIndexTemplate(any(GetIndexTemplateRequest.class));
assertThat(optionalGetIndexTemplateResponse, notNullValue());
assertThat(optionalGetIndexTemplateResponse.isPresent(), equalTo(false));
}

@Test
void getExistingTemplate_should_return_template_if_template_exists() throws IOException {
when(booleanResponse.value()).thenReturn(true);
when(openSearchIndicesClient.existsIndexTemplate(any(ExistsIndexTemplateRequest.class)))
.thenReturn(booleanResponse);
final IndexTemplateItem indexTemplateItem = mock(IndexTemplateItem.class);
when(getIndexTemplateResponse.indexTemplates()).thenReturn(Collections.singletonList(indexTemplateItem));
when(openSearchIndicesClient.getIndexTemplate(any(GetIndexTemplateRequest.class)))
.thenReturn(getIndexTemplateResponse);

final Optional<IndexTemplateItem> optionalIndexTemplateItem = objectUnderTest.getTemplate(indexTemplateName);
final Optional<GetIndexTemplateResponse> optionalGetIndexTemplateResponse = objectUnderTest.getTemplate(
indexTemplateName);

assertThat(optionalIndexTemplateItem, notNullValue());
assertThat(optionalIndexTemplateItem.isPresent(), equalTo(true));
assertThat(optionalIndexTemplateItem.get(), equalTo(indexTemplateItem));
assertThat(optionalGetIndexTemplateResponse, notNullValue());
assertThat(optionalGetIndexTemplateResponse.isPresent(), equalTo(true));
assertThat(optionalGetIndexTemplateResponse.get(), equalTo(getIndexTemplateResponse));
}


Expand Down
Loading

0 comments on commit afb1c76

Please sign in to comment.