Skip to content

Commit ca91690

Browse files
Decoupling SW360 and Jfrog connection Code changes
1 parent 061f121 commit ca91690

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+401
-106
lines changed

src/LCT.Common/CommonAppSettings.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public string ProjectType
6262
}
6363
}
6464
public bool MultipleProjectType { get; set; } = false;
65+
public bool BasicSBOM { get; set; } = true;
6566
public Telemetry Telemetry { get; set; }
6667
public SW360 SW360 { get; set; }
6768
public Directory Directory { get; set; }
@@ -117,7 +118,7 @@ public string URL
117118
{
118119
if (string.IsNullOrEmpty(value))
119120
{
120-
throw new ArgumentNullException($"Provide a sw360 url - {value}");
121+
121122
}
122123
else
123124
{
@@ -133,7 +134,6 @@ public string ProjectName
133134
}
134135
set
135136
{
136-
CommonHelper.CheckNullOrEmpty(nameof(ProjectName), value);
137137
m_ProjectName = value;
138138
}
139139
}
@@ -145,7 +145,6 @@ public string ProjectID
145145
}
146146
set
147147
{
148-
CommonHelper.CheckNullOrEmpty(nameof(ProjectID), value);
149148
m_ProjectID = value;
150149
}
151150
}
@@ -157,8 +156,7 @@ public string Token
157156
return m_Token;
158157
}
159158
set
160-
{
161-
CommonHelper.CheckNullOrEmpty(nameof(Token), value);
159+
{
162160
m_Token = value;
163161
}
164162
}

src/LCT.Common/CommonHelper.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public static void WriteToConsoleTable(Dictionary<string, int> printData, Dictio
117117
Logger.Info("Summary :\n");
118118
if (!string.IsNullOrWhiteSpace(ProjectSummaryLink)) { Logger.Info($"{ProjectSummaryLink}"); }
119119
Logger.Info($"{"=",5}{string.Join("", Enumerable.Repeat("=", 88)),5}");
120-
Logger.Info($"{"|",5}{Feature,70} {"|",5} {Count,5} {"|",5}");
120+
Logger.Info($"{"|",5}{Feature,-70} {"|",5} {Count,5} {"|",5}");
121121
Logger.Info($"{"=",5}{string.Join("", Enumerable.Repeat("=", 88)),5}");
122122
foreach (var item in printData)
123123
{
@@ -253,8 +253,8 @@ public static string AddSpecificValuesToBOMFormat(Bom listOfComponentsToBom)
253253

254254
return formattedString;
255255
}
256-
257-
256+
257+
258258
public static string[] GetRepoList(CommonAppSettings appSettings)
259259
{
260260
var projectTypeMappings = new Dictionary<string, Func<Artifactory>>
@@ -281,7 +281,7 @@ public static string[] GetRepoList(CommonAppSettings appSettings)
281281
}
282282

283283
return Array.Empty<string>();
284-
}
284+
}
285285
#endregion
286286

287287
#region private

src/LCT.Common/Constants/FileConstant.cs

+1
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@ public static class FileConstant
5353
public const string NugetAssetFile = "project.assets.json";
5454
public const string multipleversionsFileName = "Multipleversions.json";
5555
public const string artifactoryReportNotApproved = "ReportNotApproved.json";
56+
public const string basicSBOMName = "CycloneDX";
5657
}
5758
}

src/LCT.Common/SettingsManager.cs

+10-7
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,23 @@ public void CheckRequiredArgsToRun(CommonAppSettings appSettings, string current
121121
{
122122
//Required parameters to run Package Identifier
123123
List<string> identifierReqParameters = new List<string>()
124-
{
125-
"SW360.ProjectID",
126-
"SW360.Token",
127-
"Jfrog.Token",
128-
"SW360.URL",
129-
"Jfrog.URL",
124+
{
130125
"Directory.InputFolder",
131126
"Directory.OutputFolder",
132127
"ProjectType"
133128
};
129+
if (!appSettings.BasicSBOM)
130+
{
131+
identifierReqParameters.Add($"SW360.ProjectID");
132+
identifierReqParameters.Add($"SW360.Token");
133+
identifierReqParameters.Add($"Jfrog.Token");
134+
identifierReqParameters.Add($"SW360.URL");
135+
identifierReqParameters.Add($"Jfrog.URL");
136+
}
134137
//Check if ProjectType contains a value and add InternalRepos key accordingly
135138
if (!string.IsNullOrWhiteSpace(appSettings.ProjectType))
136139
{
137-
if (!appSettings.ProjectType.Equals("ALPINE", StringComparison.InvariantCultureIgnoreCase))
140+
if (!appSettings.BasicSBOM && !appSettings.ProjectType.Equals("ALPINE", StringComparison.InvariantCultureIgnoreCase))
138141
{
139142
identifierReqParameters.Add($"{appSettings.ProjectType}.Artifactory.InternalRepos");
140143
}

src/LCT.Common/appSettings.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
{
88
"TimeOut": 400,
9+
"BasicSBOM": true,
910
"ProjectType": "<Insert ProjectType>",
1011
"MultipleProjectType": false,
1112
"Telemetry": {

src/LCT.PackageIdentifier.UTest/CommonIdentiferHelperTests.cs

+67
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using LCT.APICommunications.Model.AQL;
99
using System.Collections.Generic;
1010
using LCT.PackageIdentifier;
11+
using LCT.Common.Constants;
12+
using LCT.Common;
1113

1214
namespace LCT.PackageIdentifier.UTest
1315
{
@@ -66,5 +68,70 @@ public void GetRepodetailsFromPerticularOrder_NoSpecificRepo_ReturnsFirstRepo()
6668
var result = CommonIdentiferHelper.GetRepodetailsFromPerticularOrder(aqlResults);
6769
Assert.AreEqual("generic-repo", result);
6870
}
71+
[Test]
72+
public void GetBomFileName_WhenBasicSBOMIsFalse_ReturnsProjectNameBomFileName()
73+
{
74+
// Arrange
75+
var appSettings = new CommonAppSettings
76+
{
77+
BasicSBOM = false,
78+
SW360=new SW360() { ProjectName= "TestProject" }
79+
};
80+
81+
// Act
82+
string result = CommonIdentiferHelper.GetBomFileName(appSettings);
83+
84+
// Assert
85+
Assert.AreEqual("TestProject_Bom.cdx.json", result);
86+
}
87+
88+
[Test]
89+
public void GetBomFileName_WhenBasicSBOMIsTrue_ReturnsBasicSBOMNameBomFileName()
90+
{
91+
// Arrange
92+
var appSettings = new CommonAppSettings
93+
{
94+
BasicSBOM = true
95+
};
96+
97+
// Act
98+
string result = CommonIdentiferHelper.GetBomFileName(appSettings);
99+
100+
// Assert
101+
Assert.AreEqual($"{FileConstant.basicSBOMName}_Bom.cdx.json", result);
102+
}
103+
104+
[Test]
105+
public void GetDefaultProjectName_WhenBasicSBOMIsFalse_ReturnsProjectName()
106+
{
107+
// Arrange
108+
var appSettings = new CommonAppSettings
109+
{
110+
BasicSBOM = false,
111+
SW360 = new SW360() { ProjectName = "TestProject" }
112+
};
113+
114+
// Act
115+
string result = CommonIdentiferHelper.GetDefaultProjectName(appSettings);
116+
117+
// Assert
118+
Assert.AreEqual("TestProject", result);
119+
}
120+
121+
[Test]
122+
public void GetDefaultProjectName_WhenBasicSBOMIsTrue_ReturnsBasicSBOMName()
123+
{
124+
// Arrange
125+
var appSettings = new CommonAppSettings
126+
{
127+
BasicSBOM = true
128+
};
129+
130+
// Act
131+
string result = CommonIdentiferHelper.GetDefaultProjectName(appSettings);
132+
133+
// Assert
134+
Assert.AreEqual(FileConstant.basicSBOMName, result);
135+
}
69136
}
70137
}

src/LCT.PackageIdentifier.UTest/DisplayInformationTests.cs

+132-1
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,152 @@
11
using LCT.Common;
2+
using LCT.Common.Interface;
23
using LCT.Common.Model;
4+
using log4net;
5+
using log4net.Appender;
6+
using log4net.Config;
37
using NUnit.Framework;
8+
using System;
49
using System.Collections.Generic;
10+
using System.IO;
511

6-
namespace LCT.PackageIdentifier.Tests
12+
namespace LCT.PackageIdentifier.UTest
713
{
814
[TestFixture]
915
public class DisplayInformationTests
1016
{
1117
private CommonAppSettings appSettings;
18+
private CatoolInfo caToolInformation;
19+
private MemoryAppender memoryAppender;
1220

1321
[SetUp]
1422
public void Setup()
1523
{
1624
appSettings = new CommonAppSettings();
1725
}
26+
27+
[Test]
28+
public void LogInputParameters_ShouldLogCorrectMessage_WhenBasicSBOMIsFalse()
29+
{
30+
// Arrange
31+
string listOfInternalRepoList = "repo1,repo2";
32+
string listOfInclude = "include1,include2";
33+
string listOfExclude = "exclude1,exclude2";
34+
string listOfExcludeComponents = "component1,component2";
35+
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
36+
string OutFolder = Path.GetDirectoryName(exePath);
37+
IFolderAction folderAction = new FolderAction();
38+
IFileOperations fileOperations = new FileOperations();
39+
CommonAppSettings appSettings = new CommonAppSettings(folderAction, fileOperations)
40+
{
41+
ProjectType = "NPM",
42+
BasicSBOM = false,
43+
SW360 = new SW360()
44+
{
45+
URL = "http://sw360.url",
46+
AuthTokenType = "Bearer",
47+
ProjectName = "ProjectName",
48+
ProjectID = "ProjectID",
49+
ExcludeComponents = new List<string> { "component1", "component2" }
50+
},
1851

52+
Directory = new LCT.Common.Directory(folderAction, fileOperations)
53+
{
54+
InputFolder = OutFolder + @"\PackageIdentifierUTTestFiles",
55+
OutputFolder = OutFolder + @"\PackageIdentifierUTTestFiles"
56+
}
57+
};
58+
caToolInformation = new CatoolInfo
59+
{
60+
CatoolVersion = "1.0.0",
61+
CatoolRunningLocation = "runningLocation"
62+
};
63+
64+
memoryAppender = new MemoryAppender();
65+
BasicConfigurator.Configure(memoryAppender);
66+
// Act
67+
DisplayInformation.LogInputParameters(caToolInformation, appSettings, listOfInternalRepoList, listOfInclude, listOfExclude, listOfExcludeComponents);
68+
69+
// Assert
70+
string expectedLogMessage = $"Input Parameters used in Package Identifier:\n\t" +
71+
$"CaToolVersion\t\t --> {caToolInformation.CatoolVersion}\n\t" +
72+
$"CaToolRunningPath\t --> {caToolInformation.CatoolRunningLocation}\n\t" +
73+
$"PackageFilePath\t\t --> {appSettings.Directory.InputFolder}\n\t" +
74+
$"BomFolderPath\t\t --> {appSettings.Directory.OutputFolder}\n\t" +
75+
$"SW360Url\t\t --> {appSettings.SW360.URL}\n\t" +
76+
$"SW360AuthTokenType\t --> {appSettings.SW360.AuthTokenType}\n\t" +
77+
$"SW360ProjectName\t --> {appSettings.SW360.ProjectName}\n\t" +
78+
$"SW360ProjectID\t\t --> {appSettings.SW360.ProjectID}\n\t" +
79+
$"InternalRepoList\t --> {listOfInternalRepoList}\n\t" +
80+
$"ProjectType\t\t --> {appSettings.ProjectType}\n\t" +
81+
$"LogFolderPath\t\t --> {Log4Net.CatoolLogPath}\n\t" +
82+
$"Include\t\t\t --> {listOfInclude}\n\t" +
83+
$"Exclude\t\t\t --> {listOfExclude}\n\t" +
84+
$"ExcludeComponents\t --> {listOfExcludeComponents}\n";
85+
86+
var logEvents = memoryAppender.GetEvents();
87+
Assert.IsNotEmpty(logEvents);
88+
var actualLogMessage = logEvents[0].RenderedMessage;
89+
Assert.AreEqual(expectedLogMessage, actualLogMessage);
90+
}
91+
[Test]
92+
public void LogInputParameters_ShouldLogCorrectMessage_WhenBasicSBOMIsTrue()
93+
{
94+
// Arrange
95+
string listOfInternalRepoList = "repo1,repo2";
96+
string listOfInclude = "include1,include2";
97+
string listOfExclude = "exclude1,exclude2";
98+
string listOfExcludeComponents = "component1,component2";
99+
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
100+
string OutFolder = Path.GetDirectoryName(exePath);
101+
IFolderAction folderAction = new FolderAction();
102+
IFileOperations fileOperations = new FileOperations();
103+
CommonAppSettings appSettings = new CommonAppSettings(folderAction, fileOperations)
104+
{
105+
ProjectType = "NPM",
106+
BasicSBOM = true,
107+
SW360 = new SW360()
108+
{
109+
URL = "http://sw360.url",
110+
AuthTokenType = "Bearer",
111+
ProjectName = "ProjectName",
112+
ProjectID = "ProjectID",
113+
ExcludeComponents = new List<string> { "component1", "component2" }
114+
},
115+
116+
Directory = new LCT.Common.Directory(folderAction, fileOperations)
117+
{
118+
InputFolder = OutFolder + @"\PackageIdentifierUTTestFiles",
119+
OutputFolder = OutFolder + @"\PackageIdentifierUTTestFiles"
120+
}
121+
};
122+
caToolInformation = new CatoolInfo
123+
{
124+
CatoolVersion = "1.0.0",
125+
CatoolRunningLocation = "runningLocation"
126+
};
127+
128+
memoryAppender = new MemoryAppender();
129+
BasicConfigurator.Configure(memoryAppender);
130+
// Act
131+
DisplayInformation.LogInputParameters(caToolInformation, appSettings, listOfInternalRepoList, listOfInclude, listOfExclude, listOfExcludeComponents);
132+
133+
// Assert
134+
string expectedLogMessage = $"Input Parameters used in Package Identifier:\n\t" +
135+
$"CaToolVersion\t\t --> {caToolInformation.CatoolVersion}\n\t" +
136+
$"CaToolRunningPath\t --> {caToolInformation.CatoolRunningLocation}\n\t" +
137+
$"PackageFilePath\t\t --> {appSettings.Directory.InputFolder}\n\t" +
138+
$"BomFolderPath\t\t --> {appSettings.Directory.OutputFolder}\n\t" +
139+
$"ProjectType\t\t --> {appSettings.ProjectType}\n\t" +
140+
$"LogFolderPath\t\t --> {Log4Net.CatoolLogPath}\n\t" +
141+
$"Include\t\t\t --> {listOfInclude}\n\t" +
142+
$"Exclude\t\t\t --> {listOfExclude}\n\t" +
143+
$"ExcludeComponents\t --> {listOfExcludeComponents}\n";
144+
145+
var logEvents = memoryAppender.GetEvents();
146+
Assert.IsNotEmpty(logEvents);
147+
var actualLogMessage = logEvents[0].RenderedMessage;
148+
Assert.AreEqual(expectedLogMessage, actualLogMessage);
149+
}
19150
[TestCase("NPM", "p*-lock.json,*.cdx.json", "node_modules,Test", "Npm-Test")]
20151
[TestCase("NUGET", "p*-lock.json,*.cdx.json", "package,Test", "Nuget-Test")]
21152
[TestCase("MAVEN", "*.cdx.json", "package,Test", "Maven-Test")]

0 commit comments

Comments
 (0)