-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpdxRelationshipsTests.cs
More file actions
261 lines (238 loc) · 9.74 KB
/
SpdxRelationshipsTests.cs
File metadata and controls
261 lines (238 loc) · 9.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
using DemaConsulting.SpdxModel.IO;
using DemaConsulting.SpdxModel.Transform;
namespace DemaConsulting.SpdxModel.Tests.Transforms;
/// <summary>
/// Tests for the <see cref="SpdxRelationship" /> transforms.
/// </summary>
[TestClass]
public class SpdxRelationshipsTests
{
/// <summary>
/// Test SPDX document for relationships.
/// </summary>
private const string TestDocumentContents =
"""
{
"files": [],
"packages": [ {
"SPDXID": "SPDXRef-Package-1",
"name": "Test Package",
"versionInfo": "1.0.0",
"packageFileName": "package1.zip",
"downloadLocation": "https://github.com/demaconsulting/SpdxTool",
"licenseConcluded": "MIT"
},
{
"SPDXID": "SPDXRef-Package-2",
"name": "Another Test Package",
"versionInfo": "2.0.0",
"packageFileName": "package2.tar",
"downloadLocation": "https://github.com/demaconsulting/SpdxModel",
"licenseConcluded": "MIT"
}
],
"spdxVersion": "SPDX-2.2",
"dataLicense": "CC0-1.0",
"SPDXID": "SPDXRef-DOCUMENT",
"name": "Test Document",
"documentNamespace": "https://sbom.spdx.org",
"creationInfo": {
"created": "2021-10-01T00:00:00Z",
"creators": [ "Person: Malcolm Nixon" ]
},
"documentDescribes": [ "SPDXRef-Package-1" ]
}
""";
/// <summary>
/// Tests adding a relationship with a missing ID.
/// </summary>
[TestMethod]
public void SpdxRelationships_AddSingle_MissingId()
{
// Arrange: Deserialize the test document contents
var document = Spdx2JsonDeserializer.Deserialize(TestDocumentContents);
// Act: Attempt to add a relationship with a non-existent ID
var ex = Assert.ThrowsExactly<ArgumentException>(() =>
{
SpdxRelationships.Add(
document,
new SpdxRelationship
{
Id = "SPDXRef-Package-Missing",
RelatedSpdxElement = "SPDXRef-Package-2",
RelationshipType = SpdxRelationshipType.DependsOn
});
});
// Assert: Verify the exception message and that no relationships were added
Assert.StartsWith("Element SPDXRef-Package-Missing not found in SPDX document", ex.Message);
Assert.AreEqual("relationship", ex.ParamName);
Assert.IsEmpty(document.Relationships);
}
/// <summary>
/// Tests adding a relationship with a missing related element.
/// </summary>
[TestMethod]
public void SpdxRelationships_AddSingle_MissingRelatedElement()
{
// Arrange: Deserialize the test document contents
var document = Spdx2JsonDeserializer.Deserialize(TestDocumentContents);
// Act: Attempt to add a relationship with a missing related element
var ex = Assert.ThrowsExactly<ArgumentException>(() =>
{
SpdxRelationships.Add(
document,
new SpdxRelationship
{
Id = "SPDXRef-Package-1",
RelatedSpdxElement = "SPDXRef-Package-Missing",
RelationshipType = SpdxRelationshipType.DependsOn
});
});
// Assert: Verify the exception message and that no relationships were added
Assert.StartsWith("Element SPDXRef-Package-Missing not found in SPDX document", ex.Message);
Assert.AreEqual("relationship", ex.ParamName);
Assert.IsEmpty(document.Relationships);
}
/// <summary>
/// Tests adding a relationship.
/// </summary>
[TestMethod]
public void SpdxRelationships_AddSingle_Success()
{
// Arrange: Deserialize the test document contents
var document = Spdx2JsonDeserializer.Deserialize(TestDocumentContents);
// Act: Add a new relationship
SpdxRelationships.Add(
document,
new SpdxRelationship
{
Id = "SPDXRef-Package-1",
RelatedSpdxElement = "SPDXRef-Package-2",
RelationshipType = SpdxRelationshipType.DependsOn
});
// Assert: Verify the relationship was added correctly
Assert.HasCount(1, document.Relationships);
Assert.AreEqual("SPDXRef-Package-1", document.Relationships[0].Id);
Assert.AreEqual("SPDXRef-Package-2", document.Relationships[0].RelatedSpdxElement);
Assert.AreEqual(SpdxRelationshipType.DependsOn, document.Relationships[0].RelationshipType);
}
/// <summary>
/// Tests adding a duplicate relationship.
/// </summary>
[TestMethod]
public void SpdxRelationships_AddSingle_Duplicate()
{
// Arrange: Deserialize the test document contents
var document = Spdx2JsonDeserializer.Deserialize(TestDocumentContents);
// Act: Add the same new relationship twice
SpdxRelationships.Add(
document,
new SpdxRelationship
{
Id = "SPDXRef-Package-1",
RelatedSpdxElement = "SPDXRef-Package-2",
RelationshipType = SpdxRelationshipType.DependsOn
});
SpdxRelationships.Add(
document,
new SpdxRelationship
{
Id = "SPDXRef-Package-1",
RelatedSpdxElement = "SPDXRef-Package-2",
RelationshipType = SpdxRelationshipType.DependsOn
});
// Assert: Verify the relationship was added only once
Assert.HasCount(1, document.Relationships);
Assert.AreEqual("SPDXRef-Package-1", document.Relationships[0].Id);
Assert.AreEqual("SPDXRef-Package-2", document.Relationships[0].RelatedSpdxElement);
Assert.AreEqual(SpdxRelationshipType.DependsOn, document.Relationships[0].RelationshipType);
}
/// <summary>
/// Tests adding multiple relationships.
/// </summary>
[TestMethod]
public void SpdxRelationships_AddMultiple_Success()
{
// Arrange: Deserialize the test document contents
var document = Spdx2JsonDeserializer.Deserialize(TestDocumentContents);
// Act: Add relationship
SpdxRelationships.Add(
document,
[
new SpdxRelationship
{
Id = "SPDXRef-Package-1",
RelatedSpdxElement = "SPDXRef-Package-2",
RelationshipType = SpdxRelationshipType.DependsOn
}
]);
// Assert: Verify the relationship was added correctly
Assert.HasCount(1, document.Relationships);
Assert.AreEqual("SPDXRef-Package-1", document.Relationships[0].Id);
Assert.AreEqual("SPDXRef-Package-2", document.Relationships[0].RelatedSpdxElement);
Assert.AreEqual(SpdxRelationshipType.DependsOn, document.Relationships[0].RelationshipType);
}
/// <summary>
/// Tests adding multiple relationships with a duplicate.
/// </summary>
[TestMethod]
public void SpdxRelationships_AddMultiple_Duplicate()
{
// Arrange: Deserialize the test document contents
var document = Spdx2JsonDeserializer.Deserialize(TestDocumentContents);
// Act: Add new relationships with a duplicate
SpdxRelationships.Add(
document,
[
new SpdxRelationship
{
Id = "SPDXRef-Package-1",
RelatedSpdxElement = "SPDXRef-Package-2",
RelationshipType = SpdxRelationshipType.DependsOn
},
new SpdxRelationship
{
Id = "SPDXRef-Package-1",
RelatedSpdxElement = "SPDXRef-Package-2",
RelationshipType = SpdxRelationshipType.DependsOn
}
]);
// Assert: Verify the relationship was added only once
Assert.HasCount(1, document.Relationships);
Assert.AreEqual("SPDXRef-Package-1", document.Relationships[0].Id);
Assert.AreEqual("SPDXRef-Package-2", document.Relationships[0].RelatedSpdxElement);
Assert.AreEqual(SpdxRelationshipType.DependsOn, document.Relationships[0].RelationshipType);
}
/// <summary>
/// Tests adding multiple relationships with a duplicate and replace.
/// </summary>
[TestMethod]
public void SpdxRelationships_AddMultiple_Replace()
{
// Arrange: Deserialize the test document contents and add an initial relationship
var document = Spdx2JsonDeserializer.Deserialize(TestDocumentContents);
SpdxRelationships.Add(
document,
new SpdxRelationship
{
Id = "SPDXRef-Package-1",
RelatedSpdxElement = "SPDXRef-Package-2",
RelationshipType = SpdxRelationshipType.DependsOn
});
// Act: Add new relationships duplicating the original, but with a different type
SpdxRelationships.Add(document, [
new SpdxRelationship
{
Id = "SPDXRef-Package-1",
RelatedSpdxElement = "SPDXRef-Package-2",
RelationshipType = SpdxRelationshipType.BuildToolOf
}
],
true);
// Assert: Verify the relationship was replaced with the new type
Assert.HasCount(1, document.Relationships);
Assert.AreEqual("SPDXRef-Package-1", document.Relationships[0].Id);
Assert.AreEqual("SPDXRef-Package-2", document.Relationships[0].RelatedSpdxElement);
Assert.AreEqual(SpdxRelationshipType.BuildToolOf, document.Relationships[0].RelationshipType);
}
}