1919
2020package org .elasticsearch .repositories .gcs ;
2121
22- import java .io .IOException ;
23- import java .io .InputStream ;
24- import java .util .Collections ;
25- import java .util .Map ;
26-
2722import com .google .api .client .googleapis .auth .oauth2 .GoogleCredential ;
23+ import com .google .api .client .http .GenericUrl ;
24+ import com .google .api .client .http .HttpIOExceptionHandler ;
25+ import com .google .api .client .http .HttpRequest ;
26+ import com .google .api .client .http .HttpRequestFactory ;
27+ import com .google .api .client .http .HttpRequestInitializer ;
28+ import com .google .api .client .http .HttpResponse ;
29+ import com .google .api .client .http .HttpUnsuccessfulResponseHandler ;
30+ import com .google .api .client .testing .http .MockHttpTransport ;
2831import org .elasticsearch .common .settings .Settings ;
32+ import org .elasticsearch .common .unit .TimeValue ;
2933import org .elasticsearch .env .Environment ;
3034import org .elasticsearch .env .TestEnvironment ;
3135import org .elasticsearch .repositories .gcs .GoogleCloudStorageService .InternalGoogleCloudStorageService ;
3236import org .elasticsearch .test .ESTestCase ;
3337
38+ import java .io .IOException ;
39+ import java .io .InputStream ;
40+ import java .util .Collections ;
41+ import java .util .Map ;
42+
43+ import static java .util .Collections .emptyMap ;
44+ import static java .util .Collections .singletonMap ;
45+ import static org .mockito .Matchers .any ;
46+ import static org .mockito .Matchers .anyBoolean ;
47+ import static org .mockito .Mockito .mock ;
48+ import static org .mockito .Mockito .times ;
49+ import static org .mockito .Mockito .verify ;
50+ import static org .mockito .Mockito .when ;
51+
3452public class GoogleCloudStorageServiceTests extends ESTestCase {
3553
3654 private InputStream getDummyCredentialStream () throws IOException {
@@ -47,13 +65,56 @@ GoogleCredential getDefaultCredential() throws IOException {
4765 }
4866 };
4967 assertSame (cred , service .getCredential ("default" ));
68+
69+ service .new DefaultHttpRequestInitializer (cred , null , null );
5070 }
5171
5272 public void testClientCredential () throws Exception {
5373 GoogleCredential cred = GoogleCredential .fromStream (getDummyCredentialStream ());
54- Map <String , GoogleCredential > credentials = Collections . singletonMap ("clientname" , cred );
74+ Map <String , GoogleCredential > credentials = singletonMap ("clientname" , cred );
5575 Environment env = TestEnvironment .newEnvironment (Settings .builder ().put ("path.home" , createTempDir ()).build ());
5676 InternalGoogleCloudStorageService service = new InternalGoogleCloudStorageService (env , credentials );
5777 assertSame (cred , service .getCredential ("clientname" ));
5878 }
79+
80+ /**
81+ * Test that the {@link InternalGoogleCloudStorageService.DefaultHttpRequestInitializer} attaches new instances
82+ * of {@link HttpIOExceptionHandler} and {@link HttpUnsuccessfulResponseHandler} for every HTTP requests.
83+ */
84+ public void testDefaultHttpRequestInitializer () throws IOException {
85+ final Environment environment = mock (Environment .class );
86+ when (environment .settings ()).thenReturn (Settings .EMPTY );
87+
88+ final GoogleCredential credential = mock (GoogleCredential .class );
89+ when (credential .handleResponse (any (HttpRequest .class ), any (HttpResponse .class ), anyBoolean ())).thenReturn (false );
90+
91+ final TimeValue readTimeout = TimeValue .timeValueSeconds (randomIntBetween (1 , 120 ));
92+ final TimeValue connectTimeout = TimeValue .timeValueSeconds (randomIntBetween (1 , 120 ));
93+
94+ final InternalGoogleCloudStorageService service = new InternalGoogleCloudStorageService (environment , emptyMap ());
95+ final HttpRequestInitializer initializer = service .new DefaultHttpRequestInitializer (credential , connectTimeout , readTimeout );
96+ final HttpRequestFactory requestFactory = new MockHttpTransport ().createRequestFactory (initializer );
97+
98+ final HttpRequest request1 = requestFactory .buildGetRequest (new GenericUrl ());
99+ assertEquals ((int ) connectTimeout .millis (), request1 .getConnectTimeout ());
100+ assertEquals ((int ) readTimeout .millis (), request1 .getReadTimeout ());
101+ assertSame (credential , request1 .getInterceptor ());
102+ assertNotNull (request1 .getIOExceptionHandler ());
103+ assertNotNull (request1 .getUnsuccessfulResponseHandler ());
104+
105+ final HttpRequest request2 = requestFactory .buildGetRequest (new GenericUrl ());
106+ assertEquals ((int ) connectTimeout .millis (), request2 .getConnectTimeout ());
107+ assertEquals ((int ) readTimeout .millis (), request2 .getReadTimeout ());
108+ assertSame (request1 .getInterceptor (), request2 .getInterceptor ());
109+ assertNotNull (request2 .getIOExceptionHandler ());
110+ assertNotSame (request1 .getIOExceptionHandler (), request2 .getIOExceptionHandler ());
111+ assertNotNull (request2 .getUnsuccessfulResponseHandler ());
112+ assertNotSame (request1 .getUnsuccessfulResponseHandler (), request2 .getUnsuccessfulResponseHandler ());
113+
114+ request1 .getUnsuccessfulResponseHandler ().handleResponse (null , null , false );
115+ verify (credential , times (1 )).handleResponse (any (HttpRequest .class ), any (HttpResponse .class ), anyBoolean ());
116+
117+ request2 .getUnsuccessfulResponseHandler ().handleResponse (null , null , false );
118+ verify (credential , times (2 )).handleResponse (any (HttpRequest .class ), any (HttpResponse .class ), anyBoolean ());
119+ }
59120}
0 commit comments