1 // Copyright 2017 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "google/cloud/bigtable/table.h"
16 #include "google/cloud/bigtable/testing/mock_read_rows_reader.h"
17 #include "google/cloud/bigtable/testing/table_test_fixture.h"
18 #include "google/cloud/testing_util/assert_ok.h"
19 #include "absl/memory/memory.h"
20
21 namespace google {
22 namespace cloud {
23 namespace bigtable {
24 inline namespace BIGTABLE_CLIENT_NS {
25 namespace {
26
27 namespace btproto = ::google::bigtable::v2;
28 using ::google::cloud::bigtable::testing::MockReadRowsReader;
29 using ::google::cloud::testing_util::IsContextMDValid;
30 using ::testing::Return;
31
32 class TableReadRowTest : public bigtable::testing::TableTestFixture {};
33
TEST_F(TableReadRowTest,ReadRowSimple)34 TEST_F(TableReadRowTest, ReadRowSimple) {
35 auto const response = bigtable::testing::ReadRowsResponseFromString(R"(
36 chunks {
37 row_key: "r1"
38 family_name { value: "fam" }
39 qualifier { value: "col" }
40 timestamp_micros: 42000
41 value: "value"
42 commit_row: true
43 }
44 )");
45
46 EXPECT_CALL(*client_, ReadRows)
47 .WillOnce([&response, this](grpc::ClientContext* context,
48 btproto::ReadRowsRequest const& req) {
49 auto stream = absl::make_unique<MockReadRowsReader>(
50 "google.bigtable.v2.Bigtable.ReadRows");
51 EXPECT_CALL(*stream, Read)
52 .WillOnce([response](btproto::ReadRowsResponse* r) {
53 *r = response;
54 return true;
55 })
56 .WillOnce(Return(false));
57 EXPECT_CALL(*stream, Finish).WillOnce(Return(grpc::Status::OK));
58
59 EXPECT_STATUS_OK(
60 IsContextMDValid(*context, "google.bigtable.v2.Bigtable.ReadRows",
61 google::cloud::internal::ApiClientHeader()));
62 EXPECT_EQ(1, req.rows().row_keys_size());
63 EXPECT_EQ("r1", req.rows().row_keys(0));
64 EXPECT_EQ(1, req.rows_limit());
65 EXPECT_EQ(table_.table_name(), req.table_name());
66 return stream;
67 });
68
69 auto result = table_.ReadRow("r1", bigtable::Filter::PassAllFilter());
70 ASSERT_STATUS_OK(result);
71 EXPECT_TRUE(std::get<0>(*result));
72 auto row = std::get<1>(*result);
73 EXPECT_EQ("r1", row.row_key());
74 }
75
TEST_F(TableReadRowTest,ReadRowMissing)76 TEST_F(TableReadRowTest, ReadRowMissing) {
77 EXPECT_CALL(*client_, ReadRows)
78 .WillOnce([this](grpc::ClientContext* context,
79 btproto::ReadRowsRequest const& req) {
80 auto stream = absl::make_unique<MockReadRowsReader>(
81 "google.bigtable.v2.Bigtable.ReadRows");
82 EXPECT_CALL(*stream, Read).WillOnce(Return(false));
83 EXPECT_CALL(*stream, Finish).WillOnce(Return(grpc::Status::OK));
84
85 EXPECT_STATUS_OK(
86 IsContextMDValid(*context, "google.bigtable.v2.Bigtable.ReadRows",
87 google::cloud::internal::ApiClientHeader()));
88 EXPECT_EQ(1, req.rows().row_keys_size());
89 EXPECT_EQ("r1", req.rows().row_keys(0));
90 EXPECT_EQ(1, req.rows_limit());
91 EXPECT_EQ(table_.table_name(), req.table_name());
92 return stream;
93 });
94
95 auto result = table_.ReadRow("r1", bigtable::Filter::PassAllFilter());
96 ASSERT_STATUS_OK(result);
97 EXPECT_FALSE(std::get<0>(*result));
98 }
99
TEST_F(TableReadRowTest,UnrecoverableFailure)100 TEST_F(TableReadRowTest, UnrecoverableFailure) {
101 EXPECT_CALL(*client_, ReadRows)
102 .WillRepeatedly([](grpc::ClientContext* context,
103 btproto::ReadRowsRequest const&) {
104 auto stream = absl::make_unique<MockReadRowsReader>(
105 "google.bigtable.v2.Bigtable.ReadRows");
106 EXPECT_CALL(*stream, Read).WillRepeatedly(Return(false));
107 EXPECT_CALL(*stream, Finish)
108 .WillRepeatedly(Return(
109 grpc::Status(grpc::StatusCode::PERMISSION_DENIED, "uh oh")));
110
111 EXPECT_STATUS_OK(
112 IsContextMDValid(*context, "google.bigtable.v2.Bigtable.ReadRows",
113 google::cloud::internal::ApiClientHeader()));
114 return stream;
115 });
116
117 auto row = table_.ReadRow("r1", bigtable::Filter::PassAllFilter());
118 EXPECT_FALSE(row);
119 }
120
121 } // anonymous namespace
122 } // namespace BIGTABLE_CLIENT_NS
123 } // namespace bigtable
124 } // namespace cloud
125 } // namespace google
126