|
| 1 | +//----------------------------------------------------------------------- |
| 2 | +// <copyright company="nBuildKit"> |
| 3 | +// Copyright (c) nBuildKit. All rights reserved. |
| 4 | +// Licensed under the Apache License, Version 2.0 license. See LICENCE.md file in the project root for full license information. |
| 5 | +// </copyright> |
| 6 | +//----------------------------------------------------------------------- |
| 7 | + |
| 8 | +using System; |
| 9 | +using System.Diagnostics.CodeAnalysis; |
| 10 | +using System.IO; |
| 11 | +using System.Reflection; |
| 12 | +using Microsoft.Build.Framework; |
| 13 | +using Microsoft.Build.Utilities; |
| 14 | +using Moq; |
| 15 | +using NBuildKit.MsBuild.Tasks.Tests; |
| 16 | +using Nuclei; |
| 17 | +using NUnit.Framework; |
| 18 | + |
| 19 | +namespace NBuildKit.MsBuild.Tasks.Web |
| 20 | +{ |
| 21 | + [TestFixture] |
| 22 | + [SuppressMessage( |
| 23 | + "Microsoft.StyleCop.CSharp.DocumentationRules", |
| 24 | + "SA1600:ElementsMustBeDocumented", |
| 25 | + Justification = "Unit tests do not need documentation.")] |
| 26 | + public sealed class WebDeleteTest : TaskTest |
| 27 | + { |
| 28 | + [Test] |
| 29 | + public void Execute() |
| 30 | + { |
| 31 | + var fileToUpload = Assembly.GetExecutingAssembly().LocalFilePath(); |
| 32 | + |
| 33 | + var targetUri = "http://www.example.com/mypath"; |
| 34 | + |
| 35 | + var webClient = new Mock<IInternalWebClient>(); |
| 36 | + { |
| 37 | + webClient.Setup(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>())) |
| 38 | + .Callback<Uri, string, string>( |
| 39 | + (uri, method, path) => |
| 40 | + { |
| 41 | + Assert.AreEqual(new Uri(targetUri + "/" + Path.GetFileName(path)), uri); |
| 42 | + Assert.AreEqual(fileToUpload, path); |
| 43 | + }) |
| 44 | + .Returns(new byte[0]) |
| 45 | + .Verifiable(); |
| 46 | + } |
| 47 | + |
| 48 | + Func<IInternalWebClient> builder = () => webClient.Object; |
| 49 | + |
| 50 | + InitializeBuildEngine(); |
| 51 | + |
| 52 | + var task = new WebDelete(builder); |
| 53 | + task.BuildEngine = BuildEngine.Object; |
| 54 | + task.Items = new ITaskItem[] { new TaskItem(fileToUpload) }; |
| 55 | + task.Source = new TaskItem(targetUri); |
| 56 | + task.UseDefaultCredentials = false; |
| 57 | + |
| 58 | + var result = task.Execute(); |
| 59 | + Assert.IsTrue(result, "Expected the task to finish successfully"); |
| 60 | + |
| 61 | + webClient.Verify(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once()); |
| 62 | + |
| 63 | + VerifyNumberOfLogMessages(numberOfErrorMessages: 0, numberOfWarningMessages: 0, numberOfNormalMessages: 3); |
| 64 | + } |
| 65 | + |
| 66 | + [Test] |
| 67 | + public void ExecuteWithEmptyUrl() |
| 68 | + { |
| 69 | + var fileToUpload = Assembly.GetExecutingAssembly().LocalFilePath(); |
| 70 | + |
| 71 | + var webClient = new Mock<IInternalWebClient>(); |
| 72 | + { |
| 73 | + webClient.Setup(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>())) |
| 74 | + .Verifiable(); |
| 75 | + } |
| 76 | + |
| 77 | + Func<IInternalWebClient> builder = () => webClient.Object; |
| 78 | + |
| 79 | + InitializeBuildEngine(); |
| 80 | + |
| 81 | + var task = new WebDelete(builder); |
| 82 | + task.BuildEngine = BuildEngine.Object; |
| 83 | + task.Items = new ITaskItem[] { new TaskItem(fileToUpload) }; |
| 84 | + task.Source = new TaskItem(string.Empty); |
| 85 | + task.UseDefaultCredentials = false; |
| 86 | + |
| 87 | + var result = task.Execute(); |
| 88 | + Assert.IsFalse(result, "Expected the task to not finish successfully"); |
| 89 | + |
| 90 | + webClient.Verify(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never()); |
| 91 | + |
| 92 | + VerifyNumberOfLogMessages(numberOfErrorMessages: 1, numberOfWarningMessages: 0, numberOfNormalMessages: 0); |
| 93 | + } |
| 94 | + |
| 95 | + [Test] |
| 96 | + public void ExecuteWithInvalidUrl() |
| 97 | + { |
| 98 | + var fileToUpload = Assembly.GetExecutingAssembly().LocalFilePath(); |
| 99 | + |
| 100 | + var webClient = new Mock<IInternalWebClient>(); |
| 101 | + { |
| 102 | + webClient.Setup(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>())) |
| 103 | + .Verifiable(); |
| 104 | + } |
| 105 | + |
| 106 | + Func<IInternalWebClient> builder = () => webClient.Object; |
| 107 | + |
| 108 | + InitializeBuildEngine(); |
| 109 | + |
| 110 | + var task = new WebDelete(builder); |
| 111 | + task.BuildEngine = BuildEngine.Object; |
| 112 | + task.Items = new ITaskItem[] { new TaskItem(fileToUpload) }; |
| 113 | + task.Source = new TaskItem("this is not a valid URL"); |
| 114 | + task.UseDefaultCredentials = false; |
| 115 | + |
| 116 | + var result = task.Execute(); |
| 117 | + Assert.IsFalse(result, "Expected the task to not finish successfully"); |
| 118 | + |
| 119 | + webClient.Verify(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never()); |
| 120 | + |
| 121 | + VerifyNumberOfLogMessages(numberOfErrorMessages: 1, numberOfWarningMessages: 0, numberOfNormalMessages: 0); |
| 122 | + } |
| 123 | + |
| 124 | + [Test] |
| 125 | + public void ExecuteWithoutItems() |
| 126 | + { |
| 127 | + var webClient = new Mock<IInternalWebClient>(); |
| 128 | + { |
| 129 | + webClient.Setup(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>())) |
| 130 | + .Verifiable(); |
| 131 | + } |
| 132 | + |
| 133 | + Func<IInternalWebClient> builder = () => webClient.Object; |
| 134 | + |
| 135 | + InitializeBuildEngine(); |
| 136 | + |
| 137 | + var task = new WebDelete(builder); |
| 138 | + task.BuildEngine = BuildEngine.Object; |
| 139 | + task.Items = new ITaskItem[0]; |
| 140 | + task.Source = new TaskItem("http://www.microsoft.com/default.aspx"); |
| 141 | + task.UseDefaultCredentials = false; |
| 142 | + |
| 143 | + var result = task.Execute(); |
| 144 | + Assert.IsFalse(result, "Expected the task to not finish successfully"); |
| 145 | + |
| 146 | + webClient.Verify(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never()); |
| 147 | + |
| 148 | + VerifyNumberOfLogMessages(numberOfErrorMessages: 1, numberOfWarningMessages: 0, numberOfNormalMessages: 0); |
| 149 | + } |
| 150 | + |
| 151 | + [Test] |
| 152 | + public void ExecuteWithoutUrl() |
| 153 | + { |
| 154 | + var fileToUpload = Assembly.GetExecutingAssembly().LocalFilePath(); |
| 155 | + |
| 156 | + var webClient = new Mock<IInternalWebClient>(); |
| 157 | + { |
| 158 | + webClient.Setup(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>())) |
| 159 | + .Verifiable(); |
| 160 | + } |
| 161 | + |
| 162 | + Func<IInternalWebClient> builder = () => webClient.Object; |
| 163 | + |
| 164 | + InitializeBuildEngine(); |
| 165 | + |
| 166 | + var task = new WebDelete(builder); |
| 167 | + task.BuildEngine = BuildEngine.Object; |
| 168 | + task.Items = new ITaskItem[] { new TaskItem(fileToUpload) }; |
| 169 | + task.UseDefaultCredentials = false; |
| 170 | + |
| 171 | + var result = task.Execute(); |
| 172 | + Assert.IsFalse(result, "Expected the task to not finish successfully"); |
| 173 | + |
| 174 | + webClient.Verify(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never()); |
| 175 | + |
| 176 | + VerifyNumberOfLogMessages(numberOfErrorMessages: 1, numberOfWarningMessages: 0, numberOfNormalMessages: 0); |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments