Skip to content

Commit 46171b2

Browse files
committed
feat: support for licenses added.
1 parent 55a34c2 commit 46171b2

File tree

4 files changed

+275
-1
lines changed

4 files changed

+275
-1
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* new method GetGroupList().
99
* add support for groups for all methods where it did not yet exist.
1010
* new method GetInfo().
11+
* support for licenses added: new methods GetLicenseList(), GetLicense(), CreateLicense().
1112

1213
## 1.1.0 (2020-06-25)
1314
* supports FOSSology REST API v1.0.16 (FOSSology 3.8.0 built @ 2020/06/19).
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// ---------------------------------------------------------------------------
2+
// <copyright file="License.cs" company="Tethys">
3+
// Copyright (C) 2022 T. Graf
4+
// </copyright>
5+
//
6+
// Licensed under the MIT License.
7+
// SPDX-License-Identifier: MIT
8+
//
9+
// Unless required by applicable law or agreed to in writing,
10+
// software distributed under the License is distributed on an
11+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
12+
// either express or implied.
13+
// ---------------------------------------------------------------------------
14+
15+
namespace Fossology.Rest.Dotnet.Model
16+
{
17+
using Newtonsoft.Json;
18+
19+
/// <summary>
20+
/// Fossology license information.
21+
/// </summary>
22+
public class License
23+
{
24+
/// <summary>
25+
/// Gets or sets the short name.
26+
/// </summary>
27+
[JsonProperty("shortName")]
28+
public string ShortName { get; set; }
29+
30+
/// <summary>
31+
/// Gets or sets the full name.
32+
/// </summary>
33+
[JsonProperty("fullName")]
34+
public string FullName { get; set; }
35+
36+
/// <summary>
37+
/// Gets or sets the license text.
38+
/// </summary>
39+
[JsonProperty("text")]
40+
public string LicenseText { get; set; }
41+
42+
/// <summary>
43+
/// Gets or sets the url.
44+
/// </summary>
45+
[JsonProperty("url")]
46+
public string Url { get; set; }
47+
48+
/// <summary>
49+
/// Gets or sets the risk.
50+
/// </summary>
51+
[JsonProperty("risk")]
52+
public int Risk { get; set; }
53+
54+
/// <summary>
55+
/// Gets or sets a value indicating whether this is a license candidate.
56+
/// </summary>
57+
[JsonProperty("isCandidate")]
58+
public bool IsCandidate { get; set; }
59+
60+
/// <inheritdoc />
61+
public override string ToString()
62+
{
63+
return $"{this.ShortName}: {this.FullName}, parent={this.Risk}, {this.Url}, {this.IsCandidate}, '{this.LicenseText}'";
64+
}
65+
} // License
66+
}

Fossology.Rest.Dotnet.Test/FossologyClientTest.cs

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,84 @@ public void TestGetGroupList()
736736
Assert.IsTrue(actual.Count > 0);
737737
}
738738

739+
/// <summary>
740+
/// Unit test.
741+
/// </summary>
742+
[TestMethod]
743+
public void TestGetLicenseList()
744+
{
745+
var client = new FossologyClient(LocalUrl, Token);
746+
747+
// default
748+
var actual = client.GetLicenseList();
749+
Assert.IsNotNull(actual);
750+
Assert.IsTrue(actual.Count == 100);
751+
752+
actual = client.GetLicenseList(1, 1, "main", true);
753+
Assert.IsNotNull(actual);
754+
Assert.IsTrue(actual.Count == 1);
755+
}
756+
757+
/// <summary>
758+
/// Unit test.
759+
/// </summary>
760+
[TestMethod]
761+
public void TestGetLicense()
762+
{
763+
var client = new FossologyClient(LocalUrl, Token);
764+
765+
var actual = client.GetLicense("MIT");
766+
Assert.IsNotNull(actual);
767+
Assert.AreEqual("MIT", actual.ShortName);
768+
Assert.AreEqual("MIT License", actual.FullName);
769+
Assert.AreEqual(0, actual.Risk);
770+
Assert.IsFalse(actual.IsCandidate);
771+
}
772+
773+
/// <summary>
774+
/// Unit test.
775+
/// </summary>
776+
[TestMethod]
777+
public void TestCreateLicense_Fail()
778+
{
779+
var client = new FossologyClient(LocalUrl, Token);
780+
781+
var license = new License();
782+
license.ShortName = "MIT";
783+
license.FullName = "MIT License";
784+
785+
try
786+
{
787+
client.CreateLicense(license);
788+
Assert.IsFalse(true, "This must not happen!");
789+
}
790+
catch (FossologyApiException fex)
791+
{
792+
Assert.IsNotNull(fex);
793+
Assert.AreEqual(HttpStatusCode.Conflict, fex.HttpStatusCode);
794+
}
795+
}
796+
797+
/// <summary>
798+
/// Unit test.
799+
/// </summary>
800+
[TestMethod]
801+
public void TestCreateLicense()
802+
{
803+
var client = new FossologyClient(LocalUrl, Token);
804+
805+
var license = new License();
806+
license.ShortName = "TOM-MIT";
807+
license.FullName = "Tom's MIT License";
808+
license.IsCandidate = true;
809+
license.Risk = 0;
810+
license.LicenseText = "Some dummy license text";
811+
812+
var actual = client.CreateLicense(license);
813+
Assert.IsNotNull(actual);
814+
Assert.AreEqual(201, actual.Code);
815+
}
816+
739817
#if false // not yet supported by Fossology
740818
/// <summary>
741819
/// Unit test.
@@ -754,7 +832,7 @@ public void TestCreateGroup()
754832

755833
//// ---------------------------------------------------------------------
756834

757-
#region SUPPORT METHODS
835+
#region SUPPORT METHODS
758836
private static void WaitUntilUploadIsDone(FossologyClient client, int id)
759837
{
760838
while (!client.IsUploadUnpacked(id))
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// ---------------------------------------------------------------------------
2+
// <copyright file="FossologyClientLicense.cs" company="Tethys">
3+
// Copyright (C) 2019-2022 T. Graf
4+
// </copyright>
5+
//
6+
// Licensed under the MIT License.
7+
// SPDX-License-Identifier: MIT
8+
//
9+
// Unless required by applicable law or agreed to in writing,
10+
// software distributed under the License is distributed on an
11+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
12+
// either express or implied.
13+
// ---------------------------------------------------------------------------
14+
15+
namespace Fossology.Rest.Dotnet
16+
{
17+
using System.Collections.Generic;
18+
using Fossology.Rest.Dotnet.Model;
19+
20+
using Newtonsoft.Json;
21+
22+
using RestSharp;
23+
24+
using JsonSerializer = RestSharp.Serialization.Json.JsonSerializer;
25+
26+
/// <summary>
27+
/// Client for the SW360 REST API.
28+
/// </summary>
29+
public partial class FossologyClient
30+
{
31+
/// <summary>
32+
/// Gets the list of all licenses.
33+
/// </summary>
34+
/// <param name="page">The page.</param>
35+
/// <param name="limit">The limit.</param>
36+
/// <param name="kind">The license kind, one of candidate, main, all.</param>
37+
/// <param name="active">if set to <c>true</c>, return only active licenses.</param>
38+
/// <param name="groupName">The group name to choose.</param>
39+
/// <returns>A list of <see cref="License" /> objects.</returns>
40+
public IReadOnlyList<License> GetLicenseList(
41+
int page = 1,
42+
int limit = 100,
43+
string kind = "all",
44+
bool active = false,
45+
string groupName = "")
46+
{
47+
Log.Debug("Getting list of licenses...");
48+
49+
var request = new RestRequest(this.Url + $"/license?kind={kind}", Method.GET);
50+
request.RequestFormat = DataFormat.Json;
51+
request.JsonSerializer = new JsonSerializer();
52+
request.Parameters.Clear();
53+
request.AddHeader("page", page.ToString());
54+
request.AddHeader("limit", limit.ToString());
55+
request.AddHeader("active", active.ToString());
56+
57+
if (!string.IsNullOrEmpty(groupName))
58+
{
59+
request.AddHeader("groupName", groupName);
60+
} // if
61+
62+
var response = this.api.Execute(request);
63+
var list = JsonConvert.DeserializeObject<List<License>>(
64+
response.Content,
65+
new JsonSerializerSettings
66+
{
67+
NullValueHandling = NullValueHandling.Ignore,
68+
});
69+
return list;
70+
} // GetLicenseList()
71+
72+
/// <summary>
73+
/// Gets the license with the specified short name.
74+
/// </summary>
75+
/// <param name="shortName">The license short name.</param>
76+
/// <param name="groupName">The group name to choose.</param>
77+
/// <returns>A <see cref="License"/> object.</returns>
78+
public License GetLicense(string shortName, string groupName = "")
79+
{
80+
Log.Debug($"Getting license {shortName}...");
81+
82+
var request = new RestRequest(this.Url + $"/license/{shortName}", Method.GET);
83+
request.RequestFormat = DataFormat.Json;
84+
request.JsonSerializer = new JsonSerializer();
85+
request.Parameters.Clear();
86+
if (!string.IsNullOrEmpty(groupName))
87+
{
88+
request.AddHeader("groupName", groupName);
89+
} // if
90+
91+
var response = this.api.Execute(request);
92+
var license = JsonConvert.DeserializeObject<License>(
93+
response.Content,
94+
new JsonSerializerSettings
95+
{
96+
NullValueHandling = NullValueHandling.Ignore,
97+
});
98+
return license;
99+
} // GetLicense()
100+
101+
/// <summary>
102+
/// Creates a new license.
103+
/// </summary>
104+
/// <param name="newLicense">The new license to be created.</param>
105+
/// <param name="groupName">The group name to choose (optional).</param>
106+
/// <returns>An <see cref="Result" /> object.</returns>
107+
/// &gt;
108+
/// <remarks>The message property of the result contains the folder id
109+
/// which is needed for further operations.</remarks>
110+
public Result CreateLicense(License newLicense, string groupName = "")
111+
{
112+
var json = JsonConvert.SerializeObject(newLicense);
113+
var request = new RestRequest(this.Url + "/license", Method.POST);
114+
request.RequestFormat = DataFormat.Json;
115+
if (!string.IsNullOrEmpty(groupName))
116+
{
117+
request.AddHeader("groupName", groupName);
118+
} // if
119+
120+
request.AddJsonBody(json);
121+
122+
var resultRaw = this.api.Execute(request);
123+
var result = JsonConvert.DeserializeObject<Result>(resultRaw.Content);
124+
Log.Debug($"Folder {result.Message} created.");
125+
126+
return result;
127+
} // CreateLicense()
128+
} // FossologyClient
129+
}

0 commit comments

Comments
 (0)