public IEnumerable<FileSystemInfoContract> GetChildItem(DirectoryInfoContract parent)
 {
     try {
         return gateway.GetChildItemAsync(rootName, parent.Id).Result;
     } catch (AggregateException ex) when (ex.InnerExceptions.Count == 1) {
         throw ex.InnerExceptions[0];
     }
 }
            public TestDirectory(IAsyncCloudGateway gateway, RootName root, string apiKey, string path)
            {
                this.gateway = gateway;
                this.root = root;

                var rootDirectory = gateway.GetRootAsync(root, apiKey).Result;
                directory = gateway.NewDirectoryItemAsync(root, rootDirectory.Id, path).Result;
            }
        public void NewItem_WherePathIsRootAndItemTypeIsDirectory_CallsGatewayCorrectly()
        {
            var rootName = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;
            var newItem = new DirectoryInfoContract(@"\SubDir", "SubDir", DateTime.Now, DateTime.Now);

            var gatewayMock = mockingFixture.InitializeGetChildItems(rootName, @"\", rootDirectoryItems);
            gatewayMock.Setup(g => g.NewDirectoryItem(rootName, new DirectoryId(@"\"), "NewSubDir"))
                .Returns(newItem)
                .Verifiable();
            compositionFixture.ExportGateway(gatewayMock.Object);

            var result = new PipelineFixture().Invoke(
                FileSystemFixture.NewDriveCommand,
                @"New-Item -Type Directory -Path X:\ -Name NewSubDir"
            );

            Assert.AreEqual(1, result.Count, "Unexpected number of results");
            Assert.AreEqual(newItem, result[0].BaseObject, "Mismatching result");

            gatewayMock.VerifyAll();
        }
 public DirectoryInfoContract NewDirectoryItem(DirectoryInfoContract parent, string name)
 {
     return gateway.NewDirectoryItem(rootName, parent.Id, name);
 }
        public FileSystemInfoContract MoveItem(FileSystemInfoContract source, string movePath, DirectoryInfoContract destination)
        {
            InvalidateDrive();

            return gateway.MoveItem(rootName, source.Id, movePath, destination.Id);
        }
        public FileSystemInfoContract CopyItem(FileSystemInfoContract source, string copyPath, DirectoryInfoContract destination, bool recurse)
        {
            InvalidateDrive();

            return gateway.CopyItem(rootName, source.Id, copyPath, destination.Id, recurse);
        }
        public void RenameItem_WherePathIsDirectoryInSubDirectory_RenamesDirectory()
        {
            var rootName = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;
            var subDirectoryItems = fileSystemFixture.SubDirectoryItems;

            var original = (DirectoryInfoContract)subDirectoryItems.Single(i => i.Name == "SubSubDir");
            var renamed = default(DirectoryInfoContract);

            var gatewayMock = mockingFixture.InitializeGetChildItems(rootName, @"\SubDir", rootDirectoryItems, subDirectoryItems);
            gatewayMock.Setup(g => g.RenameItem(rootName, new DirectoryId(@"\SubDir\SubSubDir"), "SubSubDirRenamed"))
                .Returns(renamed = new DirectoryInfoContract(original.Id.Value.Replace("SubSubDir", "SubSubDirRenamed"), original.Name.Replace("SubSubDir", "SubSubDirRenamed"), original.Created, original.Updated))
                .Verifiable();
            compositionFixture.ExportGateway(gatewayMock.Object);

            var result = new PipelineFixture().Invoke(
                FileSystemFixture.NewDriveCommand,
                @"Rename-Item -Path X:\SubDir\SubSubDir -NewName SubSubDirRenamed",
                @"Get-ChildItem -Path X:\SubDir"
            );

            Assert.AreEqual(subDirectoryItems.Count(), result.Count, "Unexpected number of results");
            CollectionAssert.DoesNotContain(result.Select(p => p.BaseObject).ToArray(), original, "Unrenamed original remains");
            CollectionAssert.Contains(result.Select(p => p.BaseObject).ToArray(), renamed, "Renamed item is missing");

            gatewayMock.VerifyAll();
        }
        public void MoveItemAsync_WherePathIsDirectoryAndDestinationDirectoryIsDifferent_MovesDirectory()
        {
            var rootName = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;
            var subDirectory2Items = fileSystemFixture.SubDirectory2Items;

            var original = (DirectoryInfoContract)rootDirectoryItems.Single(i => i.Name == "SubDir");
            var moved = default(DirectoryInfoContract);

            var gatewayMock = mockingFixture.InitializeGetChildItemsAsync(rootName, @"\SubDir2", rootDirectoryItems, subDirectory2Items);
            gatewayMock.Setup(g => g.MoveItemAsync(rootName, new DirectoryId(@"\SubDir"), @"SubDirMove", new DirectoryId(@"\SubDir2"), It.IsAny<Func<FileSystemInfoLocator>>()))
                .ReturnsAsync(moved = new DirectoryInfoContract(original.Id.Value.Replace("SubDir", "SubDirMove"), original.Name.Replace("SubDir", "SubDirMove"), original.Created, original.Updated))
                .Verifiable();
            compositionFixture.ExportAsyncGateway(gatewayMock.Object);

            var result = new PipelineFixture().Invoke(
                FileSystemFixture.NewDriveCommand,
                @"Move-Item -Path X:\SubDir -Destination X:\SubDir2\SubDirMove",
                @"Get-ChildItem -Path X:\SubDir2"
            );

            Assert.AreEqual(subDirectory2Items.Count() + 1, result.Count, "Unexpected number of results");
            CollectionAssert.Contains(result.Select(p => p.BaseObject).ToArray(), moved, "Copied item is missing");

            gatewayMock.VerifyAll();
        }
 public FileSystemInfoContract MoveItem(FileSystemInfoContract source, string movePath, DirectoryInfoContract destination)
 {
     try {
         Func<FileSystemInfoLocator> locator = () => new FileSystemInfoLocator(source);
         return gateway.MoveItemAsync(rootName, source.Id, movePath, destination.Id, locator).Result;
     } catch (AggregateException ex) when (ex.InnerExceptions.Count == 1) {
         throw ex.InnerExceptions[0];
     } finally {
         InvalidateDrive();
     }
 }
        public void NewItem_WherePathIsIncompleteAndItemTypeIsFile_CreatesNewFile()
        {
            var rootName = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;
            var newIntermediateDirectory = new DirectoryInfoContract(@"\NewSubDir", "NewSubDir", DateTime.Now, DateTime.Now);
            var newItem = new FileInfoContract(@"\NewSubDir\NewSubFile", "NewSubFile", DateTime.Now, DateTime.Now, 0, null);

            var gatewayMock = mockingFixture.InitializeGetChildItems(rootName, @"\", rootDirectoryItems);
            gatewayMock.Setup(g => g.NewDirectoryItem(rootName, new DirectoryId(@"\"), "NewSubDir"))
                .Returns(newIntermediateDirectory)
                .Verifiable();
            gatewayMock.Setup(g => g.NewFileItem(rootName, new DirectoryId(@"\NewSubDir"), "NewSubFile", null, It.Is<IProgress<ProgressValue>>(p => true)))
                .Returns(newItem)
                .Verifiable();
            var subDirs = new Queue<string>(new[] { @"\", @"\NewSubDir" });
            var predicateIterator = new Func<string, bool>(s => s == subDirs.Dequeue());
            gatewayMock.SetupSequence(g => g.GetChildItem(rootName, It.Is<DirectoryId>(d => predicateIterator(d.Value))))
                .Returns(new FileSystemInfoContract[0])
                .Returns(new FileSystemInfoContract[0])
                .Throws(new InvalidOperationException(@"Redundant access to directory"));
            compositionFixture.ExportGateway(gatewayMock.Object);

            var result = new PipelineFixture().Invoke(
                FileSystemFixture.NewDriveCommand,
                @"New-Item -Type File -Path X:\NewSubDir -Name NewSubFile -Force",
                @"Get-ChildItem -Path X:\ -Filter 'NewSub*' -Recurse"
            );

            Assert.AreEqual(2, result.Count, "Unexpected number of results");
            CollectionAssert.AreEqual(result.Select(i => i.BaseObject).ToArray(), new FileSystemInfoContract[] { newIntermediateDirectory, newItem }, "Unexpected result");

            gatewayMock.VerifyAll();
        }
        public void NewItem_WherePathIsSubDirAndItemTypeIsDirectory_CreatesNewDirectory()
        {
            var rootName = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;
            var subDirectoryItems = fileSystemFixture.SubDirectoryItems;
            var newItem = new DirectoryInfoContract(@"\SubDir\NewSubSubDir", "NewSubSubDir", DateTime.Now, DateTime.Now);

            var gatewayMock = mockingFixture.InitializeGetChildItems(rootName, @"\SubDir", rootDirectoryItems, subDirectoryItems);
            gatewayMock.Setup(g => g.NewDirectoryItem(rootName, new DirectoryId(@"\SubDir"), "NewSubSubDir"))
                .Returns(newItem)
                .Verifiable();
            compositionFixture.ExportGateway(gatewayMock.Object);

            var result = new PipelineFixture().Invoke(
                FileSystemFixture.NewDriveCommand,
                @"New-Item -Type Directory -Path X:\SubDir -Name NewSubSubDir",
                @"Get-ChildItem -Path X:\SubDir"
            );

            Assert.AreEqual(subDirectoryItems.Length + 1, result.Count, "Unexpected number of results");
            CollectionAssert.Contains(result.Select(p => p.BaseObject).ToArray(), newItem, "Missing result");

            gatewayMock.VerifyAll();
        }
        public void CopyItem_WherePathIsDirectoryAndDestinationDirectoryIsDifferent_CopiesDirectory()
        {
            var rootName = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;
            var subDirectoryItems = fileSystemFixture.SubDirectoryItems;

            var original = (DirectoryInfoContract)rootDirectoryItems.Single(i => i.Name == "SubDir");
            var copy = default(DirectoryInfoContract);

            var gatewayMock = mockingFixture.InitializeGetChildItems(rootName, @"\SubDir", rootDirectoryItems, subDirectoryItems);
            gatewayMock.Setup(g => g.CopyItem(rootName, new DirectoryId(@"\SubDir"), @"SubDirCopy", new DirectoryId(@"\SubDir"), false))
                .Returns(copy = new DirectoryInfoContract(original.Id.Value.Replace("SubDir", "SubDirCopy"), original.Name.Replace("SubDir", "SubDirCopy"), original.Created, original.Updated))
                .Verifiable();
            compositionFixture.ExportGateway(gatewayMock.Object);

            var result = new PipelineFixture().Invoke(
                FileSystemFixture.NewDriveCommand,
                @"Copy-Item -Path X:\SubDir -Destination X:\SubDir\SubDirCopy",
                @"Get-ChildItem -Path X:\SubDir"
            );

            Assert.AreEqual(subDirectoryItems.Count() + 1, result.Count, "Unexpected number of results");
            CollectionAssert.Contains(result.Select(p => p.BaseObject).ToArray(), copy, "Copied item is missing");

            gatewayMock.VerifyAll();
        }
        public FileInfoContract NewFileItem(DirectoryInfoContract parent, string name, Stream content, ProgressProxy progress)
        {
            try {
                if (content != null && !string.IsNullOrEmpty(encryptionKey))
                    content = content.Encrypt(encryptionKey);

                ProgressProxy.ProgressFunc<FileInfoContract> func = p => gateway.NewFileItemAsync(rootName, parent.Id, name, content, p);
                return ProgressProxy.TraceProgressOn(func, progress);
            } catch (AggregateException ex) when (ex.InnerExceptions.Count == 1) {
                throw ex.InnerExceptions[0];
            } finally {
                InvalidateDrive();
            }
        }
 public DirectoryInfoContract NewDirectoryItem(DirectoryInfoContract parent, string name)
 {
     try {
         return gateway.NewDirectoryItemAsync(rootName, parent.Id, name).Result;
     } catch (AggregateException ex) when (ex.InnerExceptions.Count == 1) {
         throw ex.InnerExceptions[0];
     }
 }
        public FileInfoContract NewFileItem(DirectoryInfoContract parent, string name, Stream content, ProgressProxy progress)
        {
            if (!string.IsNullOrEmpty(encryptionKey))
                content = content.Encrypt(encryptionKey);

            InvalidateDrive();

            return gateway.NewFileItem(rootName, parent.Id, name, content, progress);
        }
 public IEnumerable<FileSystemInfoContract> GetChildItem(DirectoryInfoContract parent)
 {
     return gateway.GetChildItem(rootName, parent.Id);
 }
 public FileSystemInfoContract CopyItem(FileSystemInfoContract source, string copyPath, DirectoryInfoContract destination, bool recurse)
 {
     try {
         return gateway.CopyItemAsync(rootName, source.Id, copyPath, destination.Id, recurse).Result;
     } catch (AggregateException ex) when (ex.InnerExceptions.Count == 1) {
         throw ex.InnerExceptions[0];
     } finally {
         InvalidateDrive();
     }
 }