Example #1
0
        public void NewItemAsync_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.InitializeGetChildItemsAsync(rootName, @"\SubDir", rootDirectoryItems, subDirectoryItems);

            gatewayMock.Setup(g => g.NewDirectoryItemAsync(rootName, new DirectoryId(@"\SubDir"), "NewSubSubDir"))
            .ReturnsAsync(newItem)
            .Verifiable();
            compositionFixture.ExportAsyncGateway(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 RenameItemAsync_WherePathIsFile_RenamesFile()
        {
            var rootName           = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;

            var original = (FileInfoContract)rootDirectoryItems.Single(i => i.Name == "File.ext");
            var renamed  = default(FileInfoContract);

            var gatewayMock = mockingFixture.InitializeGetChildItemsAsync(rootName, @"\", rootDirectoryItems);

            gatewayMock.Setup(g => g.RenameItemAsync(rootName, new FileId(@"\File.ext"), "FileRenamed.ext", It.IsAny <Func <FileSystemInfoLocator> >()))
            .ReturnsAsync(renamed = new FileInfoContract(original.Id.Value.Replace("File", "FileRenamed"), original.Name.Replace("File", "FileRenamed"), original.Created, original.Updated, original.Size, original.Hash))
            .Verifiable();
            compositionFixture.ExportAsyncGateway(gatewayMock.Object);

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

            Assert.AreEqual(rootDirectoryItems.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();
        }
Example #3
0
        public void NewItemAsync_WherePathIsSubDirAndItemTypeIsFile_CanRemoveNewFile()
        {
            var rootName           = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;
            var subDirectoryItems  = fileSystemFixture.SubDirectoryItems;
            var newItem            = new FileInfoContract(@"\SubDir\NewSubFile", "NewSubFile", DateTime.Now, DateTime.Now, 0, null);

            var gatewayMock = mockingFixture.InitializeGetChildItemsAsync(rootName, @"\SubDir", rootDirectoryItems, subDirectoryItems);

            gatewayMock.Setup(g => g.NewFileItemAsync(rootName, new DirectoryId(@"\SubDir"), "NewSubFile", null, It.Is <IProgress <ProgressValue> >(p => true)))
            .ReturnsAsync(newItem)
            .Verifiable();
            gatewayMock.Setup(g => g.RemoveItemAsync(rootName, new FileId(@"\SubDir\NewSubFile"), false))
            .ReturnsAsync(true)
            .Verifiable();
            compositionFixture.ExportAsyncGateway(gatewayMock.Object);

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

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

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

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

            var gatewayMock = mockingFixture.InitializeGetChildItemsAsync(rootName, @"\SubDir", rootDirectoryItems, subDirectoryItems);

            gatewayMock.Setup(g => g.CopyItemAsync(rootName, new DirectoryId(@"\SubDir\SubSubDir"), "SubSubDirCopy", new DirectoryId(@"\SubDir"), false))
            .ReturnsAsync(copied = new DirectoryInfoContract(original.Id.Value.Replace("SubSubDir", "SubSubDirCopy"), original.Name.Replace("SubSubDir", "SubSubDirCopy"), original.Created, original.Updated))
            .Verifiable();
            compositionFixture.ExportAsyncGateway(gatewayMock.Object);

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

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

            gatewayMock.VerifyAll();
        }
Example #5
0
        public void SetContentAsync_WhereNodeIsFileAndEncodingIsByte_AcceptsContent()
        {
            var rootName           = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;
            var content            = TestContent.MultiLineTestContent.Select(c => Convert.ToByte(c)).ToArray();

            var gatewayMock = mockingFixture.InitializeGetChildItemsAsync(rootName, @"\", rootDirectoryItems);

            gatewayMock.Setup(g => g.ClearContentAsync(rootName, new FileId(@"\File.ext"), It.IsAny <Func <FileSystemInfoLocator> >()))
            .ReturnsAsync(true)
            .Verifiable();
            gatewayMock.Setup(g => g.SetContentAsync(rootName, new FileId(@"\File.ext"), It.Is <Stream>(s => new BinaryReader(s, System.Text.Encoding.Default, true).ReadBytes((int)s.Length).SequenceEqual(content)), It.Is <IProgress <ProgressValue> >(p => true), It.IsAny <Func <FileSystemInfoLocator> >()))
            .ReturnsAsync(true)
            .Verifiable();
            compositionFixture.ExportAsyncGateway(gatewayMock.Object);

            var pipelineFixture = new PipelineFixture();

            pipelineFixture.SetVariable("value", content);
            pipelineFixture.Invoke(
                FileSystemFixture.NewDriveCommand,
                @"Set-Content -Path X:\File.ext $value -Encoding Byte"
                );

            gatewayMock.VerifyAll();
        }
        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();
        }
Example #7
0
        public void NewItemAsync_WhereItemIsAlreadyPresent_Throws()
        {
            var rootName           = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;
            var content            = TestContent.SingleLineTestContent;

            var gatewayMock = mockingFixture.InitializeGetChildItemsAsync(rootName, @"\", rootDirectoryItems);

            compositionFixture.ExportAsyncGateway(gatewayMock.Object);

            try {
                var pipelineFixture = new PipelineFixture();
                pipelineFixture.SetVariable("value", content);
                pipelineFixture.Invoke(
                    FileSystemFixture.NewDriveCommand,
                    @"New-Item -Type File -Path X:\ -Name File.ext -Value $value"
                    );

                throw new InvalidOperationException("Expected Exception was not thrown");
            } catch (AssertFailedException ex) {
                Assert.IsTrue(ex.Message.EndsWith("NotSpecified: (X:\\File.ext:String) [New-Item], NotSupportedException", StringComparison.Ordinal));
            }

            gatewayMock.VerifyAll();
        }
Example #8
0
        public void NewItemAsync_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.InitializeGetChildItemsAsync(rootName, @"\", rootDirectoryItems);

            gatewayMock.Setup(g => g.NewDirectoryItemAsync(rootName, new DirectoryId(@"\"), "NewSubDir"))
            .ReturnsAsync(newIntermediateDirectory)
            .Verifiable();
            gatewayMock.Setup(g => g.NewFileItemAsync(rootName, new DirectoryId(@"\NewSubDir"), "NewSubFile", null, It.Is <IProgress <ProgressValue> >(p => true)))
            .ReturnsAsync(newItem)
            .Verifiable();
            var subDirs           = new Queue <string>(new[] { @"\", @"\NewSubDir" });
            var predicateIterator = new Func <string, bool>(s => s == subDirs.Dequeue());

            gatewayMock.SetupSequence(g => g.GetChildItemAsync(rootName, It.Is <DirectoryId>(d => predicateIterator(d.Value))))
            .ReturnsAsync(new FileSystemInfoContract[0])
            .ReturnsAsync(new FileSystemInfoContract[0])
            .ThrowsAsync(new InvalidOperationException(@"Redundant access to directory"));
            compositionFixture.ExportAsyncGateway(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 CopyItem_WherePathIsFile_CopiesFile()
        {
            var rootName           = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;

            var original = (FileInfoContract)rootDirectoryItems.Single(i => i.Name == "File.ext");
            var copy     = default(FileInfoContract);

            var gatewayMock = mockingFixture.InitializeGetChildItems(rootName, @"\", rootDirectoryItems);

            gatewayMock.Setup(g => g.CopyItem(rootName, new FileId(@"\File.ext"), @"FileCopy.ext", new DirectoryId(@"\"), false))
            .Returns(copy = new FileInfoContract(original.Id.Value.Replace("File", "FileCopy"), original.Name.Replace("File", "FileCopy"), original.Created, original.Updated, original.Size, original.Hash))
            .Verifiable();
            compositionFixture.ExportGateway(gatewayMock.Object);

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

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

            gatewayMock.VerifyAll();
        }
Example #10
0
        public void MoveItemAsync_WherePathIsFileAndDestinationDirectoryIsDifferent_MovesFile()
        {
            var rootName           = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;
            var subDirectoryItems  = fileSystemFixture.SubDirectoryItems;

            var original = (FileInfoContract)subDirectoryItems.Single(i => i.Name == "SubFile.ext");
            var moved    = default(FileInfoContract);

            var gatewayMock = mockingFixture.InitializeGetChildItemsAsync(rootName, @"\SubDir", rootDirectoryItems, subDirectoryItems);

            gatewayMock.Setup(g => g.MoveItemAsync(rootName, new FileId(@"\File.ext"), @"FileMove.ext", new DirectoryId(@"\SubDir"), It.IsAny <Func <FileSystemInfoLocator> >()))
            .ReturnsAsync(moved = new FileInfoContract(original.Id.Value.Replace("File", "FileMove"), original.Name.Replace("File", "FileMove"), original.Created, original.Updated, original.Size, original.Hash))
            .Verifiable();
            compositionFixture.ExportAsyncGateway(gatewayMock.Object);

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

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

            gatewayMock.VerifyAll();
        }
Example #11
0
        public void NewItemAsync_WhereEncryptionKeyIsSpecifiedAndItemTypeIsFile_PassesEncryptedValue()
        {
            var rootName           = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;
            var content            = TestContent.SingleLineTestContent;
            var newItem            = new FileInfoContract(@"\NewFile", "NewFile", DateTime.Now, DateTime.Now, content.Length, FileSystemFixture.GetHash(content));

            var gatewayMock = mockingFixture.InitializeGetChildItemsAsync(rootName, @"\", rootDirectoryItems);

            gatewayMock.Setup(g => g.NewFileItemAsync(rootName, new DirectoryId(@"\"), "NewFile", It.Is <Stream>(s => EncryptionFixture.GetDecryptingReader(s, FileSystemFixture.EncryptionKey).ReadToEnd() == content), It.Is <IProgress <ProgressValue> >(p => true)))
            .ReturnsAsync(newItem)
            .Verifiable();
            compositionFixture.ExportAsyncGateway(gatewayMock.Object);

            var pipelineFixture = new PipelineFixture();

            pipelineFixture.SetVariable("value", content);
            var result = pipelineFixture.Invoke(
                FileSystemFixture.NewDriveCommandWithEncryptionKey,
                @"New-Item -Type File -Path X:\ -Name NewFile -Value $value"
                );

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

            gatewayMock.VerifyAll();
        }
Example #12
0
        public void SetContentAsync_WhereNodeIsFileAndPassThruIsSet_ReturnsContent()
        {
            var rootName           = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;
            var content            = TestContent.MultiLineTestContent.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            var gatewayMock = mockingFixture.InitializeGetChildItemsAsync(rootName, @"\", rootDirectoryItems);

            gatewayMock.Setup(g => g.ClearContentAsync(rootName, new FileId(@"\File.ext"), It.IsAny <Func <FileSystemInfoLocator> >()))
            .ReturnsAsync(true)
            .Verifiable();
            gatewayMock.Setup(g => g.SetContentAsync(rootName, new FileId(@"\File.ext"), It.Is <Stream>(s => new StreamReader(s, System.Text.Encoding.Default, true, 1024, true).ReadToEnd().TrimEnd('\r', '\n') == string.Join("\r\n", content)), It.Is <IProgress <ProgressValue> >(p => true), It.IsAny <Func <FileSystemInfoLocator> >()))
            .ReturnsAsync(true)
            .Verifiable();
            compositionFixture.ExportAsyncGateway(gatewayMock.Object);

            var pipelineFixture = new PipelineFixture();

            pipelineFixture.SetVariable("value", content);
            var result = pipelineFixture.Invoke(
                FileSystemFixture.NewDriveCommand,
                @"Set-Content -Path X:\File.ext $value -PassThru"
                );

            Assert.AreEqual(1, result.Count, "Unexpected number of results");
            Assert.IsInstanceOfType(result[0].BaseObject, typeof(string[]), "Results is not of type string[]");
            CollectionAssert.AreEqual(content, (string[])result[0].BaseObject, "Mismatching content");

            gatewayMock.VerifyAll();
        }
Example #13
0
        public void SetContentAsync_WherePathIsUnknownFile_Throws()
        {
            var rootName           = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;
            var content            = TestContent.MultiLineTestContent.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            var gatewayMock = mockingFixture.InitializeGetChildItemsAsync(rootName, @"\", rootDirectoryItems);

            compositionFixture.ExportAsyncGateway(gatewayMock.Object);

            try {
                var pipelineFixture = new PipelineFixture();
                pipelineFixture.SetVariable("value", content);
                pipelineFixture.Invoke(
                    FileSystemFixture.NewDriveCommand,
                    @"Set-Content -Path X:\FileUnknown $value"
                    );

                throw new InvalidOperationException("Expected Exception was not thrown");
            } catch (AssertFailedException ex) {
                Assert.IsTrue(ex.Message.EndsWith(@"ObjectNotFound: (X:\FileUnknown:String) [Set-Content], ItemNotFoundException", StringComparison.Ordinal));
            }

            gatewayMock.VerifyAll();
        }
Example #14
0
        public void NewDrive_WhereCredentialsAreSpecified_CreatesDrive()
        {
            var gatewayMock = new Mock <ICloudGateway>(MockBehavior.Strict).Object;

            compositionFixture.ExportGateway(gatewayMock);

            var pipelineFixture = new PipelineFixture();

            pipelineFixture.SetVariable("credential", PipelineFixture.GetCredential("TestUser", "TestPassword"));
            var result = pipelineFixture.Invoke(new string[] {
                string.Format(CultureInfo.InvariantCulture,
                              "New-PSDrive -PSProvider {0} -Name Y -Root '{1}' -Description '{2}' -Credential $credential -ApiKey {3} -EncryptionKey {4}",
                              CloudProvider.PROVIDER_NAME, CompositionFixture.MOCKGATEWAY_NAME + "|" + root, description, apiKey, encryptionKey),
                string.Format(CultureInfo.InvariantCulture,
                              "Get-PSDrive -PSProvider {0}", CloudProvider.PROVIDER_NAME)
            });

            Assert.AreEqual(1, result.Count, "Unexpected number of results");
            Assert.IsInstanceOfType(result[0].BaseObject, typeof(PSDriveInfo), "Results is not of type PSDriveInfo");

            var driveInfo = result[0].BaseObject as PSDriveInfo;

            Assert.AreEqual("Y:", driveInfo.Root, "Unexpected root");
            Assert.AreEqual(CompositionFixture.MOCKGATEWAY_NAME + "@TestUser", ((CloudDrive)driveInfo).DisplayRoot, "Unexpected display root");
            Assert.AreEqual(description, driveInfo.Description, "Unexpected description");
        }
        public void GetChildItemAsync_WherePathIsSubDirectoryAndForceIsSpecified_RefreshesResult()
        {
            var rootName                   = FileSystemFixture.GetRootName();
            var rootDirectoryItems         = fileSystemFixture.RootDirectoryItems;
            var subDirectoryItems          = fileSystemFixture.SubDirectoryItems;
            var subDirectoryItemsRefreshed = fileSystemFixture.SubDirectoryItems
                                             .Select(f => f is FileInfoContract
                    ? new FileInfoContract(f.Id.Value.Insert(f.Id.Value.IndexOf(".ext", StringComparison.Ordinal), "Refreshed"), f.Name.Insert(f.Name.IndexOf(".ext", StringComparison.Ordinal), "Refreshed"), f.Created, f.Updated, ((FileInfoContract)f).Size, ((FileInfoContract)f).Hash) as FileSystemInfoContract
                    : new DirectoryInfoContract(f.Id + "Refreshed", f.Name + "Refreshed", f.Created, f.Updated) as FileSystemInfoContract)
                                             .ToArray();

            var gatewayMock = new MockingFixture().InitializeGetChildItemsAsync(rootName, string.Empty, rootDirectoryItems);

            gatewayMock.SetupSequence(g => g.GetChildItemAsync(rootName, new DirectoryId(@"\SubDir")))
            .ReturnsAsync(subDirectoryItems)
            .ReturnsAsync(subDirectoryItemsRefreshed)
            .ThrowsAsync(new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, @"Redundant access to \SubDir")));
            compositionFixture.ExportAsyncGateway(gatewayMock.Object);

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

            Assert.AreEqual(subDirectoryItemsRefreshed.Length, result.Count, "Unexpected number of results");
            CollectionAssert.AreEquivalent(subDirectoryItemsRefreshed, result.Select(p => p.BaseObject).Cast <FileSystemInfoContract>().ToList());
        }
Example #16
0
        public void SetContentAsync_WhereNodeIsFileAndEncodingIsUnknown_AcceptsContent()
        {
            var rootName           = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;
            var content            = TestContent.MultiLineTestContent.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            var gatewayMock = mockingFixture.InitializeGetChildItemsAsync(rootName, @"\", rootDirectoryItems);

            gatewayMock.Setup(g => g.ClearContentAsync(rootName, new FileId(@"\File.ext"), It.IsAny <Func <FileSystemInfoLocator> >()))
            .ReturnsAsync(true)
            .Verifiable();
            gatewayMock.Setup(g => g.SetContentAsync(rootName, new FileId(@"\File.ext"), It.Is <Stream>(s => new StreamReader(s, System.Text.Encoding.Default, true, 1024, true).ReadToEnd().TrimEnd('\r', '\n') == string.Join("\r\n", content)), It.Is <IProgress <ProgressValue> >(p => true), It.IsAny <Func <FileSystemInfoLocator> >()))
            .ReturnsAsync(true)
            .Verifiable();
            compositionFixture.ExportAsyncGateway(gatewayMock.Object);

            var pipelineFixture = new PipelineFixture();

            pipelineFixture.SetVariable("value", content);
            pipelineFixture.Invoke(
                FileSystemFixture.NewDriveCommand,
                @"Set-Content -Path X:\File.ext $value"
                );

            gatewayMock.VerifyAll();
        }
        public void SetContent_WhereNodeIsFileAndEncryptionKeyIsSpecified_EncryptsContent()
        {
            var rootName           = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;
            var content            = TestContent.MultiLineTestContent.Select(c => Convert.ToByte(c)).ToArray();

            var gatewayMock = mockingFixture.InitializeGetChildItems(rootName, @"\", rootDirectoryItems);

            gatewayMock.Setup(g => g.ClearContent(rootName, new FileId(@"\File.ext"))).Verifiable();
            Func <Stream, byte[], bool> matches = (s, c) => {
                var d = s.Decrypt(FileSystemFixture.EncryptionKey);
                return(new BinaryReader(d, System.Text.Encoding.Default, true).ReadBytes((int)d.Length).SequenceEqual(content));
            };

            gatewayMock.Setup(g => g.SetContent(rootName, new FileId(@"\File.ext"), It.Is <Stream>(s => matches(s, content)), It.Is <IProgress <ProgressValue> >(p => true))).Verifiable();
            compositionFixture.ExportGateway(gatewayMock.Object);

            var pipelineFixture = new PipelineFixture();

            pipelineFixture.SetVariable("value", content);
            pipelineFixture.Invoke(
                FileSystemFixture.NewDriveCommandWithEncryptionKey,
                @"Set-Content -Path X:\File.ext $value -Encoding Byte"
                );

            gatewayMock.VerifyAll();
        }
Example #18
0
        public void MoveItem_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.InitializeGetChildItems(rootName, @"\SubDir2", rootDirectoryItems, subDirectory2Items);

            gatewayMock.Setup(g => g.MoveItem(rootName, new DirectoryId(@"\SubDir"), @"SubDirMove", new DirectoryId(@"\SubDir2")))
            .Returns(moved = new DirectoryInfoContract(original.Id.Value.Replace("SubDir", "SubDirMove"), original.Name.Replace("SubDir", "SubDirMove"), original.Created, original.Updated))
            .Verifiable();
            compositionFixture.ExportGateway(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, "Moved item is missing");

            gatewayMock.VerifyAll();
        }
        public void NewDriveAsync_WhereGatewayIsExported_PerformsComposition()
        {
            var gatewayMock = new Mock <IAsyncCloudGateway>(MockBehavior.Strict).Object;

            compositionFixture.ExportAsyncGateway(gatewayMock);

            var pipelineFixture = new PipelineFixture();

            pipelineFixture.Invoke(string.Format(CultureInfo.InvariantCulture,
                                                 "New-PSDrive -PSProvider {0} -Name X -Root '{1}' -Description '{2}' -ApiKey {3} -EncryptionKey {4}",
                                                 CloudProvider.PROVIDER_NAME, CompositionFixture.MOCKGATEWAY_NAME + "|" + root, description, apiKey, encryptionKey)
                                   );
        }
        public void RemoveDrive_RemovesDrive()
        {
            var gatewayMock = new Mock <ICloudGateway>(MockBehavior.Strict).Object;

            compositionFixture.ExportGateway(gatewayMock);

            var result = new PipelineFixture().Invoke(new string[] {
                string.Format(CultureInfo.InvariantCulture,
                              "New-PSDrive -PSProvider {0} -Name X -Root '{1}' -Description '{2}' -ApiKey {3} -EncryptionKey {4}",
                              CloudProvider.PROVIDER_NAME, CompositionFixture.MOCKGATEWAY_NAME + "|" + root, description, apiKey, encryptionKey),
                "Remove-PSDrive -Name X",
                string.Format(CultureInfo.InvariantCulture,
                              "Get-PSDrive -PSProvider {0}", CloudProvider.PROVIDER_NAME)
            });

            Assert.AreEqual(0, result.Count, "Unexpected number of results");
        }
        public void GetChildItemAsync_WherePathIsRoot_CallsGatewayCorrectly()
        {
            var rootName           = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;

            var gatewayMock = mockingFixture.InitializeGetChildItemsAsync(rootName, @"\", rootDirectoryItems);

            compositionFixture.ExportAsyncGateway(gatewayMock.Object);

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

            Assert.AreEqual(rootDirectoryItems.Length, result.Count, "Unexpected number of results");
            CollectionAssert.AreEquivalent(rootDirectoryItems, result.Select(p => p.BaseObject).Cast <FileSystemInfoContract>().ToList());
        }
        public void GetChildItemAsync_WherePathIsRootAndIncludeIsSpecified_CallsGatewayCorrectly()
        {
            var rootName           = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;

            var gatewayMock = mockingFixture.InitializeGetChildItemsAsync(rootName, @"\", rootDirectoryItems);

            compositionFixture.ExportAsyncGateway(gatewayMock.Object);

            var result = new PipelineFixture().Invoke(
                FileSystemFixture.NewDriveCommand,
                @"Get-ChildItem -Path X:\ -Include *File.ext"
                );

            Assert.AreEqual(3, result.Count, "Unexpected number of results");
            CollectionAssert.AreEqual(rootDirectoryItems.Where(i => i.Name.EndsWith("File.ext", StringComparison.Ordinal)).ToList(), result.Select(r => r.BaseObject).ToList(), "Mismatched result");
        }
Example #23
0
        public void ResolvePath_WhereBasePathIsRoot_CallsGatewayCorrectly()
        {
            var rootName           = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;

            var gatewayMock = mockingFixture.InitializeGetChildItems(rootName, @"\", rootDirectoryItems);

            compositionFixture.ExportGateway(gatewayMock.Object);

            var result = new PipelineFixture().Invoke(new string[] {
                FileSystemFixture.NewDriveCommand,
                @"Resolve-Path X:\*"
            });

            Assert.AreEqual(rootDirectoryItems.Length, result.Count, "Unexpected number of results");
            CollectionAssert.AllItemsAreInstancesOfType(result.Select(i => i.BaseObject).ToList(), typeof(PathInfo), "Result is not of type PathInfo");
        }
        public void GetChildItemAsync_WherePathIsRootAndFilterIsSpecified_CallsGatewayCorrectly()
        {
            var rootName           = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;

            var gatewayMock = mockingFixture.InitializeGetChildItemsAsyncWithFilter(rootName, @"\", "*File.ext", rootDirectoryItems);

            compositionFixture.ExportAsyncGateway(gatewayMock.Object);

            var result = new PipelineFixture().Invoke(
                FileSystemFixture.NewDriveCommand,
                @"Get-ChildItem -Path X:\ -Filter *File.ext"
                );

            Assert.AreEqual(rootDirectoryItems.Count(i => i.Name.EndsWith("File.ext", StringComparison.Ordinal)), result.Count, "Unexpected number of results");
            CollectionAssert.AllItemsAreInstancesOfType(result.Select(i => i.BaseObject).ToList(), typeof(FileInfoContract), "Results are not of type FileInfoContract");
            CollectionAssert.AreEqual(rootDirectoryItems.Where(i => i.Name.EndsWith("File.ext", StringComparison.Ordinal)).ToList(), result.Select(i => i.BaseObject).ToList(), "Mismatched result");
        }
Example #25
0
        public void ResolvePath_WhereBasePathIsSubDirectoryAndPatternContainsPrefix_CallsGatewayCorrectly()
        {
            var rootName           = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;
            var subDirectoryItems  = fileSystemFixture.SubDirectoryItems;

            var gatewayMock = mockingFixture.InitializeGetChildItems(rootName, @"\SubDir", rootDirectoryItems, subDirectoryItems);

            compositionFixture.ExportGateway(gatewayMock.Object);

            var result = new PipelineFixture().Invoke(new string[] {
                FileSystemFixture.NewDriveCommand,
                @"Resolve-Path X:\SubDir\Sub*"
            });

            Assert.AreEqual(subDirectoryItems.Count(i => i.Name.StartsWith("Sub", StringComparison.Ordinal)), result.Count, "Unexpected number of results");
            CollectionAssert.AllItemsAreInstancesOfType(result.Select(i => i.BaseObject).ToList(), typeof(PathInfo), "Result is not of type PathInfo");
            CollectionAssert.AreEqual(subDirectoryItems.Where(i => i.Name.StartsWith("Sub", StringComparison.Ordinal)).Select(i => @"X:" + i.Id).ToList(), result.Select(i => ((PathInfo)i.BaseObject).Path).ToList());
        }
Example #26
0
        public void GetContentAsync_WhereNodeIsFileAndEncodingIsByte_ReturnsContent()
        {
            var rootName           = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;
            var content            = new MemoryStream(TestContent.MultiLineTestContent.Select(c => Convert.ToByte(c)).ToArray());

            var gatewayMock = mockingFixture.InitializeGetChildItemsAsync(rootName, @"\", rootDirectoryItems);

            gatewayMock.SetupSequence(g => g.GetContentAsync(rootName, new FileId(@"\File.ext")))
            .ReturnsAsync(content)
            .ThrowsAsync(new InvalidOperationException(@"Redundant access to \File.ext"));
            compositionFixture.ExportAsyncGateway(gatewayMock.Object);

            var result = new PipelineFixture().Invoke(
                FileSystemFixture.NewDriveCommand,
                @"Get-Content -Path X:\File.ext -Encoding Byte"
                );

            CollectionAssert.AreEqual(content.ToArray(), result.Select(p => p.BaseObject).ToArray(), "Mismatching content");
        }
        public void GetChildItemAsync_WherePathIsRootAndCredentialsAreSpecified_CallsGatewayCorrectly()
        {
            var rootName           = FileSystemFixture.GetRootName("@TestUser");
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;

            var gatewayMock = mockingFixture.InitializeGetChildItemsAsync(rootName, @"\", rootDirectoryItems);

            compositionFixture.ExportAsyncGateway(gatewayMock.Object);

            var pipelineFixture = new PipelineFixture();

            pipelineFixture.SetVariable("credential", PipelineFixture.GetCredential("TestUser", "TestPassword"));
            var result = pipelineFixture.Invoke(
                FileSystemFixture.NewDriveCommandWithCredential,
                @"Get-ChildItem -Path Y:\"
                );

            Assert.AreEqual(rootDirectoryItems.Length, result.Count, "Unexpected number of results");
            CollectionAssert.AreEquivalent(rootDirectoryItems, result.Select(p => p.BaseObject).Cast <FileSystemInfoContract>().ToList());
        }
        public void ClearContent_WhereNodeIsFile_ClearsContent()
        {
            var rootName           = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;
            var content            = TestContent.MultiLineTestContent.Select(c => Convert.ToByte(c)).ToArray();

            var gatewayMock = mockingFixture.InitializeGetChildItems(rootName, @"\", rootDirectoryItems);

            gatewayMock.Setup(g => g.ClearContent(rootName, new FileId(@"\File.ext"))).Verifiable();
            compositionFixture.ExportGateway(gatewayMock.Object);

            var pipelineFixture = new PipelineFixture();

            pipelineFixture.SetVariable("value", content);
            pipelineFixture.Invoke(
                FileSystemFixture.NewDriveCommand,
                @"Clear-Content -Path X:\File.ext"
                );

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

            var gatewayMock = mockingFixture.InitializeGetChildItems(rootName, @"\", rootDirectoryItems);

            gatewayMock.Setup(g => g.RemoveItem(rootName, new FileId(@"\File.ext"), false)).Verifiable();
            compositionFixture.ExportGateway(gatewayMock.Object);

            var result = new PipelineFixture().Invoke(
                FileSystemFixture.NewDriveCommand,
                @"Remove-Item -Path X:\File.ext",
                @"Get-ChildItem -Path X:\"
                );

            Assert.AreEqual(rootDirectoryItems.Length - 1, result.Count, "Unexpected number of results");
            CollectionAssert.AreEquivalent(rootDirectoryItems.Where(f => f.Name != "File.ext").ToList(), result.Select(p => p.BaseObject).Cast <FileSystemInfoContract>().ToList());

            gatewayMock.VerifyAll();
        }
Example #30
0
        public void GetContentAsync_WhereNodeIsFileAndEncodingIsByteAndReadCountIsZero_ReturnsContent()
        {
            var rootName           = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;
            var content            = new MemoryStream(TestContent.MultiLineTestContent.Select(c => Convert.ToByte(c)).ToArray());

            var gatewayMock = mockingFixture.InitializeGetChildItemsAsync(rootName, @"\", rootDirectoryItems);

            gatewayMock.SetupSequence(g => g.GetContentAsync(rootName, new FileId(@"\File.ext")))
            .ReturnsAsync(content)
            .ThrowsAsync(new InvalidOperationException(@"Redundant access to \File.ext"));
            compositionFixture.ExportAsyncGateway(gatewayMock.Object);

            var result = new PipelineFixture().Invoke(
                FileSystemFixture.NewDriveCommand,
                @"Get-Content -Path X:\File.ext -Encoding Byte -ReadCount 0"
                );

            Assert.AreEqual(1, result.Count, "Unexpected number of results");
            Assert.IsInstanceOfType(result[0].BaseObject, typeof(byte[]), "Results is not of type byte[]");
            CollectionAssert.AreEqual(content.ToArray(), result[0].BaseObject as byte[], "Mismatching content");
        }