/// <summary> /// Applies expected result normalizers to the specified payload element. /// </summary> /// <param name="expectedResultPayloadElement">The payload element to apply the normalizers to.</param> /// <param name="testConfiguration">The test configuration to use.</param> /// <returns>The expected result after normalization.</returns> private ODataPayloadElement ApplyExpectedResultNormalizers(ODataPayloadElement expectedResultPayloadElement, ReaderTestConfiguration testConfiguration) { // If we have some normalizers and some of them do apply to this test configuration // make a copy and apply the normalizer. if (this.ExpectedResultNormalizers != null) { ODataPayloadElement expectedResultPayloadElementCopy = null; foreach (var getNormalizerFunc in this.ExpectedResultNormalizers) { var normalizer = getNormalizerFunc(testConfiguration); if (normalizer != null) { if (expectedResultPayloadElementCopy == null) { expectedResultPayloadElementCopy = expectedResultPayloadElement.DeepCopy(); } expectedResultPayloadElementCopy = normalizer(expectedResultPayloadElementCopy); } } expectedResultPayloadElement = expectedResultPayloadElementCopy ?? expectedResultPayloadElement; } return(expectedResultPayloadElement); }
/// <summary> /// Normalizes the specified payload element. /// </summary> /// <param name="payloadElement">The payload element to normalize.</param> /// <returns>The normalized payload element.</returns> public ODataPayloadElement Normalize(ODataPayloadElement payloadElement) { // Allways make a deep copy since we will modify all the batch parts. payloadElement = payloadElement.DeepCopy(); this.Recurse(payloadElement); return(payloadElement); }
private void RunMessageSizeLimitTests( IEnumerable <ReaderTestConfiguration> testConfigurations, EdmModel model, ODataPayloadElement payload, MessageSizeLimitTestCase[] testCases, Func <ReaderTestConfiguration, bool> skipTestConfigurationFunc = null) { var transformScope = this.Settings.PayloadTransformFactory.EmptyScope(); using (transformScope.Apply()) { this.CombinatorialEngineProvider.RunCombinations( testCases, testConfigurations, (testCase, testConfiguration) => { int size = -1; if (testConfiguration.Format == ODataFormat.Atom && testCase.AtomSizes != null) { size = testConfiguration.IsRequest ? testCase.AtomSizes.RequestSize : testCase.AtomSizes.ResponseSize; } else if (testConfiguration.Format == ODataFormat.Json && testCase.JsonLightSizes != null) { size = testConfiguration.IsRequest ? testCase.JsonLightSizes.RequestSize : testCase.JsonLightSizes.ResponseSize; } else if (testCase.RawSizes != null) { size = testConfiguration.IsRequest ? testCase.RawSizes.RequestSize : testCase.RawSizes.ResponseSize; } int maxSize = testCase.MaxMessageSize >= 0 ? testCase.MaxMessageSize : 1024 * 1024; ExpectedException expectedException = size < 0 ? null : ODataExpectedExceptions.ODataException("MessageStreamWrappingStream_ByteLimitExceeded", size.ToString(), maxSize.ToString()); var testDescriptor = new PayloadReaderTestDescriptor(this.Settings) { PayloadEdmModel = model, PayloadElement = payload.DeepCopy(), ExpectedException = expectedException, SkipTestConfiguration = skipTestConfigurationFunc, ApplyPayloadTransformations = false, }; testDescriptor.ExpectedResultNormalizers.Add( tc => (Func <ODataPayloadElement, ODataPayloadElement>)null); if (testCase.MaxMessageSize > 0) { testConfiguration = new ReaderTestConfiguration(testConfiguration); testConfiguration.MessageReaderSettings.MessageQuotas.MaxReceivedMessageSize = testCase.MaxMessageSize; } testDescriptor.RunTest(testConfiguration); }); } }
/// <summary> /// Writes the payload to the stream using the given callback, then verifies the payload using the test deserializer /// </summary> /// <param name="originalPayload">The payload being tested, of which a copy will be made</param> /// <param name="message">The stream to write to</param> /// <param name="odataVersion">The OData protocol version to be used for writing payloads.</param> /// <param name="format">The current format</param> /// <param name="writeToStream">The callback to write to the stream</param> private void WriteAndLogODataPayload(ODataPayloadElement originalPayload, TestMessage message, ODataVersion odataVersion, ODataFormat format, Action <ODataPayloadElement> writeToStream) { ExceptionUtilities.CheckArgumentNotNull(originalPayload, "originalPayload"); ExceptionUtilities.CheckArgumentNotNull(writeToStream, "writeToStream"); // This is needed because we may modify the payload in use but the same is used in another iteration of the combinatorial engine var payload = originalPayload.DeepCopy(); WriteToStream(format, writeToStream, payload); var newPayload = TestWriterUtils.ReadToString(message); this.Logger.LogPayload(newPayload); }
/// <summary> /// Normalizes the observed batch payload element. /// </summary> /// <param name="expectedPayload">The expected payload element.</param> /// <param name="observedPayload">The observed payload element.</param> /// <returns>The normalized payload element.</returns> public ODataPayloadElement Normalize(ODataPayloadElement expectedPayload, ODataPayloadElement observedPayload) { ExceptionUtilities.CheckArgumentNotNull(expectedPayload, "expectedPayload"); ODataPayloadElement copyOfObservedPayload = observedPayload.DeepCopy(); var visitor = new BatchPayloadNormalizingVisitor { Assert = this.Assert, NormalizerSelector = this.NormalizerSelector, }; visitor.Normalize(expectedPayload, copyOfObservedPayload); return(copyOfObservedPayload); }
/// <summary> /// Normalizes the specified payload element. /// </summary> /// <param name="payloadElement">The payload element to normalize.</param> /// <returns>The normalized payload element.</returns> public ODataPayloadElement Normalize(ODataPayloadElement payloadElement) { // Allways make a deep copy since we will modify all the batch parts. payloadElement = payloadElement.DeepCopy(); this.Recurse(payloadElement); return payloadElement; }
/// <summary> /// Applies expected result normalizers to the specified payload element. /// </summary> /// <param name="expectedResultPayloadElement">The payload element to apply the normalizers to.</param> /// <param name="testConfiguration">The test configuration to use.</param> /// <returns>The expected result after normalization.</returns> private ODataPayloadElement ApplyExpectedResultNormalizers(ODataPayloadElement expectedResultPayloadElement, ReaderTestConfiguration testConfiguration) { // If we have some normalizers and some of them do apply to this test configuration // make a copy and apply the normalizer. if (this.ExpectedResultNormalizers != null) { ODataPayloadElement expectedResultPayloadElementCopy = null; foreach (var getNormalizerFunc in this.ExpectedResultNormalizers) { var normalizer = getNormalizerFunc(testConfiguration); if (normalizer != null) { if (expectedResultPayloadElementCopy == null) { expectedResultPayloadElementCopy = expectedResultPayloadElement.DeepCopy(); } expectedResultPayloadElementCopy = normalizer(expectedResultPayloadElementCopy); } } expectedResultPayloadElement = expectedResultPayloadElementCopy ?? expectedResultPayloadElement; } return expectedResultPayloadElement; }
private void RunMessageSizeLimitTests( IEnumerable<ReaderTestConfiguration> testConfigurations, EdmModel model, ODataPayloadElement payload, MessageSizeLimitTestCase[] testCases, Func<ReaderTestConfiguration, bool> skipTestConfigurationFunc = null) { var transformScope = this.Settings.PayloadTransformFactory.EmptyScope(); using (transformScope.Apply()) { this.CombinatorialEngineProvider.RunCombinations( testCases, testConfigurations, (testCase, testConfiguration) => { int size = -1; if (testConfiguration.Format == ODataFormat.Atom && testCase.AtomSizes != null) { size = testConfiguration.IsRequest ? testCase.AtomSizes.RequestSize : testCase.AtomSizes.ResponseSize; } else if (testConfiguration.Format == ODataFormat.Json && testCase.JsonLightSizes != null) { size = testConfiguration.IsRequest ? testCase.JsonLightSizes.RequestSize : testCase.JsonLightSizes.ResponseSize; } else if (testCase.RawSizes != null) { size = testConfiguration.IsRequest ? testCase.RawSizes.RequestSize : testCase.RawSizes.ResponseSize; } int maxSize = testCase.MaxMessageSize >= 0 ? testCase.MaxMessageSize : 1024 * 1024; ExpectedException expectedException = size < 0 ? null : ODataExpectedExceptions.ODataException("MessageStreamWrappingStream_ByteLimitExceeded", size.ToString(), maxSize.ToString()); var testDescriptor = new PayloadReaderTestDescriptor(this.Settings) { PayloadEdmModel = model, PayloadElement = payload.DeepCopy(), ExpectedException = expectedException, SkipTestConfiguration = skipTestConfigurationFunc, ApplyPayloadTransformations = false, }; testDescriptor.ExpectedResultNormalizers.Add( tc => (Func<ODataPayloadElement, ODataPayloadElement>)null); if (testCase.MaxMessageSize > 0) { testConfiguration = new ReaderTestConfiguration(testConfiguration); testConfiguration.MessageReaderSettings.MessageQuotas.MaxReceivedMessageSize = testCase.MaxMessageSize; } testDescriptor.RunTest(testConfiguration); }); } }
private static ODataPayloadElement EnsureAnnotationsBeforeProperties(ODataPayloadElement payload) { var complexPayload = payload.DeepCopy() as ComplexInstance; ExceptionUtilities.CheckObjectNotNull(complexPayload, "Payload is not ComplexInstance"); complexPayload.Properties = complexPayload.Properties .OrderBy(p => p.Name) .ThenBy( (p) => { var navProperty = p as NavigationPropertyInstance; if (navProperty != null) { if (navProperty.Value is DeferredLink || navProperty.AssociationLink != null) { return 0; } else { ExpandedLink expandedLink = navProperty.Value as ExpandedLink; if (expandedLink != null && expandedLink.ExpandedElement != null) { var expandedFeed = expandedLink.ExpandedElement as EntitySetInstance; if (expandedFeed == null || expandedFeed.Count > 0) { return 1; } } } } return 2; }).ToArray(); return complexPayload; }
/// <summary> /// Writes the payload to the stream using the given callback, then verifies the payload using the test deserializer /// </summary> /// <param name="originalPayload">The payload being tested, of which a copy will be made</param> /// <param name="message">The stream to write to</param> /// <param name="odataVersion">The OData protocol version to be used for writing payloads.</param> /// <param name="format">The current format</param> /// <param name="writeToStream">The callback to write to the stream</param> private void WriteAndLogODataPayload(ODataPayloadElement originalPayload, TestMessage message, ODataVersion odataVersion, ODataFormat format, Action<ODataPayloadElement> writeToStream) { ExceptionUtilities.CheckArgumentNotNull(originalPayload, "originalPayload"); ExceptionUtilities.CheckArgumentNotNull(writeToStream, "writeToStream"); // This is needed because we may modify the payload in use but the same is used in another iteration of the combinatorial engine var payload = originalPayload.DeepCopy(); WriteToStream(format, writeToStream, payload); var newPayload = TestWriterUtils.ReadToString(message); this.Logger.LogPayload(newPayload); }
/// <summary> /// Returns the payload to be used for this test case and the specified test configuration. /// </summary> /// <param name="testConfiguration">The test configuration to use.</param> /// <returns>The payload to use for testing.</returns> public static byte[] GetPayload( ReaderTestConfiguration testConfiguration, List <Func <ReaderTestConfiguration, Func <ODataPayloadElement, ODataPayloadElement> > > payloadNormalizers, PayloadReaderTestDescriptor.Settings settings, ODataPayloadElement payloadElement) { IPayloadSerializer payloadSerializer = null; // Apply all payload element transforms before serialization. IPayloadTransform <ODataPayloadElement> payloadElementTransform = settings.PayloadTransformFactory.GetTransform <ODataPayloadElement>(); ODataPayloadElement transformedODataElement = null; if (payloadElementTransform.TryTransform(payloadElement, out transformedODataElement)) { payloadElement = transformedODataElement; } ODataPayloadElement payloadElementToSerialize = payloadElement; // Apply all normalizers/fixups before serialization if (payloadNormalizers != null) { ODataPayloadElement payloadElementCopy = null; foreach (var getPayloadNormalizerFunc in payloadNormalizers) { var normalizer = getPayloadNormalizerFunc(testConfiguration); if (normalizer != null) { if (payloadElementCopy == null) { payloadElementCopy = payloadElementToSerialize.DeepCopy(); } payloadElementCopy = normalizer(payloadElementCopy); } } payloadElementToSerialize = payloadElementCopy ?? payloadElementToSerialize; } if (testConfiguration.Format == ODataFormat.Atom) { payloadSerializer = new XmlPayloadSerializer(settings.PayloadElementToXmlConverter); } else if (testConfiguration.Format == ODataFormat.Json) { // Create a copy of the payload element so that we can add annotations to it. payloadElementToSerialize = payloadElementToSerialize.DeepCopy(); // Annotate elements with version and response/request as appropriate PayloadFormatVersionAnnotatingVisitor.AnnotateJsonLight( payloadElementToSerialize, testConfiguration.Version.ToDataServiceProtocolVersion(), testConfiguration.IsRequest); payloadSerializer = new JsonPayloadSerializer(settings.PayloadElementToJsonLightConverter.ConvertToJsonLight); } else if (testConfiguration.Format == null) { if (payloadElementToSerialize.ElementType == ODataPayloadElementType.PrimitiveValue) { PrimitiveValue primitiveValue = (PrimitiveValue)payloadElementToSerialize; if (primitiveValue.ClrValue == null) { throw new NotSupportedException("Reading null values is not supported (since we don't support writing null values)."); } else if (primitiveValue.ClrValue.GetType() == typeof(byte[])) { payloadSerializer = settings.BinaryValuePayloadElementConverter; } else { payloadSerializer = settings.TextValuePayloadElementConverter; } } else if (payloadElementToSerialize.ElementType == ODataPayloadElementType.BatchRequestPayload || payloadElementToSerialize.ElementType == ODataPayloadElementType.BatchResponsePayload) { return(SerializeBatchPayload(payloadElementToSerialize, settings)); } else { throw new NotImplementedException("Default format not yet implemented for payload test descriptor and payload element type '" + payloadElementToSerialize.ElementType + "'."); } } else { throw new NotSupportedException("Unexpected format."); } // Default encoding is UTF8 return(payloadSerializer.SerializeToBinary(payloadElementToSerialize, null)); }