|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.ml.common.transport.batch; |
| 7 | + |
| 8 | +import static org.junit.Assert.assertEquals; |
| 9 | +import static org.junit.Assert.assertNotNull; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.function.Consumer; |
| 16 | + |
| 17 | +import org.junit.Before; |
| 18 | +import org.junit.Rule; |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.rules.ExpectedException; |
| 21 | +import org.opensearch.common.io.stream.BytesStreamOutput; |
| 22 | +import org.opensearch.common.settings.Settings; |
| 23 | +import org.opensearch.common.xcontent.LoggingDeprecationHandler; |
| 24 | +import org.opensearch.common.xcontent.XContentFactory; |
| 25 | +import org.opensearch.common.xcontent.XContentType; |
| 26 | +import org.opensearch.core.common.io.stream.StreamInput; |
| 27 | +import org.opensearch.core.xcontent.NamedXContentRegistry; |
| 28 | +import org.opensearch.core.xcontent.ToXContent; |
| 29 | +import org.opensearch.core.xcontent.XContentBuilder; |
| 30 | +import org.opensearch.core.xcontent.XContentParser; |
| 31 | +import org.opensearch.search.SearchModule; |
| 32 | + |
| 33 | +public class MLBatchIngestionInputTests { |
| 34 | + |
| 35 | + private MLBatchIngestionInput mlBatchIngestionInput; |
| 36 | + |
| 37 | + private Map<String, String> dataSource; |
| 38 | + |
| 39 | + @Rule |
| 40 | + public final ExpectedException exceptionRule = ExpectedException.none(); |
| 41 | + |
| 42 | + private final String expectedInputStr = "{" |
| 43 | + + "\"index_name\":\"test index\"," |
| 44 | + + "\"text_embedding_field_map\":{" |
| 45 | + + "\"chapter\":\"chapter_embedding\"" |
| 46 | + + "}," |
| 47 | + + "\"data_source\":{" |
| 48 | + + "\"source\":\"s3://samplebucket/output/sampleresults.json.out\"," |
| 49 | + + "\"type\":\"s3\"" |
| 50 | + + "}," |
| 51 | + + "\"credential\":{" |
| 52 | + + "\"region\":\"test region\"" |
| 53 | + + "}" |
| 54 | + + "}"; |
| 55 | + |
| 56 | + @Before |
| 57 | + public void setUp() { |
| 58 | + dataSource = new HashMap<>(); |
| 59 | + dataSource.put("type", "s3"); |
| 60 | + dataSource.put("source", "s3://samplebucket/output/sampleresults.json.out"); |
| 61 | + |
| 62 | + Map<String, String> credentials = Map.of("region", "test region"); |
| 63 | + Map<String, String> fieldMapping = Map.of("chapter", "chapter_embedding"); |
| 64 | + |
| 65 | + mlBatchIngestionInput = MLBatchIngestionInput |
| 66 | + .builder() |
| 67 | + .indexName("test index") |
| 68 | + .credential(credentials) |
| 69 | + .fieldMapping(fieldMapping) |
| 70 | + .dataSources(dataSource) |
| 71 | + .build(); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void constructorMLBatchIngestionInput_NullName() { |
| 76 | + exceptionRule.expect(IllegalArgumentException.class); |
| 77 | + exceptionRule.expectMessage("index name for ingestion is null"); |
| 78 | + |
| 79 | + MLBatchIngestionInput.builder().indexName(null).dataSources(dataSource).build(); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void constructorMLBatchIngestionInput_NullSource() { |
| 84 | + exceptionRule.expect(IllegalArgumentException.class); |
| 85 | + exceptionRule.expectMessage("dataSources for ingestion is null"); |
| 86 | + MLBatchIngestionInput.builder().indexName("test index").dataSources(null).build(); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testToXContent_FullFields() throws Exception { |
| 91 | + XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 92 | + mlBatchIngestionInput.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 93 | + assertNotNull(builder); |
| 94 | + String jsonStr = builder.toString(); |
| 95 | + assertEquals(expectedInputStr, jsonStr); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testParse() throws Exception { |
| 100 | + testParseFromJsonString(expectedInputStr, parsedInput -> { |
| 101 | + assertEquals("test index", parsedInput.getIndexName()); |
| 102 | + assertEquals("test region", parsedInput.getCredential().get("region")); |
| 103 | + assertEquals("chapter_embedding", parsedInput.getFieldMapping().get("chapter")); |
| 104 | + assertEquals("s3", parsedInput.getDataSources().get("type")); |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + private void testParseFromJsonString(String expectedInputString, Consumer<MLBatchIngestionInput> verify) throws Exception { |
| 109 | + XContentParser parser = XContentType.JSON |
| 110 | + .xContent() |
| 111 | + .createParser( |
| 112 | + new NamedXContentRegistry(new SearchModule(Settings.EMPTY, Collections.emptyList()).getNamedXContents()), |
| 113 | + LoggingDeprecationHandler.INSTANCE, |
| 114 | + expectedInputString |
| 115 | + ); |
| 116 | + parser.nextToken(); |
| 117 | + MLBatchIngestionInput parsedInput = MLBatchIngestionInput.parse(parser); |
| 118 | + verify.accept(parsedInput); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void readInputStream_Success() throws IOException { |
| 123 | + readInputStream( |
| 124 | + mlBatchIngestionInput, |
| 125 | + parsedInput -> assertEquals(mlBatchIngestionInput.getIndexName(), parsedInput.getIndexName()) |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + private void readInputStream(MLBatchIngestionInput input, Consumer<MLBatchIngestionInput> verify) throws IOException { |
| 130 | + BytesStreamOutput bytesStreamOutput = new BytesStreamOutput(); |
| 131 | + input.writeTo(bytesStreamOutput); |
| 132 | + StreamInput streamInput = bytesStreamOutput.bytes().streamInput(); |
| 133 | + MLBatchIngestionInput parsedInput = new MLBatchIngestionInput(streamInput); |
| 134 | + verify.accept(parsedInput); |
| 135 | + } |
| 136 | +} |
0 commit comments