Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class TypedComponentConverter : JsonConverter
{ ComponentType.Vcpkg, typeof(VcpkgComponent) },
{ ComponentType.Spdx, typeof(SpdxComponent) },
{ ComponentType.DotNet, typeof(DotNetComponent) },
{ ComponentType.CppSdk, typeof(CppSdkComponent) },
};

public override bool CanWrite
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,7 @@ public enum ComponentType : byte

[EnumMember]
DotNet = 19,

[EnumMember]
CppSdk = 20,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#nullable disable
namespace Microsoft.ComponentDetection.Contracts.TypedComponent;

using System.Collections.Generic;
using System.Text.Json.Serialization;
using PackageUrl;

/// <summary>
/// Represents a C++ SDK component.
/// </summary>
public class CppSdkComponent : TypedComponent
{
/// <summary>
/// Initializes a new instance of the <see cref="CppSdkComponent"/> class.
/// </summary>
public CppSdkComponent()
{
/* Reserved for deserialization */
}

/// <summary>
/// Initializes a new instance of the <see cref="CppSdkComponent"/> class.
/// </summary>
/// <param name="name">The name of the component.</param>
/// <param name="version">The version of the component.</param>
public CppSdkComponent(string name, string version)
{
this.Name = this.ValidateRequiredInput(name, nameof(this.Name), nameof(ComponentType.CppSdk));
this.Version = this.ValidateRequiredInput(version, nameof(this.Version), nameof(ComponentType.CppSdk));
}

[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("version")]
public string Version { get; set; }

[JsonIgnore]
public override ComponentType Type => ComponentType.CppSdk;

[JsonPropertyName("packageUrl")]
public override PackageURL PackageUrl
{
get
{
var qualifiers = new SortedDictionary<string, string>
Comment thread
grvillic marked this conversation as resolved.
{
{ "type", "cppsdk" },
};
return new PackageURL("generic", null, this.Name, this.Version, qualifiers, null);
}
}

protected override string ComputeId() => $"{this.Name} {this.Version} - {this.Type}";
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace Microsoft.ComponentDetection.Contracts.TypedComponent;
[JsonDerivedType(typeof(CargoComponent), typeDiscriminator: nameof(ComponentType.Cargo))]
[JsonDerivedType(typeof(ConanComponent), typeDiscriminator: nameof(ComponentType.Conan))]
[JsonDerivedType(typeof(CondaComponent), typeDiscriminator: nameof(ComponentType.Conda))]
[JsonDerivedType(typeof(CppSdkComponent), typeDiscriminator: nameof(ComponentType.CppSdk))]
[JsonDerivedType(typeof(DockerImageComponent), typeDiscriminator: nameof(ComponentType.DockerImage))]
[JsonDerivedType(typeof(DockerReferenceComponent), typeDiscriminator: nameof(ComponentType.DockerReference))]
[JsonDerivedType(typeof(DotNetComponent), typeDiscriminator: nameof(ComponentType.DotNet))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#nullable disable
namespace Microsoft.ComponentDetection.Contracts.Tests;

using System;
using AwesomeAssertions;
using Microsoft.ComponentDetection.Contracts.TypedComponent;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
[TestCategory("Governance/All")]
[TestCategory("Governance/ComponentDetection")]
public class CppSdkComponentTests
{
[TestMethod]
public void Constructor_ShouldInitializeProperties()
{
var name = "AwesomeCppLibrary";
var version = "1.2.3";

var component = new CppSdkComponent(name, version);

component.Name.Should().Be(name);
component.Version.Should().Be(version);
component.Type.Should().Be(ComponentType.CppSdk);
component.Id.Should().Be($"{name} {version} - {component.Type}");
}

[TestMethod]
public void Constructor_ShouldThrowException_WhenNameIsNull()
{
Action action = () => new CppSdkComponent(null, "1.2.3");
action.Should().Throw<ArgumentException>().WithMessage("*Name*");
}

[TestMethod]
public void Constructor_ShouldThrowException_WhenNameIsWhitespace()
{
Action action = () => new CppSdkComponent(" ", "1.2.3");
action.Should().Throw<ArgumentException>().WithMessage("*Name*");
}

[TestMethod]
public void Constructor_ShouldThrowException_WhenNameIsEmpty()
{
Action action = () => new CppSdkComponent(string.Empty, "1.2.3");
action.Should().Throw<ArgumentException>().WithMessage("*Name*");
}

[TestMethod]
public void Constructor_ShouldThrowException_WhenVersionIsNull()
{
Action action = () => new CppSdkComponent("AwesomeCppLibrary", null);
action.Should().Throw<ArgumentException>().WithMessage("*Version*");
}

[TestMethod]
public void Constructor_ShouldThrowException_WhenVersionIsWhitespace()
{
Action action = () => new CppSdkComponent("AwesomeCppLibrary", " ");
action.Should().Throw<ArgumentException>().WithMessage("*Version*");
}

[TestMethod]
public void Constructor_ShouldThrowException_WhenVersionIsEmpty()
{
Action action = () => new CppSdkComponent("AwesomeCppLibrary", string.Empty);
action.Should().Throw<ArgumentException>().WithMessage("*Version*");
}

[TestMethod]
public void PackageUrl_ShouldReturnCorrectFormat()
{
var name = "AwesomeCppLibrary";
var version = "1.2.3";
var component = new CppSdkComponent(name, version);

var packageUrl = component.PackageUrl;

packageUrl.Type.Should().Be("generic");
#pragma warning disable CA1308 // PackageURL normalizes to lowercase
packageUrl.Name.Should().Be(name.ToLowerInvariant());
#pragma warning restore CA1308
packageUrl.Version.Should().Be(version);
packageUrl.Namespace.Should().BeNull();
packageUrl.Qualifiers.Should().ContainKey("type");
packageUrl.Qualifiers["type"].Should().Be("cppsdk");
}
Comment thread
grvillic marked this conversation as resolved.
}
Comment thread
grvillic marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,16 @@ public void TypedComponent_Serialization_LinuxComponent()
linuxComponent.Name.Should().Be("SomeLinuxComponentName");
linuxComponent.Version.Should().Be("SomeLinuxComponentVersion");
}

[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");
}
}
Loading