blob: 8c4e0b6e61d0988257970719a19fbc3d5792b2eb [file] [log] [blame]
Rolf Badorekef2bf512019-08-20 11:17:15 +03001/*
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 <cstring>
18#include "private/abort.hpp"
19#include "private/tst/redisreplybuilder.hpp"
20
21using namespace shareddatalayer;
22using namespace shareddatalayer::redis;
23using namespace shareddatalayer::tst;
24
25RedisReplyBuilder::RedisReplyBuilder():
26 builtRedisReplies({}),
27 defaultStringValue({ 'a', 'b', 'c' }),
28 errorMessage(nullptr),
29 requiredRedisModuleCommands(getRequiredRedisModuleCommands()),
30 commandItems({}),
31 commandListQueryElements({})
32{
33 initArrayReplyContent();
34 initCommandListQueryReplyContent();
35}
36
37RedisReplyBuilder::~RedisReplyBuilder()
38{
39 delete[] errorMessage;
40
41 for (auto& redisReplyPtr : commandListQueryElements)
42 {
43 delete *(redisReplyPtr->element);
44 delete redisReplyPtr;
45 }
46
47 for (auto& redisReplyPtr : builtRedisReplies)
48 {
49 delete redisReplyPtr;
50 }
51}
52
53void RedisReplyBuilder::initArrayReplyContent()
54{
55 if (!arrayReplyElements.empty())
56 SHAREDDATALAYER_ABORT("ArrayReplyContent initialization should done only once");
57
58 arrayReplyElement1.type = REDIS_REPLY_STRING;
59 arrayReplyElement1.integer = 0;
60 arrayReplyElement1.str = const_cast<char*>(&defaultStringValue[0]);
61 arrayReplyElement1.len = static_cast<int>(defaultStringValue.size());
62 arrayReplyElements.push_back(&arrayReplyElement1);
63 arrayReplyElement2.type = REDIS_REPLY_NIL;
64 arrayReplyElement2.integer = 0;
65 arrayReplyElement2.str = nullptr;
66 arrayReplyElement2.len = 0;
67 arrayReplyElements.push_back(&arrayReplyElement2);
68}
69
70void RedisReplyBuilder::initCommandListQueryReplyContent()
71{
72 if (!commandItems.empty() || !commandListQueryElements.empty())
73 SHAREDDATALAYER_ABORT("CommandListQueryReplyContent initialization should done only once");
74
75 redisReply * commandItem = nullptr;
76 redisReply * arrayItem = nullptr;
77
78 for (auto& requiredRedisModuleCommand : requiredRedisModuleCommands)
79 {
80 commandItem = new redisReply();
81 commandItem->type = REDIS_REPLY_STRING;
82 commandItem->integer = 0;
83 commandItem->str = const_cast<char*>(requiredRedisModuleCommand.c_str());
84 commandItem->len = static_cast<int>(requiredRedisModuleCommand.size());
85 commandItems.push_back(commandItem);
86 }
87
88 for (auto& commandItem : commandItems)
89 {
90 arrayItem = new redisReply();
91 arrayItem->type = REDIS_REPLY_ARRAY;
92 arrayItem->elements = 1;
93 arrayItem->element = &commandItem;
94
95 commandListQueryElements.push_back(arrayItem);
96 }
97}
98
99redisReply& RedisReplyBuilder::buildNilReply()
100{
101 auto rr = new redisReply();
102 rr->type = REDIS_REPLY_NIL;
103 builtRedisReplies.push_back(rr);
104 return std::ref(*rr);
105}
106
107redisReply& RedisReplyBuilder::buildIntegerReply()
108{
109 auto rr = new redisReply();
110 rr->type = REDIS_REPLY_INTEGER;
111 rr->integer = 10;
112 builtRedisReplies.push_back(rr);
113 return std::ref(*rr);
114}
115
116redisReply& RedisReplyBuilder::buildStatusReply()
117{
118 auto rr = new redisReply();
119 rr->type = REDIS_REPLY_STATUS;
120 rr->str = const_cast<char*>(&defaultStringValue[0]);
121 rr->len = static_cast<int>(defaultStringValue.size());
122 builtRedisReplies.push_back(rr);
123 return std::ref(*rr);
124}
125
126redisReply& RedisReplyBuilder::buildStringReply()
127{
128 auto rr = new redisReply();
129 rr->type = REDIS_REPLY_STRING;
130 rr->str = const_cast<char*>(&defaultStringValue[0]);
131 rr->len = static_cast<int>(defaultStringValue.size());
132 builtRedisReplies.push_back(rr);
133 return std::ref(*rr);
134}
135
136redisReply& RedisReplyBuilder::buildArrayReply()
137{
138 auto rr = new redisReply();
139 rr->type = REDIS_REPLY_ARRAY;
140 rr->elements = 2;
141 rr->element = &arrayReplyElements[0];
142 builtRedisReplies.push_back(rr);
143 return std::ref(*rr);
144}
145
146redisReply& RedisReplyBuilder::buildCommandListQueryReply()
147{
148 if (commandListQueryElements.empty())
149 SHAREDDATALAYER_ABORT("Cannot built command list query reply");
150
151 auto rr = new redisReply();
152 rr->type = REDIS_REPLY_ARRAY;
153 rr->elements = commandListQueryElements.size();
154 rr->element = &commandListQueryElements[0];
155 builtRedisReplies.push_back(rr);
156 return std::ref(*rr);
157}
158
159redisReply& RedisReplyBuilder::buildIncompleteCommandListQueryReply()
160{
161 if (commandListQueryElements.empty())
162 SHAREDDATALAYER_ABORT("Cannot built incomplete command list query reply");
163
164 auto rr = new redisReply();
165 rr->type = REDIS_REPLY_ARRAY;
166 rr->elements = commandListQueryElements.size() - 1;
167 rr->element = &commandListQueryElements[0];
168 builtRedisReplies.push_back(rr);
169 return std::ref(*rr);
170}
171
172redisReply& RedisReplyBuilder::buildErrorReply(const std::string& msg)
173{
174 auto rr = new redisReply();
175 auto len(msg.size());
176 delete[] errorMessage;
177 errorMessage = new char[len];
178 std::memcpy(errorMessage, msg.c_str(), len);
179 rr->type = REDIS_REPLY_ERROR;
180 rr->str = errorMessage;
181 rr->len = static_cast<int>(len);
182 builtRedisReplies.push_back(rr);
183 return std::ref(*rr);
184}