Skip to content

Commit

Permalink
unit test case addition to imrove code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Viji committed Sep 16, 2024
1 parent 6ad6fab commit 328d213
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/ArtifactoryUploader/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using LCT.Common.Model;
using LCT.ArtifactPublisher;
using System.Diagnostics.CodeAnalysis;

namespace ArtifactoryUploader
{
[ExcludeFromCodeCoverage]
public static class Program
{
private static bool m_Verbose = false;
Expand Down
32 changes: 32 additions & 0 deletions src/LCT.PackageIdentifier.UTest/BomValidatorUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public async Task ValidateAppSettings_ProvidedProjectID_ReturnsProjectName()
mockISw360ProjectService.Verify(x => x.GetProjectNameByProjectIDFromSW360(It.IsAny<string>(), It.IsAny<string>(), projectReleases), Times.AtLeastOnce);

}

[TestCase]
public Task ValidateAppSettings_ProvidedProjectID_ReturnsInvalidDataException()
{
Expand All @@ -78,5 +79,36 @@ public Task ValidateAppSettings_ProvidedProjectID_ReturnsInvalidDataException()
Assert.ThrowsAsync<InvalidDataException>(async () => await BomValidator.ValidateAppSettings(CommonAppSettings, mockISw360ProjectService.Object, projectReleases));
return Task.CompletedTask;
}

[TestCase]
public async Task ValidateAppSettings_ProvidedProjectID_EndsTheApplicationOnClosedProject()
{
//Arrange
string projectName = "Test";
ProjectReleases projectReleases = new ProjectReleases();
projectReleases.clearingState = "CLOSED";
var CommonAppSettings = new CommonAppSettings(mockIFolderAction.Object)
{
SW360ProjectName = "Test"
};
mockISw360ProjectService.Setup(x => x.GetProjectNameByProjectIDFromSW360(It.IsAny<String>(), It.IsAny<string>(), projectReleases))
.ReturnsAsync(projectName);

mockIFileOperations.Setup(x => x.ValidateFilePath(It.IsAny<string>()))
.Callback((string message) => { })
.Verifiable();

mockIFolderAction.Setup(x => x.ValidateFolderPath(It.IsAny<string>()))
.Callback((string message) => { })
.Verifiable();
CommonAppSettings.PackageFilePath = "";

//Act
await BomValidator.ValidateAppSettings(CommonAppSettings, mockISw360ProjectService.Object, projectReleases);

//Assert
mockISw360ProjectService.Verify(x => x.GetProjectNameByProjectIDFromSW360(It.IsAny<string>(), It.IsAny<string>(), projectReleases), Times.AtLeastOnce);

}
}
}
5 changes: 5 additions & 0 deletions src/LCT.PackageIdentifier/BomHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
using LCT.Services.Interface;
using log4net;
using log4net.Core;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -116,6 +118,9 @@ public static string GetHashCodeUsingNpmView(string name, string version)
hashCode = result?.StdOut;
return hashCode?.Trim() ?? string.Empty;
}

[Obsolete("not used")]
[ExcludeFromCodeCoverage]
public static Result GetDependencyList(string bomFilePath, string depFilePath)
{
bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
Expand Down
5 changes: 3 additions & 2 deletions src/LCT.PackageIdentifier/BomValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace LCT.PackageIdentifier
public static class BomValidator
{
static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public static async Task ValidateAppSettings(CommonAppSettings appSettings, ISw360ProjectService bomService, ProjectReleases projectReleases)
public static async Task<int> ValidateAppSettings(CommonAppSettings appSettings, ISw360ProjectService bomService, ProjectReleases projectReleases)
{
string sw360ProjectName = await bomService.GetProjectNameByProjectIDFromSW360(appSettings.SW360ProjectID, appSettings.SW360ProjectName,projectReleases);

Expand All @@ -33,12 +33,13 @@ public static async Task ValidateAppSettings(CommonAppSettings appSettings, ISw3
{
Logger.Error($"Provided Sw360 project is not in active state ,Please make sure you added the correct project details that is in active state..");
Logger.Debug($"ValidateAppSettings() : Sw360 project " + projectReleases.Name + " is in " + projectReleases.clearingState + " state.");
CommonHelper.CallEnvironmentExit(-1);
return -1;
}
else
{
appSettings.SW360ProjectName = sw360ProjectName;
}
return 0;
}
}
}
10 changes: 7 additions & 3 deletions src/LCT.PackageIdentifier/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
using LCT.APICommunications.Interfaces;
using LCT.APICommunications;
using LCT.APICommunications.Model;
using System.Globalization;
using System.Linq;
using LCT.ArtifactPublisher;
using System.Diagnostics.CodeAnalysis;


namespace LCT.PackageIdentifier
{
/// <summary>
/// Program class
/// </summary>
[ExcludeFromCodeCoverage]
public class Program
{
private static bool m_Verbose = false;
Expand Down Expand Up @@ -155,7 +155,11 @@ private static async Task ValidateAppsettingsFile(CommonAppSettings appSettings,
Timeout = appSettings.TimeOut
};
ISw360ProjectService sw360ProjectService = new Sw360ProjectService(new SW360ApicommunicationFacade(sw360ConnectionSettings));
await BomValidator.ValidateAppSettings(appSettings, sw360ProjectService, projectReleases);
int isValid = await BomValidator.ValidateAppSettings(appSettings, sw360ProjectService, projectReleases);
if (isValid == -1)
{
CommonHelper.CallEnvironmentExit(-1);
}
}
private static string DisplayInclude(CommonAppSettings appSettings)
{
Expand Down
Binary file not shown.
20 changes: 20 additions & 0 deletions src/LCT.SW360PackageCreator.UTest/CreatorValidatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,30 @@ public async Task ValidateAppSettings_TestPositive()
//Act
await CreatorValidator.ValidateAppSettings(CommonAppSettings, mockISw360ProjectService.Object,projectReleases);

//Assert
mockISw360ProjectService.Verify(x => x.GetProjectNameByProjectIDFromSW360(It.IsAny<string>(), It.IsAny<string>(), projectReleases), Times.AtLeastOnce);
}

[TestCase]
public async Task ValidateAppSettings_OnClosedProject_EndsTheApplication()
{
//Arrange
string projectName = "Test";
ProjectReleases projectReleases = new ProjectReleases();
projectReleases.clearingState = "CLOSED";
var CommonAppSettings = new CommonAppSettings();
CommonAppSettings.SW360ProjectName = "Test";
mockISw360ProjectService.Setup(x => x.GetProjectNameByProjectIDFromSW360(It.IsAny<String>(), It.IsAny<string>(), projectReleases))
.ReturnsAsync(projectName);

//Act
await CreatorValidator.ValidateAppSettings(CommonAppSettings, mockISw360ProjectService.Object, projectReleases);

//Assert
mockISw360ProjectService.Verify(x => x.GetProjectNameByProjectIDFromSW360(It.IsAny<string>(), It.IsAny<string>(), projectReleases), Times.AtLeastOnce);

}

[TestCase]
public void ValidateAppSettings_TestNegative()
{
Expand Down
60 changes: 60 additions & 0 deletions src/LCT.SW360PackageCreator.UTest/DebianPackageDownloaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using LCT.SW360PackageCreator.Interfaces;
using Moq;
using NUnit.Framework;
using System;
using System.IO;
using System.Threading.Tasks;

Expand Down Expand Up @@ -133,5 +134,64 @@ public async Task DownloadReleaseAttachmentSourceForDebian_ByRetry_ReturnsEmptyD
Assert.That(string.IsNullOrEmpty(attachmentUrlList));
}

//string attachmentJson = outFolder + @"..\..\..\src\LCT.SW360PackageCreator.UTest\ComponentCreatorUTFiles\Attachment.json";
//

public void GetPatchedFileFromDownloadedFolder_ProvidedPatchURLs_ReturnsEmptyDownloadPath()
{
//Arrange
Result result = new Result()
{
ExitCode = 0
};
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string outFolder = Path.GetDirectoryName(exePath);
string downloadFolderPath = outFolder + @"..\..\..\src\LCT.SW360PackageCreator.UTest\ComponentCreatorUTFiles";
// \adduser_3.118-debian10-combined.tar.bz2";

//Provided patch URLs for applying patch
var lstComparisonBomData = new ComparisonBomData()
{
Name = "dash",
Version = ""
};
string patchedFilePath = string.Empty;

// ACT
patchedFilePath = DebianPackageDownloader.GetPatchedFileFromDownloadedFolder(downloadFolderPath);

// Assert
Assert.That(!string.IsNullOrEmpty(patchedFilePath));
}

[TestCase]
public void GetPatchedFileFromDownloadedFolder_ProvidedPatchURLs_ThrowsArgumentException()
{
// Arrange
string patchedFilePath = string.Empty;

// Act
patchedFilePath = DebianPackageDownloader.GetPatchedFileFromDownloadedFolder(null);

// Assert
Assert.That(string.IsNullOrEmpty(patchedFilePath));
}

[TestCase]
public void ApplyPatchforComponents_ProvidedPatchURLs_ReturnsEmptyDownloadPath()
{
//Arrange
string actual = string.Empty;
Result result = null;
Mock<IDebianPatcher> debianPatcher = new Mock<IDebianPatcher>();
debianPatcher.Setup(x=>x.ApplyPatch(It.IsAny<ComparisonBomData>(), It.IsAny<string>(),
It.IsAny<string>())).Returns(result);

//Act
DebianPackageDownloader debianPackageDownloader = new DebianPackageDownloader(debianPatcher.Object);
debianPackageDownloader.ApplyPatchforComponents(new ComparisonBomData() ,string.Empty, string.Empty);
//Assert
Assert.That(actual!= null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
</ItemGroup>

<ItemGroup>
<None Update="ComponentCreatorUTFiles\adduser_3.118-debian10-combined.tar.bz2">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="ComponentCreatorUTFiles\Attachment.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
5 changes: 3 additions & 2 deletions src/LCT.SW360PackageCreator/CreatorValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace LCT.SW360PackageCreator
public static class CreatorValidator
{
static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public static async Task ValidateAppSettings(CommonAppSettings appSettings, ISw360ProjectService sw360ProjectService, ProjectReleases projectReleases)
public static async Task<int> ValidateAppSettings(CommonAppSettings appSettings, ISw360ProjectService sw360ProjectService, ProjectReleases projectReleases)
{
string sw360ProjectName = await sw360ProjectService.GetProjectNameByProjectIDFromSW360(appSettings.SW360ProjectID, appSettings.SW360ProjectName,projectReleases);

Expand All @@ -33,12 +33,13 @@ public static async Task ValidateAppSettings(CommonAppSettings appSettings, ISw3
{
Logger.Error($"Provided Sw360 project is not in active state ,Please make sure you added the correct project details that is in active state..");
Logger.Debug($"ValidateAppSettings() : Sw360 project " + projectReleases.Name + " is in " + projectReleases.clearingState + " state.");
CommonHelper.CallEnvironmentExit(-1);
return -1;
}
else
{
appSettings.SW360ProjectName = sw360ProjectName;
}
return 0;
}
}
}
4 changes: 2 additions & 2 deletions src/LCT.SW360PackageCreator/DebianPackageDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private static async Task<string> DownloadTarFileAndGetPath(ComparisonBomData co
return downloadPath;
}

private string ApplyPatchforComponents(ComparisonBomData component, string localDownloadPath, string fileName)
public string ApplyPatchforComponents(ComparisonBomData component, string localDownloadPath, string fileName)
{
Result result;
string patchedFile = string.Empty;
Expand Down Expand Up @@ -253,7 +253,7 @@ private static void DeletePatchedFolderAndFile(string folderPath, string downloa
}
}

private static string GetPatchedFileFromDownloadedFolder(string currentDownloadFolder)
public static string GetPatchedFileFromDownloadedFolder(string currentDownloadFolder)
{
string patchedFilePath = string.Empty;
try
Expand Down
8 changes: 7 additions & 1 deletion src/LCT.SW360PackageCreator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
Expand All @@ -29,6 +30,7 @@ namespace LCT.SW360PackageCreator
/// <summary>
/// Program class
/// </summary>
[ExcludeFromCodeCoverage]
public class Program
{
public static Stopwatch CreatorStopWatch { get; set; }
Expand Down Expand Up @@ -57,7 +59,11 @@ static async Task Main(string[] args)

string FolderPath = InitiateLogger(appSettings);
settingsManager.CheckRequiredArgsToRun(appSettings, "Creator");
await CreatorValidator.ValidateAppSettings(appSettings, sw360ProjectService, projectReleases);
int isValid = await CreatorValidator.ValidateAppSettings(appSettings, sw360ProjectService, projectReleases);
if (isValid == -1)
{
CommonHelper.CallEnvironmentExit(-1);
}

Logger.Logger.Log(null, Level.Notice, $"\n====================<<<<< Package creator >>>>>====================", null);
Logger.Logger.Log(null, Level.Notice, $"\nStart of Package creator execution : {DateTime.Now}", null);
Expand Down

0 comments on commit 328d213

Please sign in to comment.