-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathComplexPropertyPathItemHandlerTests.cs
More file actions
363 lines (337 loc) · 13.8 KB
/
ComplexPropertyPathItemHandlerTests.cs
File metadata and controls
363 lines (337 loc) · 13.8 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.Net.Http;
using Microsoft.OData.Edm;
using Microsoft.OpenApi.OData.Edm;
using Xunit;
namespace Microsoft.OpenApi.OData.PathItem.Tests;
public class ComplexPropertyPathItemHandlerTests
{
private readonly ComplexPropertyItemHandler _pathItemHandler = new(new());
[Fact]
public void CreatePathItemThrowsForNullContext()
{
Assert.Throws<ArgumentNullException>("context",
() => _pathItemHandler.CreatePathItem(context: null, path: new ODataPath()));
}
[Theory]
[InlineData(true, true, 2)]
[InlineData(true, false, 0)]
[InlineData(false, false, 2)]
[InlineData(false, true, 2)]
public void SetsDefaultOperations(bool useAnnotationToGeneratePath, bool annotationAvailable, int operationCount)
{
var annotation = annotationAvailable
? @"
<Annotation Term=""Org.OData.Capabilities.V1.UpdateRestrictions"">
<Record>
<PropertyValue Property=""Updatable"" Bool=""true"" />
</Record>
</Annotation>
<Annotation Term=""Org.OData.Capabilities.V1.ReadRestrictions"">
<Record>
<PropertyValue Property=""Readable"" Bool=""true"" />
</Record>
</Annotation>"
: "";
var target = @"""NS.Customer/BillingAddress""";
var model = EntitySetPathItemHandlerTests.GetEdmModel(annotation: annotation, target: target);
var convertSettings = new OpenApiConvertSettings
{
RequireRestrictionAnnotationsToGenerateComplexPropertyPaths = useAnnotationToGeneratePath
};
var context = new ODataContext(model, convertSettings);
var entitySet = model.EntityContainer.FindEntitySet("Customers");
Assert.NotNull(entitySet); // guard
var entityType = entitySet.EntityType;
var property = entityType.FindProperty("BillingAddress");
Assert.NotNull(property); // guard
var path = new ODataPath(new ODataNavigationSourceSegment(entitySet), new ODataKeySegment(entityType), new ODataComplexPropertySegment(property as IEdmStructuralProperty));
Assert.Equal(ODataPathKind.ComplexProperty, path.Kind); // guard
var pathItem = _pathItemHandler.CreatePathItem(context, path);
Assert.NotNull(pathItem);
Assert.Equal(operationCount, pathItem.Operations?.Count ?? 0);
if (operationCount > 0)
{
Assert.True(pathItem.Operations.ContainsKey(HttpMethod.Get));
Assert.True(pathItem.Operations.ContainsKey(HttpMethod.Patch));
}
else
{
Assert.Null(pathItem.Operations);
}
}
[Theory]
[InlineData(true, true, 1)]
[InlineData(true, false, 0)]
[InlineData(false, false, 2)]
[InlineData(false, true, 2)]
public void SetsUpdateOperationWithUpdateMethodUpdateRestrictions(bool useAnnotationToGeneratePath, bool annotationAvailable, int operationCount)
{
var annotation = annotationAvailable
? @"
<Annotation Term=""Org.OData.Capabilities.V1.UpdateRestrictions"">
<Record>
<PropertyValue Property=""UpdateMethod"">
<EnumMember>Org.OData.Capabilities.V1.HttpMethod/PUT</EnumMember>
</PropertyValue>
<PropertyValue Property=""Updatable"" Bool=""true"" />
</Record>
</Annotation>
<Annotation Term=""Org.OData.Capabilities.V1.ReadRestrictions"">
<Record>
<PropertyValue Property=""Readable"" Bool=""false"" />
</Record>
</Annotation>"
: "";
var target = @"""NS.Customer/BillingAddress""";
var model = EntitySetPathItemHandlerTests.GetEdmModel(annotation: annotation, target: target);
var convertSettings = new OpenApiConvertSettings
{
RequireRestrictionAnnotationsToGenerateComplexPropertyPaths = useAnnotationToGeneratePath
};
var context = new ODataContext(model, convertSettings);
var entitySet = model.EntityContainer.FindEntitySet("Customers");
Assert.NotNull(entitySet); // guard
var entityType = entitySet.EntityType;
var property = entityType.FindProperty("BillingAddress");
Assert.NotNull(property); // guard
var path = new ODataPath(new ODataNavigationSourceSegment(entitySet), new ODataKeySegment(entityType), new ODataComplexPropertySegment(property as IEdmStructuralProperty));
Assert.Equal(ODataPathKind.ComplexProperty, path.Kind); // guard
var pathItem = _pathItemHandler.CreatePathItem(context, path);
Assert.NotNull(pathItem);
Assert.Equal(operationCount, pathItem.Operations?.Count ?? 0);
if (operationCount > 0)
{
if (annotationAvailable)
{
Assert.True(pathItem.Operations.ContainsKey(HttpMethod.Put));
}
else
{
Assert.True(pathItem.Operations.ContainsKey(HttpMethod.Get));
Assert.True(pathItem.Operations.ContainsKey(HttpMethod.Patch));
}
}
else
{
Assert.Null(pathItem.Operations);
}
}
[Theory]
[InlineData(true, true, 1)]
[InlineData(true, false, 0)]
[InlineData(false, false, 3)]
[InlineData(false, true, 3)]
public void SetsPostOnCollectionProperties(bool useAnnotationToGeneratePath, bool annotationAvailable, int operationCount)
{
var annotation = annotationAvailable
? @"
<Annotation Term=""Org.OData.Capabilities.V1.InsertRestrictions"">
<Record>
<PropertyValue Property=""Insertable"" Bool=""true"" />
</Record>
</Annotation>
<Annotation Term=""Org.OData.Capabilities.V1.ReadRestrictions"">
<Record>
<PropertyValue Property=""Description"" String=""Create groupLifecyclePolicy"" />
<!-- No Readable property defined! -->
</Record>
</Annotation>"
: "";
var target = @"""NS.Customer/AlternativeAddresses""";
var model = EntitySetPathItemHandlerTests.GetEdmModel(annotation: annotation, target: target);
var convertSettings = new OpenApiConvertSettings
{
RequireRestrictionAnnotationsToGenerateComplexPropertyPaths = useAnnotationToGeneratePath
};
var context = new ODataContext(model, convertSettings);
var entitySet = model.EntityContainer.FindEntitySet("Customers");
Assert.NotNull(entitySet); // guard
var entityType = entitySet.EntityType;
var property = entityType.FindProperty("AlternativeAddresses");
Assert.NotNull(property); // guard
var path = new ODataPath(new ODataNavigationSourceSegment(entitySet), new ODataKeySegment(entityType), new ODataComplexPropertySegment(property as IEdmStructuralProperty));
Assert.Equal(ODataPathKind.ComplexProperty, path.Kind); // guard
var pathItem = _pathItemHandler.CreatePathItem(context, path);
Assert.NotNull(pathItem);
Assert.Equal(operationCount, pathItem.Operations?.Count ?? 0);
if (operationCount > 0)
{
Assert.True(pathItem.Operations.ContainsKey(HttpMethod.Post));
}
else
{
Assert.Null(pathItem.Operations);
}
}
[Fact]
public void CreateComplexPropertyPathItemAddsCustomAttributeValuesToPathExtensions()
{
// Arrange
var model = EntitySetPathItemHandlerTests.GetEdmModel("");
ODataContext context = new(model);
context.Settings.CustomXMLAttributesMapping.Add("ags:IsHidden", "x-ms-isHidden");
var entitySet = model.EntityContainer.FindEntitySet("Customers");
Assert.NotNull(entitySet); // guard
var entityType = entitySet.EntityType;
var property = entityType.FindProperty("AlternativeAddresses");
Assert.NotNull(property); // guard
var path = new ODataPath(new ODataNavigationSourceSegment(entitySet), new ODataKeySegment(entityType), new ODataComplexPropertySegment(property as IEdmStructuralProperty));
Assert.Equal(ODataPathKind.ComplexProperty, path.Kind); // guard
// Act
var pathItem = _pathItemHandler.CreatePathItem(context, path);
// Assert
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);
}
[Theory]
[InlineData("true", "true", "true", 3)]
[InlineData("false", "false", "false", 0)]
[InlineData ("false", "false", "true", 1)]
public void CreatesComplexPropertyPathsBasedOnTargetPathAnnotations(string readable, string insertable, string updatable, int operationCount)
{
// Arrange
var annotation = $@"
<Annotation Term=""Org.OData.Capabilities.V1.InsertRestrictions"">
<Record>
<PropertyValue Property=""Insertable"" Bool=""{insertable}"" />
</Record>
</Annotation>
<Annotation Term=""Org.OData.Capabilities.V1.ReadRestrictions"">
<Record>
<PropertyValue Property=""Readable"" Bool=""{readable}"" />
</Record>
</Annotation>
<Annotation Term=""Org.OData.Capabilities.V1.UpdateRestrictions"">
<Record>
<PropertyValue Property=""Updatable"" Bool=""{updatable}"" />
</Record>
</Annotation>";
var target = @"""NS.Default/Customers/AlternativeAddresses""";
var model = EntitySetPathItemHandlerTests.GetEdmModel(annotation, target);
var context = new ODataContext(model, new OpenApiConvertSettings());
var entitySet = model.EntityContainer.FindEntitySet("Customers");
Assert.NotNull(entitySet); // guard
var entityType = entitySet.EntityType;
var property = entityType.FindProperty("AlternativeAddresses");
Assert.NotNull(property); // guard
var path = new ODataPath(
new ODataNavigationSourceSegment(entitySet),
new ODataKeySegment(entityType),
new ODataComplexPropertySegment(property as IEdmStructuralProperty));
Assert.Equal(ODataPathKind.ComplexProperty, path.Kind); // guard
// Act
var pathItem = _pathItemHandler.CreatePathItem(context, path);
// Assert
Assert.NotNull(pathItem);
Assert.Equal(operationCount, pathItem.Operations?.Count ?? 0);
if (operationCount == 1)
{
Assert.True(pathItem.Operations.ContainsKey(HttpMethod.Patch));
Assert.False(pathItem.Operations.ContainsKey(HttpMethod.Post));
Assert.False(pathItem.Operations.ContainsKey(HttpMethod.Get));
}
else if (operationCount == 3)
{
Assert.True(pathItem.Operations.ContainsKey(HttpMethod.Patch));
Assert.True(pathItem.Operations.ContainsKey(HttpMethod.Post));
Assert.True(pathItem.Operations.ContainsKey(HttpMethod.Get));
}
}
[Theory]
[InlineData(false, 2)]
[InlineData(true, 2)]
public void CreatesComplexPropertyPathItemUsesHttpPutForUpdateWhenSettingIsEnabled(bool useHttpPutForUpdate, int operationCount)
{
// Arrange
var annotation = @"
<Annotation Term=""Org.OData.Capabilities.V1.UpdateRestrictions"">
<Record>
<PropertyValue Property=""Updatable"" Bool=""true"" />
</Record>
</Annotation>
<Annotation Term=""Org.OData.Capabilities.V1.ReadRestrictions"">
<Record>
<PropertyValue Property=""Readable"" Bool=""true"" />
</Record>
</Annotation>";
var target = @"""NS.Customer/BillingAddress""";
var model = EntitySetPathItemHandlerTests.GetEdmModel(annotation: annotation, target: target);
var convertSettings = new OpenApiConvertSettings
{
UseHttpPutForUpdate = useHttpPutForUpdate
};
var context = new ODataContext(model, convertSettings);
var entitySet = model.EntityContainer.FindEntitySet("Customers");
Assert.NotNull(entitySet); // guard
var entityType = entitySet.EntityType;
var property = entityType.FindProperty("BillingAddress");
Assert.NotNull(property); // guard
var path = new ODataPath(new ODataNavigationSourceSegment(entitySet), new ODataKeySegment(entityType), new ODataComplexPropertySegment(property as IEdmStructuralProperty));
Assert.Equal(ODataPathKind.ComplexProperty, path.Kind); // guard
// Act
var pathItem = _pathItemHandler.CreatePathItem(context, path);
// Assert
Assert.NotNull(pathItem);
Assert.Equal(operationCount, pathItem.Operations?.Count ?? 0);
Assert.True(pathItem.Operations.ContainsKey(HttpMethod.Get));
if (useHttpPutForUpdate)
{
Assert.True(pathItem.Operations.ContainsKey(HttpMethod.Put));
Assert.False(pathItem.Operations.ContainsKey(HttpMethod.Patch));
}
else
{
Assert.True(pathItem.Operations.ContainsKey(HttpMethod.Patch));
Assert.False(pathItem.Operations.ContainsKey(HttpMethod.Put));
}
}
[Fact]
public void CreateComplexPropertyPathItemPrefersUpdateMethodAnnotationOverUseHttpPutForUpdateSetting()
{
// Arrange - annotation specifies PUT explicitly, setting is disabled (default PATCH)
var annotation = @"
<Annotation Term=""Org.OData.Capabilities.V1.UpdateRestrictions"">
<Record>
<PropertyValue Property=""UpdateMethod"">
<EnumMember>Org.OData.Capabilities.V1.HttpMethod/PUT</EnumMember>
</PropertyValue>
<PropertyValue Property=""Updatable"" Bool=""true"" />
</Record>
</Annotation>
<Annotation Term=""Org.OData.Capabilities.V1.ReadRestrictions"">
<Record>
<PropertyValue Property=""Readable"" Bool=""true"" />
</Record>
</Annotation>";
var target = @"""NS.Customer/BillingAddress""";
var model = EntitySetPathItemHandlerTests.GetEdmModel(annotation: annotation, target: target);
var convertSettings = new OpenApiConvertSettings
{
UseHttpPutForUpdate = false // Setting says use PATCH (default)
};
var context = new ODataContext(model, convertSettings);
var entitySet = model.EntityContainer.FindEntitySet("Customers");
Assert.NotNull(entitySet); // guard
var entityType = entitySet.EntityType;
var property = entityType.FindProperty("BillingAddress");
Assert.NotNull(property); // guard
var path = new ODataPath(new ODataNavigationSourceSegment(entitySet), new ODataKeySegment(entityType), new ODataComplexPropertySegment(property as IEdmStructuralProperty));
Assert.Equal(ODataPathKind.ComplexProperty, path.Kind); // guard
// Act
var pathItem = _pathItemHandler.CreatePathItem(context, path);
// Assert
Assert.NotNull(pathItem);
Assert.Equal(2, pathItem.Operations?.Count ?? 0);
Assert.True(pathItem.Operations.ContainsKey(HttpMethod.Get));
// Should use PUT from annotation, not PATCH from setting
Assert.True(pathItem.Operations.ContainsKey(HttpMethod.Put));
Assert.False(pathItem.Operations.ContainsKey(HttpMethod.Patch));
}
}