|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using FluentAssertions; |
| 5 | +using Moq; |
| 6 | +using OctoConfig.Core.Octopus; |
| 7 | +using Octopus.Client; |
| 8 | +using Octopus.Client.Model; |
| 9 | +using Octopus.Client.Repositories.Async; |
| 10 | +using Xunit; |
| 11 | +using Xunit.Extensions; |
| 12 | + |
| 13 | +namespace OctoConfig.Tests |
| 14 | +{ |
| 15 | + public static class IOctopusAsyncRepositoryExtensionsTests |
| 16 | + { |
| 17 | + public class ValidateRolesTests |
| 18 | + { |
| 19 | + [Theory] |
| 20 | + [MemberData(nameof(FoundData))] |
| 21 | + public async Task FoundRoleShouldNotThrow(List<string> server, List<string> passedIn) |
| 22 | + { |
| 23 | + var machineMock = new Mock<IMachineRoleRepository>(); |
| 24 | + machineMock.Setup(m => m.GetAllRoleNames()).Returns(Task.FromResult(server)); |
| 25 | + var mock = new Mock<IOctopusAsyncRepository>(); |
| 26 | + mock.Setup(or => or.MachineRoles).Returns(machineMock.Object); |
| 27 | + await mock.Object.ValidateRoles(passedIn).ConfigureAwait(false); |
| 28 | + } |
| 29 | + |
| 30 | + [Theory] |
| 31 | + [MemberData(nameof(MissingData))] |
| 32 | + public void MissingRoleShouldNotThrow(List<string> server, List<string> passedIn, string missing) |
| 33 | + { |
| 34 | + var machineMock = new Mock<IMachineRoleRepository>(); |
| 35 | + machineMock.Setup(m => m.GetAllRoleNames()).Returns(Task.FromResult(server)); |
| 36 | + var mock = new Mock<IOctopusAsyncRepository>(); |
| 37 | + mock.Setup(or => or.MachineRoles).Returns(machineMock.Object); |
| 38 | + Func<Task> test = () => mock.Object.ValidateRoles(passedIn); |
| 39 | + test.Should().Throw<ArgumentException>().WithMessage($"Unable to find a machine role with the name '{missing}'"); |
| 40 | + } |
| 41 | + |
| 42 | + public static IEnumerable<object[]> FoundData => new[] |
| 43 | + { |
| 44 | + new object[] { new List<string>() { "api" }, new List<string>() { "api" } }, |
| 45 | + new object[] { new List<string>() { "api", "web" }, new List<string>() { "api", "web" } }, |
| 46 | + new object[] { new List<string>() { "web", "redis", "api" }, new List<string>() { "redis" } } |
| 47 | + }; |
| 48 | + |
| 49 | + public static IEnumerable<object[]> MissingData => new[] |
| 50 | + { |
| 51 | + new object[] { new List<string>() { "web" }, new List<string>() { "api" }, "api" }, |
| 52 | + new object[] { new List<string>(), new List<string>() { "api" }, "api" }, |
| 53 | + new object[] { new List<string>() { "web", "api" }, new List<string>() { "redis" }, "redis" } |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + public class ValidateEnvironmentTests |
| 58 | + { |
| 59 | + [Theory] |
| 60 | + [InlineData("QA")] |
| 61 | + public async Task FoundEnvironmentShouldNotThrow(string environment) |
| 62 | + { |
| 63 | + var environmentMock = new Mock<IEnvironmentRepository>(); |
| 64 | + environmentMock.Setup(e => e.FindByName(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<object>())).Returns(Task.FromResult(new EnvironmentResource())); |
| 65 | + var mock = new Mock<IOctopusAsyncRepository>(); |
| 66 | + mock.Setup(or => or.Environments).Returns(environmentMock.Object); |
| 67 | + await mock.Object.ValidateEnvironment(environment).ConfigureAwait(false); |
| 68 | + } |
| 69 | + |
| 70 | + [Theory] |
| 71 | + [InlineData("QA")] |
| 72 | + public void MissingEnvironmentShouldNotThrow(string environment) |
| 73 | + { |
| 74 | + var environmentMock = new Mock<IEnvironmentRepository>(); |
| 75 | + environmentMock.Setup(e => e.FindByName(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<object>())).Returns(Task.FromResult<EnvironmentResource>(null)); |
| 76 | + var mock = new Mock<IOctopusAsyncRepository>(); |
| 77 | + mock.Setup(or => or.Environments).Returns(environmentMock.Object); |
| 78 | + Func<Task> test = () => mock.Object.ValidateEnvironment(environment); |
| 79 | + test.Should().Throw<ArgumentException>().WithMessage($"Unable to find an environment with the name '{environment}'"); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public class ValidateLibraryTests |
| 84 | + { |
| 85 | + [Theory] |
| 86 | + [InlineData("Shared")] |
| 87 | + public async Task FoundLibraryShouldNotThrow(string library) |
| 88 | + { |
| 89 | + var libraryMock = new Mock<ILibraryVariableSetRepository>(); |
| 90 | + libraryMock.Setup(e => e.FindByName(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<object>())).Returns(Task.FromResult(new LibraryVariableSetResource())); |
| 91 | + var mock = new Mock<IOctopusAsyncRepository>(); |
| 92 | + mock.Setup(or => or.LibraryVariableSets).Returns(libraryMock.Object); |
| 93 | + await mock.Object.ValidateLibrary(library).ConfigureAwait(false); |
| 94 | + } |
| 95 | + |
| 96 | + [Theory] |
| 97 | + [InlineData("QA")] |
| 98 | + public void MissingLibraryShouldNotThrow(string library) |
| 99 | + { |
| 100 | + var libraryMock = new Mock<ILibraryVariableSetRepository>(); |
| 101 | + libraryMock.Setup(e => e.FindByName(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<object>())).Returns(Task.FromResult<LibraryVariableSetResource>(null)); |
| 102 | + var mock = new Mock<IOctopusAsyncRepository>(); |
| 103 | + mock.Setup(or => or.LibraryVariableSets).Returns(libraryMock.Object); |
| 104 | + Func<Task> test = () => mock.Object.ValidateLibrary(library); |
| 105 | + test.Should().Throw<ArgumentException>().WithMessage($"Unable to find a library with the name '{library}'"); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public class ValidateProjectTests |
| 110 | + { |
| 111 | + [Theory] |
| 112 | + [InlineData("QA")] |
| 113 | + public async Task FoundProjectShouldNotThrow(string project) |
| 114 | + { |
| 115 | + var projectMock = new Mock<IProjectRepository>(); |
| 116 | + projectMock.Setup(e => e.FindByName(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<object>())).Returns(Task.FromResult(new ProjectResource())); |
| 117 | + var mock = new Mock<IOctopusAsyncRepository>(); |
| 118 | + mock.Setup(or => or.Projects).Returns(projectMock.Object); |
| 119 | + await mock.Object.ValidateProject(project).ConfigureAwait(false); |
| 120 | + } |
| 121 | + |
| 122 | + [Theory] |
| 123 | + [InlineData("QA")] |
| 124 | + public void MissingProjectShouldNotThrow(string project) |
| 125 | + { |
| 126 | + var projectMock = new Mock<IProjectRepository>(); |
| 127 | + projectMock.Setup(e => e.FindByName(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<object>())).Returns(Task.FromResult<ProjectResource>(null)); |
| 128 | + var mock = new Mock<IOctopusAsyncRepository>(); |
| 129 | + mock.Setup(or => or.Projects).Returns(projectMock.Object); |
| 130 | + Func<Task> test = () => mock.Object.ValidateProject(project); |
| 131 | + test.Should().Throw<ArgumentException>().WithMessage($"Unable to find a project with the name '{project}'"); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + public class ValidateTenantTests |
| 136 | + { |
| 137 | + [Theory] |
| 138 | + [InlineData("QA")] |
| 139 | + public async Task FoundRoleShouldNotThrow(string tenant) |
| 140 | + { |
| 141 | + var tenantMock = new Mock<ITenantRepository>(); |
| 142 | + tenantMock.Setup(e => e.FindByName(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<object>())).Returns(Task.FromResult(new TenantResource())); |
| 143 | + var mock = new Mock<IOctopusAsyncRepository>(); |
| 144 | + mock.Setup(or => or.Tenants).Returns(tenantMock.Object); |
| 145 | + await mock.Object.ValidateTenant(tenant).ConfigureAwait(false); |
| 146 | + } |
| 147 | + |
| 148 | + [Theory] |
| 149 | + [InlineData("QA")] |
| 150 | + public void MissingRoleShouldNotThrow(string tenant) |
| 151 | + { |
| 152 | + var tenantMock = new Mock<ITenantRepository>(); |
| 153 | + tenantMock.Setup(e => e.FindByName(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<object>())).Returns(Task.FromResult<TenantResource>(null)); |
| 154 | + var mock = new Mock<IOctopusAsyncRepository>(); |
| 155 | + mock.Setup(or => or.Tenants).Returns(tenantMock.Object); |
| 156 | + Func<Task> test = () => mock.Object.ValidateTenant(tenant); |
| 157 | + test.Should().Throw<ArgumentException>().WithMessage($"Unable to find a tenant with the name '{tenant}'"); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments