-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathComponentRecorderTests.cs
More file actions
563 lines (437 loc) · 24.3 KB
/
ComponentRecorderTests.cs
File metadata and controls
563 lines (437 loc) · 24.3 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#nullable disable
namespace Microsoft.ComponentDetection.Common.Tests;
using System;
using System.Collections.Generic;
using System.Linq;
using AwesomeAssertions;
using Microsoft.ComponentDetection.Common.DependencyGraph;
using Microsoft.ComponentDetection.Contracts;
using Microsoft.ComponentDetection.Contracts.BcdeModels;
using Microsoft.ComponentDetection.Contracts.TypedComponent;
using Microsoft.VisualStudio.TestTools.UnitTesting;
#pragma warning disable IDE0001 // Simplify Names
using DependencyGraph = Microsoft.ComponentDetection.Common.DependencyGraph.DependencyGraph;
#pragma warning restore IDE0001 // Simplify Names
[TestClass]
public class ComponentRecorderTests
{
private ComponentRecorder componentRecorder;
[TestInitialize]
public void TestInitialize()
{
this.componentRecorder = new ComponentRecorder();
}
[TestMethod]
public void RegisterUsage_RegisterNewDetectedComponent_NodeInTheGraphIsCreated()
{
var location = "location";
var singleFileComponentRecorder = this.componentRecorder.CreateSingleFileComponentRecorder(location);
var detectedComponent = new DetectedComponent(new NpmComponent("test", "1.0.0"));
singleFileComponentRecorder.RegisterUsage(detectedComponent);
singleFileComponentRecorder.GetComponent(detectedComponent.Component.Id).Should().NotBeNull();
var dependencyGraph = this.componentRecorder.GetDependencyGraphForLocation(location);
dependencyGraph.GetDependenciesForComponent(detectedComponent.Component.Id).Should().NotBeNull();
}
[TestMethod]
public void RegisterUsage_NewDetectedComponentHasParent_NewRelationshipIsInserted()
{
var location = "location";
var singleFileComponentRecorder = this.componentRecorder.CreateSingleFileComponentRecorder(location);
var detectedComponent = new DetectedComponent(new NpmComponent("test", "1.0.0"));
var parentComponent = new DetectedComponent(new NpmComponent("test2", "2.0.0"));
singleFileComponentRecorder.RegisterUsage(parentComponent);
singleFileComponentRecorder.RegisterUsage(detectedComponent, parentComponentId: parentComponent.Component.Id);
var dependencyGraph = this.componentRecorder.GetDependencyGraphForLocation(location);
dependencyGraph.GetDependenciesForComponent(parentComponent.Component.Id).Should().Contain(detectedComponent.Component.Id);
}
[TestMethod]
public void RegisterUsage_DetectedComponentIsNull_ArgumentNullExceptionIsThrown()
{
var singleFileComponentRecorder = this.componentRecorder.CreateSingleFileComponentRecorder("location");
Action action = () => singleFileComponentRecorder.RegisterUsage(null);
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void RegisterUsage_DevelopmentDependencyHasValue_componentNodeHasDependencyScope()
{
var location = "location";
var singleFileComponentRecorder = this.componentRecorder.CreateSingleFileComponentRecorder(location);
var detectedComponent = new DetectedComponent(new MavenComponent("org.apache.maven", "maven-artifact", "3.6.1"));
singleFileComponentRecorder.RegisterUsage(detectedComponent, dependencyScope: DependencyScope.MavenProvided);
var dependencyGraph = this.componentRecorder.GetDependencyGraphForLocation(location);
dependencyGraph.GetDependencyScope(detectedComponent.Component.Id).Should().NotBeNull();
dependencyGraph.GetDependencyScope(detectedComponent.Component.Id).Should().Be(DependencyScope.MavenProvided);
}
[TestMethod]
public void RegisterUsage_DetectedComponentWithNullComponent_ArgumentExceptionIsThrown()
{
var singleFileComponentRecorder = this.componentRecorder.CreateSingleFileComponentRecorder("location");
var detectedComponent = new DetectedComponent(null);
Action action = () => singleFileComponentRecorder.RegisterUsage(detectedComponent);
action.Should().Throw<ArgumentException>();
}
[TestMethod]
public void RegisterUsage_DetectedComponentExistAndUpdateFunctionIsNull_NotExceptionIsThrown()
{
var singleFileComponentRecorder = this.componentRecorder.CreateSingleFileComponentRecorder("location");
var detectedComponent = new DetectedComponent(new NpmComponent("test", "1.0.0"));
singleFileComponentRecorder.RegisterUsage(detectedComponent);
Action action = () => singleFileComponentRecorder.RegisterUsage(detectedComponent);
action.Should().NotThrow<Exception>();
}
[TestMethod]
public void CreateComponentsingleFileComponentRecorderForLocation_LocationIsNull_ArgumentNullExceptionIsThrown()
{
Action action = () => this.componentRecorder.CreateSingleFileComponentRecorder(null);
action.Should().Throw<ArgumentNullException>();
action = () => this.componentRecorder.CreateSingleFileComponentRecorder(string.Empty);
action.Should().Throw<ArgumentNullException>();
action = () => this.componentRecorder.CreateSingleFileComponentRecorder(" ");
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void GetComponent_ComponentNotExist_NullIsReturned()
{
this.componentRecorder.CreateSingleFileComponentRecorder("someMockLocation").GetComponent("nonexistedcomponentId").Should().BeNull();
}
[TestMethod]
public void GetDetectedComponents_AreComponentsRegistered_ComponentsAreReturned()
{
var singleFileComponentRecorder = this.componentRecorder.CreateSingleFileComponentRecorder("location");
var detectedComponent1 = new DetectedComponent(new NpmComponent("test", "1.0.0"));
var detectedComponent2 = new DetectedComponent(new NpmComponent("test", "2.0.0"));
singleFileComponentRecorder.RegisterUsage(detectedComponent1);
singleFileComponentRecorder.RegisterUsage(detectedComponent2);
var detectedComponents = this.componentRecorder.GetDetectedComponents();
detectedComponents.Should().HaveCount(2);
detectedComponents.Should().Contain(detectedComponent1);
detectedComponents.Should().Contain(detectedComponent2);
}
[TestMethod]
public void GetDetectedComponents_NoComponentsAreRegistered_EmptyCollectionIsReturned()
{
var detectedComponents = this.componentRecorder.GetDetectedComponents();
detectedComponents.Should().NotBeNull();
detectedComponents.Should().BeEmpty();
}
[TestMethod]
public void GetDetectedComponents_SameComponentAcrossFiles_MergesLicensesConcluded()
{
var recorder1 = this.componentRecorder.CreateSingleFileComponentRecorder("file1.json");
var recorder2 = this.componentRecorder.CreateSingleFileComponentRecorder("file2.json");
var component1 = new DetectedComponent(new NpmComponent("pkg", "1.0.0"))
{
LicensesConcluded = ["MIT"],
};
var component2 = new DetectedComponent(new NpmComponent("pkg", "1.0.0"))
{
LicensesConcluded = ["MIT", "Apache-2.0"],
};
recorder1.RegisterUsage(component1);
recorder2.RegisterUsage(component2);
var result = this.componentRecorder.GetDetectedComponents().Single();
result.LicensesConcluded.Should().HaveCount(2);
result.LicensesConcluded.Should().Contain("MIT");
result.LicensesConcluded.Should().Contain("Apache-2.0");
}
[TestMethod]
public void GetDetectedComponents_SameComponentAcrossFiles_MergesSuppliers()
{
var recorder1 = this.componentRecorder.CreateSingleFileComponentRecorder("file1.json");
var recorder2 = this.componentRecorder.CreateSingleFileComponentRecorder("file2.json");
var component1 = new DetectedComponent(new NpmComponent("pkg", "1.0.0"))
{
Suppliers = [new ActorInfo { Name = "Contoso", Type = "Organization" }],
};
var component2 = new DetectedComponent(new NpmComponent("pkg", "1.0.0"))
{
Suppliers = [new ActorInfo { Name = "contoso", Type = "organization" }, new ActorInfo { Name = "Fabrikam", Type = "Organization" }],
};
recorder1.RegisterUsage(component1);
recorder2.RegisterUsage(component2);
var result = this.componentRecorder.GetDetectedComponents().Single();
// "Contoso"/"Organization" and "contoso"/"organization" are equal (case-insensitive); "Fabrikam" is kept
result.Suppliers.Should().HaveCount(2);
result.Suppliers.Should().Contain(s => string.Equals(s.Name, "Contoso", System.StringComparison.OrdinalIgnoreCase));
result.Suppliers.Should().Contain(s => s.Name == "Fabrikam");
}
[TestMethod]
public void GetDetectedComponents_SameComponentAcrossFiles_OneHasNullFields_PreservesNonNull()
{
var recorder1 = this.componentRecorder.CreateSingleFileComponentRecorder("file1.json");
var recorder2 = this.componentRecorder.CreateSingleFileComponentRecorder("file2.json");
var component1 = new DetectedComponent(new NpmComponent("pkg", "1.0.0"));
var component2 = new DetectedComponent(new NpmComponent("pkg", "1.0.0"))
{
LicensesConcluded = ["MIT"],
Suppliers = [new ActorInfo { Name = "Contoso", Type = "Organization" }],
};
recorder1.RegisterUsage(component1);
recorder2.RegisterUsage(component2);
var result = this.componentRecorder.GetDetectedComponents().Single();
result.LicensesConcluded.Should().ContainSingle().Which.Should().Be("MIT");
result.Suppliers.Should().ContainSingle().Which.Name.Should().Be("Contoso");
}
[TestMethod]
public void GetDetectedComponents_SameComponentAcrossFiles_LicensesConcludedWithNullEntry_DoesNotThrow()
{
var recorder1 = this.componentRecorder.CreateSingleFileComponentRecorder("file1.json");
var recorder2 = this.componentRecorder.CreateSingleFileComponentRecorder("file2.json");
var component1 = new DetectedComponent(new NpmComponent("pkg", "1.0.0"))
{
LicensesConcluded = ["MIT", null],
};
var component2 = new DetectedComponent(new NpmComponent("pkg", "1.0.0"))
{
LicensesConcluded = [null, "Apache-2.0"],
};
recorder1.RegisterUsage(component1);
recorder2.RegisterUsage(component2);
var result = this.componentRecorder.GetDetectedComponents().Single();
result.LicensesConcluded.Should().Contain("MIT");
result.LicensesConcluded.Should().Contain("Apache-2.0");
}
[TestMethod]
public void GetDetectedComponents_SameComponentAcrossFiles_SuppliersWithNullEntry_DoesNotThrow()
{
var recorder1 = this.componentRecorder.CreateSingleFileComponentRecorder("file1.json");
var recorder2 = this.componentRecorder.CreateSingleFileComponentRecorder("file2.json");
var component1 = new DetectedComponent(new NpmComponent("pkg", "1.0.0"))
{
Suppliers = [new ActorInfo { Name = "Contoso", Type = "Organization" }, null],
};
var component2 = new DetectedComponent(new NpmComponent("pkg", "1.0.0"))
{
Suppliers = [null, new ActorInfo { Name = "Fabrikam", Type = "Organization" }],
};
recorder1.RegisterUsage(component1);
recorder2.RegisterUsage(component2);
var result = this.componentRecorder.GetDetectedComponents().Single();
result.Suppliers.Should().Contain(s => s != null && s.Name == "Contoso");
result.Suppliers.Should().Contain(s => s != null && s.Name == "Fabrikam");
}
[TestMethod]
public void GetDetectedComponents_SameComponentAcrossThreeFiles_MergesAllFields()
{
var recorder1 = this.componentRecorder.CreateSingleFileComponentRecorder("file1.json");
var recorder2 = this.componentRecorder.CreateSingleFileComponentRecorder("file2.json");
var recorder3 = this.componentRecorder.CreateSingleFileComponentRecorder("file3.json");
var component1 = new DetectedComponent(new NpmComponent("pkg", "1.0.0"))
{
LicensesConcluded = ["MIT"],
Suppliers = [new ActorInfo { Name = "Alice", Type = "Person" }],
};
var component2 = new DetectedComponent(new NpmComponent("pkg", "1.0.0"))
{
LicensesConcluded = ["Apache-2.0"],
Suppliers = [new ActorInfo { Name = "Bob", Type = "Person" }],
};
var component3 = new DetectedComponent(new NpmComponent("pkg", "1.0.0"))
{
LicensesConcluded = ["MIT", "GPL-3.0"],
Suppliers = [new ActorInfo { Name = "Alice", Type = "Person" }, new ActorInfo { Name = "Contoso", Type = "Organization" }],
};
recorder1.RegisterUsage(component1);
recorder2.RegisterUsage(component2);
recorder3.RegisterUsage(component3);
var result = this.componentRecorder.GetDetectedComponents().Single();
result.LicensesConcluded.Should().HaveCount(3);
result.LicensesConcluded.Should().Contain("MIT");
result.LicensesConcluded.Should().Contain("Apache-2.0");
result.LicensesConcluded.Should().Contain("GPL-3.0");
// "Alice"/"Person" appears in file1 and file3 — should be deduped
result.Suppliers.Should().HaveCount(3);
result.Suppliers.Should().Contain(s => s.Name == "Alice");
result.Suppliers.Should().Contain(s => s.Name == "Bob");
result.Suppliers.Should().Contain(s => s.Name == "Contoso");
}
[TestMethod]
public void GetDetectedComponents_DifferentComponentsAcrossFiles_FieldsNotMixed()
{
var recorder1 = this.componentRecorder.CreateSingleFileComponentRecorder("file1.json");
var recorder2 = this.componentRecorder.CreateSingleFileComponentRecorder("file2.json");
var componentA = new DetectedComponent(new NpmComponent("pkg-a", "1.0.0"))
{
LicensesConcluded = ["MIT"],
Suppliers = [new ActorInfo { Name = "Alice", Type = "Person" }],
};
var componentB = new DetectedComponent(new NpmComponent("pkg-b", "2.0.0"))
{
LicensesConcluded = ["GPL-3.0"],
Suppliers = [new ActorInfo { Name = "Bob", Type = "Person" }],
};
recorder1.RegisterUsage(componentA);
recorder2.RegisterUsage(componentB);
var results = this.componentRecorder.GetDetectedComponents().ToList();
results.Should().HaveCount(2);
var resultA = results.Single(c => ((NpmComponent)c.Component).Name == "pkg-a");
resultA.LicensesConcluded.Should().BeEquivalentTo(["MIT"]);
resultA.Suppliers.Should().ContainSingle().Which.Name.Should().Be("Alice");
var resultB = results.Single(c => ((NpmComponent)c.Component).Name == "pkg-b");
resultB.LicensesConcluded.Should().BeEquivalentTo(["GPL-3.0"]);
resultB.Suppliers.Should().ContainSingle().Which.Name.Should().Be("Bob");
}
[TestMethod]
public void GetAllDependencyGraphs_ReturnsImmutableDictionaryWithContents()
{
// Setup an initial, simple graph.
var singleFileComponentRecorder = this.componentRecorder.CreateSingleFileComponentRecorder("/some/location");
// We want to take a look at how the class is used by it's friends
var internalsView = (ComponentRecorder.SingleFileComponentRecorder)singleFileComponentRecorder;
var graph = internalsView.DependencyGraph;
var component1 = new DependencyGraph.ComponentRefNode { Id = "component1" };
graph.AddComponent(component1);
var component2 = new DependencyGraph.ComponentRefNode { Id = "component2" };
graph.AddComponent(component2);
component2.DependedOnByIds.Add(component1.Id);
component1.DependencyIds.Add(component2.Id);
// Get readonly content from graph
var allGraphs = this.componentRecorder.GetDependencyGraphsByLocation();
var expectedGraph = allGraphs["/some/location"];
// Verify content looks correct
expectedGraph.Should().NotBeNull();
var allComponents = expectedGraph.GetComponents();
allComponents.Should().Contain(component1.Id);
allComponents.Should().Contain(component2.Id);
var component1Deps = expectedGraph.GetDependenciesForComponent(component1.Id);
component1Deps.Should().Contain(component2.Id);
var component2Deps = expectedGraph.GetDependenciesForComponent(component2.Id);
component2Deps.Should().BeEmpty();
// Verify dictionary is immutable (type enforces, make sure we can't interact with a downcasted dictionary)
var asDictionary = allGraphs as IDictionary<string, IDependencyGraph>;
asDictionary.Should().NotBeNull();
Action attemptedSet = () => asDictionary["/some/location"] = null;
attemptedSet.Should().Throw<NotSupportedException>();
Action attemptedClear = () => asDictionary.Clear();
attemptedClear.Should().Throw<NotSupportedException>();
}
[TestMethod]
public void GetAllDependencyGraphs_ReturnedGraphsAreImmutable()
{
// Setup an initial, simple graph.
var singleFileComponentRecorder = this.componentRecorder.CreateSingleFileComponentRecorder("/some/location");
// We want to take a look at how the class is used by it's friends
var internalsView = (ComponentRecorder.SingleFileComponentRecorder)singleFileComponentRecorder;
var graph = internalsView.DependencyGraph;
var component1 = new DependencyGraph.ComponentRefNode { Id = "component1" };
graph.AddComponent(component1);
var component2 = new DependencyGraph.ComponentRefNode { Id = "component2" };
graph.AddComponent(component2);
component2.DependedOnByIds.Add(component1.Id);
component1.DependencyIds.Add(component2.Id);
// Get readonly content from graph
var allGraphs = this.componentRecorder.GetDependencyGraphsByLocation();
var expectedGraph = allGraphs["/some/location"];
// Verify content looks correct
var component1Deps = expectedGraph.GetDependenciesForComponent(component1.Id);
component1Deps.Should().NotBeEmpty();
// Verify componentId set can't be interacted with after downcasting
var asCollection = component1Deps as ICollection<string>;
asCollection.Should().NotBeNull();
Action attemptedClear = () => asCollection.Clear();
attemptedClear.Should().Throw<NotSupportedException>();
Action attemptedAdd = () => asCollection.Add("should't work");
attemptedAdd.Should().Throw<NotSupportedException>();
}
[TestMethod]
public void GetDetectedComponents_BareAndRichAcrossFiles_BareSubsumedIntoRich()
{
var recorder1 = this.componentRecorder.CreateSingleFileComponentRecorder("package.json");
var recorder2 = this.componentRecorder.CreateSingleFileComponentRecorder("package-lock.json");
var bareComponent = new DetectedComponent(new NpmComponent("lodash", "4.17.23"));
var richComponent = new DetectedComponent(new NpmComponent("lodash", "4.17.23") { DownloadUrl = new Uri("https://registry.npmjs.org/lodash/-/lodash-4.17.23.tgz") });
recorder1.RegisterUsage(bareComponent);
recorder2.RegisterUsage(richComponent);
var results = this.componentRecorder.GetDetectedComponents().ToList();
// Only the rich entry should remain
results.Should().ContainSingle();
results[0].Component.Id.Should().Be(richComponent.Component.Id);
results[0].Component.Id.Should().NotBe(bareComponent.Component.Id);
}
[TestMethod]
public void GetDetectedComponents_BareAndMultipleRichAcrossFiles_BareMergesIntoAllRich()
{
var recorder1 = this.componentRecorder.CreateSingleFileComponentRecorder("package.json");
var recorder2 = this.componentRecorder.CreateSingleFileComponentRecorder("lockfile-a.json");
var recorder3 = this.componentRecorder.CreateSingleFileComponentRecorder("lockfile-b.json");
var bareComponent = new DetectedComponent(new NpmComponent("lodash", "4.17.23"))
{
LicensesConcluded = ["MIT"],
};
var richA = new DetectedComponent(new NpmComponent("lodash", "4.17.23") { DownloadUrl = new Uri("https://registry-a.example.com/lodash-4.17.23.tgz") });
var richB = new DetectedComponent(new NpmComponent("lodash", "4.17.23") { DownloadUrl = new Uri("https://registry-b.example.com/lodash-4.17.23.tgz") });
recorder1.RegisterUsage(bareComponent);
recorder2.RegisterUsage(richA);
recorder3.RegisterUsage(richB);
var results = this.componentRecorder.GetDetectedComponents().ToList();
// Two rich entries, bare dropped
results.Should().HaveCount(2);
results.Should().NotContain(c => c.Component.Id == bareComponent.Component.Id);
// Bare's license merged into both rich entries
results.Should().OnlyContain(c => c.LicensesConcluded != null && c.LicensesConcluded.Contains("MIT"));
}
[TestMethod]
public void GetDetectedComponents_TwoRichDifferentUrls_BothKeptSeparate()
{
var recorder1 = this.componentRecorder.CreateSingleFileComponentRecorder("lockfile-a.json");
var recorder2 = this.componentRecorder.CreateSingleFileComponentRecorder("lockfile-b.json");
var richA = new DetectedComponent(new NpmComponent("lodash", "4.17.23") { DownloadUrl = new Uri("https://registry-a.example.com/lodash-4.17.23.tgz") });
var richB = new DetectedComponent(new NpmComponent("lodash", "4.17.23") { DownloadUrl = new Uri("https://registry-b.example.com/lodash-4.17.23.tgz") });
recorder1.RegisterUsage(richA);
recorder2.RegisterUsage(richB);
var results = this.componentRecorder.GetDetectedComponents().ToList();
results.Should().HaveCount(2);
results.Should().Contain(c => c.Component.Id == richA.Component.Id);
results.Should().Contain(c => c.Component.Id == richB.Component.Id);
}
[TestMethod]
public void GetDetectedComponents_BareOnlyAcrossFiles_MergesIntoSingleBare()
{
var recorder1 = this.componentRecorder.CreateSingleFileComponentRecorder("package.json");
var recorder2 = this.componentRecorder.CreateSingleFileComponentRecorder("other-package.json");
var bare1 = new DetectedComponent(new NpmComponent("lodash", "4.17.23"))
{
LicensesConcluded = ["MIT"],
};
var bare2 = new DetectedComponent(new NpmComponent("lodash", "4.17.23"))
{
LicensesConcluded = ["Apache-2.0"],
};
recorder1.RegisterUsage(bare1);
recorder2.RegisterUsage(bare2);
var results = this.componentRecorder.GetDetectedComponents().ToList();
// Same Id → merged into one
results.Should().ContainSingle();
results[0].LicensesConcluded.Should().Contain("MIT");
results[0].LicensesConcluded.Should().Contain("Apache-2.0");
}
[TestMethod]
public void GetDetectedComponents_BareAndRich_MetadataMergedCorrectly()
{
var recorder1 = this.componentRecorder.CreateSingleFileComponentRecorder("package.json");
var recorder2 = this.componentRecorder.CreateSingleFileComponentRecorder("package-lock.json");
var bareComponent = new DetectedComponent(new NpmComponent("lodash", "4.17.23"))
{
LicensesConcluded = ["MIT"],
Suppliers = [new ActorInfo { Name = "Lodash Team", Type = "Organization" }],
};
var richComponent = new DetectedComponent(new NpmComponent("lodash", "4.17.23") { DownloadUrl = new Uri("https://registry.npmjs.org/lodash/-/lodash-4.17.23.tgz") })
{
LicensesConcluded = ["Apache-2.0"],
Suppliers = [new ActorInfo { Name = "Contoso", Type = "Organization" }],
};
recorder1.RegisterUsage(bareComponent);
recorder2.RegisterUsage(richComponent);
var results = this.componentRecorder.GetDetectedComponents().ToList();
results.Should().ContainSingle();
var result = results[0];
// Licenses from both bare and rich should be merged
result.LicensesConcluded.Should().HaveCount(2);
result.LicensesConcluded.Should().Contain("Apache-2.0");
result.LicensesConcluded.Should().Contain("MIT");
// Suppliers from both should be merged
result.Suppliers.Should().HaveCount(2);
result.Suppliers.Should().Contain(s => s.Name == "Lodash Team");
result.Suppliers.Should().Contain(s => s.Name == "Contoso");
}
}