diff --git a/src/LCT.APICommunications/Model/ProjectReleases.cs b/src/LCT.APICommunications/Model/ProjectReleases.cs index 33e4ae30..4867504e 100644 --- a/src/LCT.APICommunications/Model/ProjectReleases.cs +++ b/src/LCT.APICommunications/Model/ProjectReleases.cs @@ -23,6 +23,12 @@ public class ProjectReleases [JsonProperty("version")] public string Version { get; set; } + [JsonProperty("state")] + public string state { get; set; } + + [JsonProperty("clearingState")] + public string clearingState { get; set; } + [JsonProperty("_embedded")] public ReleaseEmbedded Embedded { get; set; } diff --git a/src/LCT.Common/ExceptionHandling.cs b/src/LCT.Common/ExceptionHandling.cs index a32eb7b8..e134863d 100644 --- a/src/LCT.Common/ExceptionHandling.cs +++ b/src/LCT.Common/ExceptionHandling.cs @@ -25,7 +25,7 @@ public static void HttpException(HttpRequestException ex, HttpResponseMessage re Logger.Logger.Log(null, Level.Error, $"The exception may be caused by an incorrect projectid or missing token for {exceptionSource} , Please ensure that a valid token is provided and try again:{ex.Message}", null); } - else if (500 <= Convert.ToInt32(ex.StatusCode) && Convert.ToInt32(ex.StatusCode) <= 599) + else if ((500 <= Convert.ToInt32(ex.StatusCode) && Convert.ToInt32(ex.StatusCode) <= 599) || ex.StatusCode == null) { Logger.Logger.Log(null, Level.Error, $"The exception may arise because {exceptionSource} is currently unresponsive:{ex.Message} Please try again later", null); } @@ -43,7 +43,7 @@ public static void FossologyException(HttpRequestException ex) } } - public static void ArgumentException( string message) + public static void ArgumentException(string message) { Logger.Logger.Log(null, Level.Error, $"Missing Arguments: Please provide the below arguments via inline or in the appSettings.json file to proceed.", null); Logger.Logger.Log(null, Level.Warn, $"{message}", null); diff --git a/src/LCT.PackageIdentifier.UTest/BomValidatorUnitTests.cs b/src/LCT.PackageIdentifier.UTest/BomValidatorUnitTests.cs index da6c7d0b..e3f20d3b 100644 --- a/src/LCT.PackageIdentifier.UTest/BomValidatorUnitTests.cs +++ b/src/LCT.PackageIdentifier.UTest/BomValidatorUnitTests.cs @@ -13,6 +13,7 @@ using System.IO; using System.Threading.Tasks; using LCT.Common; +using LCT.APICommunications.Model; namespace PackageIdentifier.UTest { @@ -27,13 +28,14 @@ public class BomValidatorUnitTests public async Task ValidateAppSettings_ProvidedProjectID_ReturnsProjectName() { //Arrange - string projectName = "Test"; + ProjectReleases projectReleases = new ProjectReleases(); + projectReleases.Name = "Test"; var CommonAppSettings = new CommonAppSettings(mockIFolderAction.Object) { SW360ProjectName = "Test" }; mockISw360ProjectService.Setup(x => x.GetProjectNameByProjectIDFromSW360(It.IsAny(), It.IsAny())) - .ReturnsAsync(projectName); + .ReturnsAsync(projectReleases); mockIFileOperations.Setup(x => x.ValidateFilePath(It.IsAny())) .Callback((string message) => { }) @@ -55,13 +57,14 @@ public async Task ValidateAppSettings_ProvidedProjectID_ReturnsProjectName() public Task ValidateAppSettings_ProvidedProjectID_ReturnsInvalidDataException() { //Arrange - string projectName = null; + ProjectReleases projectReleases = new ProjectReleases(); + projectReleases.Name = ""; var CommonAppSettings = new CommonAppSettings(mockIFolderAction.Object) { SW360ProjectName = "Test" }; mockISw360ProjectService.Setup(x => x.GetProjectNameByProjectIDFromSW360(It.IsAny(), It.IsAny())) - .ReturnsAsync(projectName); + .ReturnsAsync(projectReleases); mockIFileOperations.Setup(x => x.ValidateFilePath(It.IsAny())) .Callback((string message) => { }) diff --git a/src/LCT.PackageIdentifier/BomValidator.cs b/src/LCT.PackageIdentifier/BomValidator.cs index 29e69ed9..ed884175 100644 --- a/src/LCT.PackageIdentifier/BomValidator.cs +++ b/src/LCT.PackageIdentifier/BomValidator.cs @@ -4,10 +4,14 @@ // SPDX-License-Identifier: MIT // -------------------------------------------------------------------------------------------------------------------- +using LCT.APICommunications.Model; using LCT.Common; using LCT.Services.Interface; +using System; using System.IO; using System.Threading.Tasks; +using log4net; +using System.Reflection; namespace LCT.PackageIdentifier { @@ -16,17 +20,24 @@ 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) { - string sw360ProjectName = await bomService.GetProjectNameByProjectIDFromSW360(appSettings.SW360ProjectID, appSettings.SW360ProjectName); + ProjectReleases projectReleases = await bomService.GetProjectNameByProjectIDFromSW360(appSettings.SW360ProjectID, appSettings.SW360ProjectName); - if (string.IsNullOrEmpty(sw360ProjectName)) + if (projectReleases == null || string.IsNullOrEmpty(projectReleases.Name)) { throw new InvalidDataException($"Invalid Project Id - {appSettings.SW360ProjectID}"); } + else if (projectReleases.clearingState == "CLOSED") + { + 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."); + Environment.Exit(-1); + } else { - appSettings.SW360ProjectName = sw360ProjectName; + appSettings.SW360ProjectName = projectReleases.Name; } } } diff --git a/src/LCT.SW360PackageCreator.UTest/CreatorValidatorTest.cs b/src/LCT.SW360PackageCreator.UTest/CreatorValidatorTest.cs index e00e73a1..fb4c5560 100644 --- a/src/LCT.SW360PackageCreator.UTest/CreatorValidatorTest.cs +++ b/src/LCT.SW360PackageCreator.UTest/CreatorValidatorTest.cs @@ -12,6 +12,7 @@ using System.IO; using LCT.Common; using System.Threading.Tasks; +using LCT.APICommunications.Model; namespace SW360ComponentCreator.UTest { @@ -28,11 +29,12 @@ public CreatorValidatorTest() public async Task ValidateAppSettings_TestPositive() { //Arrange - string projectName = "Test"; + ProjectReleases projectReleases = new ProjectReleases(); + projectReleases.Name = "Test"; var CommonAppSettings = new CommonAppSettings(); CommonAppSettings.SW360ProjectName = "Test"; mockISw360ProjectService.Setup(x => x.GetProjectNameByProjectIDFromSW360(It.IsAny(), It.IsAny())) - .ReturnsAsync(projectName); + .ReturnsAsync(projectReleases); //Act await CreatorValidator.ValidateAppSettings(CommonAppSettings, mockISw360ProjectService.Object); @@ -45,12 +47,11 @@ public async Task ValidateAppSettings_TestPositive() public void ValidateAppSettings_TestNegative() { //Arrange - string projectName = null; + ProjectReleases projectReleases = new ProjectReleases(); + projectReleases.Name = null; var CommonAppSettings = new CommonAppSettings(); mockISw360ProjectService.Setup(x => x.GetProjectNameByProjectIDFromSW360(It.IsAny(), It.IsAny())) - .ReturnsAsync(projectName); - - //Act + .ReturnsAsync(projectReleases); //Assert Assert.ThrowsAsync(() => CreatorValidator.ValidateAppSettings(CommonAppSettings, mockISw360ProjectService.Object)); diff --git a/src/LCT.SW360PackageCreator/CreatorHelper.cs b/src/LCT.SW360PackageCreator/CreatorHelper.cs index 3f96207e..0513c5c5 100644 --- a/src/LCT.SW360PackageCreator/CreatorHelper.cs +++ b/src/LCT.SW360PackageCreator/CreatorHelper.cs @@ -202,16 +202,11 @@ private static async Task GetAttachmentUrlList(ComparisonBomData compone public async Task> SetContentsForComparisonBOM(List lstComponentForBOM, ISW360Service sw360Service) { Logger.Debug($"SetContentsForComparisonBOM():Start"); - List comparisonBomData = new List(); + List comparisonBomData; Logger.Logger.Log(null, Level.Notice, $"Collecting comparison BOM Data...", null); componentsAvailableInSw360 = await sw360Service.GetAvailableReleasesInSw360(lstComponentForBOM); - //Checking components count before getting status of individual comp details - if (componentsAvailableInSw360?.Count > 0) - { - comparisonBomData = await GetComparisionBomItems(lstComponentForBOM, sw360Service); - } - + comparisonBomData = await GetComparisionBomItems(lstComponentForBOM, sw360Service); Logger.Debug($"SetContentsForComparisonBOM():End"); return comparisonBomData; } diff --git a/src/LCT.SW360PackageCreator/CreatorValidator.cs b/src/LCT.SW360PackageCreator/CreatorValidator.cs index 007fe567..e941609c 100644 --- a/src/LCT.SW360PackageCreator/CreatorValidator.cs +++ b/src/LCT.SW360PackageCreator/CreatorValidator.cs @@ -8,7 +8,10 @@ using System.IO; using System.Threading.Tasks; using LCT.Common; - +using LCT.APICommunications.Model; +using System; +using log4net; +using System.Reflection; namespace LCT.SW360PackageCreator { @@ -17,17 +20,24 @@ 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) { - string sw360ProjectName = await sw360ProjectService.GetProjectNameByProjectIDFromSW360(appSettings.SW360ProjectID, appSettings.SW360ProjectName); + ProjectReleases projectReleases = await sw360ProjectService.GetProjectNameByProjectIDFromSW360(appSettings.SW360ProjectID, appSettings.SW360ProjectName); - if (string.IsNullOrEmpty(sw360ProjectName)) + if (projectReleases == null || string.IsNullOrEmpty(projectReleases.Name)) { throw new InvalidDataException($"Invalid Project Id - {appSettings.SW360ProjectID}"); } + else if (projectReleases.clearingState == "CLOSED") + { + 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."); + Environment.Exit(-1); + } else { - appSettings.SW360ProjectName = sw360ProjectName; + appSettings.SW360ProjectName = projectReleases.Name; } } } diff --git a/src/LCT.Services.UTest/Sw360ProjectServiceTest.cs b/src/LCT.Services.UTest/Sw360ProjectServiceTest.cs index 11dbe3e3..99a7030d 100644 --- a/src/LCT.Services.UTest/Sw360ProjectServiceTest.cs +++ b/src/LCT.Services.UTest/Sw360ProjectServiceTest.cs @@ -19,193 +19,193 @@ namespace LCT.Services.UTest { - [TestFixture] - public class Sw360ProjectServiceTest - { - [SetUp] - public void Setup() + [TestFixture] + public class Sw360ProjectServiceTest { - //implement + [SetUp] + public void Setup() + { + //implement + } + + [Test] + public async Task GetProjectNameByProjectIDFromSW360_InvalidSW360Credentials_HttpRequestException_ReturnsProjectResponseAsNull() + { + // Arrange + Mock sw360ApicommunicationFacadeMck = new Mock(); + sw360ApicommunicationFacadeMck.Setup(x => x.GetProjectById(It.IsAny())).Throws(); + ISw360ProjectService sw360ProjectService = new Sw360ProjectService(sw360ApicommunicationFacadeMck.Object); + + // Act + var actualProjectName = await sw360ProjectService.GetProjectNameByProjectIDFromSW360("shdjdkhsdfdkfhdhifsodo", "TestProject"); + + // Assert + Assert.IsNull(actualProjectName, "GetProjectNameByProjectIDFromSW360 does not return empty on exception"); + } + + [Test] + public async Task GetProjectNameByProjectIDFromSW360_InvalidSW360Credentials_AggregateException_ReturnsProjectResponseAsNull() + { + // Arrange + Mock sw360ApicommunicationFacadeMck = new Mock(); + sw360ApicommunicationFacadeMck.Setup(x => x.GetProjectById(It.IsAny())).Throws(); + ISw360ProjectService sw360ProjectService = new Sw360ProjectService(sw360ApicommunicationFacadeMck.Object); + + // Act + var actualProjectName = await sw360ProjectService.GetProjectNameByProjectIDFromSW360("shdjdkhsdfdkfhdhifsodo", "TestProject"); + + // Assert + Assert.IsNull(actualProjectName, "GetProjectNameByProjectIDFromSW360 does not return empty on exception"); + } + + [Test] + public async Task GetProjectNameByProjectIDFromSW360_ValidProjectIdAndName_ReturnsProjectNameAsNull() + { + // Arrange + ProjectReleases projectsMapper = new ProjectReleases(); + projectsMapper.Name = "TestProject"; + + Mock sw360ApicommunicationFacadeMck = new Mock(); + sw360ApicommunicationFacadeMck.Setup(x => x.GetProjectById(It.IsAny())).Throws(); + ISw360ProjectService sw360ProjectService = new Sw360ProjectService(sw360ApicommunicationFacadeMck.Object); + + // Act + var actualProjectName = await sw360ProjectService.GetProjectNameByProjectIDFromSW360("shdjdkhsdfdkfhdhifsodo", "TestProject"); + + // Assert + Assert.IsNull(actualProjectName, "Project Id not exist"); + } + + [Test] + public async Task GetProjectNameByProjectIDFromSW360_ValidProjectNameAndId_ReturnsTheProjectName() + { + // Arrange + string expectedName = "TestProject"; + ProjectReleases projectsMapper = new ProjectReleases(); + projectsMapper.Name = "TestProject"; + var projectDataSerialized = JsonConvert.SerializeObject(projectsMapper); + HttpResponseMessage httpResponseMessage = new HttpResponseMessage(); + var content = new StringContent(projectDataSerialized, Encoding.UTF8, "application/json"); + httpResponseMessage.Content = content; + + Mock sw360ApicommunicationFacadeMck = new Mock(); + sw360ApicommunicationFacadeMck.Setup(x => x.GetProjectById(It.IsAny())).ReturnsAsync(httpResponseMessage); + ISw360ProjectService sw360ProjectService = new Sw360ProjectService(sw360ApicommunicationFacadeMck.Object); + + // Act + var actualProjectName = await sw360ProjectService.GetProjectNameByProjectIDFromSW360("2c0a03b6d4edaf1b2ccdf64d0d0004f7", "TestProject"); + + // Assert + Assert.That(actualProjectName.Name, Is.EqualTo(expectedName), "Project Id not exist"); + } + + [Test] + public async Task GetProjectLinkedReleasesByProjectId_InvalidSW360Credentials_HttpRequestException_ReturnsEmptyList() + { + // Arrange + List expected = new List(); + Mock sw360ApicommunicationFacadeMck = new Mock(); + sw360ApicommunicationFacadeMck.Setup(x => x.GetProjectById(It.IsAny())).Throws(); + ISw360ProjectService sw360ProjectService = new Sw360ProjectService(sw360ApicommunicationFacadeMck.Object); + + // Act + List actual = await sw360ProjectService.GetAlreadyLinkedReleasesByProjectId("shdjdkhsdfdkfhdhifsodo"); + + // Assert + Assert.That(actual.Count, Is.EqualTo(expected.Count), "GetProjectLinkedReleasesByProjectId does not return empty on exception"); + } + + [Test] + public async Task GetProjectLinkedReleasesByProjectId_InvalidSW360Credentials_AggregateException_ReturnsEmptyList() + { + // Arrange + List expected = new List(); + Mock sw360ApicommunicationFacadeMck = new Mock(); + sw360ApicommunicationFacadeMck.Setup(x => x.GetProjectById(It.IsAny())).Throws(); + ISw360ProjectService sw360ProjectService = new Sw360ProjectService(sw360ApicommunicationFacadeMck.Object); + + // Act + List actual = await sw360ProjectService.GetAlreadyLinkedReleasesByProjectId("shdjdkhsdfdkfhdhifsodo"); + + // Assert + Assert.That(actual.Count, Is.EqualTo(expected.Count), "GetProjectLinkedReleasesByProjectId does not return empty on exception"); + } + + [Test] + public async Task GetProjectLinkedReleasesByProjectId_ValidProjectId_ErrorBadRequest_ReturnsEmptyList() + { + // Arrange + List expected = new List(); + HttpResponseMessage httpResponseMessage = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest); + Mock sw360ApicommunicationFacadeMck = new Mock(); + sw360ApicommunicationFacadeMck.Setup(x => x.GetProjectById(It.IsAny())).ReturnsAsync(httpResponseMessage); + ISw360ProjectService sw360ProjectService = new Sw360ProjectService(sw360ApicommunicationFacadeMck.Object); + + // Act + List actual = await sw360ProjectService.GetAlreadyLinkedReleasesByProjectId("shdjdkhsdfdkfhdhifsodo"); + + // Assert + Assert.That(actual.Count, Is.EqualTo(expected.Count), "GetProjectLinkedReleasesByProjectId does not return empty on exception"); + } + + [Test] + public async Task GetProjectLinkedReleasesByProjectId_ValidProjectId_ReturnsReleasesLinked() + { + // Arrange + Self self = new Self() { Href = "http://md2pdvnc:8095/resource/api/releases/ff8d19674e737371be578cafec0c663e" }; + Links links = new Links() { Self = self }; + Sw360Releases sw360Releases = new Sw360Releases() { Name = "tslib", Version = "2.2.0", Links = links }; + ProjectReleases projectReleases = new ProjectReleases(); + projectReleases.Embedded = new ReleaseEmbedded() { Sw360Releases = new List() { sw360Releases } }; + + + List expected = new List(); + HttpResponseMessage httpResponseMessage = new HttpResponseMessage(System.Net.HttpStatusCode.OK); + httpResponseMessage.Content = new ObjectContent(projectReleases, new JsonMediaTypeFormatter(), "application/some-format"); + + Mock sW360ApicommunicationFacadeMck = new Mock(); + sW360ApicommunicationFacadeMck.Setup(x => x.GetProjectById(It.IsAny())).ReturnsAsync(httpResponseMessage); + ISw360ProjectService sw360ProjectService = new Sw360ProjectService(sW360ApicommunicationFacadeMck.Object); + + // Act + List actual = await sw360ProjectService.GetAlreadyLinkedReleasesByProjectId("shdjdkhsdfdkfhdhifsodo"); + + // Assert + Assert.That(actual.Count, Is.EqualTo(expected.Count), "GetProjectLinkedReleasesByProjectId does not return empty on exception"); + } + + [Test] + public async Task GetAlreadyLinkedReleasesByProjectId_PassProjectId_SuccessFullyReturnsReleaseLinked() + { + // Arrange + Self self = new Self() { Href = "http://md2pdvnc:8095/resource/api/releases/ff8d19674e737371be578cafec0c663e" }; + Links links = new Links() { Self = self }; + + Sw360Releases sw360Releases = new Sw360Releases() { Name = "tslib", Version = "2.2.0", Links = links }; + Sw360LinkedRelease sw360LinkedRelease = new Sw360LinkedRelease(); + sw360LinkedRelease.Release = "http://md2pdvnc:8095/resource/api/releases/ff8d19674e737371be578cafec0c663e"; + List sw360LinkedReleases = new List(); + sw360LinkedReleases.Add(sw360LinkedRelease); + ProjectReleases projectReleases = new ProjectReleases(); + projectReleases.Embedded = + new ReleaseEmbedded() { Sw360Releases = new List() { sw360Releases } }; + projectReleases.LinkedReleases = sw360LinkedReleases; + + + List expected = new List(); + HttpResponseMessage httpResponseMessage = new HttpResponseMessage(System.Net.HttpStatusCode.OK); + httpResponseMessage.Content = new ObjectContent(projectReleases, new JsonMediaTypeFormatter(), "application/some-format"); + + Mock sW360ApicommunicationFacadeMck = new Mock(); + sW360ApicommunicationFacadeMck.Setup(x => x.GetProjectById(It.IsAny())).ReturnsAsync(httpResponseMessage); + ISw360ProjectService sw360ProjectService = new Sw360ProjectService(sW360ApicommunicationFacadeMck.Object); + + // Act + List actual = await sw360ProjectService.GetAlreadyLinkedReleasesByProjectId("shdjdkhsdfdkfhdhifsodo"); + + + // Assert + Assert.That(actual.Count, Is.GreaterThan(0)); + } } - - [Test] - public async Task GetProjectNameByProjectIDFromSW360_InvalidSW360Credentials_HttpRequestException_ReturnsProjectNameAsEmpty() - { - // Arrange - Mock sw360ApicommunicationFacadeMck = new Mock(); - sw360ApicommunicationFacadeMck.Setup(x => x.GetProjectById(It.IsAny())).Throws(); - ISw360ProjectService sw360ProjectService = new Sw360ProjectService(sw360ApicommunicationFacadeMck.Object); - - // Act - var actualProjectName = await sw360ProjectService.GetProjectNameByProjectIDFromSW360("shdjdkhsdfdkfhdhifsodo", "TestProject"); - - // Assert - Assert.That(actualProjectName, Is.EqualTo(string.Empty), "GetProjectNameByProjectIDFromSW360 does not return empty on exception"); - } - - [Test] - public async Task GetProjectNameByProjectIDFromSW360_InvalidSW360Credentials_AggregateException_ReturnsProjectNameAsEmpty() - { - // Arrange - Mock sw360ApicommunicationFacadeMck = new Mock(); - sw360ApicommunicationFacadeMck.Setup(x => x.GetProjectById(It.IsAny())).Throws(); - ISw360ProjectService sw360ProjectService = new Sw360ProjectService(sw360ApicommunicationFacadeMck.Object); - - // Act - var actualProjectName = await sw360ProjectService.GetProjectNameByProjectIDFromSW360("shdjdkhsdfdkfhdhifsodo", "TestProject"); - - // Assert - Assert.That(actualProjectName, Is.EqualTo(string.Empty), "GetProjectNameByProjectIDFromSW360 does not return empty on exception"); - } - - [Test] - public async Task GetProjectNameByProjectIDFromSW360_ValidProjectIdAndName_ReturnsProjectNameAsEmpty() - { - // Arrange - ProjectReleases projectsMapper = new ProjectReleases(); - projectsMapper.Name = "TestProject"; - - Mock sw360ApicommunicationFacadeMck = new Mock(); - sw360ApicommunicationFacadeMck.Setup(x => x.GetProjectById(It.IsAny())).Throws(); - ISw360ProjectService sw360ProjectService = new Sw360ProjectService(sw360ApicommunicationFacadeMck.Object); - - // Act - var actualProjectName = await sw360ProjectService.GetProjectNameByProjectIDFromSW360("shdjdkhsdfdkfhdhifsodo", "TestProject"); - - // Assert - Assert.That(actualProjectName, Is.EqualTo(string.Empty), "Project Id not exist"); - } - - [Test] - public async Task GetProjectNameByProjectIDFromSW360_ValidProjectNameAndId_ReturnsTheProjectName() - { - // Arrange - string expectedName = "TestProject"; - ProjectReleases projectsMapper = new ProjectReleases(); - projectsMapper.Name = "TestProject"; - var projectDataSerialized = JsonConvert.SerializeObject(projectsMapper); - HttpResponseMessage httpResponseMessage = new HttpResponseMessage(); - var content = new StringContent(projectDataSerialized, Encoding.UTF8, "application/json"); - httpResponseMessage.Content = content; - - Mock sw360ApicommunicationFacadeMck = new Mock(); - sw360ApicommunicationFacadeMck.Setup(x => x.GetProjectById(It.IsAny())).ReturnsAsync(httpResponseMessage); - ISw360ProjectService sw360ProjectService = new Sw360ProjectService(sw360ApicommunicationFacadeMck.Object); - - // Act - var actualProjectName = await sw360ProjectService.GetProjectNameByProjectIDFromSW360("2c0a03b6d4edaf1b2ccdf64d0d0004f7", "TestProject"); - - // Assert - Assert.That(actualProjectName, Is.EqualTo(expectedName), "Project Id not exist"); - } - - [Test] - public async Task GetProjectLinkedReleasesByProjectId_InvalidSW360Credentials_HttpRequestException_ReturnsEmptyList() - { - // Arrange - List expected = new List(); - Mock sw360ApicommunicationFacadeMck = new Mock(); - sw360ApicommunicationFacadeMck.Setup(x => x.GetProjectById(It.IsAny())).Throws(); - ISw360ProjectService sw360ProjectService = new Sw360ProjectService(sw360ApicommunicationFacadeMck.Object); - - // Act - List actual = await sw360ProjectService.GetAlreadyLinkedReleasesByProjectId("shdjdkhsdfdkfhdhifsodo"); - - // Assert - Assert.That(actual.Count, Is.EqualTo(expected.Count), "GetProjectLinkedReleasesByProjectId does not return empty on exception"); - } - - [Test] - public async Task GetProjectLinkedReleasesByProjectId_InvalidSW360Credentials_AggregateException_ReturnsEmptyList() - { - // Arrange - List expected = new List(); - Mock sw360ApicommunicationFacadeMck = new Mock(); - sw360ApicommunicationFacadeMck.Setup(x => x.GetProjectById(It.IsAny())).Throws(); - ISw360ProjectService sw360ProjectService = new Sw360ProjectService(sw360ApicommunicationFacadeMck.Object); - - // Act - List actual = await sw360ProjectService.GetAlreadyLinkedReleasesByProjectId("shdjdkhsdfdkfhdhifsodo"); - - // Assert - Assert.That(actual.Count, Is.EqualTo(expected.Count), "GetProjectLinkedReleasesByProjectId does not return empty on exception"); - } - - [Test] - public async Task GetProjectLinkedReleasesByProjectId_ValidProjectId_ErrorBadRequest_ReturnsEmptyList() - { - // Arrange - List expected = new List(); - HttpResponseMessage httpResponseMessage = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest); - Mock sw360ApicommunicationFacadeMck = new Mock(); - sw360ApicommunicationFacadeMck.Setup(x => x.GetProjectById(It.IsAny())).ReturnsAsync(httpResponseMessage); - ISw360ProjectService sw360ProjectService = new Sw360ProjectService(sw360ApicommunicationFacadeMck.Object); - - // Act - List actual = await sw360ProjectService.GetAlreadyLinkedReleasesByProjectId("shdjdkhsdfdkfhdhifsodo"); - - // Assert - Assert.That(actual.Count, Is.EqualTo(expected.Count), "GetProjectLinkedReleasesByProjectId does not return empty on exception"); - } - - [Test] - public async Task GetProjectLinkedReleasesByProjectId_ValidProjectId_ReturnsReleasesLinked() - { - // Arrange - Self self = new Self() { Href = "http://md2pdvnc:8095/resource/api/releases/ff8d19674e737371be578cafec0c663e" }; - Links links = new Links() { Self = self }; - Sw360Releases sw360Releases = new Sw360Releases() { Name = "tslib", Version = "2.2.0", Links = links }; - ProjectReleases projectReleases = new ProjectReleases(); - projectReleases.Embedded = new ReleaseEmbedded() { Sw360Releases = new List() { sw360Releases } }; - - - List expected = new List(); - HttpResponseMessage httpResponseMessage = new HttpResponseMessage(System.Net.HttpStatusCode.OK); - httpResponseMessage.Content = new ObjectContent(projectReleases, new JsonMediaTypeFormatter(), "application/some-format"); - - Mock sW360ApicommunicationFacadeMck = new Mock(); - sW360ApicommunicationFacadeMck.Setup(x => x.GetProjectById(It.IsAny())).ReturnsAsync(httpResponseMessage); - ISw360ProjectService sw360ProjectService = new Sw360ProjectService(sW360ApicommunicationFacadeMck.Object); - - // Act - List actual = await sw360ProjectService.GetAlreadyLinkedReleasesByProjectId("shdjdkhsdfdkfhdhifsodo"); - - // Assert - Assert.That(actual.Count, Is.EqualTo(expected.Count), "GetProjectLinkedReleasesByProjectId does not return empty on exception"); - } - - [Test] - public async Task GetAlreadyLinkedReleasesByProjectId_PassProjectId_SuccessFullyReturnsReleaseLinked() - { - // Arrange - Self self = new Self() { Href = "http://md2pdvnc:8095/resource/api/releases/ff8d19674e737371be578cafec0c663e" }; - Links links = new Links() { Self = self }; - - Sw360Releases sw360Releases = new Sw360Releases() { Name = "tslib", Version = "2.2.0", Links = links }; - Sw360LinkedRelease sw360LinkedRelease = new Sw360LinkedRelease(); - sw360LinkedRelease.Release = "http://md2pdvnc:8095/resource/api/releases/ff8d19674e737371be578cafec0c663e"; - List sw360LinkedReleases = new List(); - sw360LinkedReleases.Add(sw360LinkedRelease); - ProjectReleases projectReleases = new ProjectReleases(); - projectReleases.Embedded = - new ReleaseEmbedded() { Sw360Releases = new List() { sw360Releases } }; - projectReleases.LinkedReleases = sw360LinkedReleases; - - - List expected = new List(); - HttpResponseMessage httpResponseMessage = new HttpResponseMessage(System.Net.HttpStatusCode.OK); - httpResponseMessage.Content = new ObjectContent(projectReleases, new JsonMediaTypeFormatter(), "application/some-format"); - - Mock sW360ApicommunicationFacadeMck = new Mock(); - sW360ApicommunicationFacadeMck.Setup(x => x.GetProjectById(It.IsAny())).ReturnsAsync(httpResponseMessage); - ISw360ProjectService sw360ProjectService = new Sw360ProjectService(sW360ApicommunicationFacadeMck.Object); - - // Act - List actual = await sw360ProjectService.GetAlreadyLinkedReleasesByProjectId("shdjdkhsdfdkfhdhifsodo"); - - - // Assert - Assert.That(actual.Count, Is.GreaterThan(0)); - } - } } diff --git a/src/LCT.Services.UTest/Sw360ServiceTest.cs b/src/LCT.Services.UTest/Sw360ServiceTest.cs index 6ede5fc2..cad45553 100644 --- a/src/LCT.Services.UTest/Sw360ServiceTest.cs +++ b/src/LCT.Services.UTest/Sw360ServiceTest.cs @@ -42,10 +42,10 @@ public async Task GetProjectNameByProjectIDFromSW360_ProvidedProjectIdReturnsPro // Act ISw360ProjectService sw360Service = new Sw360ProjectService(swApiCommunicationFacade.Object); - string sw360ProjectName = await sw360Service.GetProjectNameByProjectIDFromSW360("4aa1165e2d23da3d383692eb9c000a43", "Test"); + ProjectReleases projectReleases = await sw360Service.GetProjectNameByProjectIDFromSW360("4aa1165e2d23da3d383692eb9c000a43", "Test"); // Assert - Assert.AreEqual("Test", sw360ProjectName); + Assert.AreEqual("Test", projectReleases.Name); } @@ -60,7 +60,7 @@ public async Task GetAvailableReleasesInSw360_ForGivenData_Returns0results() componentRelease.Embedded = new ReleaseEmbedded(); Mock swApiCommunicationFacade = new Mock(); swApiCommunicationFacade.Setup(x => x.GetReleases()).ReturnsAsync(string.Empty); - + // Act ISW360Service sW360Service = new Sw360Service(swApiCommunicationFacade.Object); var result = await sW360Service.GetAvailableReleasesInSw360(components); diff --git a/src/LCT.Services/Interface/ISw360ProjectService.cs b/src/LCT.Services/Interface/ISw360ProjectService.cs index b5eac164..3f4033f9 100644 --- a/src/LCT.Services/Interface/ISw360ProjectService.cs +++ b/src/LCT.Services/Interface/ISw360ProjectService.cs @@ -21,7 +21,7 @@ public interface ISw360ProjectService /// projectId /// projectName /// string - Task GetProjectNameByProjectIDFromSW360(string projectId, string projectName); + Task GetProjectNameByProjectIDFromSW360(string projectId, string projectName); Task> GetAlreadyLinkedReleasesByProjectId(string projectId); } diff --git a/src/LCT.Services/Sw360CommonService.cs b/src/LCT.Services/Sw360CommonService.cs index 3193642a..afd7cb3a 100644 --- a/src/LCT.Services/Sw360CommonService.cs +++ b/src/LCT.Services/Sw360CommonService.cs @@ -121,15 +121,6 @@ private async Task> GetCompListFromExternalIDCombinations public async Task GetReleaseDataByExternalId(string releaseName, string releaseVersion, string releaseExternalId) { Logger.Debug($"GetReleaseDataByExternalId(): Release name - {releaseName}@{releaseVersion}"); - string externalIdUriString; - if (releaseExternalId.Contains(Dataconstant.PurlCheck()["NPM"])) - { - externalIdUriString = Uri.EscapeDataString(releaseExternalId); - } - else - { - externalIdUriString = releaseExternalId; - } Releasestatus releasestatus = new Releasestatus(); releasestatus.isReleaseExist = false; @@ -138,11 +129,21 @@ public async Task GetReleaseDataByExternalId(string releaseName, { foreach (string externalIdKey in externalIdKeyList) { - HttpResponseMessage httpResponseComponent = await m_SW360ApiCommunicationFacade.GetReleaseByExternalId(externalIdUriString, externalIdKey); + HttpResponseMessage httpResponseComponent = await m_SW360ApiCommunicationFacade.GetReleaseByExternalId(releaseExternalId, externalIdKey); var responseContent = httpResponseComponent?.Content?.ReadAsStringAsync()?.Result ?? string.Empty; var componentsRelease = JsonConvert.DeserializeObject(responseContent); var sw360releasesdata = componentsRelease?.Embedded?.Sw360Releases ?? new List(); + //It's for Local Sw360 servers,making an API call with EscapeDataString.. + if (sw360releasesdata.Count == 0 && releaseExternalId.Contains(Dataconstant.PurlCheck()["NPM"])) + { + releaseExternalId = Uri.EscapeDataString(releaseExternalId); + httpResponseComponent = await m_SW360ApiCommunicationFacade.GetReleaseByExternalId(releaseExternalId, externalIdKey); + responseContent = httpResponseComponent?.Content?.ReadAsStringAsync()?.Result ?? string.Empty; + componentsRelease = JsonConvert.DeserializeObject(responseContent); + sw360releasesdata = componentsRelease?.Embedded?.Sw360Releases ?? new List(); + } + if (sw360releasesdata.Count > 0) { Releasestatus releaseStatus = GetReleaseExistStatus(releaseName, externalIdKey, sw360releasesdata); diff --git a/src/LCT.Services/Sw360ProjectService.cs b/src/LCT.Services/Sw360ProjectService.cs index 0dd5b48d..40d699a1 100644 --- a/src/LCT.Services/Sw360ProjectService.cs +++ b/src/LCT.Services/Sw360ProjectService.cs @@ -39,24 +39,22 @@ public Sw360ProjectService(ISW360ApicommunicationFacade sw360ApiCommunicationFac /// projectId /// projectName /// string - public async Task GetProjectNameByProjectIDFromSW360(string projectId, string projectName) + public async Task GetProjectNameByProjectIDFromSW360(string projectId, string projectName) { - string sw360ProjectName = string.Empty; + ProjectReleases projectReleases = null; try { var response = await m_SW360ApiCommunicationFacade.GetProjectById(projectId); if (response == null || !response.IsSuccessStatusCode) { - return string.Empty; + return projectReleases; } - string result = response.Content?.ReadAsStringAsync()?.Result ?? string.Empty; + if (!string.IsNullOrEmpty(result)) { - var projectInfo = JsonConvert.DeserializeObject(result); - sw360ProjectName = projectInfo?.Name; - + projectReleases = JsonConvert.DeserializeObject(result); } } catch (HttpRequestException ex) @@ -72,7 +70,7 @@ public async Task GetProjectNameByProjectIDFromSW360(string projectId, s Environment.ExitCode = -1; } - return sw360ProjectName; + return projectReleases; } public async Task> GetAlreadyLinkedReleasesByProjectId(string projectId)