Skip to content

Commit 78d79d1

Browse files
Part 9
Part 9
1 parent 1f13cfe commit 78d79d1

13 files changed

+777
-3
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace UserManagement.ApplicationTests
2+
{
3+
using AutoMapper;
4+
using System.Data;
5+
using UserManagement.Application.Common.Mappings;
6+
public class BaseFixture
7+
{
8+
public IMapper Mapper { get; }
9+
10+
11+
public IDbConnection DBConnection { get; }
12+
13+
public BaseFixture()
14+
{
15+
var configurationProvider = new MapperConfiguration(cfg =>
16+
{
17+
cfg.AddProfile<MappingProfile>();
18+
});
19+
20+
this.Mapper = configurationProvider.CreateMapper();
21+
}
22+
}
23+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
namespace UserManagement.ApplicationTests.User.Commands
2+
{
3+
using AutoMapper;
4+
using Shouldly;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using UserManagement.Application.Common.Interfaces;
8+
using UserManagement.Application.User.Commands;
9+
using UserManagement.Domain.UnitOfWork;
10+
using Xunit;
11+
12+
[Collection("UserCollection")]
13+
public class AddUserCommandTest
14+
{
15+
private readonly IConfigConstants constant;
16+
private readonly IMapper mapper;
17+
private readonly IUnitOfWork unitOfWork;
18+
19+
public AddUserCommandTest(UserFixture userFixture)
20+
{
21+
constant = userFixture.Constant;
22+
mapper = userFixture.Mapper;
23+
unitOfWork = userFixture.UnitOfWork;
24+
}
25+
26+
[Fact]
27+
public async Task Handle_ReturnsCorrectVM()
28+
{
29+
var command = new AddUserCommand
30+
{
31+
FirstName = "John",
32+
LastName = "Doe",
33+
City = "Falls Chruch",
34+
Country = "USA",
35+
State = "VA",
36+
Zip = "22044",
37+
DOB = new System.DateTime(1980, 01, 01),
38+
EmailAddress = "[email protected]",
39+
Gender = "M",
40+
PhoneNumber = "444-443-4444",
41+
};
42+
43+
var handler = new AddUserCommand.AddNewUserHandler(constant, mapper, unitOfWork);
44+
var result = await handler.Handle(command, CancellationToken.None);
45+
result.ShouldBeOfType<int>();
46+
}
47+
48+
[Fact]
49+
public async Task Handle_ReturnCorrectUserID_WhenSendCorrectPayload()
50+
{
51+
var command = new AddUserCommand
52+
{
53+
FirstName = "John",
54+
LastName = "Doe",
55+
City = "Falls Chruch",
56+
Country = "USA",
57+
State = "VA",
58+
Zip = "22044",
59+
DOB = new System.DateTime(1980, 01, 01),
60+
EmailAddress = "[email protected]",
61+
Gender = "M",
62+
PhoneNumber = "444-443-4444",
63+
};
64+
65+
var handler = new AddUserCommand.AddNewUserHandler(constant, mapper, unitOfWork);
66+
var result = await handler.Handle(command, CancellationToken.None);
67+
result.ShouldBe(100);
68+
}
69+
}
70+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using Shouldly;
2+
using System.Linq;
3+
using UserManagement.Application.Common.Interfaces;
4+
using UserManagement.Application.User.Commands;
5+
using Xunit;
6+
7+
namespace UserManagement.ApplicationTests.User.Commands
8+
{
9+
[Collection("UserCollection")]
10+
public class AddUserCommandValidatorTest
11+
{
12+
private readonly IConfigConstants constant;
13+
14+
public AddUserCommandValidatorTest(UserFixture userFixture)
15+
{
16+
constant = userFixture.Constant;
17+
}
18+
19+
[Fact]
20+
public void Validate_ReturnTrue_WhenAllDataIsValid()
21+
{
22+
var command = new AddUserCommand
23+
{
24+
FirstName = "John",
25+
LastName = "Doe",
26+
City = "Falls Chruch",
27+
Country = "USA",
28+
State = "VA",
29+
Zip = "22044",
30+
DOB = new System.DateTime(1980, 01, 01),
31+
EmailAddress = "[email protected]",
32+
Gender = "M",
33+
PhoneNumber = "444-443-4444",
34+
};
35+
36+
var validator = new AddUserCommandValidator(constant);
37+
var result = validator.Validate(command);
38+
result.IsValid.ShouldBeTrue();
39+
}
40+
41+
[Fact]
42+
public void Validate_ReturnFalse_WhenAllDataIsInValid()
43+
{
44+
var command = new AddUserCommand
45+
{
46+
FirstName = string.Empty,
47+
LastName = string.Empty,
48+
City = string.Empty,
49+
Country = string.Empty,
50+
State = string.Empty,
51+
Zip = null,
52+
EmailAddress = string.Empty,
53+
Gender = string.Empty,
54+
PhoneNumber = string.Empty,
55+
};
56+
57+
var validator = new AddUserCommandValidator(constant);
58+
var result = validator.Validate(command);
59+
result.IsValid.ShouldBeFalse();
60+
}
61+
62+
[Fact]
63+
public void Validate_ReturnFalse_WhenFirstNameIsEmpty()
64+
{
65+
var command = new AddUserCommand
66+
{
67+
FirstName = string.Empty,
68+
LastName = "Doe",
69+
City = "Falls Chruch",
70+
Country = "USA",
71+
State = "VA",
72+
Zip = "22044",
73+
DOB = new System.DateTime(1980, 01, 01),
74+
EmailAddress = "[email protected]",
75+
Gender = "M",
76+
PhoneNumber = "444-443-4444",
77+
};
78+
79+
var validator = new AddUserCommandValidator(constant);
80+
var result = validator.Validate(command);
81+
result.Errors.FirstOrDefault(x => x.ErrorMessage == constant.MSG_USER_NULLFIRSTNAME).ErrorMessage.ShouldBe(constant.MSG_USER_NULLFIRSTNAME);
82+
result.IsValid.ShouldBeFalse();
83+
}
84+
85+
[Fact]
86+
public void Validate_ReturnFalse_WhenLastNameIsEmpty()
87+
{
88+
var command = new AddUserCommand
89+
{
90+
FirstName = "John",
91+
LastName = string.Empty,
92+
City = "Falls Chruch",
93+
Country = "USA",
94+
State = "VA",
95+
Zip = "22044",
96+
DOB = new System.DateTime(1980, 01, 01),
97+
EmailAddress = "[email protected]",
98+
Gender = "M",
99+
PhoneNumber = "444-443-4444",
100+
};
101+
102+
var validator = new AddUserCommandValidator(constant);
103+
var result = validator.Validate(command);
104+
result.Errors.FirstOrDefault(x => x.ErrorMessage == constant.MSG_USER_NULLLASTNAME).ErrorMessage.ShouldBe(constant.MSG_USER_NULLLASTNAME);
105+
result.IsValid.ShouldBeFalse();
106+
}
107+
108+
}
109+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using AutoMapper;
2+
using Shouldly;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using UserManagement.Application.Common.Interfaces;
6+
using UserManagement.Application.User.Commands;
7+
using UserManagement.Domain.UnitOfWork;
8+
using Xunit;
9+
10+
namespace UserManagement.ApplicationTests.User.Commands
11+
{
12+
[Collection("UserCollection")]
13+
public class DeleteUserCommandTest
14+
{
15+
private readonly IConfigConstants constant;
16+
private readonly IMapper mapper;
17+
private readonly IUnitOfWork unitOfWork;
18+
19+
public DeleteUserCommandTest(UserFixture userFixture)
20+
{
21+
constant = userFixture.Constant;
22+
mapper = userFixture.Mapper;
23+
unitOfWork = userFixture.UnitOfWork;
24+
}
25+
26+
[Fact]
27+
public async Task Handle_ReturnsCorrectVM()
28+
{
29+
var command = new DeleteUserCommand
30+
{
31+
UserID = 100,
32+
};
33+
34+
var handler = new DeleteUserCommand.DeleteUserHandler(constant, mapper, unitOfWork);
35+
var result = await handler.Handle(command, CancellationToken.None);
36+
result.ShouldBeOfType<bool>();
37+
}
38+
39+
[Fact]
40+
public async Task Handle_ReturnTrue_WhenSendCorrectUserIDIsSent()
41+
{
42+
var command = new DeleteUserCommand
43+
{
44+
UserID = 100,
45+
};
46+
47+
var handler = new DeleteUserCommand.DeleteUserHandler(constant, mapper, unitOfWork);
48+
var result = await handler.Handle(command, CancellationToken.None);
49+
result.ShouldBe(true);
50+
}
51+
}
52+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Shouldly;
2+
using System.Linq;
3+
using UserManagement.Application.Common.Interfaces;
4+
using UserManagement.Application.User.Commands;
5+
using Xunit;
6+
7+
namespace UserManagement.ApplicationTests.User.Commands
8+
{
9+
[Collection("UserCollection")]
10+
public class DeleteUserCommandValidatorTest
11+
{
12+
private readonly IConfigConstants constant;
13+
14+
public DeleteUserCommandValidatorTest(UserFixture userFixture)
15+
{
16+
constant = userFixture.Constant;
17+
}
18+
19+
[Fact]
20+
public void Validate_ReturnTrue_WhenAllDataIsValid()
21+
{
22+
var command = new DeleteUserCommand
23+
{
24+
UserID = 100,
25+
};
26+
27+
var validator = new DeleteUserCommandValidator(constant);
28+
var result = validator.Validate(command);
29+
result.IsValid.ShouldBeTrue();
30+
}
31+
32+
[Fact]
33+
public void Validate_ReturnFalse_WhenAllDataIsInValid()
34+
{
35+
var command = new DeleteUserCommand
36+
{
37+
UserID = 0,
38+
};
39+
40+
var validator = new DeleteUserCommandValidator(constant);
41+
var result = validator.Validate(command);
42+
result.IsValid.ShouldBeFalse();
43+
}
44+
45+
}
46+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using AutoMapper;
2+
using Shouldly;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using UserManagement.Application.Common.Interfaces;
6+
using UserManagement.Application.User.Commands;
7+
using UserManagement.Domain.UnitOfWork;
8+
using Xunit;
9+
10+
11+
namespace UserManagement.ApplicationTests.User.Commands
12+
{
13+
[Collection("UserCollection")]
14+
public class UpdateUserCommandTest
15+
{
16+
private readonly IConfigConstants constant;
17+
private readonly IMapper mapper;
18+
private readonly IUnitOfWork unitOfWork;
19+
20+
public UpdateUserCommandTest(UserFixture userFixture)
21+
{
22+
constant = userFixture.Constant;
23+
mapper = userFixture.Mapper;
24+
unitOfWork = userFixture.UnitOfWork;
25+
}
26+
27+
[Fact]
28+
public async Task Handle_ReturnsCorrectVM()
29+
{
30+
var command = new UpdateUserCommand
31+
{
32+
UserID = 100,
33+
City = "SpringField",
34+
Country = "USA",
35+
State = "VA",
36+
Zip = "66006",
37+
PhoneNumber = "888-88-8888",
38+
};
39+
40+
var handler = new UpdateUserCommand.UpdateUserHandler(constant, mapper, unitOfWork);
41+
var result = await handler.Handle(command, CancellationToken.None);
42+
result.ShouldBeOfType<bool>();
43+
}
44+
45+
[Fact]
46+
public async Task Handle_ReturnTrue_WhenSendCorrectPayloadIsSent()
47+
{
48+
var command = new UpdateUserCommand
49+
{
50+
UserID = 100,
51+
City = "SpringField",
52+
Country = "USA",
53+
State = "VA",
54+
Zip = "66006",
55+
PhoneNumber = "888-88-8888",
56+
};
57+
58+
var handler = new UpdateUserCommand.UpdateUserHandler(constant, mapper, unitOfWork);
59+
var result = await handler.Handle(command, CancellationToken.None);
60+
result.ShouldBe(true);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)