|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.action.admin.indices.streamingingestion; |
| 10 | + |
| 11 | +import org.opensearch.common.io.stream.BytesStreamOutput; |
| 12 | +import org.opensearch.core.common.io.stream.StreamInput; |
| 13 | +import org.opensearch.test.OpenSearchTestCase; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +public class IngestionUpdateStateResponseTests extends OpenSearchTestCase { |
| 20 | + |
| 21 | + public void testSerialization() throws IOException { |
| 22 | + IngestionStateShardFailure[] shardFailures = new IngestionStateShardFailure[] { |
| 23 | + new IngestionStateShardFailure("index1", 0, "test failure") }; |
| 24 | + IngestionUpdateStateResponse response = new IngestionUpdateStateResponse(true, true, shardFailures, "test error"); |
| 25 | + |
| 26 | + try (BytesStreamOutput out = new BytesStreamOutput()) { |
| 27 | + response.writeTo(out); |
| 28 | + |
| 29 | + try (StreamInput in = out.bytes().streamInput()) { |
| 30 | + IngestionUpdateStateResponse deserializedResponse = new IngestionUpdateStateResponse(in); |
| 31 | + assertTrue(deserializedResponse.isAcknowledged()); |
| 32 | + assertTrue(deserializedResponse.isShardsAcknowledged()); |
| 33 | + assertNotNull(deserializedResponse.getShardFailures()); |
| 34 | + assertEquals(1, deserializedResponse.getShardFailures().length); |
| 35 | + assertEquals("index1", deserializedResponse.getShardFailures()[0].getIndex()); |
| 36 | + assertEquals(0, deserializedResponse.getShardFailures()[0].getShard()); |
| 37 | + assertEquals("test error", deserializedResponse.getErrorMessage()); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public void testShardFailureGrouping() { |
| 43 | + IngestionStateShardFailure[] shardFailures = new IngestionStateShardFailure[] { |
| 44 | + new IngestionStateShardFailure("index1", 0, "failure 1"), |
| 45 | + new IngestionStateShardFailure("index1", 1, "failure 2"), |
| 46 | + new IngestionStateShardFailure("index2", 0, "failure 3") }; |
| 47 | + IngestionUpdateStateResponse response = new IngestionUpdateStateResponse(true, true, shardFailures, "test error"); |
| 48 | + |
| 49 | + Map<String, List<IngestionStateShardFailure>> groupedFailures = IngestionStateShardFailure.groupShardFailuresByIndex(shardFailures); |
| 50 | + assertEquals(2, groupedFailures.size()); |
| 51 | + assertEquals(2, groupedFailures.get("index1").size()); |
| 52 | + assertEquals(1, groupedFailures.get("index2").size()); |
| 53 | + assertEquals(0, groupedFailures.get("index1").get(0).getShard()); |
| 54 | + assertEquals(1, groupedFailures.get("index1").get(1).getShard()); |
| 55 | + assertEquals(0, groupedFailures.get("index2").get(0).getShard()); |
| 56 | + } |
| 57 | +} |
0 commit comments