|
4 | 4 | // SPDX-License-Identifier: MIT |
5 | 5 | // -------------------------------------------------------------------------------------------------------------------- |
6 | 6 |
|
| 7 | +using CycloneDX.Models; |
| 8 | +using LCT.APICommunications.Model; |
7 | 9 | using LCT.APICommunications.Model.AQL; |
8 | 10 | using LCT.ArtifactoryUploader; |
9 | 11 | using LCT.Services.Interface; |
10 | 12 | using Moq; |
11 | 13 | using NUnit.Framework; |
12 | 14 | using System.Collections.Generic; |
| 15 | +using System.Reflection; |
13 | 16 | using System.Threading.Tasks; |
14 | 17 |
|
15 | 18 | namespace AritfactoryUploader.UTest |
@@ -40,7 +43,248 @@ public async Task GetJfrogRepoInfoForAllTypePackages_GivenDestRepoNames_ReturnsA |
40 | 43 | // Assert |
41 | 44 | Assert.That(actualAqlResultList.Count, Is.GreaterThan(2)); |
42 | 45 | } |
| 46 | + [Test] |
| 47 | + public void GetJfrogRepoPath_GivenAqlResultWithEmptyPath_ReturnsRepoAndName() |
| 48 | + { |
| 49 | + // Arrange |
| 50 | + var aqlResult = new AqlResult |
| 51 | + { |
| 52 | + Repo = "test-repo", |
| 53 | + Name = "test-name", |
| 54 | + Path = "" |
| 55 | + }; |
| 56 | + |
| 57 | + var methodInfo = typeof(JfrogRepoUpdater).GetMethod("GetJfrogRepoPath", BindingFlags.NonPublic | BindingFlags.Static); |
| 58 | + |
| 59 | + // Act |
| 60 | + var result = (string)methodInfo.Invoke(null, new object[] { aqlResult }); |
| 61 | + |
| 62 | + // Assert |
| 63 | + Assert.AreEqual("test-repo/test-name", result); |
| 64 | + } |
| 65 | + |
| 66 | + [Test] |
| 67 | + public void GetJfrogRepoPath_GivenAqlResultWithDotPath_ReturnsRepoAndName() |
| 68 | + { |
| 69 | + // Arrange |
| 70 | + var aqlResult = new AqlResult |
| 71 | + { |
| 72 | + Repo = "test-repo", |
| 73 | + Name = "test-name", |
| 74 | + Path = "." |
| 75 | + }; |
| 76 | + |
| 77 | + var methodInfo = typeof(JfrogRepoUpdater).GetMethod("GetJfrogRepoPath", BindingFlags.NonPublic | BindingFlags.Static); |
| 78 | + |
| 79 | + // Act |
| 80 | + var result = (string)methodInfo.Invoke(null, new object[] { aqlResult }); |
| 81 | + |
| 82 | + // Assert |
| 83 | + Assert.AreEqual("test-repo/test-name", result); |
| 84 | + } |
| 85 | + |
| 86 | + [Test] |
| 87 | + public void GetJfrogRepoPath_GivenAqlResultWithValidPath_ReturnsRepoPathAndName() |
| 88 | + { |
| 89 | + // Arrange |
| 90 | + var aqlResult = new AqlResult |
| 91 | + { |
| 92 | + Repo = "test-repo", |
| 93 | + Name = "test-name", |
| 94 | + Path = "test-path" |
| 95 | + }; |
| 96 | + |
| 97 | + var methodInfo = typeof(JfrogRepoUpdater).GetMethod("GetJfrogRepoPath", BindingFlags.NonPublic | BindingFlags.Static); |
| 98 | + |
| 99 | + // Act |
| 100 | + var result = (string)methodInfo.Invoke(null, new object[] { aqlResult }); |
| 101 | + |
| 102 | + // Assert |
| 103 | + Assert.AreEqual("test-repo/test-path/test-name", result); |
| 104 | + } |
| 105 | + [Test] |
| 106 | + public void GetJfrogInfoOfThePackageUploaded_GivenConanPackage_ReturnsMatchingAqlResult() |
| 107 | + { |
| 108 | + // Arrange |
| 109 | + var jfrogPackagesListAql = new List<AqlResult> |
| 110 | + { |
| 111 | + new AqlResult { Path = "package1/1.0.0", Name = "package.conan" }, |
| 112 | + new AqlResult { Path = "package2/2.0.0", Name = "package2.conan" } |
| 113 | + }; |
| 114 | + |
| 115 | + var package = new ComponentsToArtifactory |
| 116 | + { |
| 117 | + Name = "package1", |
| 118 | + Version = "1.0.0", |
| 119 | + ComponentType = "CONAN" |
| 120 | + }; |
| 121 | + |
| 122 | + string packageNameExtension = "conan"; |
| 123 | + |
| 124 | + var methodInfo = typeof(JfrogRepoUpdater).GetMethod("GetJfrogInfoOfThePackageUploaded", BindingFlags.NonPublic | BindingFlags.Static); |
| 125 | + |
| 126 | + // Act |
| 127 | + var result = (AqlResult)methodInfo.Invoke(null, new object[] { jfrogPackagesListAql, package, packageNameExtension }); |
| 128 | + |
| 129 | + // Assert |
| 130 | + Assert.IsNotNull(result); |
| 131 | + Assert.AreEqual("package1/1.0.0", result.Path); |
| 132 | + Assert.AreEqual("package.conan", result.Name); |
| 133 | + } |
| 134 | + |
| 135 | + [Test] |
| 136 | + public void GetJfrogInfoOfThePackageUploaded_GivenNonConanPackage_ReturnsMatchingAqlResult() |
| 137 | + { |
| 138 | + // Arrange |
| 139 | + var jfrogPackagesListAql = new List<AqlResult> |
| 140 | + { |
| 141 | + new AqlResult { Path = "package1", Name = "1.0.0.jar" }, |
| 142 | + new AqlResult { Path = "package2", Name = "2.0.0.jar" } |
| 143 | + }; |
| 144 | + |
| 145 | + var package = new ComponentsToArtifactory |
| 146 | + { |
| 147 | + Name = "package1", |
| 148 | + Version = "1.0.0", |
| 149 | + ComponentType = "MAVEN" |
| 150 | + }; |
| 151 | + |
| 152 | + string packageNameExtension = "jar"; |
| 153 | + |
| 154 | + var methodInfo = typeof(JfrogRepoUpdater).GetMethod("GetJfrogInfoOfThePackageUploaded", BindingFlags.NonPublic | BindingFlags.Static); |
43 | 155 |
|
| 156 | + // Act |
| 157 | + var result = (AqlResult)methodInfo.Invoke(null, new object[] { jfrogPackagesListAql, package, packageNameExtension }); |
| 158 | + |
| 159 | + // Assert |
| 160 | + Assert.IsNotNull(result); |
| 161 | + Assert.AreEqual("package1", result.Path); |
| 162 | + Assert.AreEqual("1.0.0.jar", result.Name); |
| 163 | + } |
| 164 | + |
| 165 | + [Test] |
| 166 | + public void GetJfrogInfoOfThePackageUploaded_GivenNoMatchingPackage_ReturnsNull() |
| 167 | + { |
| 168 | + // Arrange |
| 169 | + var jfrogPackagesListAql = new List<AqlResult> |
| 170 | + { |
| 171 | + new AqlResult { Path = "package1", Name = "1.0.0.jar" }, |
| 172 | + new AqlResult { Path = "package2", Name = "2.0.0.jar" } |
| 173 | + }; |
| 174 | + |
| 175 | + var package = new ComponentsToArtifactory |
| 176 | + { |
| 177 | + Name = "package3", |
| 178 | + Version = "3.0.0", |
| 179 | + ComponentType = "MAVEN" |
| 180 | + }; |
| 181 | + |
| 182 | + string packageNameExtension = "jar"; |
| 183 | + |
| 184 | + var methodInfo = typeof(JfrogRepoUpdater).GetMethod("GetJfrogInfoOfThePackageUploaded", BindingFlags.NonPublic | BindingFlags.Static); |
| 185 | + |
| 186 | + // Act |
| 187 | + var result = (AqlResult)methodInfo.Invoke(null, new object[] { jfrogPackagesListAql, package, packageNameExtension }); |
| 188 | + |
| 189 | + // Assert |
| 190 | + Assert.IsNull(result); |
| 191 | + } |
| 192 | + [Test] |
| 193 | + public void UpdateJfroRepoPathProperty_GivenComponentNotInUploadList_SkipsUpdate() |
| 194 | + { |
| 195 | + // Arrange |
| 196 | + var bom = new Bom |
| 197 | + { |
| 198 | + Components = new List<Component> |
| 199 | + { |
| 200 | + new Component |
| 201 | + { |
| 202 | + Name = "component1", |
| 203 | + Version = "1.0.0", |
| 204 | + Purl = "pkg:generic/[email protected]", |
| 205 | + Properties = null |
| 206 | + }, |
| 207 | + new Component |
| 208 | + { |
| 209 | + Name = "component2", |
| 210 | + Version = "2.0.0", |
| 211 | + Purl = "pkg:generic/[email protected]", |
| 212 | + Properties = null |
| 213 | + } |
| 214 | + } |
| 215 | + }; |
| 216 | + |
| 217 | + var uploadedPackages = new List<ComponentsToArtifactory> |
| 218 | + { |
| 219 | + new ComponentsToArtifactory |
| 220 | + { |
| 221 | + Name = "component3", // Does not match any component in BOM |
| 222 | + Version = "3.0.0", |
| 223 | + Purl = "pkg:generic/[email protected]", |
| 224 | + ComponentType = "MAVEN" |
| 225 | + } |
| 226 | + }; |
| 227 | + |
| 228 | + var jfrogPackagesListAql = new List<AqlResult> |
| 229 | + { |
| 230 | + new AqlResult { Repo = "repo1", Path = "path1", Name = "component3-3.0.0.jar" } |
| 231 | + }; |
| 232 | + |
| 233 | + // Act |
| 234 | + var methodInfo = typeof(JfrogRepoUpdater).GetMethod("UpdateJfroRepoPathProperty", BindingFlags.NonPublic | BindingFlags.Static); |
| 235 | + var updatedComponents = (List<Component>)methodInfo.Invoke(null, new object[] { bom, uploadedPackages, jfrogPackagesListAql }); |
| 236 | + |
| 237 | + // Assert |
| 238 | + Assert.IsNotNull(updatedComponents); |
| 239 | + Assert.AreEqual(2, updatedComponents.Count); |
| 240 | + |
| 241 | + // Verify that no properties were added to the components in BOM |
| 242 | + Assert.IsNull(updatedComponents[0].Properties); |
| 243 | + Assert.IsNull(updatedComponents[1].Properties); |
| 244 | + } |
| 245 | + |
| 246 | + [Test] |
| 247 | + public void UpdateJfroRepoPathProperty_GivenJfrogDataNotFound_SkipsUpdate() |
| 248 | + { |
| 249 | + // Arrange |
| 250 | + var bom = new Bom |
| 251 | + { |
| 252 | + Components = new List<Component> |
| 253 | + { |
| 254 | + new Component |
| 255 | + { |
| 256 | + Name = "component1", |
| 257 | + Version = "1.0.0", |
| 258 | + Purl = "pkg:generic/[email protected]", |
| 259 | + Properties = null |
| 260 | + } |
| 261 | + } |
| 262 | + }; |
| 263 | + |
| 264 | + var uploadedPackages = new List<ComponentsToArtifactory> |
| 265 | + { |
| 266 | + new ComponentsToArtifactory |
| 267 | + { |
| 268 | + Name = "component1", |
| 269 | + Version = "1.0.0", |
| 270 | + Purl = "pkg:generic/[email protected]", |
| 271 | + ComponentType = "MAVEN" |
| 272 | + } |
| 273 | + }; |
| 274 | + |
| 275 | + var jfrogPackagesListAql = new List<AqlResult>(); // No matching JFrog data |
| 276 | + |
| 277 | + // Act |
| 278 | + var methodInfo = typeof(JfrogRepoUpdater).GetMethod("UpdateJfroRepoPathProperty", BindingFlags.NonPublic | BindingFlags.Static); |
| 279 | + var updatedComponents = (List<Component>)methodInfo.Invoke(null, new object[] { bom, uploadedPackages, jfrogPackagesListAql }); |
| 280 | + |
| 281 | + // Assert |
| 282 | + Assert.IsNotNull(updatedComponents); |
| 283 | + Assert.AreEqual(1, updatedComponents.Count); |
| 284 | + |
| 285 | + // Verify that no properties were added to the component |
| 286 | + Assert.IsNull(updatedComponents[0].Properties); |
| 287 | + } |
44 | 288 |
|
45 | 289 | } |
46 | 290 | } |
0 commit comments