JsonStoreScriptModuleTests.cs 36 KB

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