public static IEdmModel BuildEdmModel(ODataModelBuilder builder)
        {
            if (builder == null)
            {
                throw Error.ArgumentNull("builder");
            }

            EdmModel model = new EdmModel();
            EdmEntityContainer container = new EdmEntityContainer(builder.Namespace, builder.ContainerName);

            // add types and sets, building an index on the way.
            Dictionary<Type, IEdmStructuredType> edmTypeMap = model.AddTypes(builder.StructuralTypes);
            Dictionary<string, EdmEntitySet> edmEntitySetMap = model.AddEntitySets(builder, container, edmTypeMap);

            // add procedures
            model.AddProcedures(builder.Procedures, container, edmTypeMap, edmEntitySetMap);

            // finish up
            model.AddElement(container);

            // build the map from IEdmEntityType to IEdmFunctionImport
            model.SetAnnotationValue<BindableProcedureFinder>(model, new BindableProcedureFinder(model));

            return model;
        }
        public void GetModelID_Returns_DifferentIDForDifferentModels()
        {
            EdmModel model1 = new EdmModel();
            EdmModel model2 = new EdmModel();

            Assert.NotEqual(ModelContainer.GetModelID(model1), ModelContainer.GetModelID(model2));
        }
        public void GetModelID_AndThen_GetModel_ReturnsOriginalModel()
        {
            EdmModel model = new EdmModel();
            string modelID = ModelContainer.GetModelID(model);

            Assert.Same(model, ModelContainer.GetModel(modelID));
        }
        private EdmEntityType ConvertToEdmEntityType(JSchema schema, EdmModel model, string name)
        {
            var type = new EdmEntityType("namespace", name);

            foreach (var property in schema.Properties)
            {
                if (property.Value.Type == null)
                    throw new Exception("Type specyfication missing.");

                var structuralType = MapPropertyToStructuralType(property, schema, model);

                if (structuralType != null)
                {
                    type.AddStructuralProperty(property.Key, structuralType);
                }
                else
                {
                    type.AddStructuralProperty(property.Key, ToEdmPrimitiveType(property.Value.Type.Value));
                }
            }

            model.AddElement(type);

            return type;
        }
        public void AdvancedMapODataServiceRoute_ConfiguresARoute_WithAnODataRouteConstraintAndVersionConstraints()
        {
            // Arrange
            HttpRouteCollection routes = new HttpRouteCollection();
            IEdmModel model = new EdmModel();
            string routeName = "name";
            string routePrefix = "prefix";
            var pathHandler = new DefaultODataPathHandler();
            var conventions = new List<IODataRoutingConvention>();

            // Act
            routes.MapODataServiceRoute(routeName, routePrefix, model, pathHandler, conventions);

            // Assert
            IHttpRoute odataRoute = routes[routeName];
            Assert.Single(routes);
            Assert.Equal(routePrefix + "/{*odataPath}", odataRoute.RouteTemplate);
            Assert.Equal(2, odataRoute.Constraints.Count);

            var odataPathConstraint = Assert.Single(odataRoute.Constraints.Values.OfType<ODataPathRouteConstraint>());
            Assert.Same(model, odataPathConstraint.EdmModel);
            Assert.IsType<DefaultODataPathHandler>(odataPathConstraint.PathHandler);
            Assert.NotNull(odataPathConstraint.RoutingConventions);

            var odataVersionConstraint = Assert.Single(odataRoute.Constraints.Values.OfType<ODataVersionConstraint>());
            Assert.Equal(ODataVersion.V1, odataVersionConstraint.MinVersion);
            Assert.Equal(ODataVersion.V3, odataVersionConstraint.MaxVersion);
        }
        private static EdmModel CreateCustomerProductsModel()
        {
            var model = new EdmModel();

            var container = new EdmEntityContainer("defaultNamespace", "DefaultContainer");
            model.AddElement(container);

            var productType = new EdmEntityType("defaultNamespace", "Product");
            model.AddElement(productType);

            var customerType = new EdmEntityType("defaultNamespace", "Customer");
            customerType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Products", Target = productType, TargetMultiplicity = EdmMultiplicity.Many });
            productType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Customers", Target = customerType, TargetMultiplicity = EdmMultiplicity.Many });

            model.AddElement(customerType);

            var productSet = new EdmEntitySet(
                container,
                "Products",
                productType);

            container.AddElement(productSet);

            var customerSet = new EdmEntitySet(
                container,
                "Customers",
                customerType);

            var productsNavProp = customerType.NavigationProperties().Single(np => np.Name == "Products");
            customerSet.AddNavigationTarget(productsNavProp, productSet);
            container.AddElement(customerSet);

            return model;
        }
        public void GetEntitySetLinkBuilder_ReturnsDefaultEntitySetBuilder_IfNotSet()
        {
            IEdmModel model = new EdmModel();
            EdmEntityContainer container = new EdmEntityContainer("NS", "Container");
            EdmEntityType entityType = new EdmEntityType("NS", "Entity");
            IEdmEntitySet entitySet = new EdmEntitySet(container, "EntitySet", entityType);

            Assert.NotNull(model.GetEntitySetLinkBuilder(entitySet));
        }
        public void GetActionLinkBuilder_ReturnsDefaultActionLinkBuilder_IfNotSet()
        {
            // Arrange
            IEdmModel model = new EdmModel();
            IEdmEntityContainer container = new EdmEntityContainer("NS", "Container");
            IEdmFunctionImport action = new EdmFunctionImport(container, "Action", returnType: null);

            Assert.NotNull(model.GetActionLinkBuilder(action));
        }
Example #9
0
 private IEdmModel AddFunctions(IEdmModel model)
 {
     if (model != null)
     {
         var edmModel            = new Microsoft.Data.Edm.Library.EdmModel();
         var iEdmEntityContainer = model.EntityContainers().Single();
         var container           = new EdmEntityContainer(iEdmEntityContainer.Namespace, iEdmEntityContainer.Name);
     }
     return(model);
 }
        public void GetMetadata_Returns_EdmModelFromRequest()
        {
            IEdmModel model = new EdmModel();

            ODataMetadataController controller = new ODataMetadataController();
            controller.Request = new HttpRequestMessage();
            controller.Request.ODataProperties().Model = model;

            IEdmModel responseModel = controller.GetMetadata();
            Assert.Equal(model, responseModel);
        }
        public void PathHandlerGetter_ReturnsDefaultPathHandlerByDefault()
        {
            HttpRequestMessage request = new HttpRequestMessage();
            IEdmModel model = new EdmModel();
            request.ODataProperties().Model = model;

            var pathHandler = request.ODataProperties().PathHandler;

            Assert.NotNull(pathHandler);
            Assert.IsType<DefaultODataPathHandler>(pathHandler);
        }
        public void GetODataPathHandlerReturnsDefaultPathHandlerByDefault()
        {
            HttpRequestMessage request = new HttpRequestMessage();
            IEdmModel model = new EdmModel();
            request.SetEdmModel(model);

            var pathHandler = request.GetODataPathHandler();

            Assert.NotNull(pathHandler);
            Assert.IsType<DefaultODataPathHandler>(pathHandler);
        }
        public static IEdmModel SimpleCustomerOrderModel()
        {
            var model = new EdmModel();
            var customerType = new EdmEntityType("Default", "Customer");
            customerType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32);
            customerType.AddStructuralProperty("FirstName", EdmPrimitiveTypeKind.String);
            customerType.AddStructuralProperty("LastName", EdmPrimitiveTypeKind.String);
            IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance
                .GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: true);
            customerType.AddStructuralProperty("City", primitiveTypeReference, defaultValue: null,
                concurrencyMode: EdmConcurrencyMode.Fixed);
            model.AddElement(customerType);

            var orderType = new EdmEntityType("Default", "Order");
            orderType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32);
            orderType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            orderType.AddStructuralProperty("Shipment", EdmPrimitiveTypeKind.String);
            model.AddElement(orderType);

            var addressType = new EdmComplexType("Default", "Address");
            addressType.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("State", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("Country", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("ZipCode", EdmPrimitiveTypeKind.String);
            model.AddElement(addressType);

            // Add navigations
            customerType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name = "Orders",
                Target = orderType,
                TargetMultiplicity = EdmMultiplicity.Many
            });
            orderType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name = "Customer",
                Target = customerType,
                TargetMultiplicity = EdmMultiplicity.One
            });

            var container = new EdmEntityContainer("Default", "Container");
            var customerSet = container.AddEntitySet("Customers", customerType);
            var orderSet = container.AddEntitySet("Orders", orderType);
            customerSet.AddNavigationTarget(customerType.NavigationProperties().Single(np => np.Name == "Orders"), orderSet);
            orderSet.AddNavigationTarget(orderType.NavigationProperties().Single(np => np.Name == "Customer"), customerSet);

            EntitySetLinkBuilderAnnotation linkAnnotation = new MockEntitySetLinkBuilderAnnotation();
            model.SetEntitySetLinkBuilder(customerSet, linkAnnotation);
            model.SetEntitySetLinkBuilder(orderSet, linkAnnotation);

            model.AddElement(container);
            return model;
        }
        private IEdmTypeReference MapArray(KeyValuePair<string, JSchema> property, JSchema parent, EdmModel model)
        {
            var entityPrimitiveType = ToEdmPrimitiveType(property.Value.Items.Single().Type.Value);

            var entityType = EdmCoreModel.Instance.GetPrimitiveType(entityPrimitiveType);

            var collectionType = new EdmCollectionType(new EdmPrimitiveTypeReference(entityType, false));

            bool isNullable = !parent.Required.Contains(property.Key);

            return new EdmCollectionTypeReference(collectionType, isNullable);
        }
        public IEdmModel ToEdmModel(JSchema schema)
        {
            var model = new EdmModel();

            var container = new EdmEntityContainer("namespace", "containerName");

            var rootType = ConvertToEdmEntityType(schema, model, "root");

            container.AddEntitySet("root", rootType);
            model.AddElement(container);

            return model;
        }
        public void SetEdmModelThenGetReturnsWhatYouSet()
        {
            // Arrange
            HttpRequestMessage request = new HttpRequestMessage();
            IEdmModel model = new EdmModel();

            // Act
            request.SetEdmModel(model);
            IEdmModel newModel = request.GetEdmModel();

            // Assert
            Assert.Same(model, newModel);
        }
        public void ModelGetter_Returns_ModelSetter()
        {
            // Arrange
            HttpRequestMessage request = new HttpRequestMessage();
            IEdmModel model = new EdmModel();

            // Act
            request.ODataProperties().Model = model;
            IEdmModel newModel = request.ODataProperties().Model;

            // Assert
            Assert.Same(model, newModel);
        }
        static TableStorageQueryIndexInspector()
        {
            EdmModel = new EdmModel();
            TableServiceEntity = new EdmEntityType("Glimpse.WindowsAzure.Storage", "TableServiceEntity");
            TableServiceEntity.AddStructuralProperty("PartitionKey", EdmPrimitiveTypeKind.String);
            TableServiceEntity.AddStructuralProperty("RowKey", EdmPrimitiveTypeKind.String);
            TableServiceEntity.AddStructuralProperty("Timestamp", EdmPrimitiveTypeKind.DateTime);
            EdmModel.AddElement(TableServiceEntity);

            var container = new EdmEntityContainer("TestModel", "DefaultContainer");
            container.AddEntitySet("Entities", TableServiceEntity);
            EdmModel.AddElement(container);
        }
        public void GetMetadata_Returns_EdmModelFromSetODataFormatter()
        {
            IEdmModel model = new EdmModel();
            IEnumerable<ODataMediaTypeFormatter> oDataFormatters = ODataMediaTypeFormatters.Create(model);
            HttpConfiguration configuration = new HttpConfiguration();
            configuration.Formatters.InsertRange(0, oDataFormatters);

            ODataMetadataController controller = new ODataMetadataController();
            controller.Request = new HttpRequestMessage();
            controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;

            IEdmModel responseModel = controller.GetMetadata();
            Assert.Equal(model, responseModel);
        }
        public void GetActionLinkBuilder_After_SetActionLinkBuilder()
        {
            // Arrange
            IEdmModel model = new EdmModel();
            IEdmEntityContainer container = new EdmEntityContainer("NS", "Container");
            IEdmFunctionImport action = new Mock<IEdmFunctionImport>().Object;
            ActionLinkBuilder builder = new ActionLinkBuilder(_=> null, followsConventions: false);

            // Act
            model.SetActionLinkBuilder(action, builder);
            var result = model.GetActionLinkBuilder(action);

            // Assert
            Assert.Same(builder, result);
        }
        public void MapODataRoute_AddsBatchRoute_WhenBatchHandlerIsProvided()
        {
            HttpRouteCollection routes = new HttpRouteCollection();
            IEdmModel model = new EdmModel();
            string routeName = "name";
            string routePrefix = "prefix";

            var batchHandler = new DefaultODataBatchHandler(new HttpServer());
            routes.MapODataRoute(routeName, routePrefix, model, batchHandler);

            IHttpRoute batchRoute = routes["nameBatch"];
            Assert.NotNull(batchRoute);
            Assert.Same(batchHandler, batchRoute.Handler);
            Assert.Equal("prefix/$batch", batchRoute.RouteTemplate);
        }
        public void GetMetadata_Returns_EdmModelFromSetODataFormatter()
        {
            IEdmModel model = new EdmModel();
            ODataMediaTypeFormatter oDataFormatter = new ODataMediaTypeFormatter(model);
            HttpConfiguration configuration = new HttpConfiguration();
            configuration.SetODataFormatter(oDataFormatter);

            ODataMetadataController controller = new ODataMetadataController();
            controller.Request = new HttpRequestMessage();
            controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;

            Assert.Equal(
                model,
                controller.GetMetadata());
        }
        public void GetMetadata_Returns_EdmModelFromSetODataFormatter()
        {
            IEdmModel model = new EdmModel();
            ODataMediaTypeFormatter oDataFormatter = new ODataMediaTypeFormatter(model);
            HttpConfiguration configuration = new HttpConfiguration();
            configuration.Formatters.Insert(0, oDataFormatter);

            ODataMetadataController controller = new ODataMetadataController();
            controller.Request = new HttpRequestMessage();
            controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;

            IEdmModel responseModel;
            Assert.True(controller.GetMetadata().TryGetContentValue<IEdmModel>(out responseModel));
            Assert.Equal(model, responseModel);
        }
        public void GetEntitySetLinkBuilder_After_SetEntitySetLinkBuilder()
        {
            // Arrange
            IEdmModel model = new EdmModel();
            EdmEntityContainer container = new EdmEntityContainer("NS", "Container");
            EdmEntityType entityType = new EdmEntityType("NS", "Entity");
            IEdmEntitySet entitySet = new EdmEntitySet(container, "EntitySet", entityType);
            EntitySetLinkBuilderAnnotation entitySetLinkBuilder = new EntitySetLinkBuilderAnnotation();

            // Act
            model.SetEntitySetLinkBuilder(entitySet, entitySetLinkBuilder);
            var result = model.GetEntitySetLinkBuilder(entitySet);

            // Assert
            Assert.Same(entitySetLinkBuilder, result);
        }
        public void ODataMetadataSerializer_Works()
        {
            // Arrange
            ODataMetadataSerializer serializer = new ODataMetadataSerializer();
            MemoryStream stream = new MemoryStream();
            IODataResponseMessage message = new ODataMessageWrapper(stream);
            ODataMessageWriterSettings settings = new ODataMessageWriterSettings();
            IEdmModel model = new EdmModel();

            // Act
            serializer.WriteObject("42", typeof(IEdmModel), new ODataMessageWriter(message, settings, model), new ODataSerializerContext());

            // Assert
            stream.Seek(0, SeekOrigin.Begin);
            XElement element = XElement.Load(stream);
            Assert.Equal("Edmx", element.Name.LocalName);
        }
        public void MapODataRoute_ConfiguresARoute_WithAnODataRouteConstraint()
        {
            HttpRouteCollection routes = new HttpRouteCollection();
            IEdmModel model = new EdmModel();
            string routeName = "name";
            string routePrefix = "prefix";

            routes.MapODataRoute(routeName, routePrefix, model);

            IHttpRoute odataRoute = routes[routeName];
            Assert.Single(routes);
            Assert.Equal(routePrefix + "/{*odataPath}", odataRoute.RouteTemplate);
            var constraint = Assert.Single(odataRoute.Constraints);
            var odataConstraint = Assert.IsType<ODataPathRouteConstraint>(constraint.Value);
            Assert.Same(model, odataConstraint.EdmModel);
            Assert.IsType<DefaultODataPathHandler>(odataConstraint.PathHandler);
            Assert.IsType<List<IODataRoutingConvention>>(odataConstraint.RoutingConventions);
        }
        public IEdmModel BuildModel(IEnumerable<TableData> tables)
        {
            var model = new EdmModel();            
            var container = new EdmEntityContainer("sitecore.com", "Visits");
            model.AddElement(container);            
            

            var tableMap = new Dictionary<TableDataSchema, EdmEntityTypeWrapper>();
            foreach (var table in tables)
            {
                var typeWrapper = AddTable(model, table);
                container.AddEntitySet(table.Name, typeWrapper.Type);
                tableMap.Add(table.Schema, typeWrapper);
            }

            foreach (var t in tableMap)
            {
                foreach (var reference in t.Key.RelatedTables)
                {
                    if (reference.RelationType == RelationType.Child || reference.RelationType == RelationType.DimensionReference)
                    {
                        var source = t.Value;
                        var target = tableMap[reference.RelatedTable];
                                                

                        t.Value.Type.AddBidirectionalNavigation(new EdmNavigationPropertyInfo()
                        {
                            Name = reference.RelatedTable.Name,
                            TargetMultiplicity = EdmMultiplicity.Many,                            
                            Target = target.Type
                        }, new EdmNavigationPropertyInfo()
                        {
                            Name = t.Key.Name,
                            TargetMultiplicity = reference.RelationType == RelationType.Child ? EdmMultiplicity.One : EdmMultiplicity.ZeroOrOne,
                            DependentProperties = reference.RelatedFields.Select(f=>target.Properties[f]),
                            Target = source.Type
                        });
                    }
                }
            }

            return model;
        }
        public void EntityContainer_Is_Default_ShowsUp_In_Metadata()
        {
            // Arrange
            EdmModel model = new EdmModel();
            EdmEntityContainer container = new EdmEntityContainer("Default", "SampleContainer");
            model.AddElement(container);

            // Act
            model.SetIsDefaultEntityContainer(container, isDefaultContainer: true);

            // Assert
            MemoryStream stream = new MemoryStream();
            ODataMessageWriter writer = new ODataMessageWriter(new ODataMessageWrapper(stream) as IODataResponseMessage, new ODataMessageWriterSettings(), model);
            writer.WriteMetadataDocument();
            stream.Seek(0, SeekOrigin.Begin);
            XElement element = XElement.Load(stream);
            var containerXml = element.Descendants().Where(n => n.Name.LocalName == "EntityContainer").SingleOrDefault();
            Assert.Equal("SampleContainer", containerXml.Attribute("Name").Value);
            Assert.Equal("true", containerXml.Attributes().Where(a => a.Name.LocalName == "IsDefaultEntityContainer").Single().Value);
        }
        public static IEdmModel BuildEdmModel(ODataModelBuilder builder)
        {
            if (builder == null)
            {
                throw Error.ArgumentNull("builder");
            }

            EdmModel model = new EdmModel();
            EdmEntityContainer container = new EdmEntityContainer(builder.Namespace, builder.ContainerName);

            // add types and sets, building an index on the way.
            Dictionary<string, IEdmStructuredType> edmTypeMap = model.AddTypes(builder.StructuralTypes);
            Dictionary<string, EdmEntitySet> edmEntitySetMap = model.AddEntitySets(builder.EntitySets, container, edmTypeMap);
            
            // add procedures
            container.AddProcedures(builder.Procedures, edmTypeMap, edmEntitySetMap);

            // finish up
            model.AddElement(container);
            return model;
        }
        public static IEdmModel SimpleCustomerOrderModel()
        {
            var model = new EdmModel();
            var customerType = new EdmEntityType("Default", "Customer");
            customerType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32);
            customerType.AddStructuralProperty("FirstName", EdmPrimitiveTypeKind.String);
            customerType.AddStructuralProperty("LastName", EdmPrimitiveTypeKind.String);
            model.AddElement(customerType);

            var orderType = new EdmEntityType("Default", "Order");
            orderType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32);
            orderType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            model.AddElement(orderType);

            var addressType = new EdmComplexType("Default", "Address");
            addressType.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("State", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("Country", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("ZipCode", EdmPrimitiveTypeKind.String);
            model.AddElement(addressType);

            // Add navigations
            customerType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Orders", Target = orderType, TargetMultiplicity = EdmMultiplicity.Many });
            orderType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Customer", Target = customerType, TargetMultiplicity = EdmMultiplicity.One });

            var container = new EdmEntityContainer("Default", "Container");
            var customerSet = container.AddEntitySet("Customers", customerType);
            var orderSet = container.AddEntitySet("Orders", orderType);
            customerSet.AddNavigationTarget(customerType.NavigationProperties().Single(np => np.Name == "Orders"), orderSet);
            orderSet.AddNavigationTarget(orderType.NavigationProperties().Single(np => np.Name == "Customer"), customerSet);

            Mock<IEntitySetLinkBuilder> linkAnnotation = new Mock<IEntitySetLinkBuilder>();
            model.SetEntitySetLinkBuilderAnnotation(customerSet, linkAnnotation.Object);
            model.SetEntitySetLinkBuilderAnnotation(orderSet, linkAnnotation.Object);

            model.AddElement(container);
            return model;
        }
        public void ToDictionary_ContainsAllStructuralProperties_IfInstanceIsNotNull()
        {
            // Arrange
            EdmModel model = new EdmModel();
            EdmEntityType entityType = new EdmEntityType("NS", "Name");
            model.AddElement(entityType);
            model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity)));
            entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32);
            IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false);
            SelectExpandWrapper<TestEntity> testWrapper = new SelectExpandWrapper<TestEntity>
            {
                Instance = new TestEntity { SampleProperty = 42 },
                ModelID = ModelContainer.GetModelID(model)
            };

            // Act
            var result = testWrapper.ToDictionary();

            // Assert
            Assert.Equal(42, result["SampleProperty"]);
        }