Skip to content

Commit 061f121

Browse files
Merge pull request #255 from siemens/CodeCoverage_27_02_25
Continuous Clearing Tool code coverage reached to 80%
2 parents e853132 + 2df80e8 commit 061f121

11 files changed

+665
-15
lines changed

src/AritfactoryUploader.UTest/PackageUploadInformationTest.cs

+43
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
using System.Net;
1313
using System.Text;
1414
using System.Threading.Tasks;
15+
using LCT.Common.Constants;
16+
using Newtonsoft.Json;
17+
using System.IO;
1518

1619
namespace AritfactoryUploader.UTest
1720
{
@@ -158,5 +161,45 @@ public void DisplayErrorForJfrogFoundPackages_GivenJfrogNotFoundPackages_Results
158161
// Assert
159162
Assert.That(JfrogNotFoundPackages.Count, Is.GreaterThanOrEqualTo(1));
160163
}
164+
165+
[Test]
166+
public void GetNotApprovedDebianPackages_FileExists_ShouldUpdateDebianComponents()
167+
{
168+
// Arrange
169+
var unknownPackages = new List<ComponentsToArtifactory>
170+
{
171+
new ComponentsToArtifactory { Name = "Package1", Version = "1.0.0" },
172+
new ComponentsToArtifactory { Name = "Package2", Version = "2.0.0" }
173+
};
174+
var projectResponse = new ProjectResponse();
175+
var mockFileOperations = new Mock<IFileOperations>();
176+
var filepath = Path.GetTempPath();
177+
var filename = Path.Combine(filepath, $"Artifactory_{FileConstant.artifactoryReportNotApproved}");
178+
179+
var existingProjectResponse = new ProjectResponse
180+
{
181+
Debian = new List<JsonComponents>
182+
{
183+
new JsonComponents { Name = "ExistingPackage", Version = "1.0.0" }
184+
}
185+
};
186+
var json = JsonConvert.SerializeObject(existingProjectResponse);
187+
File.WriteAllText(filename, json);
188+
189+
// Act
190+
PackageUploadInformation.GetNotApprovedDebianPackages(unknownPackages, projectResponse, mockFileOperations.Object, filepath, filename);
191+
192+
// Assert
193+
mockFileOperations.Verify(m => m.WriteContentToReportNotApprovedFile(It.Is<ProjectResponse>(pr =>
194+
pr.Debian.Count == 2 &&
195+
pr.Debian[0].Name == "Package1" &&
196+
pr.Debian[0].Version == "1.0.0" &&
197+
pr.Debian[1].Name == "Package2" &&
198+
pr.Debian[1].Version == "2.0.0"
199+
), filepath, FileConstant.artifactoryReportNotApproved, "Artifactory"), Times.Once);
200+
201+
// Cleanup
202+
File.Delete(filename);
203+
}
161204
}
162205
}
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

+33-6
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
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
14-
{
15-
[SetUp]
16-
public void Setup()
17-
{
18-
// Method intentionally left empty.
19-
}
19+
{
20+
2021

2122
[Test]
2223
public void DebainJfrogApiCommunication_CopyFromRemoteRepo_ReturnsInvalidOperationException()
@@ -56,5 +57,31 @@ public void DebainJfrogApiCommunication_GetPackageInfo_ReturnsInvalidOperationEx
5657
//Assert
5758
Assert.ThrowsAsync<InvalidOperationException>(async () => await jfrogApicommunication.GetPackageInfo(new ComponentsToArtifactory()));
5859
}
60+
[Test]
61+
public void DebainJfrogApiCommunication_GetApiKey_ReturnsInvalidOperationException()
62+
{
63+
//Arrange
64+
ArtifactoryCredentials repoCredentials = new ArtifactoryCredentials();
65+
66+
//Act
67+
JfrogApicommunication jfrogApicommunication = new DebianJfrogAPICommunication("", "", repoCredentials, 100);
68+
69+
//Assert
70+
Assert.ThrowsAsync<InvalidOperationException>(async () => await jfrogApicommunication.GetApiKey());
71+
}
72+
73+
[Test]
74+
public void DebianJfrogApiCommunication_MoveFromRepo_ReturnsInvalidOperationException()
75+
{
76+
//Arrange
77+
ArtifactoryCredentials repoCredentials = new ArtifactoryCredentials();
78+
79+
//Act
80+
JfrogApicommunication jfrogApicommunication = new DebianJfrogAPICommunication("", "", repoCredentials, 100);
81+
82+
//Assert
83+
Assert.ThrowsAsync<InvalidOperationException>(async () => await jfrogApicommunication.MoveFromRepo(new ComponentsToArtifactory()));
84+
}
5985
}
86+
6087
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using LCT.APICommunications.Model;
2+
using Moq;
3+
using Moq.Protected;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Net;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace LCT.APICommunications.UTest
12+
{
13+
[TestFixture]
14+
public class JfrogAqlApiCommunicationUTest
15+
{
16+
17+
18+
[Test]
19+
public void JfrogAqlApiCommunication_CheckConnection_ReturnsInvalidOperationException()
20+
{
21+
// Arrange
22+
ArtifactoryCredentials repoCredentials = new ArtifactoryCredentials();
23+
string invalidDomainName = ""; // Invalid domain name
24+
int timeout = 30; // Timeout in seconds
25+
26+
JfrogAqlApiCommunication jfrogApiCommunication = new JfrogAqlApiCommunication(invalidDomainName, repoCredentials, timeout);
27+
28+
// Act & Assert
29+
Assert.ThrowsAsync<InvalidOperationException>(async () => await jfrogApiCommunication.CheckConnection());
30+
}
31+
32+
[Test]
33+
public void JfrogAqlApiCommunication_GetInternalComponentDataByRepo_ReturnsInvalidOperationException()
34+
{
35+
// Arrange
36+
ArtifactoryCredentials repoCredentials = new ArtifactoryCredentials();
37+
string invalidDomainName = ""; // Invalid domain name
38+
int timeout = 30; // Timeout in seconds
39+
string invalidRepoName = "invalid-repo-name"; // Invalid repo name
40+
41+
JfrogAqlApiCommunication jfrogApiCommunication = new JfrogAqlApiCommunication(invalidDomainName, repoCredentials, timeout);
42+
43+
// Act & Assert
44+
Assert.ThrowsAsync<InvalidOperationException>(async () => await jfrogApiCommunication.GetInternalComponentDataByRepo(invalidRepoName));
45+
}
46+
[Test]
47+
public void JfrogAqlApiCommunication_GetNpmComponentDataByRepo_ReturnsInvalidOperationException()
48+
{
49+
// Arrange
50+
ArtifactoryCredentials repoCredentials = new ArtifactoryCredentials();
51+
string invalidDomainName = ""; // Invalid domain name
52+
int timeout = 30; // Timeout in seconds
53+
string invalidRepoName = "invalid-npm-repo"; // Invalid repo name
54+
55+
JfrogAqlApiCommunication jfrogApiCommunication = new JfrogAqlApiCommunication(invalidDomainName, repoCredentials, timeout);
56+
57+
// Act & Assert
58+
Assert.ThrowsAsync<InvalidOperationException>(async () => await jfrogApiCommunication.GetNpmComponentDataByRepo(invalidRepoName));
59+
}
60+
[Test]
61+
public void JfrogAqlApiCommunication_GetPypiComponentDataByRepo_ReturnsInvalidOperationException()
62+
{
63+
// Arrange
64+
ArtifactoryCredentials repoCredentials = new ArtifactoryCredentials();
65+
string invalidDomainName = ""; // Invalid domain name
66+
int timeout = 30; // Timeout in seconds
67+
string invalidRepoName = "invalid-pypi-repo"; // Invalid repo name
68+
69+
JfrogAqlApiCommunication jfrogApiCommunication = new JfrogAqlApiCommunication(invalidDomainName, repoCredentials, timeout);
70+
71+
// Act & Assert
72+
Assert.ThrowsAsync<InvalidOperationException>(async () => await jfrogApiCommunication.GetPypiComponentDataByRepo(invalidRepoName));
73+
}
74+
[Test]
75+
public void JfrogAqlApiCommunication_GetPackageInfo_ReturnsArgumentException_WhenNoPackageNameOrPathProvided()
76+
{
77+
// Arrange
78+
ArtifactoryCredentials repoCredentials = new ArtifactoryCredentials();
79+
string invalidDomainName = ""; // Invalid domain name
80+
int timeout = 30; // Timeout in seconds
81+
82+
JfrogAqlApiCommunication jfrogApiCommunication = new JfrogAqlApiCommunication(invalidDomainName, repoCredentials, timeout);
83+
84+
// Create a ComponentsToArtifactory object with invalid parameters (both packageName and path are null or empty)
85+
ComponentsToArtifactory component = new ComponentsToArtifactory
86+
{
87+
JfrogPackageName = null,
88+
Path = null
89+
};
90+
91+
// Act & Assert
92+
Assert.ThrowsAsync<ArgumentException>(async () => await jfrogApiCommunication.GetPackageInfo(component));
93+
}
94+
95+
}
96+
}

src/LCT.APICommunications.UTest/NPMJfrogAPICommunicationUTest.cs

+23
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,30 @@ public void NpmJfrogApiCommunication_CopyFromRemoteRepo_ReturnsInvalidOperationE
3131
//Assert
3232
Assert.ThrowsAsync<InvalidOperationException>(async () => await jfrogApicommunication.CopyFromRemoteRepo(new ComponentsToArtifactory()));
3333
}
34+
[Test]
35+
public void NpmJfrogApiCommunication_MoveFromRepo_ReturnsInvalidOperationException()
36+
{
37+
//Arrange
38+
ArtifactoryCredentials repoCredentials = new ArtifactoryCredentials();
39+
40+
//Act
41+
JfrogApicommunication jfrogApicommunication = new NpmJfrogApiCommunication("", "", repoCredentials, 100);
3442

43+
//Assert
44+
Assert.ThrowsAsync<InvalidOperationException>(async () => await jfrogApicommunication.MoveFromRepo(new ComponentsToArtifactory()));
45+
}
46+
[Test]
47+
public void NpmJfrogApiCommunication_GetApiKey_ReturnsInvalidOperationException()
48+
{
49+
//Arrange
50+
ArtifactoryCredentials repoCredentials = new ArtifactoryCredentials();
51+
52+
//Act
53+
JfrogApicommunication jfrogApicommunication = new NpmJfrogApiCommunication("", "", repoCredentials, 100);
54+
55+
//Assert
56+
Assert.ThrowsAsync<InvalidOperationException>(async () => await jfrogApicommunication.GetApiKey());
57+
}
3558
[Test]
3659
public void NpmJfrogApiCommunication_UpdatePackagePropertiesInJfrog_ReturnsInvalidOperationException()
3760
{

src/LCT.APICommunications.UTest/PythonJfrogApiCommunicationUTest.cs

+27
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
// --------------------------------------------------------------------------------------------------------------------
66

77
using LCT.APICommunications.Model;
8+
using System.Threading;
89

910
namespace LCT.APICommunications.UTest
1011
{
1112
[TestFixture]
1213
public class PythonJfrogApiCommunicationUTest
1314
{
15+
16+
1417
[Test]
1518
public void PythonJfrogApiCommunication_CopyFromRemoteRepo_ReturnsInvalidOperationException()
1619
{
@@ -23,6 +26,30 @@ public void PythonJfrogApiCommunication_CopyFromRemoteRepo_ReturnsInvalidOperati
2326
//Assert
2427
Assert.ThrowsAsync<InvalidOperationException>(async () => await jfrogApicommunication.CopyFromRemoteRepo(new ComponentsToArtifactory()));
2528
}
29+
[Test]
30+
public void PythonJfrogApiCommunication_MoveFromRepo_ReturnsInvalidOperationException()
31+
{
32+
//Arrange
33+
ArtifactoryCredentials repoCredentials = new ArtifactoryCredentials();
34+
35+
//Act
36+
JfrogApicommunication jfrogApicommunication = new PythonJfrogApiCommunication("", "", repoCredentials, 100);
37+
38+
//Assert
39+
Assert.ThrowsAsync<InvalidOperationException>(async () => await jfrogApicommunication.MoveFromRepo(new ComponentsToArtifactory()));
40+
}
41+
[Test]
42+
public void PythonJfrogApiCommunication_GetApiKey_ReturnsInvalidOperationException()
43+
{
44+
//Arrange
45+
ArtifactoryCredentials repoCredentials = new ArtifactoryCredentials();
46+
47+
//Act
48+
JfrogApicommunication jfrogApicommunication = new PythonJfrogApiCommunication("", "", repoCredentials, 100);
49+
50+
//Assert
51+
Assert.ThrowsAsync<InvalidOperationException>(async () => await jfrogApicommunication.GetApiKey());
52+
}
2653

2754
[Test]
2855
public void PythonJfrogApiCommunication_UpdatePackagePropertiesInJfrog_ReturnsInvalidOperationException()

0 commit comments

Comments
 (0)