From b679a72ff0bec51d0df240cb120d1436959c9aaf Mon Sep 17 00:00:00 2001 From: fahadshamiinsta Date: Mon, 4 Dec 2023 15:58:11 +1100 Subject: [PATCH] reusing variables Signed-off-by: fahadshamiinsta --- .../GoogleApplicationDefaultCredentials.java | 1 - .../gcs/GoogleCloudStorageServiceTests.java | 33 +++++++------------ 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/plugins/repository-gcs/src/main/java/org/opensearch/repositories/gcs/GoogleApplicationDefaultCredentials.java b/plugins/repository-gcs/src/main/java/org/opensearch/repositories/gcs/GoogleApplicationDefaultCredentials.java index c07c430c259a7..1bb2b8676aafb 100644 --- a/plugins/repository-gcs/src/main/java/org/opensearch/repositories/gcs/GoogleApplicationDefaultCredentials.java +++ b/plugins/repository-gcs/src/main/java/org/opensearch/repositories/gcs/GoogleApplicationDefaultCredentials.java @@ -9,7 +9,6 @@ package org.opensearch.repositories.gcs; import com.google.auth.oauth2.GoogleCredentials; -import com.google.cloud.storage.StorageOptions; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; diff --git a/plugins/repository-gcs/src/test/java/org/opensearch/repositories/gcs/GoogleCloudStorageServiceTests.java b/plugins/repository-gcs/src/test/java/org/opensearch/repositories/gcs/GoogleCloudStorageServiceTests.java index 931492fd16921..601d8f6ac47d1 100644 --- a/plugins/repository-gcs/src/test/java/org/opensearch/repositories/gcs/GoogleCloudStorageServiceTests.java +++ b/plugins/repository-gcs/src/test/java/org/opensearch/repositories/gcs/GoogleCloudStorageServiceTests.java @@ -63,16 +63,17 @@ public class GoogleCloudStorageServiceTests extends OpenSearchTestCase { + final TimeValue connectTimeValue = TimeValue.timeValueNanos(randomIntBetween(0, 2000000)); + final TimeValue readTimeValue = TimeValue.timeValueNanos(randomIntBetween(0, 2000000)); + final String applicationName = randomAlphaOfLength(randomIntBetween(1, 10)).toLowerCase(Locale.ROOT); + final String endpoint = randomFrom("http://", "https://") + + randomFrom("www.opensearch.org", "www.googleapis.com", "localhost/api", "google.com/oauth") + + ":" + + randomIntBetween(1, 65535); + final String projectIdName = randomAlphaOfLength(randomIntBetween(1, 10)).toLowerCase(Locale.ROOT); + public void testClientInitializer() throws Exception { final String clientName = randomAlphaOfLength(randomIntBetween(1, 10)).toLowerCase(Locale.ROOT); - final TimeValue connectTimeValue = TimeValue.timeValueNanos(randomIntBetween(0, 2000000)); - final TimeValue readTimeValue = TimeValue.timeValueNanos(randomIntBetween(0, 2000000)); - final String applicationName = randomAlphaOfLength(randomIntBetween(1, 10)).toLowerCase(Locale.ROOT); - final String endpoint = randomFrom("http://", "https://") - + randomFrom("www.opensearch.org", "www.googleapis.com", "localhost/api", "google.com/oauth") - + ":" - + randomIntBetween(1, 65535); - final String projectIdName = randomAlphaOfLength(randomIntBetween(1, 10)).toLowerCase(Locale.ROOT); final Settings settings = Settings.builder() .put( GoogleCloudStorageClientSettings.CONNECT_TIMEOUT_SETTING.getConcreteSettingForNamespace(clientName).getKey(), @@ -205,8 +206,8 @@ public void testToTimeout() { assertEquals(0, GoogleCloudStorageService.toTimeout(TimeValue.MINUS_ONE).intValue()); } /* - The following methods test the Google Application Default Credential instead of - service account file. + The following method test the Google Application Default Credential instead of + using service account file. Considered use of Junit Mocking due to static method GoogleCredentials.getApplicationDefault and avoiding environment variables to set which later use GCE. */ @@ -221,7 +222,6 @@ public void testApplicationDefaultCredential() throws Exception { StorageOptions storageOptions = service.createStorageOptions(settings,mockHttpTransportOptions); assertNotNull(storageOptions); assertEquals(storageOptions.getCredentials().toString(),mockGoogleCredentials.toString()); - } /** @@ -243,16 +243,7 @@ public void applicationDefaultCredentialThrowsErrorWhenNotAvailable() throws Exc assertThat(exception.getMessage(),containsString("The Application Default Credentials are not available")); } - private static GoogleCloudStorageClientSettings getGoogleCloudStorageClientSettings() throws URISyntaxException { - final TimeValue connectTimeValue = TimeValue.timeValueNanos(randomIntBetween(0, 2000000)); - final TimeValue readTimeValue = TimeValue.timeValueNanos(randomIntBetween(0, 2000000)); - final String applicationName = randomAlphaOfLength(randomIntBetween(1, 10)).toLowerCase(Locale.ROOT); - final String endpoint = randomFrom("http://", "https://") - + randomFrom("www.opensearch.org", "www.googleapis.com", "localhost/api", "google.com/oauth") - + ":" - + randomIntBetween(1, 65535); - final String projectIdName = randomAlphaOfLength(randomIntBetween(1, 10)).toLowerCase(Locale.ROOT); - + private GoogleCloudStorageClientSettings getGoogleCloudStorageClientSettings() throws URISyntaxException { return new GoogleCloudStorageClientSettings( null,endpoint,projectIdName, connectTimeValue,readTimeValue,applicationName,