|
| 1 | +using System.Linq; |
| 2 | +using System.Net.Http; |
| 3 | +using System.Threading.Tasks; |
| 4 | + |
| 5 | +using FluentAssertions; |
| 6 | + |
| 7 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 8 | + |
| 9 | +using Newtonsoft.Json; |
| 10 | +using Newtonsoft.Json.Linq; |
| 11 | + |
| 12 | +namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Document.Tests |
| 13 | +{ |
| 14 | + [TestClass] |
| 15 | + [TestCategory(Constants.TestCategory)] |
| 16 | + public class Get_ApplicationJson_DataType_Tests |
| 17 | + { |
| 18 | + private static HttpClient http = new HttpClient(); |
| 19 | + |
| 20 | + private JObject _doc; |
| 21 | + |
| 22 | + [TestInitialize] |
| 23 | + public async Task Init() |
| 24 | + { |
| 25 | + var json = await http.GetStringAsync(Constants.OpenApiDocEndpoint).ConfigureAwait(false); |
| 26 | + this._doc = JsonConvert.DeserializeObject<JObject>(json); |
| 27 | + } |
| 28 | + |
| 29 | + [DataTestMethod] |
| 30 | + [DataRow("/get-applicationjson-datatype", "get", "200")] |
| 31 | + public void Given_OpenApiDocument_Then_It_Should_Return_OperationResponse(string path, string operationType, string responseCode) |
| 32 | + { |
| 33 | + var responses = this._doc["paths"][path][operationType]["responses"]; |
| 34 | + |
| 35 | + responses[responseCode].Should().NotBeNull(); |
| 36 | + } |
| 37 | + |
| 38 | + [DataTestMethod] |
| 39 | + [DataRow("/get-applicationjson-datatype", "get", "200", "application/json")] |
| 40 | + public void Given_OpenApiDocument_Then_It_Should_Return_OperationResponseContentType(string path, string operationType, string responseCode, string contentType) |
| 41 | + { |
| 42 | + var content = this._doc["paths"][path][operationType]["responses"][responseCode]["content"]; |
| 43 | + |
| 44 | + content[contentType].Should().NotBeNull(); |
| 45 | + } |
| 46 | + |
| 47 | + [DataTestMethod] |
| 48 | + [DataRow("/get-applicationjson-datatype", "get", "200", "application/json", "dataTypeClass")] |
| 49 | + public void Given_OpenApiDocument_Then_It_Should_Return_OperationResponseContentTypeSchema(string path, string operationType, string responseCode, string contentType, string reference) |
| 50 | + { |
| 51 | + var content = this._doc["paths"][path][operationType]["responses"][responseCode]["content"]; |
| 52 | + |
| 53 | + var @ref = content[contentType]["schema"]["$ref"]; |
| 54 | + |
| 55 | + @ref.Value<string>().Should().Be($"#/components/schemas/{reference}"); |
| 56 | + } |
| 57 | + [DataTestMethod] |
| 58 | + [DataRow("dataTypeClass", "object")] |
| 59 | + public void Given_OpenApiDocument_Then_It_Should_Return_ComponentSchema(string @ref, string refType) |
| 60 | + { |
| 61 | + var schemas = this._doc["components"]["schemas"]; |
| 62 | + |
| 63 | + var schema = schemas[@ref]; |
| 64 | + |
| 65 | + schema.Should().NotBeNull(); |
| 66 | + schema.Value<string>("type").Should().Be(refType); |
| 67 | + } |
| 68 | + |
| 69 | + [DataTestMethod] |
| 70 | + [DataRow("dataTypeClass", "dateTimeValue1", "string", "date-time", false)] |
| 71 | + [DataRow("dataTypeClass", "dateTimeValue2", "string", "date", false)] |
| 72 | + [DataRow("dataTypeClass", "dateTimeValue3", "string", "time", false)] |
| 73 | + [DataRow("dataTypeClass", "nullableDateTimeValue1", "string", "date-time", true)] |
| 74 | + [DataRow("dataTypeClass", "nullableDateTimeValue2", "string", "date", true)] |
| 75 | + [DataRow("dataTypeClass", "nullableDateTimeValue3", "string", "time", true)] |
| 76 | + [DataRow("dataTypeClass", "dateTimeOffsetValue1", "string", "date-time", false)] |
| 77 | + [DataRow("dataTypeClass", "dateTimeOffsetValue2", "string", "date", false)] |
| 78 | + [DataRow("dataTypeClass", "dateTimeOffsetValue3", "string", "time", false)] |
| 79 | + [DataRow("dataTypeClass", "nullableDateTimeOffsetValue1", "string", "date-time", true)] |
| 80 | + [DataRow("dataTypeClass", "nullableDateTimeOffsetValue2", "string", "date", true)] |
| 81 | + [DataRow("dataTypeClass", "nullableDateTimeOffsetValue3", "string", "time", true)] |
| 82 | + public void Given_OpenApiDocument_Then_It_Should_Return_ComponentSchemaProperty(string @ref, string propertyName, string propertyType, string propertyFormat, bool propertyNullable) |
| 83 | + { |
| 84 | + var properties = this._doc["components"]["schemas"][@ref]["properties"]; |
| 85 | + |
| 86 | + var value = properties[propertyName]; |
| 87 | + |
| 88 | + value.Should().NotBeNull(); |
| 89 | + value.Value<string>("type").Should().Be(propertyType); |
| 90 | + value.Value<string>("format").Should().Be(propertyFormat); |
| 91 | + value.Value<bool>("nullable").Should().Be(propertyNullable); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments