2017-10-19 23:39:57 +02:00
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
|
// <copyright file="EdmModelHelper.cs" company="Microsoft">
|
|
|
|
|
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
|
|
|
|
|
// </copyright>
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
using Microsoft.OData.Edm;
|
|
|
|
|
using Microsoft.OData.Edm.Csdl;
|
|
|
|
|
|
2017-11-11 01:14:41 +01:00
|
|
|
|
namespace Microsoft.OpenApi.OData.Tests
|
2017-10-19 23:39:57 +02:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Edm model helpers
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class EdmModelHelper
|
|
|
|
|
{
|
|
|
|
|
public static IEdmModel EmptyModel { get; } = new EdmModel();
|
|
|
|
|
|
|
|
|
|
public static IEdmModel BasicEdmModel { get; }
|
|
|
|
|
|
|
|
|
|
public static IEdmModel TripServiceModel { get; }
|
|
|
|
|
|
|
|
|
|
static EdmModelHelper()
|
|
|
|
|
{
|
|
|
|
|
BasicEdmModel = CreateEdmModel();
|
|
|
|
|
TripServiceModel = LoadTripServiceModel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEdmModel LoadTripServiceModel()
|
|
|
|
|
{
|
|
|
|
|
string csdl = Resources.GetString("TripService.OData.xml");
|
|
|
|
|
return CsdlReader.Parse(XElement.Parse(csdl).CreateReader());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEdmModel CreateEdmModel()
|
|
|
|
|
{
|
|
|
|
|
var model = new EdmModel();
|
|
|
|
|
|
|
|
|
|
var enumType = new EdmEnumType("DefaultNs", "Color");
|
|
|
|
|
var blue = enumType.AddMember("Blue", new EdmEnumMemberValue(0));
|
|
|
|
|
enumType.AddMember("White", new EdmEnumMemberValue(1));
|
|
|
|
|
model.AddElement(enumType);
|
|
|
|
|
|
|
|
|
|
var person = new EdmEntityType("DefaultNs", "Person");
|
|
|
|
|
var entityId = person.AddStructuralProperty("UserName", EdmCoreModel.Instance.GetString(false));
|
|
|
|
|
person.AddKeys(entityId);
|
|
|
|
|
|
|
|
|
|
var city = new EdmEntityType("DefaultNs", "City");
|
|
|
|
|
var cityId = city.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false));
|
|
|
|
|
city.AddKeys(cityId);
|
|
|
|
|
|
|
|
|
|
var countryOrRegion = new EdmEntityType("DefaultNs", "CountryOrRegion");
|
|
|
|
|
var countryId = countryOrRegion.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false));
|
|
|
|
|
countryOrRegion.AddKeys(countryId);
|
|
|
|
|
|
|
|
|
|
var complex = new EdmComplexType("DefaultNs", "Address");
|
|
|
|
|
complex.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false));
|
|
|
|
|
var navP = complex.AddUnidirectionalNavigation(
|
|
|
|
|
new EdmNavigationPropertyInfo()
|
|
|
|
|
{
|
|
|
|
|
Name = "City",
|
|
|
|
|
Target = city,
|
|
|
|
|
TargetMultiplicity = EdmMultiplicity.One,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var derivedComplex = new EdmComplexType("DefaultNs", "WorkAddress", complex);
|
|
|
|
|
var navP2 = derivedComplex.AddUnidirectionalNavigation(
|
|
|
|
|
new EdmNavigationPropertyInfo()
|
|
|
|
|
{
|
|
|
|
|
Name = "CountryOrRegion",
|
|
|
|
|
Target = countryOrRegion,
|
|
|
|
|
TargetMultiplicity = EdmMultiplicity.One,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
person.AddStructuralProperty("HomeAddress", new EdmComplexTypeReference(complex, false));
|
|
|
|
|
person.AddStructuralProperty("WorkAddress", new EdmComplexTypeReference(complex, false));
|
|
|
|
|
person.AddStructuralProperty("Addresses",
|
|
|
|
|
new EdmCollectionTypeReference(new EdmCollectionType(new EdmComplexTypeReference(complex, false))));
|
|
|
|
|
|
|
|
|
|
model.AddElement(person);
|
|
|
|
|
model.AddElement(city);
|
|
|
|
|
model.AddElement(countryOrRegion);
|
|
|
|
|
model.AddElement(complex);
|
|
|
|
|
model.AddElement(derivedComplex);
|
|
|
|
|
|
|
|
|
|
var entityContainer = new EdmEntityContainer("DefaultNs", "Container");
|
|
|
|
|
model.AddElement(entityContainer);
|
|
|
|
|
EdmEntitySet people = new EdmEntitySet(entityContainer, "People", person);
|
|
|
|
|
EdmEntitySet cities = new EdmEntitySet(entityContainer, "City", city);
|
|
|
|
|
EdmEntitySet countriesOrRegions = new EdmEntitySet(entityContainer, "CountryOrRegion", countryOrRegion);
|
|
|
|
|
people.AddNavigationTarget(navP, cities, new EdmPathExpression("HomeAddress/City"));
|
|
|
|
|
people.AddNavigationTarget(navP, cities, new EdmPathExpression("Addresses/City"));
|
|
|
|
|
people.AddNavigationTarget(navP2, countriesOrRegions,
|
|
|
|
|
new EdmPathExpression("WorkAddress/DefaultNs.WorkAddress/CountryOrRegion"));
|
|
|
|
|
entityContainer.AddElement(people);
|
|
|
|
|
entityContainer.AddElement(cities);
|
|
|
|
|
entityContainer.AddElement(countriesOrRegions);
|
|
|
|
|
|
|
|
|
|
return model;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|