public void WriteTopLevelErrorWithCollectionOfComplexInstanceAnnotation() { var result = SetupSerializerAndRunTest(null, serializer => { ODataError error = new ODataError(); var instanceAnnotations = new Collection<ODataInstanceAnnotation>(); var collection = new ODataCollectionValue { TypeName = "Collection(ns.ErrorDetails)", Items = new[] { new ODataComplexValue(), new ODataComplexValue { TypeName = "ns.ErrorDetails" } } }; ODataInstanceAnnotation annotation = new ODataInstanceAnnotation("sample.collection", collection); instanceAnnotations.Add(annotation); error.InstanceAnnotations = instanceAnnotations; serializer.WriteTopLevelError(error, false); }); result.Should().Contain("\"[email protected]\":\"#Collection(ns.ErrorDetails)\",\"@sample.collection\":[{},{}]"); }
public void WriteTopLevelErrorWithStringInstanceAnnotationWithTypeNameAttribute() { var result = SetupSerializerAndRunTest(null, serializer => { ODataError error = new ODataError(); var instanceAnnotations = new Collection<ODataInstanceAnnotation>(); var primitiveValue = new ODataPrimitiveValue("stringValue"); primitiveValue.SetAnnotation(new SerializationTypeNameAnnotation() { TypeName = "Custom.Type" }); ODataInstanceAnnotation annotation = new ODataInstanceAnnotation("sample.primitive", primitiveValue); instanceAnnotations.Add(annotation); error.InstanceAnnotations = instanceAnnotations; serializer.WriteTopLevelError(error, false); }); result.Should().Contain("\"[email protected]\":\"#Custom.Type\",\"@sample.primitive\":\"stringValue\""); }
public void WriteTopLevelErrorWithComplexInstanceAnnotation() { var result = SetupSerializerAndRunTest(null, serializer => { ODataError error = new ODataError(); var instanceAnnotations = new Collection<ODataInstanceAnnotation>(); var complexValue = new ODataComplexValue(); complexValue.TypeName = "ns.ErrorDetails"; ODataInstanceAnnotation annotation = new ODataInstanceAnnotation("sample.complex", complexValue); instanceAnnotations.Add(annotation); error.InstanceAnnotations = instanceAnnotations; serializer.WriteTopLevelError(error, false); }); result.Should().Contain("\"@sample.complex\":{\"@odata.type\":\"#ns.ErrorDetails\"}"); }
public void WriteTopLevelErrorWithDateInstanceAnnotation() { var result = SetupSerializerAndRunTest(null, serializer => { ODataError error = new ODataError(); var instanceAnnotations = new Collection<ODataInstanceAnnotation>(); var primitiveValue = new ODataPrimitiveValue(new Date(2014, 8, 8)); ODataInstanceAnnotation annotation = new ODataInstanceAnnotation("sample.primitive", primitiveValue); instanceAnnotations.Add(annotation); error.InstanceAnnotations = instanceAnnotations; serializer.WriteTopLevelError(error, false); }); result.Should().Contain("\"[email protected]\":\"#Date\",\"@sample.primitive\":\"2014-08-08\""); }
public void WriteTopLevelErrorWithTimeOfDayInstanceAnnotation() { var result = SetupSerializerAndRunTest(null, serializer => { ODataError error = new ODataError(); var instanceAnnotations = new Collection<ODataInstanceAnnotation>(); var primitiveValue = new ODataPrimitiveValue(new TimeOfDay(12, 30, 5, 90)); ODataInstanceAnnotation annotation = new ODataInstanceAnnotation("sample.primitive", primitiveValue); instanceAnnotations.Add(annotation); error.InstanceAnnotations = instanceAnnotations; serializer.WriteTopLevelError(error, false); }); result.Should().Contain("\"[email protected]\":\"#TimeOfDay\",\"@sample.primitive\":\"12:30:05.0900000\""); }
public void ComplexTypeInstanceAnnotation() { ODataMessageReaderSettings readerSettings = new ODataMessageReaderSettings() { BaseUri = ServiceBaseUri }; foreach (var mimeType in TestMimeTypes) { var requestMessage = new HttpWebRequestMessage(new Uri(ServiceBaseUri.AbsoluteUri + "People(1)", UriKind.Absolute)); requestMessage.SetHeader("Accept", mimeType); requestMessage.SetHeader("Prefer", string.Format("{0}={1}", IncludeAnnotation, "*")); var responseMessage = requestMessage.GetResponse(); Assert.Equal(200, responseMessage.StatusCode); if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata)) { using (var messageReader = new ODataMessageReader(responseMessage, readerSettings, Model)) { var reader = messageReader.CreateODataResourceReader(); ODataResource entry = null; bool startHomeAddress = false; while (reader.Read()) { if (reader.State == ODataReaderState.NestedResourceInfoStart) { ODataNestedResourceInfo navigation = reader.Item as ODataNestedResourceInfo; if (navigation != null && navigation.Name == "HomeAddress") { startHomeAddress = true; } } else if (reader.State == ODataReaderState.NestedResourceInfoEnd) { ODataNestedResourceInfo navigation = reader.Item as ODataNestedResourceInfo; if (navigation != null && navigation.Name == "HomeAddress") { startHomeAddress = false; } } else if (reader.State == ODataReaderState.ResourceEnd) { entry = reader.Item as ODataResource; if (startHomeAddress) { // Verify Annotation on Complex Type ODataInstanceAnnotation annotationOnHomeAddress = entry.InstanceAnnotations.Last(); Assert.Equal(string.Format("{0}.AddressType", TestModelNameSpace), annotationOnHomeAddress.Name); Assert.Equal("Home", (annotationOnHomeAddress.Value as ODataPrimitiveValue).Value); // TODO : Fix #625 //// Verify Annotation on Property in Complex Type //ODataInstanceAnnotation annotationOnCity = entry.Properties.SingleOrDefault(p => p.Name.Equals("City")).InstanceAnnotations.SingleOrDefault(); //Assert.Equal(string.Format("{0}.CityInfo", TestModelNameSpace), annotationOnCity.Name); //Assert.Equal(2, (annotationOnCity.Value as ODataComplexValue).Properties.Count()); } } } // Verify Annotation on Property of Entity ODataInstanceAnnotation annotationonEmails = entry.Properties.SingleOrDefault(p => p.Name.Equals("Emails")).InstanceAnnotations.SingleOrDefault(); Assert.Equal(string.Format("{0}.DisplayName", TestModelNameSpace), annotationonEmails.Name); Assert.Equal("EmailAddresses", (annotationonEmails.Value as ODataPrimitiveValue).Value); Assert.Equal(ODataReaderState.Completed, reader.State); } } } }
public void WriteTopLevelErrorWithDateTimeInstanceAnnotation() { var result = SetupSerializerAndRunTest(null, serializer => { ODataError error = new ODataError(); var instanceAnnotations = new Collection<ODataInstanceAnnotation>(); var primitiveValue = new ODataPrimitiveValue(new DateTimeOffset(2000, 1, 1, 12, 30, 0, new TimeSpan())); ODataInstanceAnnotation annotation = new ODataInstanceAnnotation("sample.primitive", primitiveValue); instanceAnnotations.Add(annotation); error.InstanceAnnotations = instanceAnnotations; serializer.WriteTopLevelError(error, false); }); result.Should().Contain("\"[email protected]\":\"#DateTimeOffset\",\"@sample.primitive\":\"2000-01-01T12:30:00Z\""); }
/// <summary> /// Creates a new instance of this class by consuming xml from the given reader. /// Creates an Atom-specific instance annotation from the format-independent representation of an annotation. /// </summary> /// <param name="odataInstanceAnnotation">The format-independent represetnation of an instance annotation.</param> /// <param name="target">The value of the target attribute on the m:annotation element, or null if the attribute should be omitted.</param> /// <returns>The created AtomInstanceAnnotation.</returns> internal static AtomInstanceAnnotation CreateFrom(ODataInstanceAnnotation odataInstanceAnnotation, string target) { Debug.Assert(odataInstanceAnnotation != null, "odataInstanceAnnotation != null"); return(new AtomInstanceAnnotation(target, odataInstanceAnnotation.Name, odataInstanceAnnotation.Value)); }
public void GetInstanceAnnotationsForWritingShouldReturnInstanceAnnotationsFromODataErrorWithInstanceAnnotations() { ODataInstanceAnnotation instanceAnnotation = new ODataInstanceAnnotation("namespace.name", new ODataPrimitiveValue("value")); this.odataError.InstanceAnnotations.Add(instanceAnnotation); this.odataError.InstanceAnnotations.Should().Contain(instanceAnnotation).And.HaveCount(1); }
public void TheNamePropertyShouldReturnTheAnnotationName() { const string name = "instance.annotation"; var annotation = new ODataInstanceAnnotation(name, new ODataPrimitiveValue("value")); annotation.Name.Should().Be(name); }
public void TheValuePropertyShouldReturnTheAnnotationValue() { foreach(ODataValue value in new ODataValue[] {new ODataNullValue(), new ODataPrimitiveValue(1), new ODataComplexValue(), new ODataCollectionValue()}) { var annotation = new ODataInstanceAnnotation("namespace.name", value); annotation.Value.Should().BeSameAs(value); } }
/// <summary> /// Convert an instance annotation to clr object. /// </summary> /// <param name="instanceAnnotation">Instance annotation to be converted</param> /// <param name="clrInstanceAnnotation">The clr object</param> /// <returns>A dictionary of clr-typed instance annotation</returns> private bool TryConvertToClrInstanceAnnotation(ODataInstanceAnnotation instanceAnnotation, out object clrInstanceAnnotation) { clrInstanceAnnotation = null; var primitiveValue = instanceAnnotation.Value as ODataPrimitiveValue; if (primitiveValue != null) { clrInstanceAnnotation = primitiveValue.Value; return(true); } var enumValue = instanceAnnotation.Value as ODataEnumValue; if (enumValue != null) { var type = this.MaterializerContext.Context.ResolveTypeFromName(enumValue.TypeName); if (type != null) { clrInstanceAnnotation = EnumValueMaterializationPolicy.MaterializeODataEnumValue(type, enumValue); return(true); } return(false); } var collectionValue = instanceAnnotation.Value as ODataCollectionValue; if (collectionValue != null) { var serverSideModel = this.MaterializerContext.Context.Format.LoadServiceModel(); var valueTerm = serverSideModel.FindTerm(instanceAnnotation.Name); if (valueTerm != null && valueTerm.Type != null && valueTerm.Type.Definition != null) { var edmCollectionType = valueTerm.Type.Definition as IEdmCollectionType; if (edmCollectionType != null) { Type collectionItemType = null; var elementType = edmCollectionType.ElementType; PrimitiveType primitiveType; if (PrimitiveType.TryGetPrimitiveType(elementType.FullName(), out primitiveType)) { collectionItemType = primitiveType.ClrType; } else { collectionItemType = this.MaterializerContext.Context.ResolveTypeFromName(elementType.FullName()); } if (collectionItemType != null) { Type collectionICollectionType = typeof(ICollection <>).MakeGenericType(new Type[] { collectionItemType }); ClientTypeAnnotation collectionClientTypeAnnotation = this.MaterializerContext.ResolveTypeForMaterialization( collectionICollectionType, collectionValue.TypeName); bool isElementNullable = edmCollectionType.ElementType.IsNullable; var collectionInstance = this.CollectionValueMaterializationPolicy.CreateCollectionInstance( collectionClientTypeAnnotation.EdmTypeReference as IEdmCollectionTypeReference, collectionClientTypeAnnotation.ElementType); this.CollectionValueMaterializationPolicy.ApplyCollectionDataValues( collectionValue.Items, collectionValue.TypeName, collectionInstance, collectionItemType, ClientTypeUtil.GetAddToCollectionDelegate(collectionICollectionType), isElementNullable); clrInstanceAnnotation = collectionInstance; return(true); } } } return(false); } var nullValue = instanceAnnotation.Value as ODataNullValue; if (nullValue != null) { clrInstanceAnnotation = null; return(true); } return(false); }
private static void AddInstanceAnnotationToContainer(IODataInstanceAnnotationContainer instanceAnnotationContainer, ODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext, ODataInstanceAnnotation annotation, string propertyName) { IEdmTypeReference propertyType = null; object annotationValue = ConvertAnnotationValue(annotation.Value, ref propertyType, deserializerProvider, readContext); if (string.IsNullOrEmpty(propertyName)) { instanceAnnotationContainer.AddResourceAnnotation(annotation.Name, annotationValue); } else { instanceAnnotationContainer.AddPropertyAnnotation(propertyName, annotation.Name, annotationValue); } }
/// <summary> /// Creates a new instance of this class by consuming xml from the given reader. /// Creates an Atom-specific instance annotation from the format-independent representation of an annotation. /// </summary> /// <param name="odataInstanceAnnotation">The format-independent represetnation of an instance annotation.</param> /// <param name="target">The value of the target attribute on the m:annotation element, or null if the attribute should be omitted.</param> /// <returns>The created AtomInstanceAnnotation.</returns> internal static AtomInstanceAnnotation CreateFrom(ODataInstanceAnnotation odataInstanceAnnotation, string target) { Debug.Assert(odataInstanceAnnotation != null, "odataInstanceAnnotation != null"); return new AtomInstanceAnnotation(target, odataInstanceAnnotation.Name, odataInstanceAnnotation.Value); }
public void WriteTopLevelErrorWithCollectionOfComplexInstanceAnnotationWithNoTypeNameShouldThrow() { SetupSerializerAndRunTest(null, serializer => { ODataError error = new ODataError(); var instanceAnnotations = new Collection<ODataInstanceAnnotation>(); var collection = new ODataCollectionValue { Items = new[] { new ODataComplexValue(), new ODataComplexValue { TypeName = "ns.ErrorDetails" } } }; ODataInstanceAnnotation annotation = new ODataInstanceAnnotation("sample.collection", collection); instanceAnnotations.Add(annotation); error.InstanceAnnotations = instanceAnnotations; Action writeError = () => serializer.WriteTopLevelError(error, false); writeError.ShouldThrow<ODataException>().WithMessage(Strings.WriterValidationUtils_MissingTypeNameWithMetadata); }); }
public void WriteTopLevelErrorWithStringInstanceAnnotation() { var result = SetupSerializerAndRunTest(null, serializer => { ODataError error = new ODataError(); var instanceAnnotations = new Collection<ODataInstanceAnnotation>(); ODataInstanceAnnotation annotation = new ODataInstanceAnnotation("sample.primitive", new ODataPrimitiveValue("stringValue")); instanceAnnotations.Add(annotation); error.InstanceAnnotations = instanceAnnotations; serializer.WriteTopLevelError(error, false); }); result.Should().Contain("\"@sample.primitive\":\"stringValue\""); }
/// <summary> /// Writes an ODataError with the given custom instance annotation to the test stream. /// </summary> private void WriteError(params KeyValuePair<string, ODataValue>[] annotations) { var writerSettings = new ODataMessageWriterSettings { DisableMessageStreamDisposal = true }; writerSettings.SetContentType(ODataFormat.Json); writerSettings.SetServiceDocumentUri(new Uri("http://example.com/")); IODataResponseMessage messageToWrite = new InMemoryMessage { StatusCode = 400, Stream = this.stream }; var error = new ODataError(); var instanceAnnotations = new Collection<ODataInstanceAnnotation>(); error.SetInstanceAnnotations(instanceAnnotations); foreach (var pair in annotations) { ODataInstanceAnnotation annotation = new ODataInstanceAnnotation(pair.Key, pair.Value); instanceAnnotations.Add(annotation); } using (var writer = new ODataMessageWriter(messageToWrite, writerSettings, this.model)) { writer.WriteError(error, false); } }
public void EntityReferenceLinksTest() { string resultUri1String = "http://odata.org/linkresult1"; string resultUri2String = "linkresult2"; string resultUri3String = "http://odata.org/linkresult3"; ODataEntityReferenceLink entityReferenceLink1 = new ODataEntityReferenceLink { Url = new Uri(resultUri1String) }; ODataEntityReferenceLink entityReferenceLink2 = new ODataEntityReferenceLink { Url = new Uri(resultUri2String, UriKind.Relative) }; ODataEntityReferenceLink entityReferenceLink3 = new ODataEntityReferenceLink { Url = new Uri(resultUri3String) }; entityReferenceLink3.InstanceAnnotations.Add(new ODataInstanceAnnotation("TestModel.unknown", new ODataPrimitiveValue(123))); entityReferenceLink3.InstanceAnnotations.Add(new ODataInstanceAnnotation("custom.name", new ODataPrimitiveValue(456))); Uri nextPageLink = new Uri("http://odata.org/nextpage"); Uri relativeNextPageLink = new Uri("relative-nextpage", UriKind.Relative); long?[] inputCounts = new long?[] { null, 1, 3, -1, -3, 0, long.MaxValue, long.MinValue }; Uri[] inputNextLinks = new Uri[] { nextPageLink, relativeNextPageLink, null }; ODataInstanceAnnotation[][] inputAnnotations = new ODataInstanceAnnotation[][] { new ODataInstanceAnnotation[0], new ODataInstanceAnnotation[] { new ODataInstanceAnnotation("TestModel.annotation", new ODataPrimitiveValue(321)), new ODataInstanceAnnotation("custom.annotation", new ODataPrimitiveValue(654)) } }; ODataEntityReferenceLink[][] inputReferenceLinks = new ODataEntityReferenceLink[][] { new ODataEntityReferenceLink[] { entityReferenceLink1, entityReferenceLink2, entityReferenceLink3 }, new ODataEntityReferenceLink[] { entityReferenceLink1, entityReferenceLink3 }, new ODataEntityReferenceLink[] { entityReferenceLink1 }, new ODataEntityReferenceLink[0], null }; var testCases = inputCounts.SelectMany( inputCount => inputNextLinks.SelectMany( inputNextLink => inputReferenceLinks.Select( (inputReferenceLink, index) => new ODataEntityReferenceLinks { Count = inputCount, Links = inputReferenceLink, NextPageLink = inputNextLink, InstanceAnnotations = inputAnnotations[index == 1 ? 1 : 0] }))); var testDescriptors = testCases.Select( testCase => new PayloadWriterTestDescriptor <ODataEntityReferenceLinks>(this.Settings, testCase, this.CreateExpectedCallback(testCase, /*forceNextLinkAndCountAtEnd*/ false))); // TODO: also cover the cases in protocol v1 (errors for inline count and next page link; different format for JSON) this.CombinatorialEngineProvider.RunCombinations( testDescriptors, this.WriterTestConfigurationProvider.ExplicitFormatConfigurationsWithIndent, (testDescriptor, testConfiguration) => { ODataEntityReferenceLinks entityReferenceLinks = testDescriptor.PayloadItems.Single(); testConfiguration = testConfiguration.Clone(); testConfiguration.MessageWriterSettings.SetServiceDocumentUri(ServiceDocumentUri); if (!testConfiguration.IsRequest) { testConfiguration.MessageWriterSettings.ShouldIncludeAnnotation = ODataUtils.CreateAnnotationFilter("*"); } // When writing JSON lite, always provide a model and a non-null nav prop. // The error cases when a model isn't provided or the nav prop is null are tested in JsonLightEntityReferenceLinkWriterTests if (testConfiguration.Format == ODataFormat.Json) { testDescriptor.Model = CreateModelWithNavProps(); var edmModel = testDescriptor.GetMetadataProvider(); } TestWriterUtils.WriteAndVerifyTopLevelContent( testDescriptor, testConfiguration, (messageWriter) => messageWriter.WriteEntityReferenceLinks(entityReferenceLinks), this.Assert, baselineLogger: this.Logger); }); }