private string WriteAndVerifyEmployeeFeed(ODataMessageWriterSettings settings, string mimeType, bool hasModel) { // create a feed with two entries var employeeFeed = new ODataFeed(); if (mimeType == MimeTypes.ApplicationAtomXml) { employeeFeed.Id = new Uri(this.ServiceUri + "Person/" + NameSpace + "Employee"); } if (!hasModel) { employeeFeed.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo() { NavigationSourceName = "Person", NavigationSourceEntityTypeName = NameSpace + "Person", ExpectedTypeName = NameSpace + "Employee" }); } ODataEntry employeeEntry = WritePayloadHelper.CreateEmployeeEntryNoMetadata(false); ODataEntry specialEmployeeEntry = WritePayloadHelper.CreateSpecialEmployeeEntryNoMetadata(false); // expected result with AutoGeneratedUrlsShouldPutKeyValueInDedicatedSegment Dictionary <string, object> expectedEmployeeObject = WritePayloadHelper.ComputeExpectedFullMetadataEntryObject(WritePayloadHelper.EmployeeType, "Person/-3", employeeEntry, hasModel, true); Dictionary <string, object> expectedSpecialEmployeeObject = WritePayloadHelper.ComputeExpectedFullMetadataEntryObject(WritePayloadHelper.SpecialEmployeeType, "Person/-10", specialEmployeeEntry, hasModel, true); // write the response message and read using ODL reader var responseMessage = new StreamResponseMessage(new MemoryStream()); responseMessage.SetHeader("Content-Type", mimeType); string result = string.Empty; using (var messageWriter = this.CreateODataMessageWriter(responseMessage, settings, hasModel)) { var odataWriter = this.CreateODataFeedWriter(messageWriter, WritePayloadHelper.PersonSet, WritePayloadHelper.EmployeeType, hasModel); odataWriter.WriteStart(employeeFeed); odataWriter.WriteStart(employeeEntry); odataWriter.WriteEnd(); // toggle AutoComputePayloadMetadataInJson, this should not affect the writing result settings.AutoComputePayloadMetadataInJson = !settings.AutoComputePayloadMetadataInJson; odataWriter.WriteStart(specialEmployeeEntry); odataWriter.WriteEnd(); odataWriter.WriteEnd(); result = this.ReadFeedEntryMessage(true, responseMessage, mimeType, WritePayloadHelper.PersonSet, WritePayloadHelper.PersonType); } // For Json light, verify the resulting metadata is as expected if (mimeType != MimeTypes.ApplicationAtomXml) { JavaScriptSerializer jScriptSerializer = new JavaScriptSerializer(); Dictionary <string, object> resultObject = jScriptSerializer.DeserializeObject(result) as Dictionary <string, object>; VerifyODataContextAnnotation(this.ServiceUri + "$metadata#Person/" + NameSpace + "Employee", resultObject, mimeType); settings.AutoComputePayloadMetadataInJson = !settings.AutoComputePayloadMetadataInJson; VerifyEntry(expectedEmployeeObject, employeeEntry, new ODataNavigationLink[] { }, new ODataProperty[] { }, (resultObject.Last().Value as object[]).First() as Dictionary <string, object>, mimeType, settings.AutoComputePayloadMetadataInJson); VerifyEntry(expectedSpecialEmployeeObject, specialEmployeeEntry, new ODataNavigationLink[] { }, new ODataProperty[] { }, (resultObject.Last().Value as object[]).Last() as Dictionary <string, object>, mimeType, settings.AutoComputePayloadMetadataInJson); } return(result); }
private string WriteAndVerifyPersonFeed(ODataMessageWriterSettings settings, string mimeType, bool hasModel) { // create a Person feed containing a person, an employee, a special employee var personFeed = new ODataFeed(); if (mimeType == MimeTypes.ApplicationAtomXml) { personFeed.Id = new Uri(this.ServiceUri + "Person"); } if (!hasModel) { personFeed.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo() { NavigationSourceName = "Person", NavigationSourceEntityTypeName = NameSpace + "Person" }); } ODataEntry personEntry = WritePayloadHelper.CreatePersonEntryNoMetadata(hasModel); Dictionary <string, object> expectedPersonObject = WritePayloadHelper.ComputeExpectedFullMetadataEntryObject(WritePayloadHelper.PersonType, "Person(-5)", personEntry, hasModel); ODataEntry employeeEntry = WritePayloadHelper.CreateEmployeeEntryNoMetadata(hasModel); Dictionary <string, object> expectedEmployeeObject = WritePayloadHelper.ComputeExpectedFullMetadataEntryObject(WritePayloadHelper.EmployeeType, "Person(-3)", employeeEntry, hasModel, true); ODataEntry specialEmployeeEntry = WritePayloadHelper.CreateSpecialEmployeeEntryNoMetadata(hasModel); Dictionary <string, object> expectedSpecialEmployeeObject = WritePayloadHelper.ComputeExpectedFullMetadataEntryObject(WritePayloadHelper.SpecialEmployeeType, "Person(-10)", specialEmployeeEntry, hasModel, true); // write the response message and read using ODL reader var responseMessage = new StreamResponseMessage(new MemoryStream()); responseMessage.SetHeader("Content-Type", mimeType); string result = string.Empty; using (var messageWriter = this.CreateODataMessageWriter(responseMessage, settings, hasModel)) { var odataWriter = this.CreateODataFeedWriter(messageWriter, WritePayloadHelper.PersonSet, WritePayloadHelper.PersonType, hasModel); odataWriter.WriteStart(personFeed); odataWriter.WriteStart(personEntry); odataWriter.WriteEnd(); odataWriter.WriteStart(employeeEntry); odataWriter.WriteEnd(); odataWriter.WriteStart(specialEmployeeEntry); odataWriter.WriteEnd(); // Finish writing the feed. odataWriter.WriteEnd(); result = this.ReadFeedEntryMessage(true, responseMessage, mimeType, WritePayloadHelper.PersonSet, WritePayloadHelper.PersonType); } // For Json light, verify the resulting metadata is as expected if (mimeType != MimeTypes.ApplicationAtomXml) { JavaScriptSerializer jScriptSerializer = new JavaScriptSerializer(); Dictionary <string, object> resultObject = jScriptSerializer.DeserializeObject(result) as Dictionary <string, object>; VerifyODataContextAnnotation(this.ServiceUri + "$metadata#Person", resultObject, mimeType); VerifyEntry(expectedPersonObject, personEntry, new ODataNavigationLink[] { }, new ODataProperty[] { }, (resultObject.Last().Value as object[]).ElementAt(0) as Dictionary <string, object>, mimeType, settings.AutoComputePayloadMetadataInJson); VerifyEntry(expectedEmployeeObject, employeeEntry, new ODataNavigationLink[] { }, new ODataProperty[] { }, (resultObject.Last().Value as object[]).ElementAt(1) as Dictionary <string, object>, mimeType, settings.AutoComputePayloadMetadataInJson); VerifyEntry(expectedSpecialEmployeeObject, specialEmployeeEntry, new ODataNavigationLink[] { }, new ODataProperty[] { }, (resultObject.Last().Value as object[]).ElementAt(2) as Dictionary <string, object>, mimeType, settings.AutoComputePayloadMetadataInJson); } return(result); }