Skip to content

Commit 3a83a16

Browse files
committed
Web Api 2 actin filter attribute event methods unit tests added
1 parent c25a83b commit 3a83a16

File tree

3 files changed

+215
-0
lines changed

3 files changed

+215
-0
lines changed
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.Specialized;
4+
using System.Linq;
5+
using System.Net.Http;
6+
using System.Net.Http.Formatting;
7+
using System.Web.Http;
8+
using System.Web.Http.Controllers;
9+
using System.Web.Http.Filters;
10+
using System.Web.Http.Routing;
11+
using JQDT.Application;
12+
using JQDT.DI;
13+
using JQDT.ModelBinders;
14+
using JQDT.Models;
15+
using JQDT.WebAPI;
16+
using Moq;
17+
using NUnit.Framework;
18+
using Tests.UnitTests.Mocks;
19+
using Tests.UnitTests.Models;
20+
21+
namespace Tests.UnitTests
22+
{
23+
public class JQDataTableAttributeWebApi2UnitTests
24+
{
25+
[Test]
26+
[TestCase(nameof(JQDataTableAttribute.OnDataProcessing))]
27+
[TestCase(nameof(JQDataTableAttribute.OnSearchDataProcessing))]
28+
[TestCase(nameof(JQDataTableAttribute.OnSearchDataProcessed))]
29+
[TestCase(nameof(JQDataTableAttribute.OnCustomFiltersDataProcessing))]
30+
[TestCase(nameof(JQDataTableAttribute.OnCustomFiltersDataProcessed))]
31+
[TestCase(nameof(JQDataTableAttribute.OnColumnsFilterDataProcessing))]
32+
[TestCase(nameof(JQDataTableAttribute.OnColumnsFilterDataProcessed))]
33+
[TestCase(nameof(JQDataTableAttribute.OnSortDataProcessing))]
34+
[TestCase(nameof(JQDataTableAttribute.OnSortDataProcessed))]
35+
[TestCase(nameof(JQDataTableAttribute.OnPagingDataProcessing))]
36+
[TestCase(nameof(JQDataTableAttribute.OnPagingDataProcessed))]
37+
[TestCase(nameof(JQDataTableAttribute.OnDataProcessed))]
38+
public void AllEventsInsideWebApiJQDTAttributeShouldBeRaized(string eventName)
39+
{
40+
var serviceLocatorMock = this.GetServiceLocatorMock();
41+
var executeFunctionMock = this.GetExecuteFunctionProviderMock(serviceLocatorMock);
42+
var contextFake = this.GetHttpContextFake();
43+
44+
var testAttr = new JQDataTableWebApi2TestAttribute(serviceLocatorMock.Object, executeFunctionMock.Object);
45+
46+
testAttr.OnActionExecuted(contextFake);
47+
48+
Assert.IsTrue(testAttr.CalledEvents.Contains(eventName));
49+
}
50+
51+
[Test]
52+
public void AllEventsInsideWebApi2JQDTAttributeShouldBeCalledInCorrectOrder()
53+
{
54+
var serviceLocatorMock = this.GetServiceLocatorMock();
55+
var executeFunctionMock = this.GetExecuteFunctionProviderMock(serviceLocatorMock);
56+
var contextFake = this.GetHttpContextFake();
57+
58+
var testAttr = new JQDataTableWebApi2TestAttribute(serviceLocatorMock.Object, executeFunctionMock.Object);
59+
60+
testAttr.OnActionExecuted(contextFake);
61+
62+
var expectedEventsCalls = new List<string>
63+
{
64+
nameof(JQDataTableAttribute.OnDataProcessing),
65+
nameof(JQDataTableAttribute.OnSearchDataProcessing),
66+
nameof(JQDataTableAttribute.OnSearchDataProcessed),
67+
nameof(JQDataTableAttribute.OnCustomFiltersDataProcessing),
68+
nameof(JQDataTableAttribute.OnCustomFiltersDataProcessed),
69+
nameof(JQDataTableAttribute.OnColumnsFilterDataProcessing),
70+
nameof(JQDataTableAttribute.OnColumnsFilterDataProcessed),
71+
nameof(JQDataTableAttribute.OnSortDataProcessing),
72+
nameof(JQDataTableAttribute.OnSortDataProcessed),
73+
nameof(JQDataTableAttribute.OnPagingDataProcessing),
74+
nameof(JQDataTableAttribute.OnPagingDataProcessed),
75+
nameof(JQDataTableAttribute.OnDataProcessed),
76+
};
77+
78+
Assert.IsTrue(expectedEventsCalls.SequenceEqual(testAttr.CalledEvents));
79+
}
80+
81+
private HttpActionExecutedContext GetHttpContextFake()
82+
{
83+
var contextFake = new HttpActionExecutedContext(
84+
new HttpActionContext(
85+
new HttpControllerContext(
86+
new HttpConfiguration(),
87+
new Mock<IHttpRouteData>().Object,
88+
new HttpRequestMessage()),
89+
new Mock<HttpActionDescriptor>().Object),
90+
null);
91+
92+
contextFake.Response = new HttpResponseMessage()
93+
{
94+
Content = new ObjectContent(typeof(IQueryable<IntModel>), new List<IntModel>().AsQueryable(), new JsonMediaTypeFormatter()),
95+
StatusCode = System.Net.HttpStatusCode.OK
96+
};
97+
98+
return contextFake;
99+
}
100+
101+
private Mock<IServiceLocator> GetServiceLocatorMock()
102+
{
103+
var serviceLocatorMock = new Mock<IServiceLocator>();
104+
serviceLocatorMock.Setup(x => x.GetSearchDataProcessor<IntModel>()).Returns(new DataProcessorFilterFake<IntModel>());
105+
serviceLocatorMock.Setup(x => x.GetColumnsFilterDataProcessor<IntModel>()).Returns(new DataProcessorFilterFake<IntModel>());
106+
serviceLocatorMock.Setup(x => x.GetCustomFiltersDataProcessor<IntModel>()).Returns(new DataProcessorFilterFake<IntModel>());
107+
serviceLocatorMock.Setup(x => x.GetPagingDataProcessor<IntModel>()).Returns(new DataProcessorNotFilterFake<IntModel>());
108+
serviceLocatorMock.Setup(x => x.GetSortDataProcessor<IntModel>()).Returns(new DataProcessorNotFilterFake<IntModel>());
109+
110+
return serviceLocatorMock;
111+
}
112+
113+
private Mock<IExecuteFunctionProvider<HttpActionExecutedContext>> GetExecuteFunctionProviderMock(Mock<IServiceLocator> serviceLocatorMock)
114+
{
115+
var modelBinderMock = new Mock<IFormModelBinder>();
116+
modelBinderMock.Setup(x => x.BindModel<IQueryable<IntModel>>(It.IsAny<NameValueCollection>(), It.IsAny<IQueryable<IntModel>>())).Returns(new RequestInfoModel() { TableParameters = new DataTableAjaxPostModel { Draw = 0 } });
117+
118+
var executeFunctionProviderMock = new Mock<IExecuteFunctionProvider<HttpActionExecutedContext>>();
119+
executeFunctionProviderMock.Setup(x => x.GetAppInicializationFunc(It.IsAny<Type>(), It.IsAny<Type>())).Returns(new Func<HttpActionExecutedContext, IServiceLocator, IFormModelBinder, IApplicationBase>((ac, sl, fmb) => { return new TestApplication<IntModel>(serviceLocatorMock.Object, modelBinderMock.Object); }));
120+
121+
return executeFunctionProviderMock;
122+
}
123+
}
124+
125+
public class JQDataTableWebApi2TestAttribute : JQDataTableAttribute
126+
{
127+
public List<string> CalledEvents { get; private set; }
128+
129+
internal JQDataTableWebApi2TestAttribute(IServiceLocator serviceLocator, IExecuteFunctionProvider<HttpActionExecutedContext> executeFunctionProvider)
130+
: base(serviceLocator, executeFunctionProvider)
131+
{
132+
this.CalledEvents = new List<string>();
133+
}
134+
135+
public override void OnDataProcessing(ref object data, RequestInfoModel requestInfoModel)
136+
{
137+
this.CalledEvents.Add(nameof(JQDataTableAttribute.OnDataProcessing));
138+
}
139+
140+
public override void OnDataProcessed(ref object data, RequestInfoModel requestInfoModel)
141+
{
142+
this.CalledEvents.Add(nameof(JQDataTableAttribute.OnDataProcessed));
143+
}
144+
145+
public override void OnSearchDataProcessing(ref object data, RequestInfoModel requestInfoModel)
146+
{
147+
this.CalledEvents.Add(nameof(JQDataTableAttribute.OnSearchDataProcessing));
148+
}
149+
150+
public override void OnSearchDataProcessed(ref object data, RequestInfoModel requestInfoModel)
151+
{
152+
this.CalledEvents.Add(nameof(JQDataTableAttribute.OnSearchDataProcessed));
153+
}
154+
155+
public override void OnCustomFiltersDataProcessing(ref object data, RequestInfoModel requestInfoModel)
156+
{
157+
this.CalledEvents.Add(nameof(JQDataTableAttribute.OnCustomFiltersDataProcessing));
158+
}
159+
160+
public override void OnCustomFiltersDataProcessed(ref object data, RequestInfoModel requestInfoModel)
161+
{
162+
this.CalledEvents.Add(nameof(JQDataTableAttribute.OnCustomFiltersDataProcessed));
163+
}
164+
165+
public override void OnColumnsFilterDataProcessing(ref object data, RequestInfoModel requestInfoModel)
166+
{
167+
this.CalledEvents.Add(nameof(JQDataTableAttribute.OnColumnsFilterDataProcessing));
168+
}
169+
170+
public override void OnColumnsFilterDataProcessed(ref object data, RequestInfoModel requestInfoModel)
171+
{
172+
this.CalledEvents.Add(nameof(JQDataTableAttribute.OnColumnsFilterDataProcessed));
173+
}
174+
175+
public override void OnSortDataProcessing(ref object data, RequestInfoModel requestInfoModel)
176+
{
177+
this.CalledEvents.Add(nameof(JQDataTableAttribute.OnSortDataProcessing));
178+
}
179+
180+
public override void OnSortDataProcessed(ref object data, RequestInfoModel requestInfoModel)
181+
{
182+
this.CalledEvents.Add(nameof(JQDataTableAttribute.OnSortDataProcessed));
183+
}
184+
185+
public override void OnPagingDataProcessing(ref object data, RequestInfoModel requestInfoModel)
186+
{
187+
this.CalledEvents.Add(nameof(JQDataTableAttribute.OnPagingDataProcessing));
188+
}
189+
190+
public override void OnPagingDataProcessed(ref object data, RequestInfoModel requestInfoModel)
191+
{
192+
this.CalledEvents.Add(nameof(JQDataTableAttribute.OnPagingDataProcessed));
193+
}
194+
}
195+
}

src/Tests/UnitTests/Tests.UnitTests/Tests.UnitTests.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@
6565
<Reference Include="Moq, Version=4.7.145.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
6666
<HintPath>..\..\..\packages\Moq.4.7.145\lib\net45\Moq.dll</HintPath>
6767
</Reference>
68+
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
69+
<HintPath>..\..\..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
70+
<Private>True</Private>
71+
</Reference>
6872
<Reference Include="nunit.framework, Version=3.9.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
6973
<HintPath>..\..\..\packages\NUnit.3.9.0\lib\net45\nunit.framework.dll</HintPath>
7074
</Reference>
@@ -74,10 +78,20 @@
7478
<Reference Include="System.Linq.Dynamic, Version=1.0.6132.35681, Culture=neutral, processorArchitecture=MSIL">
7579
<HintPath>..\..\..\packages\System.Linq.Dynamic.1.0.7\lib\net40\System.Linq.Dynamic.dll</HintPath>
7680
</Reference>
81+
<Reference Include="System.Net.Http" />
82+
<Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
83+
<HintPath>..\..\..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
84+
</Reference>
7785
<Reference Include="System.Web" />
7886
<Reference Include="System.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
7987
<HintPath>..\..\..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.Helpers.dll</HintPath>
8088
</Reference>
89+
<Reference Include="System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
90+
<HintPath>..\..\..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll</HintPath>
91+
</Reference>
92+
<Reference Include="System.Web.Http.WebHost, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
93+
<HintPath>..\..\..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.3\lib\net45\System.Web.Http.WebHost.dll</HintPath>
94+
</Reference>
8195
<Reference Include="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
8296
<HintPath>..\..\..\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll</HintPath>
8397
</Reference>
@@ -97,6 +111,7 @@
97111
<ItemGroup>
98112
<Compile Include="ApplicationBaseUnitTests.cs" />
99113
<Compile Include="JQDataTableAttributeMvcUnitTests.cs" />
114+
<Compile Include="JQDataTableAttributeWebApi2UnitTests.cs" />
100115
<Compile Include="Mocks\AppMock.cs" />
101116
<Compile Include="ColumnsFilterDataProcessorUnitTests.cs" />
102117
<Compile Include="Common\TestHelpers.cs" />

src/Tests/UnitTests/Tests.UnitTests/packages.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
<package id="FakeData" version="1.0.0" targetFramework="net461" />
55
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net461" />
66
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net461" />
7+
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net461" />
8+
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net461" />
9+
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net461" />
10+
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net461" />
711
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net461" />
812
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net461" />
913
<package id="Moq" version="4.7.145" targetFramework="net461" />
1014
<package id="MSTest.TestAdapter" version="1.2.0" targetFramework="net461" />
1115
<package id="MSTest.TestFramework" version="1.2.0" targetFramework="net461" />
16+
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net461" />
1217
<package id="NUnit" version="3.9.0" targetFramework="net461" />
1318
<package id="System.Linq.Dynamic" version="1.0.7" targetFramework="net461" />
1419
</packages>

0 commit comments

Comments
 (0)