Skip to content

Commit 2083ded

Browse files
committed
Add E2E Test for DynamicProperty Issue #330 PR #350
1 parent 8f21df3 commit 2083ded

5 files changed

Lines changed: 221 additions & 6 deletions

File tree

OData/src/System.Web.OData/OData/Routing/Conventions/DynamicPropertyRoutingConvention.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Net.Http;
77
using System.Web.Http;
88
using System.Web.Http.Controllers;
9+
using System.Web.OData.Extensions;
910
using System.Web.OData.Formatter;
1011
using Microsoft.OData.Edm.Library;
1112

@@ -84,7 +85,7 @@ public override string SelectAction(ODataPath odataPath, HttpControllerContext c
8485
if (controllerContext.Request.Method == HttpMethod.Get)
8586
{
8687
string actionNamePrefix = String.Format(CultureInfo.InvariantCulture, "Get{0}", _actionName);
87-
actionName = actionMap.FindMatchingAction(actionNamePrefix + "From" + complexType.Name);
88+
actionName = actionMap.FindMatchingAction(actionNamePrefix + "From" + propertyAccessSegment.Property.Name);
8889
}
8990
break;
9091
default: break;
@@ -98,11 +99,11 @@ public override string SelectAction(ODataPath odataPath, HttpControllerContext c
9899
controllerContext.RouteData.Values[ODataRouteConstants.Key] = keyValueSegment.Value;
99100
}
100101

101-
controllerContext.RouteData.Values[ODataRouteConstants.DynamicProperty] =
102-
dynamicPropertSegment.PropertyName;
103-
controllerContext.RouteData.Values[ODataParameterValue.ParameterValuePrefix + ODataRouteConstants.DynamicProperty] =
104-
new ODataParameterValue(dynamicPropertSegment.PropertyName,
105-
EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(string)));
102+
controllerContext.RouteData.Values[ODataRouteConstants.DynamicProperty] = dynamicPropertSegment.PropertyName;
103+
var key = ODataParameterValue.ParameterValuePrefix + ODataRouteConstants.DynamicProperty;
104+
var value = new ODataParameterValue(dynamicPropertSegment.PropertyName, EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(string)));
105+
controllerContext.RouteData.Values[key] = value;
106+
controllerContext.Request.ODataProperties().RoutingConventionsStore[key] = value;
106107
return actionName;
107108
}
108109
return null;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Web.Http;
2+
using System.Web.OData;
3+
using System.Web.OData.Routing;
4+
5+
namespace WebStack.QA.Test.OData.Routing.DynamicProperties
6+
{
7+
public class DynamicCustomersController : ODataController
8+
{
9+
public IHttpActionResult GetId(int key)
10+
{
11+
return Ok(string.Format("{0}_{1}", "Id", key));
12+
}
13+
14+
public IHttpActionResult GetDynamicProperty(int key, string dynamicProperty)
15+
{
16+
return Ok(string.Format("{0}_{1}_{2}", dynamicProperty, "GetDynamicProperty", key));
17+
}
18+
19+
public IHttpActionResult GetDynamicPropertyFromAccount([FromODataUri] int key, [FromODataUri] string dynamicProperty)
20+
{
21+
return Ok(string.Format("{0}_{1}_{2}", dynamicProperty, "GetDynamicPropertyFromAccount", key));
22+
}
23+
24+
[HttpGet]
25+
[ODataRoute("DynamicCustomers({id})/Order/{pName:dynamicproperty}")]
26+
public IHttpActionResult GetDynamicPropertyFromOrder([FromODataUri] int id, [FromODataUri] string pName)
27+
{
28+
return Ok(string.Format("{0}_{1}_{2}", pName, "GetDynamicPropertyFromOrder", id));
29+
}
30+
}
31+
32+
public class DynamicSingleCustomerController : ODataController
33+
{
34+
public IHttpActionResult GetDynamicProperty(string dynamicProperty)
35+
{
36+
return Ok(string.Format("{0}_{1}", dynamicProperty, "GetDynamicProperty"));
37+
}
38+
39+
public IHttpActionResult GetDynamicPropertyFromAccount([FromODataUri] string dynamicProperty)
40+
{
41+
return Ok(string.Format("{0}_{1}", dynamicProperty, "GetDynamicPropertyFromAccount"));
42+
}
43+
44+
[HttpGet]
45+
[ODataRoute("DynamicSingleCustomer/Order/{pName:dynamicproperty}")]
46+
public IHttpActionResult GetDynamicPropertyFromOrder(string pName)
47+
{
48+
return Ok(string.Format("{0}_{1}", pName, "GetDynamicPropertyFromOrder"));
49+
}
50+
}
51+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.Collections.Generic;
2+
3+
namespace WebStack.QA.Test.OData.Routing.DynamicProperties
4+
{
5+
public class DynamicCustomer
6+
{
7+
public int Id { get; set; }
8+
9+
public Account Account { get; set; }
10+
11+
public Order Order { get; set; }
12+
13+
public Account SecondAccount { get; set; }
14+
15+
public Dictionary<string, object> DynamicProperties { get; set; }
16+
}
17+
18+
public class Order
19+
{
20+
public string Name { get; set; }
21+
22+
public Dictionary<string, object> DynamicProperties { get; set; }
23+
}
24+
25+
public class Account
26+
{
27+
public string Name { get; set; }
28+
29+
public string Number { get; set; }
30+
31+
public Dictionary<string, object> DynamicProperties { get; set; }
32+
}
33+
34+
public class DynamicVipCustomer : DynamicCustomer
35+
{
36+
public string VipCode { get; set; }
37+
}
38+
39+
public class DynamicSingleCustomer
40+
{
41+
public int Id { get; set; }
42+
43+
public string Name { get; set; }
44+
45+
public Account Account { get; set; }
46+
47+
public Order Order { get; set; }
48+
49+
public Account SecondAccount { get; set; }
50+
51+
public Dictionary<string, object> DynamicProperties { get; set; }
52+
}
53+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using System.Net;
2+
using System.Net.Http;
3+
using System.Web.Http;
4+
using System.Web.Http.Dispatcher;
5+
using System.Web.OData;
6+
using System.Web.OData.Builder;
7+
using System.Web.OData.Extensions;
8+
using Microsoft.OData.Edm;
9+
using Nuwa;
10+
using WebStack.QA.Test.OData.Common;
11+
using Xunit;
12+
using Xunit.Extensions;
13+
14+
namespace WebStack.QA.Test.OData.Routing.DynamicProperties
15+
{
16+
[NuwaFramework]
17+
[NuwaTrace(NuwaTraceAttribute.Tag.Off)]
18+
public class DynamicPropertiesTest
19+
{
20+
[NuwaBaseAddress]
21+
public string BaseAddress { get; set; }
22+
23+
[NuwaHttpClient]
24+
public HttpClient Client { get; set; }
25+
[NuwaConfiguration]
26+
public static void UpdateConfiguration(HttpConfiguration configuration)
27+
{
28+
var controllers = new[] {
29+
typeof(DynamicCustomersController),
30+
typeof(DynamicSingleCustomerController),
31+
typeof(MetadataController),
32+
};
33+
34+
TestAssemblyResolver resolver = new TestAssemblyResolver(new TypesInjectionAssembly(controllers));
35+
configuration.Services.Replace(typeof(IAssembliesResolver), resolver);
36+
37+
configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
38+
39+
configuration.Routes.Clear();
40+
configuration.GetHttpServer();
41+
configuration.MapODataServiceRoute(routeName: "odata", routePrefix: "odata",
42+
model: GetEdmModel());
43+
44+
configuration.EnsureInitialized();
45+
}
46+
47+
[Theory]
48+
[InlineData("DynamicCustomers(1)/DynamicPropertyName", "DynamicPropertyName_GetDynamicProperty_1")]
49+
[InlineData("DynamicCustomers(2)/Account/DynamicPropertyName", "DynamicPropertyName_GetDynamicPropertyFromAccount_2")]
50+
[InlineData("DynamicCustomers(3)/Order/DynamicPropertyName", "DynamicPropertyName_GetDynamicPropertyFromOrder_3")]
51+
[InlineData("DynamicCustomers(4)/WebStack.QA.Test.OData.Routing.DynamicProperties.DynamicVipCustomer/DynamicPropertyName", "DynamicPropertyName_GetDynamicProperty_4")]
52+
[InlineData("DynamicCustomers(5)/WebStack.QA.Test.OData.Routing.DynamicProperties.DynamicVipCustomer/Account/DynamicPropertyName", "DynamicPropertyName_GetDynamicPropertyFromAccount_5")]
53+
[InlineData("DynamicSingleCustomer/DynamicPropertyName", "DynamicPropertyName_GetDynamicProperty")]
54+
[InlineData("DynamicSingleCustomer/Account/DynamicPropertyName", "DynamicPropertyName_GetDynamicPropertyFromAccount")]
55+
[InlineData("DynamicSingleCustomer/Order/DynamicPropertyName", "DynamicPropertyName_GetDynamicPropertyFromOrder")]
56+
[InlineData("DynamicCustomers(1)/Id", "Id_1")]
57+
public void AccessPropertyTest(string uri, string expected)
58+
{
59+
string requestUri = string.Format("{0}/odata/{1}", BaseAddress, uri);
60+
61+
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri);
62+
63+
var response = Client.SendAsync(request).Result;
64+
65+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
66+
Assert.Contains(expected, response.Content.ReadAsStringAsync().Result);
67+
}
68+
69+
[Theory]
70+
[InlineData("Put")]
71+
[InlineData("Patch")]
72+
[InlineData("Post")]
73+
[InlineData("Delete")]
74+
public void AccessDynamicPropertyWithWrongMethodTest(string method)
75+
{
76+
string requestUri = string.Format("{0}/odata/DynamicCustomers(1)/DynamicPropertyName", BaseAddress);
77+
78+
HttpRequestMessage request = new HttpRequestMessage(new HttpMethod(method), requestUri);
79+
80+
var response = Client.SendAsync(request).Result;
81+
82+
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
83+
}
84+
85+
[Theory]
86+
[InlineData("DynamicCustomers(2)/SecondAccount/DynamicPropertyName")]
87+
[InlineData("DynamicSingleCustomer/SecondAccount/DynamicPropertyName")]
88+
public void AccessDynamicPropertyWithoutImplementMethod(string uri)
89+
{
90+
string requestUri = string.Format("{0}/odata/{1}", BaseAddress, uri);
91+
92+
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri);
93+
94+
var response = Client.SendAsync(request).Result;
95+
96+
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
97+
}
98+
99+
private static IEdmModel GetEdmModel()
100+
{
101+
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
102+
builder.EntitySet<DynamicCustomer>("DynamicCustomers");
103+
builder.Singleton<DynamicSingleCustomer>("DynamicSingleCustomer");
104+
return builder.GetEdmModel();
105+
}
106+
}
107+
}

OData/test/E2ETest/WebStack.QA.Test.OData/WebStack.QA.Test.OData.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@
152152
<Compile Include="DollarLevels\DollarLevelsDataModel.cs" />
153153
<Compile Include="DollarLevels\DollarLevelsEdmModel.cs" />
154154
<Compile Include="DollarLevels\DollarLevelsTest.cs" />
155+
<Compile Include="Routing\DynamicProperties\DynamicPropertiesController.cs" />
156+
<Compile Include="Routing\DynamicProperties\DynamicPropertiesModel.cs" />
157+
<Compile Include="Routing\DynamicProperties\DynamicPropertiesTest.cs" />
155158
<Compile Include="UriParserExtension\CaseInsensitiveTest.cs" />
156159
<Compile Include="UriParserExtension\EnumPrefixFreeTest.cs" />
157160
<Compile Include="UriParserExtension\UnqualifiedCallTest.cs" />

0 commit comments

Comments
 (0)