114 lines
5.1 KiB
C#
114 lines
5.1 KiB
C#
// ------------------------------------------------------------
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
|
|
// ------------------------------------------------------------
|
|
|
|
using System.Xml.Linq;
|
|
using Microsoft.OData.Edm;
|
|
using Microsoft.OData.Edm.Csdl;
|
|
using Microsoft.OData.Edm.Vocabularies;
|
|
using Microsoft.OData.Edm.Vocabularies.V1;
|
|
|
|
namespace Microsoft.OpenApi.OData.Tests
|
|
{
|
|
/// <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);
|
|
EdmSingleton me = new EdmSingleton(entityContainer, "Me", person);
|
|
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);
|
|
entityContainer.AddElement(me);
|
|
|
|
var coreDescription = CoreVocabularyModel.DescriptionTerm;
|
|
var annotation = new EdmVocabularyAnnotation(people, coreDescription, new EdmStringConstant("People's description."));
|
|
model.AddVocabularyAnnotation(annotation);
|
|
|
|
var coreLongDescription = CoreVocabularyModel.LongDescriptionTerm;
|
|
annotation = new EdmVocabularyAnnotation(people, coreLongDescription, new EdmStringConstant("People's Long description."));
|
|
model.AddVocabularyAnnotation(annotation);
|
|
|
|
return model;
|
|
}
|
|
}
|
|
}
|