OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
FetchInventoryDescendents2HandlerTests.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections.Generic;
30 using System.Linq;
31 using System.Net;
32 using System.Text.RegularExpressions;
33 using log4net;
34 using log4net.Config;
35 using NUnit.Framework;
36 using OpenMetaverse;
37 using OpenSim.Capabilities.Handlers;
38 using OpenSim.Framework;
39 using OpenSim.Framework.Servers.HttpServer;
40 using OpenSim.Region.Framework.Scenes;
41 using OpenSim.Services.Interfaces;
42 using OpenSim.Tests.Common;
43 
44 namespace OpenSim.Capabilities.Handlers.FetchInventory.Tests
45 {
46  [TestFixture]
48  {
49  private UUID m_userID = UUID.Zero;
50  private Scene m_scene;
51  private UUID m_rootFolderID;
52  private int m_rootDescendents;
53  private UUID m_notecardsFolder;
54  private UUID m_objectsFolder;
55 
56  private void Init()
57  {
58  // Create an inventory that looks like this:
59  //
60  // /My Inventory
61  // <other system folders>
62  // /Objects
63  // Some Object
64  // /Notecards
65  // Notecard 1
66  // Notecard 2
67  // /Test Folder
68  // Link to notecard -> /Notecards/Notecard 2
69  // Link to Objects folder -> /Objects
70 
71  m_scene = new SceneHelpers().SetupScene();
72 
73  m_scene.InventoryService.CreateUserInventory(m_userID);
74 
75  m_rootFolderID = m_scene.InventoryService.GetRootFolder(m_userID).ID;
76 
77  InventoryFolderBase of = m_scene.InventoryService.GetFolderForType(m_userID, FolderType.Object);
78  m_objectsFolder = of.ID;
79 
80  // Add an object
81  InventoryItemBase item = new InventoryItemBase(new UUID("b0000000-0000-0000-0000-00000000000b"), m_userID);
82  item.AssetID = UUID.Random();
83  item.AssetType = (int)AssetType.Object;
84  item.Folder = m_objectsFolder;
85  item.Name = "Some Object";
86  m_scene.InventoryService.AddItem(item);
87 
88  InventoryFolderBase ncf = m_scene.InventoryService.GetFolderForType(m_userID, FolderType.Notecard);
89  m_notecardsFolder = ncf.ID;
90 
91  // Add a notecard
92  item = new InventoryItemBase(new UUID("10000000-0000-0000-0000-000000000001"), m_userID);
93  item.AssetID = UUID.Random();
94  item.AssetType = (int)AssetType.Notecard;
95  item.Folder = m_notecardsFolder;
96  item.Name = "Test Notecard 1";
97  m_scene.InventoryService.AddItem(item);
98  // Add another notecard
99  item.ID = new UUID("20000000-0000-0000-0000-000000000002");
100  item.AssetID = new UUID("a0000000-0000-0000-0000-00000000000a");
101  item.Name = "Test Notecard 2";
102  m_scene.InventoryService.AddItem(item);
103 
104  // Add a folder
105  InventoryFolderBase folder = new InventoryFolderBase(new UUID("f0000000-0000-0000-0000-00000000000f"), "Test Folder", m_userID, m_rootFolderID);
106  m_scene.InventoryService.AddFolder(folder);
107 
108  // Add a link to notecard 2 in Test Folder
109  item.AssetID = item.ID; // use item ID of notecard 2
110  item.ID = new UUID("40000000-0000-0000-0000-000000000004");
111  item.AssetType = (int)AssetType.Link;
112  item.Folder = folder.ID;
113  item.Name = "Link to notecard";
114  m_scene.InventoryService.AddItem(item);
115 
116  // Add a link to the Objects folder in Test Folder
117  item.AssetID = m_scene.InventoryService.GetFolderForType(m_userID, FolderType.Object).ID; // use item ID of Objects folder
118  item.ID = new UUID("50000000-0000-0000-0000-000000000005");
119  item.AssetType = (int)AssetType.LinkFolder;
120  item.Folder = folder.ID;
121  item.Name = "Link to Objects folder";
122  m_scene.InventoryService.AddItem(item);
123 
124  InventoryCollection coll = m_scene.InventoryService.GetFolderContent(m_userID, m_rootFolderID);
125  m_rootDescendents = coll.Items.Count + coll.Folders.Count;
126  Console.WriteLine("Number of descendents: " + m_rootDescendents);
127  }
128 
129  [Test]
130  public void Test_001_SimpleFolder()
131  {
132  TestHelpers.InMethod();
133 
134  Init();
135 
136  FetchInvDescHandler handler = new FetchInvDescHandler(m_scene.InventoryService, null, m_scene);
139 
140  string request = "<llsd><map><key>folders</key><array><map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
141  request += m_rootFolderID;
142  request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map></array></map></llsd>";
143 
144  string llsdresponse = handler.FetchInventoryDescendentsRequest(request, "/FETCH", string.Empty, req, resp);
145 
146  Assert.That(llsdresponse != null, Is.True, "Incorrect null response");
147  Assert.That(llsdresponse != string.Empty, Is.True, "Incorrect empty response");
148  Assert.That(llsdresponse.Contains("00000000-0000-0000-0000-000000000000"), Is.True, "Response should contain userID");
149 
150  string descendents = "descendents</key><integer>" + m_rootDescendents + "</integer>";
151  Assert.That(llsdresponse.Contains(descendents), Is.True, "Incorrect number of descendents");
152  Console.WriteLine(llsdresponse);
153  }
154 
155  [Test]
157  {
158  TestHelpers.InMethod();
159 
160  FetchInvDescHandler handler = new FetchInvDescHandler(m_scene.InventoryService, null, m_scene);
163 
164  string request = "<llsd><map><key>folders</key><array>";
165  request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
166  request += m_rootFolderID;
167  request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map>";
168  request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
169  request += m_notecardsFolder;
170  request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map>";
171  request += "</array></map></llsd>";
172 
173  string llsdresponse = handler.FetchInventoryDescendentsRequest(request, "/FETCH", string.Empty, req, resp);
174  Console.WriteLine(llsdresponse);
175 
176  string descendents = "descendents</key><integer>" + m_rootDescendents + "</integer>";
177  Assert.That(llsdresponse.Contains(descendents), Is.True, "Incorrect number of descendents for root folder");
178  descendents = "descendents</key><integer>2</integer>";
179  Assert.That(llsdresponse.Contains(descendents), Is.True, "Incorrect number of descendents for Notecard folder");
180 
181  Assert.That(llsdresponse.Contains("10000000-0000-0000-0000-000000000001"), Is.True, "Notecard 1 is missing from response");
182  Assert.That(llsdresponse.Contains("20000000-0000-0000-0000-000000000002"), Is.True, "Notecard 2 is missing from response");
183  }
184 
185  [Test]
186  public void Test_003_Links()
187  {
188  TestHelpers.InMethod();
189 
190  FetchInvDescHandler handler = new FetchInvDescHandler(m_scene.InventoryService, null, m_scene);
193 
194  string request = "<llsd><map><key>folders</key><array><map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
195  request += "f0000000-0000-0000-0000-00000000000f";
196  request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map></array></map></llsd>";
197 
198  string llsdresponse = handler.FetchInventoryDescendentsRequest(request, "/FETCH", string.Empty, req, resp);
199  Console.WriteLine(llsdresponse);
200 
201  string descendents = "descendents</key><integer>2</integer>";
202  Assert.That(llsdresponse.Contains(descendents), Is.True, "Incorrect number of descendents for Test Folder");
203 
204  // Make sure that the note card link is included
205  Assert.That(llsdresponse.Contains("Link to notecard"), Is.True, "Link to notecard is missing");
206 
207  //Make sure the notecard item itself is included
208  Assert.That(llsdresponse.Contains("Test Notecard 2"), Is.True, "Notecard 2 item (the source) is missing");
209 
210  // Make sure that the source item is before the link item
211  int pos1 = llsdresponse.IndexOf("Test Notecard 2");
212  int pos2 = llsdresponse.IndexOf("Link to notecard");
213  Assert.Less(pos1, pos2, "Source of link is after link");
214 
215  // Make sure the folder link is included
216  Assert.That(llsdresponse.Contains("Link to Objects folder"), Is.True, "Link to Objects folder is missing");
217 
218 /* contents of link folder are not supposed to be listed
219  // Make sure the objects inside the Objects folder are included
220  // Note: I'm not entirely sure this is needed, but that's what I found in the implementation
221  Assert.That(llsdresponse.Contains("Some Object"), Is.True, "Some Object item (contents of the source) is missing");
222 */
223  // Make sure that the source item is before the link item
224  pos1 = llsdresponse.IndexOf("Some Object");
225  pos2 = llsdresponse.IndexOf("Link to Objects folder");
226  Assert.Less(pos1, pos2, "Contents of source of folder link is after folder link");
227  }
228 
229  [Test]
231  {
232  TestHelpers.InMethod();
233 
234  FetchInvDescHandler handler = new FetchInvDescHandler(m_scene.InventoryService, null, m_scene);
237 
238  string request = "<llsd><map><key>folders</key><array>";
239  request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
240  request += m_rootFolderID;
241  request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map>";
242  request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
243  request += m_notecardsFolder;
244  request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map>";
245  request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
246  request += m_rootFolderID;
247  request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map>";
248  request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
249  request += m_notecardsFolder;
250  request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map>";
251  request += "</array></map></llsd>";
252 
253  string llsdresponse = handler.FetchInventoryDescendentsRequest(request, "/FETCH", string.Empty, req, resp);
254  Console.WriteLine(llsdresponse);
255 
256  string root_folder = "<key>folder_id</key><uuid>" + m_rootFolderID + "</uuid>";
257  string notecards_folder = "<key>folder_id</key><uuid>" + m_notecardsFolder + "</uuid>";
258 
259  Assert.That(llsdresponse.Contains(root_folder), "Missing root folder");
260  Assert.That(llsdresponse.Contains(notecards_folder), "Missing notecards folder");
261  int count = Regex.Matches(llsdresponse, root_folder).Count;
262  Assert.AreEqual(1, count, "More than 1 root folder in response");
263  count = Regex.Matches(llsdresponse, notecards_folder).Count;
264  Assert.AreEqual(2, count, "More than 1 notecards folder in response"); // Notecards will also be under root, so 2
265  }
266 
267  [Test]
268  public void Test_005_FolderZero()
269  {
270  TestHelpers.InMethod();
271 
272  Init();
273 
274  FetchInvDescHandler handler = new FetchInvDescHandler(m_scene.InventoryService, null, m_scene);
277 
278  string request = "<llsd><map><key>folders</key><array><map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
279  request += UUID.Zero;
280  request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map></array></map></llsd>";
281 
282  string llsdresponse = handler.FetchInventoryDescendentsRequest(request, "/FETCH", string.Empty, req, resp);
283 
284  Assert.That(llsdresponse != null, Is.True, "Incorrect null response");
285  Assert.That(llsdresponse != string.Empty, Is.True, "Incorrect empty response");
286  Assert.That(llsdresponse.Contains("bad_folders</key><array><uuid>00000000-0000-0000-0000-000000000000"), Is.True, "Folder Zero should be a bad folder");
287 
288  Console.WriteLine(llsdresponse);
289  }
290 
291  }
292 
293 }
Inventory Item - contains all the properties associated with an individual inventory piece...
Helpers for setting up scenes.
Definition: SceneHelpers.cs:60
virtual string Name
The name of the node (64 characters or less)
delegate void FetchInventory(IClientAPI remoteClient, UUID itemID, UUID ownerID)
Used to serialize a whole inventory for transfer over the network.