Skip to content

Commit 9724a88

Browse files
authored
tests: verify json schema (#638)
1 parent bd522a4 commit 9724a88

File tree

4 files changed

+120
-48
lines changed

4 files changed

+120
-48
lines changed

Directory.Packages.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<PackageVersion Include="MSTest.TestAdapter" Version="3.0.4" />
2727
<PackageVersion Include="MSTest.TestFramework" Version="3.0.4" />
2828
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
29+
<PackageVersion Include="Newtonsoft.Json.Schema" Version="3.0.15" />
2930
<PackageVersion Include="NuGet.ProjectModel" Version="6.6.1" />
3031
<PackageVersion Include="NuGet.Versioning" Version="6.6.1" />
3132
<PackageVersion Include="packageurl-dotnet" Version="1.0.0" />
@@ -48,4 +49,4 @@
4849
<PackageVersion Include="Faker.net" Version="2.0.154" />
4950
<PackageVersion Include="Valleysoft.DockerfileModel" Version="1.1.0" />
5051
</ItemGroup>
51-
</Project>
52+
</Project>

docs/schema/manifest.schema.json

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,24 @@
100100
"null"
101101
],
102102
"items": {
103-
"type": "integer",
103+
"type": "string",
104104
"enum": [
105-
0,
106-
1,
107-
2,
108-
3,
109-
4,
110-
6,
111-
7,
112-
8,
113-
9,
114-
10,
115-
11,
116-
12,
117-
13,
118-
14,
119-
15,
120-
16
105+
"Other",
106+
"NuGet",
107+
"Npm",
108+
"Maven",
109+
"Git",
110+
"RubyGems",
111+
"Cargo",
112+
"Pip",
113+
"Go",
114+
"DockerImage",
115+
"Pod",
116+
"Linux",
117+
"Conda",
118+
"Spdx",
119+
"Vcpkg",
120+
"DockerReference"
121121
]
122122
}
123123
}
@@ -262,16 +262,16 @@
262262
},
263263
"dependencyScope": {
264264
"type": [
265-
"integer",
265+
"string",
266266
"null"
267267
],
268268
"enum": [
269269
null,
270-
0,
271-
1,
272-
2,
273-
3,
274-
4
270+
"MavenCompile",
271+
"MavenRuntime",
272+
"MavenProvided",
273+
"MavenSystem",
274+
"MavenTest"
275275
]
276276
},
277277
"topLevelReferrers": {
@@ -326,24 +326,24 @@
326326
],
327327
"properties": {
328328
"type": {
329-
"type": "integer",
329+
"type": "string",
330330
"enum": [
331-
0,
332-
1,
333-
2,
334-
3,
335-
4,
336-
6,
337-
7,
338-
8,
339-
9,
340-
10,
341-
11,
342-
12,
343-
13,
344-
14,
345-
15,
346-
16
331+
"Other",
332+
"NuGet",
333+
"Npm",
334+
"Maven",
335+
"Git",
336+
"RubyGems",
337+
"Cargo",
338+
"Pip",
339+
"Go",
340+
"DockerImage",
341+
"Pod",
342+
"Linux",
343+
"Conda",
344+
"Spdx",
345+
"Vcpkg",
346+
"DockerReference"
347347
]
348348
},
349349
"id": {
@@ -394,13 +394,13 @@
394394
}
395395
},
396396
"resultCode": {
397-
"type": "integer",
397+
"type": "string",
398398
"enum": [
399-
0,
400-
1,
401-
2,
402-
3,
403-
4
399+
"Success",
400+
"PartialSuccess",
401+
"Error",
402+
"InputError",
403+
"TimeoutError"
404404
]
405405
},
406406
"sourceDirectory": {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
namespace Microsoft.ComponentDetection.VerificationTests;
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
using FluentAssertions;
9+
using Microsoft.ComponentDetection.Contracts.BcdeModels;
10+
using Microsoft.VisualStudio.TestTools.UnitTesting;
11+
using Newtonsoft.Json.Linq;
12+
using Newtonsoft.Json.Schema;
13+
using Newtonsoft.Json.Schema.Generation;
14+
15+
[TestClass]
16+
public class JsonSchemaTests
17+
{
18+
private string manifestFile;
19+
private JSchema repoManifestSchema;
20+
private DirectoryInfo artifactsDir;
21+
22+
[TestInitialize]
23+
public async Task InitializeAsync()
24+
{
25+
var docsDir = new DirectoryInfo(Path.Combine(Environment.GetEnvironmentVariable("GITHUB_WORKSPACE"), "docs", "schema"));
26+
27+
this.artifactsDir = new DirectoryInfo(Environment.GetEnvironmentVariable("GITHUB_NEW_ARTIFACTS_DIR"));
28+
this.manifestFile = await ReadFileAsync(this.artifactsDir, "ScanManifest*.json");
29+
this.repoManifestSchema = JSchema.Parse(await ReadFileAsync(docsDir, "manifest.schema.json"));
30+
}
31+
32+
private static async Task<string> ReadFileAsync(DirectoryInfo dir, string pattern)
33+
{
34+
var files = dir.GetFiles(pattern);
35+
files.Should().HaveCountGreaterThan(0, $"There should be at least one file matching the pattern {pattern}");
36+
return await File.ReadAllTextAsync(files.First().FullName);
37+
}
38+
39+
[TestMethod]
40+
public void CheckJsonSchemaUpdated()
41+
{
42+
var currentSchema = CreateCurrentSchema();
43+
44+
// Write schema to output dir
45+
var schemaFile = Path.Combine(this.artifactsDir.FullName, "manifest.schema.json");
46+
File.WriteAllText(schemaFile, currentSchema.ToString());
47+
48+
JToken.DeepEquals(this.repoManifestSchema, currentSchema).Should().BeTrue($"The schema in docs should be updated to match the current schema.");
49+
}
50+
51+
[TestMethod]
52+
public void VerifyManifestConformsJsonSchema()
53+
{
54+
var manifest = JObject.Parse(this.manifestFile);
55+
var valid = manifest.IsValid(this.repoManifestSchema, out IList<string> errors);
56+
57+
valid.Should().BeTrue($"The manifest generated from CD should conform to the JSON Schema in `docs/schema`. Errors: {string.Join(Environment.NewLine, errors)}");
58+
}
59+
60+
private static JSchema CreateCurrentSchema()
61+
{
62+
var generator = new JSchemaGenerator();
63+
generator.GenerationProviders.Add(new StringEnumGenerationProvider());
64+
65+
var schema = generator.Generate(typeof(ScanResult));
66+
schema.ExtensionData.Add("$schema", "http://json-schema.org/draft-07/schema#");
67+
68+
return schema;
69+
}
70+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<IsTestProject>true</IsTestProject>
@@ -21,6 +21,7 @@
2121
<PackageReference Include="Microsoft.NET.Test.Sdk" />
2222
<PackageReference Include="MSTest.TestAdapter" />
2323
<PackageReference Include="MSTest.TestFramework" />
24+
<PackageReference Include="Newtonsoft.Json.Schema" />
2425
<PackageReference Include="System.Threading.Tasks.Dataflow" />
2526
</ItemGroup>
2627

0 commit comments

Comments
 (0)