Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF.Test / NodeTest.cs @ 21141c06

History | View | Annotate | Download (17 kB)

1 f4a72cb8 pkanavos
using System;
2 f4a72cb8 pkanavos
using System.Collections.Generic;
3 f4a72cb8 pkanavos
using System.Linq;
4 f4a72cb8 pkanavos
using System.Text;
5 f4a72cb8 pkanavos
using NUnit.Framework;
6 85c6af07 pkanavos
using Newtonsoft.Json;
7 69588a95 pkanavos
using Pithos.Client.WPF.SelectiveSynch;
8 f4a72cb8 pkanavos
using Pithos.Client.WPF.Utils;
9 69588a95 pkanavos
using Pithos.Interfaces;
10 85c6af07 pkanavos
using System.IO;
11 f4a72cb8 pkanavos
12 f4a72cb8 pkanavos
namespace Pithos.Client.WPF.Test
13 f4a72cb8 pkanavos
{
14 f4a72cb8 pkanavos
    [TestFixture]
15 85c6af07 pkanavos
    internal class NodeTest
16 f4a72cb8 pkanavos
    {
17 f4a72cb8 pkanavos
        [Test]
18 f4a72cb8 pkanavos
        public void TestIteration()
19 f4a72cb8 pkanavos
        {
20 85c6af07 pkanavos
            var root = new Node<int>
21 85c6af07 pkanavos
                           {
22 85c6af07 pkanavos
                               Path = "Root",
23 85c6af07 pkanavos
                               Children =
24 85c6af07 pkanavos
                                   {
25 85c6af07 pkanavos
                                       new Node<int>
26 85c6af07 pkanavos
                                           {
27 85c6af07 pkanavos
                                               Path = "Root/Path1",
28 85c6af07 pkanavos
                                               Children =
29 85c6af07 pkanavos
                                                   {
30 85c6af07 pkanavos
                                                       new Node<int>
31 85c6af07 pkanavos
                                                           {
32 85c6af07 pkanavos
                                                               Path = "Root/Path1/Path11",
33 85c6af07 pkanavos
                                                               Children =
34 85c6af07 pkanavos
                                                                   {
35 85c6af07 pkanavos
                                                                       new Node<int>
36 85c6af07 pkanavos
                                                                           {
37 85c6af07 pkanavos
                                                                               Path = "Root/Path1/Path11/Path111",
38 85c6af07 pkanavos
                                                                               Children =
39 85c6af07 pkanavos
                                                                                   {
40 85c6af07 pkanavos
                                                                                       new Node<int>
41 85c6af07 pkanavos
                                                                                           {
42 85c6af07 pkanavos
                                                                                               Path =
43 85c6af07 pkanavos
                                                                                                   "Root/Path1/Path11/Path111/File1"
44 85c6af07 pkanavos
                                                                                           }
45 85c6af07 pkanavos
                                                                                   }
46 85c6af07 pkanavos
                                                                           }
47 85c6af07 pkanavos
                                                                   }
48 85c6af07 pkanavos
                                                           },
49 85c6af07 pkanavos
                                                       new Node<int> {Path = "Root/Path1/File2"}
50 85c6af07 pkanavos
                                                   }
51 85c6af07 pkanavos
                                           },
52 85c6af07 pkanavos
                                   }
53 85c6af07 pkanavos
                           };
54 f4a72cb8 pkanavos
            Assert.That(root.Count(), Is.EqualTo(6));
55 f4a72cb8 pkanavos
        }
56 85c6af07 pkanavos
57 f4a72cb8 pkanavos
        [Test]
58 f4a72cb8 pkanavos
        public void TestEquals()
59 f4a72cb8 pkanavos
        {
60 85c6af07 pkanavos
            var target = new Node<int>
61 85c6af07 pkanavos
                             {
62 85c6af07 pkanavos
                                 Path = "Root",
63 85c6af07 pkanavos
                                 Children =
64 85c6af07 pkanavos
                                     {
65 85c6af07 pkanavos
                                         new Node<int>
66 85c6af07 pkanavos
                                             {
67 85c6af07 pkanavos
                                                 Path = "Root/Path1",
68 85c6af07 pkanavos
                                                 Children =
69 85c6af07 pkanavos
                                                     {
70 85c6af07 pkanavos
                                                         new Node<int>
71 85c6af07 pkanavos
                                                             {
72 85c6af07 pkanavos
                                                                 Path = "Root/Path1/Path11",
73 85c6af07 pkanavos
                                                                 Children =
74 85c6af07 pkanavos
                                                                     {
75 85c6af07 pkanavos
                                                                         new Node<int>
76 85c6af07 pkanavos
                                                                             {
77 85c6af07 pkanavos
                                                                                 Path = "Root/Path1/Path11/Path111",
78 85c6af07 pkanavos
                                                                                 Children =
79 85c6af07 pkanavos
                                                                                     {
80 85c6af07 pkanavos
                                                                                         new Node<int>
81 85c6af07 pkanavos
                                                                                             {
82 85c6af07 pkanavos
                                                                                                 Path =
83 85c6af07 pkanavos
                                                                                                     "Root/Path1/Path11/Path111/File1"
84 85c6af07 pkanavos
                                                                                             }
85 85c6af07 pkanavos
                                                                                     }
86 85c6af07 pkanavos
                                                                             }
87 85c6af07 pkanavos
                                                                     }
88 85c6af07 pkanavos
                                                             },
89 85c6af07 pkanavos
                                                         new Node<int> {Path = "Root/Path1/File2"}
90 85c6af07 pkanavos
                                                     }
91 85c6af07 pkanavos
                                             },
92 85c6af07 pkanavos
                                     }
93 85c6af07 pkanavos
                             };
94 85c6af07 pkanavos
            var source = new Node<int>
95 85c6af07 pkanavos
                             {
96 85c6af07 pkanavos
                                 Path = "Root",
97 85c6af07 pkanavos
                                 Children =
98 85c6af07 pkanavos
                                     {
99 85c6af07 pkanavos
                                         new Node<int>
100 85c6af07 pkanavos
                                             {
101 85c6af07 pkanavos
                                                 Path = "Root/Path1",
102 85c6af07 pkanavos
                                                 Children =
103 85c6af07 pkanavos
                                                     {
104 85c6af07 pkanavos
                                                         new Node<int>
105 85c6af07 pkanavos
                                                             {
106 85c6af07 pkanavos
                                                                 Path = "Root/Path1/Path11",
107 85c6af07 pkanavos
                                                                 Children =
108 85c6af07 pkanavos
                                                                     {
109 85c6af07 pkanavos
                                                                         new Node<int>
110 85c6af07 pkanavos
                                                                             {
111 85c6af07 pkanavos
                                                                                 Path = "Root/Path1/Path11/Path111",
112 85c6af07 pkanavos
                                                                                 Children =
113 85c6af07 pkanavos
                                                                                     {
114 85c6af07 pkanavos
                                                                                         new Node<int>
115 85c6af07 pkanavos
                                                                                             {
116 85c6af07 pkanavos
                                                                                                 Path =
117 85c6af07 pkanavos
                                                                                                     "Root/Path1/Path11/Path111/File1"
118 85c6af07 pkanavos
                                                                                             }
119 85c6af07 pkanavos
                                                                                     }
120 85c6af07 pkanavos
                                                                             }
121 85c6af07 pkanavos
                                                                     }
122 85c6af07 pkanavos
                                                             },
123 85c6af07 pkanavos
                                                         new Node<int> {Path = "Root/Path1/File2"}
124 85c6af07 pkanavos
                                                     }
125 85c6af07 pkanavos
                                             },
126 85c6af07 pkanavos
                                     }
127 85c6af07 pkanavos
                             };
128 85c6af07 pkanavos
            Assert.That(source.Equals(target), Is.True);
129 f4a72cb8 pkanavos
        }
130 f4a72cb8 pkanavos
131 f4a72cb8 pkanavos
        [Test]
132 f4a72cb8 pkanavos
        public void TestToTree()
133 f4a72cb8 pkanavos
        {
134 85c6af07 pkanavos
            var target = new Node<int>
135 85c6af07 pkanavos
                             {
136 85c6af07 pkanavos
                                 Path = "Root",
137 85c6af07 pkanavos
                                 Children =
138 85c6af07 pkanavos
                                     {
139 85c6af07 pkanavos
                                         new Node<int>
140 85c6af07 pkanavos
                                             {
141 85c6af07 pkanavos
                                                 Path = "Root/Path1",
142 85c6af07 pkanavos
                                                 Children =
143 85c6af07 pkanavos
                                                     {
144 85c6af07 pkanavos
                                                         new Node<int> {Path = "Root/Path1/File2"},
145 85c6af07 pkanavos
                                                         new Node<int>
146 85c6af07 pkanavos
                                                             {
147 85c6af07 pkanavos
                                                                 Path = "Root/Path1/Path11",
148 85c6af07 pkanavos
                                                                 Children =
149 85c6af07 pkanavos
                                                                     {
150 85c6af07 pkanavos
                                                                         new Node<int>
151 85c6af07 pkanavos
                                                                             {
152 85c6af07 pkanavos
                                                                                 Path = "Root/Path1/Path11/Path111",
153 85c6af07 pkanavos
                                                                                 Children =
154 85c6af07 pkanavos
                                                                                     {
155 85c6af07 pkanavos
                                                                                         new Node<int>
156 85c6af07 pkanavos
                                                                                             {
157 85c6af07 pkanavos
                                                                                                 Path =
158 85c6af07 pkanavos
                                                                                                     "Root/Path1/Path11/Path111/File1"
159 85c6af07 pkanavos
                                                                                             }
160 85c6af07 pkanavos
                                                                                     }
161 85c6af07 pkanavos
                                                                             }
162 85c6af07 pkanavos
                                                                     }
163 85c6af07 pkanavos
                                                             },
164 85c6af07 pkanavos
                                                     }
165 85c6af07 pkanavos
                                             },
166 85c6af07 pkanavos
                                     }
167 85c6af07 pkanavos
                             };
168 85c6af07 pkanavos
            var source = new[]
169 85c6af07 pkanavos
                             {
170 85c6af07 pkanavos
                                 Tuple.Create("Root", 0),
171 85c6af07 pkanavos
                                 Tuple.Create("Root/Path1", 0),
172 85c6af07 pkanavos
                                 Tuple.Create("Root/Path1/Path11", 0),
173 85c6af07 pkanavos
                                 Tuple.Create("Root/Path1/Path11/Path111", 0),
174 85c6af07 pkanavos
                                 Tuple.Create("Root/Path1/Path11/Path111/File1", 0),
175 85c6af07 pkanavos
                                 Tuple.Create("Root/Path1/File2", 0)
176 85c6af07 pkanavos
                             };
177 85c6af07 pkanavos
178 85c6af07 pkanavos
            Assert.That(source.ToTree(s => s.Item1, s => s.Item2).First().Equals(target), Is.True);
179 85c6af07 pkanavos
        }
180 85c6af07 pkanavos
181 85c6af07 pkanavos
        [Test]
182 f4a72cb8 pkanavos
183 69588a95 pkanavos
        public void TestObjectInfoToTree()
184 69588a95 pkanavos
        {
185 85c6af07 pkanavos
           
186 85c6af07 pkanavos
            var account = "someaccount";
187 85c6af07 pkanavos
            var container = "Root";
188 85c6af07 pkanavos
            var source = new[]
189 85c6af07 pkanavos
                             {
190 85c6af07 pkanavos
                                 new ObjectInfo
191 85c6af07 pkanavos
                                     {
192 85c6af07 pkanavos
                                         Account = account,
193 85c6af07 pkanavos
                                         Container = container,
194 85c6af07 pkanavos
                                         Name = "Path1",
195 85c6af07 pkanavos
                                         Content_Type = "application/directory"
196 85c6af07 pkanavos
                                     },
197 85c6af07 pkanavos
                                 new ObjectInfo
198 85c6af07 pkanavos
                                     {
199 85c6af07 pkanavos
                                         Account = account,
200 85c6af07 pkanavos
                                         Container = container,
201 85c6af07 pkanavos
                                         Name = "Path1/Path11",
202 85c6af07 pkanavos
                                         Content_Type = "application/folder"
203 85c6af07 pkanavos
                                     },
204 85c6af07 pkanavos
                                 new ObjectInfo
205 85c6af07 pkanavos
                                     {Account = account, Container = container, Name = "Path1/Path11/Path111"},
206 85c6af07 pkanavos
                                 new ObjectInfo
207 85c6af07 pkanavos
                                     {
208 85c6af07 pkanavos
                                         Account = account,
209 85c6af07 pkanavos
                                         Container = container,
210 85c6af07 pkanavos
                                         Name = "Path1/Path11/Path111/File1",
211 85c6af07 pkanavos
                                         Content_Type = "application/octet-stream"
212 85c6af07 pkanavos
                                     },
213 85c6af07 pkanavos
                                 new ObjectInfo {Account = account, Container = container, Name = "Path1/File2"},
214 85c6af07 pkanavos
                                 new ObjectInfo {Account = account, Container = container, Name = "Path1/File3"},
215 85c6af07 pkanavos
                                 new ObjectInfo {Account = account, Container = container, Name = "Path1/File"},
216 85c6af07 pkanavos
                                 new ObjectInfo {Account = account, Container = container, Name = "Path2/File2"},                                 
217 85c6af07 pkanavos
                                 new ObjectInfo {Account = account, Container = container, Name = "Path2/Path21/File2"},
218 85c6af07 pkanavos
                                 new ObjectInfo {Account = account, Container = container, Name = "File02"},
219 85c6af07 pkanavos
                                 new ObjectInfo {Account = account, Container = container, Name = "File03"}
220 85c6af07 pkanavos
                             };
221 85c6af07 pkanavos
222 bbc59cd1 pkanavos
            var tree = source.  ToTree();
223 85c6af07 pkanavos
            var allNodes = (from DirectoryRecord root in tree
224 85c6af07 pkanavos
                            from DirectoryRecord record in root
225 85c6af07 pkanavos
                            select record).ToList();
226 85c6af07 pkanavos
            Assert.That(allNodes.Count, Is.EqualTo(5));
227 85c6af07 pkanavos
        }
228 85c6af07 pkanavos
        public void InferFromPath()
229 85c6af07 pkanavos
        {
230 85c6af07 pkanavos
           
231 69588a95 pkanavos
            var account = "someaccount";
232 69588a95 pkanavos
            var container = "Root";
233 85c6af07 pkanavos
            var source = new[]
234 85c6af07 pkanavos
                             {                                
235 85c6af07 pkanavos
                                 new ObjectInfo {Account = account, Container = container, Name = "Path1/File2"},
236 85c6af07 pkanavos
                                 new ObjectInfo {Account = account, Container = container, Name = "Path1/File3"},
237 85c6af07 pkanavos
                                 new ObjectInfo {Account = account, Container = container, Name = "Path1/Path11/Path111/File3"},
238 85c6af07 pkanavos
                                 new ObjectInfo {Account = account, Container = container, Name = "Path2"},
239 85c6af07 pkanavos
                                 new ObjectInfo {Account = account, Container = container, Name = "Path2/Path21",Content_Type="application/directory"},
240 85c6af07 pkanavos
                                 new ObjectInfo {Account = account, Container = container, Name = "Path2/Path21/Path01",Content_Type="application/directory"},
241 85c6af07 pkanavos
                                 new ObjectInfo {Account = account, Container = container, Name = "Path2/Path21/File211"},
242 85c6af07 pkanavos
                                 new ObjectInfo {Account = account, Container = container, Name = "x2",Content_Type="application/directory"},
243 85c6af07 pkanavos
                                 new ObjectInfo {Account = account, Container = container, Name = "x2/x21",Content_Type="application/directory"},
244 85c6af07 pkanavos
                                 new ObjectInfo {Account = account, Container = container, Name = "x2/x22",Content_Type="application/directory"},
245 85c6af07 pkanavos
                             };
246 69588a95 pkanavos
247 69588a95 pkanavos
            var tree = source.ToTree();
248 69588a95 pkanavos
            var allNodes = (from DirectoryRecord root in tree
249 69588a95 pkanavos
                            from DirectoryRecord record in root
250 85c6af07 pkanavos
                            select record).ToList();
251 85c6af07 pkanavos
            //Root Nodes
252 85c6af07 pkanavos
            Assert.That(tree.Count, Is.EqualTo(3));
253 85c6af07 pkanavos
            //Total nodes
254 85c6af07 pkanavos
            Assert.That(allNodes.Count, Is.EqualTo(9));
255 85c6af07 pkanavos
256 85c6af07 pkanavos
            Assert.That(allNodes.Any(r=>r.DisplayName=="Path1"));
257 85c6af07 pkanavos
            Assert.That(allNodes.Any(r => r.DisplayName == "Path11"));
258 85c6af07 pkanavos
            Assert.That(allNodes.Any(r => r.DisplayName == "Path111"));
259 85c6af07 pkanavos
            Assert.That(allNodes.Any(r => r.DisplayName == "Path2"));
260 85c6af07 pkanavos
            Assert.That(allNodes.Any(r => r.DisplayName == "Path21"));
261 85c6af07 pkanavos
        }   
262 85c6af07 pkanavos
263 85c6af07 pkanavos
264 85c6af07 pkanavos
        [Test]
265 85c6af07 pkanavos
        public void TestShared()
266 85c6af07 pkanavos
        {
267 85c6af07 pkanavos
            var content = File.ReadAllText("json.txt");
268 85c6af07 pkanavos
            var items=JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
269 85c6af07 pkanavos
            Assert.That(items.Count,Is.EqualTo(23));
270 85c6af07 pkanavos
            var tree = items.ToTree();
271 85c6af07 pkanavos
            var allNodes = (from DirectoryRecord root in tree
272 85c6af07 pkanavos
                            from DirectoryRecord record in root
273 85c6af07 pkanavos
                            select record).ToList();
274 85c6af07 pkanavos
            Assert.That(allNodes.Count,Is.EqualTo(10));
275 85c6af07 pkanavos
        }
276 85c6af07 pkanavos
277 85c6af07 pkanavos
        [Test]
278 85c6af07 pkanavos
        public void TestOwn()
279 85c6af07 pkanavos
        {
280 85c6af07 pkanavos
            var content = File.ReadAllText("json2.txt");
281 85c6af07 pkanavos
            var items=JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
282 85c6af07 pkanavos
            Assert.That(items.Count,Is.EqualTo(22));
283 85c6af07 pkanavos
            var tree = items.ToTree();
284 85c6af07 pkanavos
            var allNodes = (from DirectoryRecord root in tree
285 85c6af07 pkanavos
                            from DirectoryRecord record in root
286 85c6af07 pkanavos
                            select record).ToList();
287 85c6af07 pkanavos
            Assert.That(allNodes.Count,Is.EqualTo(9));
288 f4a72cb8 pkanavos
        }
289 bbc59cd1 pkanavos
290 bbc59cd1 pkanavos
        [Test]
291 bbc59cd1 pkanavos
        public void TestUri()
292 bbc59cd1 pkanavos
        {
293 bbc59cd1 pkanavos
            var storageUri = new Uri("http://pithos.okeanos.grnet.gr/v1/pkanavos@gmail.com");
294 bbc59cd1 pkanavos
            var uri = new Uri(storageUri, "pithos");
295 bbc59cd1 pkanavos
            
296 bbc59cd1 pkanavos
            var expected = new Uri("http://pithos.okeanos.grnet.gr/v1/pkanavos@gmail.com/pithos");
297 bbc59cd1 pkanavos
            Assert.That(uri, Is.EqualTo(expected));
298 bbc59cd1 pkanavos
        }
299 f4a72cb8 pkanavos
    }
300 f4a72cb8 pkanavos
}