Skip to content

Commit

Permalink
UT fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Viji committed Sep 30, 2024
1 parent 99437b8 commit e93bb7e
Show file tree
Hide file tree
Showing 11 changed files with 705 additions and 9 deletions.
263 changes: 263 additions & 0 deletions src/AritfactoryUploader.UTest/PackageUploadHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
using System.Linq;
using LCT.ArtifactoryUploader.Model;
using LCT.APICommunications;
using System.Net.Http;
using System.Net;
using LCT.APICommunications.Model.AQL;
using LCT.Services.Interface;
using Moq;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;

namespace AritfactoryUploader.UTest
{
Expand Down Expand Up @@ -317,5 +323,262 @@ private static List<Component> GetComponentList()
componentLists.Add(comp4);
return componentLists;
}

[Test]
public void GetUploadPackageDetails_CoversAllScenarios()
{
// Arrange
DisplayPackagesInfo displayPackagesInfo = new DisplayPackagesInfo()
{
JfrogFoundPackagesConan = new List<ComponentsToArtifactory>()
{
new ComponentsToArtifactory()
{
ResponseMessage = new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK
}
}
},
JfrogFoundPackagesMaven = new List<ComponentsToArtifactory>()
{
new ComponentsToArtifactory()
{
ResponseMessage = new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK
}
}
},
JfrogFoundPackagesNpm = new List<ComponentsToArtifactory>()
{
new ComponentsToArtifactory()
{
ResponseMessage = new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK
}
}
},
JfrogFoundPackagesNuget = new List<ComponentsToArtifactory>()
{
new ComponentsToArtifactory()
{
ResponseMessage = new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK
}
}
},
JfrogFoundPackagesPython = new List<ComponentsToArtifactory>()
{
new ComponentsToArtifactory()
{
ResponseMessage = new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK
}
}
},
JfrogFoundPackagesDebian = new List<ComponentsToArtifactory>()
{
new ComponentsToArtifactory()
{
ResponseMessage = new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK
}
}
}
};

// Act
List<ComponentsToArtifactory> uploadedPackages = PackageUploadHelper.GetUploadePackageDetails(displayPackagesInfo);

// Assert
Assert.AreEqual(6, uploadedPackages.Count);
}

[Test]
public async Task GetSrcRepoDetailsForPyPiOrConanPackages_WhenPypiRepoExists_ReturnsArtifactoryRepoName()
{
// Arrange
Property prop1 = new Property
{
Name = Dataconstant.Cdx_ArtifactoryRepoName,
Value = "Reponame"
};
List<Property> properties = new List<Property>() { prop1 };
var item = new Component
{
Purl = "pypi://example-package",
Properties = properties,
Name = "pypi component",
Version = "1.0.0"
};

//GetInternalComponentDataByRepo
var aqlResultList = new List<AqlResult>
{
new AqlResult
{
Repo = "pypi-repo",
Path = "path/to/package",
Name = "pypi component-1.0.0",
}
};
var jFrogServiceMock = new Mock<IJFrogService>();
jFrogServiceMock.Setup(x => x.GetInternalComponentDataByRepo(It.IsAny<string>())).ReturnsAsync(aqlResultList);
PackageUploadHelper.jFrogService = jFrogServiceMock.Object;

// Act
var result = await PackageUploadHelper.GetSrcRepoDetailsForPyPiOrConanPackages(item);

// Assert
Assert.IsNotNull(result);
Assert.AreEqual("pypi-repo", result.Repo);
Assert.AreEqual("path/to/package", result.Path);
}

[Test]
public async Task GetSrcRepoDetailsForPyPiOrConanPackages_WhenConanRepoExists_ReturnsArtifactoryRepoName()
{
// Arrange
Property prop1 = new Property
{
Name = Dataconstant.Cdx_ArtifactoryRepoName,
Value = "Reponame"
};
List<Property> properties = new List<Property>() { prop1 };

var item = new Component
{
Purl = "conan://example-package",
Properties = properties,
Name = "conan component",
Version = "1.0.0"
};

var aqlResultList = new List<AqlResult>
{
new AqlResult
{
Repo = "conan-repo",
Path = "path/to/conan component/1.0.0",
Name = "conan component-1.0.0",
}
};

var jFrogServiceMock = new Mock<IJFrogService>();
jFrogServiceMock.Setup(x => x.GetInternalComponentDataByRepo(It.IsAny<string>())).ReturnsAsync(aqlResultList);
PackageUploadHelper.jFrogService = jFrogServiceMock.Object;

// Act
var result = await PackageUploadHelper.GetSrcRepoDetailsForPyPiOrConanPackages(item);

// Assert
Assert.IsNotNull(result);
Assert.AreEqual("conan-repo", result.Repo);
Assert.AreEqual("path/to/conan component/1.0.0", result.Path);
}

[Test]
public async Task GetSrcRepoDetailsForPyPiOrConanPackages_WhenNoRepoExists_ReturnsNull()
{
// Arrange
var item = new Component
{
Purl = "unknown://example-package"
};
var jFrogServiceMock = new Mock<IJFrogService>();
PackageUploadHelper.jFrogService = jFrogServiceMock.Object;

// Act
var result = await PackageUploadHelper.GetSrcRepoDetailsForPyPiOrConanPackages(item);

// Assert
Assert.IsNull(result);
}

[Test]
[TestCase("NPM", ".tgz")]
[TestCase("NUGET", ".nupkg")]
[TestCase("MAVEN", ".jar")]
[TestCase("DEBIAN", ".deb")]
[TestCase("PYTHON", ".whl")]
[TestCase("CONAN", "package.tgz")]
public void GetPkgeNameExtensionBasedOnComponentType_GivenType_ReturnsPkgNameExtension(string type, string extension)
{
// Arrange
var package = new ComponentsToArtifactory();
package.ComponentType = type;
// Act
var actualExtension = PackageUploadHelper.GetPackageNameExtensionBasedOnComponentType(package);
// Assert
Assert.AreEqual(extension, actualExtension);
}

[Test]
public void GetJfrogApiCommInstance_GivenComponent_ReturnsJfrogApiCommunicationInstance()
{
// Arrange
var component = new ComponentsToArtifactory
{
ComponentType = "MAVEN",
JfrogApi = "https://api.jfrog.com",
SrcRepoName = "maven-repo",
ApiKey = "api-key",
Email = "[email protected]"
};
var timeout = 5000;

// Act
var result = PackageUploadHelper.GetJfrogApiCommInstance(component, timeout);

// Assert
Assert.IsInstanceOf<MavenJfrogApiCommunication>(result);
}

[Test]
public void GetJfrogApiCommInstance_GivenComponentWithPythonType_ReturnsJfrogApiCommunicationInstance()
{
// Arrange
var component = new ComponentsToArtifactory
{
ComponentType = "PYTHON",
JfrogApi = "https://api.jfrog.com",
SrcRepoName = "python-repo",
ApiKey = "api-key",
Email = "[email protected]"
};
var timeout = 5000;

// Act
var result = PackageUploadHelper.GetJfrogApiCommInstance(component, timeout);

// Assert
Assert.IsInstanceOf<PythonJfrogApiCommunication>(result);
}

[Test]
public void GetJfrogApiCommInstance_GivenComponentWithUnknownType_ReturnsJfrogApiCommunicationInstance()
{
// Arrange
var component = new ComponentsToArtifactory
{
ComponentType = "UNKNOWN",
JfrogApi = "https://api.jfrog.com",
SrcRepoName = "unknown-repo",
ApiKey = "api-key",
Email = "[email protected]"
};
var timeout = 5000;

// Act
var result = PackageUploadHelper.GetJfrogApiCommInstance(component, timeout);

// Assert
Assert.IsInstanceOf<NpmJfrogApiCommunication>(result);
}
}
}
10 changes: 5 additions & 5 deletions src/ArtifactoryUploader/PackageUploadHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ private static string GetComponentType(Component item)
return string.Empty;
}

private async static Task<AqlResult> GetSrcRepoDetailsForPyPiOrConanPackages(Component item)
public async static Task<AqlResult> GetSrcRepoDetailsForPyPiOrConanPackages(Component item)
{
if (item.Purl.Contains("pypi", StringComparison.OrdinalIgnoreCase))
{
Expand Down Expand Up @@ -1053,7 +1053,7 @@ private static async Task SourceRepoFoundToUploadArtifactory(PackageType package
}
}

private static IJFrogApiCommunication GetJfrogApiCommInstance(ComponentsToArtifactory component, int timeout)
public static IJFrogApiCommunication GetJfrogApiCommInstance(ComponentsToArtifactory component, int timeout)
{

ArtifactoryCredentials repoCredentials = new ArtifactoryCredentials()
Expand Down Expand Up @@ -1155,7 +1155,7 @@ private static void IncrementCountersBasedOnPackageType(UploaderKpiData uploader
}
}

private static async Task<List<AqlResult>> GetListOfComponentsFromRepo(string[] repoList, IJFrogService jFrogService)
public static async Task<List<AqlResult>> GetListOfComponentsFromRepo(string[] repoList, IJFrogService jFrogService)
{
if (repoList != null && repoList.Length > 0)
{
Expand Down Expand Up @@ -1287,7 +1287,7 @@ private static string GetJfrogRepoPath(AqlResult aqlResult)
return $"{aqlResult.Repo}/{aqlResult.Path}/{aqlResult.Name}";
}

private static string GetPackageNameExtensionBasedOnComponentType(ComponentsToArtifactory package)
public static string GetPackageNameExtensionBasedOnComponentType(ComponentsToArtifactory package)
{
string packageNameEXtension = string.Empty;
if (package.ComponentType.Equals("NPM", StringComparison.OrdinalIgnoreCase))
Expand Down Expand Up @@ -1332,7 +1332,7 @@ private static async Task<List<AqlResult>> GetJfrogRepoInfoForAllTypePackages(Li
return aqlResultList;
}

private static List<ComponentsToArtifactory> GetUploadePackageDetails(DisplayPackagesInfo displayPackagesInfo)
public static List<ComponentsToArtifactory> GetUploadePackageDetails(DisplayPackagesInfo displayPackagesInfo)
{
List<ComponentsToArtifactory> uploadedPackages = new List<ComponentsToArtifactory>();

Expand Down
2 changes: 1 addition & 1 deletion src/LCT.Common.UTests/ComponentEqualityComparerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using CycloneDX.Models;
using NUnit.Framework;

namespace LCT.Common.UTests
namespace LCT.Common.UTest
{
[TestFixture]
public class ComponentEqualityComparerTests
Expand Down
39 changes: 39 additions & 0 deletions src/LCT.Common.UTests/EnvironmentTypeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// --------------------------------------------------------------------------------------------------------------------
// SPDX-FileCopyrightText: 2024 Siemens AG
//
// SPDX-License-Identifier: MIT
// --------------------------------------------------------------------------------------------------------------------

using LCT.Common.Runtime;
using NUnit.Framework;
using System;

namespace LCT.Common.UTest
{
[TestFixture]
internal class EnvironmentTypeTests
{
[Test]
public void EnumValues_ShouldMatchExpectedValues()
{
// Arrange
var expectedValues = new[]
{
EnvironmentType.Unknown,
EnvironmentType.GitLab,
EnvironmentType.AzurePipeline,
EnvironmentType.AzureRelease
};

// Act
var actualValues = (EnvironmentType[])Enum.GetValues(typeof(EnvironmentType));

// Assert
Assert.That(actualValues.Length, Is.EqualTo(expectedValues.Length));
for (int i = 0; i < expectedValues.Length; i++)
{
Assert.That(actualValues[i], Is.EqualTo(expectedValues[i]));
}
}
}
}
2 changes: 1 addition & 1 deletion src/LCT.Common.UTests/FileOperationsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Collections.Generic;
using System.IO;

namespace LCT.Common.UTests
namespace LCT.Common.UTest
{
[TestFixture]
public class FileOperationsTest
Expand Down
2 changes: 1 addition & 1 deletion src/LCT.Common.UTests/FolderActionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using NUnit.Framework;
using System.IO;

namespace LCT.Common.UTests
namespace LCT.Common.UTest
{
[TestFixture]
public class FolderActionTest
Expand Down
Loading

0 comments on commit e93bb7e

Please sign in to comment.