|
| 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 | +} |
0 commit comments