-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathFunctionTests.cs
More file actions
317 lines (259 loc) · 13.4 KB
/
FunctionTests.cs
File metadata and controls
317 lines (259 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web.Http.Dispatcher;
using System.Web.OData.Builder;
using System.Web.OData.Extensions;
using FluentAssertions;
using Microsoft.OData.Edm;
using Microsoft.Owin.Hosting;
using NUnit.Framework;
using Owin;
using Swashbuckle.Swagger;
using SwashbuckleODataSample.Models;
using System.Web.Http.Routing.Constraints;
using System.Web.Http;
namespace Swashbuckle.OData.Tests
{
[TestFixture]
public class FunctionTests
{
[Test]
public async Task It_supports_functions_with_enum_parameters()
{
using (WebApp.Start(HttpClientUtils.BaseAddress, appBuilder => Configuration(appBuilder, typeof(ProductsV1Controller))))
{
// Arrange
var httpClient = HttpClientUtils.GetHttpClient(HttpClientUtils.BaseAddress);
// Verify that the OData route in the test controller is valid
var products = await httpClient.GetJsonAsync<ODataResponse<List<Product1>>>("/odata/v1/Products/Default.EnumParam(Id=3,EnumValue=SwashbuckleODataSample.Models.MyEnum'ValueOne')");
products.Should().NotBeNull();
products.Value.Count.Should().Be(2);
// Act
var swaggerDocument = await httpClient.GetJsonAsync<SwaggerDocument>("swagger/docs/v1");
// Assert
PathItem pathItem;
swaggerDocument.paths.TryGetValue("/odata/v1/Products/Default.EnumParam(Id={Id},EnumValue=SwashbuckleODataSample.Models.MyEnum'{EnumValue}')", out pathItem);
pathItem.Should().NotBeNull();
pathItem.get.Should().NotBeNull();
await ValidationUtils.ValidateSwaggerJson();
}
}
[Test]
public async Task It_supports_a_function_bound_to_an_entity_with_enum_parameters()
{
using (WebApp.Start(HttpClientUtils.BaseAddress, appBuilder => Configuration(appBuilder, typeof(ProductsV1Controller))))
{
// Arrange
var httpClient = HttpClientUtils.GetHttpClient(HttpClientUtils.BaseAddress);
// Verify that the OData route in the test controller is valid
var match = await httpClient.GetJsonAsync<ODataResponse<bool>>("/odata/v1/Products(3)/Default.IsEnumValueMatch(EnumValue=SwashbuckleODataSample.Models.MyEnum'ValueOne')");
match.Should().NotBeNull();
// Act
var swaggerDocument = await httpClient.GetJsonAsync<SwaggerDocument>("swagger/docs/v1");
// Assert
PathItem pathItem;
swaggerDocument.paths.TryGetValue("/odata/v1/Products({Id})/Default.IsEnumValueMatch(EnumValue=SwashbuckleODataSample.Models.MyEnum'{EnumValue}')", out pathItem);
pathItem.Should().NotBeNull();
pathItem.get.Should().NotBeNull();
await ValidationUtils.ValidateSwaggerJson();
}
}
[Test]
public async Task It_supports_functions_with_multiple_parameters()
{
using (WebApp.Start(HttpClientUtils.BaseAddress, appBuilder => Configuration(appBuilder, typeof(ProductsV1Controller))))
{
// Arrange
var httpClient = HttpClientUtils.GetHttpClient(HttpClientUtils.BaseAddress);
// Verify that the OData route in the test controller is valid
var products = await httpClient.GetJsonAsync<ODataResponse<List<Product1>>>("/odata/v1/Products/Default.MultipleParams(Id=3,Year=2015)");
products.Should().NotBeNull();
products.Value.Count.Should().Be(2);
// Act
var swaggerDocument = await httpClient.GetJsonAsync<SwaggerDocument>("swagger/docs/v1");
// Assert
PathItem pathItem;
swaggerDocument.paths.TryGetValue("/odata/v1/Products/Default.MultipleParams(Id={Id},Year={Year})", out pathItem);
pathItem.Should().NotBeNull();
pathItem.get.Should().NotBeNull();
await ValidationUtils.ValidateSwaggerJson();
}
}
[Test]
public async Task It_supports_a_parameterless_function_bound_to_a_collection()
{
using (WebApp.Start(HttpClientUtils.BaseAddress, appBuilder => Configuration(appBuilder, typeof(ProductsV1Controller))))
{
// Arrange
var httpClient = HttpClientUtils.GetHttpClient(HttpClientUtils.BaseAddress);
// Act
var swaggerDocument = await httpClient.GetJsonAsync<SwaggerDocument>("swagger/docs/v1");
// Assert
PathItem pathItem;
swaggerDocument.paths.TryGetValue("/odata/v1/Products/Default.MostExpensive()", out pathItem);
pathItem.Should().NotBeNull();
pathItem.get.Should().NotBeNull();
await ValidationUtils.ValidateSwaggerJson();
}
}
[Test]
public async Task It_supports_a_function_bound_to_an_entity_set_that_returns_a_collection()
{
using (WebApp.Start(HttpClientUtils.BaseAddress, appBuilder => Configuration(appBuilder, typeof(ProductsV1Controller))))
{
// Arrange
var httpClient = HttpClientUtils.GetHttpClient(HttpClientUtils.BaseAddress);
// Act
var swaggerDocument = await httpClient.GetJsonAsync<SwaggerDocument>("swagger/docs/v1");
// Assert
PathItem pathItem;
swaggerDocument.paths.TryGetValue("/odata/v1/Products/Default.Top10()", out pathItem);
pathItem.Should().NotBeNull();
pathItem.get.Should().NotBeNull();
await ValidationUtils.ValidateSwaggerJson();
}
}
[Test]
public async Task It_supports_a_function_bound_to_an_entity()
{
using (WebApp.Start(HttpClientUtils.BaseAddress, appBuilder => Configuration(appBuilder, typeof(ProductsV1Controller))))
{
// Arrange
var httpClient = HttpClientUtils.GetHttpClient(HttpClientUtils.BaseAddress);
// Act
var swaggerDocument = await httpClient.GetJsonAsync<SwaggerDocument>("swagger/docs/v1");
// Assert
PathItem pathItem;
swaggerDocument.paths.TryGetValue("/odata/v1/Products({Id})/Default.GetPriceRank()", out pathItem);
pathItem.Should().NotBeNull();
pathItem.get.Should().NotBeNull();
await ValidationUtils.ValidateSwaggerJson();
}
}
[Test]
public async Task It_supports_a_function_that_accepts_a_string_parameter()
{
using (WebApp.Start(HttpClientUtils.BaseAddress, appBuilder => Configuration(appBuilder, typeof(ProductsV1Controller))))
{
// Arrange
var httpClient = HttpClientUtils.GetHttpClient(HttpClientUtils.BaseAddress);
// Act
var swaggerDocument = await httpClient.GetJsonAsync<SwaggerDocument>("swagger/docs/v1");
// Assert
PathItem pathItem;
swaggerDocument.paths.TryGetValue("/odata/v1/Products({Id})/Default.CalculateGeneralSalesTax(state='{state}')", out pathItem);
pathItem.Should().NotBeNull();
pathItem.get.Should().NotBeNull();
await ValidationUtils.ValidateSwaggerJson();
}
}
[Test]
public async Task It_supports_unbound_functions()
{
using (WebApp.Start(HttpClientUtils.BaseAddress, appBuilder => Configuration(appBuilder, typeof(ProductsV1Controller))))
{
// Arrange
var httpClient = HttpClientUtils.GetHttpClient(HttpClientUtils.BaseAddress);
// Act
var swaggerDocument = await httpClient.GetJsonAsync<SwaggerDocument>("swagger/docs/v1");
// Assert
PathItem pathItem;
swaggerDocument.paths.TryGetValue("/odata/v1/GetSalesTaxRate(state='{state}')", out pathItem);
pathItem.Should().NotBeNull();
pathItem.get.Should().NotBeNull();
await ValidationUtils.ValidateSwaggerJson();
}
}
[Test]
public async Task It_supports_a_function_bound_to_an_entity_using_route_param()
{
Action<HttpConfiguration> configAction = config =>
{
// Define a route to a controller class that contains functions
var route = config.MapODataServiceRoute("FunctionsODataRouteWithParam", "odata/v1/{intParam}", GetFunctionsEdmModel());
route.Constraints.Add("intParam", new IntRouteConstraint());
};
using (WebApp.Start(HttpClientUtils.BaseAddress, appBuilder => Configuration(appBuilder, typeof(ProductsV1Controller),configAction)))
{
// Arrange
var httpClient = HttpClientUtils.GetHttpClient(HttpClientUtils.BaseAddress);
// Act
var swaggerDocument = await httpClient.GetJsonAsync<SwaggerDocument>("swagger/docs/v1");
// Assert
PathItem pathItem;
swaggerDocument.paths.TryGetValue("/odata/v1/{intParam}/Products({Id})/Default.GetPriceRank()", out pathItem);
pathItem.Should().NotBeNull();
pathItem.get.Should().NotBeNull();
await ValidationUtils.ValidateSwaggerJson();
}
}
private static void Configuration(IAppBuilder appBuilder, Type targetController)
{
var config = appBuilder.GetStandardHttpConfig(targetController);
var controllerSelector = new UnitTestODataVersionControllerSelector(config, targetController);
config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector);
// Define a route to a controller class that contains functions
config.MapODataServiceRoute("FunctionsODataRoute", "odata/v1", GetFunctionsEdmModel());
controllerSelector.RouteVersionSuffixMapping.Add("FunctionsODataRoute", "V1");
config.EnsureInitialized();
}
private static void Configuration(IAppBuilder appBuilder, Type targetController, Action<HttpConfiguration> configAction)
{
var config = appBuilder.GetStandardHttpConfig(targetController);
var controllerSelector = new UnitTestODataVersionControllerSelector(config, targetController);
config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector);
configAction(config);
controllerSelector.RouteVersionSuffixMapping.Add("FunctionsODataRoute", "V1");
config.EnsureInitialized();
}
private static IEdmModel GetFunctionsEdmModel()
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Product>("Products");
var productType = builder.EntityType<Product>();
// Function bound to a collection that accepts an enum parameter
var enumParamFunction = productType.Collection.Function("EnumParam");
enumParamFunction.Parameter<int>("Id");
enumParamFunction.Parameter<MyEnum>("EnumValue");
enumParamFunction.ReturnsCollectionFromEntitySet<Product>("Products");
// Function bound to an entity that accepts an enum parameter
var enumParamEntityFunction = productType.Function("IsEnumValueMatch");
enumParamEntityFunction.Parameter<MyEnum>("EnumValue");
enumParamEntityFunction.Returns<bool>();
// Function bound to a collection that accepts multiple parameters
var multiParamFunction = productType.Collection.Function("MultipleParams");
multiParamFunction.Parameter<int>("Id");
multiParamFunction.Parameter<int>("Year");
multiParamFunction.ReturnsCollectionFromEntitySet<Product>("Products");
// Function bound to a collection
// Returns the most expensive product, a single entity
productType.Collection
.Function("MostExpensive")
.Returns<double>();
// Function bound to a collection
// Returns the top 10 product, a collection
productType.Collection
.Function("Top10")
.ReturnsCollectionFromEntitySet<Product>("Products");
// Function bound to a single entity
// Returns the instance's price rank among all products
productType
.Function("GetPriceRank")
.Returns<int>();
// Function bound to a single entity
// Accept a string as parameter and return a double
// This function calculate the general sales tax base on the
// state
productType
.Function("CalculateGeneralSalesTax")
.Returns<double>()
.Parameter<string>("state");
// Unbound Function
builder.Function("GetSalesTaxRate")
.Returns<double>()
.Parameter<string>("state");
return builder.GetEdmModel();
}
}
}