99
1010package org .elasticsearch .rest .root ;
1111
12- import org .elasticsearch .action .ActionListener ;
1312import org .elasticsearch .action .support .ActionFilters ;
13+ import org .elasticsearch .action .support .ActionTestUtils ;
1414import org .elasticsearch .cluster .ClusterName ;
1515import org .elasticsearch .cluster .ClusterState ;
1616import org .elasticsearch .cluster .block .ClusterBlock ;
1717import org .elasticsearch .cluster .block .ClusterBlockLevel ;
1818import org .elasticsearch .cluster .block .ClusterBlocks ;
19+ import org .elasticsearch .cluster .metadata .Metadata ;
1920import org .elasticsearch .cluster .service .ClusterService ;
2021import org .elasticsearch .common .settings .Settings ;
2122import org .elasticsearch .rest .RestStatus ;
2627import org .elasticsearch .transport .TransportService ;
2728
2829import java .util .Collections ;
29- import java .util .concurrent .atomic .AtomicReference ;
30+ import java .util .concurrent .atomic .AtomicBoolean ;
3031
3132import static org .mockito .Mockito .mock ;
3233import static org .mockito .Mockito .times ;
@@ -39,7 +40,7 @@ public void testMainActionClusterAvailable() {
3940 final ClusterService clusterService = mock (ClusterService .class );
4041 final ClusterName clusterName = new ClusterName ("elasticsearch" );
4142 final Settings settings = Settings .builder ().put ("node.name" , "my-node" ).build ();
42- ClusterBlocks blocks ;
43+ final ClusterBlocks blocks ;
4344 if (randomBoolean ()) {
4445 if (randomBoolean ()) {
4546 blocks = ClusterBlocks .EMPTY_CLUSTER_BLOCK ;
@@ -73,7 +74,12 @@ public void testMainActionClusterAvailable() {
7374 )
7475 .build ();
7576 }
76- ClusterState state = ClusterState .builder (clusterName ).blocks (blocks ).build ();
77+ final Metadata .Builder metadata = new Metadata .Builder ();
78+ if (randomBoolean ()) {
79+ metadata .clusterUUID (randomUUID ());
80+ metadata .clusterUUIDCommitted (randomBoolean ());
81+ }
82+ final ClusterState state = ClusterState .builder (clusterName ).metadata (metadata ).blocks (blocks ).build ();
7783 when (clusterService .state ()).thenReturn (state );
7884
7985 TransportService transportService = new TransportService (
@@ -85,21 +91,21 @@ public void testMainActionClusterAvailable() {
8591 null ,
8692 Collections .emptySet ()
8793 );
88- TransportMainAction action = new TransportMainAction ( settings , transportService , mock ( ActionFilters . class ), clusterService );
89- AtomicReference < MainResponse > responseRef = new AtomicReference <>();
90- action . doExecute ( mock (Task .class ), new MainRequest (), new ActionListener <>() {
91- @ Override
92- public void onResponse ( MainResponse mainResponse ) {
93- responseRef . set (mainResponse );
94- }
95-
96- @ Override
97- public void onFailure ( Exception e ) {
98- logger . error ( "unexpected error" , e );
99- }
100- } );
94+ final AtomicBoolean listenerCalled = new AtomicBoolean ( );
95+ new TransportMainAction ( settings , transportService , mock ( ActionFilters . class ), clusterService ). doExecute (
96+ mock (Task .class ),
97+ new MainRequest (),
98+ ActionTestUtils . assertNoFailureListener ( mainResponse -> {
99+ assertNotNull (mainResponse );
100+ assertEquals (
101+ state . metadata (). clusterUUIDCommitted () ? state . metadata (). clusterUUID () : Metadata . UNKNOWN_CLUSTER_UUID ,
102+ mainResponse . getClusterUuid ()
103+ );
104+ assertFalse ( listenerCalled . getAndSet ( true ) );
105+ })
106+ );
101107
102- assertNotNull ( responseRef .get ());
108+ assertTrue ( listenerCalled .get ());
103109 verify (clusterService , times (1 )).state ();
104110 }
105111}
0 commit comments