/// <summary>
        /// Retrieves the paths for a media entity stream.
        /// </summary>
        /// <param name="entityType">The entity type.</param>
        /// <param name="currentPath">The current OData path.</param>
        private void RetrieveMediaEntityStreamPaths(IEdmEntityType entityType, ODataPath currentPath)
        {
            Debug.Assert(entityType != null);
            Debug.Assert(currentPath != null);

            bool createValuePath = true;

            foreach (IEdmStructuralProperty sp in entityType.DeclaredStructuralProperties())
            {
                if (sp.Type.AsPrimitive().IsStream())
                {
                    currentPath.Push(new ODataStreamPropertySegment(sp.Name));
                    AppendPath(currentPath.Clone());
                    currentPath.Pop();
                }

                if (sp.Name.Equals("content", System.StringComparison.OrdinalIgnoreCase))
                {
                    createValuePath = false;
                }
            }

            /* Create a /$value path only if entity has stream and
             * does not contain a structural property named Content
             */
            if (createValuePath && entityType.HasStream)
            {
                currentPath.Push(new ODataStreamContentSegment());
                AppendPath(currentPath.Clone());
                currentPath.Pop();
            }
        }
Example #2
0
        public void EntityType_reference_extensions()
        {
            IEdmModel      edmModel          = this.GetEdmModel();
            IEdmEntityType derivedEntityType = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.BaseType != null);
            IEdmEntityType baseEntityType    = derivedEntityType.BaseEntityType();

            Assert.IsNotNull(baseEntityType, "Base entity type should not be null!");

            IEdmEntityTypeReference derivedEntityTypeRef = (IEdmEntityTypeReference)derivedEntityType.ToTypeReference();

            Assert.AreEqual(baseEntityType, derivedEntityTypeRef.BaseEntityType(), "EntityTypeReference.BaseEntityType()");
            Assert.AreEqual(baseEntityType, derivedEntityTypeRef.BaseType(), "EntityTypeReference.BaseType()");

            Assert.AreEqual(derivedEntityType.IsAbstract, derivedEntityTypeRef.IsAbstract(), "StructuralTypeReference.IsAbstract()");
            Assert.AreEqual(derivedEntityType.IsOpen, derivedEntityTypeRef.IsOpen(), "StructuralTypeReference.IsOpen()");

            Assert.AreEqual(derivedEntityType.DeclaredStructuralProperties().Count(), derivedEntityTypeRef.DeclaredStructuralProperties().Count(), "StructuralTypeReference.DeclaredStructuralProperties()");
            Assert.AreEqual(derivedEntityType.StructuralProperties().Count(), derivedEntityTypeRef.StructuralProperties().Count(), "StructuralTypeReference.StructuralProperties()");

            Assert.AreEqual(derivedEntityType.DeclaredNavigationProperties().Count(), derivedEntityTypeRef.DeclaredNavigationProperties().Count(), "EntityTypeReference.DeclaredNavigationProperties()");
            Assert.AreEqual(derivedEntityType.NavigationProperties().Count(), derivedEntityTypeRef.NavigationProperties().Count(), "EntityTypeReference.NavigationProperties()");

            IEdmNavigationProperty result = derivedEntityTypeRef.FindNavigationProperty("_Not_Exist_");

            Assert.IsNull(result, "Should not find Navigation Property {0}", "_Not_Exist_");

            var navigation = derivedEntityType.NavigationProperties().First();

            result = derivedEntityTypeRef.FindNavigationProperty(navigation.Name);
            Assert.AreEqual(navigation, result, "FindNavigationProperty({0})", navigation.Name);
        }
        private void VerifyPathItemOperationsForStreamPropertySegment(string annotation, OperationType[] expected)
        {
            // Arrange
            IEdmModel     model     = GetEdmModel(annotation);
            ODataContext  context   = new ODataContext(model);
            IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet("Todos");

            Assert.NotNull(entitySet); // guard
            IEdmEntityType entityType = entitySet.EntityType();

            IEdmStructuralProperty sp = entityType.DeclaredStructuralProperties().First(c => c.Name == "Logo");
            ODataPath path            = new ODataPath(new ODataNavigationSourceSegment(entitySet),
                                                      new ODataKeySegment(entityType),
                                                      new ODataStreamPropertySegment(sp.Name));

            // Act
            var pathItem = _pathItemHandler.CreatePathItem(context, path);

            // Assert
            Assert.NotNull(pathItem);

            Assert.NotNull(pathItem.Operations);
            Assert.NotEmpty(pathItem.Operations);
            Assert.Equal(expected, pathItem.Operations.Select(e => e.Key));
        }
 protected override void ProcessEntityType(IEdmEntityType element)
 {
     this.BeginElement <IEdmEntityType>(element, new Action <IEdmEntityType>(this.schemaWriter.WriteEntityTypeElementHeader), new Action <IEdmEntityType> [0]);
     if (((element.DeclaredKey != null) && (element.DeclaredKey.Count <IEdmStructuralProperty>() > 0)) && (element.BaseType == null))
     {
         this.VisitEntityTypeDeclaredKey(element.DeclaredKey);
     }
     base.VisitProperties(element.DeclaredStructuralProperties().Cast <IEdmProperty>());
     base.VisitProperties(element.DeclaredNavigationProperties().Cast <IEdmProperty>());
     this.EndElement(element);
 }
Example #5
0
        protected override void ProcessEntityType(IEdmEntityType element)
        {
            this.BeginElement(element, this.schemaWriter.WriteEntityTypeElementHeader);
            if (element.DeclaredKey != null && element.DeclaredKey.Any())
            {
                this.VisitEntityTypeDeclaredKey(element.DeclaredKey);
            }

            this.VisitProperties(element.DeclaredStructuralProperties().Cast <IEdmProperty>());
            this.VisitProperties(element.DeclaredNavigationProperties().Cast <IEdmProperty>());
            this.EndElement(element);
        }
        private void FillStockContentsForEntityWithoutNavigation(IEdmEntityType edmType, IEdmModel edmModel, EdmModel stockModel)
        {
            var stockType = (EdmEntityType)stockModel.FindType(edmType.FullName());

            this.SetImmediateAnnotations(edmType, stockType, edmModel, stockModel);

            foreach (var edmProperty in edmType.DeclaredStructuralProperties())
            {
                ConvertToStockStructuralProperty((IEdmStructuralProperty)edmProperty, edmModel, stockModel);
            }

            if (edmType.DeclaredKey != null)
            {
                stockType.AddKeys(edmType.DeclaredKey.Select(n => stockType.FindProperty(n.Name) as IEdmStructuralProperty).ToArray());
            }
        }
        public void CreateMediaEntityPathItemReturnsCorrectItem()
        {
            // Arrange
            IEdmModel     model     = GetEdmModel("");
            ODataContext  context   = new ODataContext(model);
            IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet("Todos");
            IEdmSingleton singleton = model.EntityContainer.FindSingleton("me");

            Assert.NotNull(entitySet); // guard
            Assert.NotNull(singleton);
            IEdmEntityType entityType = entitySet.EntityType();

            IEdmStructuralProperty sp = entityType.DeclaredStructuralProperties().First(c => c.Name == "Logo");
            ODataPath path            = new ODataPath(new ODataNavigationSourceSegment(entitySet),
                                                      new ODataKeySegment(entityType),
                                                      new ODataStreamPropertySegment(sp.Name));

            IEdmEntityType         user        = model.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "user");
            IEdmNavigationProperty navProperty = user.DeclaredNavigationProperties().First(c => c.Name == "photo");
            ODataPath path2 = new ODataPath(new ODataNavigationSourceSegment(singleton),
                                            new ODataNavigationPropertySegment(navProperty),
                                            new ODataStreamContentSegment());

            // Act
            var pathItem  = _pathItemHandler.CreatePathItem(context, path);
            var pathItem2 = _pathItemHandler.CreatePathItem(context, path2);

            // Assert
            Assert.NotNull(pathItem);
            Assert.NotNull(pathItem2);

            Assert.NotNull(pathItem.Operations);
            Assert.NotNull(pathItem2.Operations);
            Assert.NotEmpty(pathItem.Operations);
            Assert.NotEmpty(pathItem2.Operations);
            Assert.Equal(2, pathItem.Operations.Count);
            Assert.Equal(2, pathItem2.Operations.Count);
            Assert.Equal(new OperationType[] { OperationType.Get, OperationType.Put },
                         pathItem.Operations.Select(o => o.Key));
            Assert.Equal(new OperationType[] { OperationType.Get, OperationType.Put },
                         pathItem2.Operations.Select(o => o.Key));
            Assert.NotEmpty(pathItem.Description);
        }
        public void ConvertEntityType_Inheritance()
        {
            var taupoModel = new EntityModelSchema()
            {
                new EntityType("Derived")
                {
                    BaseType = "Base",
                },
                new EntityType("Base")
                {
                    new MemberProperty("p1", EdmDataTypes.Int16)
                    {
                        IsPrimaryKey = true
                    },
                    new MemberProperty("p2", EdmDataTypes.Guid),
                    new MemberProperty("p3", EdmDataTypes.Double),
                },
            }
            .ApplyDefaultNamespace("NS1")
            .Resolve();

            IEdmModel result = this.converter.ConvertToEdmModel(taupoModel);

            Assert.IsNull(result.EntityContainer);
            Assert.AreEqual(2, result.SchemaElements.Count());
            Assert.AreEqual(2, result.SchemaElements.OfType <IEdmEntityType>().Count());

            IEdmEntityType convertedDerived = result.SchemaElements.OfType <IEdmEntityType>().ElementAt(0);
            IEdmEntityType convertedBase    = result.SchemaElements.OfType <IEdmEntityType>().ElementAt(1);

            Assert.AreEqual("NS1.Derived", convertedDerived.FullName());
            Assert.AreEqual(0, convertedDerived.DeclaredStructuralProperties().Count());
            Assert.AreEqual(convertedBase, convertedDerived.BaseEntityType());

            Assert.AreEqual("NS1.Base", convertedBase.FullName());
            Assert.AreEqual(3, convertedBase.DeclaredStructuralProperties().Count());

            Assert.AreEqual(1, convertedDerived.Key().Count());
            Assert.AreEqual(1, convertedBase.DeclaredKey.Count());
            Assert.AreEqual(convertedBase.DeclaredKey.First(), convertedDerived.Key().First());
        }
        private EntityType ConvertToTaupoEntityType(IEdmEntityType edmEntityType)
        {
            var taupoEntityType = new EntityType(edmEntityType.Namespace, edmEntityType.Name)
            {
                IsAbstract = edmEntityType.IsAbstract,
                IsOpen     = edmEntityType.IsOpen,
            };

            if (edmEntityType.BaseType != null)
            {
                taupoEntityType.BaseType = new EntityTypeReference(edmEntityType.BaseEntityType().Namespace, edmEntityType.BaseEntityType().Name);
            }

            foreach (var edmProperty in edmEntityType.DeclaredStructuralProperties())
            {
                var taupoProperty = this.ConvertToTaupoProperty(edmProperty);
                taupoProperty.IsPrimaryKey = edmEntityType.Key().Contains(edmProperty);
                taupoEntityType.Add(taupoProperty);
            }

            this.ConvertAnnotationsIntoTaupo(edmEntityType, taupoEntityType);
            return(taupoEntityType);
        }
        private void VerifyMediaEntityPutOperation(string annotation, bool enableOperationId)
        {
            // Arrange
            IEdmModel model = MediaEntityGetOperationHandlerTests.GetEdmModel(annotation);
            OpenApiConvertSettings settings = new OpenApiConvertSettings
            {
                EnableOperationId = enableOperationId
            };

            ODataContext  context = new ODataContext(model, settings);
            IEdmEntitySet todos   = model.EntityContainer.FindEntitySet("Todos");
            IEdmSingleton me      = model.EntityContainer.FindSingleton("me");

            Assert.NotNull(todos);

            IEdmEntityType         todo = model.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "Todo");
            IEdmStructuralProperty sp   = todo.DeclaredStructuralProperties().First(c => c.Name == "Logo");
            ODataPath path = new ODataPath(new ODataNavigationSourceSegment(todos),
                                           new ODataKeySegment(todos.EntityType()),
                                           new ODataStreamPropertySegment(sp.Name));

            IEdmEntityType         user        = model.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "user");
            IEdmNavigationProperty navProperty = user.DeclaredNavigationProperties().First(c => c.Name == "photo");
            ODataPath path2 = new ODataPath(new ODataNavigationSourceSegment(me),
                                            new ODataNavigationPropertySegment(navProperty),
                                            new ODataStreamContentSegment());

            // Act
            var putOperation  = _operationalHandler.CreateOperation(context, path);
            var putOperation2 = _operationalHandler.CreateOperation(context, path2);

            // Assert
            Assert.NotNull(putOperation);
            Assert.NotNull(putOperation2);
            Assert.Equal("Update media content for Todo in Todos", putOperation.Summary);
            Assert.Equal("Update media content for the navigation property photo in me", putOperation2.Summary);
            Assert.NotNull(putOperation.Tags);
            Assert.NotNull(putOperation2.Tags);

            var tag  = Assert.Single(putOperation.Tags);
            var tag2 = Assert.Single(putOperation2.Tags);

            Assert.Equal("Todos.Todo", tag.Name);
            Assert.Equal("me.profilePhoto", tag2.Name);

            Assert.NotNull(putOperation.Responses);
            Assert.NotNull(putOperation2.Responses);
            Assert.Equal(2, putOperation.Responses.Count);
            Assert.Equal(2, putOperation2.Responses.Count);
            Assert.Equal(new[] { "204", "default" }, putOperation.Responses.Select(r => r.Key));
            Assert.Equal(new[] { "204", "default" }, putOperation2.Responses.Select(r => r.Key));

            if (!string.IsNullOrEmpty(annotation))
            {
                Assert.Equal(2, putOperation.RequestBody.Content.Keys.Count);
                Assert.True(putOperation.RequestBody.Content.ContainsKey("image/png"));
                Assert.True(putOperation.RequestBody.Content.ContainsKey("image/jpeg"));

                Assert.Equal(1, putOperation2.RequestBody.Content.Keys.Count);
                Assert.True(putOperation2.RequestBody.Content.ContainsKey(Constants.ApplicationOctetStreamMediaType));
            }
            else
            {
                Assert.Equal(1, putOperation.RequestBody.Content.Keys.Count);
                Assert.Equal(1, putOperation2.RequestBody.Content.Keys.Count);
                Assert.True(putOperation.RequestBody.Content.ContainsKey(Constants.ApplicationOctetStreamMediaType));
                Assert.True(putOperation2.RequestBody.Content.ContainsKey(Constants.ApplicationOctetStreamMediaType));
            }

            if (enableOperationId)
            {
                Assert.Equal("Todos.Todo.UpdateLogo", putOperation.OperationId);
                Assert.Equal("me.UpdatePhotoContent", putOperation2.OperationId);
            }
            else
            {
                Assert.Null(putOperation.OperationId);
                Assert.Null(putOperation2.OperationId);
            }
        }
        public void ConvertPrimitiveTypeProperties()
        {
            var taupoModel = new EntityModelSchema()
            {
                new EntityType("NS1", "Entity1")
                {
                    new MemberProperty("p0", EdmDataTypes.Binary(100, true)),
                    new MemberProperty("p1", EdmDataTypes.Boolean),
                    new MemberProperty("p2", EdmDataTypes.Byte),
                    new MemberProperty("p4", EdmDataTypes.DateTimeOffset(7)),
                    new MemberProperty("p5", EdmDataTypes.Decimal(10, 2)),
                    new MemberProperty("p6", EdmDataTypes.Double),
                    new MemberProperty("p7", EdmDataTypes.Guid),
                    new MemberProperty("p8", EdmDataTypes.Int16),
                    new MemberProperty("p9", EdmDataTypes.Int32),
                    new MemberProperty("p10", EdmDataTypes.Int64),
                    new MemberProperty("p11", EdmDataTypes.SByte),
                    new MemberProperty("p12", EdmDataTypes.Single),
                    new MemberProperty("p13", EdmDataTypes.String(50, true, false)),
                    new MemberProperty("p14", EdmDataTypes.Time(11)),
                },
            };

            IEdmModel result = this.converter.ConvertToEdmModel(taupoModel);

            IEdmEntityType entity = result.SchemaElements.OfType <IEdmEntityType>().First();

            Assert.AreEqual(14, entity.DeclaredStructuralProperties().Count());
            Assert.AreEqual("Edm.Binary", entity.DeclaredStructuralProperties().ElementAt(0).Type.FullName());
            Assert.AreEqual("Edm.Boolean", entity.DeclaredStructuralProperties().ElementAt(1).Type.FullName());
            Assert.AreEqual("Edm.Byte", entity.DeclaredStructuralProperties().ElementAt(2).Type.FullName());
            Assert.AreEqual("Edm.DateTimeOffset", entity.DeclaredStructuralProperties().ElementAt(3).Type.FullName());
            Assert.AreEqual("Edm.Decimal", entity.DeclaredStructuralProperties().ElementAt(4).Type.FullName());
            Assert.AreEqual("Edm.Double", entity.DeclaredStructuralProperties().ElementAt(5).Type.FullName());
            Assert.AreEqual("Edm.Guid", entity.DeclaredStructuralProperties().ElementAt(6).Type.FullName());
            Assert.AreEqual("Edm.Int16", entity.DeclaredStructuralProperties().ElementAt(7).Type.FullName());
            Assert.AreEqual("Edm.Int32", entity.DeclaredStructuralProperties().ElementAt(8).Type.FullName());
            Assert.AreEqual("Edm.Int64", entity.DeclaredStructuralProperties().ElementAt(9).Type.FullName());
            Assert.AreEqual("Edm.SByte", entity.DeclaredStructuralProperties().ElementAt(10).Type.FullName());
            Assert.AreEqual("Edm.Single", entity.DeclaredStructuralProperties().ElementAt(11).Type.FullName());
            Assert.AreEqual("Edm.String", entity.DeclaredStructuralProperties().ElementAt(12).Type.FullName());
            Assert.AreEqual("Edm.Duration", entity.DeclaredStructuralProperties().ElementAt(13).Type.FullName());

            foreach (var p in entity.DeclaredStructuralProperties())
            {
                Assert.AreEqual(EdmTypeKind.Primitive, p.Type.TypeKind());
                Assert.IsFalse(p.Type.IsNullable);
            }
        }
        public void ConvertEntityType()
        {
            var taupoModel = new EntityModelSchema()
            {
                new EntityType("NS1", "Entity1")
                {
                    new MemberProperty("p1", EdmDataTypes.Int16)
                    {
                        new AttributeAnnotation()
                        {
                            Content = new XAttribute(this.annotationNamespace + "foo1", "bar1")
                        },
                    },
                    new MemberProperty("p2", EdmDataTypes.Int64)
                    {
                        IsPrimaryKey = true
                    },
                    new MemberProperty("p3", EdmDataTypes.Boolean)
                    {
                        IsPrimaryKey = true
                    },
                    new AttributeAnnotation()
                    {
                        Content = new XAttribute(this.annotationNamespace + "foo2", "bar2")
                    },
                },
            };

            IEdmModel result = this.converter.ConvertToEdmModel(taupoModel);

            Assert.IsNull(result.EntityContainer);
            Assert.AreEqual(1, result.SchemaElements.Count());
            Assert.AreEqual(1, result.SchemaElements.OfType <IEdmEntityType>().Count());

            IEdmEntityType entity = result.SchemaElements.OfType <IEdmEntityType>().First();

            Assert.AreEqual("NS1.Entity1", entity.FullName());
            Assert.AreEqual(3, entity.DeclaredStructuralProperties().Count());
            Assert.AreEqual("p1", entity.DeclaredStructuralProperties().ElementAt(0).Name);
            Assert.AreEqual("p2", entity.DeclaredStructuralProperties().ElementAt(1).Name);
            Assert.AreEqual("p3", entity.DeclaredStructuralProperties().ElementAt(2).Name);

            Assert.AreEqual(2, entity.DeclaredKey.Count());
            Assert.AreEqual("p2", entity.DeclaredKey.ElementAt(0).Name);
            Assert.AreEqual("p3", entity.DeclaredKey.ElementAt(1).Name);

            Assert.AreEqual(1, result.DirectValueAnnotations(entity).Count());
            Assert.AreEqual("bogus", result.DirectValueAnnotations(entity).First().NamespaceUri);
            Assert.AreEqual("foo2", result.DirectValueAnnotations(entity).First().Name);
            Assert.AreEqual("bar2", (((IEdmDirectValueAnnotation)result.DirectValueAnnotations(entity).First()).Value as IEdmStringValue).Value);

            IEdmStructuralProperty p1 = entity.DeclaredStructuralProperties().ElementAt(0);

            Assert.AreEqual(1, result.DirectValueAnnotations(p1).Count());
            Assert.AreEqual("bogus", result.DirectValueAnnotations(p1).First().NamespaceUri);
            Assert.AreEqual("foo1", result.DirectValueAnnotations(p1).First().Name);
            Assert.AreEqual("bar1", (((IEdmDirectValueAnnotation)result.DirectValueAnnotations(p1).First()).Value as IEdmStringValue).Value);

            IEdmStructuralProperty p2 = entity.DeclaredStructuralProperties().ElementAt(1);

            Assert.AreEqual(0, result.DirectValueAnnotations(p2).Count());
        }
        private EntityType ConvertToTaupoEntityType(IEdmEntityType edmEntityType)
        {
            var taupoEntityType = new EntityType(edmEntityType.Namespace, edmEntityType.Name)
            {
                IsAbstract = edmEntityType.IsAbstract,
                IsOpen = edmEntityType.IsOpen,
            };

            if (edmEntityType.BaseType != null)
            {
                taupoEntityType.BaseType = new EntityTypeReference(edmEntityType.BaseEntityType().Namespace, edmEntityType.BaseEntityType().Name);
            }

            foreach (var edmProperty in edmEntityType.DeclaredStructuralProperties())
            {
                var taupoProperty = this.ConvertToTaupoProperty(edmProperty);
                taupoProperty.IsPrimaryKey = edmEntityType.Key().Contains(edmProperty);
                taupoEntityType.Add(taupoProperty);
            }

            this.ConvertAnnotationsIntoTaupo(edmEntityType, taupoEntityType);
            return taupoEntityType;
        }
        private void FillStockContentsForEntityWithoutNavigation(IEdmEntityType edmType, IEdmModel edmModel, EdmModel stockModel)
        {
            var stockType = (EdmEntityType)stockModel.FindType(edmType.FullName());
            this.SetImmediateAnnotations(edmType, stockType, edmModel, stockModel);

            foreach (var edmProperty in edmType.DeclaredStructuralProperties())
            {
                ConvertToStockStructuralProperty((IEdmStructuralProperty)edmProperty, edmModel, stockModel);
            }

            if (edmType.DeclaredKey != null)
            {
                stockType.AddKeys(edmType.DeclaredKey.Select(n => stockType.FindProperty(n.Name) as IEdmStructuralProperty).ToArray());
            }
        }
Example #15
0
 private IEdmStructuralProperty GetStructuralProperty(IEdmEntityType entityType, string identifier)
 {
     return(entityType.DeclaredStructuralProperties().FirstOrDefault(x => x.Name.Equals(identifier)));
 }