comparison OrthancServer/UnitTestsSources/LuaServerTests.cpp @ 4059:e241e5f3f088 framework

moved LuaTests and MemoryCacheTests from OrthancServer to OrthancFramework
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 11 Jun 2020 14:12:07 +0200
parents OrthancServer/UnitTestsSources/LuaTests.cpp@05b8fd21089c
children 0953b3dc3261
comparison
equal deleted inserted replaced
4058:2a8bf0991be0 4059:e241e5f3f088
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #include "PrecompiledHeadersUnitTests.h"
35 #include "gtest/gtest.h"
36
37 #include "../../OrthancFramework/Sources/OrthancException.h"
38 #include "../../OrthancFramework/Sources/Toolbox.h"
39 #include "../../OrthancFramework/Sources/Lua/LuaFunctionCall.h"
40
41 #include <OrthancServerResources.h>
42
43 #include <boost/lexical_cast.hpp>
44
45 #if !defined(UNIT_TESTS_WITH_HTTP_CONNEXIONS)
46 # error "Please set UNIT_TESTS_WITH_HTTP_CONNEXIONS to 0 or 1"
47 #endif
48
49
50 TEST(Lua, Json)
51 {
52 Orthanc::LuaContext lua;
53
54 {
55 std::string command;
56 Orthanc::ServerResources::GetFileResource(command, Orthanc::ServerResources::LUA_TOOLBOX);
57 lua.Execute(command);
58 }
59
60 lua.Execute("a={}");
61 lua.Execute("a['x'] = 10");
62 lua.Execute("a['y'] = {}");
63 lua.Execute("a['y'][1] = 20");
64 lua.Execute("a['y'][2] = 20");
65 lua.Execute("PrintRecursive(a)");
66 lua.Execute("function f(a) print(a.bool) return a.bool,20,30,40,50,60 end");
67
68 Json::Value v, vv, o;
69 //v["a"] = "b";
70 v.append("hello");
71 v.append("world");
72 v.append("42");
73 vv.append("sub");
74 vv.append("set");
75 v.append(vv);
76 o = Json::objectValue;
77 o["x"] = 10;
78 o["y"] = 20;
79 o["z"] = 20.5f;
80 v.append(o);
81
82 {
83 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
84 f.PushJson(v);
85 f.Execute();
86 }
87
88 {
89 Orthanc::LuaFunctionCall f(lua, "f");
90 f.PushJson(o);
91 ASSERT_THROW(f.ExecutePredicate(), Orthanc::OrthancException);
92 }
93
94 o["bool"] = false;
95
96 {
97 Orthanc::LuaFunctionCall f(lua, "f");
98 f.PushJson(o);
99 ASSERT_FALSE(f.ExecutePredicate());
100 }
101
102 o["bool"] = true;
103
104 {
105 Orthanc::LuaFunctionCall f(lua, "f");
106 f.PushJson(o);
107 ASSERT_TRUE(f.ExecutePredicate());
108 }
109 }
110
111
112 TEST(Lua, Simple)
113 {
114 Orthanc::LuaContext lua;
115
116 {
117 std::string command;
118 Orthanc::ServerResources::GetFileResource(command, Orthanc::ServerResources::LUA_TOOLBOX);
119 lua.Execute(command);
120 }
121
122 {
123 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
124 f.PushString("hello");
125 f.Execute();
126 }
127
128 {
129 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
130 f.PushBoolean(true);
131 f.Execute();
132 }
133
134 {
135 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
136 f.PushInteger(42);
137 f.Execute();
138 }
139
140 {
141 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
142 f.PushDouble(3.1415);
143 f.Execute();
144 }
145 }
146
147
148 TEST(Lua, Http)
149 {
150 Orthanc::LuaContext lua;
151
152 #if UNIT_TESTS_WITH_HTTP_CONNEXIONS == 1
153 // The "http://www.orthanc-server.com/downloads/third-party/" does
154 // not automatically redirect to HTTPS, so we cas use it even if the
155 // OpenSSL/HTTPS support is disabled in curl
156 const std::string BASE = "http://www.orthanc-server.com/downloads/third-party/";
157
158 #if LUA_VERSION_NUM >= 502
159 // Since Lua >= 5.2.0, the function "loadstring" has been replaced by "load"
160 lua.Execute("JSON = load(HttpGet('" + BASE + "JSON.lua')) ()");
161 #else
162 lua.Execute("JSON = loadstring(HttpGet('" + BASE + "JSON.lua')) ()");
163 #endif
164
165 const std::string url(BASE + "Product.json");
166 #endif
167
168 std::string s;
169 lua.Execute(s, "print(HttpGet({}))");
170 ASSERT_EQ("nil", Orthanc::Toolbox::StripSpaces(s));
171
172 #if UNIT_TESTS_WITH_HTTP_CONNEXIONS == 1
173 lua.Execute(s, "print(string.len(HttpGet(\"" + url + "\")))");
174 ASSERT_LE(100, boost::lexical_cast<int>(Orthanc::Toolbox::StripSpaces(s)));
175
176 // Parse a JSON file
177 lua.Execute(s, "print(JSON:decode(HttpGet(\"" + url + "\")) ['Product'])");
178 ASSERT_EQ("OrthancClient", Orthanc::Toolbox::StripSpaces(s));
179
180 #if 0
181 // This part of the test can only be executed if one instance of
182 // Orthanc is running on the localhost
183
184 lua.Execute("modality = {}");
185 lua.Execute("table.insert(modality, 'ORTHANC')");
186 lua.Execute("table.insert(modality, 'localhost')");
187 lua.Execute("table.insert(modality, 4242)");
188
189 lua.Execute(s, "print(HttpPost(\"http://localhost:8042/tools/execute-script\", \"print('hello world')\"))");
190 ASSERT_EQ("hello world", Orthanc::Toolbox::StripSpaces(s));
191
192 lua.Execute(s, "print(JSON:decode(HttpPost(\"http://localhost:8042/tools/execute-script\", \"print('[10,42,1000]')\")) [2])");
193 ASSERT_EQ("42", Orthanc::Toolbox::StripSpaces(s));
194
195 // Add/remove a modality with Lua
196 Json::Value v;
197 lua.Execute(s, "print(HttpGet('http://localhost:8042/modalities/lua'))");
198 ASSERT_EQ(0, Orthanc::Toolbox::StripSpaces(s).size());
199 lua.Execute(s, "print(HttpPut('http://localhost:8042/modalities/lua', JSON:encode(modality)))");
200 lua.Execute(v, "print(HttpGet('http://localhost:8042/modalities/lua'))");
201 ASSERT_TRUE(v.type() == Json::arrayValue);
202 lua.Execute(s, "print(HttpDelete('http://localhost:8042/modalities/lua'))");
203 lua.Execute(s, "print(HttpGet('http://localhost:8042/modalities/lua'))");
204 ASSERT_EQ(0, Orthanc::Toolbox::StripSpaces(s).size());
205 #endif
206
207 #endif
208 }