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