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 | |
Timo Tietavainen | a0745d2 | 2019-11-28 09:55:22 +0200 | [diff] [blame^] | 17 | /* |
| 18 | * This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 19 | * platform project (RICP). |
| 20 | */ |
| 21 | |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 22 | #include <gtest/gtest.h> |
| 23 | #include "private/redis/contentsbuilder.hpp" |
| 24 | #include "private/redis/contents.hpp" |
| 25 | |
| 26 | using namespace shareddatalayer; |
| 27 | using namespace shareddatalayer::redis; |
| 28 | using namespace testing; |
| 29 | |
| 30 | namespace |
| 31 | { |
| 32 | class ContentsBuilderTest: public testing::Test |
| 33 | { |
| 34 | public: |
| 35 | std::unique_ptr<ContentsBuilder> contentsBuilder; |
| 36 | AsyncConnection::Namespace ns; |
| 37 | char nsKeySeparator; |
| 38 | std::string string; |
| 39 | std::string string2; |
| 40 | std::string string3; |
| 41 | AsyncConnection::Key key; |
| 42 | AsyncConnection::Key key2; |
| 43 | AsyncConnection::Data data; |
| 44 | AsyncConnection::Data data2; |
| 45 | AsyncConnection::Keys keys; |
| 46 | AsyncConnection::DataMap dataMap; |
| 47 | |
| 48 | ContentsBuilderTest(): |
| 49 | ns("ns"), |
| 50 | nsKeySeparator('#'), |
| 51 | string("string"), |
| 52 | string2("string2"), |
| 53 | string3("string3"), |
| 54 | key("key"), |
| 55 | key2("key2"), |
| 56 | data({11,12}), |
| 57 | data2({21,22}), |
| 58 | keys({key,key2}), |
| 59 | dataMap({{key,data},{key2,data2}}) |
| 60 | { |
| 61 | contentsBuilder.reset(new ContentsBuilder(nsKeySeparator)); |
| 62 | } |
| 63 | |
| 64 | void expectStringInContents(Contents& contents, std::string& string, int pos) |
| 65 | { |
| 66 | EXPECT_EQ(string, contents.stack[pos]); |
| 67 | EXPECT_EQ(string.size(), contents.sizes[pos]); |
| 68 | } |
| 69 | |
| 70 | void expectDataMapInContents(Contents& contents, int pos) |
| 71 | { |
| 72 | expectKeyInContents(contents, key, pos); |
| 73 | expectDataInContents(contents, data, pos+1); |
| 74 | expectKeyInContents(contents, key2, pos+2); |
| 75 | expectDataInContents(contents, data2, pos+3); |
| 76 | } |
| 77 | |
| 78 | void expectKeyInContents(Contents& contents, AsyncConnection::Key& key, int pos) |
| 79 | { |
| 80 | AsyncConnection::Key keyWithPrefix('{' + ns + '}' + nsKeySeparator + key); |
| 81 | EXPECT_EQ(keyWithPrefix, contents.stack[pos]); |
| 82 | EXPECT_EQ(keyWithPrefix.size(), contents.sizes[pos]); |
| 83 | } |
| 84 | |
| 85 | void expectDataInContents(Contents& contents, AsyncConnection::Data& data, int pos) |
| 86 | { |
| 87 | EXPECT_EQ(data.size(), contents.sizes[pos]); |
| 88 | std::string dataAsString(data.begin(), data.end()); |
| 89 | EXPECT_EQ(dataAsString, contents.stack[pos]); |
| 90 | } |
| 91 | |
| 92 | void expectMessageInContents(Contents& contents, int pos) |
| 93 | { |
| 94 | EXPECT_EQ(string2, contents.stack[pos]); |
| 95 | EXPECT_EQ(string2.size(), contents.sizes[pos]); |
| 96 | } |
| 97 | |
| 98 | void expectKeysInContents(Contents& contents, int pos) |
| 99 | { |
| 100 | expectKeyInContents(contents, key, pos); |
| 101 | expectKeyInContents(contents, key2, pos+1); |
| 102 | } |
| 103 | }; |
| 104 | } |
| 105 | |
| 106 | TEST_F(ContentsBuilderTest, BuildWithString) |
| 107 | { |
| 108 | auto contents(contentsBuilder->build(string)); |
| 109 | EXPECT_EQ(size_t(1), contents.stack.size()); |
| 110 | EXPECT_EQ(size_t(1), contents.sizes.size()); |
| 111 | expectStringInContents(contents, string, 0); |
| 112 | } |
| 113 | |
| 114 | TEST_F(ContentsBuilderTest, BuildWithStringAndString2) |
| 115 | { |
| 116 | auto contents(contentsBuilder->build(string, string2)); |
| 117 | EXPECT_EQ(size_t(2), contents.stack.size()); |
| 118 | EXPECT_EQ(size_t(2), contents.sizes.size()); |
| 119 | expectStringInContents(contents, string, 0); |
| 120 | expectStringInContents(contents, string2, 1); |
| 121 | } |
| 122 | |
| 123 | TEST_F(ContentsBuilderTest, BuildWithStringString2AndString3) |
| 124 | { |
| 125 | auto contents(contentsBuilder->build(string, string2, string3)); |
| 126 | EXPECT_EQ(size_t(3), contents.stack.size()); |
| 127 | EXPECT_EQ(size_t(3), contents.sizes.size()); |
| 128 | expectStringInContents(contents, string, 0); |
| 129 | expectStringInContents(contents, string2, 1); |
| 130 | expectStringInContents(contents, string3, 2); |
| 131 | } |
| 132 | |
| 133 | TEST_F(ContentsBuilderTest, BuildWithStringAndDataMap) |
| 134 | { |
| 135 | auto contents(contentsBuilder->build(string, ns, dataMap)); |
| 136 | EXPECT_EQ(size_t(5), contents.stack.size()); |
| 137 | EXPECT_EQ(size_t(5), contents.sizes.size()); |
| 138 | expectStringInContents(contents, string, 0); |
| 139 | expectDataMapInContents(contents, 1); |
| 140 | } |
| 141 | |
| 142 | TEST_F(ContentsBuilderTest, BuildWithStringDataMapAndTwoStrings) |
| 143 | { |
| 144 | auto contents(contentsBuilder->build(string, ns, dataMap, string2, string3)); |
| 145 | EXPECT_EQ(size_t(7), contents.stack.size()); |
| 146 | EXPECT_EQ(size_t(7), contents.sizes.size()); |
| 147 | expectStringInContents(contents, string, 0); |
| 148 | expectDataMapInContents(contents, 1); |
| 149 | expectStringInContents(contents, string2, 5); |
| 150 | expectStringInContents(contents, string3, 6); |
| 151 | } |
| 152 | |
| 153 | TEST_F(ContentsBuilderTest, BuildWithStringKeyAndData) |
| 154 | { |
| 155 | auto contents(contentsBuilder->build(string, ns, key, data)); |
| 156 | EXPECT_EQ(size_t(3), contents.stack.size()); |
| 157 | EXPECT_EQ(size_t(3), contents.sizes.size()); |
| 158 | expectStringInContents(contents, string, 0); |
| 159 | expectKeyInContents(contents, key, 1); |
| 160 | expectDataInContents(contents, data, 2); |
| 161 | } |
| 162 | |
| 163 | TEST_F(ContentsBuilderTest, BuildWithStringKeyDataAndTwoStrings) |
| 164 | { |
| 165 | auto contents(contentsBuilder->build(string, ns, key, data, string2, string3)); |
| 166 | EXPECT_EQ(size_t(5), contents.stack.size()); |
| 167 | EXPECT_EQ(size_t(5), contents.sizes.size()); |
| 168 | expectStringInContents(contents, string, 0); |
| 169 | expectKeyInContents(contents, key, 1); |
| 170 | expectDataInContents(contents, data, 2); |
| 171 | expectStringInContents(contents, string2, 3); |
| 172 | expectStringInContents(contents, string3, 4); |
| 173 | } |
| 174 | |
| 175 | TEST_F(ContentsBuilderTest, BuildWithStringKeyAndTwoDatas) |
| 176 | { |
| 177 | auto contents(contentsBuilder->build(string, ns, key, data, data2)); |
| 178 | EXPECT_EQ(size_t(4), contents.stack.size()); |
| 179 | EXPECT_EQ(size_t(4), contents.sizes.size()); |
| 180 | expectStringInContents(contents, string, 0); |
| 181 | expectKeyInContents(contents, key, 1); |
| 182 | expectDataInContents(contents, data, 2); |
| 183 | expectDataInContents(contents, data2, 3); |
| 184 | } |
| 185 | |
| 186 | TEST_F(ContentsBuilderTest, BuildWithStringKeyTwoDatasAndTwoStrings) |
| 187 | { |
| 188 | auto contents(contentsBuilder->build(string, ns, key, data, data2, string2, string3)); |
| 189 | EXPECT_EQ(size_t(6), contents.stack.size()); |
| 190 | EXPECT_EQ(size_t(6), contents.sizes.size()); |
| 191 | expectStringInContents(contents, string, 0); |
| 192 | expectKeyInContents(contents, key, 1); |
| 193 | expectDataInContents(contents, data, 2); |
| 194 | expectDataInContents(contents, data2, 3); |
| 195 | expectStringInContents(contents, string2, 4); |
| 196 | expectStringInContents(contents, string3, 5); |
| 197 | } |
| 198 | |
| 199 | TEST_F(ContentsBuilderTest, BuildWithStringAndKeys) |
| 200 | { |
| 201 | auto contents(contentsBuilder->build(string, ns, keys)); |
| 202 | EXPECT_EQ(size_t(3), contents.stack.size()); |
| 203 | EXPECT_EQ(size_t(3), contents.sizes.size()); |
| 204 | expectStringInContents(contents, string, 0); |
| 205 | expectKeysInContents(contents, 1); |
| 206 | } |
| 207 | |
| 208 | TEST_F(ContentsBuilderTest, BuildWithStringKeysAndTwoStrings) |
| 209 | { |
| 210 | auto contents(contentsBuilder->build(string, ns, keys, string2, string3)); |
| 211 | EXPECT_EQ(size_t(5), contents.stack.size()); |
| 212 | EXPECT_EQ(size_t(5), contents.sizes.size()); |
| 213 | expectStringInContents(contents, string, 0); |
| 214 | expectKeysInContents(contents, 1); |
| 215 | expectStringInContents(contents, string2, 3); |
| 216 | expectStringInContents(contents, string3, 4); |
| 217 | } |