public async Task ListOfDatesSerializesAsOData() { // Arrange const string expect = "{" + "\"@odata.context\":\"http://localhost/$metadata#Collection(Edm.Date)\",\"value\":[" + "\"0001-01-01\",\"2015-02-26\",\"9999-12-31\"" + "]" + "}"; List <Date> listOfDates = new List <Date>(); listOfDates.Add(Date.MinValue); listOfDates.Add(new Date(2015, 2, 26)); listOfDates.Add(Date.MaxValue); var routeName = "OData"; var model = GetSampleModel(); var config = RoutingConfigurationFactory.CreateWithRootContainer(routeName, b => b.AddService(ServiceLifetime.Singleton, s => model)); var request = RequestFactory.Create(HttpMethod.Get, "http://localhost/property", config, routeName); var payload = new ODataPayloadKind[] { ODataPayloadKind.Collection }; var formatter = FormatterTestHelper.GetFormatter(payload, request); var content = FormatterTestHelper.GetContent(listOfDates, formatter, ODataMediaTypes.ApplicationJsonODataMinimalMetadata); // Act & Assert JsonAssert.Equal(expect, await FormatterTestHelper.GetContentResult(content, request)); }
public async Task ListOfNullableDateTimeSerializesAsOData() { // Arrange DateTime dt1 = new DateTime(1978, 11, 15, 01, 12, 13, DateTimeKind.Local); DateTime dt2 = new DateTime(2014, 10, 27, 12, 25, 26, DateTimeKind.Local); List <DateTime?> listOfDateTime = new List <DateTime?> { dt1, null, dt2 }; var routeName = "OData"; var model = GetSampleModel(); var config = RoutingConfigurationFactory.CreateWithRootContainer(routeName, b => b.AddService(ServiceLifetime.Singleton, s => model)); var request = RequestFactory.Create(HttpMethod.Get, "http://localhost/property", config, routeName); var payload = new ODataPayloadKind[] { ODataPayloadKind.Collection }; var formatter = FormatterTestHelper.GetFormatter(payload, request); var content = FormatterTestHelper.GetContent(listOfDateTime, formatter, ODataMediaTypes.ApplicationJsonODataMinimalMetadata); // Act & Assert dynamic result = JObject.Parse(await FormatterTestHelper.GetContentResult(content, request)); Assert.Equal(3, result["value"].Count); DateTimeOffset?dto = (DateTimeOffset?)result["value"][0]; Assert.Equal(new DateTimeOffset(dt1), dto.Value); dto = (DateTimeOffset?)result["value"][1]; Assert.Null(dto); dto = (DateTimeOffset?)result["value"][2]; Assert.Equal(new DateTimeOffset(dt2), dto.Value); }
public async Task ListOfDateTimeSerializesAsOData_CustomTimeZone() { // Arrange const string expect = "{" + "\"@odata.context\":\"http://localhost/$metadata#Collection(Edm.DateTimeOffset)\",\"value\":[" + "\"1978-11-14T17:12:13-08:00\",\"2014-10-27T04:25:26-08:00\"" + "]" + "}"; List <DateTime> listOfDateTime = new List <DateTime>(); listOfDateTime.Add(new DateTime(1978, 11, 15, 01, 12, 13, DateTimeKind.Utc)); listOfDateTime.Add(new DateTime(2014, 10, 27, 12, 25, 26, DateTimeKind.Utc)); var routeName = "OData"; var model = GetSampleModel(); var config = RoutingConfigurationFactory.CreateWithRootContainer(routeName, b => b.AddService(ServiceLifetime.Singleton, s => model)); config.SetTimeZoneInfo(TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time")); var request = RequestFactory.Create(HttpMethod.Get, "http://localhost/property", config, routeName); var payload = new ODataPayloadKind[] { ODataPayloadKind.Collection }; var formatter = FormatterTestHelper.GetFormatter(payload, request); var content = FormatterTestHelper.GetContent(listOfDateTime, formatter, ODataMediaTypes.ApplicationJsonODataMinimalMetadata); // Act & Assert JsonAssert.Equal(expect, await FormatterTestHelper.GetContentResult(content, request)); }
public async Task ListOfNullableTimeOfDaysSerializesAsOData() { // Arrange const string expect = "{" + "\"@odata.context\":\"http://localhost/$metadata#Collection(Edm.TimeOfDay)\",\"value\":[" + "\"00:00:00.0000000\",\"01:02:03.0040000\",null,\"23:59:59.9999999\"" + "]" + "}"; List <TimeOfDay?> listOfDates = new List <TimeOfDay?>(); listOfDates.Add(TimeOfDay.MinValue); listOfDates.Add(new TimeOfDay(1, 2, 3, 4)); listOfDates.Add(null); listOfDates.Add(TimeOfDay.MaxValue); var routeName = "OData"; var model = GetSampleModel(); var config = RoutingConfigurationFactory.CreateWithRootContainer(routeName, b => b.AddService(ServiceLifetime.Singleton, s => model)); var request = RequestFactory.Create(HttpMethod.Get, "http://localhost/property", config, routeName); var payload = new ODataPayloadKind[] { ODataPayloadKind.Collection }; var formatter = FormatterTestHelper.GetFormatter(payload, request); var content = FormatterTestHelper.GetContent(listOfDates, formatter, ODataMediaTypes.ApplicationJsonODataMinimalMetadata); // Act & Assert JsonAssert.Equal(expect, await FormatterTestHelper.GetContentResult(content, request)); }
public async Task PrimitiveTypesSerializeAsOData(Type valueType, object value, string mediaType, string resourceName) { // Arrange string expectedEntity = Resources.GetString(resourceName); Assert.NotNull(expectedEntity); // Guard ODataConventionModelBuilder modelBuilder = ODataConventionModelBuilderFactory.Create(); modelBuilder.EntitySet <WorkItem>("WorkItems"); IEdmModel model = modelBuilder.GetEdmModel(); IEdmProperty property = model.EntityContainer.EntitySets().Single().EntityType().Properties().First(); ODataPath path = new ODataPath(new PropertySegment(property as IEdmStructuralProperty)); var configuration = RoutingConfigurationFactory.CreateWithRootContainer("OData"); var request = RequestFactory.Create(HttpMethod.Get, "http://localhost/WorkItems(10)/ID", configuration, "OData", path); var formatter = FormatterTestHelper.GetFormatter(new ODataPayloadKind[] { ODataPayloadKind.Property }, request, mediaType); // Act Type type = (value != null) ? value.GetType() : typeof(Nullable <int>); var content = FormatterTestHelper.GetContent(value, type, formatter, mediaType.ToString()); string actualEntity = await FormatterTestHelper.GetContentResult(content, request); // Assert Assert.NotNull(valueType); JsonAssert.Equal(expectedEntity, actualEntity); }
public async Task ArrayOfBooleansSerializesAsOData() { // Arrange var routeName = "OData"; var model = GetSampleModel(); var config = RoutingConfigurationFactory.CreateWithRootContainer(routeName, b => b.AddService(ServiceLifetime.Singleton, s => model)); var request = RequestFactory.Create(HttpMethod.Get, "http://localhost/property", config, routeName); var payload = new ODataPayloadKind[] { ODataPayloadKind.Collection }; var formatter = FormatterTestHelper.GetFormatter(payload, request); var content = FormatterTestHelper.GetContent(new bool[] { true, false, true, false }, formatter, ODataMediaTypes.ApplicationJsonODataMinimalMetadata); // Act & Assert JsonAssert.Equal(Resources.ArrayOfBoolean, await FormatterTestHelper.GetContentResult(content, request)); }
public async Task ComplexTypeSerializesAsOData() { // Arrange string routeName = "OData"; IEdmModel model = GetSampleModel(); var config = RoutingConfigurationFactory.Create(); config = RoutingConfigurationFactory.CreateWithRootContainer(routeName, b => b.AddService(ServiceLifetime.Singleton, s => model)); var request = RequestFactory.Create(HttpMethod.Get, "http://localhost/property", config, routeName); var payload = new ODataPayloadKind[] { ODataPayloadKind.Resource }; var formatter = FormatterTestHelper.GetFormatter(payload, request); var content = FormatterTestHelper.GetContent(new Person(0, new ReferenceDepthContext(7)), formatter, ODataMediaTypes.ApplicationJsonODataMinimalMetadata); // Act & Assert JsonAssert.Equal(Resources.PersonComplexType, await FormatterTestHelper.GetContentResult(content, request)); }
public async Task EntityTypeSerializesAsODataEntry() { // Arrange const string routeName = "OData"; IEdmEntitySet entitySet = _model.EntityContainer.FindEntitySet("employees"); ODataPath path = new ODataPath(new EntitySetSegment(entitySet)); var config = RoutingConfigurationFactory.CreateWithRootContainer(routeName, b => b.AddService(ServiceLifetime.Singleton, s => _model)); var request = RequestFactory.Create(HttpMethod.Get, "http://localhost/property", config, routeName, path); var payload = new ODataPayloadKind[] { ODataPayloadKind.Resource }; var formatter = FormatterTestHelper.GetFormatter(payload, request); Employee employee = (Employee)TypeInitializer.GetInstance(SupportedTypes.Employee); var content = FormatterTestHelper.GetContent(employee, formatter, ODataMediaTypes.ApplicationJsonODataMinimalMetadata); // Act & Assert JsonAssert.Equal(Resources.EmployeeEntry, await FormatterTestHelper.GetContentResult(content, request)); }
public async Task ListOfStringsSerializesAsOData() { // Arrange List <string> listOfStrings = new List <string>(); listOfStrings.Add("Frank"); listOfStrings.Add("Steve"); listOfStrings.Add("Tom"); listOfStrings.Add("Chandler"); var routeName = "OData"; var model = GetSampleModel(); var config = RoutingConfigurationFactory.CreateWithRootContainer(routeName, b => b.AddService(ServiceLifetime.Singleton, s => model)); var request = RequestFactory.Create(HttpMethod.Get, "http://localhost/property", config, routeName); var payload = new ODataPayloadKind[] { ODataPayloadKind.Collection }; var formatter = FormatterTestHelper.GetFormatter(payload, request); var content = FormatterTestHelper.GetContent(listOfStrings, formatter, ODataMediaTypes.ApplicationJsonODataMinimalMetadata); // Act & Assert JsonAssert.Equal(Resources.ListOfString, await FormatterTestHelper.GetContentResult(content, request)); }
public async Task IEnumerableOfEntityTypeSerializesAsODataResourceSet() { // Arrange IEdmEntitySet entitySet = _model.EntityContainer.FindEntitySet("employees"); ODataPath path = new ODataPath(new EntitySetSegment(entitySet)); var request = RequestFactory.CreateFromModel(_model, "http://localhost/property", "Route", path); var payload = new ODataPayloadKind[] { ODataPayloadKind.ResourceSet }; var formatter = FormatterTestHelper.GetFormatter(payload, request); IEnumerable <Employee> collectionOfPerson = new Collection <Employee>() { (Employee)TypeInitializer.GetInstance(SupportedTypes.Employee, 0), (Employee)TypeInitializer.GetInstance(SupportedTypes.Employee, 1), }; var content = FormatterTestHelper.GetContent(collectionOfPerson, formatter, ODataMediaTypes.ApplicationJsonODataMinimalMetadata); // Act & Assert JsonAssert.Equal(Resources.FeedOfEmployee, await FormatterTestHelper.GetContentResult(content, request)); }
public async Task NullPrimitiveValueSerializeAsODataThrows(Type valueType, object value, string mediaType, string unused) { // Arrange Assert.NotNull(valueType); Assert.NotNull(unused); ODataConventionModelBuilder modelBuilder = ODataConventionModelBuilderFactory.Create(); modelBuilder.EntitySet <WorkItem>("WorkItems"); IEdmModel model = modelBuilder.GetEdmModel(); IEdmProperty property = model.EntityContainer.EntitySets().Single().EntityType().Properties().First(); ODataPath path = new ODataPath(new PropertySegment(property as IEdmStructuralProperty)); var configuration = RoutingConfigurationFactory.CreateWithRootContainer("OData"); var request = RequestFactory.Create(HttpMethod.Get, "http://localhost/WorkItems(10)/ID", configuration, "OData", path); var formatter = FormatterTestHelper.GetFormatter(new ODataPayloadKind[] { ODataPayloadKind.Property }, request, mediaType); // Act & Assert Type type = (value != null) ? value.GetType() : typeof(Nullable <int>); var content = FormatterTestHelper.GetContent(value, type, formatter, mediaType.ToString()); await ExceptionAssert.ThrowsAsync <ODataException>( () => FormatterTestHelper.GetContentResult(content, request), "Cannot write the value 'null' in top level property; return 204 instead."); }