|
| 1 | +// Copyright 2021 Google LLC |
| 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/internal/extract_long_running_result.h" |
| 16 | +#include "google/cloud/testing_util/is_proto_equal.h" |
| 17 | +#include "google/cloud/testing_util/mock_completion_queue_impl.h" |
| 18 | +#include "google/cloud/testing_util/status_matchers.h" |
| 19 | +#include <google/bigtable/admin/v2/bigtable_instance_admin.pb.h> |
| 20 | +#include <gmock/gmock.h> |
| 21 | + |
| 22 | +namespace google { |
| 23 | +namespace cloud { |
| 24 | +inline namespace GOOGLE_CLOUD_CPP_NS { |
| 25 | +namespace internal { |
| 26 | +namespace { |
| 27 | + |
| 28 | +using ::google::bigtable::admin::v2::Instance; |
| 29 | +using ::google::cloud::testing_util::IsOk; |
| 30 | +using ::google::cloud::testing_util::IsProtoEqual; |
| 31 | +using ::google::cloud::testing_util::StatusIs; |
| 32 | +using ::google::longrunning::Operation; |
| 33 | +using ::testing::HasSubstr; |
| 34 | + |
| 35 | +TEST(ExtractLongRunningResultTest, DoneWithSuccess) { |
| 36 | + Instance expected; |
| 37 | + expected.set_name("test-instance-admin"); |
| 38 | + google::longrunning::Operation op; |
| 39 | + op.set_done(true); |
| 40 | + op.mutable_metadata()->PackFrom(expected); |
| 41 | + auto const actual = ExtractLongRunningResult<Instance>(op, "test-function"); |
| 42 | + ASSERT_STATUS_OK(actual); |
| 43 | + EXPECT_THAT(*actual, IsProtoEqual(expected)); |
| 44 | +} |
| 45 | + |
| 46 | +TEST(ExtractLongRunningResultTest, DoneWithError) { |
| 47 | + google::longrunning::Operation op; |
| 48 | + op.set_done(true); |
| 49 | + op.mutable_error()->set_code(grpc::StatusCode::PERMISSION_DENIED); |
| 50 | + op.mutable_error()->set_message("uh-oh"); |
| 51 | + auto const actual = ExtractLongRunningResult<Instance>(op, "test-function"); |
| 52 | + EXPECT_THAT(actual, |
| 53 | + StatusIs(StatusCode::kPermissionDenied, HasSubstr("uh-oh"))); |
| 54 | +} |
| 55 | + |
| 56 | +TEST(ExtractLongRunningResultTest, DoneWithoutResult) { |
| 57 | + google::longrunning::Operation op; |
| 58 | + op.set_done(true); |
| 59 | + auto const actual = ExtractLongRunningResult<Instance>(op, "test-function"); |
| 60 | + EXPECT_THAT(actual, StatusIs(StatusCode::kInternal)); |
| 61 | +} |
| 62 | + |
| 63 | +TEST(ExtractLongRunningResultTest, DoneWithInvalidContent) { |
| 64 | + google::longrunning::Operation op; |
| 65 | + op.set_done(true); |
| 66 | + op.mutable_metadata()->PackFrom(google::protobuf::Empty{}); |
| 67 | + auto const actual = ExtractLongRunningResult<Instance>(op, "test-function"); |
| 68 | + EXPECT_THAT(actual, StatusIs(StatusCode::kInternal)); |
| 69 | +} |
| 70 | + |
| 71 | +TEST(ExtractLongRunningResultTest, Error) { |
| 72 | + auto const expected = Status{StatusCode::kPermissionDenied, "uh-oh"}; |
| 73 | + auto const actual = |
| 74 | + ExtractLongRunningResult<Instance>(expected, "test-function"); |
| 75 | + ASSERT_THAT(actual, Not(IsOk())); |
| 76 | + EXPECT_EQ(expected, actual.status()); |
| 77 | +} |
| 78 | + |
| 79 | +} // namespace |
| 80 | +} // namespace internal |
| 81 | +} // namespace GOOGLE_CLOUD_CPP_NS |
| 82 | +} // namespace cloud |
| 83 | +} // namespace google |
0 commit comments