| 1 | /* Copyright (c) 2015, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gmock/gmock.h> |
| 25 | #include <gtest/gtest.h> |
| 26 | |
| 27 | #include "my_config.h" // NOLINT(build/include_subdir) |
| 28 | |
| 29 | #include "plugin/x/src/ngs/mysqlx/getter_any.h" |
| 30 | |
| 31 | namespace xpl { |
| 32 | |
| 33 | namespace test { |
| 34 | |
| 35 | class Type_handler { |
| 36 | public: |
| 37 | virtual ~Type_handler() = default; |
| 38 | |
| 39 | virtual void put(const ::google::protobuf::int64 &value) = 0; |
| 40 | virtual void put(const ::google::protobuf::uint64 &value) = 0; |
| 41 | virtual void put(const double &value) = 0; |
| 42 | virtual void put(const float &value) = 0; |
| 43 | virtual void put(const bool &value) = 0; |
| 44 | virtual void put(const std::string &value) = 0; |
| 45 | virtual void put() = 0; |
| 46 | }; |
| 47 | |
| 48 | class Mock_type_handler : public Type_handler { |
| 49 | public: |
| 50 | // Workaround for GMOCK undefined behaviour with ResultHolder |
| 51 | MOCK_METHOD1(put_void, bool(const ::google::protobuf::int64 &)); |
| 52 | MOCK_METHOD1(put_void, bool(const ::google::protobuf::uint64 &)); |
| 53 | MOCK_METHOD1(put_void, bool(const double &)); |
| 54 | MOCK_METHOD1(put_void, bool(const float &)); |
| 55 | MOCK_METHOD1(put_void, bool(const bool &)); |
| 56 | MOCK_METHOD1(put_void, bool(const std::string &)); |
| 57 | MOCK_METHOD0(put_void, bool()); |
| 58 | |
| 59 | void put(const ::google::protobuf::int64 &arg) override { put_void(arg); } |
| 60 | |
| 61 | void put(const ::google::protobuf::uint64 &arg) override { put_void(arg); } |
| 62 | |
| 63 | void put(const double &arg) override { put_void(arg); } |
| 64 | |
| 65 | void put(const float &arg) override { put_void(arg); } |
| 66 | |
| 67 | void put(const bool &arg) override { put_void(arg); } |
| 68 | |
| 69 | void put(const std::string &arg) override { put_void(arg); } |
| 70 | |
| 71 | void put() override { put_void(); } |
| 72 | }; |
| 73 | |
| 74 | using Any = Mysqlx::Datatypes::Any; |
| 75 | using Scalar = Mysqlx::Datatypes::Scalar; |
| 76 | |
| 77 | class Getter_any_testsuite : public ::testing::Test { |
| 78 | public: |
| 79 | template <typename Value_type> |
| 80 | void operator()(const Value_type &value) { |
| 81 | mock.put(value); |
| 82 | } |
| 83 | void operator()(const std::string &value, const uint32_t) { mock.put(value); } |
| 84 | void operator()() { mock.put(); } |
| 85 | |
| 86 | Mock_type_handler mock; |
| 87 | Any any; |
| 88 | }; |
| 89 | |
| 90 | TEST_F(Getter_any_testsuite, put_throwError_whenPutAnyWithoutType) { |
| 91 | ASSERT_THROW(ngs::Getter_any::put_scalar_value_to_functor(any, *this), |
| 92 | ngs::Error_code); |
| 93 | } |
| 94 | |
| 95 | TEST_F(Getter_any_testsuite, put_executesNullCallback) { |
| 96 | any.set_type(Any::SCALAR); |
| 97 | any.mutable_scalar()->set_type(Scalar::V_NULL); |
| 98 | |
| 99 | EXPECT_CALL(mock, put_void()); |
| 100 | |
| 101 | ngs::Getter_any::put_scalar_value_to_functor(any, *this); |
| 102 | } |
| 103 | |
| 104 | TEST_F(Getter_any_testsuite, put_executesSignedIntCallback) { |
| 105 | const ::google::protobuf::int64 expected_value = -10; |
| 106 | |
| 107 | any.set_type(Any::SCALAR); |
| 108 | any.mutable_scalar()->set_type(Scalar::V_SINT); |
| 109 | any.mutable_scalar()->set_v_signed_int(expected_value); |
| 110 | |
| 111 | EXPECT_CALL(mock, |
| 112 | put_void(::testing::Matcher<const ::google::protobuf::int64 &>( |
| 113 | expected_value))); |
| 114 | |
| 115 | ngs::Getter_any::put_scalar_value_to_functor(any, *this); |
| 116 | } |
| 117 | |
| 118 | TEST_F(Getter_any_testsuite, put_executesUnsignedIntCallback) { |
| 119 | ::google::protobuf::uint64 expected_value = 10; |
| 120 | |
| 121 | any.set_type(Any::SCALAR); |
| 122 | any.mutable_scalar()->set_type(Scalar::V_UINT); |
| 123 | any.mutable_scalar()->set_v_unsigned_int(expected_value); |
| 124 | |
| 125 | EXPECT_CALL(mock, |
| 126 | put_void(::testing::Matcher<const ::google::protobuf::uint64 &>( |
| 127 | expected_value))); |
| 128 | |
| 129 | ngs::Getter_any::put_scalar_value_to_functor(any, *this); |
| 130 | } |
| 131 | |
| 132 | TEST_F(Getter_any_testsuite, put_executesBoolCallback) { |
| 133 | bool expected_value = true; |
| 134 | |
| 135 | any.set_type(Any::SCALAR); |
| 136 | any.mutable_scalar()->set_type(Scalar::V_BOOL); |
| 137 | any.mutable_scalar()->set_v_bool(expected_value); |
| 138 | |
| 139 | EXPECT_CALL(mock, put_void(::testing::Matcher<const bool &>(expected_value))); |
| 140 | |
| 141 | ngs::Getter_any::put_scalar_value_to_functor(any, *this); |
| 142 | } |
| 143 | |
| 144 | TEST_F(Getter_any_testsuite, put_executesFloatCallback) { |
| 145 | float expected_value = 1.120f; |
| 146 | |
| 147 | any.set_type(Any::SCALAR); |
| 148 | any.mutable_scalar()->set_type(Scalar::V_FLOAT); |
| 149 | any.mutable_scalar()->set_v_float(expected_value); |
| 150 | |
| 151 | EXPECT_CALL(mock, |
| 152 | put_void(::testing::Matcher<const float &>(expected_value))); |
| 153 | |
| 154 | ngs::Getter_any::put_scalar_value_to_functor(any, *this); |
| 155 | } |
| 156 | |
| 157 | TEST_F(Getter_any_testsuite, put_executesDoubleCallback) { |
| 158 | double expected_value = 2.2120; |
| 159 | |
| 160 | any.set_type(Any::SCALAR); |
| 161 | any.mutable_scalar()->set_type(Scalar::V_DOUBLE); |
| 162 | any.mutable_scalar()->set_v_double(expected_value); |
| 163 | |
| 164 | EXPECT_CALL(mock, |
| 165 | put_void(::testing::Matcher<const double &>(expected_value))); |
| 166 | |
| 167 | ngs::Getter_any::put_scalar_value_to_functor(any, *this); |
| 168 | } |
| 169 | |
| 170 | TEST_F(Getter_any_testsuite, put_throwsError_whenStringWithoutValue) { |
| 171 | std::string expected_value = "Expected string"; |
| 172 | |
| 173 | any.set_type(Any::SCALAR); |
| 174 | any.mutable_scalar()->set_type(Scalar::V_STRING); |
| 175 | any.mutable_scalar()->mutable_v_string(); |
| 176 | |
| 177 | ASSERT_THROW(ngs::Getter_any::put_scalar_value_to_functor(any, *this), |
| 178 | ngs::Error_code); |
| 179 | } |
| 180 | |
| 181 | TEST_F(Getter_any_testsuite, put_executesStringCallback) { |
| 182 | std::string expected_value = "Expected string"; |
| 183 | |
| 184 | any.set_type(Any::SCALAR); |
| 185 | any.mutable_scalar()->set_type(Scalar::V_STRING); |
| 186 | any.mutable_scalar()->mutable_v_string()->set_value(expected_value); |
| 187 | |
| 188 | EXPECT_CALL( |
| 189 | mock, put_void(::testing::Matcher<const std::string &>(expected_value))); |
| 190 | |
| 191 | ngs::Getter_any::put_scalar_value_to_functor(any, *this); |
| 192 | } |
| 193 | |
| 194 | TEST_F(Getter_any_testsuite, put_executesOctetsCallback) { |
| 195 | std::string expected_value = "Expected string"; |
| 196 | |
| 197 | any.set_type(Any::SCALAR); |
| 198 | any.mutable_scalar()->set_type(Scalar::V_OCTETS); |
| 199 | any.mutable_scalar()->mutable_v_octets()->set_value(expected_value); |
| 200 | |
| 201 | EXPECT_CALL( |
| 202 | mock, put_void(::testing::Matcher<const std::string &>(expected_value))); |
| 203 | |
| 204 | ngs::Getter_any::put_scalar_value_to_functor(any, *this); |
| 205 | } |
| 206 | |
| 207 | class Getter_any_type_testsuite |
| 208 | : public Getter_any_testsuite, |
| 209 | public ::testing::WithParamInterface<Any::Type> {}; |
| 210 | |
| 211 | TEST_P(Getter_any_type_testsuite, put_throwError_whenNotSupportedType) { |
| 212 | any.set_type(GetParam()); |
| 213 | |
| 214 | ASSERT_THROW(ngs::Getter_any::put_scalar_value_to_functor(any, *this), |
| 215 | ngs::Error_code); |
| 216 | } |
| 217 | |
| 218 | INSTANTIATE_TEST_SUITE_P(InstantiationNegativeTests, Getter_any_type_testsuite, |
| 219 | ::testing::Values(Any::OBJECT, Any::ARRAY)); |
| 220 | |
| 221 | class Getter_scalar_type_testsuite |
| 222 | : public Getter_any_testsuite, |
| 223 | public ::testing::WithParamInterface<Scalar::Type> {}; |
| 224 | |
| 225 | TEST_P(Getter_scalar_type_testsuite, put_throwError_whenNotSupportedType) { |
| 226 | any.set_type(Any::SCALAR); |
| 227 | any.mutable_scalar()->set_type(GetParam()); |
| 228 | |
| 229 | ASSERT_THROW(ngs::Getter_any::put_scalar_value_to_functor(any, *this), |
| 230 | ngs::Error_code); |
| 231 | } |
| 232 | |
| 233 | INSTANTIATE_TEST_SUITE_P(InstantiationNegativeTests, |
| 234 | Getter_scalar_type_testsuite, |
| 235 | ::testing::Values(Scalar::V_SINT, Scalar::V_UINT, |
| 236 | Scalar::V_BOOL, Scalar::V_FLOAT, |
| 237 | Scalar::V_DOUBLE, Scalar::V_STRING, |
| 238 | Scalar::V_OCTETS)); |
| 239 | |
| 240 | } // namespace test |
| 241 | } // namespace xpl |