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; }
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); }
private static Uri LinkFactory(EntityInstanceContext<Entities.People> context, string ServiceUriPrefix, string EntitySetName) { var linkUri = context.Request.RequestUri.FindParentUri(ServiceUriPrefix); object id = new object(); context.EdmObject.TryGetPropertyValue("Id", out id); linkUri.Query = string.Empty; linkUri.Fragment = string.Empty; linkUri.Path = string.Format( CultureInfo.InvariantCulture, "{0}{1}({2})", linkUri.Path, EntitySetName, (int)id); linkUri.AddFiddler(); return linkUri.Uri; }
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 <IEdmTypeReference>(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(); }
/// <summary> /// Creates ETag for the entity instance. /// </summary> /// <param name="entityInstanceContext">The context that contains the entity instance.</param> /// <returns>The ETag created.</returns> public override string CreateETag(EntityInstanceContext entityInstanceContext) { Ensure.NotNull(entityInstanceContext); string etag = null; object etagGetterObject; if (entityInstanceContext.Request.Properties.TryGetValue("ETagGetter", out etagGetterObject)) { Func<object, string> etagGetter = etagGetterObject as Func<object, string>; if (etagGetter != null) { etag = etagGetter(entityInstanceContext.EntityInstance); } } if (etag == null) { etag = base.CreateETag(entityInstanceContext); } return etag; }
private static EntityTagHeaderValue CreateETag( EntityInstanceContext entityInstanceContext, IETagHandler handler) { IEdmModel model = entityInstanceContext.EdmModel; IEdmEntitySet entitySet = entityInstanceContext.NavigationSource as IEdmEntitySet; IEnumerable<IEdmStructuralProperty> concurrencyProperties; if (model != null && entitySet != null) { concurrencyProperties = model.GetConcurrencyProperties(entitySet).OrderBy(c => c.Name); } else { concurrencyProperties = Enumerable.Empty<IEdmStructuralProperty>(); } 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<IEdmTypeReference>(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 GetPropertyValue_ThrowsInvalidOperation_IfEdmObjectIsNull() { EntityInstanceContext instanceContext = new EntityInstanceContext(); Assert.Throws<InvalidOperationException>( () => instanceContext.GetPropertyValue("SomeProperty"), "The property 'EdmObject' of EntityInstanceContext cannot be null."); }
public void EmptyCtor_InitializesProperty_SerializerContext() { var context = new EntityInstanceContext(); Assert.NotNull(context.SerializerContext); }
public void Property_EntityInstance_ReturnsEdmStructuredObjectInstance() { object instance = new object(); IEdmEntityTypeReference entityType = new Mock<IEdmEntityTypeReference>().Object; IEdmModel edmModel = new Mock<IEdmModel>().Object; EntityInstanceContext entityContext = new EntityInstanceContext { EdmObject = new TypedEdmEntityObject(instance, entityType, edmModel) }; Assert.Same(instance, entityContext.EntityInstance); }
public void Property_EntityInstance_ReturnsNullWhenEdmObjectIsNull() { EntityInstanceContext entityContext = new EntityInstanceContext { EdmObject = null }; Assert.Null(entityContext.EntityInstance); }
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 does not contain an entry for the entity type 'NS.Name'."); }
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)))); EdmModel model = new EdmModel(); model.AddElement(edmType); model.SetAnnotationValue<ClrTypeAnnotation>(edmType, new ClrTypeAnnotation(typeof(TestEntity))); Mock<IEdmEntityObject> edmObject = new Mock<IEdmEntityObject>(); object propertyValue = new List<int> { 42 }; edmObject.Setup(e => e.TryGetPropertyValue("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); }
public void EmptyCtor_InitializesProperty_SerializerContext() { var context = new EntityInstanceContext(); Assert.NotNull(context.SerializerContext); }
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; }