Skip to content

Commit efb80e2

Browse files
authored
impl(bigquery): Project Stub and StubFactory (#11616)
1 parent f9157d7 commit efb80e2

File tree

8 files changed

+314
-0
lines changed

8 files changed

+314
-0
lines changed

google/cloud/bigquery/bigquery_rest.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ add_library(
7979
v2/minimal/internal/project_request.h
8080
v2/minimal/internal/project_response.cc
8181
v2/minimal/internal/project_response.h
82+
v2/minimal/internal/project_rest_stub.cc
83+
v2/minimal/internal/project_rest_stub.h
84+
v2/minimal/internal/project_rest_stub_factory.cc
85+
v2/minimal/internal/project_rest_stub_factory.h
8286
v2/minimal/internal/rest_stub_utils.cc
8387
v2/minimal/internal/rest_stub_utils.h
8488
v2/minimal/internal/table.cc
@@ -229,6 +233,7 @@ function (bigquery_rest_define_tests)
229233
v2/minimal/internal/job_test.cc
230234
v2/minimal/internal/project_request_test.cc
231235
v2/minimal/internal/project_response_test.cc
236+
v2/minimal/internal/project_rest_stub_test.cc
232237
v2/minimal/internal/project_test.cc
233238
v2/minimal/internal/rest_stub_utils_test.cc
234239
v2/minimal/internal/table_client_test.cc

google/cloud/bigquery/bigquery_rest_unit_tests.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ bigquery_rest_unit_tests = [
4141
"v2/minimal/internal/job_test.cc",
4242
"v2/minimal/internal/project_request_test.cc",
4343
"v2/minimal/internal/project_response_test.cc",
44+
"v2/minimal/internal/project_rest_stub_test.cc",
4445
"v2/minimal/internal/project_test.cc",
4546
"v2/minimal/internal/rest_stub_utils_test.cc",
4647
"v2/minimal/internal/table_client_test.cc",

google/cloud/bigquery/google_cloud_cpp_bigquery_rest.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ google_cloud_cpp_bigquery_rest_hdrs = [
5151
"v2/minimal/internal/project.h",
5252
"v2/minimal/internal/project_request.h",
5353
"v2/minimal/internal/project_response.h",
54+
"v2/minimal/internal/project_rest_stub.h",
55+
"v2/minimal/internal/project_rest_stub_factory.h",
5456
"v2/minimal/internal/rest_stub_utils.h",
5557
"v2/minimal/internal/table.h",
5658
"v2/minimal/internal/table_client.h",
@@ -101,6 +103,8 @@ google_cloud_cpp_bigquery_rest_srcs = [
101103
"v2/minimal/internal/project.cc",
102104
"v2/minimal/internal/project_request.cc",
103105
"v2/minimal/internal/project_response.cc",
106+
"v2/minimal/internal/project_rest_stub.cc",
107+
"v2/minimal/internal/project_rest_stub_factory.cc",
104108
"v2/minimal/internal/rest_stub_utils.cc",
105109
"v2/minimal/internal/table.cc",
106110
"v2/minimal/internal/table_client.cc",
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2023 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+
// https://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/bigquery/v2/minimal/internal/project_rest_stub.h"
16+
#include "google/cloud/bigquery/v2/minimal/internal/rest_stub_utils.h"
17+
#include "google/cloud/status_or.h"
18+
19+
namespace google {
20+
namespace cloud {
21+
namespace bigquery_v2_minimal_internal {
22+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
23+
24+
ProjectRestStub::~ProjectRestStub() = default;
25+
26+
StatusOr<ListProjectsResponse> DefaultProjectRestStub::ListProjects(
27+
rest_internal::RestContext& rest_context,
28+
ListProjectsRequest const& request) {
29+
// Prepare the RestRequest from ListProjectsRequest.
30+
auto rest_request =
31+
PrepareRestRequest<ListProjectsRequest>(rest_context, request);
32+
33+
// Call the rest stub and parse the RestResponse.
34+
return ParseFromRestResponse<ListProjectsResponse>(
35+
rest_stub_->Get(rest_context, std::move(*rest_request)));
36+
}
37+
38+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
39+
} // namespace bigquery_v2_minimal_internal
40+
} // namespace cloud
41+
} // namespace google
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2023 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+
// https://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+
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGQUERY_V2_MINIMAL_INTERNAL_PROJECT_REST_STUB_H
16+
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGQUERY_V2_MINIMAL_INTERNAL_PROJECT_REST_STUB_H
17+
18+
#include "google/cloud/bigquery/v2/minimal/internal/project_request.h"
19+
#include "google/cloud/bigquery/v2/minimal/internal/project_response.h"
20+
#include "google/cloud/internal/rest_client.h"
21+
#include "google/cloud/status_or.h"
22+
#include "google/cloud/version.h"
23+
#include <memory>
24+
25+
namespace google {
26+
namespace cloud {
27+
namespace bigquery_v2_minimal_internal {
28+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
29+
30+
class ProjectRestStub {
31+
public:
32+
virtual ~ProjectRestStub() = 0;
33+
34+
virtual StatusOr<ListProjectsResponse> ListProjects(
35+
rest_internal::RestContext& rest_context,
36+
ListProjectsRequest const& request) = 0;
37+
};
38+
39+
class DefaultProjectRestStub : public ProjectRestStub {
40+
public:
41+
explicit DefaultProjectRestStub(
42+
std::unique_ptr<rest_internal::RestClient> rest_stub)
43+
: rest_stub_(std::move(rest_stub)) {}
44+
45+
StatusOr<ListProjectsResponse> ListProjects(
46+
rest_internal::RestContext& rest_context,
47+
ListProjectsRequest const& request) override;
48+
49+
private:
50+
std::unique_ptr<rest_internal::RestClient> rest_stub_;
51+
};
52+
53+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
54+
} // namespace bigquery_v2_minimal_internal
55+
} // namespace cloud
56+
} // namespace google
57+
58+
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGQUERY_V2_MINIMAL_INTERNAL_PROJECT_REST_STUB_H
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2023 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+
// https://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/bigquery/v2/minimal/internal/project_rest_stub_factory.h"
16+
#include "google/cloud/common_options.h"
17+
#include "google/cloud/credentials.h"
18+
#include "google/cloud/internal/algorithm.h"
19+
#include "google/cloud/internal/unified_rest_credentials.h"
20+
#include "google/cloud/log.h"
21+
22+
namespace google {
23+
namespace cloud {
24+
namespace bigquery_v2_minimal_internal {
25+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
26+
27+
std::shared_ptr<ProjectRestStub> CreateDefaultProjectRestStub(
28+
Options const& opts) {
29+
Options local_opts = opts;
30+
if (!local_opts.has<UnifiedCredentialsOption>()) {
31+
local_opts.set<UnifiedCredentialsOption>(MakeGoogleDefaultCredentials());
32+
}
33+
34+
auto curl_rest_client = rest_internal::MakePooledRestClient(
35+
opts.get<EndpointOption>(), local_opts);
36+
37+
std::shared_ptr<ProjectRestStub> stub =
38+
std::make_shared<DefaultProjectRestStub>(std::move(curl_rest_client));
39+
40+
return stub;
41+
}
42+
43+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
44+
} // namespace bigquery_v2_minimal_internal
45+
} // namespace cloud
46+
} // namespace google
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2023 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+
// https://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+
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGQUERY_V2_MINIMAL_INTERNAL_PROJECT_REST_STUB_FACTORY_H
16+
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGQUERY_V2_MINIMAL_INTERNAL_PROJECT_REST_STUB_FACTORY_H
17+
18+
#include "google/cloud/bigquery/v2/minimal/internal/project_rest_stub.h"
19+
#include "google/cloud/options.h"
20+
#include "google/cloud/version.h"
21+
#include <memory>
22+
23+
namespace google {
24+
namespace cloud {
25+
namespace bigquery_v2_minimal_internal {
26+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
27+
28+
std::shared_ptr<ProjectRestStub> CreateDefaultProjectRestStub(
29+
Options const& opts);
30+
31+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
32+
} // namespace bigquery_v2_minimal_internal
33+
} // namespace cloud
34+
} // namespace google
35+
36+
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGQUERY_V2_MINIMAL_INTERNAL_PROJECT_REST_STUB_FACTORY_H
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Copyright 2023 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+
// https://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/bigquery/v2/minimal/internal/project_rest_stub.h"
16+
#include "google/cloud/bigquery/v2/minimal/testing/project_test_utils.h"
17+
#include "google/cloud/common_options.h"
18+
#include "google/cloud/internal/http_payload.h"
19+
#include "google/cloud/internal/make_status.h"
20+
#include "google/cloud/internal/rest_response.h"
21+
#include "google/cloud/testing_util/mock_http_payload.h"
22+
#include "google/cloud/testing_util/mock_rest_client.h"
23+
#include "google/cloud/testing_util/mock_rest_response.h"
24+
#include "google/cloud/testing_util/status_matchers.h"
25+
#include <gmock/gmock.h>
26+
27+
namespace google {
28+
namespace cloud {
29+
namespace bigquery_v2_minimal_internal {
30+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
31+
32+
namespace rest = ::google::cloud::rest_internal;
33+
using ::google::cloud::rest_internal::HttpStatusCode;
34+
using ::google::cloud::testing_util::MakeMockHttpPayloadSuccess;
35+
using ::google::cloud::testing_util::MockHttpPayload;
36+
using ::google::cloud::testing_util::MockRestClient;
37+
using ::google::cloud::testing_util::MockRestResponse;
38+
using ::google::cloud::testing_util::StatusIs;
39+
using ::testing::_;
40+
using ::testing::An;
41+
using ::testing::ByMove;
42+
using ::testing::Return;
43+
44+
ListProjectsRequest MakeListProjectsRequest() {
45+
ListProjectsRequest request;
46+
request.set_max_results(10).set_page_token("pg-123");
47+
return request;
48+
}
49+
TEST(ProjectStubTest, ListProjectsSuccess) {
50+
std::string payload =
51+
bigquery_v2_minimal_testing::MakeListProjectsResponseJsonText();
52+
auto mock_response = std::make_unique<MockRestResponse>();
53+
54+
EXPECT_CALL(*mock_response, StatusCode)
55+
.WillRepeatedly(Return(HttpStatusCode::kOk));
56+
EXPECT_CALL(*mock_response, Headers)
57+
.WillRepeatedly(Return(std::multimap<std::string, std::string>()));
58+
EXPECT_CALL(std::move(*mock_response), ExtractPayload)
59+
.WillOnce(Return(ByMove(MakeMockHttpPayloadSuccess(payload))));
60+
61+
auto mock_rest_client = std::make_unique<MockRestClient>();
62+
EXPECT_CALL(*mock_rest_client, Get(_, An<rest::RestRequest const&>()))
63+
.WillOnce(Return(ByMove(
64+
std::unique_ptr<rest::RestResponse>(std::move(mock_response)))));
65+
66+
auto request = MakeListProjectsRequest();
67+
68+
rest_internal::RestContext context;
69+
DefaultProjectRestStub rest_stub(std::move(mock_rest_client));
70+
71+
auto result = rest_stub.ListProjects(context, std::move(request));
72+
ASSERT_STATUS_OK(result);
73+
74+
auto const projects = result->projects;
75+
ASSERT_EQ(projects.size(), 1);
76+
77+
auto expected = bigquery_v2_minimal_testing::MakeProject();
78+
79+
EXPECT_EQ(result->http_response.http_status_code, HttpStatusCode::kOk);
80+
bigquery_v2_minimal_testing::AssertEquals(expected, projects[0]);
81+
}
82+
83+
TEST(ProjectStubTest, ListProjectsRestClientError) {
84+
auto mock_rest_client = std::make_unique<MockRestClient>();
85+
EXPECT_CALL(*mock_rest_client, Get(_, An<rest::RestRequest const&>()))
86+
.WillOnce(
87+
Return(rest::AsStatus(HttpStatusCode::kInternalServerError, "")));
88+
89+
auto request = MakeListProjectsRequest();
90+
91+
rest_internal::RestContext context;
92+
DefaultProjectRestStub rest_stub(std::move(mock_rest_client));
93+
94+
auto response = rest_stub.ListProjects(context, std::move(request));
95+
EXPECT_THAT(response, StatusIs(StatusCode::kUnavailable));
96+
}
97+
98+
TEST(ProjectStubTest, ListProjectsRestResponseError) {
99+
auto mock_payload = std::make_unique<MockHttpPayload>();
100+
auto mock_response = std::make_unique<MockRestResponse>();
101+
EXPECT_CALL(*mock_response, StatusCode)
102+
.WillRepeatedly(Return(HttpStatusCode::kBadRequest));
103+
EXPECT_CALL(std::move(*mock_response), ExtractPayload)
104+
.WillOnce(Return(std::move(mock_payload)));
105+
106+
auto mock_rest_client = std::make_unique<MockRestClient>();
107+
EXPECT_CALL(*mock_rest_client, Get(_, An<rest::RestRequest const&>()))
108+
.WillOnce(Return(ByMove(
109+
std::unique_ptr<rest::RestResponse>(std::move(mock_response)))));
110+
111+
auto request = MakeListProjectsRequest();
112+
113+
rest_internal::RestContext context;
114+
DefaultProjectRestStub rest_stub(std::move(mock_rest_client));
115+
116+
auto response = rest_stub.ListProjects(context, std::move(request));
117+
118+
EXPECT_THAT(response, StatusIs(StatusCode::kInvalidArgument));
119+
}
120+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
121+
} // namespace bigquery_v2_minimal_internal
122+
} // namespace cloud
123+
} // namespace google

0 commit comments

Comments
 (0)