|
14 | 14 | import org.junit.Before;
|
15 | 15 | import org.junit.Test;
|
16 | 16 | import org.opensearch.common.io.stream.BytesStreamOutput;
|
| 17 | +import org.opensearch.common.xcontent.LoggingDeprecationHandler; |
| 18 | +import org.opensearch.common.xcontent.XContentType; |
17 | 19 | import org.opensearch.core.common.io.stream.StreamInput;
|
| 20 | +import org.opensearch.core.xcontent.NamedXContentRegistry; |
| 21 | +import org.opensearch.core.xcontent.XContentBuilder; |
| 22 | +import org.opensearch.core.xcontent.XContentParser; |
| 23 | +import org.opensearch.ml.common.TestHelper; |
18 | 24 | import org.opensearch.ml.common.output.MLOutputType;
|
19 | 25 |
|
20 | 26 | public class ModelTensorOutputTest {
|
@@ -61,6 +67,109 @@ public void readInputStream_NullField() throws IOException {
|
61 | 67 | });
|
62 | 68 | }
|
63 | 69 |
|
| 70 | + @Test |
| 71 | + public void parse_Success() throws IOException { |
| 72 | + XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent()); |
| 73 | + builder.startObject(); |
| 74 | + builder.startArray(ModelTensorOutput.INFERENCE_RESULT_FIELD); |
| 75 | + |
| 76 | + builder.startObject(); |
| 77 | + builder.startArray("output"); |
| 78 | + |
| 79 | + builder.startObject(); |
| 80 | + builder.field("name", "test"); |
| 81 | + builder.field("data_type", "FLOAT32"); |
| 82 | + builder.field("shape", new long[] { 1, 3 }); |
| 83 | + builder.field("data", value); |
| 84 | + builder.endObject(); |
| 85 | + |
| 86 | + builder.endArray(); |
| 87 | + builder.endObject(); |
| 88 | + |
| 89 | + builder.endArray(); |
| 90 | + builder.endObject(); |
| 91 | + |
| 92 | + String jsonStr = TestHelper.xContentBuilderToString(builder); |
| 93 | + |
| 94 | + XContentParser parser = XContentType.JSON |
| 95 | + .xContent() |
| 96 | + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, jsonStr); |
| 97 | + parser.nextToken(); |
| 98 | + |
| 99 | + ModelTensorOutput parsedOutput = ModelTensorOutput.parse(parser); |
| 100 | + |
| 101 | + assertEquals(1, parsedOutput.getMlModelOutputs().size()); |
| 102 | + ModelTensors modelTensors = parsedOutput.getMlModelOutputs().get(0); |
| 103 | + assertEquals(1, modelTensors.getMlModelTensors().size()); |
| 104 | + ModelTensor modelTensor = modelTensors.getMlModelTensors().get(0); |
| 105 | + assertEquals("test", modelTensor.getName()); |
| 106 | + assertEquals(value.length, modelTensor.getData().length); |
| 107 | + assertEquals(value[0].doubleValue(), modelTensor.getData()[0].doubleValue(), 0.0001); |
| 108 | + assertArrayEquals(new long[] { 1, 3 }, modelTensor.getShape()); |
| 109 | + assertEquals(MLResultDataType.FLOAT32, modelTensor.getDataType()); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void parse_EmptyObject() throws IOException { |
| 114 | + XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent()); |
| 115 | + builder.startObject(); |
| 116 | + builder.endObject(); |
| 117 | + |
| 118 | + String jsonStr = TestHelper.xContentBuilderToString(builder); |
| 119 | + |
| 120 | + XContentParser parser = XContentType.JSON |
| 121 | + .xContent() |
| 122 | + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, jsonStr); |
| 123 | + parser.nextToken(); |
| 124 | + |
| 125 | + ModelTensorOutput parsedOutput = ModelTensorOutput.parse(parser); |
| 126 | + |
| 127 | + assertEquals(0, parsedOutput.getMlModelOutputs().size()); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void parse_SkipIrrelevantFields() throws IOException { |
| 132 | + XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent()); |
| 133 | + builder.startObject(); |
| 134 | + |
| 135 | + builder.field("irrelevant_field", "irrelevant_value"); |
| 136 | + |
| 137 | + builder.startArray(ModelTensorOutput.INFERENCE_RESULT_FIELD); |
| 138 | + builder.startObject(); |
| 139 | + builder.startArray("output"); |
| 140 | + builder.startObject(); |
| 141 | + builder.field("name", "test"); |
| 142 | + builder.field("data_type", "FLOAT32"); |
| 143 | + builder.field("shape", new long[] { 1, 3 }); |
| 144 | + builder.field("data", value); |
| 145 | + builder.endObject(); |
| 146 | + builder.endArray(); |
| 147 | + builder.endObject(); |
| 148 | + builder.endArray(); |
| 149 | + |
| 150 | + builder.field("another_irrelevant_field", "another_value"); |
| 151 | + |
| 152 | + builder.endObject(); |
| 153 | + |
| 154 | + String jsonStr = TestHelper.xContentBuilderToString(builder); |
| 155 | + |
| 156 | + XContentParser parser = XContentType.JSON |
| 157 | + .xContent() |
| 158 | + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, jsonStr); |
| 159 | + parser.nextToken(); |
| 160 | + |
| 161 | + ModelTensorOutput parsedOutput = ModelTensorOutput.parse(parser); |
| 162 | + |
| 163 | + assertEquals(1, parsedOutput.getMlModelOutputs().size()); |
| 164 | + ModelTensors modelTensors = parsedOutput.getMlModelOutputs().get(0); |
| 165 | + assertEquals(1, modelTensors.getMlModelTensors().size()); |
| 166 | + ModelTensor modelTensor = modelTensors.getMlModelTensors().get(0); |
| 167 | + assertEquals("test", modelTensor.getName()); |
| 168 | + assertEquals(value.length, modelTensor.getData().length); |
| 169 | + assertEquals(value[0].doubleValue(), modelTensor.getData()[0].doubleValue(), 0.0001); |
| 170 | + assertArrayEquals(new long[] { 1, 3 }, modelTensor.getShape()); |
| 171 | + } |
| 172 | + |
64 | 173 | private void readInputStream(ModelTensorOutput input, Consumer<ModelTensorOutput> verify) throws IOException {
|
65 | 174 | BytesStreamOutput bytesStreamOutput = new BytesStreamOutput();
|
66 | 175 | input.writeTo(bytesStreamOutput);
|
|
0 commit comments