Skip to content

Commit c0a83f7

Browse files
authored
Merge pull request #314 from SlimAhmad/users/slimahmad/controller/contributor-Post
MEDIUM CONTROLLERS: Contributor Post Tests
2 parents 8834694 + 93a9401 commit c0a83f7

6 files changed

Lines changed: 395 additions & 3 deletions

File tree

GitFyle.Core.Api.Tests.Unit/Controllers/ContributionTypes/ContributionTypesControllerTests.Exceptions.Get.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public partial class ContributionTypesControllerTests
1818
[Theory]
1919
[MemberData(nameof(ServerExceptions))]
2020
public async Task ShouldReturnInternalServerErrorOnGetIfServerErrorOccurredAsync(
21-
Xeption serverException)
21+
Xeption serverException)
2222
{
2323
// given
2424
InternalServerErrorObjectResult expectedInternalServerErrorObjectResult =

GitFyle.Core.Api.Tests.Unit/Controllers/ContributionTypes/ContributionTypesControllerTests.Exceptions.GetById.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public partial class ContributionTypesControllerTests
1919
[Theory]
2020
[MemberData(nameof(ValidationExceptions))]
2121
public async Task ShouldReturnBadRequestOnGetByIdIfValidationErrorOccursAsync(
22-
Xeption validationException)
22+
Xeption validationException)
2323
{
2424
// given
2525
Guid someId = Guid.NewGuid();
@@ -51,7 +51,7 @@ public async Task ShouldReturnBadRequestOnGetByIdIfValidationErrorOccursAsync(
5151
[Theory]
5252
[MemberData(nameof(ServerExceptions))]
5353
public async Task ShouldReturnInternalServerErrorOnGetByIdIfServerErrorOccursAsync(
54-
Xeption validationException)
54+
Xeption validationException)
5555
{
5656
// given
5757
Guid someId = Guid.NewGuid();
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
// ----------------------------------------------------------------------------------
2+
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
3+
// ----------------------------------------------------------------------------------
4+
5+
using System;
6+
using System.Threading.Tasks;
7+
using GitFyle.Core.Api.Models.Foundations.Contributors;
8+
using GitFyle.Core.Api.Models.Foundations.Contributors.Exceptions;
9+
using Microsoft.AspNetCore.Mvc;
10+
using Moq;
11+
using RESTFulSense.Clients.Extensions;
12+
using RESTFulSense.Models;
13+
using Xeptions;
14+
15+
namespace GitFyle.Core.Api.Tests.Unit.Controllers.Contributors
16+
{
17+
public partial class ContributorsControllerTests
18+
{
19+
[Theory]
20+
[MemberData(nameof(ValidationExceptions))]
21+
public async Task ShouldReturnBadRequestOnPostIfValidationErrorOccursAsync(
22+
Xeption validationException)
23+
{
24+
// given
25+
Contributor someContributor = CreateRandomContributor();
26+
27+
BadRequestObjectResult expectedBadRequestObjectResult =
28+
BadRequest(validationException.InnerException);
29+
30+
var expectedActionResult =
31+
new ActionResult<Contributor>(expectedBadRequestObjectResult);
32+
33+
this.contributorServiceMock.Setup(service =>
34+
service.AddContributorAsync(It.IsAny<Contributor>()))
35+
.ThrowsAsync(validationException);
36+
37+
// when
38+
ActionResult<Contributor> actualActionResult =
39+
await this.contributorsController.PostContributorAsync(someContributor);
40+
41+
// then
42+
actualActionResult.ShouldBeEquivalentTo(expectedActionResult);
43+
44+
this.contributorServiceMock.Verify(service =>
45+
service.AddContributorAsync(It.IsAny<Contributor>()),
46+
Times.Once);
47+
48+
this.contributorServiceMock.VerifyNoOtherCalls();
49+
}
50+
51+
[Theory]
52+
[MemberData(nameof(ServerExceptions))]
53+
public async Task ShouldReturnInternalServerErrorOnPostIfServerErrorOccurredAsync(
54+
Xeption serverException)
55+
{
56+
// given
57+
Contributor someContributor = CreateRandomContributor();
58+
59+
InternalServerErrorObjectResult expectedInternalServerErrorObjectResult =
60+
InternalServerError(serverException);
61+
62+
var expectedActionResult =
63+
new ActionResult<Contributor>(expectedInternalServerErrorObjectResult);
64+
65+
this.contributorServiceMock.Setup(service =>
66+
service.AddContributorAsync(It.IsAny<Contributor>()))
67+
.ThrowsAsync(serverException);
68+
69+
// when
70+
ActionResult<Contributor> actualActionResult =
71+
await this.contributorsController.PostContributorAsync(someContributor);
72+
73+
// then
74+
actualActionResult.ShouldBeEquivalentTo(expectedActionResult);
75+
76+
this.contributorServiceMock.Verify(service =>
77+
service.AddContributorAsync(It.IsAny<Contributor>()),
78+
Times.Once);
79+
80+
this.contributorServiceMock.VerifyNoOtherCalls();
81+
}
82+
83+
[Fact]
84+
public async Task ShouldReturnConflictOnPostIfAlreadyExistsContributorErrorOccurredAsync()
85+
{
86+
// given
87+
Contributor someContributor = CreateRandomContributor();
88+
var someInnerException = new Exception();
89+
string someMessage = GetRandomString();
90+
var someDictionaryData = GetRandomDictionaryData();
91+
92+
var alreadyExistsContributorException =
93+
new AlreadyExistsContributorException(
94+
message: someMessage,
95+
innerException: someInnerException,
96+
data: someInnerException.Data);
97+
98+
var contributorDependencyValidationException =
99+
new ContributorDependencyValidationException(
100+
message: someMessage,
101+
innerException: alreadyExistsContributorException,
102+
data: someDictionaryData);
103+
104+
ConflictObjectResult expectedConflictObjectResult =
105+
Conflict(alreadyExistsContributorException);
106+
107+
var expectedActionResult =
108+
new ActionResult<Contributor>(expectedConflictObjectResult);
109+
110+
this.contributorServiceMock.Setup(service =>
111+
service.AddContributorAsync(It.IsAny<Contributor>()))
112+
.ThrowsAsync(contributorDependencyValidationException);
113+
114+
// when
115+
ActionResult<Contributor> actualActionResult =
116+
await this.contributorsController.PostContributorAsync(someContributor);
117+
118+
// then
119+
actualActionResult.ShouldBeEquivalentTo(expectedActionResult);
120+
121+
this.contributorServiceMock.Verify(service =>
122+
service.AddContributorAsync(It.IsAny<Contributor>()),
123+
Times.Once);
124+
125+
this.contributorServiceMock.VerifyNoOtherCalls();
126+
}
127+
128+
[Fact]
129+
public async Task ShouldReturnFailedDependencyOnPostIfReferenceExceptionOccursAsync()
130+
{
131+
// given
132+
Contributor someContributor = CreateRandomContributor();
133+
var someInnerException = new Exception();
134+
string someMessage = GetRandomString();
135+
136+
var invalidReferenceContributorException =
137+
new InvalidReferenceContributorException(
138+
message: someMessage,
139+
innerException: someInnerException,
140+
data: someInnerException.Data);
141+
142+
var contributorDependencyValidationException =
143+
new ContributorDependencyValidationException(
144+
message: someMessage,
145+
innerException: invalidReferenceContributorException,
146+
data: invalidReferenceContributorException.Data);
147+
148+
FailedDependencyObjectResult expectedFailedDependencyObjectResult =
149+
FailedDependency(invalidReferenceContributorException);
150+
151+
var expectedActionResult =
152+
new ActionResult<Contributor>(expectedFailedDependencyObjectResult);
153+
154+
this.contributorServiceMock.Setup(service =>
155+
service.AddContributorAsync(It.IsAny<Contributor>()))
156+
.ThrowsAsync(contributorDependencyValidationException);
157+
158+
// when
159+
ActionResult<Contributor> actualActionResult =
160+
await this.contributorsController.PostContributorAsync(someContributor);
161+
162+
// then
163+
actualActionResult.ShouldBeEquivalentTo(expectedActionResult);
164+
165+
this.contributorServiceMock.Verify(service =>
166+
service.AddContributorAsync(It.IsAny<Contributor>()),
167+
Times.Once);
168+
169+
this.contributorServiceMock.VerifyNoOtherCalls();
170+
}
171+
}
172+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// ----------------------------------------------------------------------------------
2+
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
3+
// ----------------------------------------------------------------------------------
4+
5+
using System.Threading.Tasks;
6+
using Force.DeepCloner;
7+
using GitFyle.Core.Api.Models.Foundations.Contributors;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Moq;
10+
using RESTFulSense.Clients.Extensions;
11+
using RESTFulSense.Models;
12+
13+
namespace GitFyle.Core.Api.Tests.Unit.Controllers.Contributors
14+
{
15+
public partial class ContributorsControllerTests
16+
{
17+
[Fact]
18+
public async Task ShouldReturnCreatedOnPostAsync()
19+
{
20+
// given
21+
Contributor randomContributor = CreateRandomContributor();
22+
Contributor inputContributor = randomContributor;
23+
Contributor addedContributor = inputContributor;
24+
Contributor expectedContributor = addedContributor.DeepClone();
25+
26+
var expectedObjectResult =
27+
new CreatedObjectResult(expectedContributor);
28+
29+
var expectedActionResult =
30+
new ActionResult<Contributor>(expectedObjectResult);
31+
32+
this.contributorServiceMock.Setup(service =>
33+
service.AddContributorAsync(inputContributor))
34+
.ReturnsAsync(addedContributor);
35+
36+
// when
37+
ActionResult<Contributor> actualActionResult =
38+
await this.contributorsController.PostContributorAsync(
39+
inputContributor);
40+
41+
// then
42+
actualActionResult.ShouldBeEquivalentTo(
43+
expectedActionResult);
44+
45+
this.contributorServiceMock.Verify(service =>
46+
service.AddContributorAsync(inputContributor),
47+
Times.Once);
48+
49+
this.contributorServiceMock.VerifyNoOtherCalls();
50+
}
51+
}
52+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// ----------------------------------------------------------------------------------
2+
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
3+
// ----------------------------------------------------------------------------------
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using GitFyle.Core.Api.Controllers;
9+
using GitFyle.Core.Api.Models.Foundations.Contributors;
10+
using GitFyle.Core.Api.Models.Foundations.Contributors.Exceptions;
11+
using GitFyle.Core.Api.Services.Foundations.Contributors;
12+
using Moq;
13+
using RESTFulSense.Controllers;
14+
using Tynamix.ObjectFiller;
15+
using Xeptions;
16+
17+
namespace GitFyle.Core.Api.Tests.Unit.Controllers.Contributors
18+
{
19+
public partial class ContributorsControllerTests : RESTFulController
20+
{
21+
private readonly Mock<IContributorService> contributorServiceMock;
22+
private readonly ContributorsController contributorsController;
23+
24+
public ContributorsControllerTests()
25+
{
26+
this.contributorServiceMock = new Mock<IContributorService>();
27+
28+
this.contributorsController = new ContributorsController(
29+
contributorService: this.contributorServiceMock.Object);
30+
}
31+
32+
public static TheoryData<Xeption> ValidationExceptions()
33+
{
34+
var someInnerException = new Xeption();
35+
string someMessage = GetRandomString();
36+
var someDictionaryData = GetRandomDictionaryData();
37+
38+
return new TheoryData<Xeption>
39+
{
40+
new ContributorValidationException(
41+
message: someMessage,
42+
innerException: someInnerException),
43+
44+
new ContributorDependencyValidationException(
45+
message: someMessage,
46+
innerException: someInnerException,
47+
data: someDictionaryData)
48+
};
49+
}
50+
51+
public static TheoryData<Xeption> ServerExceptions()
52+
{
53+
var someInnerException = new Xeption();
54+
string someMessage = GetRandomString();
55+
56+
return new TheoryData<Xeption>
57+
{
58+
new ContributorDependencyException(
59+
message: someMessage,
60+
innerException: someInnerException),
61+
62+
new ContributorServiceException(
63+
message: someMessage,
64+
innerException: someInnerException)
65+
};
66+
}
67+
68+
private static string GetRandomString() =>
69+
new MnemonicString().GetValue();
70+
71+
private static int GetRandomNumber() =>
72+
new IntRange(min: 2, max: 9).GetValue();
73+
74+
private static Dictionary<string, string[]> GetRandomDictionaryData()
75+
{
76+
var filler = new Filler<Dictionary<string, string[]>>();
77+
78+
filler.Setup()
79+
.DictionaryItemCount(maxCount: 10);
80+
81+
return filler.Create();
82+
}
83+
84+
private static DateTimeOffset GetRandomDateTimeOffset() =>
85+
new DateTimeRange(earliestDate: DateTime.UnixEpoch).GetValue();
86+
87+
private static IQueryable<Contributor> CreateRandomnContributors() =>
88+
CreateContributorFiller().Create(count: GetRandomNumber()).AsQueryable();
89+
90+
private static Contributor CreateRandomContributor() =>
91+
CreateContributorFiller().Create();
92+
93+
private static Filler<Contributor> CreateContributorFiller()
94+
{
95+
var filler = new Filler<Contributor>();
96+
97+
filler.Setup()
98+
.OnProperty(contributor =>
99+
contributor.Contributions).IgnoreIt()
100+
101+
.OnType<DateTimeOffset>().Use(
102+
GetRandomDateTimeOffset);
103+
104+
return filler;
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)