Пример #1
0
        /// <summary>
        /// Extension method for IPayloadTransformFactory which sets a composite transformed payload.
        /// </summary>
        /// <typeparam name="TPayload">Payload object type.</typeparam>
        /// <param name="factory">Payload transform factory.</param>
        /// <param name="payload">Original payload.</param>
        /// <param name="transformedPayload">Transformed payload.</param>
        /// <returns>True if the transform is applied else returns false.</returns>
        public static bool TryTransform <TPayload>(this IPayloadTransformFactory factory, TPayload payload, out TPayload transformedPayload)
        {
            ExceptionUtilities.CheckArgumentNotNull(factory, "factory");

            IPayloadTransform <TPayload> payloadElementTransform = factory.GetTransform <TPayload>();

            transformedPayload = default(TPayload);

            return(payloadElementTransform.TryTransform(payload, out transformedPayload));
        }
Пример #2
0
        /// <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));
        }