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> /// Copy constructor. /// </summary> /// <param name="other">The <see cref="JsonWriterTestExpectedResults"/> instance to copy.</param> public JsonWriterTestExpectedResults(JsonWriterTestExpectedResults other) : base(other) { this.Json = other.Json; this.FragmentExtractor = other.FragmentExtractor; }