From a79e8b0c1cd1bb955d2db5c009c7cc9a42774d62 Mon Sep 17 00:00:00 2001 From: Chalapala RaghavendraReddy Date: Sat, 4 Jan 2025 17:40:50 +0530 Subject: [PATCH] unit test case fix --- .../PackageUploadHelperTest.cs | 50 ++++++++++++++++++- .../PackageUploadHelper.cs | 18 +++---- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/src/AritfactoryUploader.UTest/PackageUploadHelperTest.cs b/src/AritfactoryUploader.UTest/PackageUploadHelperTest.cs index 6a3292c1..f1fb8906 100644 --- a/src/AritfactoryUploader.UTest/PackageUploadHelperTest.cs +++ b/src/AritfactoryUploader.UTest/PackageUploadHelperTest.cs @@ -861,6 +861,54 @@ public void GetMoveURL_GivenInvalidComponentType_ReturnsEmptyString() // Assert Assert.AreEqual(string.Empty, result); - } + } + + [Test] + public async Task GetSrcRepoDetailsForPyPiOrConanPackages_WhenNpmRepoExists_ReturnsArtifactoryRepoName() + { + // Arrange + var repoNameProperty = new Property + { + Name = Dataconstant.Cdx_ArtifactoryRepoName, + Value = "npm-repo" + }; + var properties = new List { repoNameProperty }; + var item = new Component + { + Purl = "pkg:npm/example-package", + Properties = properties, + Name = "example-package", + Version = "1.0.0" + }; + var aqlResultList = new List + { + new AqlResult + { + Repo = "npm-repo", + Path = "path/to/package", + Name = "example-package-1.0.0", + Properties = new List + { + new AqlProperty { Key = "npm.name", Value = "example-package" }, + new AqlProperty { Key = "npm.version", Value = "1.0.0" } + } + } + }; + + var jFrogServiceMock = new Mock(); + + jFrogServiceMock.Setup(x => x.GetNpmComponentDataByRepo(It.IsAny())).ReturnsAsync(aqlResultList); + + PackageUploadHelper.jFrogService = jFrogServiceMock.Object; + + + // Act + var result = await PackageUploadHelper.GetSrcRepoDetailsForPyPiOrConanPackages(item); + + // Assert + Assert.IsNotNull(result); + Assert.AreEqual("npm-repo", result.Repo); + Assert.AreEqual("path/to/package", result.Path); + } } } diff --git a/src/ArtifactoryUploader/PackageUploadHelper.cs b/src/ArtifactoryUploader/PackageUploadHelper.cs index afc9b576..26b26eba 100644 --- a/src/ArtifactoryUploader/PackageUploadHelper.cs +++ b/src/ArtifactoryUploader/PackageUploadHelper.cs @@ -977,11 +977,11 @@ public async static Task GetSrcRepoDetailsForPyPiOrConanPackages(Comp } else if (item.Purl.Contains("npm", StringComparison.OrdinalIgnoreCase)) { - var aqlNpmResultList = await GetNpmListOfComponentsFromRepo(new string[] { item.Properties.Find(x => x.Name == Dataconstant.Cdx_ArtifactoryRepoName)?.Value }, jFrogService); + aqlResultList = await GetNpmListOfComponentsFromRepo(new string[] { item.Properties.Find(x => x.Name == Dataconstant.Cdx_ArtifactoryRepoName)?.Value }, jFrogService); - if (aqlNpmResultList.Count > 0) + if (aqlResultList.Count > 0) { - return GetNpmArtifactoryRepoName(aqlNpmResultList, item); + return GetNpmArtifactoryRepoName(aqlResultList, item); } } @@ -1215,17 +1215,17 @@ public static async Task> GetNpmListOfComponentsFromRepo(string[ private static AqlResult GetArtifactoryRepoName(List aqlResultList, Component component) { string jfrogpackageName = GetFullNameOfComponent(component); - AqlResult repoName = aqlResultList.Find(x => x.Properties.Any(p => p.Key == "pypi.normalized.name" && p.Value == jfrogpackageName) && x.Properties.Any(p => p.Key == "pypi.version" && p.Value == component.Version)); - - return repoName; + return aqlResultList.Find(x => x.Properties != null && + x.Properties.Any(p => p.Key == "pypi.normalized.name" && p.Value == jfrogpackageName) && + x.Properties.Any(p => p.Key == "pypi.version" && p.Value == component.Version)); } private static AqlResult GetNpmArtifactoryRepoName(List aqlResultList, Component component) { string jfrogpackageName = GetFullNameOfComponent(component); - AqlResult repoName = aqlResultList.Find(x => x.Properties.Any(p => p.Key == "npm.name" && p.Value == jfrogpackageName) && x.Properties.Any(p => p.Key == "npm.version" && p.Value == component.Version)); - - return repoName; + return aqlResultList.Find(x => x.Properties != null && + x.Properties.Any(p => p.Key == "npm.name" && p.Value == jfrogpackageName) && + x.Properties.Any(p => p.Key == "npm.version" && p.Value == component.Version)); }