Skip to content

Commit dec441b

Browse files
Part 11
Part 11
1 parent 657c44e commit dec441b

File tree

6 files changed

+231
-6
lines changed

6 files changed

+231
-6
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace UserManagement.API.IntegrationTests
2+
{
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.AspNetCore.Mvc.Testing;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using System.Data;
7+
using System.Data.SqlClient;
8+
using System.Net.Http;
9+
using UserManagement.Application.Common.Interfaces;
10+
using UserManagement.Domain.UnitOfWork;
11+
using UserManagement.Persistence.Constant;
12+
using UserManagement.Persistence.UnitOfWork;
13+
14+
/// <summary>
15+
/// The APIs test setup.
16+
/// </summary>
17+
/// <typeparam name="TStartup">The input object.</typeparam>
18+
public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup>
19+
where TStartup : class
20+
{
21+
/// <summary>
22+
/// Get the anonymous client for testing.
23+
/// </summary>
24+
/// <returns>The anonymous client.</returns>
25+
public HttpClient GetAnonymousClient()
26+
{
27+
return this.CreateClient();
28+
}
29+
30+
/// <summary>
31+
/// Setup for middleware.
32+
/// </summary>
33+
/// <param name="builder">Take the WebHostBuilder object.</param>
34+
protected override void ConfigureWebHost(IWebHostBuilder builder)
35+
{
36+
builder
37+
.ConfigureServices(services =>
38+
{
39+
services.AddSingleton<IConfigConstants, ConfigConstants>();
40+
services.AddSingleton<IDbConnection>(conn => new SqlConnection(conn.GetService<IConfigConstants>().TestFullStackConnection));
41+
services.AddTransient<IUnitOfWork>(uof => new UnitOfWork(uof.GetService<IDbConnection>()));
42+
});
43+
}
44+
}
45+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace UserManagement.API.IntegrationTests
2+
{
3+
using System.Net.Http;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Newtonsoft.Json;
7+
8+
/// <summary>
9+
/// Generic methods for JSON serialization.
10+
/// </summary>
11+
public static class IntegrationTestHelper
12+
{
13+
/// <summary>
14+
/// Serialize the JSON in API response.
15+
/// </summary>
16+
/// <param name="obj">The input object to be serialized.</param>
17+
/// <returns>Return the string conent the JSON.</returns>
18+
public static StringContent GetRequestContent(object obj)
19+
{
20+
return new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json");
21+
}
22+
23+
/// <summary>
24+
/// Deserialize the JSON.
25+
/// </summary>
26+
/// <typeparam name="T">Input genric class object.</typeparam>
27+
/// <param name="response">The HttpResponseMessage object.</param>
28+
/// <returns>The result.</returns>
29+
public static async Task<T> GetResponseContent<T>(HttpResponseMessage response)
30+
{
31+
var stringResponse = await response.Content.ReadAsStringAsync();
32+
33+
var result = JsonConvert.DeserializeObject<T>(stringResponse);
34+
35+
return result;
36+
}
37+
}
38+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
namespace UserManagement.API.IntegrationTests.User
2+
{
3+
using Xunit;
4+
using System.Threading.Tasks;
5+
using Shouldly;
6+
using UserManagement.Application.User.Commands;
7+
using System.Net;
8+
using System;
9+
10+
public class UserAPI : IClassFixture<CustomWebApplicationFactory<Startup>>
11+
{
12+
private readonly CustomWebApplicationFactory<Startup> factory;
13+
14+
public UserAPI(CustomWebApplicationFactory<Startup> factory)
15+
{
16+
this.factory = factory;
17+
}
18+
19+
[Fact]
20+
public async Task GivenValidGetAllUserQuery_ReturnSuccessObject()
21+
{
22+
await AddNewUser();
23+
var client = this.factory.GetAnonymousClient();
24+
var response = await client.GetAsync($"api/User/GetAll");
25+
response.EnsureSuccessStatusCode();
26+
}
27+
28+
[Fact]
29+
public async Task GivenValidGetUserQuery_ReturnSuccessObject()
30+
{
31+
var res = await AddNewUser();
32+
var client = this.factory.GetAnonymousClient();
33+
var response = await client.GetAsync($"api/User/Get?userID={res}");
34+
var result = response.Content.ReadAsStringAsync().Result;
35+
response.EnsureSuccessStatusCode();
36+
}
37+
38+
[Fact]
39+
public async Task GivenValidAddUserQuery_ReturnSuccessObject()
40+
{
41+
var res = await AddNewUser();
42+
res.ShouldBeGreaterThan(0);
43+
}
44+
45+
[Fact]
46+
public async Task GivenValidUpdateUserQuery_ReturnSuccessObject()
47+
{
48+
var res = await AddNewUser();
49+
var client = this.factory.GetAnonymousClient();
50+
var command = new UpdateUserCommand
51+
{
52+
UserID = res,
53+
City = "SpringField",
54+
Country = "USA",
55+
State = "VA",
56+
Zip = "66006",
57+
PhoneNumber = "888-88-8888",
58+
};
59+
60+
var content = IntegrationTestHelper.GetRequestContent(command);
61+
var response = await client.PutAsync($"api/User/Put", content);
62+
response.EnsureSuccessStatusCode();
63+
64+
}
65+
66+
[Fact]
67+
public async Task GivenValidDeleteUserQuery_ReturnSuccessObject()
68+
{
69+
var res = await AddNewUser();
70+
var client = this.factory.GetAnonymousClient();
71+
var response = await client.DeleteAsync($"api/User/Delete?userID={res}");
72+
response.EnsureSuccessStatusCode();
73+
}
74+
75+
[Fact]
76+
public async Task GivenValidGetUserQuery_SendNullUserID_ReturnErrorCode()
77+
{
78+
var client = this.factory.GetAnonymousClient();
79+
var response = await client.GetAsync($"api/User/Get");
80+
var result = response.Content.ReadAsStringAsync().Result;
81+
result.ShouldContain("User ID is required");
82+
response.StatusCode.ShouldBe(HttpStatusCode.BadRequest);
83+
}
84+
85+
private async Task<int> AddNewUser()
86+
{
87+
var client = this.factory.GetAnonymousClient();
88+
var command = new AddUserCommand
89+
{
90+
FirstName = "Test",
91+
LastName = "Doe",
92+
City = "Falls Chruch",
93+
Country = "USA",
94+
State = "VA",
95+
Zip = "22044",
96+
DOB = new DateTime(1980, 01, 01),
97+
EmailAddress = "[email protected]",
98+
Gender = "M",
99+
PhoneNumber = "444-443-4444",
100+
};
101+
102+
var content = IntegrationTestHelper.GetRequestContent(command);
103+
var response = await client.PostAsync($"api/User/Post", content);
104+
return Convert.ToInt32(response.Content.ReadAsStringAsync().Result);
105+
}
106+
}
107+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.8" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
12+
<PackageReference Include="MSTest.TestAdapter" Version="2.1.1" />
13+
<PackageReference Include="MSTest.TestFramework" Version="2.1.1" />
14+
<PackageReference Include="coverlet.collector" Version="1.3.0" />
15+
<PackageReference Include="Shouldly" Version="3.0.2" />
16+
<PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
17+
<PackageReference Include="xunit" Version="2.4.1" />
18+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
19+
<PrivateAssets>all</PrivateAssets>
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
</PackageReference>
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<ProjectReference Include="..\UserManagement.API\UserManagement.API.csproj" />
26+
</ItemGroup>
27+
28+
</Project>

UserManagement.Persistence.IntegrationTests/User/UserRepositoryTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public async Task TestGetUserByID_GivenCorrectParam_ReturnUserList()
3636
{
3737
var userId = await AddNewUser();
3838
var res = await userRepository.GetUser(userId);
39-
res.ShouldBeOfType<Domain.Entities.User>();
39+
res.ShouldBeOfType<UserManagement.Domain.Entities.User>();
4040
}
4141

4242

@@ -52,8 +52,8 @@ public async Task TestUpdateUser_GivenCorrectParam_ReturnTrue()
5252
Country = "USA",
5353
State = "VA",
5454
Zip = "22044",
55-
DateAdded = new DateTime(2019, 01, 01),
56-
DOB = new DateTime(1980, 01, 01),
55+
DateAdded = new System.DateTime(2019, 01, 01),
56+
DOB = new System.DateTime(1980, 01, 01),
5757
EmailAddress = "[email protected]",
5858
Gender = "F",
5959
PhoneNumber = "000-000-000",
@@ -82,8 +82,8 @@ private async Task<int> AddNewUser()
8282
Country = "USA",
8383
State = "VA",
8484
Zip = "22044",
85-
DateAdded = new DateTime(2019, 01, 01),
86-
DOB = new DateTime(1980, 01, 01),
85+
DateAdded = new System.DateTime(2019, 01, 01),
86+
DOB = new System.DateTime(1980, 01, 01),
8787
EmailAddress = "[email protected]",
8888
Gender = "M",
8989
PhoneNumber = "444-443-4444"

UserManagement.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UserManagement.Persistence"
1717
EndProject
1818
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UserManagement.Application.UnitTests", "UserManagement.Application.UnitTests\UserManagement.Application.UnitTests.csproj", "{4D0889FC-829F-4A29-8DE5-4051DDF0ED8B}"
1919
EndProject
20-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UserManagement.Persistence.IntegrationTests", "UserManagement.Persistence.IntegrationTests\UserManagement.Persistence.IntegrationTests.csproj", "{6FDA919F-2367-47DF-A308-29115F84F9FF}"
20+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UserManagement.Persistence.IntegrationTests", "UserManagement.Persistence.IntegrationTests\UserManagement.Persistence.IntegrationTests.csproj", "{6FDA919F-2367-47DF-A308-29115F84F9FF}"
21+
EndProject
22+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UserManagement.API.IntegrationTests", "UserManagement.API.IntegrationTests\UserManagement.API.IntegrationTests.csproj", "{A9CF9035-8F4B-4516-A17C-02450E2FBD98}"
2123
EndProject
2224
Global
2325
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -49,6 +51,10 @@ Global
4951
{6FDA919F-2367-47DF-A308-29115F84F9FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
5052
{6FDA919F-2367-47DF-A308-29115F84F9FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
5153
{6FDA919F-2367-47DF-A308-29115F84F9FF}.Release|Any CPU.Build.0 = Release|Any CPU
54+
{A9CF9035-8F4B-4516-A17C-02450E2FBD98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
55+
{A9CF9035-8F4B-4516-A17C-02450E2FBD98}.Debug|Any CPU.Build.0 = Debug|Any CPU
56+
{A9CF9035-8F4B-4516-A17C-02450E2FBD98}.Release|Any CPU.ActiveCfg = Release|Any CPU
57+
{A9CF9035-8F4B-4516-A17C-02450E2FBD98}.Release|Any CPU.Build.0 = Release|Any CPU
5258
EndGlobalSection
5359
GlobalSection(SolutionProperties) = preSolution
5460
HideSolutionNode = FALSE
@@ -60,6 +66,7 @@ Global
6066
{45C639CE-ED7B-4050-A71E-BB24DBDFE3AB} = {B9047929-76B2-4949-97FA-ABBA6A0F08FF}
6167
{4D0889FC-829F-4A29-8DE5-4051DDF0ED8B} = {3CCCA724-B32F-4922-AAFF-933ABA3FBAE3}
6268
{6FDA919F-2367-47DF-A308-29115F84F9FF} = {3CCCA724-B32F-4922-AAFF-933ABA3FBAE3}
69+
{A9CF9035-8F4B-4516-A17C-02450E2FBD98} = {3CCCA724-B32F-4922-AAFF-933ABA3FBAE3}
6370
EndGlobalSection
6471
GlobalSection(ExtensibilityGlobals) = postSolution
6572
SolutionGuid = {EEF049B4-7F13-4856-8723-E74D7812EB83}

0 commit comments

Comments
 (0)