Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fossology URL and Token validation in SW360 #246

Merged
merged 27 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5b7a52f
Fossology url validation
ragavareddychalapala Feb 3, 2025
2b81af8
Revert "Fossology url validation"
ragavareddychalapala Feb 3, 2025
6a4f921
Fossology validation
ragavareddychalapala Feb 4, 2025
8988fc2
Merge branch 'development' into feature/FossologyUrlValidation
crvreddy Feb 4, 2025
91d229e
updated
ragavareddychalapala Feb 4, 2025
501e2d2
Merge branch 'feature/FossologyUrlValidation' of https://github.com/s…
ragavareddychalapala Feb 4, 2025
3689715
updated
ragavareddychalapala Feb 4, 2025
c3d3f60
Merge branch 'development' into feature/FossologyUrlValidation
crvreddy Feb 4, 2025
de55636
updated code changes
ragavareddychalapala Feb 5, 2025
c53c126
Merge branch 'feature/FossologyUrlValidation' of https://github.com/s…
ragavareddychalapala Feb 5, 2025
69603f2
Merge branch 'development' into feature/FossologyUrlValidation
crvreddy Feb 5, 2025
e8be609
Updated requested changes
ragavareddychalapala Feb 5, 2025
8d9f7b7
updated changes
ragavareddychalapala Feb 6, 2025
ec61d5f
Fixed test case issue
ragavareddychalapala Feb 6, 2025
7638944
updated requested changes
ragavareddychalapala Feb 6, 2025
23137e4
updated test cases
ragavareddychalapala Feb 6, 2025
90d2aa8
Removed unnessasary publish artifacts
ragavareddychalapala Feb 6, 2025
50e50bb
Updated requested changes
ragavareddychalapala Feb 6, 2025
3627235
updated requested changes
ragavareddychalapala Feb 6, 2025
fb0599f
Updated code changes
ragavareddychalapala Feb 6, 2025
2752554
updated changes
ragavareddychalapala Feb 6, 2025
50d8b88
removed unnessasary lines
ragavareddychalapala Feb 7, 2025
fde108d
Removed sucess message
ragavareddychalapala Feb 7, 2025
badc877
Undo Changes
ragavareddychalapala Feb 7, 2025
70df2cc
undo test case
ragavareddychalapala Feb 7, 2025
80ff130
Merge branch 'development' into feature/FossologyUrlValidation
crvreddy Feb 7, 2025
491357f
updated small correction
ragavareddychalapala Feb 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ public interface ISw360ApiCommunication
string AttachComponentSourceToSW360(AttachReport attachReport);
void DownloadAttachmentUsingWebClient(string attachmentDownloadLink, string fileName);
Task<HttpResponseMessage> GetComponentDetailsByUrl(string componentLink);
Task<HttpResponseMessage> GetAllReleasesWithAllData(int page, int pageEntries);
}
}
6 changes: 6 additions & 0 deletions src/LCT.APICommunications/SW360Apicommunication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,12 @@ public async Task<HttpResponseMessage> GetComponentUsingName(string componentNam
string url = $"{sw360ComponentApi}{ApiConstant.ComponentNameUrl}{componentName}";
return await httpClient.GetAsync(url);
}
public async Task<HttpResponseMessage> GetAllReleasesWithAllData(int page, int pageEntries)
{
HttpClient httpClient = GetHttpClient();
string url = $"{sw360ReleaseApi}?page={page}&allDetails=true&page_entries={pageEntries}";
return await httpClient.GetAsync(url);
}
#endregion

#region PRIVATE METHODS
Expand Down
3 changes: 2 additions & 1 deletion src/LCT.Common/CommonAppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ public string URL
}
set
{
if (AppDomain.CurrentDomain.FriendlyName.Contains("SW360PackageCreator"))
if (!AppDomain.CurrentDomain.FriendlyName.Contains("PackageIdentifier") &&
!AppDomain.CurrentDomain.FriendlyName.Contains("ArtifactoryUploader"))
{
if (string.IsNullOrEmpty(value))
{
Expand Down
2 changes: 2 additions & 0 deletions src/LCT.Common/Constants/Dataconstant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public static class Dataconstant
public const string Cdx_Siemensfilename = "internal:siemens:clearing:siemens:filename";
public const string Cdx_SiemensDirect = "internal:siemens:clearing:siemens:direct";
public const string Cdx_ExcludeComponent = "internal:siemens:clearing:sw360:exclude";
public const string ProductionFossologyURL = "automation.fossology";
public const string StageFossologyURL = "stage.fossology";

public static Dictionary<string, string> PurlCheck()
{
Expand Down
8 changes: 4 additions & 4 deletions src/LCT.Common/PipelineArtifactUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public static void UploadArtifacts()
/// </summary>
public static void UploadLogs()
{
EnvironmentType envType = RuntimeEnvironment.GetEnvironment();
LogManager.Shutdown();
EnvironmentType envType = RuntimeEnvironment.GetEnvironment();
if (envType == EnvironmentType.AzurePipeline && !string.IsNullOrEmpty(Log4Net.CatoolLogPath) && File.Exists(Log4Net.CatoolLogPath))
{
{
LogManager.Shutdown();
Console.WriteLine($"##vso[artifact.upload containerfolder={LogContainerFolderName};artifactname={LogArtifactFolderName}]{Log4Net.CatoolLogPath}");
}
else if (envType == EnvironmentType.Unknown)
Expand All @@ -44,7 +44,7 @@ public static void UploadLogs()
/// </summary>
public static void UploadBom()
{
EnvironmentType envType = RuntimeEnvironment.GetEnvironment();
EnvironmentType envType = RuntimeEnvironment.GetEnvironment();
if (envType == EnvironmentType.AzurePipeline && !string.IsNullOrEmpty(FileOperations.CatoolBomFilePath) && File.Exists(FileOperations.CatoolBomFilePath))
{
Console.WriteLine($"##vso[artifact.upload containerfolder={BomContainerFolderName};artifactname={BomArtifactFolderName}]{FileOperations.CatoolBomFilePath}");
Expand Down
1 change: 1 addition & 0 deletions src/LCT.Facade/Interfaces/ISW360ApicommunicationFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ public interface ISW360ApicommunicationFacade
Task<HttpResponseMessage> UpdateLinkedRelease(string projectId, string releaseId, UpdateLinkedRelease updateLinkedRelease);
Task<HttpResponseMessage> GetReleaseByExternalId(string purlId,string externalIdKey = "");
Task<HttpResponseMessage> GetComponentByExternalId(string purlId, string externalIdKey = "");
Task<HttpResponseMessage> GetAllReleasesWithAllData(int page, int pageEntries);
}
}
4 changes: 4 additions & 0 deletions src/LCT.Facade/SW360ApicommunicationFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,9 @@ public Task<HttpResponseMessage> GetComponentByExternalId(string purlId, string
{
return m_sw360ApiCommunication.GetComponentByExternalId(purlId, externalIdKey);
}
public Task<HttpResponseMessage> GetAllReleasesWithAllData(int page, int pageEntries)
{
return m_sw360ApiCommunication.GetAllReleasesWithAllData(page, pageEntries);
}
}
}
198 changes: 192 additions & 6 deletions src/LCT.SW360PackageCreator.UTest/CreatorValidatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,46 @@
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;
using LCT.Common.Interface;
using System.Net;
using System.Threading;
using Moq.Protected;
using System.Security.Policy;
using System.Reflection;

namespace LCT.SW360PackageCreator.UTest
{
[TestFixture]
public class CreatorValidatorTest
{
public Mock<ISw360ProjectService> mockISw360ProjectService;
private Mock<ISW360ApicommunicationFacade> mockISW360ApicommunicationFacade;
private Mock<ISW360Service> mockISW360Service;
private Mock<ISw360CreatorService> mockISw360CreatorService;
private Mock<IEnvironmentHelper> mockEnvironmentHelper;
private Mock<HttpMessageHandler> mockHttpMessageHandler;
private HttpClient httpClient;
public CreatorValidatorTest()
{
mockISw360ProjectService = new Mock<ISw360ProjectService>(MockBehavior.Strict);
mockISW360ApicommunicationFacade = new Mock<ISW360ApicommunicationFacade>();
mockISW360Service = new Mock<ISW360Service>();
mockISw360CreatorService = new Mock<ISw360CreatorService>();
mockEnvironmentHelper = new Mock<IEnvironmentHelper>();
mockHttpMessageHandler = new Mock<HttpMessageHandler>();
httpClient = new HttpClient(mockHttpMessageHandler.Object);

}
[TestCase]
public async Task ValidateAppSettings_TestPositive()
{
//Arrange
string projectName = "Test";
ProjectReleases projectReleases=new ProjectReleases();
ProjectReleases projectReleases = new ProjectReleases();
var CommonAppSettings = new CommonAppSettings()
{
SW360 = new SW360()
Expand All @@ -42,7 +64,7 @@ public async Task ValidateAppSettings_TestPositive()
.ReturnsAsync(projectName);

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

//Assert
mockISw360ProjectService.Verify(x => x.GetProjectNameByProjectIDFromSW360(It.IsAny<string>(), It.IsAny<string>(), projectReleases), Times.AtLeastOnce);
Expand All @@ -59,10 +81,10 @@ public async Task ValidateAppSettings_OnClosedProject_EndsTheApplication()
{
SW360 = new SW360()
{
ProjectName= "Test"
ProjectName = "Test"
}
};

mockISw360ProjectService.Setup(x => x.GetProjectNameByProjectIDFromSW360(It.IsAny<String>(), It.IsAny<string>(), projectReleases))
.ReturnsAsync(projectName);

Expand All @@ -82,9 +104,9 @@ public void ValidateAppSettings_TestNegative()
ProjectReleases projectReleases = new ProjectReleases();
var CommonAppSettings = new CommonAppSettings()
{
SW360=new SW360()
SW360 = new SW360()
};
mockISw360ProjectService.Setup(x => x.GetProjectNameByProjectIDFromSW360(It.IsAny<string>(), It.IsAny<string>(),projectReleases))
mockISw360ProjectService.Setup(x => x.GetProjectNameByProjectIDFromSW360(It.IsAny<string>(), It.IsAny<string>(), projectReleases))
.ReturnsAsync(projectName);

//Act
Expand All @@ -93,6 +115,170 @@ public void ValidateAppSettings_TestNegative()
Assert.ThrowsAsync<InvalidDataException>(() => CreatorValidator.ValidateAppSettings(CommonAppSettings, mockISw360ProjectService.Object, projectReleases));

}
[Test]
public async Task TriggerFossologyValidation_TestPositive()
{
// Arrange
var appSettings = new CommonAppSettings()
{
SW360 = new SW360()
{
URL = "https://sw360.example.com"
}
};

var responseBody = "{\"_embedded\":{\"sw360:releases\":[{\"id\":\"a3c5c9d1dd469d668433fb147c01bad2\",\"name\":\"HC-Test Pugixml\",\"version\":\"V1.2\",\"clearingState\":\"APPROVED\",\"_embedded\":{\"sw360:attachments\":[[{\"filename\":\"Protocol_Pugixml - 1.2.doc\"}]]},\"_links\":{\"self\":{\"href\":\"https://sw360.siemens.com/resource/api/releases/a3c5c9d1dd469d668433fb147c01bad2\"}}}]},\"page\":{\"totalPages\":1}}";
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.GetAllReleasesWithAllData(It.IsAny<int>(), It.IsAny<int>())).ReturnsAsync(new HttpResponseMessage
{
Content = new StringContent(responseBody)
});

mockISW360ApicommunicationFacade.Setup(x => x.TriggerFossologyProcess(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(triggerStatusResponse);

mockISw360CreatorService.Setup(x => x.TriggerFossologyProcessForValidation(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(fossTriggerStatus);

// Act
await CreatorValidator.TriggerFossologyValidation(appSettings, mockISW360ApicommunicationFacade.Object);

// Assert
mockISW360ApicommunicationFacade.Verify(x => x.GetAllReleasesWithAllData(It.IsAny<int>(), It.IsAny<int>()), Times.Once);
mockISW360ApicommunicationFacade.Verify(x => x.TriggerFossologyProcess(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public async Task FossologyUrlValidation_ValidUrl_ReturnsTrue()
{
// Arrange
var appSettings = new CommonAppSettings()
{
SW360 = new SW360()
{
Fossology = new Fossology()
{
URL = "https://stage.fossology.url"
}
}
};
var responseMessage = new HttpResponseMessage(HttpStatusCode.OK);
mockHttpMessageHandler
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync(responseMessage);
// Act
var result = await CreatorValidator.FossologyUrlValidation(appSettings, httpClient, mockEnvironmentHelper.Object);

// Assert
Assert.IsTrue(result);
}

[Test]
public async Task FossologyUrlValidation_InvalidUrl_ReturnsFalse()
{
// Arrange
var appSettings = new CommonAppSettings()
{
SW360 = new SW360()
{
Fossology = new Fossology()
{
URL = "https://invalid.fossology.url"
}
}
};

var responseMessage = new HttpResponseMessage(HttpStatusCode.NotFound);
mockHttpMessageHandler
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync(responseMessage);

// Act
var result = await CreatorValidator.FossologyUrlValidation(appSettings, httpClient, mockEnvironmentHelper.Object);

// Assert
Assert.IsFalse(result);
}


[Test]
public async Task FossologyUrlValidation_InvalidUri_ThrowsException()
{
// Arrange
var appSettings = new CommonAppSettings()
{
SW360 = new SW360()
{
Fossology = new Fossology()
{
URL = "invalid_uri"
}
}
};

var result = await CreatorValidator.FossologyUrlValidation(appSettings, httpClient, mockEnvironmentHelper.Object);

// Assert
Assert.IsFalse(result);
}

[Test]
public async Task FossologyUrlValidation_HttpRequestException_ReturnsFalse()
{
// Arrange
var appSettings = new CommonAppSettings()
{
SW360 = new SW360()
{
Fossology = new Fossology()
{
URL = "https://valid.fossology.url"
}
}
};

mockHttpMessageHandler
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
)
.ThrowsAsync(new HttpRequestException());

// Act
var result = await CreatorValidator.FossologyUrlValidation(appSettings, httpClient, mockEnvironmentHelper.Object);

// Assert
Assert.IsFalse(result);
}



}
}
Loading
Loading