Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 1 | /* |
| 2 | Copyright (c) 2018-2019 Nokia. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | #include "private/redis/contentsbuilder.hpp" |
| 19 | #include "private/redis/contents.hpp" |
| 20 | |
| 21 | using namespace shareddatalayer; |
| 22 | using namespace shareddatalayer::redis; |
| 23 | using namespace testing; |
| 24 | |
| 25 | namespace |
| 26 | { |
| 27 | class ContentsBuilderTest: public testing::Test |
| 28 | { |
| 29 | public: |
| 30 | std::unique_ptr<ContentsBuilder> contentsBuilder; |
| 31 | AsyncConnection::Namespace ns; |
| 32 | char nsKeySeparator; |
| 33 | std::string string; |
| 34 | std::string string2; |
| 35 | std::string string3; |
| 36 | AsyncConnection::Key key; |
| 37 | AsyncConnection::Key key2; |
| 38 | AsyncConnection::Data data; |
| 39 | AsyncConnection::Data data2; |
| 40 | AsyncConnection::Keys keys; |
| 41 | AsyncConnection::DataMap dataMap; |
| 42 | |
| 43 | ContentsBuilderTest(): |
| 44 | ns("ns"), |
| 45 | nsKeySeparator('#'), |
| 46 | string("string"), |
| 47 | string2("string2"), |
| 48 | string3("string3"), |
| 49 | key("key"), |
| 50 | key2("key2"), |
| 51 | data({11,12}), |
| 52 | data2({21,22}), |
| 53 | keys({key,key2}), |
| 54 | dataMap({{key,data},{key2,data2}}) |
| 55 | { |
| 56 | contentsBuilder.reset(new ContentsBuilder(nsKeySeparator)); |
| 57 | } |
| 58 | |
| 59 | void expectStringInContents(Contents& contents, std::string& string, int pos) |
| 60 | { |
| 61 | EXPECT_EQ(string, contents.stack[pos]); |
| 62 | EXPECT_EQ(string.size(), contents.sizes[pos]); |
| 63 | } |
| 64 | |
| 65 | void expectDataMapInContents(Contents& contents, int pos) |
| 66 | { |
| 67 | expectKeyInContents(contents, key, pos); |
| 68 | expectDataInContents(contents, data, pos+1); |
| 69 | expectKeyInContents(contents, key2, pos+2); |
| 70 | expectDataInContents(contents, data2, pos+3); |
| 71 | } |
| 72 | |
| 73 | void expectKeyInContents(Contents& contents, AsyncConnection::Key& key, int pos) |
| 74 | { |
| 75 | AsyncConnection::Key keyWithPrefix('{' + ns + '}' + nsKeySeparator + key); |
| 76 | EXPECT_EQ(keyWithPrefix, contents.stack[pos]); |
| 77 | EXPECT_EQ(keyWithPrefix.size(), contents.sizes[pos]); |
| 78 | } |
| 79 | |
| 80 | void expectDataInContents(Contents& contents, AsyncConnection::Data& data, int pos) |
| 81 | { |
| 82 | EXPECT_EQ(data.size(), contents.sizes[pos]); |
| 83 | std::string dataAsString(data.begin(), data.end()); |
| 84 | EXPECT_EQ(dataAsString, contents.stack[pos]); |
| 85 | } |
| 86 | |
| 87 | void expectMessageInContents(Contents& contents, int pos) |
| 88 | { |
| 89 | EXPECT_EQ(string2, contents.stack[pos]); |
| 90 | EXPECT_EQ(string2.size(), contents.sizes[pos]); |
| 91 | } |
| 92 | |
| 93 | void expectKeysInContents(Contents& contents, int pos) |
| 94 | { |
| 95 | expectKeyInContents(contents, key, pos); |
| 96 | expectKeyInContents(contents, key2, pos+1); |
| 97 | } |
| 98 | }; |
| 99 | } |
| 100 | |
| 101 | TEST_F(ContentsBuilderTest, BuildWithString) |
| 102 | { |
| 103 | auto contents(contentsBuilder->build(string)); |
| 104 | EXPECT_EQ(size_t(1), contents.stack.size()); |
| 105 | EXPECT_EQ(size_t(1), contents.sizes.size()); |
| 106 | expectStringInContents(contents, string, 0); |
| 107 | } |
| 108 | |
| 109 | TEST_F(ContentsBuilderTest, BuildWithStringAndString2) |
| 110 | { |
| 111 | auto contents(contentsBuilder->build(string, string2)); |
| 112 | EXPECT_EQ(size_t(2), contents.stack.size()); |
| 113 | EXPECT_EQ(size_t(2), contents.sizes.size()); |
| 114 | expectStringInContents(contents, string, 0); |
| 115 | expectStringInContents(contents, string2, 1); |
| 116 | } |
| 117 | |
| 118 | TEST_F(ContentsBuilderTest, BuildWithStringString2AndString3) |
| 119 | { |
| 120 | auto contents(contentsBuilder->build(string, string2, string3)); |
| 121 | EXPECT_EQ(size_t(3), contents.stack.size()); |
| 122 | EXPECT_EQ(size_t(3), contents.sizes.size()); |
| 123 | expectStringInContents(contents, string, 0); |
| 124 | expectStringInContents(contents, string2, 1); |
| 125 | expectStringInContents(contents, string3, 2); |
| 126 | } |
| 127 | |
| 128 | TEST_F(ContentsBuilderTest, BuildWithStringAndDataMap) |
| 129 | { |
| 130 | auto contents(contentsBuilder->build(string, ns, dataMap)); |
| 131 | EXPECT_EQ(size_t(5), contents.stack.size()); |
| 132 | EXPECT_EQ(size_t(5), contents.sizes.size()); |
| 133 | expectStringInContents(contents, string, 0); |
| 134 | expectDataMapInContents(contents, 1); |
| 135 | } |
| 136 | |
| 137 | TEST_F(ContentsBuilderTest, BuildWithStringDataMapAndTwoStrings) |
| 138 | { |
| 139 | auto contents(contentsBuilder->build(string, ns, dataMap, string2, string3)); |
| 140 | EXPECT_EQ(size_t(7), contents.stack.size()); |
| 141 | EXPECT_EQ(size_t(7), contents.sizes.size()); |
| 142 | expectStringInContents(contents, string, 0); |
| 143 | expectDataMapInContents(contents, 1); |
| 144 | expectStringInContents(contents, string2, 5); |
| 145 | expectStringInContents(contents, string3, 6); |
| 146 | } |
| 147 | |
| 148 | TEST_F(ContentsBuilderTest, BuildWithStringKeyAndData) |
| 149 | { |
| 150 | auto contents(contentsBuilder->build(string, ns, key, data)); |
| 151 | EXPECT_EQ(size_t(3), contents.stack.size()); |
| 152 | EXPECT_EQ(size_t(3), contents.sizes.size()); |
| 153 | expectStringInContents(contents, string, 0); |
| 154 | expectKeyInContents(contents, key, 1); |
| 155 | expectDataInContents(contents, data, 2); |
| 156 | } |
| 157 | |
| 158 | TEST_F(ContentsBuilderTest, BuildWithStringKeyDataAndTwoStrings) |
| 159 | { |
| 160 | auto contents(contentsBuilder->build(string, ns, key, data, string2, string3)); |
| 161 | EXPECT_EQ(size_t(5), contents.stack.size()); |
| 162 | EXPECT_EQ(size_t(5), contents.sizes.size()); |
| 163 | expectStringInContents(contents, string, 0); |
| 164 | expectKeyInContents(contents, key, 1); |
| 165 | expectDataInContents(contents, data, 2); |
| 166 | expectStringInContents(contents, string2, 3); |
| 167 | expectStringInContents(contents, string3, 4); |
| 168 | } |
| 169 | |
| 170 | TEST_F(ContentsBuilderTest, BuildWithStringKeyAndTwoDatas) |
| 171 | { |
| 172 | auto contents(contentsBuilder->build(string, ns, key, data, data2)); |
| 173 | EXPECT_EQ(size_t(4), contents.stack.size()); |
| 174 | EXPECT_EQ(size_t(4), contents.sizes.size()); |
| 175 | expectStringInContents(contents, string, 0); |
| 176 | expectKeyInContents(contents, key, 1); |
| 177 | expectDataInContents(contents, data, 2); |
| 178 | expectDataInContents(contents, data2, 3); |
| 179 | } |
| 180 | |
| 181 | TEST_F(ContentsBuilderTest, BuildWithStringKeyTwoDatasAndTwoStrings) |
| 182 | { |
| 183 | auto contents(contentsBuilder->build(string, ns, key, data, data2, string2, string3)); |
| 184 | EXPECT_EQ(size_t(6), contents.stack.size()); |
| 185 | EXPECT_EQ(size_t(6), contents.sizes.size()); |
| 186 | expectStringInContents(contents, string, 0); |
| 187 | expectKeyInContents(contents, key, 1); |
| 188 | expectDataInContents(contents, data, 2); |
| 189 | expectDataInContents(contents, data2, 3); |
| 190 | expectStringInContents(contents, string2, 4); |
| 191 | expectStringInContents(contents, string3, 5); |
| 192 | } |
| 193 | |
| 194 | TEST_F(ContentsBuilderTest, BuildWithStringAndKeys) |
| 195 | { |
| 196 | auto contents(contentsBuilder->build(string, ns, keys)); |
| 197 | EXPECT_EQ(size_t(3), contents.stack.size()); |
| 198 | EXPECT_EQ(size_t(3), contents.sizes.size()); |
| 199 | expectStringInContents(contents, string, 0); |
| 200 | expectKeysInContents(contents, 1); |
| 201 | } |
| 202 | |
| 203 | TEST_F(ContentsBuilderTest, BuildWithStringKeysAndTwoStrings) |
| 204 | { |
| 205 | auto contents(contentsBuilder->build(string, ns, keys, string2, string3)); |
| 206 | EXPECT_EQ(size_t(5), contents.stack.size()); |
| 207 | EXPECT_EQ(size_t(5), contents.sizes.size()); |
| 208 | expectStringInContents(contents, string, 0); |
| 209 | expectKeysInContents(contents, 1); |
| 210 | expectStringInContents(contents, string2, 3); |
| 211 | expectStringInContents(contents, string3, 4); |
| 212 | } |