Skip to content

Commit dcae03e

Browse files
authored
Merge pull request #4 from sander1095/copilot/refactor-test-setup
Rewrite AcceptanceTests to use JSON files and JsonNode.DeepEquals
2 parents 5af8563 + 3ff777c commit dcae03e

File tree

3 files changed

+251
-19
lines changed

3 files changed

+251
-19
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
{
2+
"openapi": "3.1.1",
3+
"info": {
4+
"title": "Test API | v1",
5+
"description": "The API was deprecated on 01/01/2026. The API was sunset on 02/10/2026.\n\n### Links\n\n- [Version Deprecation Policy](http://my.api.com/policies/versions/deprecated.html)\n- [Version Sunset Policy](http://my.api.com/policies/versions/sunset.html)",
6+
"version": "1.0"
7+
},
8+
"servers": [
9+
{
10+
"url": "http://localhost/"
11+
}
12+
],
13+
"paths": {
14+
"/test/{id}": {
15+
"get": {
16+
"tags": [
17+
"Test"
18+
],
19+
"summary": "",
20+
"description": "",
21+
"parameters": [
22+
{
23+
"name": "id",
24+
"in": "path",
25+
"description": "",
26+
"required": true,
27+
"schema": {
28+
"pattern": "^-?(?:0|[1-9]\\d*)$",
29+
"type": "integer",
30+
"format": "int32"
31+
}
32+
},
33+
{
34+
"name": "api-version",
35+
"in": "query",
36+
"description": "The requested API version",
37+
"required": true,
38+
"schema": {
39+
"type": "string",
40+
"default": "1.0"
41+
}
42+
}
43+
],
44+
"responses": {
45+
"200": {
46+
"description": "OK",
47+
"content": {
48+
"application/json": {
49+
"schema": {
50+
"pattern": "^-?(?:0|[1-9]\\d*)$",
51+
"type": [
52+
"integer",
53+
"string"
54+
],
55+
"format": "int32"
56+
}
57+
}
58+
}
59+
},
60+
"400": {
61+
"description": "Bad Request"
62+
}
63+
}
64+
}
65+
}
66+
},
67+
"tags": [
68+
{
69+
"name": "Test"
70+
}
71+
],
72+
"x-api-versioning": [
73+
{
74+
"title": "Version Deprecation Policy",
75+
"type": "text/html",
76+
"rel": "deprecation",
77+
"url": "http://my.api.com/policies/versions/deprecated.html"
78+
},
79+
{
80+
"title": "Version Sunset Policy",
81+
"type": "text/html",
82+
"rel": "sunset",
83+
"url": "http://my.api.com/policies/versions/sunset.html"
84+
}
85+
]
86+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
{
2+
"openapi": "3.1.1",
3+
"info": {
4+
"title": "Test API | v1",
5+
"description": "The API was deprecated on 01/01/2026. The API was sunset on 02/10/2026.\n\n### Links\n\n- [Version Deprecation Policy](http://my.api.com/policies/versions/deprecated.html)\n- [Version Sunset Policy](http://my.api.com/policies/versions/sunset.html)",
6+
"version": "1.0"
7+
},
8+
"servers": [
9+
{
10+
"url": "http://localhost/"
11+
}
12+
],
13+
"paths": {
14+
"/minimal/{id}": {
15+
"get": {
16+
"tags": [
17+
"Test"
18+
],
19+
"summary": "",
20+
"description": "",
21+
"parameters": [
22+
{
23+
"name": "id",
24+
"in": "path",
25+
"description": "",
26+
"required": true,
27+
"schema": {
28+
"pattern": "^-?(?:0|[1-9]\\d*)$",
29+
"type": "integer",
30+
"format": "int32"
31+
}
32+
},
33+
{
34+
"name": "api-version",
35+
"in": "query",
36+
"description": "The requested API version",
37+
"required": true,
38+
"schema": {
39+
"type": "string",
40+
"default": "1.0"
41+
}
42+
}
43+
],
44+
"responses": {
45+
"200": {
46+
"description": "OK",
47+
"content": {
48+
"application/json": {
49+
"schema": {
50+
"pattern": "^-?(?:0|[1-9]\\d*)$",
51+
"type": [
52+
"integer",
53+
"string"
54+
],
55+
"format": "int32"
56+
}
57+
}
58+
}
59+
},
60+
"400": {
61+
"description": "Bad Request"
62+
}
63+
}
64+
}
65+
},
66+
"/Test": {
67+
"get": {
68+
"tags": [
69+
"Test"
70+
],
71+
"summary": "",
72+
"description": "",
73+
"parameters": [
74+
{
75+
"name": "id",
76+
"in": "query",
77+
"description": "",
78+
"schema": {
79+
"pattern": "^-?(?:0|[1-9]\\d*)$",
80+
"type": [
81+
"integer",
82+
"string"
83+
],
84+
"format": "int32"
85+
}
86+
},
87+
{
88+
"name": "api-version",
89+
"in": "query",
90+
"description": "The requested API version",
91+
"required": true,
92+
"schema": {
93+
"type": "string",
94+
"default": "1.0"
95+
}
96+
}
97+
],
98+
"responses": {
99+
"200": {
100+
"description": "OK",
101+
"content": {
102+
"text/plain": {
103+
"schema": {
104+
"pattern": "^-?(?:0|[1-9]\\d*)$",
105+
"type": [
106+
"integer",
107+
"string"
108+
],
109+
"format": "int32"
110+
}
111+
},
112+
"application/json": {
113+
"schema": {
114+
"pattern": "^-?(?:0|[1-9]\\d*)$",
115+
"type": [
116+
"integer",
117+
"string"
118+
],
119+
"format": "int32"
120+
}
121+
},
122+
"text/json": {
123+
"schema": {
124+
"pattern": "^-?(?:0|[1-9]\\d*)$",
125+
"type": [
126+
"integer",
127+
"string"
128+
],
129+
"format": "int32"
130+
}
131+
}
132+
}
133+
}
134+
}
135+
}
136+
}
137+
},
138+
"tags": [
139+
{
140+
"name": "Test"
141+
}
142+
],
143+
"x-api-versioning": [
144+
{
145+
"title": "Version Deprecation Policy",
146+
"type": "text/html",
147+
"rel": "deprecation",
148+
"url": "http://my.api.com/policies/versions/deprecated.html"
149+
},
150+
{
151+
"title": "Version Sunset Policy",
152+
"type": "text/html",
153+
"rel": "sunset",
154+
"url": "http://my.api.com/policies/versions/sunset.html"
155+
}
156+
]
157+
}

src/AspNetCore/WebApi/test/Asp.Versioning.OpenApi.Tests/Transformers/AcceptanceTest.cs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public async Task minimal_api_should_generate_expected_open_api_document()
2626
var builder = WebApplication.CreateBuilder();
2727

2828
builder.WebHost.UseTestServer();
29-
builder.Services.AddProblemDetails();
3029
builder.Services.AddApiVersioning( options => AddPolicies( options ) )
3130
.AddApiExplorer( options => options.GroupNameFormat = "'v'VVV" )
3231
.AddOpenApi();
@@ -43,6 +42,9 @@ public async Task minimal_api_should_generate_expected_open_api_document()
4342
app.MapOpenApi().WithDocumentPerVersion();
4443

4544
var cancellationToken = TestContext.Current.CancellationToken;
45+
using var stream = File.OpenRead( Path.Combine( AppContext.BaseDirectory, "Content", "v1-minimal.json" ) );
46+
var expected = await JsonNode.ParseAsync( stream, default, default, cancellationToken );
47+
4648
await app.StartAsync( cancellationToken );
4749

4850
using var client = app.GetTestClient();
@@ -51,18 +53,7 @@ public async Task minimal_api_should_generate_expected_open_api_document()
5153
var actual = await client.GetFromJsonAsync<JsonNode>( "/openapi/v1.json", cancellationToken );
5254

5355
// assert
54-
actual!["info"]!["version"]!.GetValue<string>().Should().Be( "1.0" );
55-
56-
var paths = actual["paths"]!.AsObject();
57-
58-
paths.Select( p => p.Key ).Should().Contain( "/test/{id}" );
59-
paths.Select( p => p.Key ).Should().NotContain( "/Test" );
60-
61-
var operation = paths["/test/{id}"]!["get"]!;
62-
var parameters = operation["parameters"]!.AsArray();
63-
64-
parameters.Should().Contain( p => p!["name"]!.GetValue<string>() == "id" );
65-
parameters.Should().Contain( p => p!["name"]!.GetValue<string>() == "api-version" );
56+
JsonNode.DeepEquals( actual, expected ).Should().BeTrue();
6657
}
6758

6859
[Fact]
@@ -124,6 +115,9 @@ public async Task mixed_api_should_generate_expected_open_api_document()
124115
app.MapOpenApi().WithDocumentPerVersion();
125116

126117
var cancellationToken = TestContext.Current.CancellationToken;
118+
using var stream = File.OpenRead( Path.Combine( AppContext.BaseDirectory, "Content", "v1-mixed.json" ) );
119+
var expected = await JsonNode.ParseAsync( stream, default, default, cancellationToken );
120+
127121
await app.StartAsync( cancellationToken );
128122

129123
using var client = app.GetTestClient();
@@ -132,12 +126,7 @@ public async Task mixed_api_should_generate_expected_open_api_document()
132126
var actual = await client.GetFromJsonAsync<JsonNode>( "/openapi/v1.json", cancellationToken );
133127

134128
// assert
135-
actual!["info"]!["version"]!.GetValue<string>().Should().Be( "1.0" );
136-
137-
var paths = actual["paths"]!.AsObject();
138-
139-
paths.Select( p => p.Key ).Should().Contain( "/minimal/{id}" );
140-
paths.Select( p => p.Key ).Should().Contain( "/Test" );
129+
JsonNode.DeepEquals( actual, expected ).Should().BeTrue();
141130
}
142131

143132
private static ApiVersioningOptions AddPolicies( ApiVersioningOptions options )

0 commit comments

Comments
 (0)