|
| 1 | +//----------------------------------------------------------------------------- |
| 2 | +// <copyright file="HttpClientTests.cs" company=".NET Foundation"> |
| 3 | +// Copyright (c) .NET Foundation and Contributors. All rights reserved. |
| 4 | +// See License.txt in the project root for license information. |
| 5 | +// </copyright> |
| 6 | +//------------------------------------------------------------------------------ |
| 7 | + |
| 8 | +using Microsoft.AspNetCore.OData; |
| 9 | +using Microsoft.AspNetCore.OData.Batch; |
| 10 | +using Microsoft.AspNetCore.OData.Routing.Controllers; |
| 11 | +using Microsoft.Extensions.DependencyInjection; |
| 12 | +using Microsoft.OData.Client.E2E.Tests.TransportLayerTests.Server; |
| 13 | +using Microsoft.OData.E2E.TestCommon; |
| 14 | +using Microsoft.OData.E2E.TestCommon.Common.Client.EndToEnd; |
| 15 | +using Microsoft.OData.E2E.TestCommon.Common.Client.EndToEnd.Default; |
| 16 | +using Microsoft.OData.E2E.TestCommon.Common.Server.EndToEnd; |
| 17 | +using Xunit; |
| 18 | +using ClientEndToEndModel = Microsoft.OData.E2E.TestCommon.Common.Client.EndToEnd; |
| 19 | + |
| 20 | +namespace Microsoft.OData.Client.E2E.Tests.TransportLayerTests |
| 21 | +{ |
| 22 | + public class HttpClientTests : EndToEndTestBase<HttpClientTests.TestsStartup> |
| 23 | + { |
| 24 | + private readonly Uri _baseUri; |
| 25 | + private readonly Container _context; |
| 26 | + |
| 27 | + public class TestsStartup : TestStartupBase |
| 28 | + { |
| 29 | + public override void ConfigureServices(IServiceCollection services) |
| 30 | + { |
| 31 | + services.ConfigureControllers(typeof(HttpClientController), typeof(MetadataController)); |
| 32 | + |
| 33 | + services.AddControllers().AddOData(opt => opt.Count().Filter().Expand().Select().OrderBy().SetMaxTop(null) |
| 34 | + .AddRouteComponents("odata", CommonEndToEndEdmModel.GetEdmModel(), new DefaultODataBatchHandler())); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public HttpClientTests(TestWebApplicationFactory<TestsStartup> fixture) |
| 39 | + : base(fixture) |
| 40 | + { |
| 41 | + _baseUri = new Uri(Client.BaseAddress, "odata/"); |
| 42 | + |
| 43 | + _context = new Container(_baseUri) |
| 44 | + { |
| 45 | + HttpClientFactory = HttpClientFactory |
| 46 | + }; |
| 47 | + |
| 48 | + ResetDataSource(); |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public async Task QueryForEntity_ShouldReturnResults() |
| 53 | + { |
| 54 | + var results = await _context.CreateQuery<ClientEndToEndModel.Product>("Products").ExecuteAsync(); |
| 55 | + Assert.NotEmpty(results); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public async Task InsertNewEntity_ShouldInsertEntity() |
| 60 | + { |
| 61 | + _context.AddToOrders(new ClientEndToEndModel.Order { OrderId = 993, }); |
| 62 | + await _context.SaveChangesAsync(); |
| 63 | + |
| 64 | + var createdOrderQuery = _context.Orders.ByKey(993); |
| 65 | + var createdOrder = await createdOrderQuery.GetValueAsync(); |
| 66 | + Assert.Equal(993, createdOrder.OrderId); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public async Task UpdateEntityWithPatch_ShouldUpdateEntitySuccessfully() |
| 71 | + { |
| 72 | + var product = _context.Products.First(); |
| 73 | + var newProductDescription = "Foo " + Guid.NewGuid().ToString(); |
| 74 | + product.Description = newProductDescription; |
| 75 | + _context.UpdateObject(product); |
| 76 | + |
| 77 | + await _context.SaveChangesAsync(); |
| 78 | + |
| 79 | + var updatedProduct = _context.Products.First(); |
| 80 | + Assert.Equal(newProductDescription, updatedProduct.Description); |
| 81 | + } |
| 82 | + |
| 83 | + [Fact] |
| 84 | + public async Task UpdateEntityWithReplaceUsingJson_ShouldUpdareTheEntity() |
| 85 | + { |
| 86 | + _context.Format.UseJson(); |
| 87 | + |
| 88 | + var product = _context.Products.First(); |
| 89 | + product.Description = "Foo " + Guid.NewGuid().ToString(); |
| 90 | + _context.UpdateObject(product); |
| 91 | + |
| 92 | + await _context.SaveChangesAsync(SaveChangesOptions.ReplaceOnUpdate); |
| 93 | + var updateProduct = _context.Products.First(); |
| 94 | + Assert.Equal(product.Description, updateProduct.Description); |
| 95 | + } |
| 96 | + |
| 97 | + [Fact] |
| 98 | + public async Task DeleteEntity_ShouldDeleteTheEntity() |
| 99 | + { |
| 100 | + var computer = _context.Computers.First(); |
| 101 | + Assert.Equal(-10, computer.ComputerId); |
| 102 | + _context.DeleteObject(computer); |
| 103 | + await _context.SaveChangesAsync(); |
| 104 | + |
| 105 | + var deletedComputer = _context.Computers.FirstOrDefault(c => c.ComputerId == -10); |
| 106 | + Assert.Null(deletedComputer); |
| 107 | + } |
| 108 | + |
| 109 | + [Fact] |
| 110 | + public async Task DoingMultipleChangesUsingBatch_ShouldExecuteSuccessfully() |
| 111 | + { |
| 112 | + _context.AddToOrders(new ClientEndToEndModel.Order { OrderId = 953, }); |
| 113 | + var customerToDelete = _context.Customers.First(); |
| 114 | + Assert.Equal(-10, customerToDelete.CustomerId); |
| 115 | + _context.DeleteObject(customerToDelete); |
| 116 | + |
| 117 | + var product = _context.Products.First(); |
| 118 | + product.Description = "Foo " + Guid.NewGuid().ToString(); |
| 119 | + _context.UpdateObject(product); |
| 120 | + |
| 121 | + await _context.SaveChangesAsync(SaveChangesOptions.BatchWithSingleChangeset); |
| 122 | + |
| 123 | + //Assert new order was created. |
| 124 | + var createdOrder = await _context.Orders.ByKey(953).GetValueAsync(); |
| 125 | + Assert.Equal(953, createdOrder.OrderId); |
| 126 | + |
| 127 | + //Assert customer was deleted |
| 128 | + var deletedCustomer = _context.Customers.FirstOrDefault(c => c.CustomerId == -10); |
| 129 | + Assert.Null(deletedCustomer); |
| 130 | + |
| 131 | + //Assert product was updated |
| 132 | + var updatedProduct = _context.Products.First(); |
| 133 | + Assert.Equal(product.Description, updatedProduct.Description); |
| 134 | + } |
| 135 | + |
| 136 | + [Fact] |
| 137 | + public async Task DoingMultipleChangesWithoutBatch_ShouldExecuteSuccessfully() |
| 138 | + { |
| 139 | + _context.AddToOrders(new ClientEndToEndModel.Order { OrderId = 953, }); |
| 140 | + _context.DeleteObject(_context.Customers.First()); |
| 141 | + |
| 142 | + var product = _context.Products.First(); |
| 143 | + product.Description = "Foo " + Guid.NewGuid().ToString(); |
| 144 | + _context.UpdateObject(product); |
| 145 | + |
| 146 | + await _context.SaveChangesAsync(); |
| 147 | + |
| 148 | + //Assert new order was created. |
| 149 | + var createdOrder = await _context.Orders.ByKey(953).GetValueAsync(); |
| 150 | + Assert.Equal(953, createdOrder.OrderId); |
| 151 | + |
| 152 | + //Assert customer was deleted |
| 153 | + var deletedCustomer = _context.Customers.FirstOrDefault(c => c.CustomerId == -10); |
| 154 | + Assert.Null(deletedCustomer); |
| 155 | + |
| 156 | + //Assert product was updated |
| 157 | + var updatedProduct = _context.Products.First(); |
| 158 | + Assert.Equal(product.Description, updatedProduct.Description); |
| 159 | + } |
| 160 | + |
| 161 | + [Fact] |
| 162 | + public async Task LoadProperty_ExecutesSuccessfully() |
| 163 | + { |
| 164 | + var product = _context.Products.First(); |
| 165 | + |
| 166 | + var response = await _context.LoadPropertyAsync(product, "Description"); |
| 167 | + Assert.Equal(200, response.StatusCode); |
| 168 | + } |
| 169 | + |
| 170 | + [Fact] |
| 171 | + public async Task BatchUpdatesAsync() |
| 172 | + { |
| 173 | + _context.AddObject("Products", new ClientEndToEndModel.Product { ProductId = 55443, Description = "My new product", }); |
| 174 | + |
| 175 | + ClientEndToEndModel.Customer customerWithCustomerInfo = null; |
| 176 | + var query = _context.Customers.Expand(c => c.Info).Where(c => c.Info != null) as DataServiceQuery<ClientEndToEndModel.Customer>; |
| 177 | + var queryResult = await query.ExecuteAsync(); |
| 178 | + customerWithCustomerInfo = queryResult.First(); |
| 179 | + |
| 180 | + var customerInfo = customerWithCustomerInfo.Info; |
| 181 | + customerInfo.Information = "New Information " + Guid.NewGuid().ToString(); |
| 182 | + _context.UpdateObject(customerInfo); |
| 183 | + |
| 184 | + _context.DetachLink(customerWithCustomerInfo, "Info", customerInfo); |
| 185 | + _context.UpdateObject(customerWithCustomerInfo); |
| 186 | + |
| 187 | + var response = await _context.SaveChangesAsync(SaveChangesOptions.BatchWithSingleChangeset); |
| 188 | + |
| 189 | + Assert.Equal(200, response.BatchStatusCode); |
| 190 | + Assert.Equal(3, response.Count()); |
| 191 | + } |
| 192 | + |
| 193 | + [Fact] |
| 194 | + //Client should not append () to navigation property query URI |
| 195 | + public async Task ClientShouldNotAppendParenthesisToNavigationPropertyQueryUri() |
| 196 | + { |
| 197 | + var product = new ClientEndToEndModel.Product { ProductId = 55445, Description = "My new product" }; |
| 198 | + _context.AddObject("Products", product); |
| 199 | + await _context.SaveChangesAsync(); |
| 200 | + _context.Detach(product); |
| 201 | + _context.AttachTo("Products", product); |
| 202 | + //Collection |
| 203 | + var response = await _context.LoadPropertyAsync(product, "RelatedProducts"); |
| 204 | + Assert.EndsWith("RelatedProducts", response.Query.RequestUri.AbsolutePath); |
| 205 | + } |
| 206 | + |
| 207 | + private void ResetDataSource() |
| 208 | + { |
| 209 | + var actionUri = new Uri(_baseUri + "httpclient/Default.ResetDataSource", UriKind.Absolute); |
| 210 | + _context.Execute(actionUri, "POST"); |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments