Skip to content

Commit a3aacb2

Browse files
MalcolmnixonMalcolm Nixon
andauthored
Cleanup SonarQube issues (#49)
* Cleanup JSON asserts with helper * Cleanup assert. --------- Co-authored-by: Malcolm Nixon <malcolm.nixon@elbitsystems-us.com>
1 parent 406b4d7 commit a3aacb2

15 files changed

+281
-237
lines changed

src/DemaConsulting.SpdxModel/IO/Spdx2JsonSerializer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,10 +461,10 @@ private static void EmitOptionalString(JsonNode json, string name, string? value
461461
json[name] = value;
462462
}
463463

464-
private static void EmitOptionalStrings(JsonNode json, string name, IReadOnlyCollection<string> values)
464+
private static void EmitOptionalStrings(JsonNode json, string name, string[] values)
465465
{
466466
// Skip if empty
467-
if (values.Count == 0)
467+
if (values.Length == 0)
468468
return;
469469

470470
var array = new JsonArray();

test/DemaConsulting.SpdxModel.Tests/IO/Spdx2JsonSerializeAnnotation.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ public void SerializeAnnotation()
4848
var json = Spdx2JsonSerializer.SerializeAnnotation(annotation);
4949

5050
// Assert
51-
Assert.AreEqual("SPDXRef-Annotation", json["SPDXID"]?.ToString());
52-
Assert.AreEqual("John Doe", json["annotator"]?.ToString());
53-
Assert.AreEqual("2021-09-01T12:00:00Z", json["annotationDate"]?.ToString());
54-
Assert.AreEqual("REVIEW", json["annotationType"]?.ToString());
55-
Assert.AreEqual("This is a comment", json["comment"]?.ToString());
51+
SpdxJsonHelpers.AssertEqual("SPDXRef-Annotation", json["SPDXID"]);
52+
SpdxJsonHelpers.AssertEqual("John Doe", json["annotator"]);
53+
SpdxJsonHelpers.AssertEqual("2021-09-01T12:00:00Z", json["annotationDate"]);
54+
SpdxJsonHelpers.AssertEqual("REVIEW", json["annotationType"]);
55+
SpdxJsonHelpers.AssertEqual("This is a comment", json["comment"]);
5656
}
5757

5858
/// <summary>
@@ -86,16 +86,17 @@ public void SerializeAnnotations()
8686
var json = Spdx2JsonSerializer.SerializeAnnotations(annotations);
8787

8888
// Assert
89+
Assert.IsNotNull(json);
8990
Assert.AreEqual(2, json.Count);
90-
Assert.AreEqual("SPDXRef-Annotation1", json[0]?["SPDXID"]?.ToString());
91-
Assert.AreEqual("John Doe", json[0]?["annotator"]?.ToString());
92-
Assert.AreEqual("2021-09-01T12:00:00Z", json[0]?["annotationDate"]?.ToString());
93-
Assert.AreEqual("REVIEW", json[0]?["annotationType"]?.ToString());
94-
Assert.AreEqual("This is a comment", json[0]?["comment"]?.ToString());
95-
Assert.AreEqual("SPDXRef-Annotation2", json[1]?["SPDXID"]?.ToString());
96-
Assert.AreEqual("Jane Doe", json[1]?["annotator"]?.ToString());
97-
Assert.AreEqual("2021-09-02T12:00:00Z", json[1]?["annotationDate"]?.ToString());
98-
Assert.AreEqual("OTHER", json[1]?["annotationType"]?.ToString());
99-
Assert.AreEqual("This is another comment", json[1]?["comment"]?.ToString());
91+
SpdxJsonHelpers.AssertEqual("SPDXRef-Annotation1", json[0]?["SPDXID"]);
92+
SpdxJsonHelpers.AssertEqual("John Doe", json[0]?["annotator"]);
93+
SpdxJsonHelpers.AssertEqual("2021-09-01T12:00:00Z", json[0]?["annotationDate"]);
94+
SpdxJsonHelpers.AssertEqual("REVIEW", json[0]?["annotationType"]);
95+
SpdxJsonHelpers.AssertEqual("This is a comment", json[0]?["comment"]);
96+
SpdxJsonHelpers.AssertEqual("SPDXRef-Annotation2", json[1]?["SPDXID"]);
97+
SpdxJsonHelpers.AssertEqual("Jane Doe", json[1]?["annotator"]);
98+
SpdxJsonHelpers.AssertEqual("2021-09-02T12:00:00Z", json[1]?["annotationDate"]);
99+
SpdxJsonHelpers.AssertEqual("OTHER", json[1]?["annotationType"]);
100+
SpdxJsonHelpers.AssertEqual("This is another comment", json[1]?["comment"]);
100101
}
101102
}

test/DemaConsulting.SpdxModel.Tests/IO/Spdx2JsonSerializeChecksum.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public void SerializeChecksum()
4646

4747
// Assert
4848
Assert.IsNotNull(json);
49-
Assert.AreEqual("SHA1", json["algorithm"]?.ToString());
50-
Assert.AreEqual("2fd4e1c67a2d28f123849ee1bb76e7391b93eb12", json["checksumValue"]?.ToString());
49+
SpdxJsonHelpers.AssertEqual("SHA1", json["algorithm"]);
50+
SpdxJsonHelpers.AssertEqual("2fd4e1c67a2d28f123849ee1bb76e7391b93eb12", json["checksumValue"]);
5151
}
5252

5353
/// <summary>
@@ -77,9 +77,9 @@ public void SerializeChecksums()
7777
// Assert
7878
Assert.IsNotNull(json);
7979
Assert.AreEqual(2, json.Count);
80-
Assert.AreEqual("SHA1", json[0]?["algorithm"]?.ToString());
81-
Assert.AreEqual("2fd4e1c67a2d28f123849ee1bb76e7391b93eb12", json[0]?["checksumValue"]?.ToString());
82-
Assert.AreEqual("MD5", json[1]?["algorithm"]?.ToString());
83-
Assert.AreEqual("d41d8cd98f00b204e9800998ecf8427e", json[1]?["checksumValue"]?.ToString());
80+
SpdxJsonHelpers.AssertEqual("SHA1", json[0]?["algorithm"]);
81+
SpdxJsonHelpers.AssertEqual("2fd4e1c67a2d28f123849ee1bb76e7391b93eb12", json[0]?["checksumValue"]);
82+
SpdxJsonHelpers.AssertEqual("MD5", json[1]?["algorithm"]);
83+
SpdxJsonHelpers.AssertEqual("d41d8cd98f00b204e9800998ecf8427e", json[1]?["checksumValue"]);
8484
}
8585
}

test/DemaConsulting.SpdxModel.Tests/IO/Spdx2JsonSerializeCreationInformation.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ public void SerializeCreationInformation()
5353
var json = Spdx2JsonSerializer.SerializeCreationInformation(creationInformation);
5454

5555
// Assert
56-
Assert.AreEqual(
56+
SpdxJsonHelpers.AssertEqual(
5757
"This package has been shipped in source and binary form.\nThe binaries were created with gcc 4.5.1 and expect to link to\ncompatible system run time libraries.",
58-
json["comment"]?.ToString());
59-
Assert.AreEqual("2010-01-29T18:30:22Z", json["created"]?.ToString());
60-
Assert.AreEqual("Tool: LicenseFind-1.0", json["creators"]?[0]?.ToString());
61-
Assert.AreEqual("Organization: ExampleCodeInspect ()", json["creators"]?[1]?.ToString());
62-
Assert.AreEqual("Person: Jane Doe ()", json["creators"]?[2]?.ToString());
63-
Assert.AreEqual("3.17", json["licenseListVersion"]?.ToString());
58+
json["comment"]);
59+
SpdxJsonHelpers.AssertEqual("2010-01-29T18:30:22Z", json["created"]);
60+
SpdxJsonHelpers.AssertEqual("Tool: LicenseFind-1.0", json["creators"]?[0]);
61+
SpdxJsonHelpers.AssertEqual("Organization: ExampleCodeInspect ()", json["creators"]?[1]);
62+
SpdxJsonHelpers.AssertEqual("Person: Jane Doe ()", json["creators"]?[2]);
63+
SpdxJsonHelpers.AssertEqual("3.17", json["licenseListVersion"]);
6464
}
6565
}

test/DemaConsulting.SpdxModel.Tests/IO/Spdx2JsonSerializeDocument.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,16 @@ public void SerializeDocument()
7474
var json = Spdx2JsonSerializer.SerializeDocument(document);
7575

7676
// Assert
77-
Assert.AreEqual("SPDXRef-DOCUMENT", json["SPDXID"]?.ToString());
78-
Assert.AreEqual("SPDX-2.3", json["spdxVersion"]?.ToString());
79-
Assert.AreEqual("SPDX-Tools-v2.0", json["name"]?.ToString());
80-
Assert.AreEqual("CC0-1.0", json["dataLicense"]?.ToString());
81-
Assert.AreEqual("This document was created using SPDX 2.0 using licenses from the web site.",
82-
json["comment"]?.ToString());
83-
Assert.AreEqual("SPDXRef-File", json["documentDescribes"]?[0]?.ToString());
84-
Assert.AreEqual("SPDXRef-File", json["documentDescribes"]?[1]?.ToString());
85-
Assert.AreEqual("SPDXRef-Package", json["documentDescribes"]?[2]?.ToString());
86-
Assert.AreEqual("http://spdx.org/spdxdocs/spdx-example-json-2.3-444504E0-4F89-41D3-9A0C-0305E82C3301",
87-
json["documentNamespace"]?.ToString());
77+
SpdxJsonHelpers.AssertEqual("SPDXRef-DOCUMENT", json["SPDXID"]);
78+
SpdxJsonHelpers.AssertEqual("SPDX-2.3", json["spdxVersion"]);
79+
SpdxJsonHelpers.AssertEqual("SPDX-Tools-v2.0", json["name"]);
80+
SpdxJsonHelpers.AssertEqual("CC0-1.0", json["dataLicense"]);
81+
SpdxJsonHelpers.AssertEqual("This document was created using SPDX 2.0 using licenses from the web site.",
82+
json["comment"]);
83+
SpdxJsonHelpers.AssertEqual("SPDXRef-File", json["documentDescribes"]?[0]);
84+
SpdxJsonHelpers.AssertEqual("SPDXRef-File", json["documentDescribes"]?[1]);
85+
SpdxJsonHelpers.AssertEqual("SPDXRef-Package", json["documentDescribes"]?[2]);
86+
SpdxJsonHelpers.AssertEqual("http://spdx.org/spdxdocs/spdx-example-json-2.3-444504E0-4F89-41D3-9A0C-0305E82C3301",
87+
json["documentNamespace"]);
8888
}
8989
}

test/DemaConsulting.SpdxModel.Tests/IO/Spdx2JsonSerializeExternalDocumentReference.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public void SerializeExternalDocumentReference()
5050
var json = Spdx2JsonSerializer.SerializeExternalDocumentReference(reference);
5151

5252
// Assert
53-
Assert.AreEqual("DocumentRef-spdx-tool-1.2", json["externalDocumentId"]?.ToString());
54-
Assert.AreEqual("SHA1", json["checksum"]?["algorithm"]?.ToString());
55-
Assert.AreEqual("d6a770ba38583ed4bb4525bd96e50461655d2759", json["checksum"]?["checksumValue"]?.ToString());
56-
Assert.AreEqual("http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301",
57-
json["spdxDocument"]?.ToString());
53+
SpdxJsonHelpers.AssertEqual("DocumentRef-spdx-tool-1.2", json["externalDocumentId"]);
54+
SpdxJsonHelpers.AssertEqual("SHA1", json["checksum"]?["algorithm"]);
55+
SpdxJsonHelpers.AssertEqual("d6a770ba38583ed4bb4525bd96e50461655d2759", json["checksum"]?["checksumValue"]);
56+
SpdxJsonHelpers.AssertEqual("http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301",
57+
json["spdxDocument"]);
5858
}
5959

6060
/// <summary>
@@ -82,11 +82,12 @@ public void SerializeExternalDocumentReferences()
8282
var json = Spdx2JsonSerializer.SerializeExternalDocumentReferences(references);
8383

8484
// Assert
85+
Assert.IsNotNull(json);
8586
Assert.AreEqual(1, json.Count);
86-
Assert.AreEqual("DocumentRef-spdx-tool-1.2", json[0]?["externalDocumentId"]?.ToString());
87-
Assert.AreEqual("SHA1", json[0]?["checksum"]?["algorithm"]?.ToString());
88-
Assert.AreEqual("d6a770ba38583ed4bb4525bd96e50461655d2759", json[0]?["checksum"]?["checksumValue"]?.ToString());
89-
Assert.AreEqual("http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301",
90-
json[0]?["spdxDocument"]?.ToString());
87+
SpdxJsonHelpers.AssertEqual("DocumentRef-spdx-tool-1.2", json[0]?["externalDocumentId"]);
88+
SpdxJsonHelpers.AssertEqual("SHA1", json[0]?["checksum"]?["algorithm"]);
89+
SpdxJsonHelpers.AssertEqual("d6a770ba38583ed4bb4525bd96e50461655d2759", json[0]?["checksum"]?["checksumValue"]);
90+
SpdxJsonHelpers.AssertEqual("http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301",
91+
json[0]?["spdxDocument"]);
9192
}
9293
}

test/DemaConsulting.SpdxModel.Tests/IO/Spdx2JsonSerializeExternalReference.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ public void SerializeExternalReference()
4848

4949
// Assert
5050
Assert.IsNotNull(json);
51-
Assert.AreEqual("SECURITY", json["referenceCategory"]?.ToString());
52-
Assert.AreEqual("cpe:2.3:a:pivotal_software:spring_framework:4.1.0:*:*:*:*:*:*:*",
53-
json["referenceLocator"]?.ToString());
54-
Assert.AreEqual("cpe23Type", json["referenceType"]?.ToString());
55-
Assert.AreEqual("Example Comment", json["comment"]?.ToString());
51+
SpdxJsonHelpers.AssertEqual("SECURITY", json["referenceCategory"]);
52+
SpdxJsonHelpers.AssertEqual("cpe:2.3:a:pivotal_software:spring_framework:4.1.0:*:*:*:*:*:*:*",
53+
json["referenceLocator"]);
54+
SpdxJsonHelpers.AssertEqual("cpe23Type", json["referenceType"]);
55+
SpdxJsonHelpers.AssertEqual("Example Comment", json["comment"]);
5656
}
5757

5858
/// <summary>
@@ -87,16 +87,16 @@ public void SerializeExternalReferences()
8787
// Assert
8888
Assert.IsNotNull(json);
8989
Assert.AreEqual(2, json.Count);
90-
Assert.AreEqual("SECURITY", json[0]?["referenceCategory"]?.ToString());
91-
Assert.AreEqual("cpe:2.3:a:pivotal_software:spring_framework:4.1.0:*:*:*:*:*:*:*",
92-
json[0]?["referenceLocator"]?.ToString());
93-
Assert.AreEqual("cpe23Type", json[0]?["referenceType"]?.ToString());
94-
Assert.AreEqual("Example Comment", json[0]?["comment"]?.ToString());
95-
Assert.AreEqual("OTHER", json[1]?["referenceCategory"]?.ToString());
96-
Assert.AreEqual("acmecorp/acmenator/4.1.3-alpha", json[1]?["referenceLocator"]?.ToString());
97-
Assert.AreEqual(
90+
SpdxJsonHelpers.AssertEqual("SECURITY", json[0]?["referenceCategory"]);
91+
SpdxJsonHelpers.AssertEqual("cpe:2.3:a:pivotal_software:spring_framework:4.1.0:*:*:*:*:*:*:*",
92+
json[0]?["referenceLocator"]);
93+
SpdxJsonHelpers.AssertEqual("cpe23Type", json[0]?["referenceType"]);
94+
SpdxJsonHelpers.AssertEqual("Example Comment", json[0]?["comment"]);
95+
SpdxJsonHelpers.AssertEqual("OTHER", json[1]?["referenceCategory"]);
96+
SpdxJsonHelpers.AssertEqual("acmecorp/acmenator/4.1.3-alpha", json[1]?["referenceLocator"]);
97+
SpdxJsonHelpers.AssertEqual(
9898
"http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#LocationRef-acmeforge",
99-
json[1]?["referenceType"]?.ToString());
100-
Assert.AreEqual("This is the external ref for Acme", json[1]?["comment"]?.ToString());
99+
json[1]?["referenceType"]);
100+
SpdxJsonHelpers.AssertEqual("This is the external ref for Acme", json[1]?["comment"]);
101101
}
102102
}

test/DemaConsulting.SpdxModel.Tests/IO/Spdx2JsonSerializeExtractedLicensingInfo.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ public void SerializeExtractedLicensingInfo()
4848
var json = Spdx2JsonSerializer.SerializeExtractedLicensingInfo(info);
4949

5050
// Assert
51-
Assert.AreEqual("MIT", json["licenseId"]?.ToString());
52-
Assert.AreEqual("This is the MIT license", json["extractedText"]?.ToString());
53-
Assert.AreEqual("MIT License", json["name"]?.ToString());
54-
Assert.AreEqual("https://opensource.org/licenses/MIT", json["seeAlsos"]?[0]?.ToString());
55-
Assert.AreEqual("This is a comment", json["comment"]?.ToString());
51+
SpdxJsonHelpers.AssertEqual("MIT", json["licenseId"]);
52+
SpdxJsonHelpers.AssertEqual("This is the MIT license", json["extractedText"]);
53+
SpdxJsonHelpers.AssertEqual("MIT License", json["name"]);
54+
SpdxJsonHelpers.AssertEqual("https://opensource.org/licenses/MIT", json["seeAlsos"]?[0]);
55+
SpdxJsonHelpers.AssertEqual("This is a comment", json["comment"]);
5656
}
5757

5858
/// <summary>
@@ -78,11 +78,12 @@ public void SerializeExtractedLicensingInfos()
7878
var json = Spdx2JsonSerializer.SerializeExtractedLicensingInfos(info);
7979

8080
// Assert
81+
Assert.IsNotNull(json);
8182
Assert.AreEqual(1, json.Count);
82-
Assert.AreEqual("MIT", json[0]?["licenseId"]?.ToString());
83-
Assert.AreEqual("This is the MIT license", json[0]?["extractedText"]?.ToString());
84-
Assert.AreEqual("MIT License", json[0]?["name"]?.ToString());
85-
Assert.AreEqual("https://opensource.org/licenses/MIT", json[0]?["seeAlsos"]?[0]?.ToString());
86-
Assert.AreEqual("This is a comment", json[0]?["comment"]?.ToString());
83+
SpdxJsonHelpers.AssertEqual("MIT", json[0]?["licenseId"]);
84+
SpdxJsonHelpers.AssertEqual("This is the MIT license", json[0]?["extractedText"]);
85+
SpdxJsonHelpers.AssertEqual("MIT License", json[0]?["name"]);
86+
SpdxJsonHelpers.AssertEqual("https://opensource.org/licenses/MIT", json[0]?["seeAlsos"]?[0]);
87+
SpdxJsonHelpers.AssertEqual("This is a comment", json[0]?["comment"]);
8788
}
8889
}

0 commit comments

Comments
 (0)