diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/BatchClientImplTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/BatchClientImplTest.java index b7c4834044a..75eb899aaa8 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/BatchClientImplTest.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/BatchClientImplTest.java @@ -30,12 +30,17 @@ import com.google.cloud.Timestamp; import com.google.cloud.grpc.GrpcTransportOptions; import com.google.cloud.grpc.GrpcTransportOptions.ExecutorFactory; +import com.google.cloud.spanner.admin.database.v1.stub.DatabaseAdminStub; +import com.google.cloud.spanner.admin.database.v1.stub.DatabaseAdminStubSettings; +import com.google.cloud.spanner.admin.instance.v1.stub.InstanceAdminStub; +import com.google.cloud.spanner.admin.instance.v1.stub.InstanceAdminStubSettings; import com.google.cloud.spanner.spi.v1.SpannerRpc; import com.google.protobuf.ByteString; import com.google.protobuf.util.Timestamps; import com.google.spanner.v1.Session; import com.google.spanner.v1.Transaction; import io.opentelemetry.api.OpenTelemetry; +import java.io.IOException; import java.util.Collections; import java.util.Map; import org.junit.Before; @@ -59,6 +64,10 @@ public final class BatchClientImplTest { @Mock private SpannerRpc gapicRpc; @Mock private SpannerOptions spannerOptions; + @Mock private InstanceAdminStubSettings instanceAdminStubSettings; + @Mock private DatabaseAdminStubSettings databaseAdminStubSettings; + @Mock private DatabaseAdminStub databaseAdminStub; + @Mock private InstanceAdminStub instanceAdminStub; @Captor private ArgumentCaptor> optionsCaptor; @Mock private BatchTransactionId txnID; @@ -72,7 +81,7 @@ public static void setupOpenTelemetry() { @SuppressWarnings("unchecked") @Before - public void setUp() { + public void setUp() throws IOException { initMocks(this); DatabaseId db = DatabaseId.of(DB_NAME); when(spannerOptions.getNumChannels()).thenReturn(4); @@ -86,6 +95,10 @@ public void setUp() { GrpcTransportOptions transportOptions = mock(GrpcTransportOptions.class); when(transportOptions.getExecutorFactory()).thenReturn(mock(ExecutorFactory.class)); when(spannerOptions.getTransportOptions()).thenReturn(transportOptions); + when(instanceAdminStubSettings.createStub()).thenReturn(instanceAdminStub); + when(databaseAdminStubSettings.createStub()).thenReturn(databaseAdminStub); + when(gapicRpc.getInstanceAdminStubSettings()).thenReturn(instanceAdminStubSettings); + when(gapicRpc.getDatabaseAdminStubSettings()).thenReturn(databaseAdminStubSettings); @SuppressWarnings("resource") SpannerImpl spanner = new SpannerImpl(gapicRpc, spannerOptions); client = new BatchClientImpl(spanner.getSessionClient(db)); diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionImplTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionImplTest.java index 87edb64134c..59aa063d99d 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionImplTest.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionImplTest.java @@ -34,6 +34,10 @@ import com.google.cloud.Timestamp; import com.google.cloud.grpc.GrpcTransportOptions; import com.google.cloud.grpc.GrpcTransportOptions.ExecutorFactory; +import com.google.cloud.spanner.admin.database.v1.stub.DatabaseAdminStub; +import com.google.cloud.spanner.admin.database.v1.stub.DatabaseAdminStubSettings; +import com.google.cloud.spanner.admin.instance.v1.stub.InstanceAdminStub; +import com.google.cloud.spanner.admin.instance.v1.stub.InstanceAdminStubSettings; import com.google.cloud.spanner.spi.v1.SpannerRpc; import com.google.cloud.spanner.v1.stub.SpannerStubSettings; import com.google.protobuf.ByteString; @@ -52,6 +56,7 @@ import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.trace.Span; import io.opentelemetry.context.Scope; +import java.io.IOException; import java.text.ParseException; import java.util.Calendar; import java.util.Collections; @@ -76,6 +81,10 @@ public class SessionImplTest { @Mock private SpannerRpc rpc; @Mock private SpannerOptions spannerOptions; + @Mock private InstanceAdminStubSettings instanceAdminStubSettings; + @Mock private DatabaseAdminStubSettings databaseAdminStubSettings; + @Mock private DatabaseAdminStub databaseAdminStub; + @Mock private InstanceAdminStub instanceAdminStub; private com.google.cloud.spanner.Session session; @Captor private ArgumentCaptor> optionsCaptor; private Map options; @@ -88,7 +97,7 @@ public static void setupOpenTelemetry() { @SuppressWarnings("unchecked") @Before - public void setUp() { + public void setUp() throws IOException { MockitoAnnotations.initMocks(this); when(spannerOptions.getNumChannels()).thenReturn(4); when(spannerOptions.getPrefetchChunks()).thenReturn(1); @@ -101,6 +110,10 @@ public void setUp() { when(spannerOptions.getTransportOptions()).thenReturn(transportOptions); when(spannerOptions.getSessionPoolOptions()).thenReturn(mock(SessionPoolOptions.class)); when(spannerOptions.getOpenTelemetry()).thenReturn(OpenTelemetry.noop()); + when(instanceAdminStubSettings.createStub()).thenReturn(instanceAdminStub); + when(databaseAdminStubSettings.createStub()).thenReturn(databaseAdminStub); + when(rpc.getInstanceAdminStubSettings()).thenReturn(instanceAdminStubSettings); + when(rpc.getDatabaseAdminStubSettings()).thenReturn(databaseAdminStubSettings); @SuppressWarnings("resource") SpannerImpl spanner = new SpannerImpl(rpc, spannerOptions); String dbName = "projects/p1/instances/i1/databases/d1"; diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerImplTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerImplTest.java index 31a6cad4c8a..dea2e3e3206 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerImplTest.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerImplTest.java @@ -29,9 +29,14 @@ import com.google.cloud.grpc.GrpcTransportOptions; import com.google.cloud.spanner.SpannerException.DoNotConstructDirectly; import com.google.cloud.spanner.SpannerImpl.ClosedException; +import com.google.cloud.spanner.admin.database.v1.stub.DatabaseAdminStub; +import com.google.cloud.spanner.admin.database.v1.stub.DatabaseAdminStubSettings; +import com.google.cloud.spanner.admin.instance.v1.stub.InstanceAdminStub; +import com.google.cloud.spanner.admin.instance.v1.stub.InstanceAdminStubSettings; import com.google.cloud.spanner.spi.v1.SpannerRpc; import com.google.spanner.v1.ExecuteSqlRequest.QueryOptions; import io.opentelemetry.api.OpenTelemetry; +import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.util.Collections; @@ -55,6 +60,10 @@ public class SpannerImplTest { @Mock private SpannerRpc rpc; @Mock private SpannerOptions spannerOptions; + @Mock private InstanceAdminStubSettings instanceAdminStubSettings; + @Mock private DatabaseAdminStubSettings databaseAdminStubSettings; + @Mock private DatabaseAdminStub databaseAdminStub; + @Mock private InstanceAdminStub instanceAdminStub; private SpannerImpl impl; @Captor ArgumentCaptor> options; @@ -66,7 +75,7 @@ public static void setupOpenTelemetry() { } @Before - public void setUp() { + public void setUp() throws IOException { MockitoAnnotations.initMocks(this); when(spannerOptions.getNumChannels()).thenReturn(4); when(spannerOptions.getDatabaseRole()).thenReturn("role"); @@ -75,6 +84,10 @@ public void setUp() { when(spannerOptions.getClock()).thenReturn(NanoClock.getDefaultClock()); when(spannerOptions.getSessionLabels()).thenReturn(Collections.emptyMap()); when(spannerOptions.getOpenTelemetry()).thenReturn(OpenTelemetry.noop()); + when(instanceAdminStubSettings.createStub()).thenReturn(instanceAdminStub); + when(databaseAdminStubSettings.createStub()).thenReturn(databaseAdminStub); + when(rpc.getInstanceAdminStubSettings()).thenReturn(instanceAdminStubSettings); + when(rpc.getDatabaseAdminStubSettings()).thenReturn(databaseAdminStubSettings); impl = new SpannerImpl(rpc, spannerOptions); } diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/TransactionManagerImplTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/TransactionManagerImplTest.java index dc28b333c4f..fd6f57d484f 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/TransactionManagerImplTest.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/TransactionManagerImplTest.java @@ -34,6 +34,10 @@ import com.google.cloud.grpc.GrpcTransportOptions; import com.google.cloud.grpc.GrpcTransportOptions.ExecutorFactory; import com.google.cloud.spanner.TransactionManager.TransactionState; +import com.google.cloud.spanner.admin.database.v1.stub.DatabaseAdminStub; +import com.google.cloud.spanner.admin.database.v1.stub.DatabaseAdminStubSettings; +import com.google.cloud.spanner.admin.instance.v1.stub.InstanceAdminStub; +import com.google.cloud.spanner.admin.instance.v1.stub.InstanceAdminStubSettings; import com.google.cloud.spanner.spi.v1.SpannerRpc; import com.google.protobuf.ByteString; import com.google.protobuf.Empty; @@ -47,6 +51,7 @@ import com.google.spanner.v1.Session; import com.google.spanner.v1.Transaction; import io.opentelemetry.api.OpenTelemetry; +import java.io.IOException; import java.util.Collections; import java.util.UUID; import java.util.concurrent.Executors; @@ -77,6 +82,10 @@ public void release(ScheduledExecutorService exec) { @Mock private SessionImpl session; @Mock TransactionRunnerImpl.TransactionContextImpl txn; + @Mock private InstanceAdminStubSettings instanceAdminStubSettings; + @Mock private DatabaseAdminStubSettings databaseAdminStubSettings; + @Mock private DatabaseAdminStub databaseAdminStub; + @Mock private InstanceAdminStub instanceAdminStub; private TransactionManagerImpl manager; @BeforeClass @@ -201,7 +210,7 @@ public void commitAfterRollbackFails() { @SuppressWarnings("unchecked") @Test - public void usesPreparedTransaction() { + public void usesPreparedTransaction() throws IOException { SpannerOptions options = mock(SpannerOptions.class); when(options.getNumChannels()).thenReturn(4); GrpcTransportOptions transportOptions = mock(GrpcTransportOptions.class); @@ -248,6 +257,11 @@ public void usesPreparedTransaction() { com.google.protobuf.Timestamp.newBuilder() .setSeconds(System.currentTimeMillis() * 1000)) .build())); + when(instanceAdminStubSettings.createStub()).thenReturn(instanceAdminStub); + when(databaseAdminStubSettings.createStub()).thenReturn(databaseAdminStub); + when(rpc.getInstanceAdminStubSettings()).thenReturn(instanceAdminStubSettings); + when(rpc.getDatabaseAdminStubSettings()).thenReturn(databaseAdminStubSettings); + DatabaseId db = DatabaseId.of("test", "test", "test"); try (SpannerImpl spanner = new SpannerImpl(rpc, options)) { DatabaseClient client = spanner.getDatabaseClient(db); @@ -263,7 +277,7 @@ public void usesPreparedTransaction() { @SuppressWarnings({"unchecked", "resource"}) @Test - public void inlineBegin() { + public void inlineBegin() throws IOException { SpannerOptions options = mock(SpannerOptions.class); when(options.getNumChannels()).thenReturn(4); GrpcTransportOptions transportOptions = mock(GrpcTransportOptions.class); @@ -332,6 +346,11 @@ public void inlineBegin() { com.google.protobuf.Timestamp.newBuilder() .setSeconds(System.currentTimeMillis() * 1000)) .build())); + when(instanceAdminStubSettings.createStub()).thenReturn(instanceAdminStub); + when(databaseAdminStubSettings.createStub()).thenReturn(databaseAdminStub); + when(rpc.getInstanceAdminStubSettings()).thenReturn(instanceAdminStubSettings); + when(rpc.getDatabaseAdminStubSettings()).thenReturn(databaseAdminStubSettings); + DatabaseId db = DatabaseId.of("test", "test", "test"); try (SpannerImpl spanner = new SpannerImpl(rpc, options)) { DatabaseClient client = spanner.getDatabaseClient(db); diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/TransactionRunnerImplTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/TransactionRunnerImplTest.java index cf5f901f65e..db92e4caa2d 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/TransactionRunnerImplTest.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/TransactionRunnerImplTest.java @@ -34,6 +34,10 @@ import com.google.cloud.grpc.GrpcTransportOptions.ExecutorFactory; import com.google.cloud.spanner.SessionClient.SessionId; import com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl; +import com.google.cloud.spanner.admin.database.v1.stub.DatabaseAdminStub; +import com.google.cloud.spanner.admin.database.v1.stub.DatabaseAdminStubSettings; +import com.google.cloud.spanner.admin.instance.v1.stub.InstanceAdminStub; +import com.google.cloud.spanner.admin.instance.v1.stub.InstanceAdminStubSettings; import com.google.cloud.spanner.spi.v1.SpannerRpc; import com.google.common.base.Preconditions; import com.google.protobuf.ByteString; @@ -63,6 +67,7 @@ import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.trace.Span; import io.opentelemetry.context.Scope; +import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.UUID; @@ -81,6 +86,7 @@ /** Unit test for {@link com.google.cloud.spanner.TransactionRunnerImpl} */ @RunWith(JUnit4.class) public class TransactionRunnerImplTest { + private static final class TestExecutorFactory implements ExecutorFactory { @Override @@ -97,6 +103,10 @@ public void release(ScheduledExecutorService exec) { @Mock private SpannerRpc rpc; @Mock private SessionImpl session; @Mock private TransactionRunnerImpl.TransactionContextImpl txn; + @Mock private InstanceAdminStubSettings instanceAdminStubSettings; + @Mock private DatabaseAdminStubSettings databaseAdminStubSettings; + @Mock private DatabaseAdminStub databaseAdminStub; + @Mock private InstanceAdminStub instanceAdminStub; private TransactionRunnerImpl transactionRunner; private boolean firstRun; private boolean usedInlinedBegin; @@ -151,7 +161,7 @@ public void setUp() { @SuppressWarnings("unchecked") @Test - public void usesPreparedTransaction() { + public void usesPreparedTransaction() throws IOException { SpannerOptions options = mock(SpannerOptions.class); when(options.getNumChannels()).thenReturn(4); GrpcTransportOptions transportOptions = mock(GrpcTransportOptions.class); @@ -196,6 +206,11 @@ public void usesPreparedTransaction() { .setCommitTimestamp( Timestamp.newBuilder().setSeconds(System.currentTimeMillis() * 1000)) .build())); + when(instanceAdminStubSettings.createStub()).thenReturn(instanceAdminStub); + when(databaseAdminStubSettings.createStub()).thenReturn(databaseAdminStub); + when(rpc.getInstanceAdminStubSettings()).thenReturn(instanceAdminStubSettings); + when(rpc.getDatabaseAdminStubSettings()).thenReturn(databaseAdminStubSettings); + DatabaseId db = DatabaseId.of("test", "test", "test"); try (SpannerImpl spanner = new SpannerImpl(rpc, options)) { DatabaseClient client = spanner.getDatabaseClient(db);