|
| 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.state; |
| 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 ShardIngestionStateTests extends OpenSearchTestCase { |
| 20 | + |
| 21 | + public void testSerialization() throws IOException { |
| 22 | + ShardIngestionState state = new ShardIngestionState("index1", 0, "POLLING", "DROP", false); |
| 23 | + |
| 24 | + try (BytesStreamOutput out = new BytesStreamOutput()) { |
| 25 | + state.writeTo(out); |
| 26 | + |
| 27 | + try (StreamInput in = out.bytes().streamInput()) { |
| 28 | + ShardIngestionState deserializedState = new ShardIngestionState(in); |
| 29 | + assertEquals(state.getIndex(), deserializedState.getIndex()); |
| 30 | + assertEquals(state.getShardId(), deserializedState.getShardId()); |
| 31 | + assertEquals(state.getPollerState(), deserializedState.getPollerState()); |
| 32 | + assertEquals(state.isPollerPaused(), deserializedState.isPollerPaused()); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public void testSerializationWithNullValues() throws IOException { |
| 38 | + ShardIngestionState state = new ShardIngestionState("index1", 0, null, null, false); |
| 39 | + |
| 40 | + try (BytesStreamOutput out = new BytesStreamOutput()) { |
| 41 | + state.writeTo(out); |
| 42 | + |
| 43 | + try (StreamInput in = out.bytes().streamInput()) { |
| 44 | + ShardIngestionState deserializedState = new ShardIngestionState(in); |
| 45 | + assertEquals(state.getIndex(), deserializedState.getIndex()); |
| 46 | + assertEquals(state.getShardId(), deserializedState.getShardId()); |
| 47 | + assertNull(deserializedState.getPollerState()); |
| 48 | + assertEquals(state.isPollerPaused(), deserializedState.isPollerPaused()); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public void testGroupShardStateByIndex() { |
| 54 | + ShardIngestionState[] states = new ShardIngestionState[] { |
| 55 | + new ShardIngestionState("index1", 0, "POLLING", "DROP", true), |
| 56 | + new ShardIngestionState("index1", 1, "PAUSED", "DROP", false), |
| 57 | + new ShardIngestionState("index2", 0, "POLLING", "DROP", true) }; |
| 58 | + |
| 59 | + Map<String, List<ShardIngestionState>> groupedStates = ShardIngestionState.groupShardStateByIndex(states); |
| 60 | + |
| 61 | + assertEquals(2, groupedStates.size()); |
| 62 | + assertEquals(2, groupedStates.get("index1").size()); |
| 63 | + assertEquals(1, groupedStates.get("index2").size()); |
| 64 | + |
| 65 | + // Verify index1 shards |
| 66 | + List<ShardIngestionState> indexStates1 = groupedStates.get("index1"); |
| 67 | + assertEquals(0, indexStates1.get(0).getShardId()); |
| 68 | + assertEquals(1, indexStates1.get(1).getShardId()); |
| 69 | + |
| 70 | + // Verify index2 shards |
| 71 | + List<ShardIngestionState> indexStates2 = groupedStates.get("index2"); |
| 72 | + assertEquals(0, indexStates2.get(0).getShardId()); |
| 73 | + } |
| 74 | +} |
0 commit comments