/// <summary> /// Creates a set of interesting collections test descriptors along with metadata. /// </summary> /// <param name="withTypeNames">true if the complex value payloads should specify type names.</param> /// <param name="fullSet">true if all available collections should be returned, false if only the most interesting subset should be returned.</param> /// <returns>List of interesting test descriptors.</returns> internal static IEnumerable <PayloadReaderTestDescriptor> CreateCollectionTestDescriptors( PayloadReaderTestDescriptor.Settings settings, bool withTypeNames, bool fullSet = true) { return(CreatePrimitiveCollectionTestDescriptors(settings, withTypeNames, fullSet) .Concat(CreateComplexCollectionTestDescriptors(settings, withTypeNames, fullSet))); }
/// <summary> /// Creates a set of interesting primitive value test descriptors along with metadata. /// </summary> /// <param name="settings">The settings for payload reader test descriptor to use.</param> /// <param name="fullSet">true if all available primitive values should be returned, false if only the most interesting subset should be returned.</param> /// <returns>List of interesting test descriptors.</returns> internal static IEnumerable <PayloadReaderTestDescriptor> CreatePrimitiveValueTestDescriptors( PayloadReaderTestDescriptor.Settings settings, bool fullSet = true) { EdmModel model = new EdmModel().Fixup(); IEnumerable <PrimitiveValue> primitiveValues = TestValues.CreatePrimitiveValuesWithMetadata(fullSet); return(primitiveValues.Select(c => new PayloadReaderTestDescriptor(settings) { PayloadElement = c, PayloadEdmModel = model })); }
/// <summary> /// Creates a set of interesting deferred navigation link test descriptors along with metadata. /// </summary> /// <param name="settings">The settings for payload reader test descriptor to use.</param> /// <param name="withMetadata">true if the generated test descriptors should have metadata.</param> /// <returns>Enumeration of interesting test descriptors.</returns> internal static IEnumerable <PayloadReaderTestDescriptor> CreateDeferredNavigationLinkTestDescriptors( PayloadReaderTestDescriptor.Settings settings, bool withMetadata) { EdmModel model = withMetadata ? new EdmModel().Fixup() : null; return(TestValues.CreateDeferredNavigationLinks().Select(navigationLink => new PayloadReaderTestDescriptor(settings) { PayloadElement = navigationLink, PayloadEdmModel = model })); }
/// <summary> /// Creates a set of interesting complex value test descriptors along with metadata. /// </summary> /// <param name="settings">The settings for payload reader test descriptor to use.</param> /// <param name="withTypeNames">true if the complex value payloads should specify type names.</param> /// <param name="withMetadata">true if the generated test descriptors should have metadata.</param> /// <param name="fullSet">true if all available complex values should be returned, false if only the most interesting subset should be returned.</param> /// <returns>List of interesting test descriptors.</returns> internal static IEnumerable <PayloadReaderTestDescriptor> CreateComplexValueTestDescriptors( PayloadReaderTestDescriptor.Settings settings, bool withTypeNames, bool fullSet = true) { EdmModel model = new EdmModel().Fixup(); IEnumerable <ComplexInstance> complexValues = TestValues.CreateComplexValues(model, withTypeNames, fullSet); return(complexValues.Select(c => new PayloadReaderTestDescriptor(settings) { PayloadElement = c, PayloadEdmModel = model })); }
/// <summary> /// Creates a set of interesting collections with complex items test descriptors along with metadata. /// </summary> /// <param name="withTypeNames">true if the complex value payloads should specify type names.</param> /// <param name="fullSet">true if all available complex collections should be returned, false if only the most interesting subset should be returned.</param> /// <returns>List of interesting test descriptors.</returns> internal static IEnumerable <PayloadReaderTestDescriptor> CreateComplexCollectionTestDescriptors( PayloadReaderTestDescriptor.Settings settings, bool withTypeNames, bool fullSet = true) { EdmModel model = new EdmModel().Fixup(); IEnumerable <ComplexMultiValue> complexCollections = TestValues.CreateComplexCollections(model, withTypeNames, fullSet); return(complexCollections.Select(collection => new PayloadReaderTestDescriptor(settings) { PayloadElement = collection, PayloadEdmModel = model, SkipTestConfiguration = tc => tc.Version < ODataVersion.V4, })); }
/// <summary> /// Creates a set of interesting entity set instances along with metadata. /// </summary> /// <param name="settings">The test descriptor settings to use.</param> /// <param name="model">If non-null, the method creates types as needed and adds them to the model.</param> /// <param name="withTypeNames">true if the payloads should specify type names.</param> /// <returns>List of test descriptors with interesting entity instances as payload.</returns> internal static IEnumerable <PayloadReaderTestDescriptor> CreateEntitySetInstanceDescriptors( PayloadReaderTestDescriptor.Settings settings, EdmModel model, bool withTypeNames) { List <PayloadReaderTestDescriptor> testDescriptors = new List <PayloadReaderTestDescriptor>(); foreach (var payloadDescriptor in TestFeeds.CreateEntitySetTestDescriptors(model, withTypeNames)) { testDescriptors.Add(new PayloadReaderTestDescriptor(settings) { PayloadDescriptor = payloadDescriptor, PayloadEdmModel = payloadDescriptor.PayloadEdmModel, SkipTestConfiguration = tc => payloadDescriptor.SkipTestConfiguration == null ? false : payloadDescriptor.SkipTestConfiguration(tc) }); } return(testDescriptors); }
internal static IEnumerable <PayloadReaderTestDescriptor> CreateEntityInstanceDescriptors( PayloadReaderTestDescriptor.Settings settings, EdmModel model, bool withTypeNames) { List <PayloadReaderTestDescriptor> testDescriptors = new List <PayloadReaderTestDescriptor>(); foreach (var payloadDescriptor in TestEntityInstances.CreateEntityInstanceTestDescriptors(model, withTypeNames)) { // NOTE so far we don't have version-specific entity instances so we don't have to specify a // minimum version or skip function; once we do we'll have to change that PayloadReaderTestDescriptor testDescriptor = new PayloadReaderTestDescriptor(settings) { PayloadDescriptor = payloadDescriptor }; testDescriptors.Add(testDescriptor); } return(testDescriptors); }
/// <summary> /// Creates a set of interesting homogeneous collection value test descriptors along with metadata. /// </summary> /// <param name="settings">The settings for the payload reader test descriptor to use.</param> /// <param name="withMetadata">true if the generated test descriptors should have metadata.</param> /// <param name="withTypeNames">true if the collection value payloads should specify type names.</param> /// <param name="withExpectedType">true if an expected type annotation should be added to the generated payload element; otherwise false.</param> /// <param name="withcollectionName">true if the collection is not in the top level, otherwise false</param> /// <param name="fullSet">true if all available collection values should be returned, false if only the most interesting subset should be returned.</param> /// <returns>List of interesting test descriptors.</returns> internal static IEnumerable <PayloadReaderTestDescriptor> CreateHomogeneousCollectionValueTestDescriptors( PayloadReaderTestDescriptor.Settings settings, bool withMetadata, bool withTypeNames, bool withExpectedType, bool withcollectionName, bool fullSet = true) { EdmModel model = withMetadata ? new EdmModel() : null; IEnumerable <ODataPayloadElementCollection> collectionValues = TestValues.CreateHomogeneousCollectionValues(model, withTypeNames, withExpectedType, withcollectionName, fullSet); return(collectionValues.Select(collectionValue => { PayloadReaderTestDescriptor testDescriptor = new PayloadReaderTestDescriptor(settings) { PayloadElement = collectionValue, PayloadEdmModel = model }; if (withExpectedType && !withTypeNames) { // fill in the type names for the expected result payload since they will be added based on the expected type testDescriptor.ExpectedResultNormalizers.Add(tc => FillTypeNamesFromTypeAnnotationsPayloadElementVisitor.Visit); } if (!withExpectedType) { testDescriptor.ExpectedResultNormalizers.Add( tc => (Func <ODataPayloadElement, ODataPayloadElement>)null); } // Do not run the test descriptor if we do not have an expected type in JSON Light because it would fail. testDescriptor.SkipTestConfiguration = tc => tc.Format == ODataFormat.Json && !withExpectedType; return testDescriptor; })); }
/// <summary> /// Creates a set of interesting expanded navigation link test descriptors along with metadata. /// </summary> /// <param name="settings">The settings for payload reader test descriptor to use.</param> /// <param name="withMetadata">true if the generated test descriptors should have metadata.</param> /// <returns>Enumeration of interesting test descriptors.</returns> internal static IEnumerable <PayloadReaderTestDescriptor> CreateExpandedNavigationLinkTestDescriptors( PayloadReaderTestDescriptor.Settings settings, bool withMetadata) { return((IEnumerable <PayloadReaderTestDescriptor>)CreateDeferredNavigationLinkTestDescriptors(settings, withMetadata).SelectMany(td => new[] { new PayloadReaderTestDescriptor(settings) { PayloadDescriptor = td.PayloadDescriptor.ExpandNavigationProperty(true) }, new PayloadReaderTestDescriptor(settings) { PayloadDescriptor = td.PayloadDescriptor.ExpandNavigationProperty(false) }, new PayloadReaderTestDescriptor(settings) { PayloadDescriptor = td.PayloadDescriptor.ExpandNavigationProperty(false, 5) }, new PayloadReaderTestDescriptor(settings) { PayloadDescriptor = td.PayloadDescriptor.ExpandNavigationProperty(false, 5, "http://odata.org/expanded_navigation_link_next_feed_link") } })); }
/// <summary> /// Creates a set of interesting service document instances. /// </summary> /// <param name="settings">The test descriptor settings to use.</param> /// <param name="withTitles">true if workspaces and collections should have a title; otherwise false.</param> /// <returns>List of test descriptors with interesting service documents as payload.</returns> internal static IEnumerable <PayloadReaderTestDescriptor> CreateServiceDocumentDescriptors(PayloadReaderTestDescriptor.Settings settings, string baseUri, bool withTitles) { return(TestServiceDocuments.CreateServiceDocuments(withTitles, baseUri).Select(sd => { PayloadReaderTestDescriptor testDescriptor = new PayloadReaderTestDescriptor(settings) { PayloadElement = sd, SkipTestConfiguration = tc => tc.IsRequest // service docs are only allowed in responses }; return testDescriptor; })); }
/// <summary> /// Creates a set of interesting stream reference (named stream) test descriptors, optionally with metadata. /// </summary> /// <param name="settings">The settings for payload reader test descriptor to use.</param> /// <param name="withMetadata">true if the generated test descriptors should have metadata.</param> /// <returns>Enumeration of interesting stream reference test descriptors.</returns> internal static IEnumerable <PayloadReaderTestDescriptor> CreateStreamReferenceValueTestDescriptors(PayloadReaderTestDescriptor.Settings settings, bool withMetadata) { EdmModel model = withMetadata ? new EdmModel().Fixup() : null; IEnumerable <NamedStreamInstance> namedStreams = TestValues.CreateStreamReferenceValues(); return(namedStreams.Select(c => new PayloadReaderTestDescriptor(settings) { PayloadElement = c, PayloadEdmModel = model })); }
/// <summary> /// Returns the serialized batch payload /// </summary> /// <param name="batchPayload">batch payload</param> /// <returns>bytes representing batch payload</returns> private static byte[] SerializeBatchPayload(ODataPayloadElement batchPayload, PayloadReaderTestDescriptor.Settings settings) { string boundary = null; Byte[] bytes = null; Func <ODataPayloadElement, string> getBoundaryAnnotation = (batchPayloadElement) => { var boundaryAnn = batchPayloadElement.Annotations.OfType <BatchBoundaryAnnotation>().Single(); ExceptionUtilities.CheckObjectNotNull(boundaryAnn, "bounday annotation cannot be null"); return(boundaryAnn.BatchBoundaryInPayload); }; var batchRequestPayload = batchPayload as BatchRequestPayload; if (batchRequestPayload != null) { boundary = getBoundaryAnnotation(batchRequestPayload); bytes = settings.BatchSerializer.SerializeBatchPayload(batchRequestPayload, boundary, Encoding.UTF8.WebName); // encoding assumed to be UTF8 } else { var batchResponsePayload = batchPayload as BatchResponsePayload; boundary = getBoundaryAnnotation(batchResponsePayload); ExceptionUtilities.CheckObjectNotNull(batchResponsePayload, "the specified batch payload is neither a request payload nor a response"); bytes = settings.BatchSerializer.SerializeBatchPayload(batchResponsePayload, boundary, Encoding.UTF8.WebName); // encoding assumed to be UTF8 } return(bytes); }
/// <summary> /// Creates a set of interesting entity reference links (collection) instances. /// </summary> /// <param name="settings">The test descriptor settings to use.</param> /// <returns>List of test descriptors with interesting entity reference links instances as payload.</returns> internal static IEnumerable <PayloadReaderTestDescriptor> CreateEntityReferenceLinksDescriptors(PayloadReaderTestDescriptor.Settings settings) { return(CreateEntityReferenceLinksValues().Select(erl => new PayloadReaderTestDescriptor(settings) { PayloadElement = erl, SkipTestConfiguration = c => (c.IsRequest) && (erl.NextLink != null || erl.InlineCount.HasValue), ExpectedResultCallback = tc => new PayloadReaderTestExpectedResult(settings.ExpectedResultSettings) })); }
/// <summary> /// Creates a set of interesting entity reference link instances. /// </summary> /// <param name="settings">The test descriptor settings to use.</param> /// <returns>List of test descriptors with interesting entity reference link instances as payload.</returns> internal static IEnumerable <PayloadReaderTestDescriptor> CreateEntityReferenceLinkDescriptors(PayloadReaderTestDescriptor.Settings settings) { return(CreateEntityReferenceLinkValues().Select(erl => new PayloadReaderTestDescriptor(settings) { PayloadElement = erl, })); }
/// <summary> /// Creates the input message for the test descriptor /// </summary> /// <param name="testConfiguration">the test configuration to use</param> /// <param name="readerTestDescriptor">The test descriptor</param> /// <param name="settings">The test descriptor settings</param> /// <param name="applyPayloadTransformations">Whether or not to apply payload transformations</param> /// <returns>The message for the test</returns> public static TestMessage CreateInputMessage(ReaderTestConfiguration testConfiguration, PayloadReaderTestDescriptor readerTestDescriptor, PayloadReaderTestDescriptor.Settings settings, bool?applyPayloadTransformations) { TestMessage testMessage; bool originalApplyTransformValue = false; var odataTransformFactory = settings.PayloadTransformFactory as ODataLibPayloadTransformFactory; try { if (applyPayloadTransformations.HasValue && odataTransformFactory != null) { originalApplyTransformValue = odataTransformFactory.ApplyTransform; odataTransformFactory.ApplyTransform = applyPayloadTransformations.Value; } if (readerTestDescriptor.TestDescriptorNormalizers != null) { foreach (var testDescriptorNormalizer in readerTestDescriptor.TestDescriptorNormalizers) { var normalizerAction = testDescriptorNormalizer(testConfiguration); if (normalizerAction != null) { normalizerAction(readerTestDescriptor); } } } MemoryStream memoryStream = new MemoryStream(GetPayload(testConfiguration, readerTestDescriptor.PayloadNormalizers, settings, readerTestDescriptor.PayloadElement)); TestStream messageStream = new TestStream(memoryStream); if (testConfiguration.Synchronous) { messageStream.FailAsynchronousCalls = true; } else { messageStream.FailSynchronousCalls = true; } testMessage = TestReaderUtils.CreateInputMessageFromStream( messageStream, testConfiguration, readerTestDescriptor.PayloadElement.GetPayloadKindFromPayloadElement(), readerTestDescriptor.PayloadElement.GetCustomContentTypeHeader(), readerTestDescriptor.UrlResolver); if (readerTestDescriptor.TestMessageWrapper != null) { testMessage = readerTestDescriptor.TestMessageWrapper(testMessage); } return(testMessage); } finally { if (applyPayloadTransformations.HasValue && odataTransformFactory != null) { odataTransformFactory.ApplyTransform = originalApplyTransformValue; } } }
/// <summary> /// Creates a set of test descriptors with interesting error payloads. /// </summary> /// <param name="settings">The test descriptor settings to use.</param> /// <returns>List of test descriptors with interesting errors as payload.</returns> internal static IEnumerable <PayloadReaderTestDescriptor> CreateErrorReaderTestDescriptors(PayloadReaderTestDescriptor.Settings settings) { return(TestErrors.CreateErrorTestDescriptors().Select(err => new PayloadReaderTestDescriptor(settings) { PayloadElement = err.PayloadElement, SkipTestConfiguration = tc => tc.IsRequest })); }
/// <summary> /// Creates error reader test descriptors with deeply nested internal exceptions. /// </summary> /// <param name="settings">The test descriptor settings to use.</param> /// <param name="depthLimit">The maximum depth limit for nested errors. Payloads with error depth over this limit should fail.</param> /// <returns>An enumerable of <see cref="PayloadReaderTestDescriptor"/> representing the deeply nested error payloads.</returns> internal static IEnumerable <PayloadReaderTestDescriptor> CreateErrorDeeplyNestedReaderTestDescriptors(PayloadReaderTestDescriptor.Settings settings, int depthLimit) { ODataInternalExceptionPayload deeplyNestedInnerError = PayloadBuilder.InnerError(); // Create 'depthLimit' levels of depth (loop 'depthLimit - 1' times since we've already constructed one inner error). for (int index = 0; index < depthLimit - 1; index++) { deeplyNestedInnerError.InnerError(deeplyNestedInnerError.DeepCopy()); } // Add one more level of depth to create an invalid payload. ODataInternalExceptionPayload tooDeeplyNestedInnerError = PayloadBuilder.InnerError().InnerError(deeplyNestedInnerError.DeepCopy()); yield return(new PayloadReaderTestDescriptor(settings) { PayloadElement = PayloadBuilder.Error().InnerError(deeplyNestedInnerError), SkipTestConfiguration = tc => tc.IsRequest }); yield return(new PayloadReaderTestDescriptor(settings) { PayloadElement = PayloadBuilder.Error().InnerError(tooDeeplyNestedInnerError), ExpectedException = ODataExpectedExceptions.ODataException("ValidationUtils_RecursionDepthLimitReached", Convert.ToString(depthLimit)), SkipTestConfiguration = tc => tc.IsRequest }); }
/// <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)); }