private WriterTestExpectedResults CreateExpectedResults(WriterTestConfiguration testConfiguration, ProjectedPropertiesTestCase testCase, bool withModel) { if (testCase.ExpectedException != null) { ExpectedException expectedException = testCase.ExpectedException(withModel); if (expectedException != null) { return(new WriterTestExpectedResults(this.Settings.ExpectedResultSettings) { ExpectedException2 = expectedException }); } } if (testConfiguration.Format == ODataFormat.Atom) { #region Atom expected result var atomExpectedResults = new AtomWriterTestExpectedResults(this.Settings.ExpectedResultSettings) { Xml = new XElement("properties", testCase.ExpectedProperties.OrderBy(p => p).Select(p => new XElement(p))).ToString(), FragmentExtractor = (result) => { // Navigation links IEnumerable <string> actualProperties; if (result == null) { actualProperties = new string[0]; } else { actualProperties = result.Elements(TestAtomConstants.AtomXNamespace + TestAtomConstants.AtomLinkElementName) .Where(link => link.Attribute(TestAtomConstants.AtomLinkRelationAttributeName).Value.StartsWith(TestAtomConstants.ODataNavigationPropertiesRelatedLinkRelationPrefix)) .Select(link => link.Attribute(TestAtomConstants.AtomLinkTitleAttributeName).Value); // Named stream links actualProperties = actualProperties.Concat(result.Elements(TestAtomConstants.AtomXNamespace + TestAtomConstants.AtomLinkElementName) .Where(link => link.Attribute(TestAtomConstants.AtomLinkRelationAttributeName).Value.StartsWith(TestAtomConstants.ODataStreamPropertyEditMediaRelatedLinkRelationPrefix)) .Select(link => link.Attribute(TestAtomConstants.AtomLinkTitleAttributeName).Value)); // Association links actualProperties = actualProperties.Concat(result.Elements(TestAtomConstants.AtomXNamespace + TestAtomConstants.AtomLinkElementName) .Where(link => link.Attribute(TestAtomConstants.AtomLinkRelationAttributeName).Value.StartsWith(TestAtomConstants.ODataNavigationPropertiesAssociationLinkRelationPrefix)) .Select(link => link.Attribute(TestAtomConstants.AtomLinkTitleAttributeName).Value)); // Properties actualProperties = actualProperties.Concat(result.Elements(TestAtomConstants.AtomXNamespace + TestAtomConstants.AtomContentElementName) .Elements(TestAtomConstants.ODataMetadataXNamespace + TestAtomConstants.AtomPropertiesElementName) .Elements().Where(e => e.Name.Namespace == TestAtomConstants.ODataXNamespace) .Select(pe => pe.Name.LocalName)); } return(new XElement("properties", actualProperties.OrderBy(p => p).Select(p => new XElement(p)))); } }; if (testCase.NestedPayload) { var originalFragmentExtractor = atomExpectedResults.FragmentExtractor; atomExpectedResults.FragmentExtractor = (result) => { // Verify that the Wrapping_ID property is not written this.Assert.IsNull(result .Element(TestAtomConstants.AtomXNamespace + TestAtomConstants.AtomContentElementName) .Element(TestAtomConstants.ODataMetadataXNamespace + TestAtomConstants.AtomPropertiesElementName), "There should be no other property but the nav link and thus no m:properties in the content."); XElement expandedNavLinkElement = result.Elements(TestAtomConstants.AtomXNamespace + TestAtomConstants.AtomLinkElementName) .Where(link => link.Attribute(TestAtomConstants.AtomLinkRelationAttributeName).Value .StartsWith(TestAtomConstants.ODataNavigationPropertiesRelatedLinkRelationPrefix + "Wrapping_ExpandedEntry")) .SingleOrDefault(); return(originalFragmentExtractor( expandedNavLinkElement == null ? null : expandedNavLinkElement .Element(TestAtomConstants.ODataMetadataXNamespace + TestAtomConstants.ODataInlineElementName) .Element(TestAtomConstants.AtomXNamespace + TestAtomConstants.AtomEntryElementName))); }; } return(atomExpectedResults); #endregion Atom expected result } else if (testConfiguration.Format == ODataFormat.Json) { #region JSON Light expected result JsonArray expectedJson = new JsonArray(); foreach (var p in testCase.ExpectedProperties.Distinct().OrderBy(p => p)) { expectedJson.Add(new JsonPrimitiveValue(p)); } var jsonExpectedResults = new JsonWriterTestExpectedResults(this.Settings.ExpectedResultSettings) { Json = expectedJson.ToText(/*writingJsonLight*/ true, testConfiguration.MessageWriterSettings.Indent), FragmentExtractor = (result) => { // Everything except association links IEnumerable <string> actualProperties; if (result == null) { actualProperties = new string[0]; } else { List <string> propertyNames = new List <string>(); foreach (JsonProperty jsonProperty in result.Object().Properties) { string propertyName = jsonProperty.Name; int atIndex = propertyName.IndexOf('@'); int dotIndex = propertyName.IndexOf('.'); if (dotIndex < 0) { propertyNames.Add(propertyName); } else if (atIndex >= 0) { propertyNames.Add(propertyName.Substring(0, atIndex)); } } actualProperties = propertyNames.Distinct(); } JsonArray r = new JsonArray(); foreach (var p in actualProperties.OrderBy(p => p)) { r.Add(new JsonPrimitiveValue(p)); } return(r); } }; if (testCase.NestedPayload) { var originalFragmentExtractor = jsonExpectedResults.FragmentExtractor; jsonExpectedResults.FragmentExtractor = (result) => { // Verify that the Wrapping_ID property is not written JsonObject resultObject = result.Object(); this.Assert.IsNull(resultObject.Property("Wrapping_ID"), "No other property but the nav. link should be written."); return(originalFragmentExtractor( resultObject.Property("Wrapping_ExpandedEntry") == null ? null : resultObject.PropertyObject("Wrapping_ExpandedEntry"))); }; } return(jsonExpectedResults); #endregion JSON Light expected result } else { throw new TaupoInvalidOperationException("The format " + testConfiguration.Format.GetType().FullName + " is not supported."); } }
/// <summary> /// Applies the payload case to a test descriptor returning a new test descriptor. /// </summary> /// <param name="testDescriptor">The test descriptor to apply the case to.</param> /// <returns>The new test descriptor.</returns> internal PayloadWriterTestDescriptor <TOut> ApplyToTestDescriptor(PayloadWriterTestDescriptor <TIn> testDescriptor) { IEnumerable <TOut> payloadItems = null; if (this.GetPayloadItems != null) { payloadItems = this.GetPayloadItems(); } else { // for the test descriptor to specify payload items TIn and TOut have to be compatible! Type inputType = typeof(TIn); Type outputType = typeof(TOut); if (inputType != outputType && !outputType.IsAssignableFrom(inputType)) { throw new NotSupportedException( "The test descriptor can only specify payload items when they are compatible with the expected output type. " + inputType.FullName + " is not compatible with " + outputType.FullName + "."); } payloadItems = testDescriptor.PayloadItems.Cast <TOut>(); } EdmModel model = (EdmModel)testDescriptor.Model; IEdmElement payloadElementModelContainer = testDescriptor.PayloadEdmElementContainer; IEdmElement payloadElementType = testDescriptor.PayloadEdmElementType; if (model != null && this.ModelBuilder != null) { model = this.ModelBuilder(model); } return(new PayloadWriterTestDescriptor <TOut>( testDescriptor.TestDescriptorSettings, payloadItems, (testConfiguration) => { if (this.ShouldSkip != null && this.ShouldSkip(testConfiguration)) { return null; } WriterTestExpectedResults expectedResults = testDescriptor.ExpectedResultCallback(testConfiguration); AtomWriterTestExpectedResults atomResults = expectedResults as AtomWriterTestExpectedResults; if (atomResults != null) { return new AtomWriterTestExpectedResults(atomResults) { FragmentExtractor = this.AtomFragmentExtractor == null ? atomResults.FragmentExtractor : (result) => atomResults.FragmentExtractor(this.AtomFragmentExtractor(testConfiguration, result)), }; } JsonWriterTestExpectedResults jsonResults = expectedResults as JsonWriterTestExpectedResults; if (jsonResults != null) { return new JsonWriterTestExpectedResults(jsonResults) { FragmentExtractor = this.JsonLightFragmentExtractor == null ? jsonResults.FragmentExtractor : (result) => jsonResults.FragmentExtractor(this.JsonLightFragmentExtractor(testConfiguration, result)), }; } return expectedResults; }) { SkipTestConfiguration = testDescriptor.SkipTestConfiguration, IsGeneratedPayload = !this.NotGenerated, Model = model, PayloadEdmElementContainer = payloadElementModelContainer, PayloadEdmElementType = payloadElementType, }); }