From 82fda7519461eeb07b3ace6965ea6aa707f3133b Mon Sep 17 00:00:00 2001 From: Nithin Sujir <118742+nithinsujir@users.noreply.github.com> Date: Thu, 27 Sep 2018 11:17:02 -0700 Subject: [PATCH] spanner: Expand test coverage for SpannerClient (#3725) Expands coverage from 53% to 78%. --- .../cloud/spanner/v1/SpannerClientTest.java | 160 ++++++++++++++++++ 1 file changed, 160 insertions(+) diff --git a/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/v1/SpannerClientTest.java b/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/v1/SpannerClientTest.java index 08a3e92f23c8..92ba718a0a0e 100644 --- a/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/v1/SpannerClientTest.java +++ b/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/v1/SpannerClientTest.java @@ -16,6 +16,7 @@ package com.google.cloud.spanner.v1; import static com.google.cloud.spanner.v1.SpannerClient.ListSessionsPagedResponse; +import static com.google.common.truth.Truth.assertThat; import com.google.api.gax.core.NoCredentialsProvider; import com.google.api.gax.grpc.GaxGrpcProperties; @@ -27,6 +28,8 @@ import com.google.api.gax.rpc.InvalidArgumentException; import com.google.api.gax.rpc.ServerStreamingCallable; import com.google.api.gax.rpc.StatusCode; +import com.google.cloud.spanner.v1.stub.SpannerStub; +import com.google.cloud.spanner.v1.stub.SpannerStubSettings; import com.google.common.collect.Lists; import com.google.protobuf.ByteString; import com.google.protobuf.Empty; @@ -101,6 +104,16 @@ public void tearDown() throws Exception { client.close(); } + @Test + public void createClientTest() throws IOException { + SpannerStub stub = + SpannerStubSettings.newBuilder() + .setCredentialsProvider(NoCredentialsProvider.create()).build().createStub(); + SpannerClient client1 = SpannerClient.create(stub); + assertThat(client1.getStub()).isEqualTo(stub); + assertThat(client1.getSettings()).isNull(); + } + @Test @SuppressWarnings("all") public void createSessionTest() { @@ -124,6 +137,27 @@ public void createSessionTest() { GaxGrpcProperties.getDefaultApiClientHeaderPattern())); } + @Test + public void createSessionWithStringName() { + String name = "projects/[PROJECT]/instances/[INSTANCE]/databases/[DATABASE]"; + Session expectedResponse = Session.newBuilder().setName(name).build(); + mockSpanner.addResponse(expectedResponse); + + DatabaseName database = DatabaseName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]"); + + Session actualResponse = client.createSession(name); + assertThat(actualResponse).isEqualTo(expectedResponse); + + List actualRequests = mockSpanner.getRequests(); + assertThat(actualRequests).hasSize(1); + + CreateSessionRequest actualRequest = (CreateSessionRequest) actualRequests.get(0); + assertThat(DatabaseName.parse(actualRequest.getDatabase())).isEqualTo(database); + assertThat(channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())).isTrue(); + } + @Test @SuppressWarnings("all") public void createSessionExceptionTest() throws Exception { @@ -163,6 +197,24 @@ public void getSessionTest() { GaxGrpcProperties.getDefaultApiClientHeaderPattern())); } + @Test + public void getSessionByStringNameTest() { + String name = "projects/[PROJECT]/instances/[INSTANCE]/databases/[DATABASE]/sessions/[SESSION]"; + SessionName sessionName = SessionName.parse(name); + Session expectedResponse = Session.newBuilder().setName(name).build(); + mockSpanner.addResponse(expectedResponse); + + Session actualResponse = client.getSession(name); + assertThat(actualResponse).isEqualTo(expectedResponse); + + List actualRequests = mockSpanner.getRequests(); + assertThat(actualRequests).hasSize(1); + + GetSessionRequest actualRequest = (GetSessionRequest) actualRequests.get(0); + assertThat(SessionName.parse(actualRequest.getName())).isEqualTo(sessionName); + + } + @Test @SuppressWarnings("all") public void getSessionExceptionTest() throws Exception { @@ -211,6 +263,27 @@ public void listSessionsTest() { GaxGrpcProperties.getDefaultApiClientHeaderPattern())); } + @Test + public void listSessionsCallableTest() { + List sessions = Arrays.asList(Session.newBuilder().build()); + ListSessionsResponse expectedResponse = + ListSessionsResponse.newBuilder() + .addAllSessions(sessions) + .build(); + mockSpanner.addResponse(expectedResponse); + + String formattedDatabase = DatabaseName.format("[PROJECT]", "[INSTANCE]", "[DATABASE]"); + + ListSessionsRequest request = ListSessionsRequest.newBuilder() + .setDatabase(formattedDatabase) + .build(); + + ListSessionsResponse listSessionsResponse = client.listSessionsCallable().call(request); + List resources = Lists.newArrayList(listSessionsResponse.getSessionsList()); + assertThat(resources).hasSize(1); + assertThat(expectedResponse.getSessionsList().get(0)).isEqualTo(resources.get(0)); + } + @Test @SuppressWarnings("all") public void listSessionsExceptionTest() throws Exception { @@ -248,6 +321,25 @@ public void deleteSessionTest() { GaxGrpcProperties.getDefaultApiClientHeaderPattern())); } + @Test + public void deleteSessionWithStringName() { + Empty expectedResponse = Empty.newBuilder().build(); + mockSpanner.addResponse(expectedResponse); + + String name = "projects/[PROJECT]/instances/[INSTANCE]/databases/[DATABASE]/sessions/[SESSION]"; + SessionName sessionName = SessionName.parse(name); + client.deleteSession(name); + + List actualRequests = mockSpanner.getRequests(); + assertThat(actualRequests).hasSize(1); + + DeleteSessionRequest actualRequest = (DeleteSessionRequest) actualRequests.get(0); + assertThat(SessionName.parse(actualRequest.getName())).isEqualTo(sessionName); + assertThat(channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())).isTrue(); + } + @Test @SuppressWarnings("all") public void deleteSessionExceptionTest() throws Exception { @@ -426,6 +518,31 @@ public void beginTransactionExceptionTest() throws Exception { } } + @Test + public void commitWithStringNameTest() { + CommitResponse expectedResponse = CommitResponse.newBuilder().build(); + mockSpanner.addResponse(expectedResponse); + + ByteString transactionId = ByteString.copyFromUtf8("28"); + List mutations = new ArrayList<>(); + + String name = "projects/[PROJECT]/instances/[INSTANCE]/databases/[DATABASE]/sessions/[SESSION]"; + SessionName sessionName = SessionName.parse(name); + CommitResponse actualResponse = client.commit(name, transactionId, mutations); + assertThat(actualResponse).isEqualTo(expectedResponse); + + List actualRequests = mockSpanner.getRequests(); + assertThat(actualRequests).hasSize(1); + + CommitRequest actualRequest = (CommitRequest) actualRequests.get(0); + assertThat(SessionName.parse(actualRequest.getSession())).isEqualTo(sessionName); + assertThat(actualRequest.getTransactionId()).isEqualTo(transactionId); + assertThat(actualRequest.getMutationsList()).isEqualTo(mutations); + assertThat(channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())).isTrue(); + } + @Test @SuppressWarnings("all") public void commitTest() { @@ -470,6 +587,29 @@ public void commitExceptionTest() throws Exception { } } + @Test + public void commitWithStringNameViaTransactionTest() { + CommitResponse expectedResponse = CommitResponse.newBuilder().build(); + mockSpanner.addResponse(expectedResponse); + + String name = "projects/[PROJECT]/instances/[INSTANCE]/databases/[DATABASE]/sessions/[SESSION]"; + TransactionOptions singleUseTransaction = TransactionOptions.newBuilder().build(); + List mutations = new ArrayList<>(); + + CommitResponse actualResponse = client.commit(name, singleUseTransaction, mutations); + assertThat(actualResponse).isEqualTo(expectedResponse); + + List actualRequests = mockSpanner.getRequests(); + assertThat(actualRequests.size()).isEqualTo(1); + + CommitRequest actualRequest = (CommitRequest) actualRequests.get(0); + assertThat(actualRequest.getSingleUseTransaction()).isEqualTo(singleUseTransaction); + assertThat(actualRequest.getMutationsList()).isEqualTo(mutations); + assertThat(channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())).isTrue(); + } + @Test @SuppressWarnings("all") public void commitTest2() { @@ -514,6 +654,26 @@ public void commitExceptionTest2() throws Exception { } } + @Test + public void rollbackWithStringNameTest() { + Empty expectedResponse = Empty.newBuilder().build(); + mockSpanner.addResponse(expectedResponse); + + String name = "projects/[PROJECT]/instances/[INSTANCE]/databases/[DATABASE]/sessions/[SESSION]"; + ByteString transactionId = ByteString.copyFromUtf8("28"); + + client.rollback(name, transactionId); + + List actualRequests = mockSpanner.getRequests(); + assertThat(actualRequests).hasSize(1); + + RollbackRequest actualRequest = (RollbackRequest) actualRequests.get(0); + assertThat(actualRequest.getTransactionId()).isEqualTo(transactionId); + assertThat(channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())).isTrue(); + } + @Test @SuppressWarnings("all") public void rollbackTest() {