Skip to content

Commit 342f79e

Browse files
authored
Merge pull request #86 from Microsoft/dm/fixed-example-tests
Fixed example tests
2 parents 69cf2c5 + dfbce81 commit 342f79e

File tree

7 files changed

+672
-55
lines changed

7 files changed

+672
-55
lines changed

test/Microsoft.OpenApi.Readers.Tests/BasicTests.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Text;
99
using SharpYaml.Serialization;
1010
using Xunit;
11+
using FluentAssertions;
1112

1213
namespace Microsoft.OpenApi.Readers.Tests
1314
{
@@ -19,7 +20,7 @@ public void CheckOpenAPIVersion()
1920
var stream = GetType().Assembly.GetManifestResourceStream(typeof(BasicTests), "Samples.petstore30.yaml");
2021
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
2122

22-
Assert.Equal("3.0.0", openApiDoc.SpecVersion.ToString());
23+
openApiDoc.SpecVersion.ToString().Should().Be("3.0.0");
2324
}
2425

2526
[Fact]
@@ -40,7 +41,7 @@ public void InlineExample()
4041
",
4142
out var parsingContext);
4243

43-
Assert.Equal("3.0.0", openApiDoc.SpecVersion.ToString());
44+
openApiDoc.SpecVersion.ToString().Should().Be("3.0.0");
4445
}
4546

4647
[Fact]
@@ -51,9 +52,8 @@ public void ParseBrokenSimplest()
5152

5253
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
5354

54-
Assert.Equal(1, context.Errors.Count);
55-
Assert.NotNull(
56-
context.Errors.Where(s => s.ToString() == "title is a required property of #/info").FirstOrDefault());
55+
context.Errors.Should().HaveCount(1);
56+
context.Errors.Select(s => s.ToString()).Should().Contain("title is a required property of #/info");
5757
}
5858

5959
[Fact]
@@ -63,11 +63,11 @@ public void ParseSimplestOpenApiEver()
6363

6464
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
6565

66-
Assert.Equal("1.0.0", openApiDoc.SpecVersion.ToString());
67-
Assert.Empty(openApiDoc.Paths);
68-
Assert.Equal("The Api", openApiDoc.Info.Title);
69-
Assert.Equal("0.9.1", openApiDoc.Info.Version.ToString());
70-
Assert.Empty(context.Errors);
66+
openApiDoc.SpecVersion.ToString().Should().Be("1.0.0");
67+
openApiDoc.Paths.Should().BeEmpty();
68+
openApiDoc.Info.Title.Should().Be("The Api");
69+
openApiDoc.Info.Version.ToString().Should().Be("0.9.1");
70+
context.Errors.Should().BeEmpty();
7171
}
7272

7373
[Fact]

test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
<RootNamespace>Microsoft.OpenApi.Readers.Tests</RootNamespace>
1212
<SignAssembly>true</SignAssembly>
1313
</PropertyGroup>
14+
<ItemGroup>
15+
<None Remove="Samples\api-with-examples.yaml" />
16+
<None Remove="Samples\petstore-expanded.yaml" />
17+
<None Remove="Samples\uber.yaml" />
18+
</ItemGroup>
1419

1520
<ItemGroup>
1621
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
@@ -37,12 +42,15 @@
3742
</ItemGroup>
3843

3944
<ItemGroup>
45+
<EmbeddedResource Include="Samples\api-with-examples.yaml" />
4046
<EmbeddedResource Include="Samples\CallbackSample.yaml" />
4147
<EmbeddedResource Include="Samples\CallbackSampleWithRef.yaml" />
48+
<EmbeddedResource Include="Samples\petstore-expanded.yaml" />
4249
<EmbeddedResource Include="Samples\Simplest.yaml" />
4350
<EmbeddedResource Include="Samples\CompleteHeader.yaml" />
4451
<EmbeddedResource Include="Samples\petstore30.yaml" />
4552
<EmbeddedResource Include="Samples\BrokenSimplest.yaml" />
53+
<EmbeddedResource Include="Samples\uber.yaml" />
4654

4755
<EmbeddedResource Include="V2Tests\V2Samples\host.2.yaml" />
4856
<EmbeddedResource Include="V2Tests\V2Samples\host.3.yaml" />

test/Microsoft.OpenApi.Readers.Tests/OpenApiExampleTests.cs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,59 @@
33
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
44
// ------------------------------------------------------------
55

6+
using SharpYaml.Serialization;
67
using System;
7-
using System.Net.Http;
88
using System.Threading.Tasks;
99
using Xunit;
1010

1111
namespace Microsoft.OpenApi.Readers.Tests
1212
{
1313
public class OpenApiExampleTests
1414
{
15-
private readonly HttpClient client;
1615

17-
public OpenApiExampleTests()
16+
[Fact()]
17+
public void ApiWithExamples()
1818
{
19-
client = new HttpClient();
20-
client.BaseAddress = new Uri(
21-
"https://raw.githubusercontent.com/OAI/OpenAPI-Specification/OpenAPI.next/examples/v3.0/");
22-
}
19+
using (var stream = GetType().Assembly.GetManifestResourceStream(GetType(), "Samples.api-with-examples.yaml"))
20+
{
21+
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
2322

24-
[Fact(Skip = "Example is not updated yet")]
25-
public async Task ApiWithExamples()
26-
{
27-
var stream = await client.GetStreamAsync("api-with-examples.yaml");
28-
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
23+
Assert.Empty(context.Errors);
24+
}
2925

30-
Assert.Empty(context.Errors);
3126
}
3227

3328
[Fact]
34-
public async Task PetStoreExpandedExample()
29+
public void PetStoreExpandedExample()
3530
{
36-
var stream = await client.GetStreamAsync("petstore-expanded.yaml");
37-
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
31+
using (var stream = GetType().Assembly.GetManifestResourceStream(GetType(), "Samples.petstore-expanded.yaml"))
32+
{
33+
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
3834

39-
Assert.Empty(context.Errors);
35+
Assert.Empty(context.Errors);
36+
}
4037
}
4138

4239
[Fact]
43-
public async Task SimplePetStore()
40+
public void SimplePetStore()
4441
{
45-
var stream = await client.GetStreamAsync("petstore.yaml");
46-
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
42+
using (var stream = GetType().Assembly.GetManifestResourceStream(GetType(), "Samples.petstore30.yaml"))
43+
{
44+
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
4745

48-
Assert.Empty(context.Errors);
46+
Assert.Empty(context.Errors);
47+
}
4948
}
5049

5150
[Fact]
52-
public async Task UberExample()
51+
public void UberExample()
5352
{
54-
var stream = await client.GetStreamAsync("uber.yaml");
55-
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
53+
using (var stream = GetType().Assembly.GetManifestResourceStream(GetType(), "Samples.uber.yaml"))
54+
{
55+
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
5656

57-
Assert.Empty(context.Errors);
57+
Assert.Empty(context.Errors);
58+
}
5859
}
5960
}
6061
}
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
openapi: "3.0.0"
2+
info:
3+
title: Simple API overview
4+
version: 2.0.1
5+
paths:
6+
/:
7+
get:
8+
operationId: listVersionsv2
9+
summary: List API versions
10+
responses:
11+
'200':
12+
description: |-
13+
200 response
14+
content:
15+
application/json:
16+
examples:
17+
foo:
18+
value: {
19+
"versions": [
20+
{
21+
"status": "CURRENT",
22+
"updated": "2011-01-21T11:33:21Z",
23+
"id": "v2.0",
24+
"links": [
25+
{
26+
"href": "http://127.0.0.1:8774/v2/",
27+
"rel": "self"
28+
}
29+
]
30+
},
31+
{
32+
"status": "EXPERIMENTAL",
33+
"updated": "2013-07-23T11:33:21Z",
34+
"id": "v3.0",
35+
"links": [
36+
{
37+
"href": "http://127.0.0.1:8774/v3/",
38+
"rel": "self"
39+
}
40+
]
41+
}
42+
]
43+
}
44+
'300':
45+
description: |-
46+
300 response
47+
content:
48+
application/json:
49+
examples:
50+
foo:
51+
value: |
52+
{
53+
"versions": [
54+
{
55+
"status": "CURRENT",
56+
"updated": "2011-01-21T11:33:21Z",
57+
"id": "v2.0",
58+
"links": [
59+
{
60+
"href": "http://127.0.0.1:8774/v2/",
61+
"rel": "self"
62+
}
63+
]
64+
},
65+
{
66+
"status": "EXPERIMENTAL",
67+
"updated": "2013-07-23T11:33:21Z",
68+
"id": "v3.0",
69+
"links": [
70+
{
71+
"href": "http://127.0.0.1:8774/v3/",
72+
"rel": "self"
73+
}
74+
]
75+
}
76+
]
77+
}
78+
/v2:
79+
get:
80+
operationId: getVersionDetailsv2
81+
summary: Show API version details
82+
responses:
83+
'200':
84+
description: |-
85+
200 response
86+
content:
87+
application/json:
88+
examples:
89+
foo:
90+
value: {
91+
"version": {
92+
"status": "CURRENT",
93+
"updated": "2011-01-21T11:33:21Z",
94+
"media-types": [
95+
{
96+
"base": "application/xml",
97+
"type": "application/vnd.openstack.compute+xml;version=2"
98+
},
99+
{
100+
"base": "application/json",
101+
"type": "application/vnd.openstack.compute+json;version=2"
102+
}
103+
],
104+
"id": "v2.0",
105+
"links": [
106+
{
107+
"href": "http://127.0.0.1:8774/v2/",
108+
"rel": "self"
109+
},
110+
{
111+
"href": "http://docs.openstack.org/api/openstack-compute/2/os-compute-devguide-2.pdf",
112+
"type": "application/pdf",
113+
"rel": "describedby"
114+
},
115+
{
116+
"href": "http://docs.openstack.org/api/openstack-compute/2/wadl/os-compute-2.wadl",
117+
"type": "application/vnd.sun.wadl+xml",
118+
"rel": "describedby"
119+
},
120+
{
121+
"href": "http://docs.openstack.org/api/openstack-compute/2/wadl/os-compute-2.wadl",
122+
"type": "application/vnd.sun.wadl+xml",
123+
"rel": "describedby"
124+
}
125+
]
126+
}
127+
}
128+
'203':
129+
description: |-
130+
203 response
131+
content:
132+
application/json:
133+
examples:
134+
foo:
135+
value: {
136+
"version": {
137+
"status": "CURRENT",
138+
"updated": "2011-01-21T11:33:21Z",
139+
"media-types": [
140+
{
141+
"base": "application/xml",
142+
"type": "application/vnd.openstack.compute+xml;version=2"
143+
},
144+
{
145+
"base": "application/json",
146+
"type": "application/vnd.openstack.compute+json;version=2"
147+
}
148+
],
149+
"id": "v2.0",
150+
"links": [
151+
{
152+
"href": "http://23.253.228.211:8774/v2/",
153+
"rel": "self"
154+
},
155+
{
156+
"href": "http://docs.openstack.org/api/openstack-compute/2/os-compute-devguide-2.pdf",
157+
"type": "application/pdf",
158+
"rel": "describedby"
159+
},
160+
{
161+
"href": "http://docs.openstack.org/api/openstack-compute/2/wadl/os-compute-2.wadl",
162+
"type": "application/vnd.sun.wadl+xml",
163+
"rel": "describedby"
164+
}
165+
]
166+
}
167+
}

0 commit comments

Comments
 (0)