public void WriteObjectInline_WritesEachEntityInstance() { // Arrange var mockSerializerProvider = new Mock<ODataSerializerProvider>(MockBehavior.Strict, _model); var mockCustomerSerializer = new Mock<ODataSerializer>(MockBehavior.Strict, ODataPayloadKind.Entry); var mockWriter = new Mock<ODataWriter>(); mockSerializerProvider .Setup(p => p.CreateEdmTypeSerializer(_customersType.ElementType())) .Returns(mockCustomerSerializer.Object); mockCustomerSerializer .Setup(s => s.WriteObjectInline(_customers[0], It.IsAny<ODataWriter>(), _writeContext)) .Verifiable(); mockCustomerSerializer .Setup(s => s.WriteObjectInline(_customers[1], It.IsAny<ODataWriter>(), _writeContext)) .Verifiable(); mockWriter .Setup(w => w.WriteStart(It.IsAny<ODataFeed>())) .Callback((ODataFeed feed) => { Assert.Equal("http://schemas.datacontract.org/2004/07/", feed.Id); }) .Verifiable(); _serializer = new ODataFeedSerializer(_customersType, mockSerializerProvider.Object); // Act _serializer.WriteObjectInline(_customers, mockWriter.Object, _writeContext); // Assert mockSerializerProvider.Verify(); mockCustomerSerializer.Verify(); mockWriter.Verify(); }
public void WriteObjectInline_WritesEachEntityInstance() { // Arrange SpyODataSerializer spy = new SpyODataSerializer(ODataPayloadKind.Entry); ODataSerializerProvider provider = new FakeODataSerializerProvider(spy); var mockWriter = new Mock<ODataWriter>(); mockWriter .Setup(w => w.WriteStart(It.IsAny<ODataFeed>())) .Callback((ODataFeed feed) => { Assert.Equal("http://schemas.datacontract.org/2004/07/", feed.Id); }) .Verifiable(); _serializer = new ODataFeedSerializer(_customersType, provider); // Act _serializer.WriteObjectInline(_customers, mockWriter.Object, _writeContext); // Assert WriteObjectInlineCall[] expectedWriteCalls = new WriteObjectInlineCall[] { new WriteObjectInlineCall { Graph = _customers[0], WriteContext = _writeContext }, new WriteObjectInlineCall { Graph = _customers[1], WriteContext = _writeContext } }; AssertEqual(expectedWriteCalls, spy.WriteObjectInlineCalls); mockWriter.Verify(); }
public void WriteObjectInline_Can_WriteCollectionOfIEdmObjects() { // Arrange IEdmTypeReference edmType = new EdmEntityTypeReference(new EdmEntityType("NS", "Name"), isNullable: false); IEdmCollectionTypeReference feedType = new EdmCollectionTypeReference(new EdmCollectionType(edmType), isNullable: false); Mock <IEdmObject> edmObject = new Mock <IEdmObject>(); edmObject.Setup(e => e.GetEdmType()).Returns(edmType); var mockWriter = new Mock <ODataWriter>(); Mock <ODataEdmTypeSerializer> customSerializer = new Mock <ODataEdmTypeSerializer>(ODataPayloadKind.Entry); customSerializer.Setup(s => s.WriteObjectInline(edmObject.Object, edmType, mockWriter.Object, _writeContext)).Verifiable(); Mock <ODataSerializerProvider> serializerProvider = new Mock <ODataSerializerProvider>(); serializerProvider.Setup(s => s.GetEdmTypeSerializer(edmType)).Returns(customSerializer.Object); ODataFeedSerializer serializer = new ODataFeedSerializer(serializerProvider.Object); // Act serializer.WriteObjectInline(new[] { edmObject.Object }, feedType, mockWriter.Object, _writeContext); // Assert customSerializer.Verify(); }
public void WriteObjectInline_Writes_RequestNextPageLink() { // Arrange var mockSerializerProvider = new Mock <ODataSerializerProvider>(_model); var mockCustomerSerializer = new Mock <ODataSerializer>(ODataPayloadKind.Entry); var mockWriter = new Mock <ODataWriter>(); Uri expectedNextLink = new Uri("http://nextlink.com"); _writeContext.Request = new HttpRequestMessage(); _writeContext.Request.Properties.Add("MS_NextPageLink", expectedNextLink); mockSerializerProvider .Setup(p => p.GetODataPayloadSerializer(typeof(Customer))) .Returns(mockCustomerSerializer.Object); mockWriter .Setup(m => m.WriteStart(It.IsAny <ODataFeed>())) .Callback((ODataFeed feed) => { Assert.Equal(expectedNextLink, feed.NextPageLink); }); _serializer = new ODataFeedSerializer(_customersType, mockSerializerProvider.Object); _serializer.WriteObjectInline(_customers, mockWriter.Object, _writeContext); }
public void WriteObjectInline_WritesEachEntityInstance() { // Arrange var mockSerializerProvider = new Mock <ODataSerializerProvider>(MockBehavior.Strict, _model); var mockCustomerSerializer = new Mock <ODataSerializer>(MockBehavior.Strict, ODataPayloadKind.Entry); var mockWriter = new Mock <ODataWriter>(); mockSerializerProvider .Setup(p => p.GetODataPayloadSerializer(typeof(Customer))) .Returns(mockCustomerSerializer.Object); mockCustomerSerializer .Setup(s => s.WriteObjectInline(_customers[0], It.IsAny <ODataWriter>(), _writeContext)) .Verifiable(); mockCustomerSerializer .Setup(s => s.WriteObjectInline(_customers[1], It.IsAny <ODataWriter>(), _writeContext)) .Verifiable(); mockWriter .Setup(w => w.WriteStart(It.IsAny <ODataFeed>())) .Callback((ODataFeed feed) => { Assert.Equal("http://schemas.datacontract.org/2004/07/", feed.Id); }) .Verifiable(); _serializer = new ODataFeedSerializer(_customersType, mockSerializerProvider.Object); // Act _serializer.WriteObjectInline(_customers, mockWriter.Object, _writeContext); // Assert mockSerializerProvider.Verify(); mockCustomerSerializer.Verify(); mockWriter.Verify(); }
public void WriteObjectInline_ThrowsSerializationException_IfGraphIsNotEnumerable() { ODataFeedSerializer serializer = new ODataFeedSerializer(_customersType, new DefaultODataSerializerProvider()); Assert.Throws <SerializationException>( () => serializer.WriteObjectInline(graph: 42, writer: new Mock <ODataWriter>().Object, writeContext: _writeContext), "ODataFeedSerializer cannot write an object of type 'System.Int32'."); }
public void WriteObjectInline_ThrowsSerializationException_CannotSerializerNull() { ODataFeedSerializer serializer = new ODataFeedSerializer(_customersType, new DefaultODataSerializerProvider()); Assert.Throws <SerializationException>( () => serializer.WriteObjectInline(graph: null, writer: new Mock <ODataWriter>().Object, writeContext: _writeContext), "Cannot serialize a null 'feed'."); }
public void WriteObjectInline_ThrowsArgumentNull_Writer() { ODataFeedSerializer serializer = new ODataFeedSerializer(_customersType, new DefaultODataSerializerProvider()); Assert.ThrowsArgumentNull( () => serializer.WriteObjectInline(graph: null, writer: null, writeContext: new ODataSerializerContext()), "writer"); }
public void WriteObjectInline_ThrowsArgumentNull_WriteContext() { ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider()); Assert.ThrowsArgumentNull( () => serializer.WriteObjectInline(graph: null, expectedType: null, writer: new Mock <ODataWriter>().Object, writeContext: null), "writeContext"); }
public void WriteObjectInline_Throws_NullElementInCollection_IfFeedContainsNullElement() { // Arrange IEnumerable instance = new object[] { null }; ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider()); // Act Assert.Throws <SerializationException>( () => serializer.WriteObjectInline(instance, _customersType, new Mock <ODataWriter>().Object, _writeContext), "Collections cannot contain null elements."); }
public void WriteObjectInline_Throws_TypeCannotBeSerialized_IfFeedContainsEntityThatCannotBeSerialized() { // Arrange Mock <ODataSerializerProvider> serializerProvider = new Mock <ODataSerializerProvider>(); serializerProvider.Setup(s => s.GetODataPayloadSerializer(_model, typeof(int))).Returns <ODataSerializer>(null); IEnumerable instance = new object[] { 42 }; ODataFeedSerializer serializer = new ODataFeedSerializer(_customersType, serializerProvider.Object); // Act Assert.Throws <SerializationException>( () => serializer.WriteObjectInline(instance, new Mock <ODataWriter>().Object, _writeContext), "'System.Int32' cannot be serialized using the ODataMediaTypeFormatter."); }
public void WriteObjectInline_WritesEachEntityInstance() { // Arrange Mock <ODataEdmTypeSerializer> customerSerializer = new Mock <ODataEdmTypeSerializer>(ODataPayloadKind.Entry); ODataSerializerProvider provider = ODataTestUtil.GetMockODataSerializerProvider(customerSerializer.Object); var mockWriter = new Mock <ODataWriter>(); customerSerializer.Setup(s => s.WriteObjectInline(_customers[0], _customersType.ElementType(), mockWriter.Object, _writeContext)).Verifiable(); customerSerializer.Setup(s => s.WriteObjectInline(_customers[1], _customersType.ElementType(), mockWriter.Object, _writeContext)).Verifiable(); _serializer = new ODataFeedSerializer(provider); // Act _serializer.WriteObjectInline(_customers, _customersType, mockWriter.Object, _writeContext); // Assert customerSerializer.Verify(); }
public void WriteObjectInline_WritesEachEntityInstance() { // Arrange Mock<ODataEntrySerializer> customerSerializer = new Mock<ODataEntrySerializer>(_customersType.ElementType(), ODataPayloadKind.Entry); ODataSerializerProvider provider = ODataTestUtil.GetMockODataSerializerProvider(customerSerializer.Object); var mockWriter = new Mock<ODataWriter>(); customerSerializer.Setup(s => s.WriteObjectInline(_customers[0], mockWriter.Object, _writeContext)).Verifiable(); customerSerializer.Setup(s => s.WriteObjectInline(_customers[1], mockWriter.Object, _writeContext)).Verifiable(); _serializer = new ODataFeedSerializer(_customersType, provider); // Act _serializer.WriteObjectInline(_customers, mockWriter.Object, _writeContext); // Assert customerSerializer.Verify(); }
public void WriteObjectInline_Writes_InlineCountAndNextLink() { // Arrange var mockSerializerProvider = new Mock <ODataSerializerProvider>(MockBehavior.Strict, _model); var mockCustomerSerializer = new Mock <ODataSerializer>(MockBehavior.Strict, ODataPayloadKind.Entry); var mockWriter = new Mock <ODataWriter>(); Uri expectedNextLink = new Uri("http://nextlink.com"); long expectedInlineCount = 1000; var result = new ODataResult <Customer>( _customers, expectedNextLink, expectedInlineCount ); mockSerializerProvider .Setup(p => p.GetODataPayloadSerializer(typeof(Customer))) .Returns(mockCustomerSerializer.Object); mockCustomerSerializer .Setup(s => s.WriteObjectInline(_customers[0], It.IsAny <ODataWriter>(), _writeContext)) .Verifiable(); mockCustomerSerializer .Setup(s => s.WriteObjectInline(_customers[1], It.IsAny <ODataWriter>(), _writeContext)) .Verifiable(); mockWriter .Setup(m => m.WriteStart(It.IsAny <ODataFeed>())) .Callback((ODataFeed feed) => { Assert.Equal(expectedNextLink, feed.NextPageLink); Assert.Equal(expectedInlineCount, feed.Count); }); _serializer = new ODataFeedSerializer(_customersType, mockSerializerProvider.Object); _serializer.WriteObjectInline(result, mockWriter.Object, _writeContext); // Assert mockSerializerProvider.Verify(); mockCustomerSerializer.Verify(); mockWriter.Verify(); }
public void WriteObjectInline_Can_WriteCollectionOfIEdmObjects() { // Arrange IEdmTypeReference edmType = new EdmEntityTypeReference(new EdmEntityType("NS", "Name"), isNullable: false); IEdmCollectionTypeReference feedType = new EdmCollectionTypeReference(new EdmCollectionType(edmType), isNullable: false); Mock<IEdmObject> edmObject = new Mock<IEdmObject>(); edmObject.Setup(e => e.GetEdmType()).Returns(edmType); var mockWriter = new Mock<ODataWriter>(); Mock<ODataEdmTypeSerializer> customSerializer = new Mock<ODataEdmTypeSerializer>(ODataPayloadKind.Entry); customSerializer.Setup(s => s.WriteObjectInline(edmObject.Object, edmType, mockWriter.Object, _writeContext)).Verifiable(); Mock<ODataSerializerProvider> serializerProvider = new Mock<ODataSerializerProvider>(); serializerProvider.Setup(s => s.GetEdmTypeSerializer(edmType)).Returns(customSerializer.Object); ODataFeedSerializer serializer = new ODataFeedSerializer(serializerProvider.Object); // Act serializer.WriteObjectInline(new[] { edmObject.Object }, feedType, mockWriter.Object, _writeContext); // Assert customSerializer.Verify(); }
public void WriteObjectInline_Throws_TypeCannotBeSerialized_IfFeedContainsEntityThatCannotBeSerialized() { // Arrange Mock<ODataSerializerProvider> serializerProvider = new Mock<ODataSerializerProvider>(); HttpRequestMessage request = new HttpRequestMessage(); serializerProvider.Setup(s => s.GetODataPayloadSerializer(_model, typeof(int), request)).Returns<ODataSerializer>(null); IEnumerable instance = new object[] { 42 }; ODataFeedSerializer serializer = new ODataFeedSerializer(serializerProvider.Object); // Act Assert.Throws<SerializationException>( () => serializer.WriteObjectInline(instance, _customersType, new Mock<ODataWriter>().Object, _writeContext), "'Default.Customer' cannot be serialized using the ODataMediaTypeFormatter."); }
public void WriteObjectInline_Throws_NullElementInCollection_IfFeedContainsNullElement() { // Arrange IEnumerable instance = new object[] { null }; ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider()); // Act Assert.Throws<SerializationException>( () => serializer.WriteObjectInline(instance, _customersType, new Mock<ODataWriter>().Object, _writeContext), "Collections cannot contain null elements."); }
public void WriteObjectInline_ThrowsSerializationException_IfGraphIsNotEnumerable() { ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider()); Assert.Throws<SerializationException>( () => serializer.WriteObjectInline(graph: 42, expectedType: _customersType, writer: new Mock<ODataWriter>().Object, writeContext: _writeContext), "ODataFeedSerializer cannot write an object of type 'System.Int32'."); }
public void WriteObjectInline_ThrowsSerializationException_CannotSerializerNull() { ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider()); Assert.Throws<SerializationException>( () => serializer.WriteObjectInline(graph: null, expectedType: _customersType, writer: new Mock<ODataWriter>().Object, writeContext: _writeContext), "Cannot serialize a null 'feed'."); }
public void WriteObjectInline_ThrowsArgumentNull_WriteContext() { ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider()); Assert.ThrowsArgumentNull( () => serializer.WriteObjectInline(graph: null, expectedType: null, writer: new Mock<ODataWriter>().Object, writeContext: null), "writeContext"); }
public void WriteObjectInline_Writes_RequestNextPageLink() { // Arrange var mockSerializerProvider = new Mock<ODataSerializerProvider>(_model); var mockCustomerSerializer = new Mock<ODataSerializer>(ODataPayloadKind.Entry); var mockWriter = new Mock<ODataWriter>(); Uri expectedNextLink = new Uri("http://nextlink.com"); _writeContext.Request = new HttpRequestMessage(); _writeContext.Request.Properties.Add("MS_NextPageLink", expectedNextLink); mockSerializerProvider .Setup(p => p.GetODataPayloadSerializer(typeof(Customer))) .Returns(mockCustomerSerializer.Object); mockWriter .Setup(m => m.WriteStart(It.IsAny<ODataFeed>())) .Callback((ODataFeed feed) => { Assert.Equal(expectedNextLink, feed.NextPageLink); }); _serializer = new ODataFeedSerializer(_customersType, mockSerializerProvider.Object); _serializer.WriteObjectInline(_customers, mockWriter.Object, _writeContext); }
public void WriteObjectInline_Sets_InlineCount_OnWriteStart() { // Arrange Mock<ODataEntrySerializer> customerSerializer = new Mock<ODataEntrySerializer>(_customersType.ElementType(), ODataPayloadKind.Entry); ODataSerializerProvider provider = ODataTestUtil.GetMockODataSerializerProvider(customerSerializer.Object); var mockWriter = new Mock<ODataWriter>(); Uri expectedNextLink = new Uri("http://nextlink.com"); long expectedInlineCount = 1000; var result = new PageResult<Customer>(_customers, expectedNextLink, expectedInlineCount); mockWriter .Setup(m => m.WriteStart(It.Is<ODataFeed>(feed => feed.Count == expectedInlineCount))) .Verifiable(); _serializer = new ODataFeedSerializer(_customersType, provider); // Act _serializer.WriteObjectInline(result, mockWriter.Object, _writeContext); // Assert mockWriter.Verify(); }
public void WriteObjectInline_Sets_NextPageLink_OnWriteEnd() { // Arrange Mock<ODataEntrySerializer> customerSerializer = new Mock<ODataEntrySerializer>(_customersType.ElementType(), ODataPayloadKind.Entry); ODataSerializerProvider provider = ODataTestUtil.GetMockODataSerializerProvider(customerSerializer.Object); var mockWriter = new Mock<ODataWriter>(); Uri expectedNextLink = new Uri("http://nextlink.com"); var result = new PageResult<Customer>(_customers, expectedNextLink, count: 0); ODataFeed actualFeed = null; mockWriter .Setup(m => m.WriteStart(It.Is<ODataFeed>(feed => feed.NextPageLink == null))) .Callback<ODataFeed>(feed => { actualFeed = feed; }) .Verifiable(); mockWriter .Setup(m => m.WriteEnd()) .Callback(() => { if (actualFeed != null) { Assert.Equal(expectedNextLink, actualFeed.NextPageLink); } }) .Verifiable(); _serializer = new ODataFeedSerializer(_customersType, provider); // Act _serializer.WriteObjectInline(result, mockWriter.Object, _writeContext); // Assert mockWriter.Verify(); }
public void WriteObjectInline_Writes_RequestNextPageLink() { // Arrange ODataSerializer customerSerializer = new StubODataSerializer(ODataPayloadKind.Entry); ODataSerializerProvider provider = new FakeODataSerializerProvider(customerSerializer); var mockWriter = new Mock<ODataWriter>(); Uri expectedNextLink = new Uri("http://nextlink.com"); _writeContext.NextPageLink = expectedNextLink; ODataFeed actualFeed = null; mockWriter .Setup(m => m.WriteStart(It.IsAny<ODataFeed>())) .Callback((ODataFeed feed) => { actualFeed = feed; }); _serializer = new ODataFeedSerializer(_customersType, provider); // Act _serializer.WriteObjectInline(_customers, mockWriter.Object, _writeContext); // Assert Assert.Equal(expectedNextLink, actualFeed.NextPageLink); }
public void WriteObjectInline_Writes_RequestCount() { // Arrange ODataSerializer customerSerializer = new StubODataSerializer(ODataPayloadKind.Entry); ODataSerializerProvider provider = new FakeODataSerializerProvider(customerSerializer); var mockWriter = new Mock<ODataWriter>(); long expectedCount = 12345; _writeContext.InlineCount = expectedCount; mockWriter .Setup(m => m.WriteStart(It.IsAny<ODataFeed>())) .Callback((ODataFeed feed) => { Assert.Equal(expectedCount, feed.Count); }); _serializer = new ODataFeedSerializer(_customersType, provider); // Act _serializer.WriteObjectInline(_customers, mockWriter.Object, _writeContext); // Assert mockWriter.Verify(); }
public void WriteObjectInline_Writes_InlineCountAndNextLink() { // Arrange SpyODataSerializer spy = new SpyODataSerializer(ODataPayloadKind.Entry); ODataSerializerProvider provider = new FakeODataSerializerProvider(spy); var mockWriter = new Mock<ODataWriter>(); Uri expectedNextLink = new Uri("http://nextlink.com"); long expectedInlineCount = 1000; var result = new PageResult<Customer>( _customers, expectedNextLink, expectedInlineCount ); ODataFeed actualFeed = null; mockWriter .Setup(m => m.WriteStart(It.IsAny<ODataFeed>())) .Callback((ODataFeed feed) => { actualFeed = feed; Assert.Equal(expectedInlineCount, feed.Count); }); _serializer = new ODataFeedSerializer(_customersType, provider); _serializer.WriteObjectInline(result, mockWriter.Object, _writeContext); // Assert WriteObjectInlineCall[] expectedWriteCalls = new WriteObjectInlineCall[] { new WriteObjectInlineCall { Graph = _customers[0], WriteContext = _writeContext }, new WriteObjectInlineCall { Graph = _customers[1], WriteContext = _writeContext } }; AssertEqual(expectedWriteCalls, spy.WriteObjectInlineCalls); mockWriter.Verify(); Assert.Equal(expectedNextLink, actualFeed.NextPageLink); }
public void WriteObjectInline_Writes_InlineCountAndNextLink() { // Arrange var mockSerializerProvider = new Mock<ODataSerializerProvider>(MockBehavior.Strict, _model); var mockCustomerSerializer = new Mock<ODataSerializer>(MockBehavior.Strict, ODataPayloadKind.Entry); var mockWriter = new Mock<ODataWriter>(); Uri expectedNextLink = new Uri("http://nextlink.com"); long expectedInlineCount = 1000; var result = new ODataResult<Customer>( _customers, expectedNextLink, expectedInlineCount ); mockSerializerProvider .Setup(p => p.CreateEdmTypeSerializer(_customersType.ElementType())) .Returns(mockCustomerSerializer.Object); mockCustomerSerializer .Setup(s => s.WriteObjectInline(_customers[0], It.IsAny<ODataWriter>(), _writeContext)) .Verifiable(); mockCustomerSerializer .Setup(s => s.WriteObjectInline(_customers[1], It.IsAny<ODataWriter>(), _writeContext)) .Verifiable(); mockWriter .Setup(m => m.WriteStart(It.IsAny<ODataFeed>())) .Callback((ODataFeed feed) => { Assert.Equal(expectedNextLink, feed.NextPageLink); Assert.Equal(expectedInlineCount, feed.Count); }); _serializer = new ODataFeedSerializer(_customersType, mockSerializerProvider.Object); _serializer.WriteObjectInline(result, mockWriter.Object, _writeContext); // Assert mockSerializerProvider.Verify(); mockCustomerSerializer.Verify(); mockWriter.Verify(); }
public void WriteObjectInline_Sets_Request_InlineCount_OnWriteStart() { // Arrange Mock<ODataEntrySerializer> customerSerializer = new Mock<ODataEntrySerializer>(_customersType.ElementType(), ODataPayloadKind.Entry); ODataSerializerProvider provider = ODataTestUtil.GetMockODataSerializerProvider(customerSerializer.Object); var mockWriter = new Mock<ODataWriter>(); long expectedCount = 12345; _writeContext.InlineCount = expectedCount; mockWriter .Setup(m => m.WriteStart(It.Is<ODataFeed>(feed => feed.Count == 12345))) .Verifiable(); _serializer = new ODataFeedSerializer(_customersType, provider); // Act _serializer.WriteObjectInline(_customers, mockWriter.Object, _writeContext); // Assert mockWriter.Verify(); }