2525import org .elasticsearch .action .admin .indices .stats .IndicesStatsResponse ;
2626import org .elasticsearch .action .admin .indices .stats .IndicesStatsTests ;
2727import org .elasticsearch .action .admin .indices .stats .ShardStats ;
28+ import org .elasticsearch .action .support .IndicesOptions ;
2829import org .elasticsearch .cluster .ClusterName ;
2930import org .elasticsearch .cluster .ClusterState ;
3031import org .elasticsearch .cluster .metadata .IndexMetaData ;
3839import org .elasticsearch .common .UUIDs ;
3940import org .elasticsearch .common .settings .Settings ;
4041import org .elasticsearch .common .unit .TimeValue ;
41- import org .elasticsearch .index .Index ;
4242import org .elasticsearch .index .cache .query .QueryCacheStats ;
4343import org .elasticsearch .index .cache .request .RequestCacheStats ;
4444import org .elasticsearch .index .engine .SegmentsStats ;
6262
6363import java .nio .file .Path ;
6464import java .util .ArrayList ;
65+ import java .util .Arrays ;
6566import java .util .Collections ;
6667import java .util .List ;
6768
7374 */
7475public class RestIndicesActionTests extends ESTestCase {
7576
76- public void testBuildTable () {
77- final Settings settings = Settings .EMPTY ;
78- UsageService usageService = new UsageService ();
79- final RestController restController = new RestController (Collections .emptySet (), null , null , null , usageService );
80- final RestIndicesAction action = new RestIndicesAction (settings , restController , new IndexNameExpressionResolver ());
81-
77+ private IndexMetaData [] buildRandomIndicesMetaData (int numIndices ) {
8278 // build a (semi-)random table
83- final int numIndices = randomIntBetween (0 , 5 );
84- Index [] indices = new Index [numIndices ];
79+ final IndexMetaData [] indicesMetaData = new IndexMetaData [numIndices ];
8580 for (int i = 0 ; i < numIndices ; i ++) {
86- indices [i ] = new Index (randomAlphaOfLength (5 ), UUIDs .randomBase64UUID ());
87- }
88-
89- final MetaData .Builder metaDataBuilder = MetaData .builder ();
90- for (final Index index : indices ) {
91- metaDataBuilder .put (IndexMetaData .builder (index .getName ())
81+ indicesMetaData [i ] = IndexMetaData .builder (randomAlphaOfLength (5 ) + i )
9282 .settings (Settings .builder ()
93- .put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT )
94- .put (IndexMetaData .SETTING_INDEX_UUID , index . getUUID ()))
83+ .put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT )
84+ .put (IndexMetaData .SETTING_INDEX_UUID , UUIDs . randomBase64UUID ()))
9585 .creationDate (System .currentTimeMillis ())
9686 .numberOfShards (1 )
9787 .numberOfReplicas (1 )
98- .state (IndexMetaData .State .OPEN ));
88+ .state (IndexMetaData .State .OPEN )
89+ .build ();
9990 }
100- final MetaData metaData = metaDataBuilder .build ();
91+ return indicesMetaData ;
92+ }
10193
94+ private ClusterState buildClusterState (IndexMetaData [] indicesMetaData ) {
95+ final MetaData .Builder metaDataBuilder = MetaData .builder ();
96+ for (IndexMetaData indexMetaData : indicesMetaData ) {
97+ metaDataBuilder .put (indexMetaData , false );
98+ }
99+ final MetaData metaData = metaDataBuilder .build ();
102100 final ClusterState clusterState = ClusterState .builder (ClusterName .CLUSTER_NAME_SETTING .getDefault (Settings .EMPTY ))
103101 .metaData (metaData )
104102 .build ();
105- final String [] indicesStr = new String [indices .length ];
106- for (int i = 0 ; i < indices .length ; i ++) {
107- indicesStr [i ] = indices [i ].getName ();
103+ return clusterState ;
104+ }
105+
106+ private ClusterHealthResponse buildClusterHealthResponse (ClusterState clusterState , IndexMetaData [] indicesMetaData ) {
107+ final String [] indicesStr = new String [indicesMetaData .length ];
108+ for (int i = 0 ; i < indicesMetaData .length ; i ++) {
109+ indicesStr [i ] = indicesMetaData [i ].getIndex ().getName ();
108110 }
109- final ClusterHealthResponse clusterHealth = new ClusterHealthResponse (
111+ final ClusterHealthResponse clusterHealthResponse = new ClusterHealthResponse (
110112 clusterState .getClusterName ().value (), indicesStr , clusterState , 0 , 0 , 0 , TimeValue .timeValueMillis (1000L )
111113 );
114+ return clusterHealthResponse ;
115+ }
112116
113- final Table table = action .buildTable (new FakeRestRequest (), indices , clusterHealth , randomIndicesStatsResponse (indices ), metaData );
117+ public void testBuildTable () {
118+ final Settings settings = Settings .EMPTY ;
119+ UsageService usageService = new UsageService ();
120+ final RestController restController = new RestController (Collections .emptySet (), null , null , null , usageService );
121+ final RestIndicesAction action = new RestIndicesAction (settings , restController , new IndexNameExpressionResolver ());
122+
123+ final IndexMetaData [] generatedIndicesMetaData = buildRandomIndicesMetaData (randomIntBetween (1 , 5 ));
124+ final ClusterState clusterState = buildClusterState (generatedIndicesMetaData );
125+ final ClusterHealthResponse clusterHealthResponse = buildClusterHealthResponse (clusterState , generatedIndicesMetaData );
126+
127+ final IndexMetaData [] sortedIndicesMetaData = action .getOrderedIndexMetaData (new String [0 ], clusterState ,
128+ IndicesOptions .strictExpand ());
129+ final IndexMetaData [] smallerSortedIndicesMetaData = removeRandomElement (sortedIndicesMetaData );
130+ final Table table = action .buildTable (new FakeRestRequest (), sortedIndicesMetaData , clusterHealthResponse ,
131+ randomIndicesStatsResponse (smallerSortedIndicesMetaData ));
114132
115133 // now, verify the table is correct
116134 int count = 0 ;
@@ -121,27 +139,27 @@ public void testBuildTable() {
121139 assertThat (headers .get (count ++).value , equalTo ("uuid" ));
122140
123141 List <List <Table .Cell >> rows = table .getRows ();
124- assertThat (rows .size (), equalTo (indices .length ));
142+ assertThat (rows .size (), equalTo (smallerSortedIndicesMetaData .length ));
125143 // TODO: more to verify (e.g. randomize cluster health, num primaries, num replicas, etc)
126144 for (int i = 0 ; i < rows .size (); i ++) {
127145 count = 0 ;
128146 final List <Table .Cell > row = rows .get (i );
129147 assertThat (row .get (count ++).value , equalTo ("red*" )); // all are red because cluster state doesn't have routing entries
130148 assertThat (row .get (count ++).value , equalTo ("open" )); // all are OPEN for now
131- assertThat (row .get (count ++).value , equalTo (indices [i ].getName ()));
132- assertThat (row .get (count ++).value , equalTo (indices [i ].getUUID ()));
149+ assertThat (row .get (count ++).value , equalTo (smallerSortedIndicesMetaData [i ]. getIndex () .getName ()));
150+ assertThat (row .get (count ++).value , equalTo (smallerSortedIndicesMetaData [i ].getIndexUUID ()));
133151 }
134152 }
135153
136- private IndicesStatsResponse randomIndicesStatsResponse (final Index [] indices ) {
154+ private IndicesStatsResponse randomIndicesStatsResponse (final IndexMetaData [] indices ) {
137155 List <ShardStats > shardStats = new ArrayList <>();
138- for (final Index index : indices ) {
139- int numShards = randomInt ( 5 );
156+ for (final IndexMetaData index : indices ) {
157+ int numShards = randomIntBetween ( 1 , 3 );
140158 int primaryIdx = randomIntBetween (-1 , numShards - 1 ); // -1 means there is no primary shard.
141159 for (int i = 0 ; i < numShards ; i ++) {
142- ShardId shardId = new ShardId (index , i );
160+ ShardId shardId = new ShardId (index . getIndex () , i );
143161 boolean primary = (i == primaryIdx );
144- Path path = createTempDir ().resolve ("indices" ).resolve (index .getUUID ()).resolve (String .valueOf (i ));
162+ Path path = createTempDir ().resolve ("indices" ).resolve (index .getIndexUUID ()).resolve (String .valueOf (i ));
145163 ShardRouting shardRouting = ShardRouting .newUnassigned (shardId , primary ,
146164 primary ? RecoverySource .EmptyStoreRecoverySource .INSTANCE : PeerRecoverySource .INSTANCE ,
147165 new UnassignedInfo (UnassignedInfo .Reason .INDEX_CREATED , null )
@@ -170,4 +188,14 @@ private IndicesStatsResponse randomIndicesStatsResponse(final Index[] indices) {
170188 shardStats .toArray (new ShardStats [shardStats .size ()]), shardStats .size (), shardStats .size (), 0 , emptyList ()
171189 );
172190 }
191+
192+ private IndexMetaData [] removeRandomElement (IndexMetaData [] array ) {
193+ assert array != null ;
194+ assert array .length > 0 ;
195+ final List <IndexMetaData > collectionLessAnItem = new ArrayList <>();
196+ collectionLessAnItem .addAll (Arrays .asList (array ));
197+ final int toRemoveIndex = randomIntBetween (0 , array .length - 1 );
198+ collectionLessAnItem .remove (toRemoveIndex );
199+ return collectionLessAnItem .toArray (new IndexMetaData [0 ]);
200+ }
173201}
0 commit comments