Skip to content

Commit ec4c768

Browse files
committed
feat: new method IsUploadUnpacked()
1 parent f53bcf2 commit ec4c768

File tree

2 files changed

+93
-22
lines changed

2 files changed

+93
-22
lines changed

Fossology.Rest.Dotnet.Test/FossologyClientTest.cs

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class FossologyClientTest
4545
/// <summary>
4646
/// The access token.
4747
/// </summary>
48-
private const string Token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1OTMwNDMxOTksIm5iZiI6MTU5MjA5MjgwMCwianRpIjoiTWk0eiIsInNjb3BlIjoid3JpdGUifQ.q7vgcRWR596ShkHrRoHxn2vOn7hsJl8UvjXYpVN4PSU";
48+
private const string Token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1OTM1NjE1OTksIm5iZiI6MTU5MjI2NTYwMCwianRpIjoiTWk0eiIsInNjb3BlIjoid3JpdGUifQ.h9oNRGV_N-GkEJGNZPp2qQCgOkb1rDv0_bnEnWjCnkg";
4949

5050
/// <summary>
5151
/// The filename of a test package.
@@ -80,6 +80,24 @@ public void TestGetVersion()
8080
Debug.WriteLine($"Version = {result}");
8181
}
8282

83+
/// <summary>
84+
/// Unit test.
85+
/// </summary>
86+
[TestMethod]
87+
public void TestGetToken()
88+
{
89+
var client = new FossologyClient(LocalUrl, string.Empty);
90+
var request = new TokenRequest();
91+
request.Username = "fossy";
92+
request.Password = "fossy";
93+
request.TokenName = "TestToken1";
94+
request.TokenScope = "write";
95+
request.TokenExpire = DateTime.Today.AddDays(3).ToString("yyyy-MM-dd");
96+
var result = client.GetToken(request);
97+
Assert.IsNotNull(result);
98+
Assert.IsTrue(result.Length > 20);
99+
}
100+
83101
/// <summary>
84102
/// Unit test.
85103
/// </summary>
@@ -222,6 +240,33 @@ public void TestUploadPackageAndCheckLookAt()
222240
} // catch
223241
}
224242

243+
private static void WaitUntilUploadIsDone(FossologyClient client, int id)
244+
{
245+
while (!client.IsUploadUnpacked(id))
246+
{
247+
Debug.WriteLine($"Waiting for upload {id} to get unpacked...");
248+
Thread.Sleep(500);
249+
} // while
250+
}
251+
252+
/// <summary>
253+
/// Unit test.
254+
/// </summary>
255+
[TestMethod]
256+
public void TestUploadPackageCheckUnpackStatus()
257+
{
258+
var client = new FossologyClient(LocalUrl, Token);
259+
var result = client.UploadPackage(@"..\..\..\TestData\xtxgd.zip", 5);
260+
Assert.IsNotNull(result);
261+
Assert.AreEqual("INFO", result.Type);
262+
Assert.AreEqual(201, result.Code);
263+
var uploadId = int.Parse(result.Message);
264+
Debug.WriteLine($"Upload id = {uploadId}");
265+
266+
WaitUntilUploadIsDone(client, uploadId);
267+
Debug.WriteLine($"Upload id = {uploadId} is now available");
268+
}
269+
225270
/// <summary>
226271
/// Unit test.
227272
/// </summary>
@@ -300,7 +345,7 @@ public void TestGetUploadList()
300345
[TestMethod]
301346
public void TestGetUploadLicenses()
302347
{
303-
const int Id = 2;
348+
const int Id = 5;
304349

305350
var client = new FossologyClient(LocalUrl, Token);
306351
var licenses = client.GetUploadLicenses(Id, "nomos", true);
@@ -503,24 +548,6 @@ public void TestSearch()
503548
Assert.IsNotNull(result);
504549
}
505550

506-
/// <summary>
507-
/// Unit test.
508-
/// </summary>
509-
[TestMethod]
510-
public void TestGetToken()
511-
{
512-
var client = new FossologyClient(LocalUrl, string.Empty);
513-
var request = new TokenRequest();
514-
request.Username = "fossy";
515-
request.Password = "fossy";
516-
request.TokenName = "TestToken1";
517-
request.TokenScope = "write";
518-
request.TokenExpire = DateTime.Today.AddDays(3).ToString("yyyy-MM-dd");
519-
var result = client.GetToken(request);
520-
Assert.IsNotNull(result);
521-
Assert.IsTrue(result.Length > 20);
522-
}
523-
524551
/// <summary>
525552
/// Finds the folder with the given name.
526553
/// </summary>
@@ -555,6 +582,17 @@ public void MyIntegrationTestLikeUnitTest()
555582
Assert.IsNotNull(version);
556583
Debug.WriteLine($"Version = {version}");
557584

585+
var request = new TokenRequest();
586+
request.Username = "fossy";
587+
request.Password = "fossy";
588+
request.TokenName = "TestToken1";
589+
request.TokenScope = "write";
590+
request.TokenExpire = DateTime.Today.AddDays(3).ToString("yyyy-MM-dd");
591+
//var tokenResult = client.GetToken(request);
592+
//Assert.IsNotNull(tokenResult);
593+
//Assert.IsTrue(tokenResult.Length > 20);
594+
595+
558596
var folderlist = client.GetFolderList();
559597
Assert.IsNotNull(folderlist);
560598
if ((folderlist.Count != 1) && (folderlist.Count != 2))
@@ -585,8 +623,7 @@ public void MyIntegrationTestLikeUnitTest()
585623
var uploadId = int.Parse(result.Message);
586624
Assert.IsTrue(uploadId > 0);
587625

588-
// ugly but required: wait some time until upload is available
589-
Thread.Sleep(3000);
626+
WaitUntilUploadIsDone(client, uploadId);
590627

591628
var uploadlist = client.GetUploadList();
592629
Assert.IsNotNull(uploadlist);
@@ -640,6 +677,17 @@ public void MyIntegrationTestLikeUnitTest()
640677
Assert.IsNotNull(job);
641678
Assert.AreEqual(jobId, job.Id);
642679

680+
// PHP Fatal error: Uncaught Exception: cannot find uploadId = 14 in /usr/local/share/fossology/lib/php/Dao/UploadDao.php:201
681+
//var summary = client.GetUploadSummary(uploadId);
682+
//Assert.IsNotNull(summary);
683+
//Assert.AreEqual(uploadId, summary.Id);
684+
685+
var licensesFound = client.GetUploadLicenses(
686+
uploadId, "nomos", true);
687+
Assert.IsNotNull(licensesFound);
688+
Assert.IsTrue(licensesFound.Count > 0);
689+
Assert.AreEqual("MIT", licensesFound[0].AgentFindings[0]);
690+
643691
result = client.TriggerReportGeneration(uploadId, "spdx2");
644692
Assert.IsNotNull(result);
645693
Assert.AreEqual("INFO", result.Type);

Fossology.Rest.Dotnet/FossologyClient.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace Fossology.Rest.Dotnet
1919
using System;
2020
using System.Collections.Generic;
2121
using System.IO;
22+
using System.Linq;
2223
using System.Net;
2324
using Model;
2425

@@ -407,6 +408,28 @@ public Result UploadPackageFromServer(int folderId, ServerUpload details, string
407408
return result;
408409
} // UploadPackageFromServer()
409410

411+
/// <summary>
412+
/// Determines whether the upload with the given id has been successfully
413+
/// uploaded and unpacked. Only when unpacking has been done, we can access
414+
/// the upload and trigger jobs.
415+
/// </summary>
416+
/// <param name="id">The identifier.</param>
417+
/// <returns><c>true</c> if the upload has been successfully unpacked;
418+
/// otherwise, <c>false</c>.</returns>
419+
public bool IsUploadUnpacked(int id)
420+
{
421+
Log.Debug($"Checking status for upload {id}...");
422+
423+
var result = this.api.Get(this.Url + $"/jobs?upload={id}");
424+
var jobs = JsonConvert.DeserializeObject<List<Job>>(result.Content,
425+
new JsonSerializerSettings
426+
{
427+
NullValueHandling = NullValueHandling.Ignore
428+
});
429+
430+
return jobs.All(job => job.Status == "Completed");
431+
} // IsUploadUnpacked()
432+
410433
/// <summary>
411434
/// Gets the upload with the specified id.
412435
/// </summary>

0 commit comments

Comments
 (0)