finish off sprint advisories and sprints
This commit is contained in:
@@ -67,41 +67,130 @@ public class PolicyPackSchemaTests
|
||||
_schema.Should().NotBeNull("Schema should be parseable");
|
||||
}
|
||||
|
||||
[Fact(Skip = "YAML-to-JSON conversion produces type mismatches; schema validation requires proper YAML type handling")]
|
||||
[Fact]
|
||||
public void StarterDay1Policy_ValidatesAgainstSchema()
|
||||
{
|
||||
var policyPath = Path.Combine(_testDataPath, "starter-day1.yaml");
|
||||
if (!File.Exists(policyPath))
|
||||
{
|
||||
Assert.True(true, "Test fixture not yet created");
|
||||
return;
|
||||
}
|
||||
|
||||
var yamlContent = File.ReadAllText(policyPath);
|
||||
var jsonNode = YamlToJson(yamlContent);
|
||||
var jsonElement = YamlToJsonElement(yamlContent);
|
||||
|
||||
var options = new EvaluationOptions
|
||||
{
|
||||
OutputFormat = OutputFormat.List
|
||||
};
|
||||
var result = _schema.Evaluate(jsonNode, options);
|
||||
var result = _schema.Evaluate(jsonElement, options);
|
||||
result.IsValid.Should().BeTrue(
|
||||
result.IsValid ? "" : $"Policy should validate against schema. Errors: {FormatErrors(result)}");
|
||||
}
|
||||
|
||||
[Theory(Skip = "YAML-to-JSON conversion produces type mismatches; schema validation requires proper YAML type handling")]
|
||||
[Theory]
|
||||
[InlineData("production.yaml")]
|
||||
[InlineData("staging.yaml")]
|
||||
[InlineData("development.yaml")]
|
||||
public void EnvironmentOverride_ValidatesAgainstSchema(string fileName)
|
||||
{
|
||||
var overridePath = Path.Combine(_testDataPath, "overrides", fileName);
|
||||
if (!File.Exists(overridePath))
|
||||
{
|
||||
Assert.True(true, $"Test fixture {fileName} not yet created");
|
||||
return;
|
||||
}
|
||||
|
||||
var yamlContent = File.ReadAllText(overridePath);
|
||||
var jsonNode = YamlToJson(yamlContent);
|
||||
var jsonElement = YamlToJsonElement(yamlContent);
|
||||
|
||||
var options = new EvaluationOptions
|
||||
{
|
||||
OutputFormat = OutputFormat.List
|
||||
};
|
||||
var result = _schema.Evaluate(jsonNode, options);
|
||||
var result = _schema.Evaluate(jsonElement, options);
|
||||
result.IsValid.Should().BeTrue(
|
||||
result.IsValid ? "" : $"{fileName} should validate against schema. Errors: {FormatErrors(result)}");
|
||||
}
|
||||
|
||||
private static JsonNode? YamlToJsonNode(string yaml)
|
||||
{
|
||||
// Use YamlDotNet with proper type handling
|
||||
var deserializer = new DeserializerBuilder()
|
||||
.WithNamingConvention(CamelCaseNamingConvention.Instance)
|
||||
.Build();
|
||||
|
||||
// Deserialize to dynamic object to preserve types
|
||||
using var reader = new StringReader(yaml);
|
||||
var yamlStream = new YamlDotNet.RepresentationModel.YamlStream();
|
||||
yamlStream.Load(reader);
|
||||
|
||||
if (yamlStream.Documents.Count == 0)
|
||||
return null;
|
||||
|
||||
var rootNode = yamlStream.Documents[0].RootNode;
|
||||
return ConvertYamlNodeToJsonNode(rootNode);
|
||||
}
|
||||
|
||||
private static JsonNode? ConvertYamlNodeToJsonNode(YamlDotNet.RepresentationModel.YamlNode node)
|
||||
{
|
||||
return node switch
|
||||
{
|
||||
YamlDotNet.RepresentationModel.YamlMappingNode mapping => ConvertMapping(mapping),
|
||||
YamlDotNet.RepresentationModel.YamlSequenceNode sequence => ConvertSequence(sequence),
|
||||
YamlDotNet.RepresentationModel.YamlScalarNode scalar => ConvertScalar(scalar),
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
|
||||
private static JsonObject ConvertMapping(YamlDotNet.RepresentationModel.YamlMappingNode mapping)
|
||||
{
|
||||
var obj = new JsonObject();
|
||||
foreach (var entry in mapping.Children)
|
||||
{
|
||||
var key = ((YamlDotNet.RepresentationModel.YamlScalarNode)entry.Key).Value ?? "";
|
||||
obj[key] = ConvertYamlNodeToJsonNode(entry.Value);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
private static JsonArray ConvertSequence(YamlDotNet.RepresentationModel.YamlSequenceNode sequence)
|
||||
{
|
||||
var arr = new JsonArray();
|
||||
foreach (var item in sequence.Children)
|
||||
{
|
||||
arr.Add(ConvertYamlNodeToJsonNode(item));
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
private static JsonNode? ConvertScalar(YamlDotNet.RepresentationModel.YamlScalarNode scalar)
|
||||
{
|
||||
var value = scalar.Value;
|
||||
if (value is null) return null;
|
||||
|
||||
// Try to parse as proper JSON types
|
||||
if (bool.TryParse(value, out var boolVal))
|
||||
return JsonValue.Create(boolVal);
|
||||
if (int.TryParse(value, out var intVal))
|
||||
return JsonValue.Create(intVal);
|
||||
if (double.TryParse(value, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out var doubleVal) && value.Contains('.'))
|
||||
return JsonValue.Create(doubleVal);
|
||||
if (value.Equals("null", StringComparison.OrdinalIgnoreCase))
|
||||
return null;
|
||||
|
||||
return JsonValue.Create(value);
|
||||
}
|
||||
|
||||
private static JsonElement YamlToJsonElement(string yaml)
|
||||
{
|
||||
var node = YamlToJsonNode(yaml);
|
||||
if (node is null) return default;
|
||||
var jsonString = node.ToJsonString();
|
||||
return JsonDocument.Parse(jsonString).RootElement;
|
||||
}
|
||||
|
||||
[Trait("Category", TestCategories.Unit)]
|
||||
[Fact]
|
||||
public void Schema_RequiresApiVersion()
|
||||
|
||||
Reference in New Issue
Block a user