-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathEntityPathItemHandlerTests.cs
More file actions
363 lines (313 loc) · 14.5 KB
/
EntityPathItemHandlerTests.cs
File metadata and controls
363 lines (313 loc) · 14.5 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using System;
using System.Linq;
using System.Net.Http;
using Microsoft.OData.Edm;
using Microsoft.OpenApi.OData.Edm;
using Microsoft.OpenApi.OData.Properties;
using Microsoft.OpenApi.OData.Tests;
using Xunit;
namespace Microsoft.OpenApi.OData.PathItem.Tests
{
public class EntityPathItemHandlerTests
{
private EntityPathItemHandler _pathItemHandler = new MyEntityPathItemHandler(new());
[Fact]
public void CreatePathItemThrowsForNullContext()
{
// Arrange & Act & Assert
Assert.Throws<ArgumentNullException>("context",
() => _pathItemHandler.CreatePathItem(context: null, path: new ODataPath()));
}
[Fact]
public void CreatePathItemThrowsForNullPath()
{
// Arrange & Act & Assert
Assert.Throws<ArgumentNullException>("path",
() => _pathItemHandler.CreatePathItem(new ODataContext(EdmCoreModel.Instance), path: null));
}
[Fact]
public void CreatePathItemThrowsForNonEntityPath()
{
// Arrange
IEdmModel model = EntitySetPathItemHandlerTests.GetEdmModel(annotation: "");
ODataContext context = new ODataContext(model);
IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet("Customers");
Assert.NotNull(entitySet); // guard
var path = new ODataPath(new ODataNavigationSourceSegment(entitySet));
Assert.Equal(ODataPathKind.EntitySet, path.Kind); // guard
// Act
Action test = () => _pathItemHandler.CreatePathItem(context, path);
// Assert
var exception = Assert.Throws<InvalidOperationException>(test);
Assert.Equal(String.Format(SRResource.InvalidPathKindForPathItemHandler, _pathItemHandler.GetType().Name, path.Kind), exception.Message);
}
[Fact]
public void CreateEntityPathItemReturnsCorrectPathItem()
{
// Arrange
IEdmModel model = EntitySetPathItemHandlerTests.GetEdmModel(annotation: "");
ODataContext context = new ODataContext(model);
IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet("Customers");
Assert.NotNull(entitySet); // guard
ODataPath path = new ODataPath(new ODataNavigationSourceSegment(entitySet), new ODataKeySegment(entitySet.EntityType));
// Act
var pathItem = _pathItemHandler.CreatePathItem(context, path);
// Assert
Assert.NotNull(pathItem);
Assert.NotNull(pathItem.Operations);
Assert.NotEmpty(pathItem.Operations);
Assert.Equal(3, pathItem.Operations.Count);
Assert.Equal(new HttpMethod[] { HttpMethod.Get, HttpMethod.Patch, HttpMethod.Delete },
pathItem.Operations.Select(o => o.Key));
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void CreateEntityPathItemReturnsCorrectPathItemWithPathParameters(bool declarePathParametersOnPathItem)
{
// Arrange
IEdmModel model = EntitySetPathItemHandlerTests.GetEdmModel(annotation: "");
OpenApiConvertSettings convertSettings = new OpenApiConvertSettings
{
DeclarePathParametersOnPathItem = declarePathParametersOnPathItem,
};
ODataContext context = new ODataContext(model, convertSettings);
IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet("Customers");
Assert.NotNull(entitySet); // guard
ODataPath path = new ODataPath(new ODataNavigationSourceSegment(entitySet), new ODataKeySegment(entitySet.EntityType));
// Act
var pathItem = _pathItemHandler.CreatePathItem(context, path);
// Assert
Assert.NotNull(pathItem);
Assert.NotNull(pathItem.Operations);
Assert.NotEmpty(pathItem.Operations);
Assert.Equal(3, pathItem.Operations.Count);
Assert.Equal(new HttpMethod[] { HttpMethod.Get, HttpMethod.Patch, HttpMethod.Delete },
pathItem.Operations.Select(o => o.Key));
if (declarePathParametersOnPathItem)
{
Assert.NotEmpty(pathItem.Parameters);
Assert.Single(pathItem.Parameters);
}
else
{
Assert.Null(pathItem.Parameters);
}
}
[Fact]
public void CreateEntityPathItemReturnsCorrectPathItemWithReferences()
{
// Test that references don't disturb the paths.
// Arrange
IEdmModel model = EdmModelHelper.InheritanceEdmModelAcrossReferences;
ODataContext context = new ODataContext(model);
IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet("Customers");
Assert.NotNull(entitySet); // guard
ODataPath path = new ODataPath(new ODataNavigationSourceSegment(entitySet), new ODataKeySegment(entitySet.EntityType));
// Act
var pathItem = _pathItemHandler.CreatePathItem(context, path);
// Assert
Assert.NotNull(pathItem);
Assert.NotNull(pathItem.Operations);
Assert.NotEmpty(pathItem.Operations);
Assert.Equal(3, pathItem.Operations.Count);
Assert.Equal(new HttpMethod[] { HttpMethod.Get, HttpMethod.Patch, HttpMethod.Delete },
pathItem.Operations.Select(o => o.Key));
Assert.NotEmpty(pathItem.Description);
}
[Theory]
[InlineData(true, new string[] { "get", "patch", "delete" })]
[InlineData(false, new string[] { "patch", "delete" })]
public void CreateEntityPathItemWorksForReadByKeyRestrictionsCapablities(bool readable, string[] expected)
{
// Arrange
string annotation = $@"
<Annotation Term=""Org.OData.Capabilities.V1.ReadRestrictions"">
<Record>
<PropertyValue Property=""ReadByKeyRestrictions"" >
<Record>
<PropertyValue Property=""Readable"" Bool=""{readable}"" />
</Record>
</PropertyValue>
</Record>
</Annotation>";
// Assert
VerifyPathItemOperations(annotation, expected);
}
[Theory]
[InlineData(true, new string[] { "get", "patch", "delete" })]
[InlineData(false, new string[] { "patch", "delete" })]
public void CreateEntityPathItemWorksForReadRestrictionsCapablities(bool readable, string[] expected)
{
// Arrange
string annotation = $@"
<Annotation Term=""Org.OData.Capabilities.V1.ReadRestrictions"">
<Record>
<PropertyValue Property=""Readable"" Bool=""{readable}"" />
</Record>
</Annotation>";
// Assert
VerifyPathItemOperations(annotation, expected);
}
[Theory]
[InlineData(true, new string[] { "get", "patch", "delete" })]
[InlineData(false, new string[] { "get", "delete" })]
public void CreateEntityPathItemWorksForUpdateRestrictionsCapablities(bool updatable, string[] expected)
{
// Arrange
string annotation = $@"
<Annotation Term=""Org.OData.Capabilities.V1.UpdateRestrictions"">
<Record>
<PropertyValue Property=""Updatable"" Bool=""{updatable}"" />
</Record>
</Annotation>";
// Assert
VerifyPathItemOperations(annotation, expected);
}
[Theory]
[InlineData(true, new string[] { "get", "patch", "delete" })]
[InlineData(false, new string[] { "get", "patch" })]
public void CreateEntityPathItemWorksForDeleteRestrictionsCapablities(bool deletable, string[] expected)
{
// Arrange
string annotation = $@"
<Annotation Term=""Org.OData.Capabilities.V1.DeleteRestrictions"">
<Record>
<PropertyValue Property=""Deletable"" Bool=""{deletable}"" />
</Record>
</Annotation>";
// Assert
VerifyPathItemOperations(annotation, expected);
}
[Theory]
[InlineData(false, new string[] { "get", "patch", "delete" })]
[InlineData(true, new string[] { "get", "put", "delete" })]
public void CreateEntityPathItemWorksForUpdateMethodRestrictionsCapabilities(bool updateMethod, string[] expected)
{
// Arrange
string annotation = updateMethod ? $@"
<Annotation Term=""Org.OData.Capabilities.V1.UpdateRestrictions"">
<Record>
<PropertyValue Property=""UpdateMethod"">
<EnumMember>Org.OData.Capabilities.V1.HttpMethod/PUT</EnumMember>
</PropertyValue>
</Record>
</Annotation>" : "";
// Assert
VerifyPathItemOperations(annotation, expected);
}
[Theory]
[InlineData(false, new string[] { "get", "patch", "delete" })]
[InlineData(true, new string[] { "get", "put", "delete" })]
public void CreateEntityPathItemUsesHttpPutForUpdateWhenSettingIsEnabled(bool useHttpPutForUpdate, string[] expected)
{
// Arrange
IEdmModel model = EntitySetPathItemHandlerTests.GetEdmModel(annotation: "");
OpenApiConvertSettings settings = new OpenApiConvertSettings
{
UseHttpPutForUpdate = useHttpPutForUpdate
};
ODataContext context = new ODataContext(model, settings);
IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet("Customers");
Assert.NotNull(entitySet); // guard
ODataPath path = new ODataPath(new ODataNavigationSourceSegment(entitySet), new ODataKeySegment(entitySet.EntityType));
// Act
var pathItem = _pathItemHandler.CreatePathItem(context, path);
// Assert
Assert.NotNull(pathItem);
Assert.NotNull(pathItem.Operations);
Assert.NotEmpty(pathItem.Operations);
Assert.Equal(expected, pathItem.Operations.Select(e => e.Key.ToString().ToLowerInvariant()));
}
[Fact]
public void CreateEntityPathItemPrefersUpdateMethodAnnotationOverUseHttpPutForUpdateSetting()
{
// Arrange - annotation specifies PUT explicitly, setting is disabled (default PATCH)
string annotation = @"
<Annotation Term=""Org.OData.Capabilities.V1.UpdateRestrictions"">
<Record>
<PropertyValue Property=""UpdateMethod"">
<EnumMember>Org.OData.Capabilities.V1.HttpMethod/PUT</EnumMember>
</PropertyValue>
</Record>
</Annotation>";
IEdmModel model = EntitySetPathItemHandlerTests.GetEdmModel(annotation);
OpenApiConvertSettings settings = new OpenApiConvertSettings
{
UseHttpPutForUpdate = false // Setting says use PATCH (default)
};
ODataContext context = new ODataContext(model, settings);
IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet("Customers");
Assert.NotNull(entitySet); // guard
ODataPath path = new ODataPath(new ODataNavigationSourceSegment(entitySet), new ODataKeySegment(entitySet.EntityType));
// Act
var pathItem = _pathItemHandler.CreatePathItem(context, path);
// Assert
Assert.NotNull(pathItem);
Assert.NotNull(pathItem.Operations);
Assert.NotEmpty(pathItem.Operations);
// Should use PUT from annotation, not PATCH from setting
Assert.Equal(new string[] { "get", "put", "delete" }, pathItem.Operations.Select(e => e.Key.ToString().ToLowerInvariant()));
}
private void VerifyPathItemOperations(string annotation, string[] expected)
{
// Arrange
IEdmModel model = EntitySetPathItemHandlerTests.GetEdmModel(annotation);
ODataContext context = new ODataContext(model);
IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet("Customers");
Assert.NotNull(entitySet); // guard
ODataPath path = new ODataPath(new ODataNavigationSourceSegment(entitySet), new ODataKeySegment(entitySet.EntityType));
// Act
var pathItem = _pathItemHandler.CreatePathItem(context, path);
// Assert
Assert.NotNull(pathItem);
Assert.NotNull(pathItem.Operations);
Assert.NotEmpty(pathItem.Operations);
Assert.Equal(expected, pathItem.Operations.Select(e => e.Key.ToString().ToLowerInvariant()));
}
[Fact]
public void CreateEntityPathItemAddsCustomAttributeValuesToPathExtensions()
{
// Arrange
IEdmModel model = EntitySetPathItemHandlerTests.GetEdmModel(annotation: "");
ODataContext context = new(model);
context.Settings.CustomXMLAttributesMapping = new()
{
{
"ags:IsHidden", "x-ms-isHidden"
},
{
"isOwner", "x-ms-isOwner"
}
};
IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet("Customers");
Assert.NotNull(entitySet); // guard
ODataPath path = new(new ODataNavigationSourceSegment(entitySet), new ODataKeySegment(entitySet.EntityType));
// Act
var pathItem = _pathItemHandler.CreatePathItem(context, path);
// Assert
Assert.NotNull(pathItem);
Assert.NotNull(pathItem.Extensions);
pathItem.Extensions.TryGetValue("x-ms-isHidden", out IOpenApiExtension isHiddenExtension);
string isHiddenValue = Assert.IsType<JsonNodeExtension>(isHiddenExtension).Node.GetValue<string>();
Assert.Equal("true", isHiddenValue);
pathItem.Extensions.TryGetValue("x-ms-isOwner", out IOpenApiExtension isOwnerExtension);
string isOwnerValue = Assert.IsType<JsonNodeExtension>(isOwnerExtension).Node.GetValue<string>();
Assert.Equal("true", isOwnerValue);
}
}
internal class MyEntityPathItemHandler : EntityPathItemHandler
{
public MyEntityPathItemHandler(OpenApiDocument document) : base(document)
{
}
protected override void AddOperation(OpenApiPathItem item, HttpMethod operationType)
{
item.AddOperation(operationType, new OpenApiOperation());
}
}
}