-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathTypedComponentSerializationTests.cs
More file actions
539 lines (474 loc) · 23.4 KB
/
Copy pathTypedComponentSerializationTests.cs
File metadata and controls
539 lines (474 loc) · 23.4 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
#nullable disable
namespace Microsoft.ComponentDetection.Contracts.Tests;
using System;
using System.Linq;
using System.Text.Json;
using AwesomeAssertions;
using Microsoft.ComponentDetection.Contracts.BcdeModels;
using Microsoft.ComponentDetection.Contracts.Internal;
using Microsoft.ComponentDetection.Contracts.TypedComponent;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
[TestCategory("Governance/All")]
[TestCategory("Governance/ComponentDetection")]
public class TypedComponentSerializationTests
{
[TestMethod]
public void TypedComponent_Serialization_Other()
{
TypedComponent tc = new OtherComponent("SomeOtherComponent", "1.2.3", new Uri("https://sampleurl.com"), "SampleHash");
var result = JsonSerializer.Serialize(tc);
var deserializedTC = JsonSerializer.Deserialize<TypedComponent>(result);
deserializedTC.Should().BeOfType(typeof(OtherComponent));
var otherComponent = (OtherComponent)deserializedTC;
otherComponent.Name.Should().Be("SomeOtherComponent");
otherComponent.Version.Should().Be("1.2.3");
otherComponent.DownloadUrl.Should().Be(new Uri("https://sampleurl.com"));
otherComponent.Hash.Should().Be("SampleHash");
// OtherComponent includes DownloadUrl in its BaseId (it's a required identity field for Other).
// Since DownloadUrl is already part of BaseId, Id should not duplicate it.
otherComponent.BaseId.Should().Be("SomeOtherComponent 1.2.3 https://sampleurl.com/ - Other");
otherComponent.Id.Should().Be(otherComponent.BaseId, "DownloadUrl is already in BaseId, so Id should not append it again");
}
[TestMethod]
public void TypedComponent_Serialization_NuGet()
{
var testComponentName = "SomeNuGetComponent";
var testVersion = "1.2.3";
string[] testAuthors = ["John Doe", "Jane Doe"];
TypedComponent tc = new NuGetComponent(testComponentName, testVersion, testAuthors);
var result = JsonSerializer.Serialize(tc);
var deserializedTC = JsonSerializer.Deserialize<TypedComponent>(result);
deserializedTC.Should().BeOfType(typeof(NuGetComponent));
var nugetComponent = (NuGetComponent)deserializedTC;
nugetComponent.Name.Should().Be(testComponentName);
nugetComponent.Version.Should().Be(testVersion);
nugetComponent.Authors.Should().BeEquivalentTo(testAuthors);
}
[TestMethod]
public void TypedComponent_Serialization_Npm()
{
var npmAuthor = new NpmAuthor("someAuthorName", "someAuthorEmail");
TypedComponent npmCompObj = new NpmComponent("SomeNpmComponent", "1.2.3")
{
Author = npmAuthor,
};
var result = JsonSerializer.Serialize(npmCompObj);
var deserializedTC = JsonSerializer.Deserialize<TypedComponent>(result);
deserializedTC.Should().BeOfType(typeof(NpmComponent));
var npmComponent = (NpmComponent)deserializedTC;
npmComponent.Name.Should().Be("SomeNpmComponent");
npmComponent.Version.Should().Be("1.2.3");
npmComponent.Author.Should().BeEquivalentTo(npmAuthor);
}
[TestMethod]
public void TypedComponent_Serialization_Npm_WithHash()
{
TypedComponent tc = new NpmComponent("SomeNpmComponent", "1.2.3", "sha1-placeholder");
var result = JsonSerializer.Serialize(tc);
var deserializedTC = JsonSerializer.Deserialize<TypedComponent>(result);
deserializedTC.Should().BeOfType(typeof(NpmComponent));
var npmComponent = (NpmComponent)deserializedTC;
npmComponent.Name.Should().Be("SomeNpmComponent");
npmComponent.Version.Should().Be("1.2.3");
npmComponent.Hash.Should().Be("sha1-placeholder");
}
[TestMethod]
public void TypedComponent_Serialization_Maven()
{
TypedComponent tc = new MavenComponent("SomeGroupId", "SomeArtifactId", "1.2.3");
var result = JsonSerializer.Serialize(tc);
var deserializedTC = JsonSerializer.Deserialize<TypedComponent>(result);
deserializedTC.Should().BeOfType(typeof(MavenComponent));
var mavenComponent = (MavenComponent)deserializedTC;
mavenComponent.GroupId.Should().Be("SomeGroupId");
mavenComponent.ArtifactId.Should().Be("SomeArtifactId");
mavenComponent.Version.Should().Be("1.2.3");
}
[TestMethod]
public void TypedComponent_Serialization_Git()
{
TypedComponent tc = new GitComponent(new Uri("http://some.com/git/url.git"), "SomeHash");
var result = JsonSerializer.Serialize(tc);
var deserializedTC = JsonSerializer.Deserialize<TypedComponent>(result);
deserializedTC.Should().BeOfType(typeof(GitComponent));
var gitComponent = (GitComponent)deserializedTC;
gitComponent.RepositoryUrl.Should().Be(new Uri("http://some.com/git/url.git"));
gitComponent.CommitHash.Should().Be("SomeHash");
}
[TestMethod]
public void TypedComponent_Serialization_RubyGems()
{
TypedComponent tc = new RubyGemsComponent("SomeGem", "1.2.3", "SampleSource");
var result = JsonSerializer.Serialize(tc);
var deserializedTC = JsonSerializer.Deserialize<TypedComponent>(result);
deserializedTC.Should().BeOfType(typeof(RubyGemsComponent));
var rubyGemComponent = (RubyGemsComponent)deserializedTC;
rubyGemComponent.Name.Should().Be("SomeGem");
rubyGemComponent.Version.Should().Be("1.2.3");
rubyGemComponent.Source.Should().Be("SampleSource");
}
[TestMethod]
public void TypedComponent_Serialization_Cargo()
{
TypedComponent tc = new CargoComponent("SomeCargoPackage", "1.2.3");
var result = JsonSerializer.Serialize(tc);
var deserializedTC = JsonSerializer.Deserialize<TypedComponent>(result);
deserializedTC.Should().BeOfType(typeof(CargoComponent));
var cargoComponent = (CargoComponent)deserializedTC;
cargoComponent.Name.Should().Be("SomeCargoPackage");
cargoComponent.Version.Should().Be("1.2.3");
}
[TestMethod]
public void TypedComponent_Serialization_Conan()
{
var md5 = Guid.NewGuid().ToString();
var sha1Hash = Guid.NewGuid().ToString();
TypedComponent tc = new ConanComponent("SomeConanPackage", "1.2.3", md5, sha1Hash);
var result = JsonSerializer.Serialize(tc);
var deserializedTC = JsonSerializer.Deserialize<TypedComponent>(result);
deserializedTC.Should().BeOfType(typeof(ConanComponent));
var conanComponent = (ConanComponent)deserializedTC;
conanComponent.Name.Should().Be("SomeConanPackage");
conanComponent.Version.Should().Be("1.2.3");
conanComponent.Md5Hash.Should().Be(md5);
conanComponent.Sha1Hash.Should().Be(sha1Hash);
conanComponent.PackageSourceURL.Should().Be("https://conan.io/center/recipes/SomeConanPackage?version=1.2.3");
}
[TestMethod]
public void TypedComponent_Serialization_Pip()
{
TypedComponent tc = new PipComponent("SomePipPackage", "1.2.3");
var result = JsonSerializer.Serialize(tc);
var deserializedTC = JsonSerializer.Deserialize<TypedComponent>(result);
deserializedTC.Should().BeOfType(typeof(PipComponent));
var pipComponent = (PipComponent)deserializedTC;
pipComponent.Name.Should().Be("SomePipPackage");
pipComponent.Version.Should().Be("1.2.3");
}
[TestMethod]
public void TypedComponent_Serialization_Go()
{
TypedComponent tc = new GoComponent("github.com/example/SomeGoPackage", "1.2.3", "SomeHash");
var result = JsonSerializer.Serialize(tc);
var deserializedTC = JsonSerializer.Deserialize<TypedComponent>(result);
deserializedTC.Should().BeOfType(typeof(GoComponent));
var goComponent = (GoComponent)deserializedTC;
goComponent.Name.Should().Be("github.com/example/SomeGoPackage");
goComponent.Version.Should().Be("1.2.3");
goComponent.Hash.Should().Be("SomeHash");
}
[TestMethod]
public void TypedComponent_Serialization_DockerImage()
{
TypedComponent tc = new DockerImageComponent("SomeImageHash", "SomeImageName", "SomeImageTag");
var result = JsonSerializer.Serialize(tc);
var deserializedTC = JsonSerializer.Deserialize<TypedComponent>(result);
deserializedTC.Should().BeOfType(typeof(DockerImageComponent));
var dockerImageComponent = (DockerImageComponent)deserializedTC;
dockerImageComponent.Digest.Should().Be("SomeImageHash");
dockerImageComponent.Name.Should().Be("SomeImageName");
dockerImageComponent.Tag.Should().Be("SomeImageTag");
}
[TestMethod]
public void TypedComponent_Serialization_PodComponent()
{
TypedComponent tc = new PodComponent("SomePodName", "SomePodVersion", "SomeSpecRepo");
var result = JsonSerializer.Serialize(tc);
var deserializedTC = JsonSerializer.Deserialize<TypedComponent>(result);
deserializedTC.Should().BeOfType(typeof(PodComponent));
var podComponent = (PodComponent)deserializedTC;
podComponent.Name.Should().Be("SomePodName");
podComponent.Version.Should().Be("SomePodVersion");
podComponent.SpecRepo.Should().Be("SomeSpecRepo");
}
[TestMethod]
public void TypedComponent_Serialization_LinuxComponent()
{
TypedComponent tc = new LinuxComponent("SomeLinuxDistribution", "SomeLinuxRelease", "SomeLinuxComponentName", "SomeLinuxComponentVersion");
var result = JsonSerializer.Serialize(tc);
var deserializedTC = JsonSerializer.Deserialize<TypedComponent>(result);
deserializedTC.Should().BeOfType(typeof(LinuxComponent));
var linuxComponent = (LinuxComponent)deserializedTC;
linuxComponent.Distribution.Should().Be("SomeLinuxDistribution");
linuxComponent.Release.Should().Be("SomeLinuxRelease");
linuxComponent.Name.Should().Be("SomeLinuxComponentName");
linuxComponent.Version.Should().Be("SomeLinuxComponentVersion");
}
[TestMethod]
public void TypedComponent_Deserialization_UnknownComponentType_ReturnsNull()
{
// Simulate a JSON payload with a component type that doesn't exist in the current version
// This tests forward compatibility when new component types are added
var unknownComponentJson = """
{
"type": "FutureComponentType",
"name": "SomeComponent",
"version": "1.0.0"
}
""";
var deserializedTC = JsonSerializer.Deserialize<TypedComponent>(unknownComponentJson);
deserializedTC.Should().BeNull();
}
[TestMethod]
public void TypedComponent_Deserialization_InvalidComponentType_ReturnsNull()
{
// Test with a completely invalid/malformed type value
var invalidComponentJson = """
{
"type": "Not_A_Valid_Enum_Value_12345",
"name": "SomeComponent",
"version": "1.0.0"
}
""";
var deserializedTC = JsonSerializer.Deserialize<TypedComponent>(invalidComponentJson);
deserializedTC.Should().BeNull();
}
[TestMethod]
public void TypedComponentMapping_AllComponentTypes_HaveMapping()
{
// Ensure every ComponentType enum value has a corresponding entry in the mapping
// This prevents forgetting to add new component types to the serialization mapping
var allComponentTypes = Enum.GetValues(typeof(ComponentType)).Cast<ComponentType>();
var mappedTypes = TypedComponentMapping.TypeDiscriminatorToType;
foreach (var componentType in allComponentTypes)
{
var typeName = componentType.ToString();
mappedTypes.Should().ContainKey(typeName, $"ComponentType.{typeName} should have a mapping in TypedComponentMapping");
}
}
[TestMethod]
public void TypedComponentMapping_AllMappedTypes_AreTypedComponentSubclasses()
{
// Ensure all mapped types are actually subclasses of TypedComponent
foreach (var kvp in TypedComponentMapping.TypeDiscriminatorToType)
{
kvp.Value.Should().BeAssignableTo<TypedComponent>($"Mapped type for '{kvp.Key}' should be a TypedComponent subclass");
}
}
[TestMethod]
public void TypedComponentMapping_AllMappedTypes_AreUnique()
{
// Ensure no two discriminators map to the same type
var mappedTypes = TypedComponentMapping.TypeDiscriminatorToType.Values.ToList();
var uniqueTypes = mappedTypes.Distinct().ToList();
mappedTypes.Should().HaveCount(uniqueTypes.Count, "Each component type should map to a unique concrete type");
}
[TestMethod]
public void TypedComponent_Serialization_TypePropertyNotDuplicated()
{
// Ensure the "type" property is only serialized once at the root level, not duplicated
TypedComponent tc = new NpmComponent("TestPackage", "1.0.0");
var json = JsonSerializer.Serialize(tc);
// Parse the JSON and check root-level properties only
using var doc = JsonDocument.Parse(json);
var typeProperties = doc.RootElement.EnumerateObject()
.Count(p => p.Name.Equals("type", StringComparison.OrdinalIgnoreCase));
typeProperties.Should().Be(1, "The 'type' property should appear exactly once at the root level in the serialized JSON");
}
[TestMethod]
public void TypedComponent_Serialization_AllComponentTypes_TypePropertyNotDuplicated()
{
// Test all component types to ensure none have duplicate "type" properties at the root level
var testComponents = new TypedComponent[]
{
new NpmComponent("test", "1.0.0"),
new NuGetComponent("test", "1.0.0"),
new MavenComponent("group", "artifact", "1.0.0"),
new PipComponent("test", "1.0.0"),
new GoComponent("github.com/example/test", "1.0.0"),
new CargoComponent("test", "1.0.0"),
new RubyGemsComponent("test", "1.0.0"),
new GitComponent(new Uri("https://github.com/test/test"), "abc123"),
new OtherComponent("test", "1.0.0", new Uri("https://example.com"), "hash"),
};
foreach (var component in testComponents)
{
var json = JsonSerializer.Serialize(component);
// Parse the JSON and check root-level properties only
using var doc = JsonDocument.Parse(json);
var typeProperties = doc.RootElement.EnumerateObject()
.Count(p => p.Name.Equals("type", StringComparison.OrdinalIgnoreCase));
typeProperties.Should().Be(1, $"The 'type' property should appear exactly once at the root level for {component.Type}");
}
}
[TestMethod]
public void TypedComponentMapping_TryGetType_NullDiscriminator_ReturnsFalseAndNull()
{
var result = TypedComponentMapping.TryGetType(null, out var targetType);
result.Should().BeFalse("TryGetType should return false for null discriminator");
targetType.Should().BeNull("targetType should be null for null discriminator");
}
[TestMethod]
public void TypedComponentMapping_TryGetType_EmptyDiscriminator_ReturnsFalseAndNull()
{
var result = TypedComponentMapping.TryGetType(string.Empty, out var targetType);
result.Should().BeFalse("TryGetType should return false for empty discriminator");
targetType.Should().BeNull("targetType should be null for empty discriminator");
}
[TestMethod]
public void TypedComponentMapping_TryGetType_WhitespaceDiscriminator_ReturnsFalseAndNull()
{
var result = TypedComponentMapping.TryGetType(" ", out var targetType);
result.Should().BeFalse("TryGetType should return false for whitespace-only discriminator");
targetType.Should().BeNull("targetType should be null for whitespace-only discriminator");
}
[TestMethod]
public void TypedComponent_Serialization_CppSdk()
{
TypedComponent tc = new CppSdkComponent("SomeCppSdk", "1.2.3");
var result = JsonSerializer.Serialize(tc);
var deserializedTC = JsonSerializer.Deserialize<TypedComponent>(result);
deserializedTC.Should().BeOfType(typeof(CppSdkComponent));
var cppSdkComponent = (CppSdkComponent)deserializedTC;
cppSdkComponent.Name.Should().Be("SomeCppSdk");
cppSdkComponent.Version.Should().Be("1.2.3");
}
[TestMethod]
public void TypedComponent_Serialization_Properties_Roundtrip()
{
ActorInfo[] authorsInfo =
[
new ActorInfo { Name = "Test Author", Type = "Person" },
new ActorInfo { Name = "Test Org", Email = "info@test-org.example.com", Type = "Organization" },
];
TypedComponent tc = new NuGetComponent("TestPackage", "13.0.4")
{
Licenses = ["MIT"],
AuthorsInfo = authorsInfo,
DownloadUrl = new Uri("https://www.example.com/api/v2/package/TestPackage/13.0.4"),
SourceUrl = new Uri("https://github.com/test-org/TestPackage"),
};
// Verify purl is present before serialization
tc.PackageUrl.ToString().Should().Be("pkg:nuget/TestPackage@13.0.4");
var json = JsonSerializer.Serialize(tc);
var deserialized = JsonSerializer.Deserialize<TypedComponent>(json);
deserialized.Should().BeOfType(typeof(NuGetComponent));
var nuget = (NuGetComponent)deserialized;
nuget.Name.Should().Be("TestPackage");
nuget.Version.Should().Be("13.0.4");
nuget.Licenses.Should().ContainSingle().Which.Should().Be("MIT");
nuget.AuthorsInfo.Should().HaveCount(2);
nuget.AuthorsInfo[0].Name.Should().Be("Test Author");
nuget.AuthorsInfo[0].Type.Should().Be("Person");
nuget.AuthorsInfo[1].Name.Should().Be("Test Org");
nuget.AuthorsInfo[1].Email.Should().Be("info@test-org.example.com");
nuget.DownloadUrl.Should().Be(new Uri("https://www.example.com/api/v2/package/TestPackage/13.0.4"));
nuget.SourceUrl.Should().Be(new Uri("https://github.com/test-org/TestPackage"));
nuget.PackageUrl.ToString().Should().Be("pkg:nuget/TestPackage@13.0.4");
}
[TestMethod]
public void TypedComponent_Serialization_Properties_NullOmittedFromJson()
{
TypedComponent tc = new NpmComponent("lodash", "4.17.21");
var json = JsonSerializer.Serialize(tc);
// These properties should not appear in JSON when null (verify structurally, not via substring)
using (var document = JsonDocument.Parse(json))
{
var root = document.RootElement;
root.TryGetProperty("licenses", out _).Should().BeFalse();
root.TryGetProperty("authorsInfo", out _).Should().BeFalse();
root.TryGetProperty("downloadUrl", out _).Should().BeFalse();
root.TryGetProperty("sourceUrl", out _).Should().BeFalse();
}
// Component should still deserialize correctly
var deserialized = JsonSerializer.Deserialize<TypedComponent>(json);
deserialized.Should().BeOfType(typeof(NpmComponent));
var npm = (NpmComponent)deserialized;
npm.Licenses.Should().BeNull();
npm.AuthorsInfo.Should().BeNull();
npm.DownloadUrl.Should().BeNull();
npm.SourceUrl.Should().BeNull();
}
[TestMethod]
public void TypedComponent_Deserialization_OldJsonWithoutNewProperties_BackwardCompatible()
{
// Simulates JSON produced by an older version of Component Detection that has no new properties
var oldJson = """
{
"type": "NuGet",
"name": "TestPackage",
"version": "1.0.0",
"authors": ["Test Author"],
"id": "TestPackage 1.0.0 - NuGet"
}
""";
var deserialized = JsonSerializer.Deserialize<TypedComponent>(oldJson);
deserialized.Should().BeOfType(typeof(NuGetComponent));
var nuget = (NuGetComponent)deserialized;
nuget.Name.Should().Be("TestPackage");
nuget.Version.Should().Be("1.0.0");
nuget.Authors.Should().ContainSingle().Which.Should().Be("Test Author");
// New properties should be null when absent from JSON
nuget.Licenses.Should().BeNull();
nuget.AuthorsInfo.Should().BeNull();
nuget.DownloadUrl.Should().BeNull();
nuget.SourceUrl.Should().BeNull();
}
[TestMethod]
public void TypedComponent_Id_MatchesBaseId_WhenNoOptionalUrls()
{
var tc = new NuGetComponent("TestPackage", "1.0.0");
tc.Id.Should().Be(tc.BaseId);
tc.BaseId.Should().Be("TestPackage 1.0.0 - NuGet");
}
[TestMethod]
public void TypedComponent_Id_IncludesDownloadUrl_WhenPresent()
{
var tc = new NuGetComponent("TestPackage", "1.0.0")
{
DownloadUrl = new Uri("https://example.com/package/1.0.0"),
};
tc.BaseId.Should().Be("TestPackage 1.0.0 - NuGet");
tc.Id.Should().Be("TestPackage 1.0.0 - NuGet [DownloadUrl:https://example.com/package/1.0.0]");
}
[TestMethod]
public void TypedComponent_Id_IncludesSourceUrl_WhenPresent()
{
var tc = new NuGetComponent("TestPackage", "1.0.0")
{
SourceUrl = new Uri("https://github.com/test-org/TestPackage"),
};
tc.BaseId.Should().Be("TestPackage 1.0.0 - NuGet");
tc.Id.Should().Be("TestPackage 1.0.0 - NuGet [SourceUrl:https://github.com/test-org/TestPackage]");
}
[TestMethod]
public void TypedComponent_Id_IncludesBothUrls_WhenPresent()
{
var tc = new NuGetComponent("TestPackage", "1.0.0")
{
DownloadUrl = new Uri("https://example.com/package/1.0.0"),
SourceUrl = new Uri("https://github.com/test-org/TestPackage"),
};
tc.BaseId.Should().Be("TestPackage 1.0.0 - NuGet");
tc.Id.Should().Be("TestPackage 1.0.0 - NuGet [DownloadUrl:https://example.com/package/1.0.0 SourceUrl:https://github.com/test-org/TestPackage]");
}
[TestMethod]
public void MavenComponent_Id_ExcludesDownloadUrlAndSourceUrl_WhenSet()
{
// MavenComponent overrides GetExtendedIdProperties to keep Id stable across detectors that
// may or may not populate DownloadUrl / SourceUrl (e.g. CDS surfaces SourceUrl from the POM,
// and DownloadUrl is deterministic from GAV — neither should affect identity).
var tc = new MavenComponent("com.google.guava", "guava", "33.0-jre")
{
DownloadUrl = new Uri("https://repo1.maven.org/maven2/com/google/guava/guava/33.0-jre/guava-33.0-jre.jar"),
SourceUrl = new Uri("https://github.com/google/guava"),
};
tc.BaseId.Should().Be("com.google.guava guava 33.0-jre - Maven");
tc.Id.Should().Be(tc.BaseId, "DownloadUrl and SourceUrl must not affect MavenComponent identity");
}
[TestMethod]
public void GitComponent_Id_ExcludesDownloadUrlAndSourceUrl_WhenSet()
{
// GitComponent overrides GetExtendedIdProperties for the same reason as MavenComponent:
// SourceUrl would duplicate RepositoryUrl, and DownloadUrl (the github archive URL) is deterministic.
var repo = new Uri("https://github.com/google/guava");
var tc = new GitComponent(repo, "abcdef1234567890")
{
DownloadUrl = new Uri("https://github.com/google/guava/archive/abcdef1234567890.zip"),
SourceUrl = repo,
};
tc.BaseId.Should().Be("https://github.com/google/guava : abcdef1234567890 - Git");
tc.Id.Should().Be(tc.BaseId, "DownloadUrl and SourceUrl must not affect GitComponent identity");
}
}