Skip to content

Commit

Permalink
unit test case fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ragavareddychalapala committed Jan 4, 2025
1 parent a03c1c1 commit a79e8b0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
50 changes: 49 additions & 1 deletion src/AritfactoryUploader.UTest/PackageUploadHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Property> { repoNameProperty };
var item = new Component
{
Purl = "pkg:npm/example-package",
Properties = properties,
Name = "example-package",
Version = "1.0.0"
};
var aqlResultList = new List<AqlResult>
{
new AqlResult
{
Repo = "npm-repo",
Path = "path/to/package",
Name = "example-package-1.0.0",
Properties = new List<AqlProperty>
{
new AqlProperty { Key = "npm.name", Value = "example-package" },
new AqlProperty { Key = "npm.version", Value = "1.0.0" }
}
}
};

var jFrogServiceMock = new Mock<IJFrogService>();

jFrogServiceMock.Setup(x => x.GetNpmComponentDataByRepo(It.IsAny<string>())).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);
}
}
}
18 changes: 9 additions & 9 deletions src/ArtifactoryUploader/PackageUploadHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -977,11 +977,11 @@ public async static Task<AqlResult> 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);
}
}

Expand Down Expand Up @@ -1215,17 +1215,17 @@ public static async Task<List<AqlResult>> GetNpmListOfComponentsFromRepo(string[
private static AqlResult GetArtifactoryRepoName(List<AqlResult> 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<AqlResult> 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));
}


Expand Down

0 comments on commit a79e8b0

Please sign in to comment.