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 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 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 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();
        }
        public void SetContentAsync_WhereNodeIsDirectory_Throws()
        {
            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.SetContentAsync(rootName, new FileId(@"\SubDir"), It.Is<Stream>(s => new BinaryReader(s, System.Text.Encoding.Default, true).ReadBytes((int)s.Length).SequenceEqual(content)), null, It.IsAny<Func<FileSystemInfoLocator>>()))
                .ThrowsAsync(new NotSupportedException(@"Access to path \SubDir is denied"));
            compositionFixture.ExportAsyncGateway(gatewayMock.Object);

            try {
                var pipelineFixture = new PipelineFixture();
                pipelineFixture.SetVariable("value", content);
                pipelineFixture.Invoke(
                    FileSystemFixture.NewDriveCommand,
                    @"Set-Content -Path X:\SubDir $value"
                );
            } catch (CmdletInvocationException ex) {
                throw ex.InnerException;
            }
        }
        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();
        }
        public void GetChildItem_WherePathIsRootAndCredentialsAreSpecified_CallsGatewayCorrectly()
        {
            var rootName = FileSystemFixture.GetRootName("@TestUser");
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;

            var gatewayMock = mockingFixture.InitializeGetChildItems(rootName, @"\", rootDirectoryItems);
            compositionFixture.ExportGateway(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 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 NewItem_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.InitializeGetChildItems(rootName, @"\", rootDirectoryItems);
            gatewayMock.Setup(g => g.NewFileItem(rootName, new DirectoryId(@"\"), "NewFile", It.Is<Stream>(s => EncryptionFixture.GetDecryptingReader(s, FileSystemFixture.EncryptionKey).ReadToEnd() == content), It.Is<IProgress<ProgressValue>>(p => true)))
                .Returns(newItem)
                .Verifiable();
            compositionFixture.ExportGateway(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();
        }
        public void NewItem_WhereItemIsAlreadyPresent_Throws()
        {
            var rootName = FileSystemFixture.GetRootName();
            var rootDirectoryItems = fileSystemFixture.RootDirectoryItems;
            var content = TestContent.SingleLineTestContent;

            var gatewayMock = mockingFixture.InitializeGetChildItems(rootName, @"\", rootDirectoryItems);
            compositionFixture.ExportGateway(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();
        }
        public void ClearContentAsync_WhereNodeIsDirectory_Throws()
        {
            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(@"\SubDir"), It.IsAny<Func<FileSystemInfoLocator>>()))
                .ThrowsAsync(new NotSupportedException(@"Access to path \SubDir is denied"));
            compositionFixture.ExportAsyncGateway(gatewayMock.Object);

            try {
                var pipelineFixture = new PipelineFixture();
                pipelineFixture.SetVariable("value", content);
                pipelineFixture.Invoke(
                    FileSystemFixture.NewDriveCommand,
                    @"Clear-Content -Path X:\SubDir"
                );
            } catch (CmdletInvocationException ex) {
                throw ex.InnerException;
            }
        }