2017-10-19 23:39:57 +02:00
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
|
// <copyright file="EdmModelOpenApiExtensionsTest.cs" company="Microsoft">
|
|
|
|
|
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
|
|
|
|
|
// </copyright>
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using Microsoft.OData.Edm;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
2017-11-11 01:14:41 +01:00
|
|
|
|
namespace Microsoft.OpenApi.OData.Tests
|
2017-10-19 23:39:57 +02:00
|
|
|
|
{
|
|
|
|
|
public class EdmModelOpenApiExtensionsTest
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public void EmptyEdmModelToOpenApiJsonWorks()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
IEdmModel model = EdmModelHelper.EmptyModel;
|
|
|
|
|
|
|
|
|
|
// Act
|
2017-11-11 01:01:43 +01:00
|
|
|
|
string json = WriteEdmModelToOpenApi(model, OpenApiFormat.Json);
|
2017-10-19 23:39:57 +02:00
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.Equal(Resources.GetString("Empty.OpenApi.json").Replace(), json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void EmptyEdmModelToOpenApiYamlWorks()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
IEdmModel model = EdmModelHelper.EmptyModel;
|
|
|
|
|
|
|
|
|
|
// Act
|
2017-11-11 01:01:43 +01:00
|
|
|
|
string yaml = WriteEdmModelToOpenApi(model, OpenApiFormat.Yaml);
|
2017-10-19 23:39:57 +02:00
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.Equal(Resources.GetString("Empty.OpenApi.yaml").Replace(), yaml);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void BasicEdmModelToOpenApiJsonWorks()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
IEdmModel model = EdmModelHelper.BasicEdmModel;
|
|
|
|
|
|
|
|
|
|
// Act
|
2017-11-11 01:01:43 +01:00
|
|
|
|
string json = WriteEdmModelToOpenApi(model, OpenApiFormat.Json);
|
2017-10-19 23:39:57 +02:00
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.Equal(Resources.GetString("Basic.OpenApi.json").Replace(), json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void BasicEdmModelToOpenApiYamlWorks()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
IEdmModel model = EdmModelHelper.BasicEdmModel;
|
|
|
|
|
|
|
|
|
|
// Act
|
2017-11-11 01:01:43 +01:00
|
|
|
|
string yaml = WriteEdmModelToOpenApi(model, OpenApiFormat.Yaml);
|
2017-10-19 23:39:57 +02:00
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.Equal(Resources.GetString("Basic.OpenApi.yaml").Replace(), yaml);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void TripServiceMetadataToOpenApiJsonWorks()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
IEdmModel model = EdmModelHelper.TripServiceModel;
|
|
|
|
|
OpenApiWriterSettings settings = new OpenApiWriterSettings
|
|
|
|
|
{
|
|
|
|
|
Version = new Version(1, 0, 1),
|
|
|
|
|
BaseUri = new Uri("http://services.odata.org/TrippinRESTierService/")
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Act
|
2017-11-11 01:01:43 +01:00
|
|
|
|
string json = WriteEdmModelToOpenApi(model, OpenApiFormat.Json, settings);
|
2017-10-19 23:39:57 +02:00
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.Equal(Resources.GetString("TripService.OpenApi.json").Replace(), json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void TripServiceMetadataToOpenApiYamlWorks()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
IEdmModel model = EdmModelHelper.TripServiceModel;
|
|
|
|
|
OpenApiWriterSettings settings = new OpenApiWriterSettings
|
|
|
|
|
{
|
|
|
|
|
Version = new Version(1, 0, 1),
|
|
|
|
|
BaseUri = new Uri("http://services.odata.org/TrippinRESTierService/")
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Act
|
2017-11-11 01:01:43 +01:00
|
|
|
|
string yaml = WriteEdmModelToOpenApi(model, OpenApiFormat.Yaml, settings);
|
2017-10-19 23:39:57 +02:00
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.Equal(Resources.GetString("TripService.OpenApi.yaml").Replace(), yaml);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-11 01:01:43 +01:00
|
|
|
|
private static string WriteEdmModelToOpenApi(IEdmModel model, OpenApiFormat target,
|
2017-10-19 23:39:57 +02:00
|
|
|
|
OpenApiWriterSettings settings = null)
|
|
|
|
|
{
|
|
|
|
|
MemoryStream stream = new MemoryStream();
|
|
|
|
|
model.WriteOpenApi(stream, target, settings);
|
|
|
|
|
stream.Flush();
|
|
|
|
|
stream.Position = 0;
|
|
|
|
|
return new StreamReader(stream).ReadToEnd();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|