private void TryAnnotateV2FeedPackage(ODataEntry entry, EntityInstanceContext entityInstanceContext)
        {
            var instance = entityInstanceContext.EntityInstance as V2FeedPackage;
            if (instance != null)
            {
                // Set Atom entry metadata
                var atomEntryMetadata = new AtomEntryMetadata();
                atomEntryMetadata.Title = instance.Id;
                if (!string.IsNullOrEmpty(instance.Authors))
                {
                    atomEntryMetadata.Authors = new[] { new AtomPersonMetadata { Name = instance.Authors } };
                }
                if (instance.LastUpdated > DateTime.MinValue)
                {
                    atomEntryMetadata.Updated = instance.LastUpdated;
                }
                if (!string.IsNullOrEmpty(instance.Summary))
                {
                    atomEntryMetadata.Summary = instance.Summary;
                }
                entry.SetAnnotation(atomEntryMetadata);

                // Add package download link
                entry.MediaResource = new ODataStreamReferenceValue
                {
                    ContentType = ContentType,
                    ReadLink = BuildLinkForStreamProperty("v2", instance.Id, instance.Version, entityInstanceContext.Request)
                };
            }
        }
        public override ODataEntry CreateEntry(SelectExpandNode selectExpandNode, EntityInstanceContext entityInstanceContext)
        {
            var entry = base.CreateEntry(selectExpandNode, entityInstanceContext);

            TryAnnotateV1FeedPackage(entry, entityInstanceContext);
            TryAnnotateV2FeedPackage(entry, entityInstanceContext);

            return entry;
        }
        public void GetPropertyValue_ThrowsInvalidOperation_IfPropertyIsNotFound()
        {
            IEdmEntityTypeReference entityType = new EdmEntityTypeReference(new EdmEntityType("NS", "Name"), isNullable: false);
            Mock<IEdmStructuredObject> edmObject = new Mock<IEdmStructuredObject>();
            edmObject.Setup(o => o.GetEdmType()).Returns(entityType);
            EntityInstanceContext instanceContext = new EntityInstanceContext(_serializerContext, entityType, edmObject.Object);

            Assert.Throws<InvalidOperationException>(
                () => instanceContext.GetPropertyValue("NotPresentProperty"),
                "The EDM instance of type '[NS.Name Nullable=False]' is missing the property 'NotPresentProperty'.");
        }
        public override ODataEntry CreateEntry(SelectExpandNode selectExpandNode, EntityInstanceContext entityInstanceContext)
        {
            ODataEntry entry = base.CreateEntry(selectExpandNode, entityInstanceContext);

            Document document = entityInstanceContext.EntityInstance as Document;
            if (entry != null && document != null)
            {
                // annotate the document with the score.
                entry.InstanceAnnotations.Add(new ODataInstanceAnnotation("org.northwind.search.score", new ODataPrimitiveValue(document.Score)));
            }

            return entry;
        }
        public void SetUp()
        {
            SetUpRequestWithOptions("/api/odata");

            serializer = new ODataPackageDefaultStreamAwareEntityTypeSerializer(
                new ODataPackageDefaultStreamAwareSerializerProvider());

            context = new EntityInstanceContext
            {
                EdmModel = model,
                Request = request
            };
        }
Beispiel #6
0
        private static EntityTagHeaderValue CreateETag(
            EntityInstanceContext entityInstanceContext,
            IETagHandler handler)
        {
            IEnumerable <IEdmStructuralProperty> concurrencyProperties =
                entityInstanceContext.EntityType.GetConcurrencyProperties().OrderBy(c => c.Name);

            IDictionary <string, object> properties = new Dictionary <string, object>();

            foreach (IEdmStructuralProperty etagProperty in concurrencyProperties)
            {
                properties.Add(etagProperty.Name, entityInstanceContext.GetPropertyValue(etagProperty.Name));
            }
            EntityTagHeaderValue etagHeaderValue = handler.CreateETag(properties);

            return(etagHeaderValue);
        }
        public void GetPropertyValue_ThrowsInvalidOperation_IfEdmObjectGetEdmTypeReturnsNull()
        {
            // Arrange
            object outObject             = null;
            Mock <IEdmEntityObject> mock = new Mock <IEdmEntityObject>();

            mock.Setup(o => o.TryGetPropertyValue(It.IsAny <string>(), out outObject)).Returns(false).Verifiable();
            mock.Setup(o => o.GetEdmType()).Returns <IEdmRowTypeReference>(null).Verifiable();
            EntityInstanceContext context = new EntityInstanceContext();

            context.EdmObject = mock.Object;

            // Act & Assert
            Assert.Throws <InvalidOperationException>(() => context.GetPropertyValue("SomeProperty"),
                                                      exceptionMessage: "The EDM type of the object of type 'Castle.Proxies.IEdmEntityObjectProxy' is null. " +
                                                      "The EDM type of an IEdmObject cannot be null.");
            mock.Verify();
        }
        public void Property_EntityInstance_ThrowsInvalidOp_EntityTypeDoesNotHaveAMapping()
        {
            EdmEntityType entityType = new EdmEntityType("NS", "Name");
            EdmModel model = new EdmModel();
            IEdmEntityObject instance = new Mock<IEdmEntityObject>().Object;
            EntityInstanceContext entityContext = new EntityInstanceContext { EntityType = entityType, EdmModel = model, EdmObject = instance };

            Assert.Throws<InvalidOperationException>(
                () => entityContext.EntityInstance, "The provided mapping doesn't contain an entry for the entity type 'NS.Name'.");
        }
 public void GetPropertyValue_ThrowsInvalidOperation_IfEdmObjectIsNull()
 {
     EntityInstanceContext instanceContext = new EntityInstanceContext();
     Assert.Throws<InvalidOperationException>(
         () => instanceContext.GetPropertyValue("SomeProperty"),
         "The property 'EdmObject' of EntityInstanceContext cannot be null.");
 }
Beispiel #10
0
        public void Ctor_SetsProperty_EntityInstance()
        {
            var context = new EntityInstanceContext(_serializerContext, _entityType, _entityInstance);

            Assert.Equal(_entityInstance, context.EntityInstance);
        }
 public void EmptyCtor_InitializesProperty_SerializerContext()
 {
     var context = new EntityInstanceContext();
     Assert.NotNull(context.SerializerContext);
 }
        public void EmptyCtor_InitializesProperty_SerializerContext()
        {
            var context = new EntityInstanceContext();

            Assert.NotNull(context.SerializerContext);
        }
        public void Property_EntityInstance_CanBeBuiltFromIEdmObject()
        {
            // Arrange
            EdmEntityType edmType = new EdmEntityType("NS", "Name");
            edmType.AddStructuralProperty("Property", EdmPrimitiveTypeKind.Int32);
            EdmModel model = new EdmModel();
            model.AddElement(edmType);
            model.SetAnnotationValue<ClrTypeAnnotation>(edmType, new ClrTypeAnnotation(typeof(TestEntity)));
            Mock<IEdmEntityObject> edmObject = new Mock<IEdmEntityObject>();
            object propertyValue = 42;
            edmObject.Setup(e => e.TryGetPropertyValue("Property", out propertyValue)).Returns(true);
            edmObject.Setup(e => e.GetEdmType()).Returns(new EdmEntityTypeReference(edmType, isNullable: false));

            EntityInstanceContext entityContext = new EntityInstanceContext { EdmModel = model, EdmObject = edmObject.Object, EntityType = edmType };

            // Act
            object resource = entityContext.EntityInstance;

            // Assert
            TestEntity testEntity = Assert.IsType<TestEntity>(resource);
            Assert.Equal(42, testEntity.Property);
        }
        public void Property_EntityInstance_ReturnsEdmStructuredObjectInstance()
        {
            object instance = new object();
            IEdmEntityTypeReference entityType = new Mock<IEdmEntityTypeReference>().Object;
            EntityInstanceContext entityContext = new EntityInstanceContext { EdmObject = new TypedEdmEntityObject(instance, entityType) };

            Assert.Same(instance, entityContext.EntityInstance);
        }
 public void Property_EntityInstance_ReturnsNullWhenEdmObjectIsNull()
 {
     EntityInstanceContext entityContext = new EntityInstanceContext { EdmObject = null };
     Assert.Null(entityContext.EntityInstance);
 }
 public void Ctor_SetsProperty_EntityInstance()
 {
     var context = new EntityInstanceContext(_serializerContext, _entityType, _entityInstance);
     Assert.Equal(_entityInstance, context.EntityInstance);
 }
        private static EntityTagHeaderValue CreateETag(
            EntityInstanceContext entityInstanceContext,
            IETagHandler handler)
        {
            IEnumerable<IEdmStructuralProperty> concurrencyProperties =
                entityInstanceContext.EntityType.GetConcurrencyProperties().OrderBy(c => c.Name);

            IDictionary<string, object> properties = new Dictionary<string, object>();
            foreach (IEdmStructuralProperty etagProperty in concurrencyProperties)
            {
                properties.Add(etagProperty.Name, entityInstanceContext.GetPropertyValue(etagProperty.Name));
            }
            EntityTagHeaderValue etagHeaderValue = handler.CreateETag(properties);
            return etagHeaderValue;
        }
        public void GetPropertyValue_ThrowsInvalidOperation_IfEdmObjectGetEdmTypeReturnsNull()
        {
            // Arrange
            object outObject = null;
            Mock<IEdmEntityObject> mock = new Mock<IEdmEntityObject>();
            mock.Setup(o => o.TryGetPropertyValue(It.IsAny<string>(), out outObject)).Returns(false).Verifiable();
            mock.Setup(o => o.GetEdmType()).Returns<IEdmRowTypeReference>(null).Verifiable();
            EntityInstanceContext context = new EntityInstanceContext();
            context.EdmObject = mock.Object;

            // Act & Assert
            Assert.Throws<InvalidOperationException>(() => context.GetPropertyValue("SomeProperty"),
                exceptionMessage: "The EDM type of the object of type 'Castle.Proxies.IEdmEntityObjectProxy' is null. " +
                "The EDM type of an IEdmObject cannot be null.");
            mock.Verify();
        }
        public void Property_EntityInstance_EdmObjectHasCollectionProperty()
        {
            // Arrange
            EdmEntityType edmType = new EdmEntityType("NS", "Name");
            edmType.AddStructuralProperty(
                "CollectionProperty",
                new EdmCollectionTypeReference(
                    new EdmCollectionType(EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: false)), isNullable: false));
            EdmModel model = new EdmModel();
            model.AddElement(edmType);
            model.SetAnnotationValue<ClrTypeAnnotation>(edmType, new ClrTypeAnnotation(typeof(TestEntity)));
            Mock<IEdmStructuredObject> edmObject = new Mock<IEdmStructuredObject>();
            object propertyValue = new List<int> { 42 };
            edmObject.Setup(e => e.TryGetValue("CollectionProperty", out propertyValue)).Returns(true);
            edmObject.Setup(e => e.GetEdmType()).Returns(new EdmEntityTypeReference(edmType, isNullable: false));

            EntityInstanceContext entityContext = new EntityInstanceContext { EdmModel = model, EdmObject = edmObject.Object, EntityType = edmType };

            // Act
            object resource = entityContext.EntityInstance;

            // Assert
            TestEntity testEntity = Assert.IsType<TestEntity>(resource);
            Assert.Equal(new[] { 42 }, testEntity.CollectionProperty);
        }