|
| 1 | +/* |
| 2 | + Copyright (c) DataStax, Inc. |
| 3 | +
|
| 4 | + This software can be used solely with DataStax Enterprise. Please consult the |
| 5 | + license at http://www.datastax.com/terms/datastax-dse-driver-license-terms |
| 6 | +*/ |
| 7 | + |
| 8 | +#include "loop_test.hpp" |
| 9 | + |
| 10 | +#include "options_request.hpp" |
| 11 | +#include "request_callback.hpp" |
| 12 | +#include "supported_response.hpp" |
| 13 | + |
| 14 | +using namespace datastax; |
| 15 | +using namespace datastax::internal; |
| 16 | +using namespace datastax::internal::core; |
| 17 | + |
| 18 | +class SupportedResponseUnitTest : public LoopTest { |
| 19 | +public: |
| 20 | + const mockssandra::RequestHandler* simple_cluster_with_options() { |
| 21 | + mockssandra::SimpleRequestHandlerBuilder builder; |
| 22 | + builder.on(mockssandra::OPCODE_OPTIONS).execute(new SupportedOptions()); |
| 23 | + return builder.build(); |
| 24 | + } |
| 25 | + |
| 26 | +public: |
| 27 | + static void on_connect(Connector* connector, StringMultimap* supported_options) { |
| 28 | + ASSERT_TRUE(connector->is_ok()); |
| 29 | + *supported_options = connector->supported_options(); |
| 30 | + } |
| 31 | + |
| 32 | +private: |
| 33 | + class SupportedOptions : public mockssandra::Action { |
| 34 | + public: |
| 35 | + virtual void on_run(mockssandra::Request* request) const { |
| 36 | + Vector<String> compression; |
| 37 | + Vector<String> cql_version; |
| 38 | + Vector<String> protocol_versions; |
| 39 | + compression.push_back("snappy"); |
| 40 | + compression.push_back("lz4"); |
| 41 | + cql_version.push_back("3.4.5"); |
| 42 | + protocol_versions.push_back("3/v3"); |
| 43 | + protocol_versions.push_back("4/v4"); |
| 44 | + |
| 45 | + StringMultimap supported; |
| 46 | + supported["COMPRESSION"] = compression; |
| 47 | + supported["CQL_VERSION"] = cql_version; |
| 48 | + supported["PROTOCOL_VERSIONS"] = protocol_versions; |
| 49 | + |
| 50 | + String body; |
| 51 | + mockssandra::encode_string_map(supported, &body); |
| 52 | + request->write(mockssandra::OPCODE_SUPPORTED, body); |
| 53 | + } |
| 54 | + }; |
| 55 | +}; |
| 56 | + |
| 57 | +TEST_F(SupportedResponseUnitTest, Simple) { |
| 58 | + mockssandra::SimpleCluster cluster(simple_cluster_with_options()); |
| 59 | + ASSERT_EQ(cluster.start_all(), 0); |
| 60 | + |
| 61 | + StringMultimap supported_options; |
| 62 | + ASSERT_EQ(0, supported_options.size()); |
| 63 | + Connector::Ptr connector(new Connector(Host::Ptr(new Host(Address("127.0.0.1", PORT))), |
| 64 | + PROTOCOL_VERSION, |
| 65 | + bind_callback(on_connect, &supported_options))); |
| 66 | + connector->connect(loop()); |
| 67 | + uv_run(loop(), UV_RUN_DEFAULT); |
| 68 | + |
| 69 | + ASSERT_EQ(3u, supported_options.size()); |
| 70 | + { |
| 71 | + Vector<String> compression = supported_options.find("COMPRESSION")->second; |
| 72 | + ASSERT_EQ(2u, compression.size()); |
| 73 | + EXPECT_EQ("snappy", compression[0]); |
| 74 | + EXPECT_EQ("lz4", compression[1]); |
| 75 | + } |
| 76 | + { |
| 77 | + Vector<String> cql_version = supported_options.find("CQL_VERSION")->second; |
| 78 | + ASSERT_EQ(1u, cql_version.size()); |
| 79 | + EXPECT_EQ("3.4.5", cql_version[0]); |
| 80 | + } |
| 81 | + { |
| 82 | + Vector<String> protocol_versions = supported_options.find("PROTOCOL_VERSIONS")->second; |
| 83 | + ASSERT_EQ(2u, protocol_versions.size()); |
| 84 | + EXPECT_EQ("3/v3", protocol_versions[0]); |
| 85 | + EXPECT_EQ("4/v4", protocol_versions[1]); |
| 86 | + } |
| 87 | + |
| 88 | + { // Non-existent key |
| 89 | + EXPECT_EQ(supported_options.end(), supported_options.find("invalid")); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +TEST_F(SupportedResponseUnitTest, UppercaseKeysOnly) { |
| 94 | + class CaseInsensitiveSupportedOptions : public mockssandra::Action { |
| 95 | + public: |
| 96 | + virtual void on_run(mockssandra::Request* request) const { |
| 97 | + Vector<String> camel_key; |
| 98 | + camel_key.push_back("success"); |
| 99 | + |
| 100 | + StringMultimap supported; |
| 101 | + supported["CamEL_KeY"] = camel_key; |
| 102 | + |
| 103 | + String body; |
| 104 | + mockssandra::encode_string_map(supported, &body); |
| 105 | + request->write(mockssandra::OPCODE_SUPPORTED, body); |
| 106 | + } |
| 107 | + }; |
| 108 | + |
| 109 | + mockssandra::SimpleRequestHandlerBuilder builder; |
| 110 | + builder.on(mockssandra::OPCODE_OPTIONS).execute(new CaseInsensitiveSupportedOptions()); |
| 111 | + mockssandra::SimpleCluster cluster(builder.build()); |
| 112 | + ASSERT_EQ(cluster.start_all(), 0); |
| 113 | + |
| 114 | + StringMultimap supported_options; |
| 115 | + ASSERT_EQ(0, supported_options.size()); |
| 116 | + Connector::Ptr connector(new Connector(Host::Ptr(new Host(Address("127.0.0.1", PORT))), |
| 117 | + PROTOCOL_VERSION, |
| 118 | + bind_callback(on_connect, &supported_options))); |
| 119 | + connector->connect(loop()); |
| 120 | + uv_run(loop(), UV_RUN_DEFAULT); |
| 121 | + |
| 122 | + ASSERT_EQ(1u, supported_options.size()); |
| 123 | + { // Uppercase |
| 124 | + Vector<String> uppercase = supported_options.find("CAMEL_KEY")->second; |
| 125 | + ASSERT_EQ(1u, uppercase.size()); |
| 126 | + EXPECT_EQ("success", uppercase[0]); |
| 127 | + } |
| 128 | + { // Exact key |
| 129 | + EXPECT_EQ(supported_options.end(), supported_options.find("CamEL_KeY")); |
| 130 | + } |
| 131 | +} |
0 commit comments