Skip to content

Commit 2a81ff3

Browse files
Code coverage
1 parent cd81e0c commit 2a81ff3

File tree

5 files changed

+690
-4
lines changed

5 files changed

+690
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using LCT.APICommunications.Model;
2+
using log4net;
3+
using Moq;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Net;
8+
using System.Security;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
12+
namespace LCT.APICommunications.UTest
13+
{
14+
[TestFixture]
15+
public class AttachmentHelperUTest
16+
{
17+
private readonly string _sw360AuthToken = "mockToken";
18+
private readonly string _sw360AuthTokenType = "Bearer";
19+
private readonly string _sw360ReleaseApi = "https://api.mock.com/release";
20+
private readonly string _attachmentFilePath = "mockFilePath";
21+
private AttachmentHelper _attachmentHelper;
22+
23+
[SetUp]
24+
public void SetUp()
25+
{
26+
27+
// Initialize the AttachmentHelper with mock logger.
28+
_attachmentHelper = new AttachmentHelper(_sw360AuthTokenType, _sw360AuthToken, _sw360ReleaseApi);
29+
}
30+
31+
[Test]
32+
public void AttachComponentSourceToSW360_ValidInput_ReturnsCorrectUrl()
33+
{
34+
// Arrange
35+
var attachReport = new AttachReport
36+
{
37+
ReleaseId = "123",
38+
AttachmentFile = _attachmentFilePath,
39+
AttachmentType = "Source",
40+
AttachmentReleaseComment = "Test comment"
41+
};
42+
43+
// Mock WebRequest behavior
44+
var webRequestMock = new Mock<HttpWebRequest>();
45+
var webResponseMock = new Mock<WebResponse>();
46+
47+
// Simulate valid response
48+
webRequestMock.Setup(x => x.GetRequestStream()).Returns(new MemoryStream());
49+
webRequestMock.Setup(x => x.GetResponse()).Returns(webResponseMock.Object);
50+
51+
// Act
52+
var result = _attachmentHelper.AttachComponentSourceToSW360(attachReport);
53+
54+
// Assert
55+
Assert.AreEqual("https://api.mock.com/release/123/attachments", result);
56+
}
57+
58+
[Test]
59+
public void WriteAttachmentsJSONFile_CreatesValidJSONFile()
60+
{
61+
// Arrange
62+
var attachReport = new AttachReport
63+
{
64+
AttachmentType = "Source",
65+
AttachmentReleaseComment = "Test comment"
66+
};
67+
string folderPath = Path.Combine(Path.GetTempPath(), "ClearingTool/DownloadedFiles");
68+
string fileName = "Attachment.json";
69+
70+
// Act
71+
AttachmentHelper.WriteAttachmentsJSONFile(fileName, folderPath, attachReport);
72+
73+
// Assert
74+
var jsonFilePath = Path.Combine(folderPath, "Attachment.json");
75+
Assert.IsTrue(File.Exists(jsonFilePath));
76+
77+
// Validate JSON content
78+
var fileContent = File.ReadAllText(jsonFilePath);
79+
Assert.IsTrue(fileContent.Contains("Source"));
80+
Assert.IsTrue(fileContent.Contains("Test comment"));
81+
82+
// Cleanup
83+
File.Delete(jsonFilePath);
84+
}
85+
}
86+
}

src/LCT.APICommunications.UTest/DebianJfrogAPICommunicationUTest.cs

+93-1
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,39 @@
66

77

88
using LCT.APICommunications.Model;
9+
using Moq;
10+
using Moq.Protected;
11+
using System.Net;
12+
using System.Net.Http;
13+
using System.Threading;
914

1015
namespace LCT.APICommunications.UTest
1116
{
1217
[TestFixture]
1318
public class DebainJfrogAPICommunicationUTest
1419
{
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;
1527
[SetUp]
1628
public void Setup()
1729
{
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);
1942
}
2043

2144
[Test]
@@ -56,5 +79,74 @@ public void DebainJfrogApiCommunication_GetPackageInfo_ReturnsInvalidOperationEx
5679
//Assert
5780
Assert.ThrowsAsync<InvalidOperationException>(async () => await jfrogApicommunication.GetPackageInfo(new ComponentsToArtifactory()));
5881
}
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+
}
59151
}
60152
}

0 commit comments

Comments
 (0)