From daa60cbddb0a53f49b36ffeb1f9d974c709017ef Mon Sep 17 00:00:00 2001 From: slimahmad6 Date: Thu, 27 Mar 2025 15:40:17 +0100 Subject: [PATCH 01/12] ShouldReturnCreatedOnPostAsync -> PASS --- .../ContributorsControllerTests.Logic.Post.cs | 52 +++++++++ .../ContributorsControllerTests.cs | 107 ++++++++++++++++++ .../Controllers/ContributorsController.cs | 31 +++++ 3 files changed, 190 insertions(+) create mode 100644 GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Logic.Post.cs create mode 100644 GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.cs create mode 100644 GitFyle.Core.Api/Controllers/ContributorsController.cs diff --git a/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Logic.Post.cs b/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Logic.Post.cs new file mode 100644 index 00000000..c4ec574e --- /dev/null +++ b/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Logic.Post.cs @@ -0,0 +1,52 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using System.Threading.Tasks; +using Force.DeepCloner; +using GitFyle.Core.Api.Models.Foundations.Contributors; +using Microsoft.AspNetCore.Mvc; +using Moq; +using RESTFulSense.Clients.Extensions; +using RESTFulSense.Models; + +namespace GitFyle.Core.Api.Tests.Unit.Controllers.Contributors +{ + public partial class ContributorsControllerTests + { + [Fact] + public async Task ShouldReturnCreatedOnPostAsync() + { + // given + Contributor randomContributor = CreateRandomContributor(); + Contributor inputContributor = randomContributor; + Contributor addedContributor = inputContributor; + Contributor expectedContributor = addedContributor.DeepClone(); + + var expectedObjectResult = + new CreatedObjectResult(expectedContributor); + + var expectedActionResult = + new ActionResult(expectedObjectResult); + + this.contributorServiceMock.Setup(service => + service.AddContributorAsync(inputContributor)) + .ReturnsAsync(addedContributor); + + // when + ActionResult actualActionResult = + await this.contributorsController.PostContributorAsync( + inputContributor); + + // then + actualActionResult.ShouldBeEquivalentTo( + expectedActionResult); + + this.contributorServiceMock.Verify(service => + service.AddContributorAsync(inputContributor), + Times.Once); + + this.contributorServiceMock.VerifyNoOtherCalls(); + } + } +} diff --git a/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.cs b/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.cs new file mode 100644 index 00000000..e9fb4e5d --- /dev/null +++ b/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.cs @@ -0,0 +1,107 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using System; +using System.Collections.Generic; +using System.Linq; +using GitFyle.Core.Api.Controllers; +using GitFyle.Core.Api.Models.Foundations.Contributors; +using GitFyle.Core.Api.Models.Foundations.Contributors.Exceptions; +using GitFyle.Core.Api.Services.Foundations.Contributors; +using Moq; +using RESTFulSense.Controllers; +using Tynamix.ObjectFiller; +using Xeptions; + +namespace GitFyle.Core.Api.Tests.Unit.Controllers.Contributors +{ + public partial class ContributorsControllerTests : RESTFulController + { + private readonly Mock contributorServiceMock; + private readonly ContributorsController contributorsController; + + public ContributorsControllerTests() + { + this.contributorServiceMock = new Mock(); + + this.contributorsController = new ContributorsController( + contributorService: this.contributorServiceMock.Object); + } + + public static TheoryData ValidationExceptions() + { + var someInnerException = new Xeption(); + string someMessage = GetRandomString(); + var someDictionaryData = GetRandomDictionaryData(); + + return new TheoryData + { + new ContributorValidationException( + message: someMessage, + innerException: someInnerException), + + new ContributorDependencyValidationException( + message: someMessage, + innerException: someInnerException, + data: someDictionaryData) + }; + } + + public static TheoryData ServerExceptions() + { + var someInnerException = new Xeption(); + string someMessage = GetRandomString(); + + return new TheoryData + { + new ContributorDependencyException( + message: someMessage, + innerException: someInnerException), + + new ContributorServiceException( + message: someMessage, + innerException: someInnerException) + }; + } + + private static string GetRandomString() => + new MnemonicString().GetValue(); + + private static int GetRandomNumber() => + new IntRange(min: 2, max: 9).GetValue(); + + private static Dictionary GetRandomDictionaryData() + { + var filler = new Filler>(); + + filler.Setup() + .DictionaryItemCount(maxCount: 10); + + return filler.Create(); + } + + private static DateTimeOffset GetRandomDateTimeOffset() => + new DateTimeRange(earliestDate: DateTime.UnixEpoch).GetValue(); + + private static IQueryable CreateRandomnContributors() => + CreateContributorFiller().Create(count: GetRandomNumber()).AsQueryable(); + + private static Contributor CreateRandomContributor() => + CreateContributorFiller().Create(); + + private static Filler CreateContributorFiller() + { + var filler = new Filler(); + + filler.Setup() + .OnProperty(contributor => + contributor.Contributions).IgnoreIt() + + .OnType().Use( + GetRandomDateTimeOffset); + + return filler; + } + } +} diff --git a/GitFyle.Core.Api/Controllers/ContributorsController.cs b/GitFyle.Core.Api/Controllers/ContributorsController.cs new file mode 100644 index 00000000..2d071eb5 --- /dev/null +++ b/GitFyle.Core.Api/Controllers/ContributorsController.cs @@ -0,0 +1,31 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using System.Threading.Tasks; +using GitFyle.Core.Api.Models.Foundations.Contributors; +using GitFyle.Core.Api.Services.Foundations.Contributors; +using Microsoft.AspNetCore.Mvc; +using RESTFulSense.Controllers; + +namespace GitFyle.Core.Api.Controllers +{ + [ApiController] + [Route("api/contributors")] + public class ContributorsController : RESTFulController + { + private readonly IContributorService contributorService; + + public ContributorsController(IContributorService contributorService) => + this.contributorService = contributorService; + + [HttpPost] + public async ValueTask> PostContributorAsync(Contributor contributor) + { + Contributor addedContributor = + await this.contributorService.AddContributorAsync(contributor); + + return Created(addedContributor); + } + } +} From 70e0a2fdaf6c808bc9c303c73740b0e83608fe24 Mon Sep 17 00:00:00 2001 From: slimahmad6 Date: Thu, 27 Mar 2025 15:40:55 +0100 Subject: [PATCH 02/12] ShouldReturnBadRequestOnPostIfValidationErrorOccursAsync -> FAIL --- ...ributorsControllerTests.Exceptions.Post.cs | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs diff --git a/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs b/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs new file mode 100644 index 00000000..b701fcac --- /dev/null +++ b/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs @@ -0,0 +1,51 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using System; +using System.Threading.Tasks; +using GitFyle.Core.Api.Models.Foundations.Contributors; +using GitFyle.Core.Api.Models.Foundations.Contributors.Exceptions; +using Microsoft.AspNetCore.Mvc; +using Moq; +using RESTFulSense.Clients.Extensions; +using RESTFulSense.Models; +using Xeptions; + +namespace GitFyle.Core.Api.Tests.Unit.Controllers.Contributors +{ + public partial class ContributorsControllerTests + { + [Theory] + [MemberData(nameof(ValidationExceptions))] + public async Task ShouldReturnBadRequestOnPostIfValidationErrorOccursAsync(Xeption validationException) + { + // given + Contributor someContributor = CreateRandomContributor(); + + BadRequestObjectResult expectedBadRequestObjectResult = + BadRequest(validationException.InnerException); + + var expectedActionResult = + new ActionResult(expectedBadRequestObjectResult); + + this.contributorServiceMock.Setup(service => + service.AddContributorAsync(It.IsAny())) + .ThrowsAsync(validationException); + + // when + ActionResult actualActionResult = + await this.contributorsController.PostContributorAsync(someContributor); + + // then + actualActionResult.ShouldBeEquivalentTo(expectedActionResult); + + this.contributorServiceMock.Verify(service => + service.AddContributorAsync(It.IsAny()), + Times.Once); + + this.contributorServiceMock.VerifyNoOtherCalls(); + } + + } +} From d913b221e097f5590d8d8c7b49b81fb69c99c591 Mon Sep 17 00:00:00 2001 From: slimahmad6 Date: Thu, 27 Mar 2025 15:42:25 +0100 Subject: [PATCH 03/12] ShouldReturnBadRequestOnPostIfValidationErrorOccursAsync -> PASS --- .../Controllers/ContributorsController.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/GitFyle.Core.Api/Controllers/ContributorsController.cs b/GitFyle.Core.Api/Controllers/ContributorsController.cs index 2d071eb5..545c9a23 100644 --- a/GitFyle.Core.Api/Controllers/ContributorsController.cs +++ b/GitFyle.Core.Api/Controllers/ContributorsController.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using GitFyle.Core.Api.Models.Foundations.Contributors; +using GitFyle.Core.Api.Models.Foundations.Contributors.Exceptions; using GitFyle.Core.Api.Services.Foundations.Contributors; using Microsoft.AspNetCore.Mvc; using RESTFulSense.Controllers; @@ -22,10 +23,21 @@ public ContributorsController(IContributorService contributorService) => [HttpPost] public async ValueTask> PostContributorAsync(Contributor contributor) { + try + { Contributor addedContributor = await this.contributorService.AddContributorAsync(contributor); return Created(addedContributor); + } + catch (ContributorValidationException contributorValidationException) + { + return BadRequest(contributorValidationException.InnerException); + } + catch (ContributorDependencyValidationException contributorDependencyValidationException) + { + return BadRequest(contributorDependencyValidationException.InnerException); + } } } } From 292384d366b4ea90747486bdbb6059af2fcfd1ab Mon Sep 17 00:00:00 2001 From: slimahmad6 Date: Thu, 27 Mar 2025 15:43:01 +0100 Subject: [PATCH 04/12] ShouldReturnInternalServerErrorOnPostIfServerErrorOccurredAsync -> FAIL --- ...ributorsControllerTests.Exceptions.Post.cs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs b/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs index b701fcac..ff5fe1d1 100644 --- a/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs +++ b/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs @@ -47,5 +47,37 @@ public async Task ShouldReturnBadRequestOnPostIfValidationErrorOccursAsync(Xepti this.contributorServiceMock.VerifyNoOtherCalls(); } + [Theory] + [MemberData(nameof(ServerExceptions))] + public async Task ShouldReturnInternalServerErrorOnPostIfServerErrorOccurredAsync( + Xeption serverException) + { + // given + Contributor someContributor = CreateRandomContributor(); + + InternalServerErrorObjectResult expectedInternalServerErrorObjectResult = + InternalServerError(serverException); + + var expectedActionResult = + new ActionResult(expectedInternalServerErrorObjectResult); + + this.contributorServiceMock.Setup(service => + service.AddContributorAsync(It.IsAny())) + .ThrowsAsync(serverException); + + // when + ActionResult actualActionResult = + await this.contributorsController.PostContributorAsync(someContributor); + + // then + actualActionResult.ShouldBeEquivalentTo(expectedActionResult); + + this.contributorServiceMock.Verify(service => + service.AddContributorAsync(It.IsAny()), + Times.Once); + + this.contributorServiceMock.VerifyNoOtherCalls(); + } + } } From 4bfcad4d09e14a55da15ae25d896426cbe4a0488 Mon Sep 17 00:00:00 2001 From: slimahmad6 Date: Thu, 27 Mar 2025 15:43:27 +0100 Subject: [PATCH 05/12] ShouldReturnInternalServerErrorOnPostIfServerErrorOccurredAsync -> PASS --- GitFyle.Core.Api/Controllers/ContributorsController.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/GitFyle.Core.Api/Controllers/ContributorsController.cs b/GitFyle.Core.Api/Controllers/ContributorsController.cs index 545c9a23..ae79aa14 100644 --- a/GitFyle.Core.Api/Controllers/ContributorsController.cs +++ b/GitFyle.Core.Api/Controllers/ContributorsController.cs @@ -38,6 +38,14 @@ public async ValueTask> PostContributorAsync(Contribut { return BadRequest(contributorDependencyValidationException.InnerException); } + catch (ContributorDependencyException contributorDependencyException) + { + return InternalServerError(contributorDependencyException); + } + catch (ContributorServiceException contributorServiceException) + { + return InternalServerError(contributorServiceException); + } } } } From 2ddd7d9eec357586dba99581fc7d71f9cd23c0a2 Mon Sep 17 00:00:00 2001 From: slimahmad6 Date: Thu, 27 Mar 2025 15:44:09 +0100 Subject: [PATCH 06/12] ShouldReturnConflictOnPostIfAlreadyExistsContributorErrorOccurredAsync -> FAIL --- ...ributorsControllerTests.Exceptions.Post.cs | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs b/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs index ff5fe1d1..cc0a5f49 100644 --- a/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs +++ b/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs @@ -79,5 +79,49 @@ public async Task ShouldReturnInternalServerErrorOnPostIfServerErrorOccurredAsyn this.contributorServiceMock.VerifyNoOtherCalls(); } + [Fact] + public async Task ShouldReturnConflictOnPostIfAlreadyExistsContributorErrorOccurredAsync() + { + // given + Contributor someContributor = CreateRandomContributor(); + var someInnerException = new Exception(); + string someMessage = GetRandomString(); + var someDictionaryData = GetRandomDictionaryData(); + + var alreadyExistsContributorException = + new AlreadyExistsContributorException( + message: someMessage, + innerException: someInnerException, + data: someInnerException.Data); + + var contributorDependencyValidationException = + new ContributorDependencyValidationException( + message: someMessage, + innerException: alreadyExistsContributorException, + data: someDictionaryData); + + ConflictObjectResult expectedConflictObjectResult = + Conflict(alreadyExistsContributorException); + + var expectedActionResult = + new ActionResult(expectedConflictObjectResult); + + this.contributorServiceMock.Setup(service => + service.AddContributorAsync(It.IsAny())) + .ThrowsAsync(contributorDependencyValidationException); + + // when + ActionResult actualActionResult = + await this.contributorsController.PostContributorAsync(someContributor); + + // then + actualActionResult.ShouldBeEquivalentTo(expectedActionResult); + + this.contributorServiceMock.Verify(service => + service.AddContributorAsync(It.IsAny()), + Times.Once); + + this.contributorServiceMock.VerifyNoOtherCalls(); + } } } From 35f27c0dd6fd4d6c86365b3ddd23f6abb0dfef64 Mon Sep 17 00:00:00 2001 From: slimahmad6 Date: Thu, 27 Mar 2025 15:44:30 +0100 Subject: [PATCH 07/12] ShouldReturnConflictOnPostIfAlreadyExistsContributorErrorOccurredAsync -> PASS --- GitFyle.Core.Api/Controllers/ContributorsController.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/GitFyle.Core.Api/Controllers/ContributorsController.cs b/GitFyle.Core.Api/Controllers/ContributorsController.cs index ae79aa14..4d0ed3b2 100644 --- a/GitFyle.Core.Api/Controllers/ContributorsController.cs +++ b/GitFyle.Core.Api/Controllers/ContributorsController.cs @@ -34,6 +34,11 @@ public async ValueTask> PostContributorAsync(Contribut { return BadRequest(contributorValidationException.InnerException); } + catch (ContributorDependencyValidationException contributorDependencyValidationException) + when (contributorDependencyValidationException.InnerException is AlreadyExistsContributorException) + { + return Conflict(contributorDependencyValidationException.InnerException); + } catch (ContributorDependencyValidationException contributorDependencyValidationException) { return BadRequest(contributorDependencyValidationException.InnerException); From c95e57ee9eb9a740102eee8d8d36e4072af824b8 Mon Sep 17 00:00:00 2001 From: slimahmad6 Date: Thu, 27 Mar 2025 15:49:50 +0100 Subject: [PATCH 08/12] CODERUB : Added invalideRefrenceContribution --- .../Controllers/ContributorsController.cs | 5 +++++ .../InvalidReferenceContributorException.cs | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 GitFyle.Core.Api/Models/Foundations/Contributors/Exceptions/InvalidReferenceContributorException.cs diff --git a/GitFyle.Core.Api/Controllers/ContributorsController.cs b/GitFyle.Core.Api/Controllers/ContributorsController.cs index 4d0ed3b2..f6ea22a2 100644 --- a/GitFyle.Core.Api/Controllers/ContributorsController.cs +++ b/GitFyle.Core.Api/Controllers/ContributorsController.cs @@ -34,6 +34,11 @@ public async ValueTask> PostContributorAsync(Contribut { return BadRequest(contributorValidationException.InnerException); } + catch (ContributorDependencyValidationException contributorDependencyValidationException) + when (contributorDependencyValidationException.InnerException is InvalidReferenceContributorException) + { + return FailedDependency(contributorDependencyValidationException.InnerException); + } catch (ContributorDependencyValidationException contributorDependencyValidationException) when (contributorDependencyValidationException.InnerException is AlreadyExistsContributorException) { diff --git a/GitFyle.Core.Api/Models/Foundations/Contributors/Exceptions/InvalidReferenceContributorException.cs b/GitFyle.Core.Api/Models/Foundations/Contributors/Exceptions/InvalidReferenceContributorException.cs new file mode 100644 index 00000000..49ea91aa --- /dev/null +++ b/GitFyle.Core.Api/Models/Foundations/Contributors/Exceptions/InvalidReferenceContributorException.cs @@ -0,0 +1,17 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using System; +using System.Collections; +using Xeptions; + +namespace GitFyle.Core.Api.Models.Foundations.Contributors.Exceptions +{ + public class InvalidReferenceContributorException : Xeption + { + public InvalidReferenceContributorException(string message, Exception innerException, IDictionary data) + : base(message, innerException,data) + { } + } +} \ No newline at end of file From 170571e3346ec45adf83fb462eeb917f0e0c0648 Mon Sep 17 00:00:00 2001 From: slimahmad6 Date: Tue, 29 Apr 2025 16:03:46 +0100 Subject: [PATCH 09/12] CODE Removed extra space --- .../ContributionTypesControllerTests.Exceptions.Get.cs | 2 +- .../ContributionTypesControllerTests.Exceptions.GetById.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/GitFyle.Core.Api.Tests.Unit/Controllers/ContributionTypes/ContributionTypesControllerTests.Exceptions.Get.cs b/GitFyle.Core.Api.Tests.Unit/Controllers/ContributionTypes/ContributionTypesControllerTests.Exceptions.Get.cs index 2affa8ff..cdcb311d 100644 --- a/GitFyle.Core.Api.Tests.Unit/Controllers/ContributionTypes/ContributionTypesControllerTests.Exceptions.Get.cs +++ b/GitFyle.Core.Api.Tests.Unit/Controllers/ContributionTypes/ContributionTypesControllerTests.Exceptions.Get.cs @@ -18,7 +18,7 @@ public partial class ContributionTypesControllerTests [Theory] [MemberData(nameof(ServerExceptions))] public async Task ShouldReturnInternalServerErrorOnGetIfServerErrorOccurredAsync( - Xeption serverException) + Xeption serverException) { // given InternalServerErrorObjectResult expectedInternalServerErrorObjectResult = diff --git a/GitFyle.Core.Api.Tests.Unit/Controllers/ContributionTypes/ContributionTypesControllerTests.Exceptions.GetById.cs b/GitFyle.Core.Api.Tests.Unit/Controllers/ContributionTypes/ContributionTypesControllerTests.Exceptions.GetById.cs index 07a863d6..5f729eab 100644 --- a/GitFyle.Core.Api.Tests.Unit/Controllers/ContributionTypes/ContributionTypesControllerTests.Exceptions.GetById.cs +++ b/GitFyle.Core.Api.Tests.Unit/Controllers/ContributionTypes/ContributionTypesControllerTests.Exceptions.GetById.cs @@ -19,7 +19,7 @@ public partial class ContributionTypesControllerTests [Theory] [MemberData(nameof(ValidationExceptions))] public async Task ShouldReturnBadRequestOnGetByIdIfValidationErrorOccursAsync( - Xeption validationException) + Xeption validationException) { // given Guid someId = Guid.NewGuid(); @@ -51,7 +51,7 @@ public async Task ShouldReturnBadRequestOnGetByIdIfValidationErrorOccursAsync( [Theory] [MemberData(nameof(ServerExceptions))] public async Task ShouldReturnInternalServerErrorOnGetByIdIfServerErrorOccursAsync( - Xeption validationException) + Xeption validationException) { // given Guid someId = Guid.NewGuid(); From baafb877bc597ee9c1e57c017f1a6d80c89d3a12 Mon Sep 17 00:00:00 2001 From: slimahmad6 Date: Tue, 29 Apr 2025 16:06:52 +0100 Subject: [PATCH 10/12] CODE RUB: Removed extra space --- .../ContributorsControllerTests.Exceptions.Post.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs b/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs index cc0a5f49..1c1ef7eb 100644 --- a/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs +++ b/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs @@ -18,7 +18,8 @@ public partial class ContributorsControllerTests { [Theory] [MemberData(nameof(ValidationExceptions))] - public async Task ShouldReturnBadRequestOnPostIfValidationErrorOccursAsync(Xeption validationException) + public async Task ShouldReturnBadRequestOnPostIfValidationErrorOccursAsync( + Xeption validationException) { // given Contributor someContributor = CreateRandomContributor(); From 84335b93faefa864467014b46a8e0f4af3f0cabc Mon Sep 17 00:00:00 2001 From: slimahmad6 Date: Tue, 29 Apr 2025 16:20:32 +0100 Subject: [PATCH 11/12] ShouldReturnFailedDependencyOnPostIfReferenceExceptionOccursAsync -> PASS --- ...ributorsControllerTests.Exceptions.Post.cs | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs b/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs index 1c1ef7eb..082e7fac 100644 --- a/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs +++ b/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs @@ -6,6 +6,8 @@ using System.Threading.Tasks; using GitFyle.Core.Api.Models.Foundations.Contributors; using GitFyle.Core.Api.Models.Foundations.Contributors.Exceptions; +using GitFyle.Core.Api.Models.Foundations.Contributors; +using GitFyle.Core.Api.Models.Foundations.Contributors.Exceptions; using Microsoft.AspNetCore.Mvc; using Moq; using RESTFulSense.Clients.Extensions; @@ -124,5 +126,49 @@ public async Task ShouldReturnConflictOnPostIfAlreadyExistsContributorErrorOccur this.contributorServiceMock.VerifyNoOtherCalls(); } + + [Fact] + public async Task ShouldReturnFailedDependencyOnPostIfReferenceExceptionOccursAsync() + { + // given + Contributor someContributor = CreateRandomContributor(); + var someInnerException = new Exception(); + string someMessage = GetRandomString(); + + var invalidReferenceContributorException = + new InvalidReferenceContributorException( + message: someMessage, + innerException: someInnerException, + data: someInnerException.Data); + + var contributorDependencyValidationException = + new ContributorDependencyValidationException( + message: someMessage, + innerException: invalidReferenceContributorException, + data: invalidReferenceContributorException.Data); + + FailedDependencyObjectResult expectedFailedDependencyObjectResult = + FailedDependency(invalidReferenceContributorException); + + var expectedActionResult = + new ActionResult(expectedFailedDependencyObjectResult); + + this.contributorServiceMock.Setup(service => + service.AddContributorAsync(It.IsAny())) + .ThrowsAsync(contributorDependencyValidationException); + + // when + ActionResult actualActionResult = + await this.contributorsController.PostContributorAsync(someContributor); + + // then + actualActionResult.ShouldBeEquivalentTo(expectedActionResult); + + this.contributorServiceMock.Verify(service => + service.AddContributorAsync(It.IsAny()), + Times.Once); + + this.contributorServiceMock.VerifyNoOtherCalls(); + } } } From be81b45359cf9ab34cc819096ee122dd1ffbcd8e Mon Sep 17 00:00:00 2001 From: slimahmad6 Date: Wed, 30 Apr 2025 15:13:02 +0100 Subject: [PATCH 12/12] CODE RUB: Exception handling and removal of extra namespaces --- ...ntributionTypesControllerTests.Exceptions.Delete.cs | 1 - .../ContributorsControllerTests.Exceptions.Post.cs | 2 -- .../Contributors/ContributorService.Exceptions.cs | 10 ++++++++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/GitFyle.Core.Api.Tests.Unit/Controllers/ContributionTypes/ContributionTypesControllerTests.Exceptions.Delete.cs b/GitFyle.Core.Api.Tests.Unit/Controllers/ContributionTypes/ContributionTypesControllerTests.Exceptions.Delete.cs index 0b37a5a8..a25c1037 100644 --- a/GitFyle.Core.Api.Tests.Unit/Controllers/ContributionTypes/ContributionTypesControllerTests.Exceptions.Delete.cs +++ b/GitFyle.Core.Api.Tests.Unit/Controllers/ContributionTypes/ContributionTypesControllerTests.Exceptions.Delete.cs @@ -3,7 +3,6 @@ // ---------------------------------------------------------------------------------- using System; -using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; using GitFyle.Core.Api.Models.Foundations.ContributionTypes; using GitFyle.Core.Api.Models.Foundations.ContributionTypes.Exceptions; diff --git a/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs b/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs index 082e7fac..0ab7085f 100644 --- a/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs +++ b/GitFyle.Core.Api.Tests.Unit/Controllers/Contributors/ContributorsControllerTests.Exceptions.Post.cs @@ -6,8 +6,6 @@ using System.Threading.Tasks; using GitFyle.Core.Api.Models.Foundations.Contributors; using GitFyle.Core.Api.Models.Foundations.Contributors.Exceptions; -using GitFyle.Core.Api.Models.Foundations.Contributors; -using GitFyle.Core.Api.Models.Foundations.Contributors.Exceptions; using Microsoft.AspNetCore.Mvc; using Moq; using RESTFulSense.Clients.Extensions; diff --git a/GitFyle.Core.Api/Services/Foundations/Contributors/ContributorService.Exceptions.cs b/GitFyle.Core.Api/Services/Foundations/Contributors/ContributorService.Exceptions.cs index 0136b9cf..28de35d1 100644 --- a/GitFyle.Core.Api/Services/Foundations/Contributors/ContributorService.Exceptions.cs +++ b/GitFyle.Core.Api/Services/Foundations/Contributors/ContributorService.Exceptions.cs @@ -56,6 +56,16 @@ private async ValueTask TryCatch(ReturningContributorFunction retur throw await CreateAndLogDependencyValidationExceptionAsync(alreadyExistsContributorException); } + catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException) + { + var invalidReferenceContributorException = + new InvalidReferenceContributorException( + message: "Invalid contributor reference error occurred.", + innerException: foreignKeyConstraintConflictException, + data: foreignKeyConstraintConflictException.Data); + + throw await CreateAndLogDependencyValidationExceptionAsync(invalidReferenceContributorException); + } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) {