Ejemplo n.º 1
0
        public void InStreamJsonLightErrorTest()
        {
            IEnumerable <PayloadReaderTestDescriptor> testDescriptors = PayloadReaderTestDescriptorGenerator.CreateErrorReaderTestDescriptors(this.Settings);

            // convert the payload element to a JSON representation usable for in-stream error testing
            testDescriptors = testDescriptors.Select(td =>
            {
                AnnotatedPayloadElementToJsonLightConverter payloadElementToJsonConverter = new AnnotatedPayloadElementToJsonLightConverter();
                JsonObject jsonObject = (JsonObject)payloadElementToJsonConverter.ConvertToJsonLightValue(td.PayloadElement);
                Debug.Assert(td.ExpectedException == null, "Don't expect errors for regular payloads (without annotation).");

                return(new PayloadReaderTestDescriptor(td)
                {
                    PayloadElement = td.PayloadElement.JsonRepresentation(jsonObject)
                });
            });

            testDescriptors = testDescriptors.Select(td => td.ToInStreamErrorTestDescriptor(ODataFormat.Json));

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                this.ReaderTestConfigurationProvider.JsonLightFormatConfigurations.Where(tc => !tc.IsRequest),
                (testDescriptor, testConfiguration) =>
            {
                // These descriptors are already tailored specifically for Json Light and
                // do not require normalization.
                testDescriptor.TestDescriptorNormalizers.Clear();
                testDescriptor.RunTest(testConfiguration);
            });
        }
        public void DeferredLinkTest()
        {
            IEdmModel model    = Test.OData.Utils.Metadata.TestModels.BuildTestModel();
            var       cityType = model.FindType("TestModel.CityType");

            Debug.Assert(cityType != null, "cityType != null");

            // TODO: add test cases that use relative URIs

            // Few hand-crafted payloads
            IEnumerable <PayloadReaderTestDescriptor> testDescriptors = new PayloadReaderTestDescriptor[]
            {
                // Single deferred link
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadDescriptor = new PayloadTestDescriptor(),
                    PayloadElement    = PayloadBuilder.Entity("TestModel.CityType").PrimitiveProperty("Id", 1).WithTypeAnnotation(cityType)
                                        .NavigationProperty("CityHall", "http://odata.org/CityHall"),
                    PayloadEdmModel = model
                },

                // Multiple deferred links
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadDescriptor = new PayloadTestDescriptor(),
                    PayloadElement    = PayloadBuilder.Entity("TestModel.CityType").PrimitiveProperty("Id", 1).WithTypeAnnotation(cityType)
                                        .NavigationProperty("CityHall", "http://odata.org/CityHall")
                                        .NavigationProperty("DOL", "http://odata.org/DOL"),
                    PayloadEdmModel = model
                },

                // Multiple deferred links with primitive properties in between
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadDescriptor = new PayloadTestDescriptor(),
                    PayloadElement    = PayloadBuilder.Entity("TestModel.CityType").WithTypeAnnotation(cityType)
                                        .Property("Id", PayloadBuilder.PrimitiveValue(1))
                                        .NavigationProperty("CityHall", "http://odata.org/CityHall")
                                        .Property("Name", PayloadBuilder.PrimitiveValue("Vienna"))
                                        .NavigationProperty("DOL", "http://odata.org/DOL"),
                    PayloadEdmModel = model
                },
            };

            // Add standard deferred link payloads
            testDescriptors = testDescriptors.Concat(PayloadReaderTestDescriptorGenerator.CreateDeferredNavigationLinkTestDescriptors(this.Settings, true));

            // Generate interesting payloads around the entry
            testDescriptors = testDescriptors.SelectMany(td => this.PayloadGenerator.GenerateReaderPayloads(td));

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                // Deferred links are response only.
                // TODO: Reenable Json Light support
                this.ReaderTestConfigurationProvider.ExplicitFormatConfigurations.Where(tc => !tc.IsRequest && tc.Format == ODataFormat.Atom),
                (testDescriptor, testConfiguration) =>
            {
                testDescriptor.RunTest(testConfiguration);
            });
        }
Ejemplo n.º 3
0
        public void TopLevelErrorReaderTest()
        {
            IEdmModel model = Microsoft.Test.OData.Utils.Metadata.TestModels.BuildTestModel();

            // Use some standard complex value payloads first
            IEnumerable <PayloadReaderTestDescriptor> testDescriptors = PayloadReaderTestDescriptorGenerator.CreateErrorReaderTestDescriptors(this.Settings);

            // make sure reading errors works with and without model
            testDescriptors = testDescriptors.Concat(testDescriptors.Select(td => new PayloadReaderTestDescriptor(td)
            {
                PayloadEdmModel = model
            }));

            // add the normalizer for ATOM error payloads
            testDescriptors = testDescriptors.Select(td =>
            {
                td.ExpectedResultNormalizers.Add(
                    tc => (Func <ODataPayloadElement, ODataPayloadElement>)null);
                return(td);
            });

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                this.ReaderTestConfigurationProvider.ExplicitFormatConfigurations,
                (testDescriptor, testConfiguration) =>
            {
                testDescriptor.RunTest(testConfiguration);
            });
        }
Ejemplo n.º 4
0
        public void InStreamAtomDeeplyRecursiveErrorTest()
        {
            int depthLimit = 5;

            IEnumerable <PayloadReaderTestDescriptor> testDescriptors = PayloadReaderTestDescriptorGenerator.CreateErrorDeeplyNestedReaderTestDescriptors(this.Settings, depthLimit);

            // Convert the payload element to an Xml representation usable for in-stream error testing
            testDescriptors = testDescriptors.Select(td =>
            {
                XElement xmlPayload = this.PayloadElementToXmlConverter.ConvertToXml(td.PayloadElement);

                return(new PayloadReaderTestDescriptor(td)
                {
                    PayloadElement = td.PayloadElement.XmlRepresentation(xmlPayload)
                });
            });

            // Convert top-level error test descriptors to in-stream errors.
            testDescriptors = testDescriptors.Select(td => td.ToInStreamErrorTestDescriptor(ODataFormat.Atom));

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                this.ReaderTestConfigurationProvider.AtomFormatConfigurations.Where(tc => !tc.IsRequest),
                (testDescriptor, testConfiguration) =>
            {
                // Copy the test configuration so we can modify the depth limit.
                testConfiguration = new ReaderTestConfiguration(testConfiguration);
                testConfiguration.MessageReaderSettings.MessageQuotas.MaxNestingDepth = depthLimit;

                testDescriptor.RunTest(testConfiguration);
            });
        }
Ejemplo n.º 5
0
        public void InStreamAtomErrorTest()
        {
            IEnumerable <PayloadReaderTestDescriptor> testDescriptors = PayloadReaderTestDescriptorGenerator.CreateErrorReaderTestDescriptors(this.Settings);

            // convert the payload element to an Xml representation usable for in-stream error testing
            testDescriptors = testDescriptors.Select(td =>
            {
                XElement xmlPayload = this.PayloadElementToXmlConverter.ConvertToXml(td.PayloadElement);
                Debug.Assert(td.ExpectedException == null, "Don't expect errors for regular payloads (without annotation).");

                return(new PayloadReaderTestDescriptor(td)
                {
                    PayloadElement = td.PayloadElement.XmlRepresentation(xmlPayload)
                });
            });

            testDescriptors = testDescriptors.Select(td => td.ToInStreamErrorTestDescriptor(ODataFormat.Atom));

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                this.ReaderTestConfigurationProvider.AtomFormatConfigurations.Where(tc => !tc.IsRequest),
                (testDescriptor, testConfiguration) =>
            {
                testDescriptor.RunTest(testConfiguration);
            });
        }
Ejemplo n.º 6
0
        private IEnumerable <PayloadReaderTestDescriptor> CreateCollectionPayloadsWithMetadata(bool withTypeNames)
        {
            // Start with the standard set of collections
            IEnumerable <PayloadReaderTestDescriptor> testDescriptors = PayloadReaderTestDescriptorGenerator.CreateCollectionTestDescriptors(this.Settings, withTypeNames);

            // Add collections with all of the primitive values (except null)
            testDescriptors = testDescriptors.Concat(PayloadReaderTestDescriptorGenerator.CreatePrimitiveValueTestDescriptors(this.Settings, false)
                                                     .Where(primitivePayload => ((PrimitiveValue)primitivePayload.PayloadElement).ClrValue != null)
                                                     .Select(primitivePayload =>
            {
                IEdmTypeReference edmType = primitivePayload.PayloadElement.GetAnnotation <EntityModelTypeAnnotation>().EdmModelType;
                var primitiveCollection   = PayloadBuilder.PrimitiveMultiValue(withTypeNames ? EntityModelUtils.GetCollectionTypeName(edmType.FullName()) : null)
                                            .Item(((PrimitiveValue)primitivePayload.PayloadElement).ClrValue)
                                            .WithTypeAnnotation(EdmCoreModel.GetCollection(edmType));
                if (!withTypeNames)
                {
                    primitiveCollection.AddAnnotation(new SerializationTypeNameTestAnnotation()
                    {
                        TypeName = null
                    });
                }
                return(new PayloadReaderTestDescriptor(primitivePayload)
                {
                    PayloadElement = primitiveCollection,
                });
            }));

            // Add collections with all of the complex values (except null)
            testDescriptors = testDescriptors.Concat(PayloadReaderTestDescriptorGenerator.CreateComplexValueTestDescriptors(this.Settings, withTypeNames, false)
                                                     .Where(complexPayload => !((ComplexInstance)complexPayload.PayloadElement).IsNull)
                                                     .Select(complexPayload =>
            {
                IEdmTypeReference edmComplexType = complexPayload.PayloadElement.GetAnnotation <EntityModelTypeAnnotation>().EdmModelType;
                var complexCollection            = PayloadBuilder.ComplexMultiValue(withTypeNames ? EntityModelUtils.GetCollectionTypeName(edmComplexType.FullName()) : null)
                                                   .Item((ComplexInstance)complexPayload.PayloadElement)
                                                   .WithTypeAnnotation(EdmCoreModel.GetCollection(edmComplexType));
                if (!withTypeNames)
                {
                    complexCollection.AddAnnotation(new SerializationTypeNameTestAnnotation()
                    {
                        TypeName = null
                    });
                }
                return(new PayloadReaderTestDescriptor(complexPayload)
                {
                    PayloadElement = complexCollection,
                });
            }));

            return(testDescriptors);
        }
Ejemplo n.º 7
0
        public void FeedReadingTest()
        {
            EdmModel model = Test.OData.Utils.Metadata.TestModels.BuildTestModel() as EdmModel;

            IEnumerable <PayloadReaderTestDescriptor> testDescriptors = PayloadReaderTestDescriptorGenerator.CreateEntitySetInstanceDescriptors(this.Settings, model, true);

            IEnumerable <PayloadReaderTestDescriptor> payloadDescriptors = testDescriptors
                                                                           .SelectMany(feed => this.PayloadGenerator.GenerateReaderPayloads(feed));

            this.CombinatorialEngineProvider.RunCombinations(
                payloadDescriptors,
                this.ReaderTestConfigurationProvider.ExplicitFormatConfigurations,
                (testDescriptor, testConfig) =>
            {
                testDescriptor.RunTest(testConfig);
            });
        }
Ejemplo n.º 8
0
        public void HomogeneousCollectionReaderTest()
        {
            IEnumerable <PayloadReaderTestDescriptor> testDescriptors =
                PayloadReaderTestDescriptorGenerator.CreateHomogeneousCollectionValueTestDescriptors(this.Settings, withMetadata: true, withTypeNames: true, withExpectedType: true, withcollectionName: false, fullSet: true)
                .Concat(PayloadReaderTestDescriptorGenerator.CreateHomogeneousCollectionValueTestDescriptors(this.Settings, withMetadata: true, withTypeNames: false, withExpectedType: true, withcollectionName: false, fullSet: true))
                .Concat(PayloadReaderTestDescriptorGenerator.CreateHomogeneousCollectionValueTestDescriptors(this.Settings, withMetadata: true, withTypeNames: true, withExpectedType: false, withcollectionName: false, fullSet: true));

            // Generate interesting payloads around the collection
            testDescriptors = testDescriptors.SelectMany(td => this.PayloadGenerator.GenerateReaderPayloads(td));
            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                this.ReaderTestConfigurationProvider.ExplicitFormatConfigurations,
                (testDescriptor, testConfiguration) =>
            {
                testDescriptor.RunTest(testConfiguration);
            });
        }
Ejemplo n.º 9
0
        public void TopLevelDeeplyRecursiveErrorTest()
        {
            int depthLimit = 5;

            IEnumerable <PayloadReaderTestDescriptor> testDescriptors = PayloadReaderTestDescriptorGenerator.CreateErrorDeeplyNestedReaderTestDescriptors(this.Settings, depthLimit);

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                this.ReaderTestConfigurationProvider.ExplicitFormatConfigurations.Where(tc => !tc.IsRequest),
                (testDescriptor, testConfiguration) =>
            {
                // Copy the test configuration so we can modify the depth limit.
                testConfiguration = new ReaderTestConfiguration(testConfiguration);
                testConfiguration.MessageReaderSettings.MessageQuotas.MaxNestingDepth = depthLimit;

                testDescriptor.RunTest(testConfiguration);
            });
        }
Ejemplo n.º 10
0
        public void TopLevelPropertiesWithMetadataTest()
        {
            IEnumerable <PayloadReaderTestDescriptor> testDescriptors = PayloadReaderTestDescriptorGenerator.CreatePrimitiveValueTestDescriptors(this.Settings);

            testDescriptors = testDescriptors.Concat(PayloadReaderTestDescriptorGenerator.CreateComplexValueTestDescriptors(this.Settings, true));
            testDescriptors = testDescriptors.Concat(PayloadReaderTestDescriptorGenerator.CreateCollectionTestDescriptors(this.Settings, true));

            testDescriptors = testDescriptors.Select(collectionTestDescriptor => collectionTestDescriptor.InProperty("propertyName"));
            testDescriptors = testDescriptors.SelectMany(td => this.PayloadGenerator.GenerateReaderPayloads(td));

            // Limit to only top-level property payloads
            testDescriptors = testDescriptors.Where(td => td.PayloadElement is PropertyInstance);

            // Add a couple of invalid cases which use a standard model
            EdmModel model = new EdmModel();

            model.ComplexType("UnusedComplexType");

            EdmEntityType unusedEntityType = model.EntityType("UnusedEntityType");

            unusedEntityType.AddKeys(unusedEntityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32, isNullable: false));
            unusedEntityType.Property("Name", EdmPrimitiveTypeKind.String, isNullable: false);

            EdmEntityType streamPropertyEntityType = model.EntityType("EntityTypeWithStreamProperty");

            streamPropertyEntityType.AddKeys(streamPropertyEntityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32, isNullable: false));
            streamPropertyEntityType.AddStructuralProperty("Video", EdmPrimitiveTypeKind.Stream, isNullable: false);
            streamPropertyEntityType.Property("NonStreamProperty", EdmPrimitiveTypeKind.Boolean, isNullable: false);

            EdmEntityType navigationPropertyEntityType = model.EntityType("EntityTypeWithNavigationProperty");

            navigationPropertyEntityType.AddKeys(navigationPropertyEntityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32, isNullable: false));
            navigationPropertyEntityType.NavigationProperty("Navigation", streamPropertyEntityType);

            model.Fixup();

            EdmEntityContainer container = model.EntityContainer as EdmEntityContainer;

            EdmFunction nameFunction = new EdmFunction(container.Namespace, "NameFunctionImport", EdmCoreModel.Instance.GetInt32(false), false /*isBound*/, null, false /*isComposable*/);

            model.AddElement(nameFunction);
            container.AddFunctionImport("NameFunctionImport", nameFunction);
            model.Fixup();

            var videoPropertyType = model.GetEntityType("TestModel.EntityTypeWithStreamProperty").Properties().Single(p => p.Name == "Video").Type;

            var explicitTestDescriptors = new[]
            {
                // Non existant type name
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement    = PayloadBuilder.Property("propertyName", PayloadBuilder.ComplexValue("TestModel.NonExistantType")),
                    PayloadEdmModel   = model,
                    ExpectedException = ODataExpectedExceptions.ODataException("ValidationUtils_UnrecognizedTypeName", "TestModel.NonExistantType"),
                    // This test has different meaning in JSON-L (no expected type + non-existent typename)
                    SkipTestConfiguration = (tc) => tc.Format == ODataFormat.Json,
                },
                // Existing type name without namespace
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement    = PayloadBuilder.Property("propertyName", PayloadBuilder.ComplexValue("UnusedComplexType")),
                    PayloadEdmModel   = model,
                    ExpectedException = ODataExpectedExceptions.ODataException("ValidationUtils_UnrecognizedTypeName", "UnusedComplexType"),
                    // This test has different meaning in JSON-L (no expected type + non-existent typename)
                    SkipTestConfiguration = (tc) => tc.Format == ODataFormat.Json,
                },
                // Existing type of wrong kind
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement    = PayloadBuilder.Property("propertyName", PayloadBuilder.ComplexValue("TestModel.UnusedEntityType")),
                    PayloadEdmModel   = model,
                    ExpectedException = ODataExpectedExceptions.ODataException("ValidationUtils_IncorrectValueTypeKind", "TestModel.UnusedEntityType", "Entity"),
                    // This test has different meaning in JSON-L
                    SkipTestConfiguration = (tc) => tc.Format == ODataFormat.Json,
                },
                // A stream is not allowed in a property with a non-stream property kind.
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement         = PayloadBuilder.Entity("TestModel.EntityTypeWithStreamProperty").StreamProperty("NonStreamProperty", "http://readlink", "http://editlink"),
                    PayloadEdmModel        = model,
                    ExpectedResultCallback = tc =>
                                             new PayloadReaderTestExpectedResult(this.Settings.ExpectedResultSettings)
                    {
                        ExpectedException = tc.Format == ODataFormat.Json
                                        ? ODataExpectedExceptions.ODataException("ODataJsonLightResourceDeserializer_PropertyWithoutValueWithWrongType", "NonStreamProperty", "Edm.Boolean")
                                        : ODataExpectedExceptions.ODataException("JsonReaderExtensions_UnexpectedNodeDetected", "PrimitiveValue", "StartObject")
                    },
                },
                // Top-level property of stream type is not allowed
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement    = PayloadBuilder.PrimitiveProperty("Video", 42).ExpectedPropertyType(videoPropertyType),
                    PayloadEdmModel   = model,
                    ExpectedException = ODataExpectedExceptions.ArgumentException("ODataMessageReader_ExpectedPropertyTypeStream"),
                },
                // Top-level deferred navigation property is not allowed
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement    = PayloadBuilder.PrimitiveProperty("Video", 42).ExpectedPropertyType(streamPropertyEntityType),
                    PayloadEdmModel   = model,
                    ExpectedException = ODataExpectedExceptions.ArgumentException("ODataMessageReader_ExpectedPropertyTypeEntityKind"),
                },
                // Top-level expanded navigation property is not allowed
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement    = PayloadBuilder.PrimitiveProperty("Video", 42).ExpectedPropertyType(streamPropertyEntityType),
                    PayloadEdmModel   = model,
                    ExpectedException = ODataExpectedExceptions.ArgumentException("ODataMessageReader_ExpectedPropertyTypeEntityKind"),
                },
            };

            testDescriptors = testDescriptors.Concat(explicitTestDescriptors);

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                this.ReaderTestConfigurationProvider.ExplicitFormatConfigurations,
                (testDescriptor, testConfiguration) =>
            {
                var property = testDescriptor.PayloadElement as PropertyInstance;
                testDescriptor.RunTest(testConfiguration);
            });
        }
Ejemplo n.º 11
0
        public void ExpandedLinkTest()
        {
            IEdmModel model = Microsoft.Test.OData.Utils.Metadata.TestModels.BuildTestModel();

            // TODO: add test cases that use relative URIs

            IEnumerable <PayloadReaderTestDescriptor> testDescriptors = PayloadReaderTestDescriptorGenerator.CreateDeferredNavigationLinkTestDescriptors(this.Settings, true);

            // Generate interesting payloads around the navigation property
            // Note that this will actually expand the deferred nav links as well.
            testDescriptors = testDescriptors.SelectMany(td => this.PayloadGenerator.GenerateReaderPayloads(td));

            IEnumerable <PayloadReaderTestDescriptor> customTestDescriptors = new PayloadReaderTestDescriptor[]
            {
                // Expanded null entry
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadDescriptor = new PayloadTestDescriptor(),
                    PayloadEdmModel   = model,
                    PayloadElement    = PayloadBuilder.Entity("TestModel.CityType")
                                        .PrimitiveProperty("Id", 1)
                                        .ExpandedNavigationProperty("PoliceStation", PayloadBuilder.NullEntity()),
                },
                // Expanded null entry after another expanded collection link
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadDescriptor = new PayloadTestDescriptor(),
                    PayloadEdmModel   = model,
                    PayloadElement    = PayloadBuilder.Entity("TestModel.CityType")
                                        .PrimitiveProperty("Id", 1)
                                        .ExpandedNavigationProperty("CityHall", PayloadBuilder.EntitySet())
                                        .ExpandedNavigationProperty("PoliceStation", PayloadBuilder.NullEntity()),
                },
                // incorrect type at related end
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadDescriptor = new PayloadTestDescriptor(),
                    PayloadEdmModel   = model,
                    PayloadElement    = PayloadBuilder.Entity("TestModel.CityType")
                                        .PrimitiveProperty("Id", 1)
                                        .ExpandedNavigationProperty("PoliceStation", PayloadBuilder.Entity("TestModel.CityType")),
                    ExpectedException = ODataExpectedExceptions.ODataException("ValidationUtils_EntryTypeNotAssignableToExpectedType", "TestModel.CityType", "TestModel.OfficeType"),
                },
                // Nested entry of depth 4 should fail because we set MaxNestingDepth = 3 below
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadDescriptor = new PayloadTestDescriptor(),
                    PayloadEdmModel   = model,
                    PayloadElement    = PayloadBuilder.Entity("TestModel.Person").PrimitiveProperty("Id", 1),
                    ExpectedException = ODataExpectedExceptions.ODataException("ValidationUtils_MaxDepthOfNestedEntriesExceeded", "3"),
                }.InEntryWithExpandedLink(true /* isSingleton */)
                .InEntryWithExpandedLink(true)
                .InEntryWithExpandedLink(true),

                // Nested entry of depth 4 within expanded feeds should fail
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadDescriptor = new PayloadTestDescriptor(),
                    PayloadEdmModel   = model,
                    PayloadElement    = PayloadBuilder.Entity("TestModel.Person").PrimitiveProperty("Id", 1),
                    ExpectedException = ODataExpectedExceptions.ODataException("ValidationUtils_MaxDepthOfNestedEntriesExceeded", "3"),
                }.InFeed()
                .InEntryWithExpandedLink(false /* isSingleton */)
                .InFeed()
                .InEntryWithExpandedLink(false)
                .InFeed()
                .InEntryWithExpandedLink(false),

                // Nested entry of depth 3 should succeed
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadDescriptor = new PayloadTestDescriptor(),
                    PayloadEdmModel   = model,
                    PayloadElement    = PayloadBuilder.Entity("TestModel.Person").PrimitiveProperty("Id", 1)
                }.InEntryWithExpandedLink(true /* isSingleton */)
                .InEntryWithExpandedLink(true),

                // Nested entry of depth 3 within expanded feeds should succeed
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadDescriptor = new PayloadTestDescriptor(),
                    PayloadEdmModel   = model,
                    PayloadElement    = PayloadBuilder.Entity("TestModel.Person").PrimitiveProperty("Id", 1)
                }.InFeed()
                .InEntryWithExpandedLink(false /* isSingleton */)
                .InFeed()
                .InEntryWithExpandedLink(false),

                // Expanded feed with a number of child entries greater than recursive depth limit should succeed.
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadDescriptor = new PayloadTestDescriptor(),
                    PayloadEdmModel   = model,
                    PayloadElement    = PayloadBuilder.EntitySet().Append(
                        PayloadBuilder.Entity("TestModel.Person").PrimitiveProperty("Id", 1),
                        PayloadBuilder.Entity("TestModel.Person").PrimitiveProperty("Id", 2),
                        PayloadBuilder.Entity("TestModel.Person").PrimitiveProperty("Id", 3),
                        PayloadBuilder.Entity("TestModel.Person").PrimitiveProperty("Id", 4),
                        PayloadBuilder.Entity("TestModel.Person").PrimitiveProperty("Id", 5)),
                }.InEntryWithExpandedLink(false /* isSingleton */)
                .InEntryWithExpandedLink(true),
            };

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors.Concat(customTestDescriptors),
                this.ReaderTestConfigurationProvider.ExplicitFormatConfigurations,
                (testDescriptor, testConfiguration) =>
            {
                testConfiguration = new ReaderTestConfiguration(testConfiguration);
                testConfiguration.MessageReaderSettings.MessageQuotas.MaxNestingDepth = 3;

                testDescriptor.RunTest(testConfiguration);
            });
        }
Ejemplo n.º 12
0
        public static IEnumerable <PayloadReaderTestDescriptor> CreateStreamPropertyMetadataTestDescriptors(PayloadReaderTestDescriptor.Settings settings)
        {
            // Get the standard stream property payloads
            IEnumerable <PayloadReaderTestDescriptor> streamPropertyTestDescriptors = PayloadReaderTestDescriptorGenerator.CreateStreamReferenceValueTestDescriptors(settings, withMetadata: true);

            // Add some reader specific payloads
            EdmModel model = new EdmModel().Fixup();

            streamPropertyTestDescriptors = streamPropertyTestDescriptors.Concat(new PayloadReaderTestDescriptor[]
            {
                // Just read link - valid for readers
                new PayloadReaderTestDescriptor(settings)
                {
                    PayloadElement  = PayloadBuilder.StreamProperty("StreamProperty", readLink: "http://odata.org/mr"),
                    PayloadEdmModel = model,
                },
                // Just content type
                new PayloadReaderTestDescriptor(settings)
                {
                    PayloadElement  = PayloadBuilder.StreamProperty("StreamProperty", contentType: "mime/type"),
                    PayloadEdmModel = model,
                    // Doesn't work for ATOM as ATOM needs the self link to put the content type on
                    SkipTestConfiguration = tc => false
                },
                // Read link and content type
                new PayloadReaderTestDescriptor(settings)
                {
                    PayloadElement  = PayloadBuilder.StreamProperty("StreamProperty", readLink: "http://odata.org/mr", contentType: "mime/type"),
                    PayloadEdmModel = model,
                },
                // Just edit link - valid for readers
                new PayloadReaderTestDescriptor(settings)
                {
                    PayloadElement  = PayloadBuilder.StreamProperty("StreamProperty", editLink: "http://odata.org/mr"),
                    PayloadEdmModel = model,
                },
                // Just etag
                new PayloadReaderTestDescriptor(settings)
                {
                    PayloadElement  = PayloadBuilder.StreamProperty("StreamProperty", etag: "etag"),
                    PayloadEdmModel = model,
                    // Doesn't work for ATOM as ATOM needs the edit link to put the etag on
                    SkipTestConfiguration = tc => false
                },
                // Just edit link and etag - valid for readers
                new PayloadReaderTestDescriptor(settings)
                {
                    PayloadElement  = PayloadBuilder.StreamProperty("StreamProperty", editLink: "http://odata.org/mr", etag: "etag"),
                    PayloadEdmModel = model,
                },
                // All properties
                new PayloadReaderTestDescriptor(settings)
                {
                    PayloadElement  = PayloadBuilder.StreamProperty("StreamProperty", readLink: "http://odata.org/mrread", contentType: "mime/type", editLink: "http://odata.org/mr", etag: "etag"),
                    PayloadEdmModel = model,
                },

                // TODO: Add more tests around values for the stream properties. Readers won't validate anything, so empty values are acceptable
            });

            return(streamPropertyTestDescriptors);
        }
Ejemplo n.º 13
0
        public void ServiceDocumentReaderTest()
        {
            // NOTE: not using the payload generator here since the service documents can only appear at the top level
            IEnumerable <PayloadReaderTestDescriptor> testDescriptors =
                PayloadReaderTestDescriptorGenerator.CreateServiceDocumentDescriptors(this.Settings, baseUri, withTitles: false);

            // Add some hand-crafted payloads
            IEnumerable <PayloadReaderTestDescriptor> manualDescriptors = new PayloadReaderTestDescriptor[]
            {
                // service doc in request should fail
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.ServiceDocument().Workspace(
                        PayloadBuilder.Workspace().ResourceCollection(null, baseUri + "collection")),
                    SkipTestConfiguration = tc => !tc.IsRequest,
                    ExpectedException     = ODataExpectedExceptions.ODataException("ODataMessageReader_ServiceDocumentInRequest"),
                },

                // null title should work
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.ServiceDocument().Workspace(
                        PayloadBuilder.Workspace().ResourceCollection(null, baseUri + "collection")),
                    SkipTestConfiguration = tc => tc.IsRequest,
                },

                // null Href should fail
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.ServiceDocument().Workspace(
                        PayloadBuilder.Workspace().ResourceCollection(null, null)),
                    SkipTestConfiguration = tc => tc.IsRequest,
                    ExpectedException     = ODataExpectedExceptions.ODataException("ValidationUtils_ServiceDocumentElementUrlMustNotBeNull"),
                },

                // multiple collections with the same name/href should work
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.ServiceDocument().Workspace(
                        PayloadBuilder.Workspace()
                        .ResourceCollection(null, baseUri + "collection")
                        .ResourceCollection(null, baseUri + "collection")),
                    SkipTestConfiguration = tc => tc.IsRequest,
                },

                // relative URI without a base URI should fail in Atom and JSON Light, but is allowed in Verbose JSON.
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.ServiceDocument().Workspace(
                        PayloadBuilder.Workspace().ResourceCollection(null, "collection")),
                    SkipTestConfiguration  = tc => tc.IsRequest,
                    ExpectedResultCallback = tc =>
                    {
                        if (tc.Format == ODataFormat.Json)
                        {
                            return(new PayloadReaderTestExpectedResult(this.PayloadExpectedResultSettings)
                            {
                                ExpectedException = null
                            });
                        }
                        throw new NotImplementedException();
                    }
                },
            };

            testDescriptors = testDescriptors.Concat(manualDescriptors);

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                this.ReaderTestConfigurationProvider.ExplicitFormatConfigurations,
                (testDescriptor, testConfiguration) =>
            {
                if (testConfiguration.Format == ODataFormat.Json)
                {
                    testDescriptor = new PayloadReaderTestDescriptor(testDescriptor);

                    // Json light requires a model
                    testDescriptor.PayloadEdmModel = new EdmModel();

                    // Add an empty expected type annotation to cause metadata link to be generated in test serializer.
                    testDescriptor.PayloadElement.AddExpectedTypeAnnotation();

                    // Json light resource collections require the "Name" property, but it won't round-trip for verbose json, so add Name here.
                    foreach (var workspace in ((ServiceDocumentInstance)testDescriptor.PayloadElement).Workspaces)
                    {
                        int count = 0;
                        foreach (var resourceCollection in workspace.ResourceCollections)
                        {
                            resourceCollection.Name = "Entity Set Name " + count;
                            count++;
                        }
                    }
                }

                ReaderTestConfiguration testConfigClone = new ReaderTestConfiguration(testConfiguration);
                if (testConfiguration.Format == ODataFormat.Json)
                {
                    testConfigClone.MessageReaderSettings.BaseUri = null;
                }

                testDescriptor.RunTest(testConfigClone);
            });
        }
Ejemplo n.º 14
0
        public void ComplexValueWithMetadataTest()
        {
            // Use some standard complex value payloads first
            IEnumerable <PayloadReaderTestDescriptor> testDescriptors = PayloadReaderTestDescriptorGenerator.CreateComplexValueTestDescriptors(this.Settings, true);

            // Add metadata validation tests
            EdmModel model            = new EdmModel();
            var      innerComplexType = model.ComplexType("InnerComplexType");

            innerComplexType.AddStructuralProperty("name", EdmCoreModel.Instance.GetString(true));

            var complexType = model.ComplexType("ComplexType");

            complexType.AddStructuralProperty("number", EdmPrimitiveTypeKind.Int32);
            complexType.AddStructuralProperty("string", EdmCoreModel.Instance.GetString(true));
            complexType.AddStructuralProperty("complex", MetadataUtils.ToTypeReference(innerComplexType, true));

            var entityType = model.EntityType("EntityType");

            entityType.KeyProperty("Id", EdmCoreModel.Instance.GetInt32(false));
            model.Fixup();

            // Test that different types of properties not present in the metadata all fail
            IEnumerable <PropertyInstance> undeclaredPropertyTestCases = new PropertyInstance[]
            {
                PayloadBuilder.PrimitiveProperty("undeclared", 42),
                PayloadBuilder.Property("undeclared", PayloadBuilder.ComplexValue(innerComplexType.FullName())),
                PayloadBuilder.Property("undeclared", PayloadBuilder.PrimitiveMultiValue(EntityModelUtils.GetCollectionTypeName("Edm.Int32"))),
                PayloadBuilder.Property("undeclared", PayloadBuilder.ComplexMultiValue(EntityModelUtils.GetCollectionTypeName("TestModel.InnerComplexType"))),
            };

            testDescriptors = testDescriptors.Concat(
                undeclaredPropertyTestCases.Select(tc =>
            {
                return(new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.ComplexValue(complexType.FullName()).WithTypeAnnotation(complexType)
                                     .Property(tc),
                    PayloadEdmModel = model,
                    ExpectedException = ODataExpectedExceptions.ODataException("ValidationUtils_PropertyDoesNotExistOnType", "undeclared", "TestModel.ComplexType"),
                });
            }));

            testDescriptors = testDescriptors.Concat(new[]
            {
                // Property which should take typename not from value but from the parent metadata
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.ComplexValue(complexType.FullName()).WithTypeAnnotation(complexType)
                                     .Property("complex", PayloadBuilder.ComplexValue(innerComplexType.FullName()).PrimitiveProperty("name", null)
                                               .JsonRepresentation("{ \"name\" : null }").XmlRepresentation("<d:name m:null=\"true\" />")
                                               .AddAnnotation(new SerializationTypeNameTestAnnotation()
                    {
                        TypeName = null
                    })),
                    PayloadEdmModel = model,
                },
                // Property which is declared in the metadata but with a different type
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.ComplexValue(complexType.FullName()).WithTypeAnnotation(complexType)
                                     .Property("complex", PayloadBuilder.ComplexValue(complexType.FullName())),
                    PayloadEdmModel   = model,
                    ExpectedException = ODataExpectedExceptions.ODataException("ValidationUtils_IncompatibleType", "TestModel.ComplexType", "TestModel.InnerComplexType"),
                },
                // Property which is declared in the metadata but with a wrong kind
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.ComplexValue(complexType.FullName()).WithTypeAnnotation(complexType)
                                     .Property("complex", PayloadBuilder.ComplexValue(entityType.FullName())),
                    PayloadEdmModel   = model,
                    ExpectedException = ODataExpectedExceptions.ODataException("ValidationUtils_IncorrectTypeKind", "TestModel.EntityType", "Complex", "Entity"),
                },
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement    = PayloadBuilder.ComplexValue("").WithTypeAnnotation(complexType),
                    PayloadEdmModel   = model,
                    ExpectedException = ODataExpectedExceptions.ODataException("ValidationUtils_UnrecognizedTypeName", string.Empty)
                },
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement    = PayloadBuilder.ComplexValue("TestModel.NonExistant").WithTypeAnnotation(complexType),
                    PayloadEdmModel   = model,
                    ExpectedException = ODataExpectedExceptions.ODataException("ValidationUtils_UnrecognizedTypeName", "TestModel.NonExistant"),
                },
            });

            // Wrap the complex type in a property
            testDescriptors = testDescriptors
                              .Select((td, index) => new PayloadReaderTestDescriptor(td)
            {
                PayloadDescriptor = td.PayloadDescriptor.InProperty("propertyName" + index)
            })
                              .SelectMany(td => this.PayloadGenerator.GenerateReaderPayloads(td));

            // Handcrafted cases
            testDescriptors = testDescriptors.Concat(new[]
            {
                // Top-level complex property without expected type
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement  = PayloadBuilder.Property("property", PayloadBuilder.ComplexValue(complexType.FullName()).PrimitiveProperty("number", 42)),
                    PayloadEdmModel = model
                },
            });

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                this.ReaderTestConfigurationProvider.ExplicitFormatConfigurations,
                (testDescriptor, testConfiguration) =>
            {
                var property = testDescriptor.PayloadElement as PropertyInstance;
                if (property != null && testConfiguration.Format == ODataFormat.Atom)
                {
                    property.Name = null;
                }
                testDescriptor.RunTest(testConfiguration);
            });
        }