Skip to content

Commit bcd3569

Browse files
committed
add put operation handler tests
1 parent 102ee5b commit bcd3569

File tree

2 files changed

+169
-1
lines changed

2 files changed

+169
-1
lines changed

src/Microsoft.OpenApi.OData.Reader/Operation/EntityUpdateOperationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
namespace Microsoft.OpenApi.OData.Operation
1616
{
1717
/// <summary>
18-
/// Base class for entity set update (patch or put) operation.
18+
/// Base class for entity set update (patch or put) operations.
1919
/// </summary>
2020
internal abstract class EntityUpdateOperationHandler : EntitySetOperationHandler
2121
{
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
// ------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
4+
// ------------------------------------------------------------
5+
6+
using System.Linq;
7+
using Microsoft.OData.Edm;
8+
using Microsoft.OpenApi.Extensions;
9+
using Microsoft.OpenApi.OData.Edm;
10+
using Microsoft.OpenApi.OData.Tests;
11+
using Xunit;
12+
13+
namespace Microsoft.OpenApi.OData.Operation.Tests
14+
{
15+
public class EntityPutOperationHandlerTests
16+
{
17+
private EntityPutOperationHandler _operationHandler = new EntityPutOperationHandler();
18+
19+
[Theory]
20+
[InlineData(true)]
21+
[InlineData(false)]
22+
public void CreateEntityPutOperationReturnsCorrectOperation(bool enableOperationId)
23+
{
24+
// Arrange
25+
IEdmModel model = EntitySetGetOperationHandlerTests.GetEdmModel("");
26+
IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet("Customers");
27+
OpenApiConvertSettings settings = new OpenApiConvertSettings
28+
{
29+
EnableOperationId = enableOperationId
30+
};
31+
ODataContext context = new ODataContext(model, settings);
32+
ODataPath path = new ODataPath(new ODataNavigationSourceSegment(entitySet), new ODataKeySegment(entitySet.EntityType()));
33+
34+
// Act
35+
var putOperation = _operationHandler.CreateOperation(context, path);
36+
37+
// Assert
38+
Assert.NotNull(putOperation);
39+
Assert.Equal("Update entity in Customers", putOperation.Summary);
40+
Assert.Equal("A business customer.", putOperation.Description);
41+
Assert.NotNull(putOperation.Tags);
42+
var tag = Assert.Single(putOperation.Tags);
43+
Assert.Equal("Customers.Customer", tag.Name);
44+
45+
Assert.NotNull(putOperation.Parameters);
46+
Assert.Equal(1, putOperation.Parameters.Count);
47+
48+
Assert.NotNull(putOperation.RequestBody);
49+
50+
Assert.NotNull(putOperation.Responses);
51+
Assert.Equal(2, putOperation.Responses.Count);
52+
Assert.Equal(new[] { "204", "default" }, putOperation.Responses.Select(r => r.Key));
53+
54+
if (enableOperationId)
55+
{
56+
Assert.Equal("Customers.Customer.UpdateCustomer", putOperation.OperationId);
57+
}
58+
else
59+
{
60+
Assert.Null(putOperation.OperationId);
61+
}
62+
}
63+
64+
[Theory]
65+
[InlineData(true)]
66+
[InlineData(false)]
67+
public void CreateEntityPutReturnsSecurityForUpdateRestrictions(bool enableAnnotation)
68+
{
69+
string annotation = @"<Annotation Term=""Org.OData.Capabilities.V1.UpdateRestrictions"">
70+
<Record>
71+
<PropertyValue Property=""Permissions"">
72+
<Collection>
73+
<Record>
74+
<PropertyValue Property=""SchemeName"" String=""Delegated (work or school account)"" />
75+
<PropertyValue Property=""Scopes"">
76+
<Collection>
77+
<Record>
78+
<PropertyValue Property=""Scope"" String=""User.ReadBasic.All"" />
79+
</Record>
80+
<Record>
81+
<PropertyValue Property=""Scope"" String=""User.Read.All"" />
82+
</Record>
83+
</Collection>
84+
</PropertyValue>
85+
</Record>
86+
<Record>
87+
<PropertyValue Property=""SchemeName"" String=""Application"" />
88+
<PropertyValue Property=""Scopes"">
89+
<Collection>
90+
<Record>
91+
<PropertyValue Property=""Scope"" String=""User.Read.All"" />
92+
</Record>
93+
<Record>
94+
<PropertyValue Property=""Scope"" String=""Directory.Read.All"" />
95+
</Record>
96+
</Collection>
97+
</PropertyValue>
98+
</Record>
99+
</Collection>
100+
</PropertyValue>
101+
<PropertyValue Property=""CustomHeaders"">
102+
<Collection>
103+
<Record>
104+
<PropertyValue Property=""Name"" String=""odata-debug"" />
105+
<PropertyValue Property=""Description"" String=""Debug support for OData services"" />
106+
<PropertyValue Property=""Required"" Bool=""false"" />
107+
<PropertyValue Property=""ExampleValues"">
108+
<Collection>
109+
<Record>
110+
<PropertyValue Property=""Value"" String=""html"" />
111+
<PropertyValue Property=""Description"" String=""Service responds with self-contained..."" />
112+
</Record>
113+
<Record>
114+
<PropertyValue Property=""Value"" String=""json"" />
115+
<PropertyValue Property=""Description"" String=""Service responds with JSON document..."" />
116+
</Record>
117+
</Collection>
118+
</PropertyValue>
119+
</Record>
120+
</Collection>
121+
</PropertyValue>
122+
</Record>
123+
</Annotation>";
124+
125+
// Arrange
126+
IEdmModel model = EntitySetGetOperationHandlerTests.GetEdmModel(enableAnnotation ? annotation : "");
127+
ODataContext context = new ODataContext(model);
128+
IEdmEntitySet customers = model.EntityContainer.FindEntitySet("Customers");
129+
Assert.NotNull(customers); // guard
130+
ODataPath path = new ODataPath(new ODataNavigationSourceSegment(customers), new ODataKeySegment(customers.EntityType()));
131+
132+
// Act
133+
var putOperation = _operationHandler.CreateOperation(context, path);
134+
135+
// Assert
136+
Assert.NotNull(putOperation);
137+
Assert.NotNull(putOperation.Security);
138+
139+
if (enableAnnotation)
140+
{
141+
Assert.Equal(2, putOperation.Security.Count);
142+
143+
string json = putOperation.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);
144+
Assert.Contains(@"
145+
""security"": [
146+
{
147+
""Delegated (work or school account)"": [
148+
""User.ReadBasic.All"",
149+
""User.Read.All""
150+
]
151+
},
152+
{
153+
""Application"": [
154+
""User.Read.All"",
155+
""Directory.Read.All""
156+
]
157+
}
158+
],".ChangeLineBreaks(), json);
159+
160+
Assert.Contains(putOperation.Parameters, p => p.Name == "odata-debug" && p.In == Models.ParameterLocation.Header);
161+
}
162+
else
163+
{
164+
Assert.Empty(putOperation.Security);
165+
}
166+
}
167+
}
168+
}

0 commit comments

Comments
 (0)