88package  org .elasticsearch .xpack .inference .services .elastic .authorization ;
99
1010import  org .apache .logging .log4j .Logger ;
11+ import  org .elasticsearch .ElasticsearchException ;
1112import  org .elasticsearch .action .ActionListener ;
1213import  org .elasticsearch .action .support .PlainActionFuture ;
1314import  org .elasticsearch .common .settings .Settings ;
1819import  org .elasticsearch .test .http .MockResponse ;
1920import  org .elasticsearch .test .http .MockWebServer ;
2021import  org .elasticsearch .threadpool .ThreadPool ;
22+ import  org .elasticsearch .xcontent .XContentParseException ;
2123import  org .elasticsearch .xpack .core .inference .results .ChatCompletionResults ;
2224import  org .elasticsearch .xpack .inference .external .http .HttpClientManager ;
2325import  org .elasticsearch .xpack .inference .external .http .sender .HttpRequestSender ;
3840import  static  org .elasticsearch .xpack .inference .Utils .mockClusterServiceEmpty ;
3941import  static  org .elasticsearch .xpack .inference .external .http .Utils .getUrl ;
4042import  static  org .elasticsearch .xpack .inference .external .http .retry .RetryingHttpSender .MAX_RETIES ;
43+ import  static  org .hamcrest .Matchers .containsString ;
44+ import  static  org .hamcrest .Matchers .instanceOf ;
4145import  static  org .hamcrest .Matchers .is ;
4246import  static  org .mockito .ArgumentMatchers .any ;
4347import  static  org .mockito .Mockito .doAnswer ;
4448import  static  org .mockito .Mockito .mock ;
4549import  static  org .mockito .Mockito .times ;
4650import  static  org .mockito .Mockito .verify ;
47- import  static  org .mockito .Mockito .verifyNoMoreInteractions ;
4851import  static  org .mockito .Mockito .when ;
4952
5053public  class  ElasticInferenceServiceAuthorizationRequestHandlerTests  extends  ESTestCase  {
@@ -135,22 +138,17 @@ public void testGetAuthorization_FailsWhenAnInvalidFieldIsFound() throws IOExcep
135138            PlainActionFuture <ElasticInferenceServiceAuthorizationModel > listener  = new  PlainActionFuture <>();
136139            authHandler .getAuthorization (listener , sender );
137140
138-             var  authResponse  = listener .actionGet (TIMEOUT );
139-             assertTrue (authResponse .getAuthorizedTaskTypes ().isEmpty ());
140-             assertTrue (authResponse .getAuthorizedModelIds ().isEmpty ());
141-             assertFalse (authResponse .isAuthorized ());
141+             var  exception  = expectThrows (XContentParseException .class , () -> listener .actionGet (TIMEOUT ));
142+             assertThat (exception .getMessage (), containsString ("failed to parse field [models]" ));
142143
143-             var  loggerArgsCaptor  = ArgumentCaptor .forClass (String .class );
144-             verify (logger ).warn (loggerArgsCaptor .capture ());
145-             var  message  = loggerArgsCaptor .getValue ();
146-             assertThat (
147-                 message ,
148-                 is (
149-                     "Failed to retrieve the authorization information from the Elastic Inference Service." 
150-                         + " Encountered an exception: org.elasticsearch.xcontent.XContentParseException: [4:28] " 
151-                         + "[ElasticInferenceServiceAuthorizationResponseEntity] failed to parse field [models]" 
152-                 )
153-             );
144+             var  stringCaptor  = ArgumentCaptor .forClass (String .class );
145+             var  exceptionCaptor  = ArgumentCaptor .forClass (Exception .class );
146+             verify (logger ).warn (stringCaptor .capture (), exceptionCaptor .capture ());
147+             var  message  = stringCaptor .getValue ();
148+             assertThat (message , containsString ("failed to parse field [models]" ));
149+ 
150+             var  capturedException  = exceptionCaptor .getValue ();
151+             assertThat (capturedException , instanceOf (XContentParseException .class ));
154152        }
155153    }
156154
@@ -196,7 +194,6 @@ public void testGetAuthorization_ReturnsAValidResponse() throws IOException {
196194
197195            var  message  = loggerArgsCaptor .getValue ();
198196            assertThat (message , is ("Retrieving authorization information from the Elastic Inference Service." ));
199-             verifyNoMoreInteractions (logger );
200197        }
201198    }
202199
@@ -230,7 +227,6 @@ public void testGetAuthorization_OnResponseCalledOnce() throws IOException {
230227
231228            var  message  = loggerArgsCaptor .getValue ();
232229            assertThat (message , is ("Retrieving authorization information from the Elastic Inference Service." ));
233-             verifyNoMoreInteractions (logger );
234230        }
235231    }
236232
@@ -252,20 +248,14 @@ public void testGetAuthorization_InvalidResponse() throws IOException {
252248            PlainActionFuture <ElasticInferenceServiceAuthorizationModel > listener  = new  PlainActionFuture <>();
253249
254250            authHandler .getAuthorization (listener , sender );
255-             var  result  = listener .actionGet (TIMEOUT );
251+             var  exception  = expectThrows ( ElasticsearchException . class , () ->  listener .actionGet (TIMEOUT ) );
256252
257-             assertThat (result ,  is ( ElasticInferenceServiceAuthorizationModel . newDisabledService () ));
253+             assertThat (exception . getMessage (),  containsString ( "Received an invalid response type from the Elastic Inference Service" ));
258254
259255            var  loggerArgsCaptor  = ArgumentCaptor .forClass (String .class );
260256            verify (logger ).warn (loggerArgsCaptor .capture ());
261257            var  message  = loggerArgsCaptor .getValue ();
262-             assertThat (
263-                 message ,
264-                 is (
265-                     "Failed to retrieve the authorization information from the Elastic Inference Service." 
266-                         + " Received an invalid response type: ChatCompletionResults" 
267-                 )
268-             );
258+             assertThat (message , containsString ("Failed to retrieve the authorization information from the Elastic Inference Service." ));
269259        }
270260
271261    }
0 commit comments