Exemplo n.º 1
0
        public void TopLevelOpenPropertiesTest()
        {
            EdmModel edmModel  = new EdmModel();
            var      container = new EdmEntityContainer("TestModel", "DefaultContainer");

            edmModel.AddElement(container);

            var openCustomerType = new EdmEntityType("TestModel", "OpenCustomerType", null, isAbstract: false, isOpen: true);

            openCustomerType.AddKeys(openCustomerType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32, isNullable: false));
            edmModel.AddElement(openCustomerType);

            var addressType = new EdmComplexType("TestModel", "AddressType");

            addressType.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String, isNullable: true);
            edmModel.AddElement(addressType);

            container.AddEntitySet("CustomerSet", openCustomerType);

            ISpatial pointValue = GeographyFactory.Point(32.0, -100.0).Build();

            IEnumerable <PropertyPayloadTestCase> testCases = new[]
            {
                new PropertyPayloadTestCase
                {
                    DebugDescription = "Top-level open primitive property.",
                    Property         = new ODataProperty {
                        Name = "Age", Value = (long)42
                    },
                    Model        = edmModel,
                    PropertyType = "Edm.Int64",
                    Json         = string.Join("$(NL)",
                                               "{{",
                                               "{0},\"value\":\"42\"",
                                               "}}")
                },
                new PropertyPayloadTestCase
                {
                    DebugDescription = "Top-level open spatial property.",
                    Property         = new ODataProperty {
                        Name = "Location", Value = pointValue
                    },
                    Model        = edmModel,
                    PropertyType = "Edm.GeographyPoint",
                    Json         = string.Join("$(NL)",
                                               "{{",
                                               "{0}," +
                                               "\"" + JsonLightConstants.ODataValuePropertyName + "\":{{",
                                               "\"type\":\"Point\",\"coordinates\":[",
                                               "-100.0,32.0",
                                               "],\"crs\":{{",
                                               "\"type\":\"name\",\"properties\":{{",
                                               "\"name\":\"EPSG:4326\"",
                                               "}}",
                                               "}}",
                                               "}}",
                                               "}}")
                },
                new PropertyPayloadTestCase
                {
                    DebugDescription = "Top-level open complex property.",
                    Property         = new ODataProperty {
                        Name = "Address", Value = new ODataComplexValue {
                            TypeName = "TestModel.AddressType"
                        }
                    },
                    Model        = edmModel,
                    PropertyType = "TestModel.AddressType",
                    Json         = string.Join("$(NL)",
                                               "{{",
                                               "{0}",
                                               "}}")
                },
            };

            IEnumerable <PayloadWriterTestDescriptor <ODataProperty> > testDescriptors = testCases.Select(testCase =>
                                                                                                          new PayloadWriterTestDescriptor <ODataProperty>(
                                                                                                              this.Settings,
                                                                                                              testCase.Property,
                                                                                                              tc => new JsonWriterTestExpectedResults(this.Settings.ExpectedResultSettings)
            {
                Json = string.Format(
                    CultureInfo.InvariantCulture,
                    testCase.Json,
                    JsonLightWriterUtils.GetMetadataUrlPropertyForProperty(testCase.PropertyType)),
                FragmentExtractor = (result) => result.RemoveAllAnnotations(true)
            })
            {
                DebugDescription = testCase.DebugDescription,
                Model            = testCase.Model,
            });

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                this.WriterTestConfigurationProvider.JsonLightFormatConfigurationsWithIndent,
                (testDescriptor, testConfiguration) =>
            {
                TestWriterUtils.WriteAndVerifyTopLevelContent(
                    testDescriptor,
                    testConfiguration,
                    (messageWriter) => messageWriter.WriteProperty(testDescriptor.PayloadItems.Single()),
                    this.Assert,
                    baselineLogger: this.Logger);
            });
        }
        public void WriteUntypedValueTest()
        {
            EdmModel edmModel = new EdmModel();

            var jsonType    = new EdmComplexType("TestModel", "JsonType");
            var jsonTypeRef = new EdmComplexTypeReference(jsonType, isNullable: true);

            edmModel.AddElement(jsonType);

            var collectionType    = new EdmCollectionType(jsonTypeRef);
            var collectionTypeRef = new EdmCollectionTypeReference(collectionType);

            var entityType = new EdmEntityType("TestModel", "EntityType");

            entityType.AddStructuralProperty("Value", jsonTypeRef);
            entityType.AddStructuralProperty("CollectionValue", collectionTypeRef);
            edmModel.AddElement(entityType);

            var container = new EdmEntityContainer("TestModel", "DefaultContainer");

            container.AddEntitySet("EntitySet", entityType);
            edmModel.AddElement(container);

            const string JsonFormat = "$(NL){{{0},\"Value\":{1}}}";

            IEnumerable <PropertyPayloadTestCase> testCases = new[]
            {
                new PropertyPayloadTestCase
                {
                    DebugDescription = "Null.",
                    Property         = new ODataProperty
                    {
                        Name  = "Value",
                        Value = new ODataUntypedValue()
                        {
                            RawValue = "null"
                        }
                    },
                },
                new PropertyPayloadTestCase
                {
                    DebugDescription = "Integer.",
                    Property         = new ODataProperty
                    {
                        Name  = "Value",
                        Value = new ODataUntypedValue()
                        {
                            RawValue = "42"
                        }
                    },
                },
                new PropertyPayloadTestCase
                {
                    DebugDescription = "Float.",
                    Property         = new ODataProperty
                    {
                        Name  = "Value",
                        Value = new ODataUntypedValue()
                        {
                            RawValue = "3.1415"
                        }
                    },
                },
                new PropertyPayloadTestCase
                {
                    DebugDescription = "String.",
                    Property         = new ODataProperty
                    {
                        Name  = "Value",
                        Value = new ODataUntypedValue()
                        {
                            RawValue = "\"string\""
                        }
                    },
                },
                new PropertyPayloadTestCase
                {
                    DebugDescription = "Array of elements of mixed types.",
                    Property         = new ODataProperty
                    {
                        Name  = "Value",
                        Value = new ODataUntypedValue()
                        {
                            RawValue = "[1, 2, \"abc\"]"
                        }
                    },
                },
                new PropertyPayloadTestCase
                {
                    DebugDescription = "Array of arrays.",
                    Property         = new ODataProperty
                    {
                        Name  = "Value",
                        Value = new ODataUntypedValue()
                        {
                            RawValue = "[ [1, \"abc\"], [2, \"def\"], [[3],[4, 5]] ]"
                        }
                    },
                },
                new PropertyPayloadTestCase
                {
                    DebugDescription = "Negative - empty RawValue",
                    Property         = new ODataProperty
                    {
                        Name  = "Value",
                        Value = new ODataUntypedValue()
                        {
                            RawValue = string.Empty
                        },
                    },
                    ExpectedException = ODataExpectedExceptions.ODataException(
                        TextRes.ODataJsonLightValueSerializer_MissingRawValueOnUntyped),
                },
                new PropertyPayloadTestCase
                {
                    DebugDescription = "Collection of Edm.Untyped elements.",
                    Property         = new ODataProperty
                    {
                        Name  = "CollectionValue",
                        Value = new ODataCollectionValue()
                        {
                            TypeName = "Collection(TestModel.JsonType)",
                            Items    = new object[]
                            {
                                new ODataUntypedValue()
                                {
                                    RawValue = "\"string\""
                                },
                                new ODataUntypedValue()
                                {
                                    RawValue = "[1, 2, \"abc\"]"
                                },
                                new ODataUntypedValue()
                                {
                                    RawValue = "3.1415"
                                }
                            }
                        }
                    },
                },
                new PropertyPayloadTestCase
                {
                    DebugDescription = "Integer.",
                    Property         = new ODataProperty
                    {
                        Name  = "Value",
                        Value = new ODataPrimitiveValue(42)
                    },
                },
                new PropertyPayloadTestCase
                {
                    DebugDescription = "Float.",
                    Property         = new ODataProperty
                    {
                        Name  = "Value",
                        Value = new ODataPrimitiveValue(3.1415)
                    },
                },
                new PropertyPayloadTestCase
                {
                    DebugDescription = "String.",
                    Property         = new ODataProperty
                    {
                        Name  = "Value",
                        Value = new ODataPrimitiveValue("string")
                    },
                },
            };

            IEnumerable <PayloadWriterTestDescriptor <ODataProperty> > testDescriptors = testCases.Select(testCase =>
                                                                                                          new PayloadWriterTestDescriptor <ODataProperty>(
                                                                                                              this.Settings,
                                                                                                              testCase.Property,
                                                                                                              tc => new JsonWriterTestExpectedResults(this.Settings.ExpectedResultSettings)
            {
                Json = string.Format(
                    CultureInfo.InvariantCulture,
                    JsonFormat,
                    JsonLightWriterUtils.GetMetadataUrlPropertyForEntry("EntitySet"),
                    GetExpectedJson(testCase.Property.Value)),
                FragmentExtractor  = (result) => result.RemoveAllAnnotations(false),
                ExpectedException2 = testCase.ExpectedException,
            })
            {
                DebugDescription = testCase.DebugDescription,
                Model            = edmModel,
            });

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                this.WriterTestConfigurationProvider.JsonLightFormatConfigurationsWithIndent,
                (testDescriptor, testConfiguration) =>
            {
                TestWriterUtils.WriteAndVerifyTopLevelContent(
                    testDescriptor,
                    testConfiguration,
                    (messageWriter) =>
                {
                    messageWriter.PrivateSettings.Validations = ~ValidationKinds.ThrowOnUndeclaredPropertyForNonOpenType;
                    messageWriter.WriteProperty(testDescriptor.PayloadItems.Single());
                },
                    this.Assert,
                    baselineLogger: this.Logger);
            });
        }
Exemplo n.º 3
0
        public void PayloadOrderTest()
        {
            EdmModel model     = new EdmModel();
            var      container = new EdmEntityContainer("TestModel", "DefaultContainer");

            model.AddElement(container);

            var otherType = new EdmEntityType("TestModel", "OtherType");

            otherType.AddKeys(otherType.AddStructuralProperty("ID", EdmCoreModel.Instance.GetInt32(false)));
            model.AddElement(otherType);
            var otherset = container.AddEntitySet("OtherType", otherType);

            var nonMLEBaseType = new EdmEntityType("TestModel", "NonMLEBaseType");

            nonMLEBaseType.AddKeys(nonMLEBaseType.AddStructuralProperty("ID", EdmCoreModel.Instance.GetInt32(false)));
            model.AddElement(nonMLEBaseType);
            var nonMLESet = container.AddEntitySet("NonMLESet", nonMLEBaseType);

            var nonMLEType = new EdmEntityType("TestModel", "NonMLEType", nonMLEBaseType);

            nonMLEType.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(true));
            nonMLEType.AddStructuralProperty("Description", EdmCoreModel.Instance.GetString(true));
            nonMLEType.AddStructuralProperty("StreamProperty", EdmPrimitiveTypeKind.Stream, isNullable: false);
            var nonMLENav = nonMLEType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "NavProp", Target = otherType, TargetMultiplicity = EdmMultiplicity.Many
            });

            nonMLESet.AddNavigationTarget(nonMLENav, otherset);
            model.AddElement(nonMLEType);

            var mleBaseType = new EdmEntityType("TestModel", "MLEBaseType");

            mleBaseType.AddKeys(mleBaseType.AddStructuralProperty("ID", EdmCoreModel.Instance.GetInt32(false)));
            model.AddElement(mleBaseType);
            var mleSet = container.AddEntitySet("MLESet", mleBaseType);

            var mleType = new EdmEntityType("TestModel", "MLEType", mleBaseType, false, false, true);

            mleType.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(true));
            mleType.AddStructuralProperty("Description", EdmCoreModel.Instance.GetString(true));
            mleType.AddStructuralProperty("StreamProperty", EdmPrimitiveTypeKind.Stream, isNullable: false);
            var mleNav = mleType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "NavProp", Target = otherType, TargetMultiplicity = EdmMultiplicity.Many
            });

            mleSet.AddNavigationTarget(mleNav, otherset);
            model.AddElement(mleType);

            IEnumerable <EntryPayloadTestCase> testCases = new[]
            {
                new EntryPayloadTestCase
                {
                    DebugDescription = "TypeName at the beginning, nothing else",
                    Entry            = new ODataEntry()
                    {
                        TypeName = "TestModel.NonMLEType"
                    },
                    Model     = model,
                    EntitySet = nonMLESet,
                    Json      = string.Join("$(NL)",
                                            "{{",
                                            "{1}" +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataTypeAnnotationName + "\":\"TestModel.NonMLEType\"{0}",
                                            "}}")
                },
                new EntryPayloadTestCase
                {
                    DebugDescription = "TypeName at the beginning, changes at the end - the one from the beginning is used (also for validation).",
                    Entry            = new ODataEntry()
                    {
                        MediaResource = new ODataStreamReferenceValue(),
                        Properties    = new []
                        {
                            new ODataProperty {
                                Name = "ID", Value = (int)42
                            },
                            new ODataProperty {
                                Name = "Name", Value = "test"
                            },
                        }
                    }
                    .WithAnnotation(new WriteEntryCallbacksAnnotation
                    {
                        BeforeWriteStartCallback = (entry) => { entry.TypeName = "TestModel.MLEType"; },
                        BeforeWriteEndCallback   = (entry) => { entry.TypeName = "NonExistingType"; }
                    }),
                    Model     = model,
                    EntitySet = mleSet,
                    Json      = string.Join("$(NL)",
                                            "{{",
                                            "{1}" +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataTypeAnnotationName + "\":\"TestModel.MLEType\"{0},\"ID\":\"42\",\"Name\":\"test\"",
                                            "}}")
                },
                new EntryPayloadTestCase
                {
                    DebugDescription = "TypeName, ID and ETag at the beginning, nothing else",
                    Entry            = new ODataEntry()
                    {
                        TypeName = "TestModel.NonMLEType", Id = new Uri("urn:id"), ETag = "etag"
                    },
                    Model     = model,
                    EntitySet = nonMLESet,
                    Json      = string.Join("$(NL)",
                                            "{{",
                                            "{1}" +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataTypeAnnotationName + "\":\"TestModel.NonMLEType\"," +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataIdAnnotationName + "\":\"urn:id\"," +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataETagAnnotationName + "\":\"etag\"{0}",
                                            "}}")
                },
                new EntryPayloadTestCase
                {
                    DebugDescription = "TypeName at the beginning, ID and ETag at the end, ID and ETag are not written and are ignored at the end",
                    Entry            = new ODataEntry()
                    {
                        TypeName = "TestModel.NonMLEType"
                    }
                    .WithAnnotation(new WriteEntryCallbacksAnnotation
                    {
                        BeforeWriteStartCallback = (entry) => { entry.Id = null; entry.ETag = null; },
                        BeforeWriteEndCallback   = (entry) => { entry.Id = new Uri("urn:id"); entry.ETag = "etag"; }
                    }),
                    Model     = model,
                    EntitySet = nonMLESet,
                    Json      = string.Join("$(NL)",
                                            "{{",
                                            "{1}" +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataTypeAnnotationName + "\":\"TestModel.NonMLEType\"{0}",
                                            "}}")
                },
                new EntryPayloadTestCase
                {
                    DebugDescription = "Everything at the beginning",
                    Entry            = new ODataEntry()
                    {
                        TypeName      = "TestModel.MLEType",
                        Id            = new Uri("urn:id"),
                        ETag          = "etag",
                        EditLink      = new Uri("http://odata.org/editlink"),
                        ReadLink      = new Uri("http://odata.org/readlink"),
                        MediaResource = new ODataStreamReferenceValue()
                        {
                            EditLink    = new Uri("http://odata.org/mediaeditlink"),
                            ReadLink    = new Uri("http://odata.org/mediareadlink"),
                            ETag        = "mediaetag",
                            ContentType = "media/contenttype"
                        },
                        Properties = new []
                        {
                            new ODataProperty {
                                Name = "ID", Value = (int)42
                            },
                            new ODataProperty {
                                Name = "Name", Value = "test"
                            },
                        }
                    },
                    Model     = model,
                    EntitySet = mleSet,
                    Json      = string.Join("$(NL)",
                                            "{{",
                                            "{1}" +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataTypeAnnotationName + "\":\"TestModel.MLEType\"," +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataIdAnnotationName + "\":\"urn:id\"," +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataETagAnnotationName + "\":\"etag\"," +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataEditLinkAnnotationName + "\":\"http://odata.org/editlink\"," +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataReadLinkAnnotationName + "\":\"http://odata.org/readlink\"," +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataMediaEditLinkAnnotationName + "\":\"http://odata.org/mediaeditlink\"," +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataMediaReadLinkAnnotationName + "\":\"http://odata.org/mediareadlink\"," +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataMediaContentTypeAnnotationName + "\":\"media/contenttype\"," +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataMediaETagAnnotationName + "\":\"mediaetag\"{0}," +
                                            "\"ID\":\"42\"," +
                                            "\"Name\":\"test\"",
                                            "}}")
                },
                new EntryPayloadTestCase
                {
                    DebugDescription = "TypeName, Id, ETag and ReadLinks at the beginning, the rest at the end",
                    Entry            = new ODataEntry()
                    {
                        TypeName      = "TestModel.MLEType",
                        Id            = new Uri("urn:id"),
                        ETag          = "etag",
                        ReadLink      = new Uri("http://odata.org/readlink"),
                        MediaResource = new ODataStreamReferenceValue()
                        {
                            ReadLink = new Uri("http://odata.org/mediareadlink")
                        },
                        Properties = new []
                        {
                            new ODataProperty {
                                Name = "ID", Value = (int)42
                            },
                            new ODataProperty {
                                Name = "Name", Value = "test"
                            },
                        }
                    }.WithAnnotation(new WriteEntryCallbacksAnnotation
                    {
                        BeforeWriteStartCallback = (entry) =>
                        {
                            entry.EditLink = null;
                            entry.MediaResource.EditLink    = null;
                            entry.MediaResource.ETag        = null;
                            entry.MediaResource.ContentType = null;
                        },
                        BeforeWriteEndCallback = (entry) =>
                        {
                            entry.EditLink = new Uri("http://odata.org/editlink");
                            entry.MediaResource.EditLink    = new Uri("http://odata.org/mediaeditlink");
                            entry.MediaResource.ETag        = "mediaetag";
                            entry.MediaResource.ContentType = "media/contenttype";
                        }
                    }),
                    Model     = model,
                    EntitySet = mleSet,
                    Json      = string.Join("$(NL)",
                                            "{{",
                                            "{1}" +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataTypeAnnotationName + "\":\"TestModel.MLEType\"," +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataIdAnnotationName + "\":\"urn:id\"," +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataETagAnnotationName + "\":\"etag\"," +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataReadLinkAnnotationName + "\":\"http://odata.org/readlink\"," +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataMediaReadLinkAnnotationName + "\":\"http://odata.org/mediareadlink\"{0}," +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataEditLinkAnnotationName + "\":\"http://odata.org/editlink\"," +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataMediaEditLinkAnnotationName + "\":\"http://odata.org/mediaeditlink\"," +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataMediaContentTypeAnnotationName + "\":\"media/contenttype\"," +
                                            "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataMediaETagAnnotationName + "\":\"mediaetag\"," +
                                            "\"ID\":\"42\"," +
                                            "\"Name\":\"test\"",
                                            "}}")
                },
            };

            IEnumerable <PayloadWriterTestDescriptor <ODataItem> > testDescriptors = testCases.Select(testCase =>
                                                                                                      new PayloadWriterTestDescriptor <ODataItem>(
                                                                                                          this.Settings,
                                                                                                          new ODataItem[] { testCase.Entry },
                                                                                                          tc => new JsonWriterTestExpectedResults(this.Settings.ExpectedResultSettings)
            {
                Json = string.Format(CultureInfo.InvariantCulture, testCase.Json,
                                     string.Empty,
                                     JsonLightWriterUtils.GetMetadataUrlPropertyForEntry(testCase.EntitySet.Name) + ","),
                FragmentExtractor = (result) => result.RemoveAllAnnotations(true)
            })
            {
                DebugDescription           = testCase.DebugDescription,
                Model                      = testCase.Model,
                PayloadEdmElementContainer = testCase.EntitySet,
                PayloadEdmElementType      = testCase.EntityType,
                SkipTestConfiguration      = testCase.SkipTestConfiguration
            });

            testDescriptors = testDescriptors.Concat(testCases.Select(testCase =>
                                                                      new PayloadWriterTestDescriptor <ODataItem>(
                                                                          this.Settings,
                                                                          new ODataItem[] { testCase.Entry, new ODataNavigationLink
                                                                                            {
                                                                                                Name         = "NavProp",
                                                                                                IsCollection = true,
                                                                                                Url          = new Uri("http://odata.org/navprop/uri")
                                                                                            } },
                                                                          tc => new JsonWriterTestExpectedResults(this.Settings.ExpectedResultSettings)
            {
                Json = string.Format(CultureInfo.InvariantCulture, testCase.Json,
                                     tc.IsRequest ?
                                     ",\"" + JsonLightUtils.GetPropertyAnnotationName("NavProp", JsonLightConstants.ODataBindAnnotationName) + "\":[$(NL)\"http://odata.org/navprop/uri\"$(NL)]" :
                                     ",\"" + JsonLightUtils.GetPropertyAnnotationName("NavProp", JsonLightConstants.ODataNavigationLinkUrlAnnotationName) + "\":\"http://odata.org/navprop/uri\"",
                                     JsonLightWriterUtils.GetMetadataUrlPropertyForEntry(testCase.EntitySet.Name) + ","),
                FragmentExtractor = (result) => result.RemoveAllAnnotations(true)
            })
            {
                DebugDescription           = testCase.DebugDescription + "- with navigation property",
                Model                      = testCase.Model,
                PayloadEdmElementContainer = testCase.EntitySet,
                PayloadEdmElementType      = testCase.EntityType,
                SkipTestConfiguration      = testCase.SkipTestConfiguration
            }))
                              .Concat(testCases.Select(testCase =>
                                                       new PayloadWriterTestDescriptor <ODataItem>(
                                                           this.Settings,
                                                           new ODataItem[] { testCase.Entry, new ODataNavigationLink
                                                                             {
                                                                                 Name               = "NavProp",
                                                                                 IsCollection       = true,
                                                                                 Url                = new Uri("http://odata.org/navprop/uri"),
                                                                                 AssociationLinkUrl = new Uri("http://odata.org/assoclink")
                                                                             } },
                                                           tc => new JsonWriterTestExpectedResults(this.Settings.ExpectedResultSettings)
            {
                Json = string.Format(CultureInfo.InvariantCulture, testCase.Json,
                                     tc.IsRequest ?
                                     ",\"" + JsonLightUtils.GetPropertyAnnotationName("NavProp", JsonLightConstants.ODataBindAnnotationName) + "\":[$(NL)\"http://odata.org/navprop/uri\"$(NL)]" :
                                     ",\"" + JsonLightUtils.GetPropertyAnnotationName("NavProp", JsonLightConstants.ODataNavigationLinkUrlAnnotationName) + "\":\"http://odata.org/navprop/uri\"" +
                                     ",\"" + JsonLightUtils.GetPropertyAnnotationName("NavProp", JsonLightConstants.ODataAssociationLinkUrlAnnotationName) + "\":\"http://odata.org/assoclink\"",
                                     JsonLightWriterUtils.GetMetadataUrlPropertyForEntry(testCase.EntitySet.Name) + ","),
                FragmentExtractor = (result) => result.RemoveAllAnnotations(true)
            })
            {
                DebugDescription           = testCase.DebugDescription + "- with navigation property",
                Model                      = testCase.Model,
                PayloadEdmElementContainer = testCase.EntitySet,
                PayloadEdmElementType      = testCase.EntityType,
                SkipTestConfiguration      = testCase.SkipTestConfiguration
            }));

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                this.WriterTestConfigurationProvider.JsonLightFormatConfigurationsWithIndent,
                (testDescriptor, testConfiguration) =>
            {
                TestWriterUtils.WriteAndVerifyODataEdmPayload(testDescriptor.DeferredLinksToEntityReferenceLinksInRequest(testConfiguration), testConfiguration, this.Assert, this.Logger);
            });
        }
Exemplo n.º 4
0
        public void SpatialPropertiesInEntryTest()
        {
            EdmModel model     = new EdmModel();
            var      container = new EdmEntityContainer("TestModel", "TestContainer");

            model.AddElement(container);

            var customerType = new EdmEntityType("TestModel", "CustomerType");

            customerType.AddKeys(customerType.AddStructuralProperty("ID", EdmCoreModel.Instance.GetInt32(false)));
            customerType.AddStructuralProperty("Location1", EdmCoreModel.Instance.GetSpatial(EdmPrimitiveTypeKind.Geography, false));
            customerType.AddStructuralProperty("Location2", EdmCoreModel.Instance.GetSpatial(EdmPrimitiveTypeKind.GeographyPoint, false));
            model.AddElement(customerType);
            var customerSet = container.AddEntitySet("CustomerSet", customerType);

            ISpatial pointValue = GeographyFactory.Point(32.0, -100.0).Build();

            IEnumerable <EntryPayloadTestCase> testCases = new[]
            {
                new EntryPayloadTestCase
                {
                    DebugDescription = "Customer instance with spatial property (expected and payload type don't match).",
                    Entry            = new ODataEntry()
                    {
                        TypeName   = "TestModel.CustomerType",
                        Properties = new ODataProperty[]
                        {
                            new ODataProperty {
                                Name = "Location1", Value = pointValue
                            }
                        }
                    },
                    Model     = model,
                    EntitySet = customerSet,
                    Json      = string.Join("$(NL)",
                                            "{{",
                                            "{0}" +
                                            "\"" + JsonLightUtils.GetPropertyAnnotationName("Location1", JsonLightConstants.ODataTypeAnnotationName) + "\":\"GeographyPoint\"," +
                                            "\"Location1\":{{",
                                            "\"type\":\"Point\",\"coordinates\":[",
                                            "-100.0,32.0",
                                            "],\"crs\":{{",
                                            "\"type\":\"name\",\"properties\":{{",
                                            "\"name\":\"EPSG:4326\"",
                                            "}}",
                                            "}}",
                                            "}}",
                                            "}}")
                },
                new EntryPayloadTestCase
                {
                    DebugDescription = "Customer instance with spatial property (expected and payload type match).",
                    Entry            = new ODataEntry()
                    {
                        TypeName   = "TestModel.CustomerType",
                        Properties = new ODataProperty[]
                        {
                            new ODataProperty {
                                Name = "Location2", Value = pointValue
                            }
                        }
                    },
                    Model     = model,
                    EntitySet = customerSet,
                    Json      = string.Join("$(NL)",
                                            "{{",
                                            "{0}" +
                                            "\"Location2\":{{",
                                            "\"type\":\"Point\",\"coordinates\":[",
                                            "-100.0,32.0",
                                            "],\"crs\":{{",
                                            "\"type\":\"name\",\"properties\":{{",
                                            "\"name\":\"EPSG:4326\"",
                                            "}}",
                                            "}}",
                                            "}}",
                                            "}}")
                },
            };

            IEnumerable <PayloadWriterTestDescriptor <ODataItem> > testDescriptors = testCases.Select(testCase =>
                                                                                                      new PayloadWriterTestDescriptor <ODataItem>(
                                                                                                          this.Settings,
                                                                                                          new ODataItem[] { testCase.Entry },
                                                                                                          tc => new JsonWriterTestExpectedResults(this.Settings.ExpectedResultSettings)
            {
                Json = string.Format(
                    CultureInfo.InvariantCulture,
                    testCase.Json,
                    JsonLightWriterUtils.GetMetadataUrlPropertyForEntry(testCase.EntitySet.Name) + ","),
                FragmentExtractor = (result) => result.RemoveAllAnnotations(true)
            })
            {
                DebugDescription           = testCase.DebugDescription,
                Model                      = testCase.Model,
                PayloadEdmElementContainer = testCase.EntitySet,
                PayloadEdmElementType      = testCase.EntityType,
                SkipTestConfiguration      = testCase.SkipTestConfiguration
            });

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                this.WriterTestConfigurationProvider.JsonLightFormatConfigurationsWithIndent,
                (testDescriptor, testConfiguration) =>
            {
                TestWriterUtils.WriteAndVerifyODataEdmPayload(testDescriptor, testConfiguration, this.Assert, this.Logger);
            });
        }
Exemplo n.º 5
0
        public void ActionAndFunctionPayloadOrderTest()
        {
            EdmModel model     = new EdmModel();
            var      container = new EdmEntityContainer("TestModel", "TestContainer");

            model.AddElement(container);

            var otherType = new EdmEntityType("TestModel", "OtherType");

            otherType.AddKeys(otherType.AddStructuralProperty("ID", EdmCoreModel.Instance.GetInt32(false)));
            model.AddElement(otherType);
            container.AddEntitySet("OtherType", otherType);

            var nonMLEBaseType = new EdmEntityType("TestModel", "NonMLEBaseType");

            nonMLEBaseType.AddKeys(nonMLEBaseType.AddStructuralProperty("ID", EdmCoreModel.Instance.GetInt32(false)));
            model.AddElement(nonMLEBaseType);
            var nonMLESet = container.AddEntitySet("NonMLESet", nonMLEBaseType);

            var nonMLEType = new EdmEntityType("TestModel", "NonMLEType", nonMLEBaseType);

            nonMLEType.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(true));
            nonMLEType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "NavProp", Target = otherType, TargetMultiplicity = EdmMultiplicity.Many
            });
            model.AddElement(nonMLEType);
            container.AddEntitySet("NonMLEType", nonMLEType);

            ODataAction action = new ODataAction
            {
                Metadata = new Uri("http://odata.org/test/$metadata#defaultAction"),
                Title    = "Default Action",
                Target   = new Uri("http://www.odata.org/defaultAction"),
            };

            ODataFunction function = new ODataFunction
            {
                Metadata = new Uri("http://odata.org/test/$metadata#defaultFunction()"),
                Title    = "Default Function",
                Target   = new Uri("defaultFunctionTarget", UriKind.Relative)
            };

            string defaultJson = string.Join("$(NL)",
                                             "{{",
                                             "{1}" +
                                             "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataTypeAnnotationName + "\":\"#TestModel.NonMLEType\"{0},\"#defaultAction\":{{",
                                             "\"title\":\"Default Action\",\"target\":\"http://www.odata.org/defaultAction\"",
                                             "}},\"#defaultFunction()\":{{",
                                             "\"title\":\"Default Function\",\"target\":\"defaultFunctionTarget\"",
                                             "}}",
                                             "}}");

            var entryWithActionAndFunction = new ODataEntry()
            {
                TypeName = "TestModel.NonMLEType",
            };

            entryWithActionAndFunction.AddAction(action);
            entryWithActionAndFunction.AddFunction(function);
            IEnumerable <EntryPayloadTestCase> testCases = new[]
            {
                new EntryPayloadTestCase
                {
                    DebugDescription = "Functions and actions available at the beginning.",
                    Entry            = entryWithActionAndFunction,
                    Model            = model,
                    EntitySet        = nonMLESet,
                    Json             = defaultJson
                },
            };

            IEnumerable <PayloadWriterTestDescriptor <ODataItem> > testDescriptors = testCases.Select(testCase =>
                                                                                                      new PayloadWriterTestDescriptor <ODataItem>(
                                                                                                          this.Settings,
                                                                                                          new ODataItem[] { testCase.Entry },
                                                                                                          tc => new JsonWriterTestExpectedResults(this.Settings.ExpectedResultSettings)
            {
                Json = string.Format(CultureInfo.InvariantCulture, testCase.Json,
                                     string.Empty,
                                     tc.IsRequest ? string.Empty : (JsonLightWriterUtils.GetMetadataUrlPropertyForEntry(testCase.EntitySet.Name) + ",")),
                FragmentExtractor = (result) => result.RemoveAllAnnotations(true)
            })
            {
                DebugDescription           = testCase.DebugDescription,
                Model                      = testCase.Model,
                PayloadEdmElementContainer = testCase.EntitySet,
                PayloadEdmElementType      = testCase.EntityType,
                SkipTestConfiguration      = testCase.SkipTestConfiguration
            });

            testDescriptors = testDescriptors.Concat(testCases.Select(testCase =>
                                                                      new PayloadWriterTestDescriptor <ODataItem>(
                                                                          this.Settings,
                                                                          new ODataItem[] { testCase.Entry, new ODataNavigationLink
                                                                                            {
                                                                                                Name         = "NavProp",
                                                                                                IsCollection = true,
                                                                                                Url          = new Uri("http://odata.org/navprop/uri")
                                                                                            } },
                                                                          tc => new JsonWriterTestExpectedResults(this.Settings.ExpectedResultSettings)
            {
                Json = string.Format(CultureInfo.InvariantCulture, testCase.Json,
                                     tc.IsRequest ?
                                     ",\"NavProp\":[$(NL){$(NL)\"__metadata\":{$(NL)\"uri\":\"http://odata.org/navprop/uri\"$(NL)}$(NL)}$(NL)]" :
                                     ",\"" + JsonLightUtils.GetPropertyAnnotationName("NavProp", JsonLightConstants.ODataNavigationLinkUrlAnnotationName) + "\":\"http://odata.org/navprop/uri\"",
                                     tc.IsRequest ? string.Empty : (JsonLightWriterUtils.GetMetadataUrlPropertyForEntry(testCase.EntitySet.Name) + ",")),
                FragmentExtractor = (result) => result.RemoveAllAnnotations(true)
            })
            {
                DebugDescription           = testCase.DebugDescription + "- with navigation property",
                Model                      = testCase.Model,
                PayloadEdmElementContainer = testCase.EntitySet,
                PayloadEdmElementType      = testCase.EntityType,
                SkipTestConfiguration      = testCase.SkipTestConfiguration
            }));

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                // Actions/functions are only supported in responses.
                this.WriterTestConfigurationProvider.JsonLightFormatConfigurationsWithIndent.Where(tc => !tc.IsRequest),
                (testDescriptor, testConfiguration) =>
            {
                TestWriterUtils.WriteAndVerifyODataEdmPayload(testDescriptor.DeferredLinksToEntityReferenceLinksInRequest(testConfiguration), testConfiguration, this.Assert, this.Logger);
            });
        }