Skip to content

Commit 93a9401

Browse files
authored
Merge branch 'main' into users/slimahmad/controller/contributor-Post
2 parents d11a342 + 8834694 commit 93a9401

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

GitFyle.Core.Api.Tests.Unit/Services/Foundations/Contributors/ContributorServiceTests.Exceptions.Modify.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Threading.Tasks;
7+
using EFxceptions.Models.Exceptions;
78
using FluentAssertions;
89
using GitFyle.Core.Api.Models.Foundations.Contributors;
910
using GitFyle.Core.Api.Models.Foundations.Contributors.Exceptions;
@@ -70,6 +71,67 @@ await Assert.ThrowsAsync<ContributorDependencyException>(
7071
this.loggingBrokerMock.VerifyNoOtherCalls();
7172
}
7273

74+
[Fact]
75+
public async Task ShouldThrowValidationExceptionOnModifyIfReferenceErrorOccursAndLogItAsync()
76+
{
77+
// given
78+
Contributor foreignKeyConflictedContributor = CreateRandomContributor();
79+
string randomMessage = GetRandomString();
80+
string exceptionMessage = randomMessage;
81+
82+
var foreignKeyConstraintConflictException =
83+
new ForeignKeyConstraintConflictException(message: exceptionMessage);
84+
85+
var invalidContributorReferenceException =
86+
new InvalidReferenceContributorException(
87+
message: "Invalid contributor reference error occurred.",
88+
innerException: foreignKeyConstraintConflictException,
89+
data: foreignKeyConstraintConflictException.Data);
90+
91+
var expectedContributorDependencyValidationException =
92+
new ContributorDependencyValidationException(
93+
message: "Contributor dependency validation error occurred, fix errors and try again.",
94+
innerException: invalidContributorReferenceException,
95+
data: invalidContributorReferenceException.Data);
96+
97+
this.dateTimeBrokerMock.Setup(broker =>
98+
broker.GetCurrentDateTimeOffsetAsync())
99+
.Throws(foreignKeyConstraintConflictException);
100+
101+
// when
102+
ValueTask<Contributor> modifyContributorTask =
103+
this.contributorService.ModifyContributorAsync(foreignKeyConflictedContributor);
104+
105+
ContributorDependencyValidationException actualContributorDependencyValidationException =
106+
await Assert.ThrowsAsync<ContributorDependencyValidationException>(
107+
modifyContributorTask.AsTask);
108+
109+
// then
110+
actualContributorDependencyValidationException.Should().BeEquivalentTo(
111+
expectedContributorDependencyValidationException);
112+
113+
this.dateTimeBrokerMock.Verify(broker =>
114+
broker.GetCurrentDateTimeOffsetAsync(),
115+
Times.Once);
116+
117+
this.storageBrokerMock.Verify(broker =>
118+
broker.SelectContributorByIdAsync(foreignKeyConflictedContributor.Id),
119+
Times.Never);
120+
121+
this.loggingBrokerMock.Verify(broker =>
122+
broker.LogErrorAsync(It.Is(SameExceptionAs(
123+
expectedContributorDependencyValidationException))),
124+
Times.Once);
125+
126+
this.storageBrokerMock.Verify(broker =>
127+
broker.UpdateContributorAsync(foreignKeyConflictedContributor),
128+
Times.Never);
129+
130+
this.dateTimeBrokerMock.VerifyNoOtherCalls();
131+
this.storageBrokerMock.VerifyNoOtherCalls();
132+
this.loggingBrokerMock.VerifyNoOtherCalls();
133+
}
134+
73135
[Fact]
74136
public async Task ShouldThrowDependencyExceptionOnModifyIfDatabaseUpdateExceptionOccursAndLogItAsync()
75137
{

0 commit comments

Comments
 (0)