Skip to content

Commit 9f9819a

Browse files
Merge pull request #1 from fullstackhub-io/Part11
Part11
2 parents c601062 + dec441b commit 9f9819a

File tree

81 files changed

+4782
-1067
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+4782
-1067
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.API/ClientApp/angular.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"src/assets"
2525
],
2626
"styles": [
27+
"./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
2728
"src/styles.css"
2829
],
2930
"scripts": []
@@ -88,6 +89,7 @@
8889
"src/assets"
8990
],
9091
"styles": [
92+
"./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
9193
"src/styles.css"
9294
],
9395
"scripts": []
@@ -119,6 +121,10 @@
119121
}
120122
}
121123
}
122-
}},
123-
"defaultProject": "ClientApp"
124-
}
124+
}
125+
},
126+
"defaultProject": "ClientApp",
127+
"cli": {
128+
"analytics": false
129+
}
130+
}

0 commit comments

Comments
 (0)