|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using APIViewWeb; |
| 5 | +using APIViewWeb.DTOs; |
| 6 | +using APIViewWeb.Helpers; |
| 7 | +using APIViewWeb.LeanControllers; |
| 8 | +using APIViewWeb.Managers; |
| 9 | +using APIViewWeb.Models; |
| 10 | +using FluentAssertions; |
| 11 | +using Microsoft.AspNetCore.Http; |
| 12 | +using Microsoft.AspNetCore.Mvc; |
| 13 | +using Microsoft.Extensions.Configuration; |
| 14 | +using Microsoft.Extensions.Logging; |
| 15 | +using Moq; |
| 16 | +using Xunit; |
| 17 | + |
| 18 | +namespace APIViewUnitTests |
| 19 | +{ |
| 20 | + public class PullRequestsControllerTests |
| 21 | + { |
| 22 | + private readonly Mock<ILogger<PullRequestsController>> _mockLogger; |
| 23 | + private readonly Mock<IPullRequestManager> _mockPullRequestManager; |
| 24 | + private readonly Mock<IConfiguration> _mockConfiguration; |
| 25 | + private readonly List<LanguageService> _languageServices; |
| 26 | + private readonly PullRequestsController _controller; |
| 27 | + |
| 28 | + public PullRequestsControllerTests() |
| 29 | + { |
| 30 | + _mockLogger = new Mock<ILogger<PullRequestsController>>(); |
| 31 | + _mockPullRequestManager = new Mock<IPullRequestManager>(); |
| 32 | + _mockConfiguration = new Mock<IConfiguration>(); |
| 33 | + _languageServices = new List<LanguageService>(); |
| 34 | + |
| 35 | + _controller = new PullRequestsController( |
| 36 | + _mockLogger.Object, |
| 37 | + _mockPullRequestManager.Object, |
| 38 | + _mockConfiguration.Object, |
| 39 | + _languageServices); |
| 40 | + } |
| 41 | + |
| 42 | + [Theory] |
| 43 | + [InlineData("client")] |
| 44 | + [InlineData("mgmt")] |
| 45 | + [InlineData("CLIENT")] |
| 46 | + [InlineData("MGMT")] |
| 47 | + public async Task CreateAPIRevisionIfAPIHasChanges_WithValidPackageType_PassesCorrectValueToManager(string packageTypeValue) |
| 48 | + { |
| 49 | + // Arrange |
| 50 | + _mockPullRequestManager.Setup(m => m.CreateAPIRevisionIfAPIHasChanges( |
| 51 | + It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), |
| 52 | + It.IsAny<string>(), It.IsAny<int>(), It.IsAny<string>(), It.IsAny<CreateAPIRevisionAPIResponse>(), |
| 53 | + It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())) |
| 54 | + .ReturnsAsync("https://test.com/review/test-id"); |
| 55 | + |
| 56 | + // Setup HTTP context for Request.Host |
| 57 | + var httpContext = new DefaultHttpContext(); |
| 58 | + httpContext.Request.Host = new HostString("test.com"); |
| 59 | + _controller.ControllerContext = new ControllerContext() |
| 60 | + { |
| 61 | + HttpContext = httpContext |
| 62 | + }; |
| 63 | + |
| 64 | + // Act |
| 65 | + var result = await _controller.CreateAPIRevisionIfAPIHasChanges( |
| 66 | + buildId: "test-build-id", |
| 67 | + artifactName: "test-artifact", |
| 68 | + filePath: "test/path", |
| 69 | + commitSha: "abc123", |
| 70 | + repoName: "test-repo", |
| 71 | + packageName: "test-package", |
| 72 | + pullRequestNumber: 123, |
| 73 | + packageType: packageTypeValue); |
| 74 | + |
| 75 | + // Assert |
| 76 | + result.Should().NotBeNull(); |
| 77 | + |
| 78 | + // Verify that the manager was called with the exact packageType value passed from controller |
| 79 | + _mockPullRequestManager.Verify(m => m.CreateAPIRevisionIfAPIHasChanges( |
| 80 | + "test-build-id", |
| 81 | + "test-artifact", |
| 82 | + "test/path", |
| 83 | + "abc123", |
| 84 | + "test-repo", |
| 85 | + "test-package", |
| 86 | + 123, |
| 87 | + "test.com", |
| 88 | + It.IsAny<CreateAPIRevisionAPIResponse>(), |
| 89 | + null, // codeFile |
| 90 | + null, // baselineCodeFile |
| 91 | + null, // language - actual value from controller |
| 92 | + "internal", // default project |
| 93 | + packageTypeValue), // packageType should be passed exactly as received |
| 94 | + Times.Once); |
| 95 | + } |
| 96 | + |
| 97 | + [Theory] |
| 98 | + [InlineData(null)] |
| 99 | + [InlineData("")] |
| 100 | + [InlineData(" ")] |
| 101 | + [InlineData("invalid")] |
| 102 | + [InlineData("unknown")] |
| 103 | + public async Task CreateAPIRevisionIfAPIHasChanges_WithInvalidPackageType_PassesValueToManager(string packageTypeValue) |
| 104 | + { |
| 105 | + // Arrange |
| 106 | + _mockPullRequestManager.Setup(m => m.CreateAPIRevisionIfAPIHasChanges( |
| 107 | + It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), |
| 108 | + It.IsAny<string>(), It.IsAny<int>(), It.IsAny<string>(), It.IsAny<CreateAPIRevisionAPIResponse>(), |
| 109 | + It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())) |
| 110 | + .ReturnsAsync("https://test.com/review/test-id"); |
| 111 | + |
| 112 | + // Setup HTTP context for Request.Host |
| 113 | + var httpContext = new DefaultHttpContext(); |
| 114 | + httpContext.Request.Host = new HostString("test.com"); |
| 115 | + _controller.ControllerContext = new ControllerContext() |
| 116 | + { |
| 117 | + HttpContext = httpContext |
| 118 | + }; |
| 119 | + |
| 120 | + // Act |
| 121 | + var result = await _controller.CreateAPIRevisionIfAPIHasChanges( |
| 122 | + buildId: "test-build-id", |
| 123 | + artifactName: "test-artifact", |
| 124 | + filePath: "test/path", |
| 125 | + commitSha: "abc123", |
| 126 | + repoName: "test-repo", |
| 127 | + packageName: "test-package", |
| 128 | + pullRequestNumber: 123, |
| 129 | + packageType: packageTypeValue); |
| 130 | + |
| 131 | + // Assert |
| 132 | + result.Should().NotBeNull(); |
| 133 | + |
| 134 | + // Verify that the manager was called with the exact packageType value (even if invalid) |
| 135 | + _mockPullRequestManager.Verify(m => m.CreateAPIRevisionIfAPIHasChanges( |
| 136 | + "test-build-id", |
| 137 | + "test-artifact", |
| 138 | + "test/path", |
| 139 | + "abc123", |
| 140 | + "test-repo", |
| 141 | + "test-package", |
| 142 | + 123, |
| 143 | + "test.com", |
| 144 | + It.IsAny<CreateAPIRevisionAPIResponse>(), |
| 145 | + null, // codeFile |
| 146 | + null, // baselineCodeFile |
| 147 | + null, // language - actual value from controller |
| 148 | + "internal", // default project |
| 149 | + packageTypeValue), // packageType should be passed exactly as received (even invalid values) |
| 150 | + Times.Once); |
| 151 | + } |
| 152 | + |
| 153 | + [Fact] |
| 154 | + public async Task CreateAPIRevisionIfAPIHasChanges_WhenPackageTypeOmitted_PassesNullToManager() |
| 155 | + { |
| 156 | + // Arrange |
| 157 | + _mockPullRequestManager.Setup(m => m.CreateAPIRevisionIfAPIHasChanges( |
| 158 | + It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), |
| 159 | + It.IsAny<string>(), It.IsAny<int>(), It.IsAny<string>(), It.IsAny<CreateAPIRevisionAPIResponse>(), |
| 160 | + It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())) |
| 161 | + .ReturnsAsync("https://test.com/review/test-id"); |
| 162 | + |
| 163 | + // Setup HTTP context for Request.Host |
| 164 | + var httpContext = new DefaultHttpContext(); |
| 165 | + httpContext.Request.Host = new HostString("test.com"); |
| 166 | + _controller.ControllerContext = new ControllerContext() |
| 167 | + { |
| 168 | + HttpContext = httpContext |
| 169 | + }; |
| 170 | + |
| 171 | + // Act - Not providing packageType parameter to test default behavior |
| 172 | + var result = await _controller.CreateAPIRevisionIfAPIHasChanges( |
| 173 | + buildId: "test-build-id", |
| 174 | + artifactName: "test-artifact", |
| 175 | + filePath: "test/path", |
| 176 | + commitSha: "abc123", |
| 177 | + repoName: "test-repo", |
| 178 | + packageName: "test-package", |
| 179 | + pullRequestNumber: 123); |
| 180 | + // packageType parameter omitted |
| 181 | + |
| 182 | + // Assert |
| 183 | + result.Should().NotBeNull(); |
| 184 | + |
| 185 | + // Verify that the manager was called with null packageType when omitted |
| 186 | + _mockPullRequestManager.Verify(m => m.CreateAPIRevisionIfAPIHasChanges( |
| 187 | + "test-build-id", |
| 188 | + "test-artifact", |
| 189 | + "test/path", |
| 190 | + "abc123", |
| 191 | + "test-repo", |
| 192 | + "test-package", |
| 193 | + 123, |
| 194 | + "test.com", |
| 195 | + It.IsAny<CreateAPIRevisionAPIResponse>(), |
| 196 | + null, // codeFile |
| 197 | + null, // baselineCodeFile |
| 198 | + null, // language - actual value from controller |
| 199 | + "internal", // default project |
| 200 | + null), // packageType should be null when omitted |
| 201 | + Times.Once); |
| 202 | + } |
| 203 | + |
| 204 | + [Fact] |
| 205 | + public async Task CreateAPIRevisionIfAPIHasChanges_WhenNoAPIRevisionUrlReturned_ReturnsAlreadyReported() |
| 206 | + { |
| 207 | + // Arrange - Manager returns null/empty URL indicating no changes |
| 208 | + _mockPullRequestManager.Setup(m => m.CreateAPIRevisionIfAPIHasChanges( |
| 209 | + It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), |
| 210 | + It.IsAny<string>(), It.IsAny<int>(), It.IsAny<string>(), It.IsAny<CreateAPIRevisionAPIResponse>(), |
| 211 | + It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())) |
| 212 | + .ReturnsAsync((string)null); // No API revision URL returned |
| 213 | + |
| 214 | + // Setup HTTP context for Request.Host |
| 215 | + var httpContext = new DefaultHttpContext(); |
| 216 | + httpContext.Request.Host = new HostString("test.com"); |
| 217 | + _controller.ControllerContext = new ControllerContext() |
| 218 | + { |
| 219 | + HttpContext = httpContext |
| 220 | + }; |
| 221 | + |
| 222 | + // Act |
| 223 | + var result = await _controller.CreateAPIRevisionIfAPIHasChanges( |
| 224 | + buildId: "test-build-id", |
| 225 | + artifactName: "test-artifact", |
| 226 | + filePath: "test/path", |
| 227 | + commitSha: "abc123", |
| 228 | + repoName: "test-repo", |
| 229 | + packageName: "test-package", |
| 230 | + pullRequestNumber: 123, |
| 231 | + packageType: "client"); |
| 232 | + |
| 233 | + // Assert |
| 234 | + result.Should().NotBeNull(); |
| 235 | + } |
| 236 | + |
| 237 | + [Fact] |
| 238 | + public async Task GetAssociatedPullRequestsAsync_ReturnsExpectedResult() |
| 239 | + { |
| 240 | + // Arrange |
| 241 | + var reviewId = "test-review-id"; |
| 242 | + var apiRevisionId = "test-revision-id"; |
| 243 | + var expectedPullRequests = new List<PullRequestModel> |
| 244 | + { |
| 245 | + new PullRequestModel { ReviewId = reviewId, PullRequestNumber = 123 }, |
| 246 | + new PullRequestModel { ReviewId = reviewId, PullRequestNumber = 456 } |
| 247 | + }; |
| 248 | + |
| 249 | + _mockPullRequestManager.Setup(m => m.GetPullRequestsModelAsync(reviewId, apiRevisionId)) |
| 250 | + .ReturnsAsync(expectedPullRequests); |
| 251 | + |
| 252 | + // Act |
| 253 | + var result = await _controller.GetAssociatedPullRequestsAsync(reviewId, apiRevisionId); |
| 254 | + |
| 255 | + // Assert |
| 256 | + result.Should().NotBeNull(); |
| 257 | + |
| 258 | + _mockPullRequestManager.Verify(m => m.GetPullRequestsModelAsync(reviewId, apiRevisionId), Times.Once); |
| 259 | + } |
| 260 | + } |
| 261 | +} |
0 commit comments