1 // Copyright 2018 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/bigtable/app_profile_config.h"
16 #include <gmock/gmock.h>
17 
18 namespace google {
19 namespace cloud {
20 namespace bigtable {
21 inline namespace BIGTABLE_CLIENT_NS {
22 namespace {
TEST(AppProfileConfig,MultiClusterUseAny)23 TEST(AppProfileConfig, MultiClusterUseAny) {
24   auto proto = AppProfileConfig::MultiClusterUseAny("my-profile").as_proto();
25   EXPECT_EQ("my-profile", proto.app_profile_id());
26   EXPECT_TRUE(proto.app_profile().has_multi_cluster_routing_use_any());
27 
28   // Verify that as_proto() for rvalue-references returns the right type.
29   static_assert(
30       std::is_rvalue_reference<decltype(
31           std::move(std::declval<AppProfileConfig>()).as_proto())>::value,
32       "Return type from as_proto() must be rvalue-reference");
33 }
34 
TEST(AppProfileConfig,SetIgnoreWarnings)35 TEST(AppProfileConfig, SetIgnoreWarnings) {
36   auto proto = AppProfileConfig::MultiClusterUseAny("my-profile")
37                    .set_ignore_warnings(true)
38                    .as_proto();
39   EXPECT_TRUE(proto.ignore_warnings());
40 }
41 
TEST(AppProfileConfig,SetDescription)42 TEST(AppProfileConfig, SetDescription) {
43   auto proto = AppProfileConfig::MultiClusterUseAny("my-profile")
44                    .set_description("my description")
45                    .as_proto();
46   EXPECT_EQ("my description", proto.app_profile().description());
47 }
48 
TEST(AppProfileConfig,SingleClusterRouting)49 TEST(AppProfileConfig, SingleClusterRouting) {
50   auto proto =
51       AppProfileConfig::SingleClusterRouting("my-profile", "the-cluster", false)
52           .as_proto();
53   EXPECT_EQ("my-profile", proto.app_profile_id());
54   ASSERT_TRUE(proto.app_profile().has_single_cluster_routing());
55   auto const& routing = proto.app_profile().single_cluster_routing();
56   EXPECT_EQ("the-cluster", routing.cluster_id());
57   EXPECT_FALSE(routing.allow_transactional_writes());
58 }
59 
TEST(AppProfileConfig,SingleClusterRoutingWithTransactionalWrites)60 TEST(AppProfileConfig, SingleClusterRoutingWithTransactionalWrites) {
61   auto proto =
62       AppProfileConfig::SingleClusterRouting("my-profile", "the-cluster", true)
63           .as_proto();
64   EXPECT_EQ("my-profile", proto.app_profile_id());
65   ASSERT_TRUE(proto.app_profile().has_single_cluster_routing());
66   auto const& routing = proto.app_profile().single_cluster_routing();
67   EXPECT_EQ("the-cluster", routing.cluster_id());
68   EXPECT_TRUE(routing.allow_transactional_writes());
69 }
70 
HasFieldNameOnce(google::protobuf::FieldMask const & mask,std::string const & name)71 bool HasFieldNameOnce(google::protobuf::FieldMask const& mask,
72                       std::string const& name) {
73   return std::count(mask.paths().begin(), mask.paths().end(), name) == 1;
74 }
75 
TEST(AppProfileUpdateConfig,SetDescription)76 TEST(AppProfileUpdateConfig, SetDescription) {
77   auto proto =
78       AppProfileUpdateConfig().set_description("a description").as_proto();
79   EXPECT_EQ("a description", proto.app_profile().description());
80   EXPECT_TRUE(HasFieldNameOnce(proto.update_mask(), "description"));
81 }
82 
TEST(AppProfileUpdateConfig,SetETag)83 TEST(AppProfileUpdateConfig, SetETag) {
84   auto proto = AppProfileUpdateConfig().set_etag("xyzzy").as_proto();
85   EXPECT_EQ("xyzzy", proto.app_profile().etag());
86   EXPECT_TRUE(HasFieldNameOnce(proto.update_mask(), "etag"));
87 }
88 
TEST(AppProfileUpdateConfig,SetMultiClusterUseAny)89 TEST(AppProfileUpdateConfig, SetMultiClusterUseAny) {
90   auto proto = AppProfileUpdateConfig().set_multi_cluster_use_any().as_proto();
91   EXPECT_TRUE(proto.app_profile().has_multi_cluster_routing_use_any());
92   EXPECT_TRUE(
93       HasFieldNameOnce(proto.update_mask(), "multi_cluster_routing_use_any"));
94 }
95 
TEST(AppProfileUpdateConfig,SetSingleClusterRouting)96 TEST(AppProfileUpdateConfig, SetSingleClusterRouting) {
97   auto proto = AppProfileUpdateConfig()
98                    .set_single_cluster_routing("c1", true)
99                    .as_proto();
100   EXPECT_TRUE(proto.app_profile().has_single_cluster_routing());
101   EXPECT_EQ("c1", proto.app_profile().single_cluster_routing().cluster_id());
102   EXPECT_TRUE(proto.app_profile()
103                   .single_cluster_routing()
104                   .allow_transactional_writes());
105   EXPECT_TRUE(HasFieldNameOnce(proto.update_mask(), "single_cluster_routing"));
106 }
107 
TEST(AppProfileUpdateConfig,SetSeveral)108 TEST(AppProfileUpdateConfig, SetSeveral) {
109   auto proto = AppProfileUpdateConfig()
110                    .set_description("foo")
111                    .set_description("bar")
112                    .set_etag("e1")
113                    .set_etag("abcdef")
114                    .set_multi_cluster_use_any()
115                    .set_single_cluster_routing("c1", true)
116                    .as_proto();
117   EXPECT_EQ("bar", proto.app_profile().description());
118   EXPECT_TRUE(HasFieldNameOnce(proto.update_mask(), "description"));
119   EXPECT_EQ("abcdef", proto.app_profile().etag());
120   EXPECT_TRUE(HasFieldNameOnce(proto.update_mask(), "etag"));
121   EXPECT_FALSE(proto.app_profile().has_multi_cluster_routing_use_any());
122   EXPECT_TRUE(proto.app_profile().has_single_cluster_routing());
123   EXPECT_EQ("c1", proto.app_profile().single_cluster_routing().cluster_id());
124   EXPECT_TRUE(proto.app_profile()
125                   .single_cluster_routing()
126                   .allow_transactional_writes());
127   EXPECT_FALSE(HasFieldNameOnce(proto.update_mask(), "multi_cluster_use_any"));
128   EXPECT_TRUE(HasFieldNameOnce(proto.update_mask(), "single_cluster_routing"));
129 }
130 }  // namespace
131 }  // namespace BIGTABLE_CLIENT_NS
132 }  // namespace bigtable
133 }  // namespace cloud
134 }  // namespace google
135