|
6 | 6 |
|
7 | 7 |
|
8 | 8 | using LCT.APICommunications.Model;
|
| 9 | +using Moq; |
| 10 | +using Moq.Protected; |
| 11 | +using System.Net; |
| 12 | +using System.Net.Http; |
| 13 | +using System.Threading; |
9 | 14 |
|
10 | 15 | namespace LCT.APICommunications.UTest
|
11 | 16 | {
|
12 | 17 | [TestFixture]
|
13 | 18 | public class DebainJfrogAPICommunicationUTest
|
14 | 19 | {
|
| 20 | + private Mock<HttpMessageHandler> _mockHandler; |
| 21 | + private HttpClient _httpClient; |
| 22 | + private DebianJfrogAPICommunication _debianJfrogAPICommunication; |
| 23 | + private ArtifactoryCredentials _credentials; |
| 24 | + private string _repoDomainName; |
| 25 | + private string _srcrepoName; |
| 26 | + private int _timeout; |
15 | 27 | [SetUp]
|
16 | 28 | public void Setup()
|
17 | 29 | {
|
18 |
| - // Method intentionally left empty. |
| 30 | + // Setup the necessary parameters for creating an instance of DebianJfrogAPICommunication |
| 31 | + _repoDomainName = "https://example.jfrog.io"; |
| 32 | + _srcrepoName = "my-repo"; |
| 33 | + _credentials = new ArtifactoryCredentials { Token = "sample-token" }; |
| 34 | + _timeout = 30; |
| 35 | + |
| 36 | + // Mock the HttpMessageHandler to mock the HTTP calls |
| 37 | + _mockHandler = new Mock<HttpMessageHandler>(); |
| 38 | + _httpClient = new HttpClient(_mockHandler.Object); |
| 39 | + |
| 40 | + // Initialize the DebianJfrogAPICommunication object |
| 41 | + _debianJfrogAPICommunication = new DebianJfrogAPICommunication(_repoDomainName, _srcrepoName, _credentials, _timeout); |
19 | 42 | }
|
20 | 43 |
|
21 | 44 | [Test]
|
@@ -56,5 +79,74 @@ public void DebainJfrogApiCommunication_GetPackageInfo_ReturnsInvalidOperationEx
|
56 | 79 | //Assert
|
57 | 80 | Assert.ThrowsAsync<InvalidOperationException>(async () => await jfrogApicommunication.GetPackageInfo(new ComponentsToArtifactory()));
|
58 | 81 | }
|
| 82 | + [Test] |
| 83 | + public async Task GetApiKey_ReturnsSuccessfulResponse() |
| 84 | + { |
| 85 | + // Arrange |
| 86 | + var expectedResponse = new HttpResponseMessage(HttpStatusCode.OK); |
| 87 | + |
| 88 | + // Mock the SendAsync method to return the expected response |
| 89 | + _mockHandler |
| 90 | + .Protected() |
| 91 | + .Setup<Task<HttpResponseMessage>>( |
| 92 | + "SendAsync", |
| 93 | + ItExpr.Is<HttpRequestMessage>(req => |
| 94 | + req.Method == HttpMethod.Get && req.RequestUri.ToString() == $"{_repoDomainName}/api/security/apiKey"), |
| 95 | + ItExpr.IsAny<CancellationToken>()) |
| 96 | + .ReturnsAsync(expectedResponse) |
| 97 | + .Verifiable(); |
| 98 | + |
| 99 | + // Act |
| 100 | + var result = await _debianJfrogAPICommunication.GetApiKey(); |
| 101 | + |
| 102 | + // Assert |
| 103 | + Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); |
| 104 | + } |
| 105 | + [Test] |
| 106 | + public async Task MoveFromRepo_ReturnsSuccessfulResponse() |
| 107 | + { |
| 108 | + // Arrange |
| 109 | + var component = new ComponentsToArtifactory |
| 110 | + { |
| 111 | + MovePackageApiUrl = $"{_repoDomainName}/api/move/package" // Example URL |
| 112 | + }; |
| 113 | + |
| 114 | + var expectedResponse = new HttpResponseMessage(HttpStatusCode.OK) |
| 115 | + { |
| 116 | + Content = new StringContent("Move successful") |
| 117 | + }; |
| 118 | + |
| 119 | + // Mock the SendAsync method to return the expected response |
| 120 | + _mockHandler |
| 121 | + .Protected() |
| 122 | + .Setup<Task<HttpResponseMessage>>( |
| 123 | + "SendAsync", |
| 124 | + ItExpr.Is<HttpRequestMessage>(req => |
| 125 | + req.Method == HttpMethod.Post && req.RequestUri.ToString() == component.MovePackageApiUrl), |
| 126 | + ItExpr.IsAny<CancellationToken>()) |
| 127 | + .ReturnsAsync(expectedResponse) |
| 128 | + .Verifiable(); |
| 129 | + |
| 130 | + // Act |
| 131 | + var result = await _debianJfrogAPICommunication.MoveFromRepo(component); |
| 132 | + |
| 133 | + // Assert |
| 134 | + Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); |
| 135 | + } |
| 136 | + } |
| 137 | + public static class HttpMessageHandlerExtensions |
| 138 | + { |
| 139 | + public static Mock<HttpMessageHandler> SetupRequest(this Mock<HttpMessageHandler> mockHandler, HttpMethod method, string requestUri) |
| 140 | + { |
| 141 | + var request = new HttpRequestMessage(method, requestUri); |
| 142 | + |
| 143 | + mockHandler |
| 144 | + .Protected() |
| 145 | + .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.Is<HttpRequestMessage>(req => req.Method == method && req.RequestUri.ToString() == requestUri), ItExpr.IsAny<System.Threading.CancellationToken>()) |
| 146 | + .ReturnsAsync(new HttpResponseMessage()) |
| 147 | + .Verifiable(); |
| 148 | + |
| 149 | + return mockHandler; |
| 150 | + } |
59 | 151 | }
|
60 | 152 | }
|
0 commit comments