Skip to content

Commit

Permalink
Fossology validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ragavareddychalapala committed Feb 4, 2025
1 parent 2b81af8 commit 6a4f921
Show file tree
Hide file tree
Showing 16 changed files with 250 additions and 1 deletion.
55 changes: 55 additions & 0 deletions src/LCT.SW360PackageCreator.UTest/CreatorValidatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,28 @@
using LCT.Common;
using System.Threading.Tasks;
using LCT.APICommunications.Model;
using LCT.Facade.Interfaces;
using LCT.APICommunications.Model.Foss;
using Newtonsoft.Json;
using System.Net.Http;

namespace LCT.SW360PackageCreator.UTest
{
[TestFixture]
public class CreatorValidatorTest
{
public Mock<ISw360ProjectService> mockISw360ProjectService;
public Mock<ISW360ApicommunicationFacade> mockISW360ApicommunicationFacade;
public Mock<ISW360CommonService> mockISW360CommonService;
public Mock<ISw360CreatorService> mockISw360CreatorService;
public Mock<ISW360Service> mockISW360Service;
public CreatorValidatorTest()
{
mockISw360ProjectService = new Mock<ISw360ProjectService>(MockBehavior.Strict);
mockISW360ApicommunicationFacade = new Mock<ISW360ApicommunicationFacade>(MockBehavior.Strict);
mockISW360CommonService = new Mock<ISW360CommonService>(MockBehavior.Strict);
mockISw360CreatorService = new Mock<ISw360CreatorService>(MockBehavior.Strict);
mockISW360Service = new Mock<ISW360Service>(MockBehavior.Strict);

}
[TestCase]
Expand Down Expand Up @@ -93,6 +105,49 @@ public void ValidateAppSettings_TestNegative()
Assert.ThrowsAsync<InvalidDataException>(() => CreatorValidator.ValidateAppSettings(CommonAppSettings, mockISw360ProjectService.Object, projectReleases));

}
[TestCase]
public async Task TriggerFossologyValidation_TestPositive()
{
// Arrange
var appSettings = new CommonAppSettings()
{
SW360 = new SW360()
{
URL = "https://sw360.example.com"
}
};
var responseBody = "{\"_embedded\":{\"sw360:releases\":[{\"_links\":{\"self\":{\"href\":\"https://sw360.example.com/resource/api/releases/123\"}}}]}}";
var releasesInfo = new ReleasesInfo
{
Name = "TestRelease",
Version = "1.0",
ClearingState = "APPROVED"
};
var fossTriggerStatus = new FossTriggerStatus
{
Links = new Links
{
Self = new Self
{
Href = "https://fossology.example.com"
}
}
};
var triggerStatusResponse = JsonConvert.SerializeObject(fossTriggerStatus);
mockISW360ApicommunicationFacade.Setup(x => x.GetReleases()).ReturnsAsync(responseBody);
mockISW360Service.Setup(x => x.GetReleaseDataOfComponent(It.IsAny<string>())).ReturnsAsync(releasesInfo);
mockISw360CreatorService.Setup(x => x.TriggerFossologyProcessForValidation(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(fossTriggerStatus);
mockISW360ApicommunicationFacade.Setup(x => x.GetReleaseById(It.IsAny<string>())).ReturnsAsync(new HttpResponseMessage
{
Content = new StringContent("{\"Name\":\"TestRelease\",\"Version\":\"1.0\",\"ClearingState\":\"APPROVED\"}")
});
mockISW360ApicommunicationFacade.Setup(x => x.TriggerFossologyProcess(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(triggerStatusResponse);
// Act
await CreatorValidator.TriggerFossologyValidation(appSettings, mockISW360ApicommunicationFacade.Object);
// Assert
mockISW360ApicommunicationFacade.Verify(x => x.GetReleases(), Times.Once);
mockISW360ApicommunicationFacade.Verify(x => x.TriggerFossologyProcess(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}

}
}
78 changes: 78 additions & 0 deletions src/LCT.SW360PackageCreator/CreatorValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
using LCT.APICommunications.Model;
using log4net;
using System.Reflection;
using LCT.Common.Interface;
using LCT.APICommunications.Model.Foss;
using LCT.APICommunications;
using LCT.Facade.Interfaces;
using LCT.Services;
using log4net.Core;
using Newtonsoft.Json;
using System;


namespace LCT.SW360PackageCreator
Expand All @@ -21,6 +29,7 @@ namespace LCT.SW360PackageCreator
public static class CreatorValidator
{
static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static IEnvironmentHelper environmentHelper;
public static async Task<int> ValidateAppSettings(CommonAppSettings appSettings, ISw360ProjectService sw360ProjectService, ProjectReleases projectReleases)
{
string sw360ProjectName = await sw360ProjectService.GetProjectNameByProjectIDFromSW360(appSettings.SW360.ProjectID, appSettings.SW360.ProjectName,projectReleases);
Expand All @@ -41,5 +50,74 @@ public static async Task<int> ValidateAppSettings(CommonAppSettings appSettings,
}
return 0;
}
public static async Task TriggerFossologyValidation(CommonAppSettings appSettings, ISW360ApicommunicationFacade sW360ApicommunicationFacade)
{
IEnvironmentHelper environmentHelper = new EnvironmentHelper();
ISW360CommonService sw360CommonService = new SW360CommonService(sW360ApicommunicationFacade);
ISw360CreatorService sw360CreatorService = new Sw360CreatorService(sW360ApicommunicationFacade, sw360CommonService);
ISW360Service sw360Service = new Sw360Service(sW360ApicommunicationFacade, sw360CommonService, environmentHelper);
ReleasesInfo releasesInfo = new ReleasesInfo();

try
{
string responseBody = await sW360ApicommunicationFacade.GetReleases();
var modelMappedObject = JsonConvert.DeserializeObject<ComponentsRelease>(responseBody);
var releaseId = string.Empty;
if (modelMappedObject != null && modelMappedObject.Embedded?.Sw360Releases?.Count > 0)
{
foreach (var ReleaseidList in modelMappedObject.Embedded?.Sw360Releases)
{
var releaseUrl = ReleaseidList.Links?.Self?.Href;
releasesInfo = await CallApiAsync(releaseUrl, sw360Service);
if (releasesInfo != null)
{
releaseId = CommonHelper.GetSubstringOfLastOccurance(releaseUrl, "/");
break;
}
}
}
else
{
Logger.Debug($"TriggerFossologyValidation():Trigger Fossology Process validation failed");
Logger.Error($"Trigger Fossology Process validation failed");
environmentHelper.CallEnvironmentExit(-1);
}
if (releasesInfo != null)
{
string sw360link = $"{releasesInfo.Name}:{releasesInfo.Version}:{appSettings.SW360.URL}{ApiConstant.Sw360ReleaseUrlApiSuffix}" +
$"{releaseId}#/tab-Summary";
FossTriggerStatus fossResult = await sw360CreatorService.TriggerFossologyProcessForValidation(releaseId, sw360link);
if (!string.IsNullOrEmpty(fossResult?.Links?.Self?.Href))
{
Logger.Logger.Log(null, Level.Info, $"SW360 Fossology Process validation successfull!!", null);
}
}
}
catch (AggregateException ex)
{
Logger.Debug($"\tError in TriggerFossologyProcess--{ex}");
Logger.Error($"Trigger Fossology Process failed.Please check fossology configuration in sw360");
environmentHelper.CallEnvironmentExit(-1);
}
}
private static async Task<ReleasesInfo> CallApiAsync(string endpoint, ISW360Service sw360Service)
{
try
{
ReleasesInfo releasesInfo = await sw360Service.GetReleaseDataOfComponent(endpoint);
var clearingState = releasesInfo.ClearingState?.ToString();
if (clearingState == "APPROVED")
{
// Return the result if the clearingState is "approved"
return releasesInfo;
}
}
catch (Exception ex)
{
Logger.Debug($"CallApiAsync():{ex}");
}

return null;
}
}
}
67 changes: 67 additions & 0 deletions src/LCT.SW360PackageCreator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net.Http;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
Expand Down Expand Up @@ -88,6 +89,12 @@ static async Task Main(string[] args)
if (appSettings.IsTestMode)
Logger.Logger.Log(null, Level.Notice, $"\tMode\t\t\t --> {appSettings.Mode}\n", null);

//Validate Fossology Url
if (appSettings.SW360.Fossology.EnableTrigger)
{
if (await FossologyUrlValidation(appSettings))
await CreatorValidator.TriggerFossologyValidation(appSettings, sW360ApicommunicationFacade);
}
await InitiatePackageCreatorProcess(appSettings, sw360ProjectService, sW360ApicommunicationFacade);

Logger.Logger.Log(null, Level.Notice, $"End of Package Creator execution: {DateTime.Now}\n", null);
Expand All @@ -96,6 +103,66 @@ static async Task Main(string[] args)
PipelineArtifactUploader.UploadArtifacts();
}

public static async Task<bool> FossologyUrlValidation(CommonAppSettings appSettings)
{
string url = appSettings.SW360.Fossology.URL.ToLower();
string prodFossUrl = "automation.fossology".ToLower();
string stageFossUrl = "stage.fossology".ToLower();
environmentHelper = new EnvironmentHelper();

if (string.IsNullOrEmpty(appSettings.SW360.Fossology.URL))
{
Logger.Error($"Fossology URL is not provided ,Please make sure to add Fossologyurl in appsettings..");
Logger.Debug($"Fossologyurlcheck() : Fossology url not provided in appsettings");
environmentHelper.CallEnvironmentExit(-1);
}
else if (Uri.IsWellFormedUriString(appSettings.SW360.Fossology.URL, UriKind.Absolute))
{
if (url.Contains(prodFossUrl) || url.Contains(stageFossUrl))
{
// Send GET request to validate Fossology URL
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync(new Uri(appSettings.SW360.Fossology.URL));
if (response.IsSuccessStatusCode)
{
// Fossology URL is valid
return true;
}
else
{
// Fossology URL is not valid
Logger.Error($"Fossology URL is not valid ,Please make sure to add valid fossologyurl in appsettings..");
Logger.Debug($"Fossologyurlcheck() : Fossology URL is not valid.");
environmentHelper.CallEnvironmentExit(-1);
}
}
catch (HttpRequestException ex)
{
// Fossology URL is not valid
Logger.Error($"Fossology URL is not working ,Please check once try again....");
Logger.Debug($"Fossologyurlcheck() : Fossology URL is not valid.{ex}");
environmentHelper.CallEnvironmentExit(-1);
}
}
}
else
{
Logger.Debug($"Fossologyurlcheck() : Fossology URL is not valid");
Logger.Error($"Fossology URL is not valid ,Please check once try again....");
environmentHelper.CallEnvironmentExit(-1);
}
}
else
{
Logger.Error($"Fossology URL is not provided ,Please make sure to add fossologyurl in appsettings..");
Logger.Debug($"Fossologyurlcheck() : Fossology url not provided in appsettings");
environmentHelper.CallEnvironmentExit(-1);
}
return false;
}
private static CatoolInfo GetCatoolVersionFromProjectfile()
{
CatoolInfo catoolInfo = new CatoolInfo();
Expand Down
1 change: 1 addition & 0 deletions src/LCT.Services/Interface/ISW360CreatorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ Task<ReleaseCreateStatus> CreateReleaseForComponent(
Task<bool> UpdatePurlIdForExistingRelease(ComparisonBomData cbomData, string releaseId, ReleasesInfo releasesInfo = null);

Task<bool> UdpateSW360ReleaseContent(Components component, string fossUrl);
Task<FossTriggerStatus> TriggerFossologyProcessForValidation(string releaseId, string sw360link);
}
}
37 changes: 37 additions & 0 deletions src/LCT.Services/Sw360CreatorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using LCT.APICommunications.Model.Foss;
using LCT.Common;
using LCT.Common.Constants;
using LCT.Common.Interface;
using LCT.Common.Model;
using LCT.Facade.Interfaces;
using LCT.Services.Interface;
Expand Down Expand Up @@ -38,6 +39,7 @@ public class Sw360CreatorService : ISw360CreatorService
static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
readonly ISW360ApicommunicationFacade m_SW360ApiCommunicationFacade;
readonly ISW360CommonService m_SW360CommonService;
private static IEnvironmentHelper environmentHelper;

public Sw360CreatorService(ISW360ApicommunicationFacade sw360ApiCommunicationFacade)
{
Expand Down Expand Up @@ -779,5 +781,40 @@ private async Task<UpdateReleaseAdditinoalData> GetUpdateReleaseContent(string r

return updateRelease;
}
public async Task<FossTriggerStatus> TriggerFossologyProcessForValidation(string releaseId, string sw360link)
{
environmentHelper = new EnvironmentHelper();
FossTriggerStatus fossTriggerStatus = null;
try
{
string triggerStatus = await m_SW360ApiCommunicationFacade.TriggerFossologyProcess(releaseId, sw360link);
fossTriggerStatus = JsonConvert.DeserializeObject<FossTriggerStatus>(triggerStatus);
}
catch (HttpRequestException ex)
{
Logger.Debug($"TriggerFossologyProcessForValidation():", ex);
Logger.Error($"Fossology Process failed.Please check fossology configuration or Token in sw360");
environmentHelper.CallEnvironmentExit(-1);
}
catch (InvalidOperationException ex)
{
Logger.Debug($"TriggerFossologyProcessForValidation():", ex);
Logger.Error($"Fossology Process failed.Please check fossology configuration in sw360");
environmentHelper.CallEnvironmentExit(-1);
}
catch (UriFormatException ex)
{
Logger.Debug($"TriggerFossologyProcessForValidation():", ex);
Logger.Error($"Fossology Process failed.Please check fossology configuration in sw360");
environmentHelper.CallEnvironmentExit(-1);
}
catch (TaskCanceledException ex)
{
Logger.Debug($"TriggerFossologyProcessForValidation():", ex);
Logger.Error($"Fossology Process failed.Please check fossology configuration in sw360");
environmentHelper.CallEnvironmentExit(-1);
}
return fossTriggerStatus;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void ComponentCreatorExe_ProvidedBOMFilePath_ReturnsSuccess()
TestConstant.SW360ProjectID, testParameters.SW360ProjectID,
TestConstant.SW360ProjectName, testParameters.SW360ProjectName,
TestConstant.ProjectType,"ALPINE",
TestConstant.FossologyURL, testParameters.FossUrl,
TestConstant.Mode,""}),
"Test to run component creator EXE execution");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void TestComponentCreatorExe_Conan()
TestConstant.SW360AuthTokenType, testParameters.SW360AuthTokenType,
TestConstant.SW360ProjectID, testParameters.SW360ProjectID,
TestConstant.SW360ProjectName, testParameters.SW360ProjectName,
TestConstant.FossologyURL, testParameters.FossUrl,
TestConstant.Mode,""
}),
"Test to run Package Creator EXE execution");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public void ComponentCreatorExe_ProvidedBOMFilePath_ReturnsSuccess()
TestConstant.SW360ProjectID, testParameters.SW360ProjectID,
TestConstant.SW360ProjectName, testParameters.SW360ProjectName,
TestConstant.ProjectType,"DEBIAN",
TestConstant.FossologyURL, testParameters.FossUrl,
TestConstant.Mode,""}),
"Test to run component creator EXE execution");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void ComponentCreatorExe_ProvidedBOMFilePath_ReturnsSuccess()
TestConstant.SW360AuthTokenType, testParameters.SW360AuthTokenType,
TestConstant.SW360ProjectID, testParameters.SW360ProjectID,
TestConstant.SW360ProjectName, testParameters.SW360ProjectName,
TestConstant.FossologyURL, testParameters.FossUrl,
TestConstant.Mode,""}),
"Test to run Package Creator EXE execution");
}
Expand Down
1 change: 1 addition & 0 deletions src/SW360IntegrationTest/NPM/ComponentCreatorTestMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void TestComponentCreatorExe_TestMode()
TestConstant.SW360AuthTokenType, testParameters.SW360AuthTokenType,
TestConstant.SW360ProjectID, testParameters.SW360ProjectID,
TestConstant.SW360ProjectName, testParameters.SW360ProjectName,
TestConstant.FossologyURL, testParameters.FossUrl,
TestConstant.Mode,"test"
}),
"Test to run Package Creator EXE execution");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public void TestComponentCreatorExe()
TestConstant.SW360AuthTokenType, testParameters.SW360AuthTokenType,
TestConstant.SW360ProjectID, testParameters.SW360ProjectID,
TestConstant.SW360ProjectName, testParameters.SW360ProjectName,
TestConstant.FossologyURL, testParameters.FossUrl,
TestConstant.Mode,"" }),
"Test to run Package Creator EXE execution");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void ComponentCreatorExe_ProvidedBOMFilePath_ReturnsSuccess()
TestConstant.SW360ProjectID, testParameters.SW360ProjectID,
TestConstant.SW360ProjectName, testParameters.SW360ProjectName,
TestConstant.ProjectType,"NUGET",
TestConstant.FossologyURL, testParameters.FossUrl,
TestConstant.Mode,""}),
"Test to run Package Creator EXE execution");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public void ComponentCreatorExe_ProvidedBOMFilePath_ReturnsSuccess()
TestConstant.SW360ProjectID, testParameters.SW360ProjectID,
TestConstant.SW360ProjectName, testParameters.SW360ProjectName,
TestConstant.ProjectType,"NUGET",
TestConstant.FossologyURL, testParameters.FossUrl,
TestConstant.Mode,""});

Assert.IsTrue(value == 0 || value == 2,
Expand Down
Loading

0 comments on commit 6a4f921

Please sign in to comment.