public void JsonLightTaupoSerializerServiceDocumentTest() { var testCases = new JsonLightSerializerTestCase[] { // Service document new JsonLightSerializerTestCase { PayloadElement = PayloadBuilder.ServiceDocument().Workspace( PayloadBuilder.Workspace() .ResourceCollection("Coll1Title", "Coll1Href") .ResourceCollection("Coll2Title", "Coll2Href") .WithTitle("WorkspaceTitle")) .WithContextUri("http://odata.org/metadatauri"), ExpectedJson = @" { """ + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataContextAnnotationName + @""":""http://odata.org/metadatauri"", """ + JsonLightConstants.ODataValuePropertyName + @""":[ { ""name"":""Coll1Title"", ""url"":""Coll1Href"" }, { ""name"":""Coll2Title"", ""url"":""Coll2Href"" } ] }" }, }; this.CombinatorialEngineProvider.RunCombinations( testCases, (testCase) => { JsonValue actualValue = this.JsonLightSerializer.ConvertToJsonLightValue(testCase.PayloadElement); JsonValue expectedValue = JsonTextPreservingParser.ParseValue(new StringReader(testCase.ExpectedJson)); this.JsonValueComparer.Compare(expectedValue, actualValue); }); }
public void ServiceDocumentReadingNullUriTest() { EdmModel model = (EdmModel)Microsoft.Test.OData.Utils.Metadata.TestModels.BuildTestModel(); model.Fixup(); ServiceDocumentInstance serviceDocument = PayloadBuilder.ServiceDocument() .Workspace(PayloadBuilder.Workspace() .ResourceCollection(/*title*/ null, /*href*/ null)); // Run only on responses this.CombinatorialEngineProvider.RunCombinations( baseUriValues, this.ReaderTestConfigurationProvider.AtomFormatConfigurations.Where(tc => !tc.IsRequest), (baseUriValue, testConfiguration) => { // NOTE: In JSON, collection names must not be null (and we expect an error) // In ATOM, the HREF of a resource collection is an attribute and will be missing if null; // this is not allowed (and we expect an error) NullUriValueTestCase <ServiceDocumentInstance> testCase = new NullUriValueTestCase <ServiceDocumentInstance> { SetNullUriAction = (instance, uri, testConfig) => instance.Workspaces[0].ResourceCollections[0].Href = null, SetExpectedUriAction = (instance, uri, testConfig) => instance.Workspaces[0].ResourceCollections[0].Href = UriToString(uri), }; this.RunNullUriReadingTest(serviceDocument, testCase, model, baseUriValue, testConfiguration); }); }
public void ServiceDocumentMessageSizeLimitReadTest() { EdmModel model = Test.OData.Utils.Metadata.TestModels.BuildTestModel() as EdmModel; ODataPayloadElement payload = PayloadBuilder.ServiceDocument().Workspace( PayloadBuilder.Workspace() .ResourceCollection(null, "http://odata.org/FirstCollection")); var testCases = new MessageSizeLimitTestCase[] { // Single byte size should fail new MessageSizeLimitTestCase { MaxMessageSize = 1, AtomSizes = new RequestResponseSizes { RequestSize = -1, ResponseSize = 248 }, JsonLightSizes = new RequestResponseSizes { RequestSize = -1, ResponseSize = 185 }, }, // Small number should fail new MessageSizeLimitTestCase { MaxMessageSize = 20, AtomSizes = new RequestResponseSizes { RequestSize = -1, ResponseSize = 248 }, JsonLightSizes = new RequestResponseSizes { RequestSize = -1, ResponseSize = 185 }, }, // Large number should work new MessageSizeLimitTestCase { MaxMessageSize = 10000, }, // Default should work new MessageSizeLimitTestCase { MaxMessageSize = -1, }, }; this.RunAtomJsonMessageSizeLimitTests(model, payload, testCases, tc => tc.IsRequest); }
/// <summary> /// Creates a set of interesting service documents. /// </summary> /// <param name="withTitles">true if workspaces and collections should have a title; otherwise false.</param> /// <returns>List of interesting service documents.</returns> public static IEnumerable <ServiceDocumentInstance> CreateServiceDocuments(bool withTitles, string baseUri) { // empty yield return(PayloadBuilder.ServiceDocument().Workspace( PayloadBuilder.Workspace().WithTitle(withTitles ? "WorkspaceTitle" : null))); // one entity set yield return(PayloadBuilder.ServiceDocument().Workspace( PayloadBuilder.Workspace() .WithTitle(withTitles ? "WorkspaceTitle" : null) .ResourceCollection(withTitles ? "FirstCollectionTitle" : null, baseUri + "FirstCollection"))); // three entity sets yield return(PayloadBuilder.ServiceDocument().Workspace( PayloadBuilder.Workspace() .WithTitle(withTitles ? "WorkspaceTitle" : null) .ResourceCollection(withTitles ? "FirstCollectionTitle" : null, baseUri + "FirstCollection") .ResourceCollection(withTitles ? "SecondCollectionTitle" : null, baseUri + "SecondCollection") .ResourceCollection(withTitles ? "ThirdCollectionTitle" : null, baseUri + "ThirdCollection"))); }
public void ServiceDocumentReadingBaseUriTest() { EdmModel model = (EdmModel)Microsoft.Test.OData.Utils.Metadata.TestModels.BuildTestModel(); model.Fixup(); ServiceDocumentInstance serviceDocument = PayloadBuilder.ServiceDocument() .Workspace(PayloadBuilder.Workspace() .ResourceCollection(/*title*/ null, /*href*/ null)); // Run only on responses this.CombinatorialEngineProvider.RunCombinations( payloadUris.Where(uri => uri.OriginalString.Length != 0), baseUriValues, resolvers, this.ReaderTestConfigurationProvider.AtomFormatConfigurations.Where(tc => !tc.IsRequest), (payloadUri, baseUriValue, resolver, testConfiguration) => { Action <ServiceDocumentInstance, Uri, ReaderTestConfiguration> setCollectionHrefAction = (instance, uri, testConfig) => instance.Workspaces[0].ResourceCollections[0].Href = UriToString(uri); this.RunBaseUriReadingTest(serviceDocument, setCollectionHrefAction, model, payloadUri, baseUriValue, resolver, testConfiguration); }); }
public void ServiceDocumentReaderJsonLightUnrecognizedODataAnnotationsTest() { var testCases = new[] { new JsonLightServiceDocumentReaderTestCase { DebugDescription = "Unrecognized odata property annotation in a resource collection should be ignored.", Json = @"{ ""@" + JsonLightConstants.ODataContextAnnotationName + @""": ""http://odata.org/test/$metadata"", """ + JsonLightConstants.ODataValuePropertyName + @""": [ { ""*****@*****.**"":""fail"", ""name"":""EntitySetName"", ""url"":""EntitySetLink"" }, ] }", ExpectedException = null }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "Unrecognized odata instance annotation in a resource collection should be ignored.", Json = @"{ ""@" + JsonLightConstants.ODataContextAnnotationName + @""": ""http://odata.org/test/$metadata"", """ + JsonLightConstants.ODataValuePropertyName + @""": [ { ""@odata.foo"":""fail"", ""name"":""EntitySetName"", ""url"":""EntitySetLink"" }, ] }", ExpectedException = null }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "Unrecognized odata property annotation on 'value' should be ignored. ", Json = @"{ ""@" + JsonLightConstants.ODataContextAnnotationName + @""": ""http://odata.org/test/$metadata"", ""*****@*****.**"": ""fail"", """ + JsonLightConstants.ODataValuePropertyName + @""": [ { ""name"":""EntitySetName"", ""url"":""EntitySetLink"" }, ] }", ExpectedException = null }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "Unrecognized odata instance annotation on the top level should be ignored.", Json = @"{ ""@" + JsonLightConstants.ODataContextAnnotationName + @""": ""http://odata.org/test/$metadata"", ""@odata.foo"": ""fail"", """ + JsonLightConstants.ODataValuePropertyName + @""": [ { ""name"":""EntitySetName"", ""url"":""EntitySetLink"" }, ] }", ExpectedException = null }, }; var testDescriptors = testCases.Select(tc => new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.ServiceDocument().Workspace( PayloadBuilder.Workspace().ResourceCollection(null, baseUri + "EntitySetLink", "EntitySetName")) .JsonRepresentation(tc.Json), PayloadEdmModel = new EdmModel(), SkipTestConfiguration = testConfiguration => testConfiguration.IsRequest, ExpectedException = tc.ExpectedException, DebugDescription = tc.DebugDescription }); this.CombinatorialEngineProvider.RunCombinations( testDescriptors, this.ReaderTestConfigurationProvider.JsonLightFormatConfigurations, (testDescriptor, testConfiguration) => { // clone the ReaderTestConfiguration and set the base URI. testConfiguration = new ReaderTestConfiguration(testConfiguration); testConfiguration.MessageReaderSettings.BaseUri = null; // These descriptors are already tailored specifically for Json Light and // do not require normalization. testDescriptor.TestDescriptorNormalizers.Clear(); testDescriptor.RunTest(testConfiguration); }); }
public void ServiceDocumentReaderJsonLightTest() { var errorTestCases = new[] { new JsonLightServiceDocumentReaderTestCase { DebugDescription = "unrecognized property in the workspace object.", Json = "{ \"@" + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata\", \"unrecognized\": 42, \"value\": [ {\"name\": \"EntitySetName\", \"url\":\"EntitySetLink\"} ] }", ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightServiceDocumentDeserializer_UnexpectedPropertyInServiceDocument", "unrecognized", "value") }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "unrecognized property in a service document element.", Json = "{ \"@" + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata\", \"value\": [ {\"name\":\"EntitySetName\", \"url\":\"EntitySetLink\", \"unrecognized\": 42} ] }", ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightServiceDocumentDeserializer_UnexpectedPropertyInServiceDocumentElement", "unrecognized", "name", "url") }, #region missing required properties new JsonLightServiceDocumentReaderTestCase { DebugDescription = "service document element without a 'name' (required in JSON light, but doesn't exist in the other formats)", Json = "{ \"@" + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata\", \"value\": [ {\"url\":\"EntitySetLink\"} ] }", ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightServiceDocumentDeserializer_MissingRequiredPropertyInServiceDocumentElement", "name") }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "service document element without a 'url'", Json = "{ \"@" + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata\", \"value\": [ {\"name\":\"EntitySetName\"} ] }", ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightServiceDocumentDeserializer_MissingRequiredPropertyInServiceDocumentElement", "url") }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "null value for 'name'", Json = "{ \"@" + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata\", \"value\": [ {\"name\":null, \"url\":\"EntitySetLink\"} ] }", ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightServiceDocumentDeserializer_MissingRequiredPropertyInServiceDocumentElement", "name") }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "null value for 'url'", Json = "{ \"@" + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata\", \"value\": [ {\"name\":\"EntitySetName\", \"url\":null} ] }", ExpectedException = ODataExpectedExceptions.ODataException("ValidationUtils_ServiceDocumentElementUrlMustNotBeNull") }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "no 'value' property", Json = "{ \"@" + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata\" }", ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightServiceDocumentDeserializer_MissingValuePropertyInServiceDocument", "value") }, #endregion missing required properties #region duplicate properties new JsonLightServiceDocumentReaderTestCase { DebugDescription = "multiple 'value' properties", Json = "{ \"@" + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata\", \"value\": [ {\"name\": \"EntitySetName\", \"url\":\"EntitySetLink\"} ], \"value\": [ {\"name\": \"EntitySetName\", \"url\":\"EntitySetLink\"} ] }", ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightServiceDocumentDeserializer_DuplicatePropertiesInServiceDocument", "value") }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "multiple 'name' properties in an entity set.", Json = "{ \"@" + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata\", \"value\": [ {\"name\": \"EntitySetName\", \"url\":\"EntitySetLink\", \"name\": \"Second name\"}]}", ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightServiceDocumentDeserializer_DuplicatePropertiesInServiceDocumentElement", "name") }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "multiple 'url' properties in an entity set.", Json = "{ \"@" + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata\", \"value\": [ {\"name\": \"EntitySetName\", \"url\":\"EntitySetLink\", \"url\": \"SecondLink\"}]}", ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightServiceDocumentDeserializer_DuplicatePropertiesInServiceDocumentElement", "url") }, #endregion duplicate properties/annotations #region incorrect json structure new JsonLightServiceDocumentReaderTestCase { DebugDescription = "invalid top-level JSON node (an array)", Json = "[]", ExpectedException = ODataExpectedExceptions.ODataException("JsonReaderExtensions_UnexpectedNodeDetected", "StartObject", "StartArray") }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "invalid top-level JSON node (a primitive value)", Json = "42", ExpectedException = ODataExpectedExceptions.ODataException("JsonReaderExtensions_UnexpectedNodeDetected", "StartObject", "PrimitiveValue") }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "invalid value of 'value' property (a primitive value)", Json = "{ \"@" + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata\", \"value\": 42 }", ExpectedException = ODataExpectedExceptions.ODataException("JsonReaderExtensions_UnexpectedNodeDetected", "StartArray", "PrimitiveValue") }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "invalid value of 'value' property (an object value)", Json = "{ \"@" + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata\", \"value\": { } }", ExpectedException = ODataExpectedExceptions.ODataException("JsonReaderExtensions_UnexpectedNodeDetected", "StartArray", "StartObject") }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "invalid value inside the array value of the 'value' property (a nested array value)", Json = "{ \"@" + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata\", \"value\": [ [] ] }", ExpectedException = ODataExpectedExceptions.ODataException("JsonReaderExtensions_UnexpectedNodeDetected", "StartObject", "StartArray") }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "invalid value inside the array value of the 'value' property (integer value)", Json = "{ \"@" + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata\", \"value\": [ 42 ] }", ExpectedException = ODataExpectedExceptions.ODataException("JsonReaderExtensions_UnexpectedNodeDetected", "StartObject", "PrimitiveValue") }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "invalid value inside the array value of the 'value' property (boolean value)", Json = "{ \"@" + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata\", \"value\": [ true ] }", ExpectedException = ODataExpectedExceptions.ODataException("JsonReaderExtensions_UnexpectedNodeDetected", "StartObject", "PrimitiveValue") }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "invalid value inside the array value of the 'value' property (string value)", Json = "{ \"@" + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata\", \"value\": [ \"string\" ] }", ExpectedException = ODataExpectedExceptions.ODataException("JsonReaderExtensions_UnexpectedNodeDetected", "StartObject", "PrimitiveValue") }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "invalid value inside the array value of the 'value' property (null)", Json = "{ \"@" + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata\", \"value\": [ null ] }", ExpectedException = ODataExpectedExceptions.ODataException("JsonReaderExtensions_UnexpectedNodeDetected", "StartObject", "PrimitiveValue") }, #endregion incorrect json structure }; var testDescriptors = errorTestCases.Select(tc => new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.ServiceDocument().Workspace( PayloadBuilder.Workspace().ResourceCollection(null, baseUri + "EntitySetLink", "EntitySetName")) .JsonRepresentation(tc.Json), PayloadEdmModel = new EdmModel(), SkipTestConfiguration = testConfiguration => testConfiguration.IsRequest, ExpectedException = tc.ExpectedException, DebugDescription = tc.DebugDescription }); this.CombinatorialEngineProvider.RunCombinations( testDescriptors, this.ReaderTestConfigurationProvider.JsonLightFormatConfigurations, (testDescriptor, testConfiguration) => { // clone the ReaderTestConfiguration and set the base URI. testConfiguration = new ReaderTestConfiguration(testConfiguration); testConfiguration.MessageReaderSettings.BaseUri = null; // These descriptors are already tailored specifically for Json Light and // do not require normalization. testDescriptor.TestDescriptorNormalizers.Clear(); testDescriptor.RunTest(testConfiguration); }); }
public void ServiceDocumentReaderJsonLightCustomAnnotationsTest() { var testCases = new[] { #region success test cases new JsonLightServiceDocumentReaderTestCase { DebugDescription = "Custom property annotation on \"name\" should be ignored.", Json = @"{ ""@" + JsonLightConstants.ODataContextAnnotationName + @""": ""http://odata.org/test/$metadata"", """ + JsonLightConstants.ODataValuePropertyName + @""": [ { ""*****@*****.**"":""ignored"", ""name"":""EntitySetName"", ""url"":""EntitySetLink"" }, ] }", }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "Custom instance annotation inside a resource collection should be ignored.", Json = @"{ ""@" + JsonLightConstants.ODataContextAnnotationName + @""": ""http://odata.org/test/$metadata"", """ + JsonLightConstants.ODataValuePropertyName + @""": [ { ""@cn.foo"":""ignored"", ""name"":""EntitySetName"", ""url"":""EntitySetLink"" }, ] }", }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "Custom property annotation on \"value\" should be ignored.", Json = @"{ ""@" + JsonLightConstants.ODataContextAnnotationName + @""": ""http://odata.org/test/$metadata"", ""*****@*****.**"": ""ignored"", """ + JsonLightConstants.ODataValuePropertyName + @""": [ { ""name"":""EntitySetName"", ""url"":""EntitySetLink"" }, ] }", }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "Custom instance annotation on the top level should be ignored.", Json = @"{ ""@" + JsonLightConstants.ODataContextAnnotationName + @""": ""http://odata.org/test/$metadata"", ""@cn.foo"": ""ignored"", """ + JsonLightConstants.ODataValuePropertyName + @""": [ { ""name"":""EntitySetName"", ""url"":""EntitySetLink"" }, ] }", }, new JsonLightServiceDocumentReaderTestCase { DebugDescription = "Annotations at different scopes with the same name should be ignored and should not throw.", Json = @"{ ""@" + JsonLightConstants.ODataContextAnnotationName + @""": ""http://odata.org/test/$metadata"", ""@cn.foo"": ""ignored"", ""*****@*****.**"": ""ignored"", """ + JsonLightConstants.ODataValuePropertyName + @""": [ { ""@cn.foo"": ""ignored"", ""name"":""EntitySetName"", ""*****@*****.**"": ""ignored"", ""url"":""EntitySetLink"" }, ] }", }, #endregion success test cases #region error test cases new JsonLightServiceDocumentReaderTestCase { Json = @"{ ""@" + JsonLightConstants.ODataContextAnnotationName + @""": ""http://odata.org/test/$metadata"", """ + JsonLightConstants.ODataValuePropertyName + @""": [ { ""*****@*****.**"":""out of order"", ""url"":""EntitySetLink"", ""name"":""EntitySetName"" }, ] }", ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightServiceDocumentDeserializer_PropertyAnnotationWithoutProperty", "name") }, new JsonLightServiceDocumentReaderTestCase { Json = @"{ ""@" + JsonLightConstants.ODataContextAnnotationName + @""": ""http://odata.org/test/$metadata"", """ + JsonLightConstants.ODataValuePropertyName + @""": [ { ""url"":""EntitySetLink"", ""name"":""EntitySetName"" }, ], ""*****@*****.**"":""out of order"" }", ExpectedException = ODataExpectedExceptions.ODataException("PropertyAnnotationAfterTheProperty", "cn.foo", "value") }, new JsonLightServiceDocumentReaderTestCase { Json = @"{ ""@" + JsonLightConstants.ODataContextAnnotationName + @""": ""http://odata.org/test/$metadata"", """ + JsonLightConstants.ODataValuePropertyName + @""": [ { ""*****@*****.**"":""ignored"", ""*****@*****.**"":""ignored"", ""name"":""EntitySetName"", ""url"":""EntitySetLink"" }, ] }", ExpectedException = null }, new JsonLightServiceDocumentReaderTestCase { Json = @"{ ""@" + JsonLightConstants.ODataContextAnnotationName + @""": ""http://odata.org/test/$metadata"", """ + JsonLightConstants.ODataValuePropertyName + @""": [ { ""@cn.foo"":""ignored"", ""name"":""EntitySetName"", ""url"":""EntitySetLink"", ""@cn.foo"":""ignored"" }, ] }", ExpectedException = null }, new JsonLightServiceDocumentReaderTestCase { Json = @"{ ""@" + JsonLightConstants.ODataContextAnnotationName + @""": ""http://odata.org/test/$metadata"", ""*****@*****.**"":""ignored"", ""*****@*****.**"":""ignored"", """ + JsonLightConstants.ODataValuePropertyName + @""": [ { ""name"":""EntitySetName"", ""url"":""EntitySetLink"" }, ] }", ExpectedException = null }, new JsonLightServiceDocumentReaderTestCase { Json = @"{ ""@" + JsonLightConstants.ODataContextAnnotationName + @""": ""http://odata.org/test/$metadata"", ""@cn.foo"":""ignored"", ""@cn.foo"":""ignored"", """ + JsonLightConstants.ODataValuePropertyName + @""": [ { ""name"":""EntitySetName"", ""url"":""EntitySetLink"" }, ] }", ExpectedException = null }, #endregion error test cases }; var testDescriptors = testCases.Select(tc => new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.ServiceDocument().Workspace( PayloadBuilder.Workspace().ResourceCollection(null, baseUri + "EntitySetLink", "EntitySetName")) .JsonRepresentation(tc.Json), PayloadEdmModel = new EdmModel(), SkipTestConfiguration = testConfiguration => testConfiguration.IsRequest, DebugDescription = tc.DebugDescription, ExpectedException = tc.ExpectedException }); this.CombinatorialEngineProvider.RunCombinations( testDescriptors, this.ReaderTestConfigurationProvider.JsonLightFormatConfigurations, (testDescriptor, testConfiguration) => { // clone the ReaderTestConfiguration and set the base URI. testConfiguration = new ReaderTestConfiguration(testConfiguration); testConfiguration.MessageReaderSettings.BaseUri = null; // These descriptors are already tailored specifically for Json Light and // do not require normalization. testDescriptor.TestDescriptorNormalizers.Clear(); testDescriptor.RunTest(testConfiguration); }); }
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); }); }
public void NoMetadataTest() { EdmModel model = new EdmModel(); var container = new EdmEntityContainer("TestModel", "DefaultContainer"); model.AddElement(container); var testDescriptors = new[] { new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.EntitySet(), SkipTestConfiguration = tc => !tc.IsRequest, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightInputContext_ModelRequiredForReading"), }, new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.EntitySet(), SkipTestConfiguration = tc => tc.IsRequest, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightDeserializer_ContextLinkNotFoundAsFirstProperty"), }, new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.EntitySet(), PayloadEdmModel = model, SkipTestConfiguration = tc => !tc.IsRequest, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightInputContext_NoEntitySetForRequest"), }, new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.Entity(), SkipTestConfiguration = tc => !tc.IsRequest, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightInputContext_ModelRequiredForReading"), }, new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.Entity(), SkipTestConfiguration = tc => tc.IsRequest, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightDeserializer_ContextLinkNotFoundAsFirstProperty"), }, new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.Entity(), PayloadEdmModel = model, SkipTestConfiguration = tc => !tc.IsRequest, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightInputContext_NoEntitySetForRequest"), }, new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.PrimitiveProperty("propertyName", 42), SkipTestConfiguration = tc => !tc.IsRequest, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightInputContext_ModelRequiredForReading"), }, new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.Property("propertyName", PayloadBuilder.ComplexValue()), SkipTestConfiguration = tc => !tc.IsRequest, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightInputContext_ModelRequiredForReading"), }, new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.Property("propertyName", PayloadBuilder.PrimitiveMultiValue()), SkipTestConfiguration = tc => !tc.IsRequest, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightInputContext_ModelRequiredForReading"), }, new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.PrimitiveCollection(), SkipTestConfiguration = tc => !tc.IsRequest, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightInputContext_ModelRequiredForReading"), }, new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.PrimitiveCollection(), SkipTestConfiguration = tc => tc.IsRequest, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightDeserializer_ContextLinkNotFoundAsFirstProperty"), }, new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.PrimitiveCollection(), //PayloadModel = model, PayloadEdmModel = model, SkipTestConfiguration = tc => !tc.IsRequest, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightInputContext_ItemTypeRequiredForCollectionReaderInRequests"), }, new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.ComplexCollection(), SkipTestConfiguration = tc => !tc.IsRequest, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightInputContext_ModelRequiredForReading"), }, new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.ComplexCollection(), SkipTestConfiguration = tc => tc.IsRequest, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightDeserializer_ContextLinkNotFoundAsFirstProperty"), }, new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.ComplexCollection(), PayloadEdmModel = model, SkipTestConfiguration = tc => !tc.IsRequest, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightInputContext_ItemTypeRequiredForCollectionReaderInRequests"), }, new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.ServiceDocument().Workspace(PayloadBuilder.Workspace()), SkipTestConfiguration = tc => tc.IsRequest, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightDeserializer_ContextLinkNotFoundAsFirstProperty"), }, new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.DeferredLink("http://odata.org/deferred"), SkipTestConfiguration = tc => !tc.IsRequest, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightInputContext_ModelRequiredForReading"), }, new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.DeferredLink("http://odata.org/deferred"), SkipTestConfiguration = tc => tc.IsRequest, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightDeserializer_ContextLinkNotFoundAsFirstProperty"), }, new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.LinkCollection(), SkipTestConfiguration = tc => tc.IsRequest, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightDeserializer_ContextLinkNotFoundAsFirstProperty"), }, new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.ComplexValue(), SkipTestConfiguration = tc => !tc.IsRequest, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightInputContext_ModelRequiredForReading"), }, new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.ComplexValue(), PayloadEdmModel = model, SkipTestConfiguration = tc => !tc.IsRequest, ExpectedException = ODataExpectedExceptions.ArgumentNullException("ODataJsonLightInputContext_OperationCannotBeNullForCreateParameterReader", "operation") }, }; this.CombinatorialEngineProvider.RunCombinations( testDescriptors, this.ReaderTestConfigurationProvider.JsonLightFormatConfigurations, (testDescriptor, testConfiguration) => { // These descriptors are already tailored specifically for Json Light and // do not require normalization. testDescriptor.TestDescriptorNormalizers.Clear(); testDescriptor.RunTest(testConfiguration); }); }
public void TestInvalidUriInAtomMetadata() { // A string which breaks URI syntax, as defined by RFC 2396 const string invalidUri = "http://::"; // Other unimportant values to use for creating payloads to test. const string personName = "Name"; const string personEmail = "*****@*****.**"; const string collectionTitle = "Collection"; const string validUri = "http://odata.org"; const string generatorName = "Test Generator"; const string generatorVersion = "1.0"; const string nonOdataRelValue = "someRel"; const string linkType = "text/html"; // Create a test descriptor for each possible place the spec says a Uri can appear and is exposed as ATOM metadata. // Note: This excludes the two spots where the spec says the value is a Uri, but we expose it as a string: // scheme attributes of category and categories elements, and the id of the source feed of an entry. // This also excludes any Uri values that are always parsed, whether or not ATOM metadata reading is on, // such as the href attribute in a collection element. IEnumerable <PayloadReaderTestDescriptor> testDescriptors = new PayloadReaderTestDescriptor[] { #region uri attribute in a generator // atom:feed/atom:generator/@uri new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.EntitySet().AtomGenerator(generatorName, invalidUri, generatorVersion), }, // atom:entry/atom:source/atom:generator/@uri new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.Entity().AtomSource(PayloadBuilder.EntitySet().AtomGenerator(generatorName, invalidUri, generatorVersion)), }, #endregion uri attribute in a generator #region uri element in person constructs // atom:feed/atom:author/atom:uri new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.EntitySet().AtomAuthor(personName, invalidUri, personEmail), }, // atom:feed/atom:contributor/atom:uri new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.EntitySet().AtomContributor(personName, invalidUri, personEmail), }, // atom:entry/atom:author/atom:uri new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.Entity().AtomAuthor(personName, invalidUri, personEmail), }, // atom:entry/atom:contributor/atom:uri new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.Entity().AtomContributor(personName, invalidUri, personEmail), }, // atom:entry/atom:source/atom:author/atom:uri new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.Entity().AtomSource(PayloadBuilder.EntitySet().AtomAuthor(personName, invalidUri, personEmail)), }, // atom:entry/atom:source/atom:contributor/atom:uri new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.Entity().AtomSource(PayloadBuilder.EntitySet().AtomContributor(personName, invalidUri, personEmail)), }, #endregion uri element in person constructs #region content of icon element // atom:feed/atom:icon new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.EntitySet().AtomIcon(invalidUri), }, // atom:entry/atom:source/atom:icon new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.Entity().AtomSource(PayloadBuilder.EntitySet().AtomIcon(invalidUri)), }, #endregion content of icon element #region content of logo element // atom:feed/atom:logo new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.EntitySet().AtomLogo(invalidUri), }, // atom:entry/atom:source/atom:logo new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.Entity().AtomSource(PayloadBuilder.EntitySet().AtomLogo(invalidUri)), }, #endregion content of logo element #region href attribute in a link // atom:feed/atom:link/@href new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.EntitySet().AtomLink(invalidUri, nonOdataRelValue, linkType), }, // atom:entry/atom:link/@href new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.Entity().AtomLink(invalidUri, nonOdataRelValue, linkType), }, // atom:entry/atom:source/atom:link/@href new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.Entity().AtomSource(PayloadBuilder.EntitySet().AtomLink(invalidUri, nonOdataRelValue, linkType)), }, #endregion href attribute in a link #region href attribute in an out-of-line categories element // app:service/app:workspace/app:collection/app:categories/@href new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = PayloadBuilder.ServiceDocument() .Workspace(PayloadBuilder.Workspace() .ResourceCollection(PayloadBuilder.ResourceCollection(collectionTitle, validUri) .AppOutOfLineCategories(invalidUri))), SkipTestConfiguration = tc => tc.IsRequest, }, #endregion href attribute in an out-of-line categories element }; // Add the expected exception to all test descriptors. testDescriptors = testDescriptors.Select(td => new PayloadReaderTestDescriptor(td) { ExpectedException = new ExpectedException(typeof(System.UriFormatException)), }); // This method is set up so that expected exceptions are only expected when ATOM metadata reading is on. this.RunAtomMetadataReaderTests(testDescriptors); }
private static ODataPayloadElement CreatePayloadElement(IEdmModel model, ODataPayloadKind payloadKind, ReaderTestConfiguration testConfig) { IEdmEntitySet citySet = model.EntityContainer.FindEntitySet("Cities"); IEdmEntityType cityType = model.EntityTypes().Single(e => e.Name == "CityType"); IEdmProperty cityNameProperty = cityType.Properties().Single(e => e.Name == "Name"); IEdmNavigationProperty policeStationNavProp = cityType.NavigationProperties().Single(e => e.Name == "PoliceStation"); IEdmOperationImport primitiveCollectionResultOperation = model.EntityContainer.FindOperationImports("PrimitiveCollectionResultOperation").Single(); IEdmOperationImport serviceOp1 = model.EntityContainer.FindOperationImports("ServiceOperation1").Single(); bool isRequest = testConfig.IsRequest; bool isJsonLightRequest = isRequest && testConfig.Format == ODataFormat.Json; switch (payloadKind) { case ODataPayloadKind.Feed: { return(PayloadBuilder.EntitySet().WithTypeAnnotation(cityType).ExpectedEntityType(cityType, citySet)); } case ODataPayloadKind.Entry: { return(PayloadBuilder.Entity("TestModel.CityType").PrimitiveProperty("Id", 1).WithTypeAnnotation(cityType).ExpectedEntityType(cityType, citySet)); } case ODataPayloadKind.Property: return(PayloadBuilder.PrimitiveProperty(isJsonLightRequest ? string.Empty : null, "SomeCityValue").ExpectedProperty(cityType, "Name")); case ODataPayloadKind.EntityReferenceLink: return(PayloadBuilder.DeferredLink("http://odata.org/entityreferencelink").ExpectedNavigationProperty(citySet, cityType, "PoliceStation")); case ODataPayloadKind.EntityReferenceLinks: return(PayloadBuilder.LinkCollection().Item(PayloadBuilder.DeferredLink("http://odata.org/entityreferencelink")).ExpectedNavigationProperty((EdmEntitySet)citySet, (EdmEntityType)cityType, "CityHall")); case ODataPayloadKind.Value: return(PayloadBuilder.PrimitiveValue("PrimitiveValue")); case ODataPayloadKind.BinaryValue: return(PayloadBuilder.PrimitiveValue(new byte[] { 0, 0, 1, 1 })); case ODataPayloadKind.Collection: return(PayloadBuilder.PrimitiveCollection().CollectionName(null).ExpectedFunctionImport((EdmOperationImport)primitiveCollectionResultOperation)); case ODataPayloadKind.ServiceDocument: Debug.Assert(!isRequest, "Not supported in requests."); return(new ServiceDocumentInstance().Workspace(PayloadBuilder.Workspace())); case ODataPayloadKind.MetadataDocument: Debug.Assert(!isRequest, "Not supported in requests."); throw new NotImplementedException(); case ODataPayloadKind.Error: Debug.Assert(!isRequest, "Not supported in requests."); return(PayloadBuilder.Error("ErrorCode")); case ODataPayloadKind.Parameter: // build parameter payload based on model definition var parameterPayload = new ComplexInstance(null, false); ODataPayloadElement a = PayloadBuilder.PrimitiveValue(123).WithTypeAnnotation(EdmCoreModel.Instance.GetInt32(false)); ODataPayloadElement b = PayloadBuilder.PrimitiveValue("stringvalue").WithTypeAnnotation(EdmCoreModel.Instance.GetString(false)); PrimitiveProperty parametera = new PrimitiveProperty("a", "Edm.Integer", ((PrimitiveValue)a).ClrValue); PrimitiveProperty parameterb = new PrimitiveProperty("b", "Edm.String", ((PrimitiveValue)b).ClrValue); parameterPayload.Add(parametera); parameterPayload.Add(parameterb); parameterPayload.ExpectedFunctionImport((EdmOperationImport)serviceOp1); return(parameterPayload); case ODataPayloadKind.Unsupported: // fall through default: throw new NotSupportedException(); } }