|
4 | 4 |
|
5 | 5 | using System; |
6 | 6 | using System.Threading.Tasks; |
| 7 | +using EFxceptions.Models.Exceptions; |
7 | 8 | using FluentAssertions; |
8 | 9 | using GitFyle.Core.Api.Models.Foundations.Contributors; |
9 | 10 | using GitFyle.Core.Api.Models.Foundations.Contributors.Exceptions; |
@@ -70,6 +71,67 @@ await Assert.ThrowsAsync<ContributorDependencyException>( |
70 | 71 | this.loggingBrokerMock.VerifyNoOtherCalls(); |
71 | 72 | } |
72 | 73 |
|
| 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 | + |
73 | 135 | [Fact] |
74 | 136 | public async Task ShouldThrowDependencyExceptionOnModifyIfDatabaseUpdateExceptionOccursAndLogItAsync() |
75 | 137 | { |
|
0 commit comments