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