root / trunk / Pithos.Network.Test / NetworkOpsTest.cs @ ed0be341
History | View | Annotate | Download (9.6 kB)
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 { UsePithos = _usePithos }; |
47 |
client.Authenticate(_userName, _apiKey); |
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 { UsePithos = _usePithos }; |
57 |
client.Authenticate(_userName, _apiKey); |
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 { UsePithos = _usePithos }; |
71 |
client.Authenticate(_userName, _apiKey); |
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 { UsePithos = _usePithos }; |
108 |
client.Authenticate(_userName, _apiKey); |
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(@"test.pptx",4096,FileOptions.DeleteOnClose)) |
114 |
{ |
115 |
stream.CopyTo(file); |
116 |
Assert.IsTrue(File.Exists(@"test.pptx")); |
117 |
} |
118 |
|
119 |
}); |
120 |
|
121 |
} |
122 |
|
123 |
[Test] |
124 |
public void TestPutObject() |
125 |
{ |
126 |
Assert.DoesNotThrow(() => |
127 |
{ |
128 |
ICloudClient client = new CloudFilesClient { UsePithos = _usePithos }; |
129 |
client.Authenticate(_userName, _apiKey); |
130 |
|
131 |
client.CreateContainer("Shares"); |
132 |
Assert.IsTrue(client.ContainerExists("Shares")); |
133 |
|
134 |
var filePath = "devguide.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 { UsePithos = _usePithos }; |
152 |
client.Authenticate(_userName, _apiKey); |
153 |
|
154 |
client.CreateContainer("Shares"); |
155 |
Assert.IsTrue(client.ContainerExists("Shares")); |
156 |
|
157 |
var filePath = "devguide.pdf"; |
158 |
FileInfo info = new FileInfo(filePath); |
159 |
|
160 |
using (var file = File.OpenRead(filePath)) |
161 |
{ |
162 |
client.PutObject("Shares", info.Name, file, info.Length); |
163 |
} |
164 |
|
165 |
|
166 |
|
167 |
var meta=client.GetObjectInfo("Shares", filePath); |
168 |
Assert.IsNotEmpty(meta.Hash); |
169 |
Assert.AreEqual(meta.Name,filePath); |
170 |
|
171 |
}); |
172 |
|
173 |
} |
174 |
|
175 |
[Test] |
176 |
public void TestDeleteObject() |
177 |
{ |
178 |
Assert.DoesNotThrow(() => |
179 |
{ |
180 |
ICloudClient client = new CloudFilesClient { UsePithos = _usePithos }; |
181 |
client.Authenticate(_userName, _apiKey); |
182 |
|
183 |
client.CreateContainer("Shares"); |
184 |
Assert.IsTrue(client.ContainerExists("Shares"),"Container Exists"); |
185 |
|
186 |
var filePath = "devguide.pdf"; |
187 |
FileInfo info=new FileInfo(filePath); |
188 |
|
189 |
using (var file = File.OpenRead(filePath)) |
190 |
{ |
191 |
client.PutObject("Shares",info.Name, file,info.Length ); |
192 |
} |
193 |
|
194 |
Assert.IsTrue(client.ObjectExists("Shares",info.Name),"File Created"); |
195 |
|
196 |
client.DeleteObject("Shares",info.Name); |
197 |
Assert.IsFalse(client.ObjectExists("Shares", info.Name),"File Deleted"); |
198 |
|
199 |
|
200 |
|
201 |
|
202 |
}); |
203 |
|
204 |
} |
205 |
|
206 |
[Test] |
207 |
public void TestFilesWithSpaces() |
208 |
{ |
209 |
ICloudClient client = new CloudFilesClient { UsePithos = _usePithos }; |
210 |
client.Authenticate(_userName, _apiKey); |
211 |
|
212 |
var testName = "Name with spaces.txt"; |
213 |
|
214 |
using(var stream=new MemoryStream()) |
215 |
using (var writer = new StreamWriter(stream)) |
216 |
{ |
217 |
|
218 |
writer.WriteLine("This is a test line"); |
219 |
stream.Seek(0, 0); |
220 |
|
221 |
client.PutObject("PITHOS",testName,stream,stream.Length); |
222 |
} |
223 |
|
224 |
Assert.DoesNotThrow(() => |
225 |
{ |
226 |
var info = client.GetObjectInfo("PITHOS", testName); |
227 |
Assert.AreEqual(testName, info.Name); |
228 |
}); |
229 |
Assert.DoesNotThrow(() => |
230 |
{ |
231 |
client.DeleteObject("PITHOS", testName); |
232 |
}); |
233 |
} |
234 |
|
235 |
[Test] |
236 |
public void TestMoveObject() |
237 |
{ |
238 |
Assert.DoesNotThrow(() => |
239 |
{ |
240 |
ICloudClient client = new CloudFilesClient { UsePithos = _usePithos }; |
241 |
client.Authenticate(_userName, _apiKey); |
242 |
|
243 |
client.CreateContainer("Shares"); |
244 |
Assert.IsTrue(client.ContainerExists("Shares"),"Container Exists"); |
245 |
|
246 |
var filePath = "devguide.pdf"; |
247 |
FileInfo info=new FileInfo(filePath); |
248 |
|
249 |
using (var file = File.OpenRead(filePath)) |
250 |
{ |
251 |
client.PutObject("Shares",info.Name, file,info.Length ); |
252 |
} |
253 |
|
254 |
Assert.IsTrue(client.ObjectExists("Shares",info.Name),"File Created"); |
255 |
|
256 |
client.MoveObject("Shares",info.Name,"smoo.pdf"); |
257 |
Assert.IsFalse(client.ObjectExists("Shares", info.Name),"Original File Deleted"); |
258 |
Assert.IsTrue(client.ObjectExists("Shares", "smoo.pdf"), "Target File Created"); |
259 |
|
260 |
}); |
261 |
|
262 |
} |
263 |
|
264 |
[Test] |
265 |
public void TestGetObjectMissing() |
266 |
{ |
267 |
|
268 |
} |
269 |
|
270 |
|
271 |
[Test] |
272 |
public void TestAuthenticateMissingArguments() |
273 |
{ |
274 |
Assert.Catch<ArgumentNullException>(() => |
275 |
{ |
276 |
ICloudClient client = new CloudFilesClient { UsePithos = _usePithos }; |
277 |
client.Authenticate("someUser",null); |
278 |
}); |
279 |
|
280 |
Assert.Catch<ArgumentNullException>(() => |
281 |
{ |
282 |
ICloudClient client = new CloudFilesClient { UsePithos = _usePithos }; |
283 |
client.Authenticate(null,"someKey"); |
284 |
}); |
285 |
|
286 |
} |
287 |
} |
288 |
|
289 |
|
290 |
} |