42
42
import org .opensearch .index .engine .Segment ;
43
43
import org .opensearch .test .OpenSearchTestCase ;
44
44
45
+ import java .util .ArrayList ;
45
46
import java .util .Collections ;
47
+ import java .util .List ;
48
+ import java .util .Map ;
46
49
47
50
import static org .opensearch .common .xcontent .XContentFactory .jsonBuilder ;
48
51
@@ -68,4 +71,54 @@ public void testToXContentSerialiationWithSortedFields() throws Exception {
68
71
response .toXContent (builder , ToXContent .EMPTY_PARAMS );
69
72
}
70
73
}
74
+
75
+ public void testGetIndices () {
76
+ final int totalIndices = 5 ;
77
+ final int shardsPerIndex = 3 ;
78
+ final int segmentsPerShard = 2 ;
79
+ // Preparing a ShardSegments list, which will have (totalIndices * shardsPerIndex) shardSegments.
80
+ // Indices will be named -> foo1, foo2, ..., foo{totalIndices}
81
+ List <ShardSegments > shardSegmentsList = new ArrayList <>();
82
+ for (int indexName = 0 ; indexName < totalIndices ; indexName ++) {
83
+ for (int shardId = 0 ; shardId < shardsPerIndex ; shardId ++) {
84
+ ShardRouting shardRouting = TestShardRouting .newShardRouting (
85
+ "foo" + indexName ,
86
+ shardId ,
87
+ "node_id" ,
88
+ true ,
89
+ ShardRoutingState .STARTED
90
+ );
91
+ List <Segment > segmentList = new ArrayList <>();
92
+ for (int segmentNum = 0 ; segmentNum < segmentsPerShard ; segmentNum ++) {
93
+ segmentList .add (new Segment ("foo" + indexName + shardId + segmentNum ));
94
+ }
95
+ shardSegmentsList .add (new ShardSegments (shardRouting , segmentList ));
96
+ }
97
+ }
98
+ Collections .shuffle (shardSegmentsList , random ());
99
+
100
+ // Prepare the IndicesSegmentResponse object and get the indicesSegments map
101
+ IndicesSegmentResponse response = new IndicesSegmentResponse (
102
+ shardSegmentsList .toArray (new ShardSegments [0 ]),
103
+ totalIndices * shardsPerIndex ,
104
+ totalIndices * shardsPerIndex ,
105
+ 0 ,
106
+ Collections .emptyList ()
107
+ );
108
+ Map <String , IndexSegments > indicesSegments = response .getIndices ();
109
+
110
+ assertEquals (totalIndices , indicesSegments .size ());
111
+ for (Map .Entry <String , IndexSegments > indexSegmentEntry : indicesSegments .entrySet ()) {
112
+ assertEquals (shardsPerIndex , indexSegmentEntry .getValue ().getShards ().size ());
113
+ for (IndexShardSegments indexShardSegment : indexSegmentEntry .getValue ().getShards ().values ()) {
114
+ for (ShardSegments shardSegment : indexShardSegment .getShards ()) {
115
+ assertEquals (segmentsPerShard , shardSegment .getSegments ().size ());
116
+ for (int i = 0 ; i < segmentsPerShard ; i ++) {
117
+ String segmentName = indexSegmentEntry .getKey () + shardSegment .getShardRouting ().getId () + i ;
118
+ assertEquals (segmentName , shardSegment .getSegments ().get (i ).getName ());
119
+ }
120
+ }
121
+ }
122
+ }
123
+ }
71
124
}
0 commit comments