Skip to content

Commit cfdbe01

Browse files
committed
feat: searching for files now returns the expected results
1 parent 2cda879 commit cfdbe01

File tree

7 files changed

+135
-30
lines changed

7 files changed

+135
-30
lines changed

Fossology.Rest.Dotnet.Model/File.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// ---------------------------------------------------------------------------
2+
// <copyright file="File.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 System.Collections.Generic;
18+
using Newtonsoft.Json;
19+
20+
/// <summary>
21+
/// Fossology file information as returned by searching for files.
22+
/// </summary>
23+
public class File
24+
{
25+
/// <summary>
26+
/// Gets or sets the hash.
27+
/// </summary>
28+
[JsonProperty("hash")]
29+
public Hash Hash{ get; set; }
30+
31+
/// <summary>
32+
/// Gets or sets the findings.
33+
/// </summary>
34+
[JsonProperty("findings")]
35+
public Findings Findings { get; set; }
36+
37+
/// <summary>
38+
/// Gets or sets the SHA256 hash.
39+
/// </summary>
40+
[JsonProperty("uploads")]
41+
public List<int> Uploads { get; set; }
42+
43+
/// <summary>
44+
/// Gets or sets the message.
45+
/// </summary>
46+
[JsonProperty("message")]
47+
public string Message { get; set; }
48+
49+
/// <inheritdoc />
50+
public override string ToString()
51+
{
52+
return $"{this.Hash}, {this.Findings}, {this.Uploads}, '{this.Message}'";
53+
}
54+
} // File
55+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// ---------------------------------------------------------------------------
2+
// <copyright file="SearchHash.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 hash information for searching.
21+
/// </summary>
22+
public class SearchHash
23+
{
24+
/// <summary>
25+
/// Gets or sets the SHA1 hash.
26+
/// </summary>
27+
[JsonProperty("sha1")]
28+
public string Sha1 { get; set; }
29+
30+
/// <summary>
31+
/// Gets or sets the MD5 hash.
32+
/// </summary>
33+
[JsonProperty("md5")]
34+
public string Md5 { get; set; }
35+
36+
/// <summary>
37+
/// Gets or sets the SHA256 hash.
38+
/// </summary>
39+
[JsonProperty("sha256")]
40+
public string Sha256 { get; set; }
41+
42+
/// <inheritdoc />
43+
public override string ToString()
44+
{
45+
return $"SHA1={this.Sha1}, MD5={this.Md5}, SHA256={this.Sha256}";
46+
}
47+
} // SearchHash
48+
}

Fossology.Rest.Dotnet.Test/FossologyClientTest.cs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ namespace Fossology.Rest.Dotnet.Test
1717
using System;
1818
using System.Collections.Generic;
1919
using System.Diagnostics;
20-
using System.IO;
2120
using System.Net;
2221
using System.Threading;
2322
using Fossology.Rest.Dotnet;
@@ -46,7 +45,7 @@ public class FossologyClientTest
4645
/// <summary>
4746
/// The access token.
4847
/// </summary>
49-
private const string Token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NDc3MzQzOTksIm5iZiI6MTY0NzM4ODgwMCwianRpIjoiTWk0eiIsInNjb3BlIjoid3JpdGUifQ._bOQV5E3LF-fQ8X5yr4g5SNlBQmBiSgb104x0nVBnqc";
48+
private const string Token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NDc4MjA3OTksIm5iZiI6MTY0NzQ3NTIwMCwianRpIjoiTWk0eiIsInNjb3BlIjoid3JpdGUifQ.8PK99xI7N482c1GC2Onzwe2p74Y80_ef4DVj99jiwn4";
5049

5150
/// <summary>
5251
/// The filename of a test package.
@@ -227,7 +226,6 @@ public void TestDeleteFolder()
227226
Assert.IsFalse(FossologyFolderExists(client, folderName));
228227
}
229228

230-
#if false
231229
/// <summary>
232230
/// Unit test.
233231
/// </summary>
@@ -243,7 +241,6 @@ public void TestUploadPackage()
243241
Assert.AreEqual(201, result.Code);
244242
Debug.WriteLine($"Upload id = {result.Message}");
245243
}
246-
#endif
247244

248245
/// <summary>
249246
/// Unit test.
@@ -626,9 +623,9 @@ public void TestDownloadReport()
626623
{
627624
const string ReportName = "Report.spdx2.rdf";
628625

629-
if (File.Exists(ReportName))
626+
if (System.IO.File.Exists(ReportName))
630627
{
631-
File.Delete(ReportName);
628+
System.IO.File.Delete(ReportName);
632629
} // if
633630

634631
var uploadId = FindUpload(PackageName2);
@@ -648,7 +645,7 @@ public void TestDownloadReport()
648645

649646
var client = new FossologyClient(LocalUrl, Token);
650647
client.DownloadReport(reportId, ReportName);
651-
Assert.IsTrue(File.Exists(ReportName));
648+
Assert.IsTrue(System.IO.File.Exists(ReportName));
652649
}
653650

654651
/// <summary>
@@ -865,16 +862,16 @@ public void MyIntegrationTestLikeUnitTest()
865862
var text = result.Message.Substring(result.Message.LastIndexOf('/') + 1);
866863
var reportId = int.Parse(text);
867864

868-
if (File.Exists(ReportFilename))
865+
if (System.IO.File.Exists(ReportFilename))
869866
{
870-
File.Delete(ReportFilename);
867+
System.IO.File.Delete(ReportFilename);
871868
} // if
872869

873870
// ugly but required: wait some time until report is available
874871
Thread.Sleep(3000);
875872

876873
client.DownloadReport(reportId, ReportFilename);
877-
Assert.IsTrue(File.Exists(ReportFilename));
874+
Assert.IsTrue(System.IO.File.Exists(ReportFilename));
878875

879876
result = client.DeleteUpload(uploadId);
880877
Assert.IsNotNull(result);
@@ -1041,17 +1038,16 @@ public void TestCreateLicense()
10411038
public void TestFileSearch()
10421039
{
10431040
var client = new FossologyClient(LocalUrl, Token);
1044-
var hash = new Hash();
1045-
hash.Sha1 = "33D683B0AC33D9C193354BAEB41264B40132B4EC";
1046-
hash.Md5 = "EBDCAC10B261D462C70A8C9D2E6458DD";
1047-
hash.Sha256 = "E8883B515FF07F7DF949CF3722C91DFCBFBEFEAF25441E727CE1F89C210DAB6F";
1048-
hash.Size = 4126720;
1049-
var hashes = new List<Hash>();
1041+
var hash = new SearchHash();
1042+
hash.Sha1 = null;
1043+
hash.Md5 = "3F40923DFBB69F90727DFE7378B8E962";
1044+
hash.Sha256 = null;
1045+
var hashes = new List<SearchHash>();
10501046
hashes.Add(hash);
10511047

10521048
var actual = client.SearchForFile(hashes);
10531049
Assert.IsNotNull(actual);
1054-
Assert.IsTrue(actual.Length > 0);
1050+
Assert.IsTrue(actual.Count > 0);
10551051

10561052
// always returns
10571053
// [
@@ -1069,7 +1065,7 @@ public void TestFileSearch()
10691065

10701066
//// ---------------------------------------------------------------------
10711067

1072-
#region TESTS THAT REQUIRE MANUAL PREPARATION
1068+
#region TESTS THAT REQUIRE MANUAL PREPARATION
10731069
#if false
10741070
/// <summary>
10751071
/// Unit test.
@@ -1087,11 +1083,11 @@ public void TestDeleteUserSuccess()
10871083
Assert.AreEqual(202, result.Code);
10881084
}
10891085
#endif
1090-
#endregion TESTS THAT REQUIRE MANUAL PREPARATION
1086+
#endregion TESTS THAT REQUIRE MANUAL PREPARATION
10911087

10921088
//// ---------------------------------------------------------------------
10931089

1094-
#region SUPPORT METHODS
1090+
#region SUPPORT METHODS
10951091
/// <summary>
10961092
/// Triggers the report generation.
10971093
/// </summary>
@@ -1280,6 +1276,6 @@ private static int FindFolder(IEnumerable<Folder> folderList, string folderName)
12801276

12811277
return -1;
12821278
} // FindFolder()
1283-
#endregion SUPPORT METHODS
1279+
#endregion SUPPORT METHODS
12841280
}
12851281
}

Fossology.Rest.Dotnet/FossologyClientSearch.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public IReadOnlyList<SearchResult> Search(
7777
request.AddHeader("license", license);
7878
} // if
7979

80-
if (!string.IsNullOrEmpty(license))
80+
if (!string.IsNullOrEmpty(copyright))
8181
{
8282
request.AddHeader("copyright", copyright);
8383
} // if
@@ -99,7 +99,7 @@ public IReadOnlyList<SearchResult> Search(
9999
/// <param name="fileHashes">A list of file hashes.</param>
100100
/// <param name="groupName">Name of the group.</param>
101101
/// <returns>A raw JSON result.</returns>
102-
public string SearchForFile(List<Hash> fileHashes, string groupName = "")
102+
public IReadOnlyList<File> SearchForFile(List<SearchHash> fileHashes, string groupName = "")
103103
{
104104
Log.Debug($"Searching for files by hash...");
105105

@@ -115,8 +115,14 @@ public string SearchForFile(List<Hash> fileHashes, string groupName = "")
115115
} // if
116116

117117
request.AddJsonBody(json);
118-
var resultRaw = this.api.Execute(request);
119-
return resultRaw.Content;
118+
var response = this.api.Execute(request);
119+
var result = JsonConvert.DeserializeObject<IReadOnlyList<File>>(
120+
response.Content,
121+
new JsonSerializerSettings
122+
{
123+
NullValueHandling = NullValueHandling.Ignore,
124+
});
125+
return result;
120126
} // SearchForFile()
121127
} // FossologyClient
122128
}

Fossology.Rest.Dotnet/FossologyClientUpload.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public Result UploadPackage(
6060
{
6161
Log.Debug($"Uploading package {fileName} to folder {folderId}...");
6262

63-
if (!File.Exists(fileName))
63+
if (!System.IO.File.Exists(fileName))
6464
{
6565
throw new FossologyApiException(ErrorCode.FileNotFound, fileName);
6666
} // if

Fossology.Rest.Dotnet/RestApi.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ public void DownloadFile(string uri, string filename)
310310
request.JsonSerializer = new JsonSerializer();
311311
this.AddHeaders(request);
312312
var data = this.client.DownloadData(request, true);
313-
File.WriteAllBytes(filename, data);
313+
System.IO.File.WriteAllBytes(filename, data);
314314
}
315315
catch (FossologyApiException)
316316
{

FossyApiDemo/MainForm.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,9 @@ private async Task<bool> ProcessFile(string fileName)
485485
} // if
486486

487487
var reportFilename = upload.UploadName + ".spdx2.rdf.xml";
488-
if (File.Exists(reportFilename))
488+
if (System.IO.File.Exists(reportFilename))
489489
{
490-
File.Delete(reportFilename);
490+
System.IO.File.Delete(reportFilename);
491491
} // if
492492

493493
this.SetStatus(90, "Downloading report...");
@@ -536,7 +536,7 @@ private async Task<bool> DownloadReport(string reportFilename)
536536
} // catch
537537
} // while
538538

539-
if (!File.Exists(reportFilename))
539+
if (!System.IO.File.Exists(reportFilename))
540540
{
541541
log.Error("Error downloading report!");
542542
return false;

0 commit comments

Comments
 (0)