Skip to content

Commit 04a4575

Browse files
committed
web api integration test for OnDataProcessed added
1 parent a657de4 commit 04a4575

File tree

11 files changed

+87
-20
lines changed

11 files changed

+87
-20
lines changed

src/JQDT.MVC/JQDataTableAttribute.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public override void OnActionExecuted(ActionExecutedContext filterContext)
4242
/// <param name="requestInfoModel">The request information model.</param>
4343
public virtual void OnDataProcessed(ref object data, RequestInfoModel requestInfoModel)
4444
{
45+
// No data processing logic by default;
4546
}
4647

4748
private void PerformOnActionExecuted(ActionExecutedContext filterContext)

src/JQDT.WebAPI/JQDataTableAttribute.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,33 @@ public override void OnActionExecuted(HttpActionExecutedContext actionExecutedCo
3232
base.OnActionExecuted(actionExecutedContext);
3333
}
3434

35+
/// <summary>
36+
/// Called when [data processed].
37+
/// </summary>
38+
/// <param name="data">The data.</param>
39+
/// <param name="requestInfoModel">The request information model.</param>
40+
public virtual void OnDataProcessed(ref object data, RequestInfoModel requestInfoModel)
41+
{
42+
// No data processing logic by default;
43+
}
44+
3545
private void PerformOnActionExecuted(HttpActionExecutedContext actionExecutedContext)
3646
{
3747
var modelType = ((System.Net.Http.ObjectContent)actionExecutedContext.Response.Content).ObjectType;
3848
var applicationInitizlizationFunction = ExecuteFunctionProvider<HttpActionExecutedContext>.GetAppInicializationFunc(modelType, typeof(ApplicationWebApi<>));
3949
var dependencyResolver = new DI.DependencyResolver();
4050
var webApiApplication = applicationInitizlizationFunction(actionExecutedContext, dependencyResolver);
51+
this.SubscribeToEvents(webApiApplication);
4152
var result = (ResultModel)webApiApplication.Execute();
4253
var formattedObjectResult = this.GetObjectResult(result);
4354
actionExecutedContext.Response.Content = new ObjectContent(typeof(object), formattedObjectResult, new JsonMediaTypeFormatter());
4455
}
4556

57+
private void SubscribeToEvents(IApplicationBase application)
58+
{
59+
application.OnDataProcessed += this.OnDataProcessed;
60+
}
61+
4662
private object GetObjectResult(ResultModel result)
4763
{
4864
return new
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace TestData.Data.Models
2+
{
3+
public class OnActionExecutedTestModel
4+
{
5+
public int Number { get; set; }
6+
}
7+
}

src/Tests/TestData.Data/TestData.Data.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<Compile Include="DataGenerator.cs" />
6262
<Compile Include="Models\AllTypesModel.cs" />
6363
<Compile Include="Models\ComplexModel.cs" />
64+
<Compile Include="Models\OnActionExecutedTestModel.cs" />
6465
<Compile Include="Properties\AssemblyInfo.cs" />
6566
</ItemGroup>
6667
<ItemGroup>

src/Tests/integrationtests/Tests.Integration.Mvc/Controllers/TestOnActionExecutedController.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
using System.Linq;
55
using System.Web.Mvc;
66
using JQDT.Models;
7+
using TestData.Data.Models;
78

89
public class TestOnActionExecutedController : Controller
910
{
1011
public ActionResult Index()
1112
{
13+
this.ViewBag.dataSourceApp = Configuration.SettingsProvider.Get("dataSourceApp");
14+
this.ViewBag.WebApi2Url = Configuration.SettingsProvider.Get("webApi2Url");
15+
1216
return View();
1317
}
1418

@@ -25,11 +29,6 @@ public ActionResult GetData()
2529
}
2630
}
2731

28-
public class OnActionExecutedTestModel
29-
{
30-
public int Number { get; set; }
31-
}
32-
3332
public class CustomOnActionExecutedAttribute : JQDT.MVC.JQDataTableAttribute
3433
{
3534
public override void OnDataProcessed(ref object data, RequestInfoModel requestInfoModel)

src/Tests/integrationtests/Tests.Integration.Mvc/Views/TestOnActionExecuted/Index.cshtml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
2-
@{
1+
@{
32
ViewBag.Title = "Index";
43
Layout = "~/Views/Shared/_Layout.cshtml";
54
}
65

7-
86
@{
97
ViewBag.Title = "Index";
108
Layout = "~/Views/Shared/_Layout.cshtml";
@@ -30,13 +28,18 @@
3028
"proccessing": true,
3129
"serverSide": true,
3230
"ajax": {
33-
url: "@Url.Action("GetData", "TestOnActionExecuted")",
34-
type: 'POST'
31+
@if (ViewBag.dataSourceApp == "mvc")
32+
{
33+
@:url: "@Url.Action("GetData", "TestOnActionExecuted")",
34+
} else if (ViewBag.dataSourceApp == "webapi2")
35+
{
36+
@:url: "@ViewBag.WebApi2Url/OnActionExecutedTest",
37+
}
38+
type: 'POST',
3539
},
3640
"columns": [
3741
{ "data": "Number" },
3842
]
3943
});
4044
</script>
41-
}
42-
45+
}

src/Tests/integrationtests/Tests.Integration.Mvc/Web.config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727

2828
<add key="webApi2Url" value=" http://localhost:50178/api" />
2929

30-
<add key="dataSourceApp" value="mvc" />
31-
<!--<add key="dataSourceApp" value="webapi2" />-->
30+
<!--<add key="dataSourceApp" value="mvc" />-->
31+
<add key="dataSourceApp" value="webapi2" />
3232
</appSettings>
3333
<system.web>
3434
<compilation debug="true" targetFramework="4.6.1" />

src/Tests/integrationtests/Tests.Integration.WebApi2/App_Start/WebApiConfig.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Web.Http;
1+
using System.Web.Http;
52

63
namespace Tests.Integration.WebApi2
74
{
@@ -19,6 +16,8 @@ public static void Register(HttpConfiguration config)
1916
routeTemplate: "api/{controller}/{id}",
2017
defaults: new { id = RouteParameter.Optional }
2118
);
19+
20+
config.Filters.Add(new EnableCorsAttribute());
2221
}
2322
}
24-
}
23+
}

src/Tests/integrationtests/Tests.Integration.WebApi2/Controllers/HomeController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using JQDT.WebAPI;
66
using TestData.Models;
77

8-
[EnableCors]
98
public class HomeController : ApiController
109
{
1110
public static IQueryable<AllTypesModel> Data { get; set; }
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace Tests.Integration.WebApi2.Controllers
2+
{
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Web.Http;
6+
using JQDT.Models;
7+
using TestData.Data.Models;
8+
9+
public class OnActionExecutedTestController : ApiController
10+
{
11+
[CustomOnActionExecuted]
12+
public IHttpActionResult Post()
13+
{
14+
var data = new List<OnActionExecutedTestModel>();
15+
for (int i = 1; i <= 5; i++)
16+
{
17+
data.Add(new OnActionExecutedTestModel { Number = i });
18+
}
19+
20+
return this.Ok(data.AsQueryable());
21+
}
22+
}
23+
24+
public class CustomOnActionExecutedAttribute : JQDT.WebAPI.JQDataTableAttribute
25+
{
26+
public override void OnDataProcessed(ref object data, RequestInfoModel requestInfoModel)
27+
{
28+
var list = ((IQueryable<OnActionExecutedTestModel>)data).ToList();
29+
for (int i = 6; i <= 10; i++)
30+
{
31+
list.Add(new OnActionExecutedTestModel { Number = i });
32+
}
33+
34+
data = list.AsQueryable();
35+
}
36+
}
37+
}

src/Tests/integrationtests/Tests.Integration.WebApi2/Tests.Integration.WebApi2.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
<Compile Include="App_Start\EnableCorsAttribute.cs" />
149149
<Compile Include="App_Start\WebApiConfig.cs" />
150150
<Compile Include="Controllers\HomeController.cs" />
151+
<Compile Include="Controllers\OnActionExecutedTestController.cs" />
151152
<Compile Include="Global.asax.cs">
152153
<DependentUpon>Global.asax</DependentUpon>
153154
</Compile>
@@ -172,6 +173,10 @@
172173
<Project>{A57F2EDC-8DC9-4A9A-BD3D-5CA31EE56910}</Project>
173174
<Name>JQDT.WebAPI</Name>
174175
</ProjectReference>
176+
<ProjectReference Include="..\..\..\JQDT\JQDT.csproj">
177+
<Project>{C82E4675-88AB-4B78-8475-F1521BDAE330}</Project>
178+
<Name>JQDT</Name>
179+
</ProjectReference>
175180
<ProjectReference Include="..\..\TestData.Data\TestData.Data.csproj">
176181
<Project>{F7F12DE8-F3C3-42A4-BBE4-1868D287D388}</Project>
177182
<Name>TestData.Data</Name>

0 commit comments

Comments
 (0)