|
| 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.source.opensearchapi; |
| 12 | + |
| 13 | +import org.junit.jupiter.api.BeforeEach; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.opensearch.dataprepper.model.event.Event; |
| 16 | +import org.opensearch.dataprepper.model.record.Record; |
| 17 | +import org.opensearch.dataprepper.plugins.source.opensearchapi.model.BulkAPIEventMetadataKeyAttributes; |
| 18 | + |
| 19 | +import java.io.ByteArrayInputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.time.Instant; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 28 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 29 | + |
| 30 | +class OpenSearchBulkByteDecoderTest { |
| 31 | + |
| 32 | + private OpenSearchBulkByteDecoder decoder; |
| 33 | + |
| 34 | + @BeforeEach |
| 35 | + void setUp() { |
| 36 | + decoder = new OpenSearchBulkByteDecoder(); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void parse_withIndexAction_createsEventWithCorrectMetadata() throws IOException { |
| 41 | + String bulk = "{\"index\":{\"_index\":\"my-index\",\"_id\":\"1\"}}\n" + |
| 42 | + "{\"field1\":\"value1\",\"field2\":42}\n"; |
| 43 | + |
| 44 | + List<Record<Event>> records = parseAll(bulk); |
| 45 | + |
| 46 | + assertEquals(1, records.size()); |
| 47 | + Event event = records.get(0).getData(); |
| 48 | + assertEquals("value1", event.get("field1", String.class)); |
| 49 | + assertEquals(42, event.get("field2", Integer.class)); |
| 50 | + assertEquals("index", event.getMetadata().getAttribute( |
| 51 | + BulkAPIEventMetadataKeyAttributes.BULK_API_EVENT_METADATA_ATTRIBUTE_ACTION)); |
| 52 | + assertEquals("my-index", event.getMetadata().getAttribute( |
| 53 | + BulkAPIEventMetadataKeyAttributes.BULK_API_EVENT_METADATA_ATTRIBUTE_INDEX)); |
| 54 | + assertEquals("1", event.getMetadata().getAttribute( |
| 55 | + BulkAPIEventMetadataKeyAttributes.BULK_API_EVENT_METADATA_ATTRIBUTE_ID)); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void parse_withDeleteAction_createsEventWithNoBody() throws IOException { |
| 60 | + String bulk = "{\"delete\":{\"_index\":\"my-index\",\"_id\":\"2\"}}\n"; |
| 61 | + |
| 62 | + List<Record<Event>> records = parseAll(bulk); |
| 63 | + |
| 64 | + assertEquals(1, records.size()); |
| 65 | + Event event = records.get(0).getData(); |
| 66 | + assertEquals("delete", event.getMetadata().getAttribute( |
| 67 | + BulkAPIEventMetadataKeyAttributes.BULK_API_EVENT_METADATA_ATTRIBUTE_ACTION)); |
| 68 | + assertEquals("my-index", event.getMetadata().getAttribute( |
| 69 | + BulkAPIEventMetadataKeyAttributes.BULK_API_EVENT_METADATA_ATTRIBUTE_INDEX)); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void parse_withMultipleActions_createsMultipleEvents() throws IOException { |
| 74 | + String bulk = "{\"index\":{\"_index\":\"idx\",\"_id\":\"1\"}}\n" + |
| 75 | + "{\"name\":\"doc1\"}\n" + |
| 76 | + "{\"create\":{\"_index\":\"idx\",\"_id\":\"2\"}}\n" + |
| 77 | + "{\"name\":\"doc2\"}\n" + |
| 78 | + "{\"delete\":{\"_index\":\"idx\",\"_id\":\"3\"}}\n"; |
| 79 | + |
| 80 | + List<Record<Event>> records = parseAll(bulk); |
| 81 | + |
| 82 | + assertEquals(3, records.size()); |
| 83 | + assertEquals("index", records.get(0).getData().getMetadata().getAttribute( |
| 84 | + BulkAPIEventMetadataKeyAttributes.BULK_API_EVENT_METADATA_ATTRIBUTE_ACTION)); |
| 85 | + assertEquals("create", records.get(1).getData().getMetadata().getAttribute( |
| 86 | + BulkAPIEventMetadataKeyAttributes.BULK_API_EVENT_METADATA_ATTRIBUTE_ACTION)); |
| 87 | + assertEquals("delete", records.get(2).getData().getMetadata().getAttribute( |
| 88 | + BulkAPIEventMetadataKeyAttributes.BULK_API_EVENT_METADATA_ATTRIBUTE_ACTION)); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void parse_withRoutingAndPipeline_setsMetadata() throws IOException { |
| 93 | + String bulk = "{\"index\":{\"_index\":\"idx\",\"routing\":\"r1\",\"pipeline\":\"p1\"}}\n" + |
| 94 | + "{\"data\":true}\n"; |
| 95 | + |
| 96 | + List<Record<Event>> records = parseAll(bulk); |
| 97 | + |
| 98 | + assertEquals(1, records.size()); |
| 99 | + Event event = records.get(0).getData(); |
| 100 | + assertEquals("r1", event.getMetadata().getAttribute( |
| 101 | + BulkAPIEventMetadataKeyAttributes.BULK_API_EVENT_METADATA_ATTRIBUTE_ROUTING)); |
| 102 | + assertEquals("p1", event.getMetadata().getAttribute( |
| 103 | + BulkAPIEventMetadataKeyAttributes.BULK_API_EVENT_METADATA_ATTRIBUTE_PIPELINE)); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void parse_withBlankLines_skipsBlankLines() throws IOException { |
| 108 | + String bulk = "\n\n{\"index\":{\"_index\":\"idx\"}}\n{\"x\":1}\n\n"; |
| 109 | + |
| 110 | + List<Record<Event>> records = parseAll(bulk); |
| 111 | + |
| 112 | + assertEquals(1, records.size()); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void parse_withEmptyInput_returnsNoEvents() throws IOException { |
| 117 | + List<Record<Event>> records = parseAll(""); |
| 118 | + assertTrue(records.isEmpty()); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + void parse_withTimeReceived_setsTimeOnEvent() throws IOException { |
| 123 | + String bulk = "{\"index\":{\"_index\":\"idx\"}}\n{\"x\":1}\n"; |
| 124 | + Instant now = Instant.now(); |
| 125 | + |
| 126 | + List<Record<Event>> records = new ArrayList<>(); |
| 127 | + decoder.parse(new ByteArrayInputStream(bulk.getBytes(StandardCharsets.UTF_8)), now, records::add); |
| 128 | + |
| 129 | + assertEquals(1, records.size()); |
| 130 | + assertNotNull(records.get(0).getData().getMetadata().getTimeReceived()); |
| 131 | + } |
| 132 | + |
| 133 | + private List<Record<Event>> parseAll(String input) throws IOException { |
| 134 | + List<Record<Event>> records = new ArrayList<>(); |
| 135 | + decoder.parse(new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)), Instant.now(), records::add); |
| 136 | + return records; |
| 137 | + } |
| 138 | +} |
0 commit comments