Skip to content

Commit

Permalink
tests: verify json schema (#638)
Browse files Browse the repository at this point in the history
  • Loading branch information
melotic authored Jul 7, 2023
1 parent bd522a4 commit 9724a88
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 48 deletions.
3 changes: 2 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<PackageVersion Include="MSTest.TestAdapter" Version="3.0.4" />
<PackageVersion Include="MSTest.TestFramework" Version="3.0.4" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="Newtonsoft.Json.Schema" Version="3.0.15" />
<PackageVersion Include="NuGet.ProjectModel" Version="6.6.1" />
<PackageVersion Include="NuGet.Versioning" Version="6.6.1" />
<PackageVersion Include="packageurl-dotnet" Version="1.0.0" />
Expand All @@ -48,4 +49,4 @@
<PackageVersion Include="Faker.net" Version="2.0.154" />
<PackageVersion Include="Valleysoft.DockerfileModel" Version="1.1.0" />
</ItemGroup>
</Project>
</Project>
92 changes: 46 additions & 46 deletions docs/schema/manifest.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,24 @@
"null"
],
"items": {
"type": "integer",
"type": "string",
"enum": [
0,
1,
2,
3,
4,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16
"Other",
"NuGet",
"Npm",
"Maven",
"Git",
"RubyGems",
"Cargo",
"Pip",
"Go",
"DockerImage",
"Pod",
"Linux",
"Conda",
"Spdx",
"Vcpkg",
"DockerReference"
]
}
}
Expand Down Expand Up @@ -262,16 +262,16 @@
},
"dependencyScope": {
"type": [
"integer",
"string",
"null"
],
"enum": [
null,
0,
1,
2,
3,
4
"MavenCompile",
"MavenRuntime",
"MavenProvided",
"MavenSystem",
"MavenTest"
]
},
"topLevelReferrers": {
Expand Down Expand Up @@ -326,24 +326,24 @@
],
"properties": {
"type": {
"type": "integer",
"type": "string",
"enum": [
0,
1,
2,
3,
4,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16
"Other",
"NuGet",
"Npm",
"Maven",
"Git",
"RubyGems",
"Cargo",
"Pip",
"Go",
"DockerImage",
"Pod",
"Linux",
"Conda",
"Spdx",
"Vcpkg",
"DockerReference"
]
},
"id": {
Expand Down Expand Up @@ -394,13 +394,13 @@
}
},
"resultCode": {
"type": "integer",
"type": "string",
"enum": [
0,
1,
2,
3,
4
"Success",
"PartialSuccess",
"Error",
"InputError",
"TimeoutError"
]
},
"sourceDirectory": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
namespace Microsoft.ComponentDetection.VerificationTests;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.ComponentDetection.Contracts.BcdeModels;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Schema.Generation;

[TestClass]
public class JsonSchemaTests
{
private string manifestFile;
private JSchema repoManifestSchema;
private DirectoryInfo artifactsDir;

[TestInitialize]
public async Task InitializeAsync()
{
var docsDir = new DirectoryInfo(Path.Combine(Environment.GetEnvironmentVariable("GITHUB_WORKSPACE"), "docs", "schema"));

this.artifactsDir = new DirectoryInfo(Environment.GetEnvironmentVariable("GITHUB_NEW_ARTIFACTS_DIR"));
this.manifestFile = await ReadFileAsync(this.artifactsDir, "ScanManifest*.json");
this.repoManifestSchema = JSchema.Parse(await ReadFileAsync(docsDir, "manifest.schema.json"));
}

private static async Task<string> ReadFileAsync(DirectoryInfo dir, string pattern)
{
var files = dir.GetFiles(pattern);
files.Should().HaveCountGreaterThan(0, $"There should be at least one file matching the pattern {pattern}");
return await File.ReadAllTextAsync(files.First().FullName);
}

[TestMethod]
public void CheckJsonSchemaUpdated()
{
var currentSchema = CreateCurrentSchema();

// Write schema to output dir
var schemaFile = Path.Combine(this.artifactsDir.FullName, "manifest.schema.json");
File.WriteAllText(schemaFile, currentSchema.ToString());

JToken.DeepEquals(this.repoManifestSchema, currentSchema).Should().BeTrue($"The schema in docs should be updated to match the current schema.");
}

[TestMethod]
public void VerifyManifestConformsJsonSchema()
{
var manifest = JObject.Parse(this.manifestFile);
var valid = manifest.IsValid(this.repoManifestSchema, out IList<string> errors);

valid.Should().BeTrue($"The manifest generated from CD should conform to the JSON Schema in `docs/schema`. Errors: {string.Join(Environment.NewLine, errors)}");
}

private static JSchema CreateCurrentSchema()
{
var generator = new JSchemaGenerator();
generator.GenerationProviders.Add(new StringEnumGenerationProvider());

var schema = generator.Generate(typeof(ScanResult));
schema.ExtensionData.Add("$schema", "http://json-schema.org/draft-07/schema#");

return schema;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<IsTestProject>true</IsTestProject>
Expand All @@ -21,6 +21,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="MSTest.TestAdapter" />
<PackageReference Include="MSTest.TestFramework" />
<PackageReference Include="Newtonsoft.Json.Schema" />
<PackageReference Include="System.Threading.Tasks.Dataflow" />
</ItemGroup>

Expand Down

0 comments on commit 9724a88

Please sign in to comment.