Added folder tests
authorPanagiotis Kanavos <pkanavos@gmail.com>
Mon, 20 Jun 2011 19:46:23 +0000 (22:46 +0300)
committerPanagiotis Kanavos <pkanavos@gmail.com>
Mon, 20 Jun 2011 19:46:23 +0000 (22:46 +0300)
trunk/Pithos.Interfaces/ICloudClient.cs
trunk/Pithos.Network.Test/FolderTests.cs [new file with mode: 0644]
trunk/Pithos.Network.Test/NetworkOpsTest.cs
trunk/Pithos.Network.Test/Pithos.Network.Test.csproj
trunk/Pithos.Network/CloudFilesClient.cs

index 6db6591..bd89aaa 100644 (file)
@@ -30,6 +30,7 @@ namespace Pithos.Interfaces
         void MoveObject(string container, string oldObjectName, string newObjectName);
         bool ObjectExists(string container,string objectName);
         ObjectInfo GetObjectInfo(string container, string objectName);
+        void CreateFolder(string container, string folder);
     }
 
 
@@ -161,6 +162,14 @@ namespace Pithos.Interfaces
 
             return default(ObjectInfo);
         }
+
+        public void CreateFolder(string container, string folder)
+        {
+            Contract.Requires(!String.IsNullOrWhiteSpace(Token));
+            Contract.Requires(StorageUrl != null);
+            Contract.Requires(!String.IsNullOrWhiteSpace(container));
+            Contract.Requires(!String.IsNullOrWhiteSpace(folder));            
+        }
     }
 
     public class ContainerInfo
diff --git a/trunk/Pithos.Network.Test/FolderTests.cs b/trunk/Pithos.Network.Test/FolderTests.cs
new file mode 100644 (file)
index 0000000..fb4863f
--- /dev/null
@@ -0,0 +1,133 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Diagnostics.Contracts;
+using System.Linq;
+using System.Text;
+using NUnit.Framework;
+using Pithos.Interfaces;
+using System.IO;
+
+
+namespace Pithos.Network.Test
+{
+    [TestFixture]
+    class FolderTests
+    {
+        private string _apiKey = "9d3cb7b231e96f72ebe96af1c6cd5112";
+        private string _userName = "pkanavos";
+
+        [SetUp]
+        public void Setup()
+        {
+            Directory.CreateDirectory("RootFolder");
+            File.WriteAllText(@"RootFolder\TestFile1.txt", "This is a test file");
+            Directory.CreateDirectory(@"RootFolder\Folder1");
+            File.WriteAllText(@"RootFolder\Folder1\TestFile1.txt", "This is a test file");
+            Directory.CreateDirectory(@"RootFolder\Folder1\Subfolder1");
+            File.WriteAllText(@"RootFolder\Folder1\Subfolder1\TestFile1.txt", "This is a test file");
+            Directory.CreateDirectory(@"RootFolder\Folder2");
+            File.WriteAllText(@"RootFolder\Folder2\TestFile1.txt", "This is a test file");
+
+        }
+
+        [TearDown]
+        public void TearDown()
+        {
+            Directory.Delete("RootFolder",true);
+        }
+        
+        [Test]
+        public void TestCreateSingleFolder([Values(true, false)]bool usePithos)
+        {
+            ICloudClient client = new CloudFilesClient { UsePithos = usePithos };
+            client.Authenticate(_userName, _apiKey);
+
+            client.CreateContainer("Pithos");
+
+            client.CreateFolder("Pithos", "RootFolder");
+
+            Assert.IsTrue(client.ObjectExists("Pithos","RootFolder"));
+            var info = client.GetObjectInfo("Pithos", "RootFolder");
+            Assert.AreEqual(@"application/directory",info.Content_Type);
+            
+        } 
+        
+        [Test]
+        public void TestCreateSubFolders([Values(true, false)]bool usePithos)
+        {
+            ICloudClient client = new CloudFilesClient { UsePithos = usePithos };
+            client.Authenticate(_userName, _apiKey);
+
+            client.CreateContainer("Pithos");
+
+            client.CreateFolder("Pithos", "RootFolder");
+            client.CreateFolder("Pithos", "RootFolder/Folder1");
+            var localInfo = new FileInfo("test.txt");
+            client.PutObject("Pithos","RootFolder/Folder1/test.txt","test.txt",localInfo.Length);
+
+
+            Assert.IsTrue(client.ObjectExists("Pithos", "RootFolder/Folder1"));
+            var folderInfo = client.GetObjectInfo("Pithos", "RootFolder/Folder1");
+            Assert.AreEqual(@"application/directory",folderInfo.Content_Type);
+
+
+            Assert.IsTrue(client.ObjectExists("Pithos", "RootFolder/Folder1/test.txt"));
+            var fileInfo = client.GetObjectInfo("Pithos", "RootFolder/Folder1/test.txt");
+            Assert.AreEqual(@"application/octet-stream", fileInfo.Content_Type);
+            
+        }
+        
+        [Test]
+        public void TestDeleteSubFolders([Values(true, false)]bool usePithos)
+        {
+            throw new NotImplementedException();
+            ICloudClient client = new CloudFilesClient { UsePithos = usePithos };
+            client.Authenticate(_userName, _apiKey);
+
+            client.CreateContainer("Pithos");
+
+            client.CreateFolder("Pithos", "RootFolder");
+            client.CreateFolder("Pithos", "RootFolder/Folder1");
+            var localInfo = new FileInfo("test.txt");
+            client.PutObject("Pithos","RootFolder/Folder1/test.txt","test.txt",localInfo.Length);
+
+            client.DeleteObject("Pithos", "RootFolder/Folder1");
+
+            Assert.IsTrue(client.ObjectExists("Pithos", "RootFolder/Folder1"));
+            var folderInfo = client.GetObjectInfo("Pithos", "RootFolder/Folder1");
+            Assert.AreEqual(@"application/directory",folderInfo.Content_Type);
+
+
+            Assert.IsTrue(client.ObjectExists("Pithos", "RootFolder/Folder1/test.txt"));
+            var fileInfo = client.GetObjectInfo("Pithos", "RootFolder/Folder1/test.txt");
+            Assert.AreEqual(@"application/octet-stream", fileInfo.Content_Type);
+            
+        } 
+        
+        [Test]
+        public void TestCreateSubFoldersInOneStep([Values(true, false)]bool usePithos)
+        {
+            ICloudClient client = new CloudFilesClient { UsePithos = usePithos };
+            client.Authenticate(_userName, _apiKey);
+
+            client.CreateContainer("Pithos");
+            
+            client.CreateFolder("Pithos", "RootFolder2/Folder1");
+
+            Assert.IsTrue(client.ObjectExists("Pithos", "RootFolder2/Folder1"));
+            var folderInfo = client.GetObjectInfo("Pithos", "RootFolder2/Folder1");
+            Assert.AreEqual(@"application/directory",folderInfo.Content_Type);
+
+            Assert.IsTrue(client.ObjectExists("Pithos", "RootFolder2"));
+            folderInfo = client.GetObjectInfo("Pithos", "RootFolder2");
+            Assert.AreEqual(@"application/directory", folderInfo.Content_Type);
+
+
+            Assert.IsTrue(client.ObjectExists("Pithos", "RootFolder2/Folder1/test.txt"));
+            var fileInfo = client.GetObjectInfo("Pithos", "RootFolder2/Folder1/test.txt");
+            Assert.AreEqual(@"application/octet-stream", fileInfo.Content_Type);
+            
+        }
+    }
+}
index 41eef3d..4949ca2 100644 (file)
@@ -318,6 +318,8 @@ namespace Pithos.Network.Test
             });
 
         }
+
+
     }
 
     
index 20a1e92..cd9855e 100644 (file)
@@ -64,6 +64,7 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="ChecksumTest.cs" />
+    <Compile Include="FolderTests.cs" />
     <Compile Include="NetworkOpsTest.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
index 485563c..6c6a992 100644 (file)
@@ -224,6 +224,25 @@ namespace Pithos.Network
             }
         }
 
+        public void CreateFolder(string container, string folder)
+        {
+            if (String.IsNullOrWhiteSpace(container))
+                throw new ArgumentNullException("container", "The container property can't be empty");
+            if (String.IsNullOrWhiteSpace(folder))
+                throw new ArgumentNullException("folder", "The folder property can't be empty");
+
+            var folderUrl=String.Format("{0}/{1}",container,folder);
+            var request = new RestRequest { Path = folderUrl, Method = WebMethod.Put };
+            request.AddHeader("Content-Type", @"application/directory");
+            request.AddHeader("Content-Length", "0");
+
+            var response = _client.Request(request);
+
+            if (response.StatusCode != HttpStatusCode.Created && response.StatusCode != HttpStatusCode.Accepted)
+                throw new WebException(String.Format("CreateFolder failed with unexpected status code {0}", response.StatusCode));
+
+        }
+
         public ContainerInfo GetContainerInfo(string container)
         {
             if (String.IsNullOrWhiteSpace(container))