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