JsonStoreScriptModuleTests.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Text;
  31. using log4net;
  32. using Nini.Config;
  33. using NUnit.Framework;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Region.CoreModules.Scripting.ScriptModuleComms;
  37. using OpenSim.Region.Framework.Scenes;
  38. using OpenSim.Region.ScriptEngine.Shared;
  39. using OpenSim.Region.ScriptEngine.Shared.Api;
  40. using OpenSim.Services.Interfaces;
  41. using OpenSim.Tests.Common;
  42. namespace OpenSim.Region.OptionalModules.Scripting.JsonStore.Tests
  43. {
  44. /// <summary>
  45. /// Tests for inventory functions in LSL
  46. /// </summary>
  47. [TestFixture]
  48. public class JsonStoreScriptModuleTests : OpenSimTestCase
  49. {
  50. private Scene m_scene;
  51. private MockScriptEngine m_engine;
  52. private ScriptModuleCommsModule m_smcm;
  53. private JsonStoreScriptModule m_jssm;
  54. [TestFixtureSetUp]
  55. public void FixtureInit()
  56. {
  57. // Don't allow tests to be bamboozled by asynchronous events. Execute everything on the same thread.
  58. Util.FireAndForgetMethod = FireAndForgetMethod.RegressionTest;
  59. }
  60. [TestFixtureTearDown]
  61. public void TearDown()
  62. {
  63. // We must set this back afterwards, otherwise later tests will fail since they're expecting multiple
  64. // threads. Possibly, later tests should be rewritten so none of them require async stuff (which regression
  65. // tests really shouldn't).
  66. Util.FireAndForgetMethod = Util.DefaultFireAndForgetMethod;
  67. }
  68. [SetUp]
  69. public override void SetUp()
  70. {
  71. base.SetUp();
  72. IConfigSource configSource = new IniConfigSource();
  73. IConfig jsonStoreConfig = configSource.AddConfig("JsonStore");
  74. jsonStoreConfig.Set("Enabled", "true");
  75. m_engine = new MockScriptEngine();
  76. m_smcm = new ScriptModuleCommsModule();
  77. JsonStoreModule jsm = new JsonStoreModule();
  78. m_jssm = new JsonStoreScriptModule();
  79. m_scene = new SceneHelpers().SetupScene();
  80. SceneHelpers.SetupSceneModules(m_scene, configSource, m_engine, m_smcm, jsm, m_jssm);
  81. try
  82. {
  83. m_smcm.RegisterScriptInvocation(this, "DummyTestMethod");
  84. }
  85. catch (ArgumentException)
  86. {
  87. Assert.Ignore("Ignoring test since running on .NET 3.5 or earlier.");
  88. }
  89. // XXX: Unfortunately, ICommsModule currently has no way of deregistering methods.
  90. }
  91. private object InvokeOp(string name, params object[] args)
  92. {
  93. return InvokeOpOnHost(name, UUID.Zero, args);
  94. }
  95. private object InvokeOpOnHost(string name, UUID hostId, params object[] args)
  96. {
  97. return m_smcm.InvokeOperation(hostId, UUID.Zero, name, args);
  98. }
  99. [Test]
  100. public void TestJsonCreateStore()
  101. {
  102. TestHelpers.InMethod();
  103. // TestHelpers.EnableLogging();
  104. // Test blank store
  105. {
  106. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{}");
  107. Assert.That(storeId, Is.Not.EqualTo(UUID.Zero));
  108. }
  109. // Test single element store
  110. {
  111. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{ 'Hello' : 'World' }");
  112. Assert.That(storeId, Is.Not.EqualTo(UUID.Zero));
  113. }
  114. // Test with an integer value
  115. {
  116. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{ 'Hello' : 42.15 }");
  117. Assert.That(storeId, Is.Not.EqualTo(UUID.Zero));
  118. string value = (string)InvokeOp("JsonGetValue", storeId, "Hello");
  119. Assert.That(value, Is.EqualTo("42.15"));
  120. }
  121. // Test with an array as the root node
  122. {
  123. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "[ 'one', 'two', 'three' ]");
  124. Assert.That(storeId, Is.Not.EqualTo(UUID.Zero));
  125. string value = (string)InvokeOp("JsonGetValue", storeId, "[1]");
  126. Assert.That(value, Is.EqualTo("two"));
  127. }
  128. }
  129. [Test]
  130. public void TestJsonDestroyStore()
  131. {
  132. TestHelpers.InMethod();
  133. // TestHelpers.EnableLogging();
  134. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{ 'Hello' : 'World' }");
  135. int dsrv = (int)InvokeOp("JsonDestroyStore", storeId);
  136. Assert.That(dsrv, Is.EqualTo(1));
  137. int tprv = (int)InvokeOp("JsonGetNodeType", storeId, "Hello");
  138. Assert.That(tprv, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_UNDEF));
  139. }
  140. [Test]
  141. public void TestJsonDestroyStoreNotExists()
  142. {
  143. TestHelpers.InMethod();
  144. // TestHelpers.EnableLogging();
  145. UUID fakeStoreId = TestHelpers.ParseTail(0x500);
  146. int dsrv = (int)InvokeOp("JsonDestroyStore", fakeStoreId);
  147. Assert.That(dsrv, Is.EqualTo(0));
  148. }
  149. [Test]
  150. public void TestJsonGetValue()
  151. {
  152. TestHelpers.InMethod();
  153. // TestHelpers.EnableLogging();
  154. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{ 'Hello' : { 'World' : 'Two' } }");
  155. {
  156. string value = (string)InvokeOp("JsonGetValue", storeId, "Hello.World");
  157. Assert.That(value, Is.EqualTo("Two"));
  158. }
  159. // Test get of path section instead of leaf
  160. {
  161. string value = (string)InvokeOp("JsonGetValue", storeId, "Hello");
  162. Assert.That(value, Is.EqualTo(""));
  163. }
  164. // Test get of non-existing value
  165. {
  166. string fakeValueGet = (string)InvokeOp("JsonGetValue", storeId, "foo");
  167. Assert.That(fakeValueGet, Is.EqualTo(""));
  168. }
  169. // Test get from non-existing store
  170. {
  171. UUID fakeStoreId = TestHelpers.ParseTail(0x500);
  172. string fakeStoreValueGet = (string)InvokeOp("JsonGetValue", fakeStoreId, "Hello");
  173. Assert.That(fakeStoreValueGet, Is.EqualTo(""));
  174. }
  175. }
  176. [Test]
  177. public void TestJsonGetJson()
  178. {
  179. TestHelpers.InMethod();
  180. // TestHelpers.EnableLogging();
  181. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{ 'Hello' : { 'World' : 'Two' } }");
  182. {
  183. string value = (string)InvokeOp("JsonGetJson", storeId, "Hello.World");
  184. Assert.That(value, Is.EqualTo("'Two'"));
  185. }
  186. // Test get of path section instead of leaf
  187. {
  188. string value = (string)InvokeOp("JsonGetJson", storeId, "Hello");
  189. Assert.That(value, Is.EqualTo("{\"World\":\"Two\"}"));
  190. }
  191. // Test get of non-existing value
  192. {
  193. string fakeValueGet = (string)InvokeOp("JsonGetJson", storeId, "foo");
  194. Assert.That(fakeValueGet, Is.EqualTo(""));
  195. }
  196. // Test get from non-existing store
  197. {
  198. UUID fakeStoreId = TestHelpers.ParseTail(0x500);
  199. string fakeStoreValueGet = (string)InvokeOp("JsonGetJson", fakeStoreId, "Hello");
  200. Assert.That(fakeStoreValueGet, Is.EqualTo(""));
  201. }
  202. }
  203. // [Test]
  204. // public void TestJsonTakeValue()
  205. // {
  206. // TestHelpers.InMethod();
  207. //// TestHelpers.EnableLogging();
  208. //
  209. // UUID storeId
  210. // = (UUID)m_smcm.InvokeOperation(
  211. // UUID.Zero, UUID.Zero, "JsonCreateStore", new object[] { "{ 'Hello' : 'World' }" });
  212. //
  213. // string value
  214. // = (string)m_smcm.InvokeOperation(
  215. // UUID.Zero, UUID.Zero, "JsonTakeValue", new object[] { storeId, "Hello" });
  216. //
  217. // Assert.That(value, Is.EqualTo("World"));
  218. //
  219. // string value2
  220. // = (string)m_smcm.InvokeOperation(
  221. // UUID.Zero, UUID.Zero, "JsonGetValue", new object[] { storeId, "Hello" });
  222. //
  223. // Assert.That(value, Is.Null);
  224. // }
  225. [Test]
  226. public void TestJsonRemoveValue()
  227. {
  228. TestHelpers.InMethod();
  229. // TestHelpers.EnableLogging();
  230. // Test remove of node in object pointing to a string
  231. {
  232. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{ 'Hello' : 'World' }");
  233. int returnValue = (int)InvokeOp( "JsonRemoveValue", storeId, "Hello");
  234. Assert.That(returnValue, Is.EqualTo(1));
  235. int result = (int)InvokeOp("JsonGetNodeType", storeId, "Hello");
  236. Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_UNDEF));
  237. string returnValue2 = (string)InvokeOp("JsonGetValue", storeId, "Hello");
  238. Assert.That(returnValue2, Is.EqualTo(""));
  239. }
  240. // Test remove of node in object pointing to another object
  241. {
  242. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{ 'Hello' : { 'World' : 'Wally' } }");
  243. int returnValue = (int)InvokeOp( "JsonRemoveValue", storeId, "Hello");
  244. Assert.That(returnValue, Is.EqualTo(1));
  245. int result = (int)InvokeOp("JsonGetNodeType", storeId, "Hello");
  246. Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_UNDEF));
  247. string returnValue2 = (string)InvokeOp("JsonGetJson", storeId, "Hello");
  248. Assert.That(returnValue2, Is.EqualTo(""));
  249. }
  250. // Test remove of node in an array
  251. {
  252. UUID storeId
  253. = (UUID)InvokeOp("JsonCreateStore", "{ 'Hello' : [ 'value1', 'value2' ] }");
  254. int returnValue = (int)InvokeOp( "JsonRemoveValue", storeId, "Hello[0]");
  255. Assert.That(returnValue, Is.EqualTo(1));
  256. int result = (int)InvokeOp("JsonGetNodeType", storeId, "Hello[0]");
  257. Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_VALUE));
  258. result = (int)InvokeOp("JsonGetNodeType", storeId, "Hello[1]");
  259. Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_UNDEF));
  260. string stringReturnValue = (string)InvokeOp("JsonGetValue", storeId, "Hello[0]");
  261. Assert.That(stringReturnValue, Is.EqualTo("value2"));
  262. stringReturnValue = (string)InvokeOp("JsonGetJson", storeId, "Hello[1]");
  263. Assert.That(stringReturnValue, Is.EqualTo(""));
  264. }
  265. // Test remove of non-existing value
  266. {
  267. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{ 'Hello' : 'World' }");
  268. int fakeValueRemove = (int)InvokeOp("JsonRemoveValue", storeId, "Cheese");
  269. Assert.That(fakeValueRemove, Is.EqualTo(0));
  270. }
  271. {
  272. // Test get from non-existing store
  273. UUID fakeStoreId = TestHelpers.ParseTail(0x500);
  274. int fakeStoreValueRemove = (int)InvokeOp("JsonRemoveValue", fakeStoreId, "Hello");
  275. Assert.That(fakeStoreValueRemove, Is.EqualTo(0));
  276. }
  277. }
  278. // [Test]
  279. // public void TestJsonTestPath()
  280. // {
  281. // TestHelpers.InMethod();
  282. //// TestHelpers.EnableLogging();
  283. //
  284. // UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{ 'Hello' : { 'World' : 'One' } }");
  285. //
  286. // {
  287. // int result = (int)InvokeOp("JsonTestPath", storeId, "Hello.World");
  288. // Assert.That(result, Is.EqualTo(1));
  289. // }
  290. //
  291. // // Test for path which does not resolve to a value.
  292. // {
  293. // int result = (int)InvokeOp("JsonTestPath", storeId, "Hello");
  294. // Assert.That(result, Is.EqualTo(0));
  295. // }
  296. //
  297. // {
  298. // int result2 = (int)InvokeOp("JsonTestPath", storeId, "foo");
  299. // Assert.That(result2, Is.EqualTo(0));
  300. // }
  301. //
  302. // // Test with fake store
  303. // {
  304. // UUID fakeStoreId = TestHelpers.ParseTail(0x500);
  305. // int fakeStoreValueRemove = (int)InvokeOp("JsonTestPath", fakeStoreId, "Hello");
  306. // Assert.That(fakeStoreValueRemove, Is.EqualTo(0));
  307. // }
  308. // }
  309. // [Test]
  310. // public void TestJsonTestPathJson()
  311. // {
  312. // TestHelpers.InMethod();
  313. //// TestHelpers.EnableLogging();
  314. //
  315. // UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{ 'Hello' : { 'World' : 'One' } }");
  316. //
  317. // {
  318. // int result = (int)InvokeOp("JsonTestPathJson", storeId, "Hello.World");
  319. // Assert.That(result, Is.EqualTo(1));
  320. // }
  321. //
  322. // // Test for path which does not resolve to a value.
  323. // {
  324. // int result = (int)InvokeOp("JsonTestPathJson", storeId, "Hello");
  325. // Assert.That(result, Is.EqualTo(1));
  326. // }
  327. //
  328. // {
  329. // int result2 = (int)InvokeOp("JsonTestPathJson", storeId, "foo");
  330. // Assert.That(result2, Is.EqualTo(0));
  331. // }
  332. //
  333. // // Test with fake store
  334. // {
  335. // UUID fakeStoreId = TestHelpers.ParseTail(0x500);
  336. // int fakeStoreValueRemove = (int)InvokeOp("JsonTestPathJson", fakeStoreId, "Hello");
  337. // Assert.That(fakeStoreValueRemove, Is.EqualTo(0));
  338. // }
  339. // }
  340. [Test]
  341. public void TestJsonGetArrayLength()
  342. {
  343. TestHelpers.InMethod();
  344. // TestHelpers.EnableLogging();
  345. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{ 'Hello' : { 'World' : [ 'one', 2 ] } }");
  346. {
  347. int result = (int)InvokeOp("JsonGetArrayLength", storeId, "Hello.World");
  348. Assert.That(result, Is.EqualTo(2));
  349. }
  350. // Test path which is not an array
  351. {
  352. int result = (int)InvokeOp("JsonGetArrayLength", storeId, "Hello");
  353. Assert.That(result, Is.EqualTo(-1));
  354. }
  355. // Test fake path
  356. {
  357. int result = (int)InvokeOp("JsonGetArrayLength", storeId, "foo");
  358. Assert.That(result, Is.EqualTo(-1));
  359. }
  360. // Test fake store
  361. {
  362. UUID fakeStoreId = TestHelpers.ParseTail(0x500);
  363. int result = (int)InvokeOp("JsonGetArrayLength", fakeStoreId, "Hello.World");
  364. Assert.That(result, Is.EqualTo(-1));
  365. }
  366. }
  367. [Test]
  368. public void TestJsonGetNodeType()
  369. {
  370. TestHelpers.InMethod();
  371. // TestHelpers.EnableLogging();
  372. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{ 'Hello' : { 'World' : [ 'one', 2 ] } }");
  373. {
  374. int result = (int)InvokeOp("JsonGetNodeType", storeId, ".");
  375. Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_OBJECT));
  376. }
  377. {
  378. int result = (int)InvokeOp("JsonGetNodeType", storeId, "Hello");
  379. Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_OBJECT));
  380. }
  381. {
  382. int result = (int)InvokeOp("JsonGetNodeType", storeId, "Hello.World");
  383. Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_ARRAY));
  384. }
  385. {
  386. int result = (int)InvokeOp("JsonGetNodeType", storeId, "Hello.World[0]");
  387. Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_VALUE));
  388. }
  389. {
  390. int result = (int)InvokeOp("JsonGetNodeType", storeId, "Hello.World[1]");
  391. Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_VALUE));
  392. }
  393. // Test for non-existent path
  394. {
  395. int result = (int)InvokeOp("JsonGetNodeType", storeId, "foo");
  396. Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_UNDEF));
  397. }
  398. // Test for non-existent store
  399. {
  400. UUID fakeStoreId = TestHelpers.ParseTail(0x500);
  401. int result = (int)InvokeOp("JsonGetNodeType", fakeStoreId, ".");
  402. Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_UNDEF));
  403. }
  404. }
  405. [Test]
  406. public void TestJsonList2Path()
  407. {
  408. TestHelpers.InMethod();
  409. // TestHelpers.EnableLogging();
  410. // Invoking these methods directly since I just couldn't get comms module invocation to work for some reason
  411. // - some confusion with the methods that take a params object[] invocation.
  412. {
  413. string result = m_jssm.JsonList2Path(UUID.Zero, UUID.Zero, new object[] { "foo" });
  414. Assert.That(result, Is.EqualTo("{foo}"));
  415. }
  416. {
  417. string result = m_jssm.JsonList2Path(UUID.Zero, UUID.Zero, new object[] { "foo", "bar" });
  418. Assert.That(result, Is.EqualTo("{foo}.{bar}"));
  419. }
  420. {
  421. string result = m_jssm.JsonList2Path(UUID.Zero, UUID.Zero, new object[] { "foo", 1, "bar" });
  422. Assert.That(result, Is.EqualTo("{foo}.[1].{bar}"));
  423. }
  424. }
  425. [Test]
  426. public void TestJsonSetValue()
  427. {
  428. TestHelpers.InMethod();
  429. // TestHelpers.EnableLogging();
  430. {
  431. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{}");
  432. int result = (int)InvokeOp("JsonSetValue", storeId, "Fun", "Times");
  433. Assert.That(result, Is.EqualTo(1));
  434. string value = (string)InvokeOp("JsonGetValue", storeId, "Fun");
  435. Assert.That(value, Is.EqualTo("Times"));
  436. }
  437. // Test setting a key containing periods with delineation
  438. {
  439. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{}");
  440. int result = (int)InvokeOp("JsonSetValue", storeId, "{Fun.Circus}", "Times");
  441. Assert.That(result, Is.EqualTo(1));
  442. string value = (string)InvokeOp("JsonGetValue", storeId, "{Fun.Circus}");
  443. Assert.That(value, Is.EqualTo("Times"));
  444. }
  445. // *** Test [] ***
  446. // Test setting a key containing unbalanced ] without delineation. Expecting failure
  447. {
  448. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{}");
  449. int result = (int)InvokeOp("JsonSetValue", storeId, "Fun]Circus", "Times");
  450. Assert.That(result, Is.EqualTo(0));
  451. string value = (string)InvokeOp("JsonGetValue", storeId, "Fun]Circus");
  452. Assert.That(value, Is.EqualTo(""));
  453. }
  454. // Test setting a key containing unbalanced [ without delineation. Expecting failure
  455. {
  456. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{}");
  457. int result = (int)InvokeOp("JsonSetValue", storeId, "Fun[Circus", "Times");
  458. Assert.That(result, Is.EqualTo(0));
  459. string value = (string)InvokeOp("JsonGetValue", storeId, "Fun[Circus");
  460. Assert.That(value, Is.EqualTo(""));
  461. }
  462. // Test setting a key containing unbalanced [] without delineation. Expecting failure
  463. {
  464. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{}");
  465. int result = (int)InvokeOp("JsonSetValue", storeId, "Fun[]Circus", "Times");
  466. Assert.That(result, Is.EqualTo(0));
  467. string value = (string)InvokeOp("JsonGetValue", storeId, "Fun[]Circus");
  468. Assert.That(value, Is.EqualTo(""));
  469. }
  470. // Test setting a key containing unbalanced ] with delineation
  471. {
  472. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{}");
  473. int result = (int)InvokeOp("JsonSetValue", storeId, "{Fun]Circus}", "Times");
  474. Assert.That(result, Is.EqualTo(1));
  475. string value = (string)InvokeOp("JsonGetValue", storeId, "{Fun]Circus}");
  476. Assert.That(value, Is.EqualTo("Times"));
  477. }
  478. // Test setting a key containing unbalanced [ with delineation
  479. {
  480. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{}");
  481. int result = (int)InvokeOp("JsonSetValue", storeId, "{Fun[Circus}", "Times");
  482. Assert.That(result, Is.EqualTo(1));
  483. string value = (string)InvokeOp("JsonGetValue", storeId, "{Fun[Circus}");
  484. Assert.That(value, Is.EqualTo("Times"));
  485. }
  486. // Test setting a key containing empty balanced [] with delineation
  487. {
  488. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{}");
  489. int result = (int)InvokeOp("JsonSetValue", storeId, "{Fun[]Circus}", "Times");
  490. Assert.That(result, Is.EqualTo(1));
  491. string value = (string)InvokeOp("JsonGetValue", storeId, "{Fun[]Circus}");
  492. Assert.That(value, Is.EqualTo("Times"));
  493. }
  494. // // Commented out as this currently unexpectedly fails.
  495. // // Test setting a key containing brackets around an integer with delineation
  496. // {
  497. // UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{}");
  498. //
  499. // int result = (int)InvokeOp("JsonSetValue", storeId, "{Fun[0]Circus}", "Times");
  500. // Assert.That(result, Is.EqualTo(1));
  501. //
  502. // string value = (string)InvokeOp("JsonGetValue", storeId, "{Fun[0]Circus}");
  503. // Assert.That(value, Is.EqualTo("Times"));
  504. // }
  505. // *** Test {} ***
  506. // Test setting a key containing unbalanced } without delineation. Expecting failure (?)
  507. {
  508. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{}");
  509. int result = (int)InvokeOp("JsonSetValue", storeId, "Fun}Circus", "Times");
  510. Assert.That(result, Is.EqualTo(0));
  511. string value = (string)InvokeOp("JsonGetValue", storeId, "Fun}Circus");
  512. Assert.That(value, Is.EqualTo(""));
  513. }
  514. // Test setting a key containing unbalanced { without delineation. Expecting failure (?)
  515. {
  516. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{}");
  517. int result = (int)InvokeOp("JsonSetValue", storeId, "Fun{Circus", "Times");
  518. Assert.That(result, Is.EqualTo(0));
  519. string value = (string)InvokeOp("JsonGetValue", storeId, "Fun}Circus");
  520. Assert.That(value, Is.EqualTo(""));
  521. }
  522. // // Commented out as this currently unexpectedly fails.
  523. // // Test setting a key containing unbalanced }
  524. // {
  525. // UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{}");
  526. //
  527. // int result = (int)InvokeOp("JsonSetValue", storeId, "{Fun}Circus}", "Times");
  528. // Assert.That(result, Is.EqualTo(0));
  529. // }
  530. // Test setting a key containing unbalanced { with delineation
  531. {
  532. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{}");
  533. int result = (int)InvokeOp("JsonSetValue", storeId, "{Fun{Circus}", "Times");
  534. Assert.That(result, Is.EqualTo(1));
  535. string value = (string)InvokeOp("JsonGetValue", storeId, "{Fun{Circus}");
  536. Assert.That(value, Is.EqualTo("Times"));
  537. }
  538. // Test setting a key containing balanced {} with delineation. This should fail.
  539. {
  540. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{}");
  541. int result = (int)InvokeOp("JsonSetValue", storeId, "{Fun{Filled}Circus}", "Times");
  542. Assert.That(result, Is.EqualTo(0));
  543. string value = (string)InvokeOp("JsonGetValue", storeId, "{Fun{Filled}Circus}");
  544. Assert.That(value, Is.EqualTo(""));
  545. }
  546. // Test setting to location that does not exist. This should fail.
  547. {
  548. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{}");
  549. int result = (int)InvokeOp("JsonSetValue", storeId, "Fun.Circus", "Times");
  550. Assert.That(result, Is.EqualTo(0));
  551. string value = (string)InvokeOp("JsonGetValue", storeId, "Fun.Circus");
  552. Assert.That(value, Is.EqualTo(""));
  553. }
  554. // Test with fake store
  555. {
  556. UUID fakeStoreId = TestHelpers.ParseTail(0x500);
  557. int fakeStoreValueSet = (int)InvokeOp("JsonSetValue", fakeStoreId, "Hello", "World");
  558. Assert.That(fakeStoreValueSet, Is.EqualTo(0));
  559. }
  560. }
  561. [Test]
  562. public void TestJsonSetJson()
  563. {
  564. TestHelpers.InMethod();
  565. // TestHelpers.EnableLogging();
  566. // Single quoted token case
  567. {
  568. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{ }");
  569. int result = (int)InvokeOp("JsonSetJson", storeId, "Fun", "'Times'");
  570. Assert.That(result, Is.EqualTo(1));
  571. string value = (string)InvokeOp("JsonGetValue", storeId, "Fun");
  572. Assert.That(value, Is.EqualTo("Times"));
  573. }
  574. // Sub-tree case
  575. {
  576. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{ }");
  577. int result = (int)InvokeOp("JsonSetJson", storeId, "Fun", "{ 'Filled' : 'Times' }");
  578. Assert.That(result, Is.EqualTo(1));
  579. string value = (string)InvokeOp("JsonGetValue", storeId, "Fun.Filled");
  580. Assert.That(value, Is.EqualTo("Times"));
  581. }
  582. // If setting single strings in JsonSetValueJson, these must be single quoted tokens, not bare strings.
  583. {
  584. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{ }");
  585. int result = (int)InvokeOp("JsonSetJson", storeId, "Fun", "Times");
  586. Assert.That(result, Is.EqualTo(0));
  587. string value = (string)InvokeOp("JsonGetValue", storeId, "Fun");
  588. Assert.That(value, Is.EqualTo(""));
  589. }
  590. // Test setting to location that does not exist. This should fail.
  591. {
  592. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{ }");
  593. int result = (int)InvokeOp("JsonSetJson", storeId, "Fun.Circus", "'Times'");
  594. Assert.That(result, Is.EqualTo(0));
  595. string value = (string)InvokeOp("JsonGetValue", storeId, "Fun.Circus");
  596. Assert.That(value, Is.EqualTo(""));
  597. }
  598. // Test with fake store
  599. {
  600. UUID fakeStoreId = TestHelpers.ParseTail(0x500);
  601. int fakeStoreValueSet = (int)InvokeOp("JsonSetJson", fakeStoreId, "Hello", "'World'");
  602. Assert.That(fakeStoreValueSet, Is.EqualTo(0));
  603. }
  604. }
  605. /// <summary>
  606. /// Test for writing json to a notecard
  607. /// </summary>
  608. /// <remarks>
  609. /// TODO: Really needs to test correct receipt of the link_message event. Could do this by directly fetching
  610. /// it via the MockScriptEngine or perhaps by a dummy script instance.
  611. /// </remarks>
  612. [Test]
  613. public void TestJsonWriteNotecard()
  614. {
  615. TestHelpers.InMethod();
  616. // TestHelpers.EnableLogging();
  617. SceneObjectGroup so = SceneHelpers.CreateSceneObject(1, TestHelpers.ParseTail(0x1));
  618. m_scene.AddSceneObject(so);
  619. UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{ 'Hello':'World' }");
  620. {
  621. string notecardName = "nc1";
  622. // Write notecard
  623. UUID writeNotecardRequestId = (UUID)InvokeOpOnHost("JsonWriteNotecard", so.UUID, storeId, "", notecardName);
  624. Assert.That(writeNotecardRequestId, Is.Not.EqualTo(UUID.Zero));
  625. TaskInventoryItem nc1Item = so.RootPart.Inventory.GetInventoryItem(notecardName);
  626. Assert.That(nc1Item, Is.Not.Null);
  627. // TODO: Should independently check the contents.
  628. }
  629. // TODO: Write partial test
  630. {
  631. // Try to write notecard for a bad path
  632. // In this case we do get a request id but no notecard is written.
  633. string badPathNotecardName = "badPathNotecardName";
  634. UUID writeNotecardBadPathRequestId
  635. = (UUID)InvokeOpOnHost("JsonWriteNotecard", so.UUID, storeId, "flibble", badPathNotecardName);
  636. Assert.That(writeNotecardBadPathRequestId, Is.Not.EqualTo(UUID.Zero));
  637. TaskInventoryItem badPathItem = so.RootPart.Inventory.GetInventoryItem(badPathNotecardName);
  638. Assert.That(badPathItem, Is.Null);
  639. }
  640. {
  641. // Test with fake store
  642. // In this case we do get a request id but no notecard is written.
  643. string fakeStoreNotecardName = "fakeStoreNotecardName";
  644. UUID fakeStoreId = TestHelpers.ParseTail(0x500);
  645. UUID fakeStoreWriteNotecardValue
  646. = (UUID)InvokeOpOnHost("JsonWriteNotecard", so.UUID, fakeStoreId, "", fakeStoreNotecardName);
  647. Assert.That(fakeStoreWriteNotecardValue, Is.Not.EqualTo(UUID.Zero));
  648. TaskInventoryItem fakeStoreItem = so.RootPart.Inventory.GetInventoryItem(fakeStoreNotecardName);
  649. Assert.That(fakeStoreItem, Is.Null);
  650. }
  651. }
  652. /// <summary>
  653. /// Test for reading json from a notecard
  654. /// </summary>
  655. /// <remarks>
  656. /// TODO: Really needs to test correct receipt of the link_message event. Could do this by directly fetching
  657. /// it via the MockScriptEngine or perhaps by a dummy script instance.
  658. /// </remarks>
  659. [Test]
  660. public void TestJsonReadNotecard()
  661. {
  662. TestHelpers.InMethod();
  663. // TestHelpers.EnableLogging();
  664. string notecardName = "nc1";
  665. SceneObjectGroup so = SceneHelpers.CreateSceneObject(1, TestHelpers.ParseTail(0x1));
  666. m_scene.AddSceneObject(so);
  667. UUID creatingStoreId = (UUID)InvokeOp("JsonCreateStore", "{ 'Hello':'World' }");
  668. // Write notecard
  669. InvokeOpOnHost("JsonWriteNotecard", so.UUID, creatingStoreId, "", notecardName);
  670. {
  671. // Read notecard
  672. UUID receivingStoreId = (UUID)InvokeOp("JsonCreateStore", "{}");
  673. UUID readNotecardRequestId = (UUID)InvokeOpOnHost("JsonReadNotecard", so.UUID, receivingStoreId, "", notecardName);
  674. Assert.That(readNotecardRequestId, Is.Not.EqualTo(UUID.Zero));
  675. string value = (string)InvokeOp("JsonGetValue", receivingStoreId, "Hello");
  676. Assert.That(value, Is.EqualTo("World"));
  677. }
  678. {
  679. // Read notecard to new single component path
  680. UUID receivingStoreId = (UUID)InvokeOp("JsonCreateStore", "{}");
  681. UUID readNotecardRequestId = (UUID)InvokeOpOnHost("JsonReadNotecard", so.UUID, receivingStoreId, "make", notecardName);
  682. Assert.That(readNotecardRequestId, Is.Not.EqualTo(UUID.Zero));
  683. string value = (string)InvokeOp("JsonGetValue", receivingStoreId, "Hello");
  684. Assert.That(value, Is.EqualTo(""));
  685. value = (string)InvokeOp("JsonGetValue", receivingStoreId, "make.Hello");
  686. Assert.That(value, Is.EqualTo("World"));
  687. }
  688. {
  689. // Read notecard to new multi-component path. This should not work.
  690. UUID receivingStoreId = (UUID)InvokeOp("JsonCreateStore", "{}");
  691. UUID readNotecardRequestId = (UUID)InvokeOpOnHost("JsonReadNotecard", so.UUID, receivingStoreId, "make.it", notecardName);
  692. Assert.That(readNotecardRequestId, Is.Not.EqualTo(UUID.Zero));
  693. string value = (string)InvokeOp("JsonGetValue", receivingStoreId, "Hello");
  694. Assert.That(value, Is.EqualTo(""));
  695. value = (string)InvokeOp("JsonGetValue", receivingStoreId, "make.it.Hello");
  696. Assert.That(value, Is.EqualTo(""));
  697. }
  698. {
  699. // Read notecard to existing multi-component path. This should work
  700. UUID receivingStoreId = (UUID)InvokeOp("JsonCreateStore", "{ 'make' : { 'it' : 'so' } }");
  701. UUID readNotecardRequestId = (UUID)InvokeOpOnHost("JsonReadNotecard", so.UUID, receivingStoreId, "make.it", notecardName);
  702. Assert.That(readNotecardRequestId, Is.Not.EqualTo(UUID.Zero));
  703. string value = (string)InvokeOp("JsonGetValue", receivingStoreId, "Hello");
  704. Assert.That(value, Is.EqualTo(""));
  705. value = (string)InvokeOp("JsonGetValue", receivingStoreId, "make.it.Hello");
  706. Assert.That(value, Is.EqualTo("World"));
  707. }
  708. {
  709. // Read notecard to invalid path. This should not work.
  710. UUID receivingStoreId = (UUID)InvokeOp("JsonCreateStore", "{ 'make' : { 'it' : 'so' } }");
  711. UUID readNotecardRequestId = (UUID)InvokeOpOnHost("JsonReadNotecard", so.UUID, receivingStoreId, "/", notecardName);
  712. Assert.That(readNotecardRequestId, Is.Not.EqualTo(UUID.Zero));
  713. string value = (string)InvokeOp("JsonGetValue", receivingStoreId, "Hello");
  714. Assert.That(value, Is.EqualTo(""));
  715. }
  716. {
  717. // Try read notecard to fake store.
  718. UUID fakeStoreId = TestHelpers.ParseTail(0x500);
  719. UUID readNotecardRequestId = (UUID)InvokeOpOnHost("JsonReadNotecard", so.UUID, fakeStoreId, "", notecardName);
  720. Assert.That(readNotecardRequestId, Is.Not.EqualTo(UUID.Zero));
  721. string value = (string)InvokeOp("JsonGetValue", fakeStoreId, "Hello");
  722. Assert.That(value, Is.EqualTo(""));
  723. }
  724. }
  725. public object DummyTestMethod(object o1, object o2, object o3, object o4, object o5) { return null; }
  726. }
  727. }