comparison UnitTestsSources/LuaTests.cpp @ 1364:111e23bb4904 query-retrieve

integration mainline->query-retrieve
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 21 May 2015 16:58:30 +0200
parents 6e7e5ed91c2d
children ac4efabeb80c 6784a119484d
comparison
equal deleted inserted replaced
953:f894be6e7cc1 1364:111e23bb4904
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * In addition, as a special exception, the copyright holders of this
12 * program give permission to link the code of its release with the
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
14 * that use the same license as the "OpenSSL" library), and distribute
15 * the linked executables. You must obey the GNU General Public License
16 * in all respects for all of the code used other than "OpenSSL". If you
17 * modify file(s) with this exception, you may extend this exception to
18 * your version of the file(s), but you are not obligated to do so. If
19 * you do not wish to do so, delete this exception statement from your
20 * version. If you delete this exception statement from all source files
21 * in the program, then also delete it here.
22 *
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 **/
31
32
33 #include "PrecompiledHeadersUnitTests.h"
34 #include "gtest/gtest.h"
35
36 #include "../Core/Toolbox.h"
37 #include "../Core/Lua/LuaFunctionCall.h"
38
39 #include <boost/lexical_cast.hpp>
40
41 #if !defined(UNIT_TESTS_WITH_HTTP_CONNEXIONS)
42 #error "Please set UNIT_TESTS_WITH_HTTP_CONNEXIONS"
43 #endif
44
45
46 TEST(Lua, Json)
47 {
48 Orthanc::LuaContext lua;
49 lua.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX);
50 lua.Execute("a={}");
51 lua.Execute("a['x'] = 10");
52 lua.Execute("a['y'] = {}");
53 lua.Execute("a['y'][1] = 20");
54 lua.Execute("a['y'][2] = 20");
55 lua.Execute("PrintRecursive(a)");
56
57 lua.Execute("function f(a) print(a.bool) return a.bool,20,30,40,50,60 end");
58
59 Json::Value v, vv, o;
60 //v["a"] = "b";
61 v.append("hello");
62 v.append("world");
63 v.append("42");
64 vv.append("sub");
65 vv.append("set");
66 v.append(vv);
67 o = Json::objectValue;
68 o["x"] = 10;
69 o["y"] = 20;
70 o["z"] = 20.5f;
71 v.append(o);
72
73 {
74 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
75 f.PushJson(v);
76 f.Execute();
77 }
78
79 {
80 Orthanc::LuaFunctionCall f(lua, "f");
81 f.PushJson(o);
82 ASSERT_THROW(f.ExecutePredicate(), Orthanc::LuaException);
83 }
84
85 o["bool"] = false;
86
87 {
88 Orthanc::LuaFunctionCall f(lua, "f");
89 f.PushJson(o);
90 ASSERT_FALSE(f.ExecutePredicate());
91 }
92
93 o["bool"] = true;
94
95 {
96 Orthanc::LuaFunctionCall f(lua, "f");
97 f.PushJson(o);
98 ASSERT_TRUE(f.ExecutePredicate());
99 }
100 }
101
102
103 TEST(Lua, Existing)
104 {
105 Orthanc::LuaContext lua;
106 lua.Execute("a={}");
107 lua.Execute("function f() end");
108
109 ASSERT_TRUE(lua.IsExistingFunction("f"));
110 ASSERT_FALSE(lua.IsExistingFunction("a"));
111 ASSERT_FALSE(lua.IsExistingFunction("Dummy"));
112 }
113
114
115 TEST(Lua, Simple)
116 {
117 Orthanc::LuaContext lua;
118 lua.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX);
119
120 {
121 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
122 f.PushString("hello");
123 f.Execute();
124 }
125
126 {
127 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
128 f.PushBoolean(true);
129 f.Execute();
130 }
131
132 {
133 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
134 f.PushInteger(42);
135 f.Execute();
136 }
137
138 {
139 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
140 f.PushDouble(3.1415);
141 f.Execute();
142 }
143 }
144
145
146 TEST(Lua, ReturnJson)
147 {
148 Json::Value b = Json::objectValue;
149 b["a"] = 42;
150 b["b"] = 44;
151 b["c"] = 43;
152
153 Json::Value c = Json::arrayValue;
154 c.append("test3");
155 c.append("test1");
156 c.append("test2");
157
158 Json::Value a = Json::objectValue;
159 a["Hello"] = "World";
160 a["List"] = Json::arrayValue;
161 a["List"].append(b);
162 a["List"].append(c);
163
164 Orthanc::LuaContext lua;
165
166 // This is the identity function (it simply returns its input)
167 lua.Execute("function identity(a) return a end");
168
169 {
170 Orthanc::LuaFunctionCall f(lua, "identity");
171 f.PushJson("hello");
172 Json::Value v;
173 f.ExecuteToJson(v);
174 ASSERT_EQ("hello", v.asString());
175 }
176
177 {
178 Orthanc::LuaFunctionCall f(lua, "identity");
179 f.PushJson(42.25);
180 Json::Value v;
181 f.ExecuteToJson(v);
182 ASSERT_FLOAT_EQ(42.25f, v.asFloat());
183 }
184
185 {
186 Orthanc::LuaFunctionCall f(lua, "identity");
187 Json::Value vv = Json::arrayValue;
188 f.PushJson(vv);
189 Json::Value v;
190 f.ExecuteToJson(v);
191 ASSERT_EQ(Json::arrayValue, v.type());
192 }
193
194 {
195 Orthanc::LuaFunctionCall f(lua, "identity");
196 Json::Value vv = Json::objectValue;
197 f.PushJson(vv);
198 Json::Value v;
199 f.ExecuteToJson(v);
200 // Lua does not make the distinction between empty lists and empty objects
201 ASSERT_EQ(Json::arrayValue, v.type());
202 }
203
204 {
205 Orthanc::LuaFunctionCall f(lua, "identity");
206 f.PushJson(b);
207 Json::Value v;
208 f.ExecuteToJson(v);
209 ASSERT_EQ(Json::objectValue, v.type());
210 ASSERT_FLOAT_EQ(42.0f, v["a"].asFloat());
211 ASSERT_FLOAT_EQ(44.0f, v["b"].asFloat());
212 ASSERT_FLOAT_EQ(43.0f, v["c"].asFloat());
213 }
214
215 {
216 Orthanc::LuaFunctionCall f(lua, "identity");
217 f.PushJson(c);
218 Json::Value v;
219 f.ExecuteToJson(v);
220 ASSERT_EQ(Json::arrayValue, v.type());
221 ASSERT_EQ("test3", v[0].asString());
222 ASSERT_EQ("test1", v[1].asString());
223 ASSERT_EQ("test2", v[2].asString());
224 }
225
226 {
227 Orthanc::LuaFunctionCall f(lua, "identity");
228 f.PushJson(a);
229 Json::Value v;
230 f.ExecuteToJson(v);
231 ASSERT_EQ("World", v["Hello"].asString());
232 ASSERT_EQ(42, v["List"][0]["a"].asInt());
233 ASSERT_EQ(44, v["List"][0]["b"].asInt());
234 ASSERT_EQ(43, v["List"][0]["c"].asInt());
235 ASSERT_EQ("test3", v["List"][1][0].asString());
236 ASSERT_EQ("test1", v["List"][1][1].asString());
237 ASSERT_EQ("test2", v["List"][1][2].asString());
238 }
239 }
240
241 TEST(Lua, Http)
242 {
243 Orthanc::LuaContext lua;
244
245 #if UNIT_TESTS_WITH_HTTP_CONNEXIONS == 1
246 lua.Execute("JSON = loadstring(HttpGet('http://www.montefiore.ulg.ac.be/~jodogne/Orthanc/ThirdPartyDownloads/JSON.lua')) ()");
247 const std::string url("http://orthanc.googlecode.com/hg/OrthancCppClient/SharedLibrary/Product.json");
248 #endif
249
250 std::string s;
251 lua.Execute(s, "print(HttpGet({}))");
252 ASSERT_EQ("ERROR", Orthanc::Toolbox::StripSpaces(s));
253
254 #if UNIT_TESTS_WITH_HTTP_CONNEXIONS == 1
255 lua.Execute(s, "print(string.len(HttpGet(\"" + url + "\")))");
256 ASSERT_LE(100, boost::lexical_cast<int>(Orthanc::Toolbox::StripSpaces(s)));
257
258 // Parse a JSON file
259 lua.Execute(s, "print(JSON:decode(HttpGet(\"" + url + "\")) ['Product'])");
260 ASSERT_EQ("OrthancClient", Orthanc::Toolbox::StripSpaces(s));
261
262 #if 0
263 // This part of the test can only be executed if one instance of
264 // Orthanc is running on the localhost
265
266 lua.Execute("modality = {}");
267 lua.Execute("table.insert(modality, 'ORTHANC')");
268 lua.Execute("table.insert(modality, 'localhost')");
269 lua.Execute("table.insert(modality, 4242)");
270
271 lua.Execute(s, "print(HttpPost(\"http://localhost:8042/tools/execute-script\", \"print('hello world')\"))");
272 ASSERT_EQ("hello world", Orthanc::Toolbox::StripSpaces(s));
273
274 lua.Execute(s, "print(JSON:decode(HttpPost(\"http://localhost:8042/tools/execute-script\", \"print('[10,42,1000]')\")) [2])");
275 ASSERT_EQ("42", Orthanc::Toolbox::StripSpaces(s));
276
277 // Add/remove a modality with Lua
278 Json::Value v;
279 lua.Execute(s, "print(HttpGet('http://localhost:8042/modalities/lua'))");
280 ASSERT_EQ(0, Orthanc::Toolbox::StripSpaces(s).size());
281 lua.Execute(s, "print(HttpPut('http://localhost:8042/modalities/lua', JSON:encode(modality)))");
282 lua.Execute(v, "print(HttpGet('http://localhost:8042/modalities/lua'))");
283 ASSERT_TRUE(v.type() == Json::arrayValue);
284 lua.Execute(s, "print(HttpDelete('http://localhost:8042/modalities/lua'))");
285 lua.Execute(s, "print(HttpGet('http://localhost:8042/modalities/lua'))");
286 ASSERT_EQ(0, Orthanc::Toolbox::StripSpaces(s).size());
287 #endif
288
289 #endif
290 }