Skip to content

Commit

Permalink
fix(test): add mock method to mock credentials. (#3312)
Browse files Browse the repository at this point in the history
This change is to avoid runtime error when creating the client bean #3307 (comment)
Adding a mock return for getMetricsCredentialType() because in gax we do not allow for null values (should not happen because default is set)

Also added together in this pr so ci could pass:

libraries bom upgrade (ci would still fail due to removal of rpcs in this version)
generation workflow

BEGIN_COMMIT_OVERRIDE
fix(deps): update dependency com.google.cloud:libraries-bom to v26.49.0

fix(test): add mock method to mock credentials.
END_COMMIT_OVERRIDE

---------

Co-authored-by: Cloud Java Bot <[email protected]>
  • Loading branch information
zhumin8 and cloud-java-bot authored Oct 23, 2024
1 parent 33ef14e commit e838c64
Show file tree
Hide file tree
Showing 33 changed files with 832 additions and 240 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.spring.autoconfigure;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.google.auth.CredentialTypeForMetrics;
import com.google.auth.Credentials;

public class TestUtils {
public static final Credentials MOCK_CREDENTIALS = createMockCredentials();

private static Credentials createMockCredentials() {
Credentials mockCredential = mock(Credentials.class);
when(mockCredential.getMetricsCredentialType())
.thenReturn(CredentialTypeForMetrics.DO_NOT_SEND);
return mockCredential;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
package com.google.cloud.spring.autoconfigure.bigquery;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

import com.google.api.gax.core.CredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.storage.v1.BigQueryWriteClient;
import com.google.cloud.spring.autoconfigure.TestUtils;
import com.google.cloud.spring.autoconfigure.core.GcpContextAutoConfiguration;
import com.google.cloud.spring.bigquery.core.BigQueryTemplate;
import org.junit.jupiter.api.Test;
Expand All @@ -34,8 +33,6 @@

class GcpBigQueryAutoConfigurationTests {

private static final GoogleCredentials MOCK_CREDENTIALS = mock(GoogleCredentials.class);

private ApplicationContextRunner contextRunner =
new ApplicationContextRunner()
.withConfiguration(
Expand All @@ -52,7 +49,7 @@ void testSettingBigQueryOptions() {
context -> {
BigQueryOptions bigQueryOptions = context.getBean(BigQuery.class).getOptions();
assertThat(bigQueryOptions.getProjectId()).isEqualTo("test-project");
assertThat(bigQueryOptions.getCredentials()).isEqualTo(MOCK_CREDENTIALS);
assertThat(bigQueryOptions.getCredentials()).isEqualTo(TestUtils.MOCK_CREDENTIALS);

BigQueryTemplate bigQueryTemplate = context.getBean(BigQueryTemplate.class);
assertThat(bigQueryTemplate.getDatasetName()).isEqualTo("test-dataset");
Expand Down Expand Up @@ -168,7 +165,7 @@ static class TestConfiguration {

@Bean
public CredentialsProvider credentialsProvider() {
return () -> MOCK_CREDENTIALS;
return () -> TestUtils.MOCK_CREDENTIALS;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
package com.google.cloud.spring.autoconfigure.core;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

import com.google.api.gax.core.CredentialsProvider;
import com.google.auth.Credentials;
import com.google.cloud.spring.autoconfigure.TestUtils;
import com.google.cloud.spring.core.DefaultGcpEnvironmentProvider;
import com.google.cloud.spring.core.DefaultGcpProjectIdProvider;
import com.google.cloud.spring.core.GcpEnvironmentProvider;
Expand Down Expand Up @@ -99,7 +98,7 @@ private static class TestConfiguration {

@Bean
public CredentialsProvider googleCredentials() {
return () -> mock(Credentials.class);
return () -> TestUtils.MOCK_CREDENTIALS;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import static org.mockito.Mockito.mock;

import com.google.api.gax.core.CredentialsProvider;
import com.google.auth.Credentials;
import com.google.cloud.spring.autoconfigure.TestUtils;
import com.google.cloud.spring.autoconfigure.core.GcpContextAutoConfiguration;
import com.google.cloud.spring.autoconfigure.datastore.GcpDatastoreAutoConfiguration;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -61,7 +61,7 @@ static class TestConfiguration {

@Bean
public CredentialsProvider credentialsProvider() {
return () -> mock(Credentials.class);
return () -> TestUtils.MOCK_CREDENTIALS;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.Mockito.mock;

import com.google.api.gax.core.CredentialsProvider;
import com.google.auth.Credentials;
import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreException;
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.EntityQuery;
import com.google.cloud.datastore.Query;
import com.google.cloud.datastore.StructuredQuery;
import com.google.cloud.spring.autoconfigure.TestUtils;
import com.google.cloud.spring.autoconfigure.core.GcpContextAutoConfiguration;
import com.google.cloud.spring.autoconfigure.datastore.DatastoreRepositoriesAutoConfiguration;
import com.google.cloud.spring.autoconfigure.datastore.DatastoreTransactionManagerAutoConfiguration;
Expand Down Expand Up @@ -118,7 +117,7 @@ static class TestConfiguration {

@Bean
public CredentialsProvider credentialsProvider() {
return () -> mock(Credentials.class);
return () -> TestUtils.MOCK_CREDENTIALS;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;

import com.google.api.gax.core.CredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.FirestoreOptions;
import com.google.cloud.spring.autoconfigure.TestUtils;
import com.google.cloud.spring.autoconfigure.core.GcpContextAutoConfiguration;
import com.google.firestore.v1.FirestoreGrpc;
import io.grpc.ClientInterceptor;
Expand Down Expand Up @@ -136,7 +135,7 @@ static class TestConfiguration {

@Bean
public CredentialsProvider credentialsProvider() {
return () -> mock(GoogleCredentials.class);
return () -> TestUtils.MOCK_CREDENTIALS;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.mockito.Mockito.when;

import com.google.api.gax.core.CredentialsProvider;
import com.google.auth.CredentialTypeForMetrics;
import com.google.auth.Credentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.auth.oauth2.UserCredentials;
Expand Down Expand Up @@ -172,6 +173,7 @@ static class TestConfiguration {
public static CredentialsProvider googleCredentials() {
UserCredentials mockUserCredential = mock(UserCredentials.class);
when(mockUserCredential.getClientId()).thenReturn(CORE_CREDENTIAL_CLIENT_ID);
when(mockUserCredential.getMetricsCredentialType()).thenReturn(CredentialTypeForMetrics.DO_NOT_SEND);
return () -> mockUserCredential;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
package com.google.cloud.spring.autoconfigure.logging;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

import com.google.api.gax.core.CredentialsProvider;
import com.google.auth.Credentials;
import com.google.cloud.spring.autoconfigure.TestUtils;
import com.google.cloud.spring.autoconfigure.core.GcpContextAutoConfiguration;
import com.google.cloud.spring.autoconfigure.trace.StackdriverTraceAutoConfiguration;
import com.google.cloud.spring.logging.TraceIdLoggingWebMvcInterceptor;
Expand Down Expand Up @@ -105,7 +104,7 @@ private static class TestConfiguration {

@Bean
public CredentialsProvider googleCredentials() {
return () -> mock(Credentials.class);
return () -> TestUtils.MOCK_CREDENTIALS;
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.rpc.StatusCode.Code;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.auth.Credentials;
import com.google.cloud.pubsub.v1.Publisher;
import com.google.cloud.pubsub.v1.Subscriber;
import com.google.cloud.spring.autoconfigure.TestUtils;
import com.google.cloud.spring.core.GcpProjectIdProvider;
import com.google.cloud.spring.pubsub.core.PubSubConfiguration;
import com.google.cloud.spring.pubsub.core.publisher.PublisherCustomizer;
Expand Down Expand Up @@ -1459,7 +1459,7 @@ public GcpProjectIdProvider projectIdProvider() {

@Bean
public CredentialsProvider googleCredentials() {
return () -> mock(Credentials.class);
return () -> TestUtils.MOCK_CREDENTIALS;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.auth.Credentials;
import com.google.cloud.pubsub.v1.TopicAdminSettings;
import com.google.cloud.spring.autoconfigure.TestUtils;
import com.google.cloud.spring.autoconfigure.core.GcpContextAutoConfiguration;
import com.google.cloud.spring.pubsub.core.PubSubConfiguration;
import org.assertj.core.data.Offset;
Expand Down Expand Up @@ -187,7 +188,7 @@ void testPublisherBatchingSettings() {
private static class TestConfiguration {
@Bean
public CredentialsProvider googleCredentials() {
return () -> mock(Credentials.class);
return () -> TestUtils.MOCK_CREDENTIALS;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.google.api.gax.core.CredentialsProvider;
import com.google.auth.Credentials;
import com.google.cloud.spring.autoconfigure.TestUtils;
import com.google.cloud.spring.core.GcpProjectIdProvider;
import com.google.cloud.spring.pubsub.core.subscriber.PubSubSubscriberOperations;
import com.google.cloud.spring.pubsub.core.subscriber.PubSubSubscriberTemplate;
Expand Down Expand Up @@ -165,7 +166,7 @@ public GcpProjectIdProvider projectIdProvider() {

@Bean
public CredentialsProvider googleCredentials() {
return () -> mock(Credentials.class);
return () -> TestUtils.MOCK_CREDENTIALS;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import com.google.api.gax.core.CredentialsProvider;
import com.google.auth.Credentials;
import com.google.cloud.spring.autoconfigure.TestUtils;
import com.google.cloud.spring.autoconfigure.pubsub.GcpPubSubAutoConfiguration;
import com.google.cloud.spring.core.GcpProjectIdProvider;
import com.google.cloud.spring.pubsub.core.PubSubTemplate;
Expand Down Expand Up @@ -55,7 +56,7 @@ class PubSubHealthIndicatorAutoConfigurationTests {
AutoConfigurations.of(
PubSubHealthIndicatorAutoConfiguration.class, GcpPubSubAutoConfiguration.class))
.withBean(GcpProjectIdProvider.class, () -> () -> "fake project")
.withBean(CredentialsProvider.class, () -> () -> mock(Credentials.class));
.withBean(CredentialsProvider.class, () -> () -> TestUtils.MOCK_CREDENTIALS);

@SuppressWarnings("unchecked")
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.api.gax.core.CredentialsProvider;
import com.google.auth.Credentials;
import com.google.cloud.monitoring.v3.MetricServiceClient;
import com.google.cloud.spring.autoconfigure.TestUtils;
import com.google.cloud.spring.autoconfigure.pubsub.GcpPubSubAutoConfiguration;
import com.google.cloud.spring.core.GcpProjectIdProvider;
import org.junit.jupiter.api.Test;
Expand All @@ -40,7 +41,7 @@ class PubSubSubscriptionHealthIndicatorAutoConfigurationTests {
PubSubSubscriptionHealthIndicatorAutoConfiguration.class,
GcpPubSubAutoConfiguration.class))
.withBean(GcpProjectIdProvider.class, () -> () -> "fake project")
.withBean(CredentialsProvider.class, () -> () -> mock(Credentials.class))
.withBean(CredentialsProvider.class, () -> () -> TestUtils.MOCK_CREDENTIALS)
.withBean(MetricServiceClient.class, () -> mock(MetricServiceClient.class));

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.api.gax.core.CredentialsProvider;
import com.google.auth.Credentials;
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
import com.google.cloud.spring.autoconfigure.TestUtils;
import com.google.cloud.spring.autoconfigure.core.GcpContextAutoConfiguration;
import com.google.cloud.spring.secretmanager.SecretManagerTemplate;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -77,7 +78,7 @@ static class TestConfig {

@Bean
public CredentialsProvider googleCredentials() {
return () -> mock(Credentials.class);
return () -> TestUtils.MOCK_CREDENTIALS;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.google.api.gax.core.CredentialsProvider;
import com.google.auth.Credentials;
import com.google.cloud.spring.autoconfigure.TestUtils;
import com.google.cloud.spring.core.GcpProjectIdProvider;
import com.google.cloud.spring.security.firebase.FirebaseJwtTokenDecoder;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -72,7 +73,7 @@ public GcpProjectIdProvider projectIdProvider() {

@Bean
public CredentialsProvider googleCredentials() {
return () -> mock(Credentials.class);
return () -> TestUtils.MOCK_CREDENTIALS;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@

import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.retrying.RetrySettings;
import com.google.auth.CredentialTypeForMetrics;
import com.google.auth.Credentials;
import com.google.cloud.NoCredentials;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spring.autoconfigure.TestUtils;
import com.google.cloud.spring.autoconfigure.core.GcpContextAutoConfiguration;
import com.google.cloud.spring.data.spanner.core.SpannerOperations;
import com.google.cloud.spring.data.spanner.core.SpannerTransactionManager;
Expand Down Expand Up @@ -186,7 +188,7 @@ static class TestConfiguration {
/** Mock bean for credentials provider. */
@Bean
public CredentialsProvider credentialsProvider() {
return () -> mock(Credentials.class);
return () -> TestUtils.MOCK_CREDENTIALS;
}

/** Mock bean for Gson. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.api.gax.core.CredentialsProvider;
import com.google.auth.Credentials;
import com.google.cloud.spanner.Statement;
import com.google.cloud.spring.autoconfigure.TestUtils;
import com.google.cloud.spring.autoconfigure.core.GcpContextAutoConfiguration;
import com.google.cloud.spring.autoconfigure.spanner.GcpSpannerAutoConfiguration;
import com.google.gson.Gson;
Expand Down Expand Up @@ -94,7 +95,7 @@ static class TestConfiguration {

@Bean
public CredentialsProvider credentialsProvider() {
return () -> mock(Credentials.class);
return () -> TestUtils.MOCK_CREDENTIALS;
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.google.api.gax.core.CredentialsProvider;
import com.google.auth.Credentials;
import com.google.cloud.spring.autoconfigure.TestUtils;
import com.google.cloud.spring.core.GcpProjectIdProvider;
import com.google.cloud.spring.storage.GoogleStorageResource;
import com.google.cloud.storage.Blob;
Expand Down Expand Up @@ -149,7 +150,7 @@ static class TestConfiguration {

@Bean
public static CredentialsProvider googleCredentials() {
return () -> mock(Credentials.class);
return () -> TestUtils.MOCK_CREDENTIALS;
}

@Bean
Expand Down
2 changes: 1 addition & 1 deletion spring-cloud-gcp-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</distributionManagement>

<properties>
<gcp-libraries-bom.version>26.48.0</gcp-libraries-bom.version>
<gcp-libraries-bom.version>26.49.0</gcp-libraries-bom.version>
<cloud-sql-socket-factory.version>1.21.0</cloud-sql-socket-factory.version>
<r2dbc-postgres-driver.version>1.0.7.RELEASE</r2dbc-postgres-driver.version>
<cloud-spanner-r2dbc.version>1.3.0</cloud-spanner-r2dbc.version>
Expand Down
Loading

0 comments on commit e838c64

Please sign in to comment.