|
| 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 | + |
| 11 | +package org.opensearch.dataprepper.plugins.sink.opensearch; |
| 12 | + |
| 13 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 14 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 15 | +import org.junit.jupiter.api.BeforeEach; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | +import org.opensearch.client.opensearch._types.VersionType; |
| 18 | +import org.opensearch.client.opensearch.core.bulk.BulkOperation; |
| 19 | +import org.opensearch.dataprepper.model.opensearch.OpenSearchBulkActions; |
| 20 | +import org.opensearch.dataprepper.plugins.sink.opensearch.bulk.SerializedJson; |
| 21 | + |
| 22 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 23 | +import static org.hamcrest.Matchers.equalTo; |
| 24 | +import static org.hamcrest.Matchers.is; |
| 25 | + |
| 26 | +public class BulkOperationFactoryTest { |
| 27 | + |
| 28 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 29 | + private BulkOperationFactory factory; |
| 30 | + |
| 31 | + @BeforeEach |
| 32 | + void setUp() { |
| 33 | + final ScriptManager scriptManager = new ScriptManager(null, null); |
| 34 | + factory = new BulkOperationFactory(VersionType.External, scriptManager, objectMapper, false); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void create_action_returns_create_operation() { |
| 39 | + final SerializedJson document = SerializedJson.fromStringAndOptionals("{\"key\":\"val\"}", "doc-1", "route-1", null); |
| 40 | + |
| 41 | + final BulkOperation result = factory.create( |
| 42 | + OpenSearchBulkActions.CREATE.toString(), document, null, "test-index", objectMapper.createObjectNode()); |
| 43 | + |
| 44 | + assertThat(result.isCreate(), is(true)); |
| 45 | + assertThat(result.create().index(), equalTo("test-index")); |
| 46 | + assertThat(result.create().id(), equalTo("doc-1")); |
| 47 | + assertThat(result.create().routing(), equalTo("route-1")); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void index_action_returns_index_operation() { |
| 52 | + final SerializedJson document = SerializedJson.fromStringAndOptionals("{\"key\":\"val\"}", "doc-1", null, null); |
| 53 | + |
| 54 | + final BulkOperation result = factory.create( |
| 55 | + OpenSearchBulkActions.INDEX.toString(), document, 1L, "test-index", objectMapper.createObjectNode()); |
| 56 | + |
| 57 | + assertThat(result.isIndex(), is(true)); |
| 58 | + assertThat(result.index().index(), equalTo("test-index")); |
| 59 | + assertThat(result.index().id(), equalTo("doc-1")); |
| 60 | + assertThat(result.index().version(), equalTo(1L)); |
| 61 | + assertThat(result.index().versionType(), equalTo(VersionType.External)); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void delete_action_returns_delete_operation() { |
| 66 | + final SerializedJson document = SerializedJson.fromStringAndOptionals("{}", "doc-1", "route-1", null); |
| 67 | + |
| 68 | + final BulkOperation result = factory.create( |
| 69 | + OpenSearchBulkActions.DELETE.toString(), document, 2L, "test-index", objectMapper.createObjectNode()); |
| 70 | + |
| 71 | + assertThat(result.isDelete(), is(true)); |
| 72 | + assertThat(result.delete().index(), equalTo("test-index")); |
| 73 | + assertThat(result.delete().id(), equalTo("doc-1")); |
| 74 | + assertThat(result.delete().routing(), equalTo("route-1")); |
| 75 | + assertThat(result.delete().version(), equalTo(2L)); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void update_action_returns_update_operation() { |
| 80 | + final ObjectNode jsonNode = objectMapper.createObjectNode().put("name", "test"); |
| 81 | + final SerializedJson document = SerializedJson.fromStringAndOptionals("{\"name\":\"test\"}", "doc-1", null, null); |
| 82 | + |
| 83 | + final BulkOperation result = factory.create( |
| 84 | + OpenSearchBulkActions.UPDATE.toString(), document, null, "test-index", jsonNode); |
| 85 | + |
| 86 | + assertThat(result.isUpdate(), is(true)); |
| 87 | + assertThat(result.update().index(), equalTo("test-index")); |
| 88 | + assertThat(result.update().id(), equalTo("doc-1")); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void upsert_action_returns_update_operation() { |
| 93 | + final ObjectNode jsonNode = objectMapper.createObjectNode().put("name", "test"); |
| 94 | + final SerializedJson document = SerializedJson.fromStringAndOptionals("{\"name\":\"test\"}", "doc-1", null, null); |
| 95 | + |
| 96 | + final BulkOperation result = factory.create( |
| 97 | + OpenSearchBulkActions.UPSERT.toString(), document, null, "test-index", jsonNode); |
| 98 | + |
| 99 | + assertThat(result.isUpdate(), is(true)); |
| 100 | + assertThat(result.update().index(), equalTo("test-index")); |
| 101 | + assertThat(result.update().id(), equalTo("doc-1")); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void unknown_action_defaults_to_index_operation() { |
| 106 | + final SerializedJson document = SerializedJson.fromStringAndOptionals("{}", "doc-1", null, null); |
| 107 | + |
| 108 | + final BulkOperation result = factory.create( |
| 109 | + "unknown_action", document, null, "test-index", objectMapper.createObjectNode()); |
| 110 | + |
| 111 | + assertThat(result.isIndex(), is(true)); |
| 112 | + assertThat(result.index().index(), equalTo("test-index")); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void create_action_without_optional_fields() { |
| 117 | + final SerializedJson document = SerializedJson.fromStringAndOptionals("{}", null, null, null); |
| 118 | + |
| 119 | + final BulkOperation result = factory.create( |
| 120 | + OpenSearchBulkActions.CREATE.toString(), document, null, "test-index", objectMapper.createObjectNode()); |
| 121 | + |
| 122 | + assertThat(result.isCreate(), is(true)); |
| 123 | + assertThat(result.create().index(), equalTo("test-index")); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + void update_with_document_filters_deserializes_from_serialized_json() { |
| 128 | + final ScriptManager scriptManager = new ScriptManager(null, null); |
| 129 | + final BulkOperationFactory filterFactory = new BulkOperationFactory(null, scriptManager, objectMapper, true); |
| 130 | + final ObjectNode jsonNode = objectMapper.createObjectNode().put("name", "original"); |
| 131 | + final SerializedJson document = SerializedJson.fromStringAndOptionals("{\"name\":\"filtered\"}", "doc-1", null, null); |
| 132 | + |
| 133 | + final BulkOperation result = filterFactory.create( |
| 134 | + OpenSearchBulkActions.UPDATE.toString(), document, null, "test-index", jsonNode); |
| 135 | + |
| 136 | + assertThat(result.isUpdate(), is(true)); |
| 137 | + assertThat(result.update().id(), equalTo("doc-1")); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void create_action_sets_pipeline() { |
| 142 | + final SerializedJson document = SerializedJson.fromStringAndOptionals("{}", "doc-1", null, "my-pipeline"); |
| 143 | + |
| 144 | + final BulkOperation result = factory.create( |
| 145 | + OpenSearchBulkActions.CREATE.toString(), document, null, "test-index", objectMapper.createObjectNode()); |
| 146 | + |
| 147 | + assertThat(result.isCreate(), is(true)); |
| 148 | + assertThat(result.create().pipeline(), equalTo("my-pipeline")); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + void index_action_sets_pipeline() { |
| 153 | + final SerializedJson document = SerializedJson.fromStringAndOptionals("{}", "doc-1", null, "my-pipeline"); |
| 154 | + |
| 155 | + final BulkOperation result = factory.create( |
| 156 | + OpenSearchBulkActions.INDEX.toString(), document, null, "test-index", objectMapper.createObjectNode()); |
| 157 | + |
| 158 | + assertThat(result.isIndex(), is(true)); |
| 159 | + assertThat(result.index().pipeline(), equalTo("my-pipeline")); |
| 160 | + } |
| 161 | +} |
0 commit comments