All files
[pithos-ms-client] / trunk / Pithos.Network.Test / NetworkOpsTest.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Diagnostics;
4 using System.Diagnostics.Contracts;
5 using System.Linq;
6 using System.Text;
7 using NUnit.Framework;
8 using Pithos.Interfaces;
9 using System.IO;
10
11 namespace Pithos.Network.Test
12 {
13     [TestFixture]
14     class NetworkOpsTest
15     {
16         [Test]
17         public void TestAuthenticate()
18         {
19             ICloudClient client = new CloudFilesClient();
20             client.Authenticate("", "");
21             string storageUrl=client.StorageUrl;
22             string token = client.Token;
23
24             Assert.IsNotEmpty(storageUrl, "Storage Url was empty");
25             Assert.IsNotEmpty(token, "Token was empty");
26         }
27
28         [Test]
29         public void TestListContainers()
30         {
31             ICloudClient client = new CloudFilesClient();
32             client.Authenticate("", "");
33
34             IList<ContainerInfo> containers=client.ListContainers();
35             Assert.IsTrue(containers.Count()>1);
36         }
37
38         [Test]
39         public void TestListObjects()
40         {
41             ICloudClient client = new CloudFilesClient();
42             client.Authenticate("", "");
43
44             IList<ObjectInfo> objects=client.ListObjects("PITHOS");
45             Assert.IsTrue(objects.Count()>=1);
46         }
47
48         [Test]
49         public void TestContainerExists()
50         {
51             ICloudClient client = new CloudFilesClient();
52             client.Authenticate("", "");
53
54             bool dnzExists=client.ContainerExists("DotNetZone");
55             bool pithosExists = client.ContainerExists("PITHOS");
56             bool mooExists = client.ContainerExists("Moo");
57             Assert.IsTrue(dnzExists);
58             Assert.IsTrue(pithosExists);
59             Assert.IsFalse(mooExists);
60         }
61
62         [Test]
63         public void TestGetContainerInfo()
64         {
65             ICloudClient client = new CloudFilesClient();
66             client.Authenticate("", "");
67
68             var dnzInfo =client.GetContainerInfo("DotNetZone");
69             Assert.AreNotEqual(ContainerInfo.Empty, dnzInfo);
70
71             var pithosInfo = client.GetContainerInfo("PITHOS");
72             Assert.AreNotEqual(ContainerInfo.Empty, pithosInfo);
73
74             var mooInfo = client.GetContainerInfo("moo");
75             Assert.AreEqual(ContainerInfo.Empty, mooInfo);
76         }
77
78         [Test]
79         public void TestCreateContainer()
80         {
81             Assert.DoesNotThrow(() =>
82                                     {
83                                         ICloudClient client = new CloudFilesClient();
84                                         client.Authenticate("", "");
85
86                                         client.CreateContainer("Shares");
87                                         Assert.IsTrue(client.ContainerExists("Shares"));
88                                         client.CreateContainer("DotNetZone");
89                                         Assert.IsTrue(client.ContainerExists("DotNetZone"));
90                                     });
91         }
92
93         [Test]
94         public void TestGetObject()
95         {
96             Assert.DoesNotThrow(() =>
97             {
98                 ICloudClient client = new CloudFilesClient();
99                 client.Authenticate("", "");
100
101                 client.CreateContainer("Shares");
102                 Assert.IsTrue(client.ContainerExists("Shares"));
103                 using (var stream = client.GetObject("DotNetZone", "OData and WCF Data Services.pptx"))
104                 using(var file=File.Create(@"e:\test.pptx",4096,FileOptions.DeleteOnClose))
105                 {
106                     stream.CopyTo(file);
107                     Assert.IsTrue(File.Exists(@"e:\test.pptx"));
108                 }
109                 
110             });
111             
112         }
113
114         [Test]
115         public void TestPutObject()
116         {
117             Assert.DoesNotThrow(() =>
118             {
119                 ICloudClient client = new CloudFilesClient();
120                 client.Authenticate("", "");
121
122                 client.CreateContainer("Shares");
123                 Assert.IsTrue(client.ContainerExists("Shares"));                
124
125                 var filePath = @"e:\DeveloperGuide.pdf";
126                 FileInfo info=new FileInfo(filePath);
127                 
128                 using (var file = File.OpenRead(filePath))
129                 {
130                     client.PutObject("Shares",info.Name, file,info.Length );
131                 }
132
133             });
134
135         }
136
137         [Test]
138         public void TestGetObjectMetadata()
139         {
140             Assert.DoesNotThrow(() =>
141             {
142                 ICloudClient client = new CloudFilesClient();
143                 client.Authenticate("", "");
144
145                 var filePath = "DeveloperGuide.pdf";
146                 var meta=client.GetObjectInfo("Shares", filePath);
147                 Assert.IsNotEmpty(meta.Hash);
148                 Assert.AreEqual(meta.Name,filePath);
149
150             });
151
152         }
153
154         [Test]
155         public void TestDeleteObject()
156         {
157             Assert.DoesNotThrow(() =>
158             {
159                 ICloudClient client = new CloudFilesClient();
160                 client.Authenticate("", "");
161
162                 client.CreateContainer("Shares");
163                 Assert.IsTrue(client.ContainerExists("Shares"),"Container Exists");                
164
165                 var filePath = @"e:\DeveloperGuide.pdf";
166                 FileInfo info=new FileInfo(filePath);
167                 
168                 using (var file = File.OpenRead(filePath))
169                 {
170                     client.PutObject("Shares",info.Name, file,info.Length );
171                 }
172
173                 Assert.IsTrue(client.ObjectExists("Shares",info.Name),"File Created");
174
175                 client.DeleteObject("Shares",info.Name);
176                 Assert.IsFalse(client.ObjectExists("Shares", info.Name),"File Deleted");
177                 
178
179
180
181             });
182
183         }
184
185         [Test]
186         public void TestFilesWithSpaces()
187         {
188             ICloudClient client = new CloudFilesClient();
189             client.Authenticate("", "");
190             
191             var testName = "Name with spaces.txt";
192             
193             using(var stream=new MemoryStream())
194             using (var writer = new StreamWriter(stream))
195             {
196                 
197                 writer.WriteLine("This is a test line");
198                 stream.Seek(0, 0);
199                 
200                 client.PutObject("PITHOS",testName,stream,stream.Length);                
201             }
202
203             Assert.DoesNotThrow(() =>
204                                     {
205                                         var info = client.GetObjectInfo("PITHOS", testName);
206                                         Assert.AreEqual(testName, info.Name);
207                                     });
208             Assert.DoesNotThrow(() =>
209                                     {
210                                         client.DeleteObject("PITHOS", testName);                                        
211                                     });
212         }
213
214         [Test]
215         public void TestMoveObject()
216         {
217             Assert.DoesNotThrow(() =>
218             {
219                 ICloudClient client = new CloudFilesClient();
220                 client.Authenticate("", "");
221
222                 client.CreateContainer("Shares");
223                 Assert.IsTrue(client.ContainerExists("Shares"),"Container Exists");                
224
225                 var filePath = @"e:\DeveloperGuide.pdf";
226                 FileInfo info=new FileInfo(filePath);
227                 
228                 using (var file = File.OpenRead(filePath))
229                 {
230                     client.PutObject("Shares",info.Name, file,info.Length );
231                 }
232
233                 Assert.IsTrue(client.ObjectExists("Shares",info.Name),"File Created");
234
235                 client.MoveObject("Shares",info.Name,"smoo.pdf");
236                 Assert.IsFalse(client.ObjectExists("Shares", info.Name),"Original File Deleted");
237                 Assert.IsTrue(client.ObjectExists("Shares", "smoo.pdf"), "Target File Created");
238
239             });
240
241         }
242
243         [Test]
244         public void TestGetObjectMissing()
245         {
246
247         }
248
249
250         [Test]
251         public void TestAuthenticateMissingArguments()
252         {
253             Assert.Catch<ArgumentNullException>(() =>
254             {
255                 ICloudClient client = new CloudFilesClient();                
256                 client.Authenticate("someUser",null);
257             });
258
259             Assert.Catch<ArgumentNullException>(() =>
260             {
261                 ICloudClient client = new CloudFilesClient();
262                 client.Authenticate(null,"someKey");
263             });
264
265         }
266     }
267
268     
269 }