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 EntityReferenceLinkReadingNullUriTest() { EdmModel model = (EdmModel)Microsoft.Test.OData.Utils.Metadata.TestModels.BuildTestModel(); model.Fixup(); DeferredLink link = PayloadBuilder.DeferredLink(/*uri*/ null); this.CombinatorialEngineProvider.RunCombinations( baseUriValues, this.ReaderTestConfigurationProvider.AtomFormatConfigurations.Where(tc => !tc.IsRequest), (baseUriValue, testConfiguration) => { // NOTE: In JSON, the value of the 'uri' property will be 'null' which is not allowed (and we expect an error) // In ATOM, the HREF of an entity reference link is stored as element content and will thus be read as // string.Empty even if the source link was null NullUriValueTestCase <DeferredLink> testCase = new NullUriValueTestCase <DeferredLink> { SetNullUriAction = (instance, uri, testConfig) => instance.UriString = null, SetExpectedUriAction = (instance, uri, testConfig) => instance.UriString = UriToString(uri), }; this.RunNullUriReadingTest(link, testCase, model, baseUriValue, testConfiguration); }); }
public void FeedReadingNullUriTest() { EdmModel model = (EdmModel)Microsoft.Test.OData.Utils.Metadata.TestModels.BuildTestModel(); model.Fixup(); EntitySetInstance feed = PayloadBuilder.EntitySet(); feed.NextLink = null; // Run only in version >= 2 since next links are only supported since v2 and not allowed in requests this.CombinatorialEngineProvider.RunCombinations( baseUriValues, this.ReaderTestConfigurationProvider.AtomFormatConfigurations.Where(tc => !tc.IsRequest), (baseUriValue, testConfiguration) => { // The next link should always be null independently of whether a base URI is specified or not NullUriValueTestCase <EntitySetInstance> testCase = new NullUriValueTestCase <EntitySetInstance> { SetNullUriAction = (instance, uri, testConfig) => instance.NextLink = null, SetExpectedUriAction = null // will use SetNullUriAction instead }; this.RunNullUriReadingTest(feed, testCase, model, baseUriValue, testConfiguration); }); }
/// <summary> /// Helper method to run a single iteration of the URI reading tests in a specified configuration. /// </summary> /// <typeparam name="T">The type of the payload to read.</typeparam> /// <param name="payloadElement">The payload to read.</param> /// <param name="setExpectedUriAction">An action to set the URI in question on the payload.</param> /// <param name="model">The metadata model.</param> /// <param name="baseUriValue">The base URI value for the current iteration.</param> /// <param name="testConfiguration">The reader test configuration.</param> private void RunNullUriReadingTest <T>( T payloadElement, NullUriValueTestCase <T> testCase, IEdmModel model, BaseUriValue baseUriValue, ReaderTestConfiguration testConfiguration) where T : ODataPayloadElement { this.Assert.IsNotNull(testCase.SetNullUriAction, "SetNullUriAction must not be null."); this.Assert.IsNull(testConfiguration.MessageReaderSettings.BaseUri, "No base URI expected on reader settings."); ExpectedException expectedException = null; Uri settingsBaseUri = baseUriValue.ReaderSettingBaseUri; // Set the base URI on the message reader settings if specified if (settingsBaseUri != null) { testConfiguration = new ReaderTestConfiguration(testConfiguration); testConfiguration.MessageReaderSettings.BaseUri = settingsBaseUri; } // Create the payload element T clonedPayloadElement = payloadElement.DeepCopy(); // modify the payload to represent the 'null' URI testCase.SetNullUriAction(clonedPayloadElement, /*uri*/ null, testConfiguration); var setExpectedUriAction = testCase.SetExpectedUriAction ?? testCase.SetNullUriAction; if (testConfiguration.Format == ODataFormat.Atom) { XElement xmlRepresentation = this.PayloadElementToXmlConverter.ConvertToXml(clonedPayloadElement); // add an xml:base attribute if specified Uri xmlBaseUri = baseUriValue.XmlBaseUri; if (xmlBaseUri != null) { xmlRepresentation.Add(new XAttribute(XNamespace.Xml.GetName("base"), xmlBaseUri.OriginalString)); } clonedPayloadElement.XmlRepresentation(xmlRepresentation); // First check all error conditions that are independent of a potential base URI if (payloadElement.ElementType == ODataPayloadElementType.ServiceDocumentInstance) { // Resource collections must have a URL expectedException = ODataExpectedExceptions.ODataException("ValidationUtils_ServiceDocumentElementUrlMustNotBeNull"); } else { if (xmlBaseUri != null) { setExpectedUriAction(clonedPayloadElement, xmlBaseUri, testConfiguration); } else if (settingsBaseUri != null) { setExpectedUriAction(clonedPayloadElement, settingsBaseUri, testConfiguration); } else { // fail for relative URIs without base URI if (payloadElement.ElementType == ODataPayloadElementType.DeferredLink || payloadElement.ElementType == ODataPayloadElementType.LinkCollection) { expectedException = ODataExpectedExceptions.ODataException("ODataAtomDeserializer_RelativeUriUsedWithoutBaseUriSpecified", string.Empty); } } // NOTE: ATOM properly detects MLEs even without read or edit link } } else { throw new NotSupportedException("Unsupported configuration format: " + testConfiguration.Format.ToString()); } PayloadReaderTestDescriptor testDescriptor = new PayloadReaderTestDescriptor(this.Settings) { PayloadElement = clonedPayloadElement, PayloadEdmModel = model, ExpectedException = expectedException, SkipTestConfiguration = tc => ODataPayloadElementConfigurationValidator.GetSkipTestConfiguration(payloadElement, ODataPayloadElementConfigurationValidator.AllValidators)(tc), }; testDescriptor.RunTest(testConfiguration); }
public void EntryReadingNullUriTest() { EdmModel model = (EdmModel)Microsoft.Test.OData.Utils.Metadata.TestModels.BuildTestModel(); model.Fixup(); NullUriValueTestCase <EntityInstance>[] testCases = new NullUriValueTestCase <EntityInstance>[] { // Setting the read link of an entry // NOTE: in JSON, the 'uri' property will be always omitted from __metadata when the read link is null (and no edit link exists); // in ATOM, the self link will be omitted from the payload new NullUriValueTestCase <EntityInstance> { SetNullUriAction = (instance, uri, testConfig) => instance.RemoveAnnotations(typeof(SelfLinkAnnotation)), }, // Setting the edit link of an entry // NOTE: in JSON, the 'uri' property will be always omitted from __metadata when the edit link is null (and no read link exists); // in ATOM, the link with the 'edit' rel will be omitted from the payload new NullUriValueTestCase <EntityInstance> { SetNullUriAction = (instance, uri, testConfig) => instance.WithEditLink(null), }, // Setting the navigation link of an entry // NOTE: in JSON, the 'uri' property in the __deferred object will have a 'null' value (and we expect an error) // in ATOM, the 'related' link for the navigation property will not have the 'href' property. new NullUriValueTestCase <EntityInstance> { SetNullUriAction = (instance, uri, testConfig) => ((NavigationPropertyInstance)instance.GetProperty("CityHall")).Value = new DeferredLink(), }, // Setting the association link of an entry // NOTE: in JSON, the associationuri property will have a 'null' value (and we expect an error) // in ATOM, the 'relatedlinks' link will not have the 'href' property new NullUriValueTestCase <EntityInstance> { SetNullUriAction = (instance, uri, testConfig) => ((NavigationPropertyInstance)instance.GetProperty("CityHall")).AssociationLink = null, }, // Setting the read link of a stream property // NOTE: in JSON, the 'media_src' property will be omitted from the __mediaresource object. // in ATOM, the 'mediaresource' link of the stream property will be omitted from the payload. new NullUriValueTestCase <EntityInstance> { SetNullUriAction = (instance, uri, testConfig) => ((NamedStreamInstance)instance.GetProperty("Skyline")).SourceLink = null, }, // Setting the edit link of a stream property // NOTE: in JSON, the 'edit_media' property will be omitted from the __mediaresource object. // in ATOM, the 'edit-media' link of the stream property will be omitted from the payload. new NullUriValueTestCase <EntityInstance> { SetNullUriAction = (instance, uri, testConfig) => ((NamedStreamInstance)instance.GetProperty("Skyline")).EditLink = null, }, // Setting the read link of the default stream // NOTE: in JSON, the 'media_src' property will be omitted from the __metadata object. // in ATOM, the <content> element will be omitted from the payload. new NullUriValueTestCase <EntityInstance> { SetNullUriAction = (instance, uri, testConfig) => instance.StreamSourceLink = null, }, // Setting the edit link of the default stream // NOTE: in JSON, the 'edit_media' property will be omitted from the __metadata object. // in ATOM, the edit-media link will be omitted from the payload. new NullUriValueTestCase <EntityInstance> { SetNullUriAction = (instance, uri, testConfig) => instance.StreamEditLink = null, }, }; EntityInstance entry = PayloadBuilder.Entity("TestModel.CityWithMapType") .PrimitiveProperty("Id", 1) .Property(PayloadBuilder.NavigationProperty("CityHall", /*link*/ "http://odata.org/nav-prop").IsCollection(true)) .StreamProperty("Skyline", /*readLink*/ "http://odata.org/stream-read", /*editLink*/ "http://odata.org/stream-edit") .AsMediaLinkEntry() .StreamSourceLink(/*link*/ "http://odata.org/media-read"); this.CombinatorialEngineProvider.RunCombinations( baseUriValues, testCases, this.ReaderTestConfigurationProvider.AtomFormatConfigurations, (baseUriValue, testCase, testConfiguration) => { this.RunNullUriReadingTest(entry, testCase, model, baseUriValue, testConfiguration); }); }