diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Exceptions.Add.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Exceptions.Add.cs index 051c01fd..b4fe54be 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Exceptions.Add.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Exceptions.Add.cs @@ -19,7 +19,7 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Groups public partial class GroupServiceTests { [Fact] - public async Task ShouldThrowCriticalDependencyExceptionOnCreateIfSqlErrorOccursAndLogItAsync() + private async Task ShouldThrowCriticalDependencyExceptionOnCreateIfSqlErrorOccursAndLogItAsync() { // given DateTimeOffset randomDateTime = GetRandomDateTime(); @@ -27,10 +27,14 @@ public async Task ShouldThrowCriticalDependencyExceptionOnCreateIfSqlErrorOccurs SqlException sqlException = GetSqlException(); var failedGroupStorageException = - new FailedGroupStorageException(sqlException); + new FailedGroupStorageException( + message: "Failed group storage error occurred, contact support.", + innerException: sqlException); var expectedGroupDependencyException = - new GroupDependencyException(failedGroupStorageException); + new GroupDependencyException( + message: "Group dependency error occurred, contact support.", + innerException: failedGroupStorageException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTimeOffset()) @@ -67,7 +71,7 @@ await Assert.ThrowsAsync( } [Fact] - public async Task ShouldThrowDependencyValidationExceptionOnCreateIfGroupAlreadyExsitsAndLogItAsync() + private async Task ShouldThrowDependencyValidationExceptionOnCreateIfGroupAlreadyExsitsAndLogItAsync() { // given Group randomGroup = CreateRandomGroup(); @@ -78,10 +82,14 @@ public async Task ShouldThrowDependencyValidationExceptionOnCreateIfGroupAlready new DuplicateKeyException(randomMessage); var alreadyExistsGroupException = - new AlreadyExistsGroupException(duplicateKeyException); + new AlreadyExistsGroupException( + message: "Group with the same id already exists.", + innerException: duplicateKeyException); var expectedGroupDependencyValidationException = - new GroupDependencyValidationException(alreadyExistsGroupException); + new GroupDependencyValidationException( + message: "Group dependency validation occurred, please try again.", + innerException: alreadyExistsGroupException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTimeOffset()) @@ -118,7 +126,7 @@ await Assert.ThrowsAsync( } [Fact] - public async void ShouldThrowValidationExceptionOnCreateIfReferenceErrorOccursAndLogItAsync() + private async void ShouldThrowValidationExceptionOnCreateIfReferenceErrorOccursAndLogItAsync() { // given Group someGroup = CreateRandomGroup(); @@ -129,10 +137,14 @@ public async void ShouldThrowValidationExceptionOnCreateIfReferenceErrorOccursAn new ForeignKeyConstraintConflictException(exceptionMessage); var invalidGroupReferenceException = - new InvalidGroupReferenceException(foreignKeyConstraintConflictException); + new InvalidGroupReferenceException( + message: "Invalid group reference error occurred.", + innerException: foreignKeyConstraintConflictException); var expectedGroupDependencyValidationException = - new GroupDependencyValidationException(invalidGroupReferenceException); + new GroupDependencyValidationException( + message: "Group dependency validation occurred, please try again.", + innerException: invalidGroupReferenceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTimeOffset()) @@ -169,7 +181,7 @@ await Assert.ThrowsAsync( } [Fact] - public async Task ShouldThrowDependencyExceptionOnCreateIfDatabaseUpdateErrorOccursAndLogItAsync() + private async Task ShouldThrowDependencyExceptionOnCreateIfDatabaseUpdateErrorOccursAndLogItAsync() { // given Group someGroup = CreateRandomGroup(); @@ -178,10 +190,14 @@ public async Task ShouldThrowDependencyExceptionOnCreateIfDatabaseUpdateErrorOcc new DbUpdateException(); var failedGroupStorageException = - new FailedGroupStorageException(databaseUpdateException); + new FailedGroupStorageException( + message: "Failed group storage error occurred, contact support.", + innerException: databaseUpdateException); var expectedGroupDependencyException = - new GroupDependencyException(failedGroupStorageException); + new GroupDependencyException( + message: "Group dependency error occurred, contact support.", + innerException: failedGroupStorageException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTimeOffset()) @@ -218,17 +234,21 @@ await Assert.ThrowsAsync( } [Fact] - public async Task ShouldThrowServiceExceptionOnCreateIfServiceErrorOccursAndLogItAsync() + private async Task ShouldThrowServiceExceptionOnCreateIfServiceErrorOccursAndLogItAsync() { // given Group someGroup = CreateRandomGroup(); var serviceException = new Exception(); var failedGroupServiceException = - new FailedGroupServiceException(serviceException); + new FailedGroupServiceException( + message: "Failed group service error occurred, please contact support.", + innerException: serviceException); var expectedGroupServiceException = - new GroupServiceException(failedGroupServiceException); + new GroupServiceException( + message: "Group service error occurred, contact support.", + innerException: failedGroupServiceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTimeOffset()) @@ -264,4 +284,4 @@ await Assert.ThrowsAsync( this.loggingBrokerMock.VerifyNoOtherCalls(); } } -} +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Exceptions.Modify.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Exceptions.Modify.cs index 8cb628bf..1124106e 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Exceptions.Modify.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Exceptions.Modify.cs @@ -19,17 +19,21 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Groups public partial class GroupServiceTests { [Fact] - public async Task ShouldThrowCriticalDependencyExceptionOnUpdateIfSqlErrorOccursAndLogItAsync() + private async Task ShouldThrowCriticalDependencyExceptionOnUpdateIfSqlErrorOccursAndLogItAsync() { // given Group randomGroup = CreateRandomGroup(); SqlException sqlException = GetSqlException(); var failedGroupStorageException = - new FailedGroupStorageException(sqlException); + new FailedGroupStorageException( + message: "Failed group storage error occurred, contact support.", + innerException: sqlException); var expectedGroupDependencyException = - new GroupDependencyException(failedGroupStorageException); + new GroupDependencyException( + message: "Group dependency error occurred, contact support.", + innerException: failedGroupStorageException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTimeOffset()) @@ -70,7 +74,7 @@ await Assert.ThrowsAsync( } [Fact] - public async void ShouldThrowValidationExceptionOnUpdateIfReferenceErrorOccursAndLogItAsync() + private async void ShouldThrowValidationExceptionOnUpdateIfReferenceErrorOccursAndLogItAsync() { // given Group someGroup = @@ -89,10 +93,14 @@ public async void ShouldThrowValidationExceptionOnUpdateIfReferenceErrorOccursAn new ForeignKeyConstraintConflictException(exceptionMessage); var invalidGroupReferenceException = - new InvalidGroupReferenceException(foreignKeyConstraintConflictException); + new InvalidGroupReferenceException( + message: "Invalid group reference error occurred.", + innerException: foreignKeyConstraintConflictException); var expectedGroupDependencyValidationException = - new GroupDependencyValidationException(invalidGroupReferenceException); + new GroupDependencyValidationException( + message: "Group dependency validation occurred, please try again.", + innerException: invalidGroupReferenceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTimeOffset()) @@ -132,17 +140,21 @@ await Assert.ThrowsAsync( } [Fact] - public async Task ShouldThrowDependencyExceptionOnModifyIfDatabaseUpdateExceptionOccursAndLogItAsync() + private async Task ShouldThrowDependencyExceptionOnModifyIfDatabaseUpdateExceptionOccursAndLogItAsync() { // given Group randomGroup = CreateRandomGroup(); var databaseUpdateException = new DbUpdateException(); var failedGroupStorageException = - new FailedGroupStorageException(databaseUpdateException); + new FailedGroupStorageException( + message: "Failed group storage error occurred, contact support.", + innerException: databaseUpdateException); var expectedGroupDependencyException = - new GroupDependencyException(failedGroupStorageException); + new GroupDependencyException( + message: "Group dependency error occurred, contact support.", + innerException: failedGroupStorageException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTimeOffset()) @@ -183,17 +195,21 @@ await Assert.ThrowsAsync( } [Fact] - public async Task ShouldThrowDependencyValidationExceptionOnUpdateIfDatabaseUpdateConcurrencyErrorOccursAndLogItAsync() + private async Task ShouldThrowDependencyValidationOnUpdateIfDatabaseUpdateConcurrencyErrorOccursAndLogItAsync() { // given Group randomGroup = CreateRandomGroup(); var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedGroupException = - new LockedGroupException(databaseUpdateConcurrencyException); + new LockedGroupException( + message: "Locked group record exception, please try again later", + innerException: databaseUpdateConcurrencyException); var expectedGroupDependencyValidationException = - new GroupDependencyValidationException(lockedGroupException); + new GroupDependencyValidationException( + message: "Group dependency validation occurred, please try again.", + innerException: lockedGroupException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTimeOffset()) @@ -234,17 +250,21 @@ await Assert.ThrowsAsync( } [Fact] - public async Task ShouldThrowServiceExceptionOnUpdateIfServiceErrorOccursAndLogItAsync() + private async Task ShouldThrowServiceExceptionOnUpdateIfServiceErrorOccursAndLogItAsync() { // given Group randomGroup = CreateRandomGroup(); var serviceException = new Exception(); var failedGroupException = - new FailedGroupServiceException(serviceException); + new FailedGroupServiceException( + message: "Failed group service error occurred, please contact support.", + innerException: serviceException); var expectedGroupServiceException = - new GroupServiceException(failedGroupException); + new GroupServiceException( + message: "Group service error occurred, contact support.", + innerException: failedGroupException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTimeOffset()) @@ -284,4 +304,4 @@ await Assert.ThrowsAsync( this.loggingBrokerMock.VerifyNoOtherCalls(); } } -} +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTestsExceptions.RemovebyId.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Exceptions.RemovebyId.cs similarity index 78% rename from Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTestsExceptions.RemovebyId.cs rename to Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Exceptions.RemovebyId.cs index 91d9d0fc..f1dc1c7d 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTestsExceptions.RemovebyId.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Exceptions.RemovebyId.cs @@ -9,10 +9,8 @@ using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; using Moq; -using Taarafo.Core.Models.GroupPosts.Exceptions; using Taarafo.Core.Models.Groups; using Taarafo.Core.Models.Groups.Exceptions; -using Taarafo.Core.Models.PostImpressions.Exceptions; using Xunit; namespace Taarafo.Core.Tests.Unit.Services.Foundations.Groups @@ -20,17 +18,21 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Groups public partial class GroupServiceTests { [Fact] - public async Task ShouldThrowCriticalDependencyExceptionOnRemoveWhenSqlExceptionOccursAndLogItAsync() + private async Task ShouldThrowCriticalDependencyExceptionOnRemoveWhenSqlExceptionOccursAndLogItAsync() { // given Guid someGroupId = Guid.NewGuid(); SqlException sqlException = GetSqlException(); var failedGroupStorageException = - new FailedGroupStorageException(sqlException); + new FailedGroupStorageException( + message: "Failed group storage error occurred, contact support.", + innerException: sqlException); var expectedGroupDependencyException = - new GroupDependencyException(failedGroupStorageException); + new GroupDependencyException( + message: "Group dependency error occurred, contact support.", + innerException: failedGroupStorageException); this.storageBrokerMock.Setup(broker => broker.SelectGroupByIdAsync(It.IsAny())) @@ -67,7 +69,7 @@ await Assert.ThrowsAsync( } [Fact] - public async Task ShouldThrowDependencyValidationOnRemoveIfDatabaseUpdateConcurrencyErrorOccursAndLogItAsync() + private async Task ShouldThrowDependencyValidationOnRemoveIfDatabaseUpdateConcurrencyErrorOccursAndLogItAsync() { // given Guid someGroupId = Guid.NewGuid(); @@ -76,10 +78,14 @@ public async Task ShouldThrowDependencyValidationOnRemoveIfDatabaseUpdateConcurr new DbUpdateConcurrencyException(); var lockedGroupException = - new LockedGroupException(databaseUpdateConcurrencyException); + new LockedGroupException( + message: "Locked group record exception, please try again later", + innerException: databaseUpdateConcurrencyException); var expectedGroupDependencyValidationException = - new GroupDependencyValidationException(lockedGroupException); + new GroupDependencyValidationException( + message: "Group dependency validation occurred, please try again.", + innerException: lockedGroupException); this.storageBrokerMock.Setup(broker => broker.SelectGroupByIdAsync(It.IsAny())) @@ -116,17 +122,21 @@ await Assert.ThrowsAsync( } [Fact] - public async Task ShouldThrowServiceExceptionOnRemoveIfExceptionOccursAndLogItAsync() + private async Task ShouldThrowServiceExceptionOnRemoveIfExceptionOccursAndLogItAsync() { // given Guid someGroupId = Guid.NewGuid(); var serviceException = new Exception(); var failedGroupServiceException = - new FailedGroupServiceException(serviceException); + new FailedGroupServiceException( + message: "Failed group service error occurred, please contact support.", + innerException: serviceException); var expectedGroupServiceException = - new GroupServiceException(failedGroupServiceException); + new GroupServiceException( + message: "Group service error occurred, contact support.", + innerException: failedGroupServiceException); this.storageBrokerMock.Setup(broker => broker.SelectGroupByIdAsync(It.IsAny())) @@ -162,4 +172,4 @@ await Assert.ThrowsAsync( this.dateTimeBrokerMock.VerifyNoOtherCalls(); } } -} +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Exceptions.RetrieveAll.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Exceptions.RetrieveAll.cs index 6eb1ffd7..014af625 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Exceptions.RetrieveAll.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Exceptions.RetrieveAll.cs @@ -12,87 +12,95 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Groups { - public partial class GroupServiceTests - { - [Fact] - public void ShouldThrowCriticalDependencyExceptionOnRetrieveAllIfSqlErrorOccursAndLogIt() - { - // given - SqlException sqlException = GetSqlException(); - - var failedGroupStorageException = - new FailedGroupStorageException(sqlException); - - var expectedGroupDependencyException = - new GroupDependencyException(failedGroupStorageException); - - this.storageBrokerMock.Setup(broker => - broker.SelectAllGroups()) - .Throws(sqlException); - - // when - Action retrieveAllGroupsAction = () => - this.groupService.RetrieveAllGroups(); - - GroupDependencyException actualGroupDependencyException = - Assert.Throws(retrieveAllGroupsAction); - - // then - actualGroupDependencyException.Should().BeEquivalentTo( - expectedGroupDependencyException); - - this.storageBrokerMock.Verify(broker => - broker.SelectAllGroups(), - Times.Once); - - this.loggingBrokerMock.Verify(broker => - broker.LogCritical(It.Is(SameExceptionAs( - expectedGroupDependencyException))), - Times.Once); - - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - } - - [Fact] - public void ShouldThrowServiceExceptionOnRetrieveAllWhenServiceErrorOccursAndLogIt() - { - //given - string exceptionMessage = GetRandomMessage(); - var serviceException = new Exception(exceptionMessage); - - var failedGroupServiceException = - new FailedGroupServiceException(serviceException); - - var expectedGroupServiceException = - new GroupServiceException(failedGroupServiceException); - - this.storageBrokerMock.Setup(broker => - broker.SelectAllGroups()) - .Throws(serviceException); - - //when - Action retrieveAllGroupsAction = () => - this.groupService.RetrieveAllGroups(); - - GroupServiceException actualGroupServiceException = - Assert.Throws(retrieveAllGroupsAction); - - //then - actualGroupServiceException.Should().BeEquivalentTo( - expectedGroupServiceException); - - this.storageBrokerMock.Verify(broker => - broker.SelectAllGroups(), - Times.Once); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedGroupServiceException))), - Times.Once); - - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - } - } -} + public partial class GroupServiceTests + { + [Fact] + private void ShouldThrowCriticalDependencyExceptionOnRetrieveAllIfSqlErrorOccursAndLogIt() + { + // given + SqlException sqlException = GetSqlException(); + + var failedGroupStorageException = + new FailedGroupStorageException( + message: "Failed group storage error occurred, contact support.", + innerException: sqlException); + + var expectedGroupDependencyException = + new GroupDependencyException( + message: "Group dependency error occurred, contact support.", + innerException: failedGroupStorageException); + + this.storageBrokerMock.Setup(broker => + broker.SelectAllGroups()) + .Throws(sqlException); + + // when + Action retrieveAllGroupsAction = () => + this.groupService.RetrieveAllGroups(); + + GroupDependencyException actualGroupDependencyException = + Assert.Throws(retrieveAllGroupsAction); + + // then + actualGroupDependencyException.Should().BeEquivalentTo( + expectedGroupDependencyException); + + this.storageBrokerMock.Verify(broker => + broker.SelectAllGroups(), + Times.Once); + + this.loggingBrokerMock.Verify(broker => + broker.LogCritical(It.Is(SameExceptionAs( + expectedGroupDependencyException))), + Times.Once); + + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + private void ShouldThrowServiceExceptionOnRetrieveAllWhenServiceErrorOccursAndLogIt() + { + //given + string exceptionMessage = GetRandomMessage(); + var serviceException = new Exception(exceptionMessage); + + var failedGroupServiceException = + new FailedGroupServiceException( + message: "Failed group service error occurred, please contact support.", + innerException: serviceException); + + var expectedGroupServiceException = + new GroupServiceException( + message: "Group service error occurred, contact support.", + innerException: failedGroupServiceException); + + this.storageBrokerMock.Setup(broker => + broker.SelectAllGroups()) + .Throws(serviceException); + + //when + Action retrieveAllGroupsAction = () => + this.groupService.RetrieveAllGroups(); + + GroupServiceException actualGroupServiceException = + Assert.Throws(retrieveAllGroupsAction); + + //then + actualGroupServiceException.Should().BeEquivalentTo( + expectedGroupServiceException); + + this.storageBrokerMock.Verify(broker => + broker.SelectAllGroups(), + Times.Once); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedGroupServiceException))), + Times.Once); + + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + } + } +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Exceptions.RetrieveById.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Exceptions.RetrieveById.cs index cfb7182d..7299bc7a 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Exceptions.RetrieveById.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Exceptions.RetrieveById.cs @@ -14,92 +14,100 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Groups { - public partial class GroupServiceTests - { - [Fact] - public async Task ShouldThrowCriticalDependencyExceptionOnRetrieveByIdIfSqlErrorOccursAndLogItAsync() - { - //given - Guid someGroupId = Guid.NewGuid(); - SqlException sqlException = GetSqlException(); - - var failedGroupStorageException = - new FailedGroupStorageException(sqlException); - - var expectedGroupDependencyException = - new GroupDependencyException(failedGroupStorageException); - - this.storageBrokerMock.Setup(broker => - broker.SelectGroupByIdAsync(It.IsAny())) - .ThrowsAsync(sqlException); - - //when - ValueTask retrieveGroupByIdTask = - this.groupService.RetrieveGroupByIdAsync(someGroupId); - - GroupDependencyException actualGroupDependencyException = - await Assert.ThrowsAsync( - retrieveGroupByIdTask.AsTask); - - //then - actualGroupDependencyException.Should().BeEquivalentTo( - expectedGroupDependencyException); - - this.storageBrokerMock.Verify(broker => - broker.SelectGroupByIdAsync(It.IsAny()), - Times.Once); - - this.loggingBrokerMock.Verify(broker => - broker.LogCritical(It.Is(SameExceptionAs( - expectedGroupDependencyException))), - Times.Once); - - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - } - - [Fact] - public async Task ShouldThrowServiceExceptionOnRetrieveByIdIfServiceErrorOccursAndLogItAsync() - { - //given - Guid someGroupId = Guid.NewGuid(); - var serviceException = new Exception(); - - var failedGroupServiceException = - new FailedGroupServiceException(serviceException); - - var expectedGroupServiceException = - new GroupServiceException(failedGroupServiceException); - - this.storageBrokerMock.Setup(broker => - broker.SelectGroupByIdAsync(It.IsAny())) - .ThrowsAsync(serviceException); - - //when - ValueTask retrieveGroupByIdTask = - this.groupService.RetrieveGroupByIdAsync(someGroupId); - - GroupServiceException actualGroupServiceException = - await Assert.ThrowsAsync( - retrieveGroupByIdTask.AsTask); - - //then - actualGroupServiceException.Should().BeEquivalentTo( - expectedGroupServiceException); - - this.storageBrokerMock.Verify(broker => - broker.SelectGroupByIdAsync(It.IsAny()), - Times.Once); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedGroupServiceException))), - Times.Once); - - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - } - } -} + public partial class GroupServiceTests + { + [Fact] + private async Task ShouldThrowCriticalDependencyExceptionOnRetrieveByIdIfSqlErrorOccursAndLogItAsync() + { + //given + Guid someGroupId = Guid.NewGuid(); + SqlException sqlException = GetSqlException(); + + var failedGroupStorageException = + new FailedGroupStorageException( + message: "Failed group storage error occurred, contact support.", + innerException: sqlException); + + var expectedGroupDependencyException = + new GroupDependencyException( + message: "Group dependency error occurred, contact support.", + innerException: failedGroupStorageException); + + this.storageBrokerMock.Setup(broker => + broker.SelectGroupByIdAsync(It.IsAny())) + .ThrowsAsync(sqlException); + + //when + ValueTask retrieveGroupByIdTask = + this.groupService.RetrieveGroupByIdAsync(someGroupId); + + GroupDependencyException actualGroupDependencyException = + await Assert.ThrowsAsync( + retrieveGroupByIdTask.AsTask); + + //then + actualGroupDependencyException.Should().BeEquivalentTo( + expectedGroupDependencyException); + + this.storageBrokerMock.Verify(broker => + broker.SelectGroupByIdAsync(It.IsAny()), + Times.Once); + + this.loggingBrokerMock.Verify(broker => + broker.LogCritical(It.Is(SameExceptionAs( + expectedGroupDependencyException))), + Times.Once); + + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + private async Task ShouldThrowServiceExceptionOnRetrieveByIdIfServiceErrorOccursAndLogItAsync() + { + //given + Guid someGroupId = Guid.NewGuid(); + var serviceException = new Exception(); + + var failedGroupServiceException = + new FailedGroupServiceException( + message: "Failed group service error occurred, please contact support.", + innerException: serviceException); + + var expectedGroupServiceException = + new GroupServiceException( + message: "Group service error occurred, contact support.", + innerException: failedGroupServiceException); + + this.storageBrokerMock.Setup(broker => + broker.SelectGroupByIdAsync(It.IsAny())) + .ThrowsAsync(serviceException); + + //when + ValueTask retrieveGroupByIdTask = + this.groupService.RetrieveGroupByIdAsync(someGroupId); + + GroupServiceException actualGroupServiceException = + await Assert.ThrowsAsync( + retrieveGroupByIdTask.AsTask); + + //then + actualGroupServiceException.Should().BeEquivalentTo( + expectedGroupServiceException); + + this.storageBrokerMock.Verify(broker => + broker.SelectGroupByIdAsync(It.IsAny()), + Times.Once); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedGroupServiceException))), + Times.Once); + + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + } + } +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Logic.Add.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Logic.Add.cs index e4916cdc..cabaf1e6 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Logic.Add.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Logic.Add.cs @@ -16,7 +16,7 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Groups public partial class GroupServiceTests { [Fact] - public async Task ShouldAddGroupAsync() + private async Task ShouldAddGroupAsync() { // given DateTimeOffset randomDateTime = GetRandomDateTime(); @@ -53,4 +53,4 @@ public async Task ShouldAddGroupAsync() this.loggingBrokerMock.VerifyNoOtherCalls(); } } -} +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Logic.Modify.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Logic.Modify.cs index 0fa3bcbf..e0362ddc 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Logic.Modify.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Logic.Modify.cs @@ -16,7 +16,7 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Groups public partial class GroupServiceTests { [Fact] - public async Task ShouldUpdateGroupAsync() + private async Task ShouldUpdateGroupAsync() { // given DateTimeOffset randomDate = GetRandomDateTimeOffset(); @@ -64,4 +64,4 @@ public async Task ShouldUpdateGroupAsync() this.loggingBrokerMock.VerifyNoOtherCalls(); } } -} +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Logic.RemoveById.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Logic.RemoveById.cs index eb385dcb..e0052808 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Logic.RemoveById.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Logic.RemoveById.cs @@ -15,7 +15,7 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Groups public partial class GroupServiceTests { [Fact] - public async void ShouldRemoveGroupByIdAsync() + private async void ShouldRemoveGroupByIdAsync() { // given Guid randomId = Guid.NewGuid(); @@ -54,4 +54,4 @@ public async void ShouldRemoveGroupByIdAsync() this.dateTimeBrokerMock.VerifyNoOtherCalls(); } } -} +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Logic.RetrieveAll.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Logic.RetrieveAll.cs index 26ac892a..3f4a710a 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Logic.RetrieveAll.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Logic.RetrieveAll.cs @@ -14,7 +14,7 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Groups public partial class GroupServiceTests { [Fact] - public void ShouldRetrieveAllGroups() + private void ShouldRetrieveAllGroups() { // given IQueryable randomGroups = CreateRandomGroups(); @@ -41,4 +41,4 @@ public void ShouldRetrieveAllGroups() this.dateTimeBrokerMock.VerifyNoOtherCalls(); } } -} +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Logic.RetrieveById.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Logic.RetrieveById.cs index 46b9e231..acd3646c 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Logic.RetrieveById.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Logic.RetrieveById.cs @@ -14,7 +14,7 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Groups public partial class GroupServiceTests { [Fact] - public async void ShouldRetrieveGroupByIdAsync() + private async void ShouldRetrieveGroupByIdAsync() { //given Group someGroup = CreateRandomGroup(); @@ -41,4 +41,4 @@ public async void ShouldRetrieveGroupByIdAsync() this.dateTimeBrokerMock.VerifyNoOtherCalls(); } } -} +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Validations.Add.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Validations.Add.cs index 1ad3f4c3..9dfe8c7f 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Validations.Add.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Validations.Add.cs @@ -9,199 +9,206 @@ using Moq; using Taarafo.Core.Models.Groups; using Taarafo.Core.Models.Groups.Exceptions; -using Taarafo.Core.Models.PostImpressions.Exceptions; using Xunit; namespace Taarafo.Core.Tests.Unit.Services.Foundations.Groups { - public partial class GroupServiceTests - { - [Fact] - public async Task ShouldThrowExceptionOnCreateIfGroupIsNullAndLogItAsync() - { - // given - Group nullGroup = null; + public partial class GroupServiceTests + { + [Fact] + private async Task ShouldThrowExceptionOnCreateIfGroupIsNullAndLogItAsync() + { + // given + Group nullGroup = null; + + var nullGroupException = + new NullGroupException(); + + var expectedGroupValidationException = + new GroupValidationException( + message: "Group validation errors occurred, please try again.", + innerException: nullGroupException); + + // when + ValueTask addGroupTask = + this.groupService.AddGroupAsync(nullGroup); - var nullGroupException = - new NullGroupException(); + GroupValidationException actualGroupValidationException = + await Assert.ThrowsAsync( + addGroupTask.AsTask); - var expectedGroupValidationException = - new GroupValidationException(nullGroupException); + // then + actualGroupValidationException.Should().BeEquivalentTo( + expectedGroupValidationException); - // when - ValueTask addGroupTask = - this.groupService.AddGroupAsync(nullGroup); + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedGroupValidationException))), + Times.Once); + + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + } + + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData(" ")] + private async Task ShouldThrowValidationExceptionOnCreateIfGroupIsInvalidAndLogItAsync( + string invalidText) + { + // given + var invalidGroup = new Group + { + Name = invalidText, + Description = invalidText + }; + + var invalidGroupException = + new InvalidGroupException(); + + invalidGroupException.AddData( + key: nameof(Group.Id), + values: "Id is required"); + + invalidGroupException.AddData( + key: nameof(Group.Name), + values: "Text is required"); + + invalidGroupException.AddData( + key: nameof(Group.Description), + values: "Text is required"); + + invalidGroupException.AddData( + key: nameof(Group.CreatedDate), + values: "Date is required"); + + invalidGroupException.AddData( + key: nameof(Group.UpdatedDate), + values: "Date is required"); + + var expectedGroupValidationException = + new GroupValidationException( + message: "Group validation errors occurred, please try again.", + innerException: invalidGroupException); + + // when + ValueTask addGroupTask = + this.groupService.AddGroupAsync(invalidGroup); GroupValidationException actualGroupValidationException = - await Assert.ThrowsAsync( - addGroupTask.AsTask); - - // then - actualGroupValidationException.Should().BeEquivalentTo( - expectedGroupValidationException); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedGroupValidationException))), - Times.Once); - - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - } - - [Theory] - [InlineData(null)] - [InlineData("")] - [InlineData(" ")] - public async Task ShouldThrowValidationExceptionOnCreateIfGroupIsInvalidAndLogItAsync( - string invalidText) - { - // given - var invalidGroup = new Group - { - Name = invalidText, - Description = invalidText - }; - - var invalidGroupException = - new InvalidGroupException(); - - invalidGroupException.AddData( - key: nameof(Group.Id), - values: "Id is required"); - - invalidGroupException.AddData( - key: nameof(Group.Name), - values: "Text is required"); - - invalidGroupException.AddData( - key: nameof(Group.Description), - values: "Text is required"); - - invalidGroupException.AddData( - key: nameof(Group.CreatedDate), - values: "Date is required"); - - invalidGroupException.AddData( - key: nameof(Group.UpdatedDate), - values: "Date is required"); - - var expectedGroupValidationException = - new GroupValidationException(invalidGroupException); - - // when - ValueTask addGroupTask = - this.groupService.AddGroupAsync(invalidGroup); + await Assert.ThrowsAsync( + addGroupTask.AsTask); + + // then + actualGroupValidationException.Should().BeEquivalentTo( + expectedGroupValidationException); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedGroupValidationException))), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.InsertGroupAsync(invalidGroup), + Times.Never); + + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + private async Task ShouldThrowValidationExceptionOnCreateIfCreateAndUpdateDatesIsNotSameAndLogItAsync() + { + // given + int randomNumber = GetRandomNumber(); + DateTimeOffset randomDateTimeOffset = GetRandomDateTimeOffset(); + Group randomGroup = CreateRandomGroup(randomDateTimeOffset); + Group invalidGroup = randomGroup; + + invalidGroup.UpdatedDate = + invalidGroup.CreatedDate.AddDays(randomNumber); + + var invalidGroupException = new InvalidGroupException(); + + invalidGroupException.AddData( + key: nameof(Group.UpdatedDate), + values: $"Date is not the same as {nameof(Group.CreatedDate)}"); + + var expectedGroupValidationException = + new GroupValidationException( + message: "Group validation errors occurred, please try again.", + innerException: invalidGroupException); + + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Returns(randomDateTimeOffset); + + // when + ValueTask addGroupTask = + this.groupService.AddGroupAsync(invalidGroup); GroupValidationException actualGroupValidationException = await Assert.ThrowsAsync( addGroupTask.AsTask); - // then - actualGroupValidationException.Should().BeEquivalentTo( - expectedGroupValidationException); + // then + actualGroupValidationException.Should() + .BeEquivalentTo(expectedGroupValidationException); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once()); this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedGroupValidationException))), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.InsertGroupAsync(invalidGroup), - Times.Never); - - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - } - - [Fact] - public async Task ShouldThrowValidationExceptionOnCreateIfCreateAndUpdateDatesIsNotSameAndLogItAsync() - { - // given - int randomNumber = GetRandomNumber(); - DateTimeOffset randomDateTimeOffset = GetRandomDateTimeOffset(); - Group randomGroup = CreateRandomGroup(randomDateTimeOffset); - Group invalidGroup = randomGroup; - - invalidGroup.UpdatedDate = - invalidGroup.CreatedDate.AddDays(randomNumber); - - var invalidGroupException = new InvalidGroupException(); - - invalidGroupException.AddData( - key: nameof(Group.UpdatedDate), - values: $"Date is not the same as {nameof(Group.CreatedDate)}"); - - var expectedGroupValidationException = - new GroupValidationException(invalidGroupException); - - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Returns(randomDateTimeOffset); - - // when - ValueTask addGroupTask = - this.groupService.AddGroupAsync(invalidGroup); - - GroupValidationException actualGroupValidationException = - await Assert.ThrowsAsync( - addGroupTask.AsTask); - - // then - actualGroupValidationException.Should() - .BeEquivalentTo(expectedGroupValidationException); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once()); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedGroupValidationException))), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.InsertGroupAsync(It.IsAny()), - Times.Never); - - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - } - - [Theory] - [MemberData(nameof(MinutesBeforeOrAfter))] - public async Task ShouldThrowValidationExceptionOnCreateIfCreatedDateIsNotRecentAndLogItAsync( - int minutesBeforeOrAfter) - { - // given - DateTimeOffset randomDateTime = - GetRandomDateTimeOffset(); - - DateTimeOffset invalidDateTime = - randomDateTime.AddMinutes(minutesBeforeOrAfter); - - Group randomGroup = CreateRandomGroup(invalidDateTime); - Group invalidGroup = randomGroup; - - var invalidGroupException = - new InvalidGroupException(); - - invalidGroupException.AddData( - key: nameof(Group.CreatedDate), - values: "Date is not recent"); - - var expectedGroupValidationException = - new GroupValidationException(invalidGroupException); - - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Returns(randomDateTime); - - // when - ValueTask addGroupTask = - this.groupService.AddGroupAsync(invalidGroup); + broker.LogError(It.Is(SameExceptionAs( + expectedGroupValidationException))), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.InsertGroupAsync(It.IsAny()), + Times.Never); + + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + } + + [Theory] + [MemberData(nameof(MinutesBeforeOrAfter))] + private async Task ShouldThrowValidationExceptionOnCreateIfCreatedDateIsNotRecentAndLogItAsync( + int minutesBeforeOrAfter) + { + // given + DateTimeOffset randomDateTime = + GetRandomDateTimeOffset(); + + DateTimeOffset invalidDateTime = + randomDateTime.AddMinutes(minutesBeforeOrAfter); + + Group randomGroup = CreateRandomGroup(invalidDateTime); + Group invalidGroup = randomGroup; + + var invalidGroupException = + new InvalidGroupException(); + + invalidGroupException.AddData( + key: nameof(Group.CreatedDate), + values: "Date is not recent"); + + var expectedGroupValidationException = + new GroupValidationException( + message: "Group validation errors occurred, please try again.", + innerException: invalidGroupException); + + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Returns(randomDateTime); + + // when + ValueTask addGroupTask = + this.groupService.AddGroupAsync(invalidGroup); GroupValidationException actualGroupValidationException = await Assert.ThrowsAsync( @@ -209,24 +216,24 @@ await Assert.ThrowsAsync( // then actualGroupValidationException.Should().BeEquivalentTo( - expectedGroupValidationException); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedGroupValidationException))), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.InsertGroupAsync(It.IsAny()), - Times.Never); - - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - } - } + expectedGroupValidationException); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedGroupValidationException))), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.InsertGroupAsync(It.IsAny()), + Times.Never); + + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + } + } } \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Validations.Modify.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Validations.Modify.cs index f92bc039..bba4ff07 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Validations.Modify.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Validations.Modify.cs @@ -16,14 +16,16 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Groups public partial class GroupServiceTests { [Fact] - public async Task ShouldThrowValidationExceptionOnUpdateIfGroupIsNullAndLogItAsync() + private async Task ShouldThrowValidationExceptionOnUpdateIfGroupIsNullAndLogItAsync() { // given Group nullGroup = null; var nullGroupException = new NullGroupException(); var expectedGroupValidationException = - new GroupValidationException(nullGroupException); + new GroupValidationException( + message: "Group validation errors occurred, please try again.", + innerException: nullGroupException); // when ValueTask modifyGroupTask = @@ -59,7 +61,7 @@ await Assert.ThrowsAsync( [InlineData(null)] [InlineData("")] [InlineData(" ")] - public async Task ShouldThrowValidationExceptionOnUpdateIfGroupIsInvalidAndLogItAsync( + private async Task ShouldThrowValidationExceptionOnUpdateIfGroupIsInvalidAndLogItAsync( string invalidText) { // given @@ -98,7 +100,9 @@ public async Task ShouldThrowValidationExceptionOnUpdateIfGroupIsInvalidAndLogIt }); var expectedGroupValidationException = - new GroupValidationException(invalidGroupException); + new GroupValidationException( + message: "Group validation errors occurred, please try again.", + innerException: invalidGroupException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTimeOffset()) @@ -134,7 +138,7 @@ await Assert.ThrowsAsync( } [Fact] - public async Task ShouldThrowValidationExceptionOnUpdateIfCreateAndUpdateDatesIsSameAndLogItAsync() + private async Task ShouldThrowValidationExceptionOnUpdateIfCreateAndUpdateDatesIsSameAndLogItAsync() { // given DateTimeOffset randomDateTime = GetRandomDateTime(); @@ -147,7 +151,9 @@ public async Task ShouldThrowValidationExceptionOnUpdateIfCreateAndUpdateDatesIs values: $"Date is the same as {nameof(Group.CreatedDate)}"); var expectedGroupValidationException = - new GroupValidationException(invalidGroupException); + new GroupValidationException( + message: "Group validation errors occurred, please try again.", + innerException: invalidGroupException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTimeOffset()) @@ -185,7 +191,7 @@ await Assert.ThrowsAsync( [Theory] [MemberData(nameof(MinutesBeforeOrAfter))] - public async Task ShouldThrowValidationExceptionOnUpdateIfUpdatedDateIsNotRecentAndLogItAsync(int minutesBeforeOrAfter) + private async Task ShouldThrowValidationExceptionOnUpdateIfUpdatedDateIsNotRecentAndLogItAsync(int minutesBeforeOrAfter) { // given DateTimeOffset dateTime = GetRandomDateTimeOffset(); @@ -201,7 +207,9 @@ public async Task ShouldThrowValidationExceptionOnUpdateIfUpdatedDateIsNotRecent values: "Date is not recent"); var expectedGroupValidatonException = - new GroupValidationException(invalidGroupException); + new GroupValidationException( + message: "Group validation errors occurred, please try again.", + innerException: invalidGroupException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTimeOffset()) @@ -242,7 +250,7 @@ await Assert.ThrowsAsync( } [Fact] - public async Task ShouldThrowValidationExceptionOnModifyIfGroupDoesNotExistAndLogItAsync() + private async Task ShouldThrowValidationExceptionOnModifyIfGroupDoesNotExistAndLogItAsync() { // given int randomNegativeMinutes = GetRandomNegativeNumber(); @@ -256,7 +264,9 @@ public async Task ShouldThrowValidationExceptionOnModifyIfGroupDoesNotExistAndLo new NotFoundGroupException(nonExistGroup.Id); var expectedGroupValidationException = - new GroupValidationException(notFoundGroupException); + new GroupValidationException( + message: "Group validation errors occurred, please try again.", + innerException: notFoundGroupException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTimeOffset()) diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Validations.RemoveById.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Validations.RemoveById.cs index 77a4b806..1c1463e4 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Validations.RemoveById.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Validations.RemoveById.cs @@ -16,7 +16,7 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Groups public partial class GroupServiceTests { [Fact] - public async Task ShouldThrowValidationExceptionOnRemoveIfIdIsInvalidAndLogItAsync() + private async Task ShouldThrowValidationExceptionOnRemoveIfIdIsInvalidAndLogItAsync() { // given Guid invalidGroupId = Guid.Empty; @@ -29,7 +29,9 @@ public async Task ShouldThrowValidationExceptionOnRemoveIfIdIsInvalidAndLogItAsy values: "Id is required"); var expectedGroupValidationException = - new GroupValidationException(invalidGroupException); + new GroupValidationException( + message: "Group validation errors occurred, please try again.", + innerException: invalidGroupException); // when ValueTask removeGroupByIdTask = @@ -62,7 +64,7 @@ await Assert.ThrowsAsync( } [Fact] - public async Task ShouldThrowNotFoundExceptionOnRemoveGroupByIdIsNotFoundAndLogItAsync() + private async Task ShouldThrowNotFoundExceptionOnRemoveGroupByIdIsNotFoundAndLogItAsync() { // given Guid inputGroupId = Guid.NewGuid(); @@ -72,7 +74,9 @@ public async Task ShouldThrowNotFoundExceptionOnRemoveGroupByIdIsNotFoundAndLogI new NotFoundGroupException(inputGroupId); var expectedGroupValidationException = - new GroupValidationException(notFoundGroupException); + new GroupValidationException( + message: "Group validation errors occurred, please try again.", + innerException: notFoundGroupException); this.storageBrokerMock.Setup(broker => broker.SelectGroupByIdAsync(It.IsAny())) @@ -108,4 +112,4 @@ await Assert.ThrowsAsync( this.dateTimeBrokerMock.VerifyNoOtherCalls(); } } -} +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Validations.RetrieveById.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Validations.RetrieveById.cs index a046a1da..79eefaf0 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Validations.RetrieveById.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.Validations.RetrieveById.cs @@ -13,27 +13,29 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Groups { - public partial class GroupServiceTests - { - [Fact] - public async Task ShouldThrowValidationExceptionOnRetrieveByIdIfIdIsInvalidAndLogItAsync() - { - //given - Guid invalidGroupId = Guid.Empty; - - var invalidGroupException = - new InvalidGroupException(); - - invalidGroupException.AddData( - key: nameof(Group.Id), - values: "Id is required"); - - var expectedGroupValidationException = - new GroupValidationException(invalidGroupException); - - //when - ValueTask retrieveGroupByIdTask = - this.groupService.RetrieveGroupByIdAsync(invalidGroupId); + public partial class GroupServiceTests + { + [Fact] + private async Task ShouldThrowValidationExceptionOnRetrieveByIdIfIdIsInvalidAndLogItAsync() + { + //given + Guid invalidGroupId = Guid.Empty; + + var invalidGroupException = + new InvalidGroupException(); + + invalidGroupException.AddData( + key: nameof(Group.Id), + values: "Id is required"); + + var expectedGroupValidationException = + new GroupValidationException( + message: "Group validation errors occurred, please try again.", + innerException: invalidGroupException); + + //when + ValueTask retrieveGroupByIdTask = + this.groupService.RetrieveGroupByIdAsync(invalidGroupId); GroupValidationException actualGroupValidationException = await Assert.ThrowsAsync( @@ -41,42 +43,44 @@ await Assert.ThrowsAsync( //then actualGroupValidationException.Should().BeEquivalentTo( - expectedGroupValidationException); + expectedGroupValidationException); - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedGroupValidationException))), - Times.Once); + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedGroupValidationException))), + Times.Once); - this.storageBrokerMock.Verify(broker => - broker.SelectGroupByIdAsync(It.IsAny()), - Times.Never); + this.storageBrokerMock.Verify(broker => + broker.SelectGroupByIdAsync(It.IsAny()), + Times.Never); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - } + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + } - [Fact] - public async Task ShouldThrowNotFoundExceptionOnRetrieveByIdIfGroupIsNotFoundAndLogItAsync() - { - //given - Guid someGroupId = Guid.NewGuid(); - Group noGroup = null; + [Fact] + private async Task ShouldThrowNotFoundExceptionOnRetrieveByIdIfGroupIsNotFoundAndLogItAsync() + { + //given + Guid someGroupId = Guid.NewGuid(); + Group noGroup = null; - var notFoundGroupException = - new NotFoundGroupException(someGroupId); + var notFoundGroupException = + new NotFoundGroupException(someGroupId); - var expectedGroupValidationException = - new GroupValidationException(notFoundGroupException); + var expectedGroupValidationException = + new GroupValidationException( + message: "Group validation errors occurred, please try again.", + innerException: notFoundGroupException); - this.storageBrokerMock.Setup(broker => - broker.SelectGroupByIdAsync(It.IsAny())) - .ReturnsAsync(noGroup); + this.storageBrokerMock.Setup(broker => + broker.SelectGroupByIdAsync(It.IsAny())) + .ReturnsAsync(noGroup); - //when - ValueTask retrieveGroupByIdTask = - this.groupService.RetrieveGroupByIdAsync(someGroupId); + //when + ValueTask retrieveGroupByIdTask = + this.groupService.RetrieveGroupByIdAsync(someGroupId); GroupValidationException actualGroupValidationException = await Assert.ThrowsAsync( @@ -84,20 +88,20 @@ await Assert.ThrowsAsync( //then actualGroupValidationException.Should().BeEquivalentTo( - expectedGroupValidationException); - - this.storageBrokerMock.Verify(broker => - broker.SelectGroupByIdAsync(It.IsAny()), - Times.Once()); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedGroupValidationException))), - Times.Once); - - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - } - } -} + expectedGroupValidationException); + + this.storageBrokerMock.Verify(broker => + broker.SelectGroupByIdAsync(It.IsAny()), + Times.Once()); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedGroupValidationException))), + Times.Once); + + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + } + } +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.cs index 14c49672..46f9e05b 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Groups/GroupServiceTests.cs @@ -20,79 +20,79 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Groups { - public partial class GroupServiceTests - { - private readonly Mock storageBrokerMock; - private readonly Mock loggingBrokerMock; - private readonly Mock dateTimeBrokerMock; - private readonly IGroupService groupService; - - public GroupServiceTests() - { - this.storageBrokerMock = new Mock(); - this.dateTimeBrokerMock = new Mock(); - this.loggingBrokerMock = new Mock(); - - this.groupService = new GroupService( - storageBroker: this.storageBrokerMock.Object, - dateTimeBroker: this.dateTimeBrokerMock.Object, - loggingBroker: this.loggingBrokerMock.Object); - } - - private static Group CreateRandomGroup() => - CreateGroupFiller(dates: GetRandomDateTime()).Create(); - - private static Group CreateRandomGroup(DateTimeOffset dates) => - CreateGroupFiller(dates: dates).Create(); - - private static IQueryable CreateRandomGroups() - { - return CreateGroupFiller(dates: GetRandomDateTimeOffset()) - .Create(count: GetRandomNumber()) - .AsQueryable(); - } - - private static DateTimeOffset GetRandomDateTime() => - new DateTimeRange(earliestDate: new DateTime()).GetValue(); - - private static int GetRandomNumber() => - new IntRange(min: 2, max: 10).GetValue(); - - private static int GetRandomNegativeNumber() => - -1 * new IntRange(min: 2, max: 10).GetValue(); - - private static DateTimeOffset GetRandomDateTimeOffset() => - new DateTimeRange(earliestDate: new DateTime()).GetValue(); - - private static string GetRandomMessage() => - new MnemonicString(wordCount: GetRandomNumber()).GetValue(); - - private static SqlException GetSqlException() => - (SqlException)FormatterServices.GetUninitializedObject(typeof(SqlException)); - - private static Expression> SameExceptionAs(Xeption expectedException) => - actualException => actualException.SameExceptionAs(expectedException); - - public static TheoryData MinutesBeforeOrAfter() - { - int randomNumber = GetRandomNumber(); - int randomNegativeNumber = GetRandomNegativeNumber(); - - return new TheoryData - { - randomNumber, - randomNegativeNumber - }; - } - - private static Filler CreateGroupFiller(DateTimeOffset dates) - { - var filler = new Filler(); - - filler.Setup() - .OnType().Use(dates); - - return filler; - } - } -} + public partial class GroupServiceTests + { + private readonly Mock storageBrokerMock; + private readonly Mock loggingBrokerMock; + private readonly Mock dateTimeBrokerMock; + private readonly IGroupService groupService; + + public GroupServiceTests() + { + this.storageBrokerMock = new Mock(); + this.dateTimeBrokerMock = new Mock(); + this.loggingBrokerMock = new Mock(); + + this.groupService = new GroupService( + storageBroker: this.storageBrokerMock.Object, + dateTimeBroker: this.dateTimeBrokerMock.Object, + loggingBroker: this.loggingBrokerMock.Object); + } + + private static Group CreateRandomGroup() => + CreateGroupFiller(dates: GetRandomDateTime()).Create(); + + private static Group CreateRandomGroup(DateTimeOffset dates) => + CreateGroupFiller(dates: dates).Create(); + + private static IQueryable CreateRandomGroups() + { + return CreateGroupFiller(dates: GetRandomDateTimeOffset()) + .Create(count: GetRandomNumber()) + .AsQueryable(); + } + + private static DateTimeOffset GetRandomDateTime() => + new DateTimeRange(earliestDate: new DateTime()).GetValue(); + + private static int GetRandomNumber() => + new IntRange(min: 2, max: 10).GetValue(); + + private static int GetRandomNegativeNumber() => + -1 * new IntRange(min: 2, max: 10).GetValue(); + + private static DateTimeOffset GetRandomDateTimeOffset() => + new DateTimeRange(earliestDate: new DateTime()).GetValue(); + + private static string GetRandomMessage() => + new MnemonicString(wordCount: GetRandomNumber()).GetValue(); + + private static SqlException GetSqlException() => + (SqlException)FormatterServices.GetUninitializedObject(typeof(SqlException)); + + private static Expression> SameExceptionAs(Xeption expectedException) => + actualException => actualException.SameExceptionAs(expectedException); + + public static TheoryData MinutesBeforeOrAfter() + { + int randomNumber = GetRandomNumber(); + int randomNegativeNumber = GetRandomNegativeNumber(); + + return new TheoryData + { + randomNumber, + randomNegativeNumber + }; + } + + private static Filler CreateGroupFiller(DateTimeOffset dates) + { + var filler = new Filler(); + + filler.Setup() + .OnType().Use(dates); + + return filler; + } + } +} \ No newline at end of file diff --git a/Taarafo.Core/Models/Groups/Exceptions/AlreadyExistsGroupException.cs b/Taarafo.Core/Models/Groups/Exceptions/AlreadyExistsGroupException.cs index 2f4bd776..3d98bc29 100644 --- a/Taarafo.Core/Models/Groups/Exceptions/AlreadyExistsGroupException.cs +++ b/Taarafo.Core/Models/Groups/Exceptions/AlreadyExistsGroupException.cs @@ -8,10 +8,16 @@ namespace Taarafo.Core.Models.Groups.Exceptions { - public class AlreadyExistsGroupException : Xeption - { - public AlreadyExistsGroupException(Exception innerException) - : base(message: "Group with the same id already exists.", innerException) - { } - } + public class AlreadyExistsGroupException : Xeption + { + public AlreadyExistsGroupException(Exception innerException) + : base( + message: "Group with the same id already exists.", + innerException: innerException) + { } + + public AlreadyExistsGroupException(string message, Exception innerException) + : base(message, innerException) + { } + } } \ No newline at end of file diff --git a/Taarafo.Core/Models/Groups/Exceptions/FailedGroupServiceException.cs b/Taarafo.Core/Models/Groups/Exceptions/FailedGroupServiceException.cs index f311338b..a67559de 100644 --- a/Taarafo.Core/Models/Groups/Exceptions/FailedGroupServiceException.cs +++ b/Taarafo.Core/Models/Groups/Exceptions/FailedGroupServiceException.cs @@ -8,10 +8,16 @@ namespace Taarafo.Core.Models.Groups.Exceptions { - public class FailedGroupServiceException : Xeption - { - public FailedGroupServiceException(Exception innerException) - : base(message: "Failed group service error occurred, please contact support.", innerException) - { } - } + public class FailedGroupServiceException : Xeption + { + public FailedGroupServiceException(Exception innerException) + : base( + message: "Failed group service error occurred, please contact support.", + innerException: innerException) + { } + + public FailedGroupServiceException(string message, Exception innerException) + : base(message, innerException) + { } + } } \ No newline at end of file diff --git a/Taarafo.Core/Models/Groups/Exceptions/FailedGroupStorageException.cs b/Taarafo.Core/Models/Groups/Exceptions/FailedGroupStorageException.cs index 040a522f..89839ea8 100644 --- a/Taarafo.Core/Models/Groups/Exceptions/FailedGroupStorageException.cs +++ b/Taarafo.Core/Models/Groups/Exceptions/FailedGroupStorageException.cs @@ -8,10 +8,16 @@ namespace Taarafo.Core.Models.Groups.Exceptions { - public class FailedGroupStorageException : Xeption - { - public FailedGroupStorageException(Exception innerException) - : base(message: "Failed group storage error occurred, contact support.", innerException) - { } - } -} + public class FailedGroupStorageException : Xeption + { + public FailedGroupStorageException(Exception innerException) + : base( + message: "Failed group storage error occurred, contact support.", + innerException: innerException) + { } + + public FailedGroupStorageException(string message, Exception innerException) + : base(message, innerException) + { } + } +} \ No newline at end of file diff --git a/Taarafo.Core/Models/Groups/Exceptions/GroupDependencyException.cs b/Taarafo.Core/Models/Groups/Exceptions/GroupDependencyException.cs index e7c24152..efbc31ab 100644 --- a/Taarafo.Core/Models/Groups/Exceptions/GroupDependencyException.cs +++ b/Taarafo.Core/Models/Groups/Exceptions/GroupDependencyException.cs @@ -7,10 +7,16 @@ namespace Taarafo.Core.Models.Groups.Exceptions { - public class GroupDependencyException : Xeption - { - public GroupDependencyException(Xeption innerException) - : base(message: "Group dependency error occurred, contact support.", innerException) - { } - } -} + public class GroupDependencyException : Xeption + { + public GroupDependencyException(Xeption innerException) + : base( + message: "Group dependency error occurred, contact support.", + innerException: innerException) + { } + + public GroupDependencyException(string message, Xeption innerException) + : base(message, innerException) + { } + } +} \ No newline at end of file diff --git a/Taarafo.Core/Models/Groups/Exceptions/GroupDependencyValidationException.cs b/Taarafo.Core/Models/Groups/Exceptions/GroupDependencyValidationException.cs index 9fdeaf01..d921bed8 100644 --- a/Taarafo.Core/Models/Groups/Exceptions/GroupDependencyValidationException.cs +++ b/Taarafo.Core/Models/Groups/Exceptions/GroupDependencyValidationException.cs @@ -10,7 +10,13 @@ namespace Taarafo.Core.Models.Groups.Exceptions public class GroupDependencyValidationException : Xeption { public GroupDependencyValidationException(Xeption innerException) - : base(message: "Group dependency validation occurred, please try again.", innerException) + : base( + message: "Group dependency validation occurred, please try again.", + innerException: innerException) + { } + + public GroupDependencyValidationException(string message, Xeption innerException) + : base(message, innerException) { } } } \ No newline at end of file diff --git a/Taarafo.Core/Models/Groups/Exceptions/GroupServiceException.cs b/Taarafo.Core/Models/Groups/Exceptions/GroupServiceException.cs index 98103869..7364ca2c 100644 --- a/Taarafo.Core/Models/Groups/Exceptions/GroupServiceException.cs +++ b/Taarafo.Core/Models/Groups/Exceptions/GroupServiceException.cs @@ -7,10 +7,16 @@ namespace Taarafo.Core.Models.Groups.Exceptions { - public class GroupServiceException : Xeption - { - public GroupServiceException(Xeption innerException) - : base(message: "Group service error occurred, contact support.", innerException) - { } - } -} + public class GroupServiceException : Xeption + { + public GroupServiceException(Xeption innerException) + : base( + message: "Group service error occurred, contact support.", + innerException: innerException) + { } + + public GroupServiceException(string message, Xeption innerException) + : base(message, innerException) + { } + } +} \ No newline at end of file diff --git a/Taarafo.Core/Models/Groups/Exceptions/GroupValidationException.cs b/Taarafo.Core/Models/Groups/Exceptions/GroupValidationException.cs index 8c37252d..9a0df03d 100644 --- a/Taarafo.Core/Models/Groups/Exceptions/GroupValidationException.cs +++ b/Taarafo.Core/Models/Groups/Exceptions/GroupValidationException.cs @@ -7,11 +7,15 @@ namespace Taarafo.Core.Models.Groups.Exceptions { - public class GroupValidationException : Xeption - { - public GroupValidationException(Xeption innerException) - : base(message: "Group validation errors occurred, please try again.", - innerException) - { } - } -} + public class GroupValidationException : Xeption + { + public GroupValidationException(Xeption innerException) + : base(message: "Group validation errors occurred, please try again.", + innerException) + { } + + public GroupValidationException(string message, Xeption innerException) + : base(message, innerException) + { } + } +} \ No newline at end of file diff --git a/Taarafo.Core/Models/Groups/Exceptions/InvalidGroupException.cs b/Taarafo.Core/Models/Groups/Exceptions/InvalidGroupException.cs index fd3ee324..65decb82 100644 --- a/Taarafo.Core/Models/Groups/Exceptions/InvalidGroupException.cs +++ b/Taarafo.Core/Models/Groups/Exceptions/InvalidGroupException.cs @@ -3,14 +3,19 @@ // FREE TO USE TO CONNECT THE WORLD // --------------------------------------------------------------- +using System; using Xeptions; namespace Taarafo.Core.Models.Groups.Exceptions { - public class InvalidGroupException : Xeption - { - public InvalidGroupException() - : base(message: "Invalid group. Please correct the errors and try again.") - { } - } -} + public class InvalidGroupException : Xeption + { + public InvalidGroupException() + : base(message: "Invalid group. Please correct the errors and try again.") + { } + + public InvalidGroupException(string message, Exception innerException) + : base(message, innerException) + { } + } +} \ No newline at end of file diff --git a/Taarafo.Core/Models/Groups/Exceptions/InvalidGroupReferenceException.cs b/Taarafo.Core/Models/Groups/Exceptions/InvalidGroupReferenceException.cs index f8f5ccb1..3af80e39 100644 --- a/Taarafo.Core/Models/Groups/Exceptions/InvalidGroupReferenceException.cs +++ b/Taarafo.Core/Models/Groups/Exceptions/InvalidGroupReferenceException.cs @@ -8,10 +8,16 @@ namespace Taarafo.Core.Models.Groups.Exceptions { - public class InvalidGroupReferenceException : Xeption - { - public InvalidGroupReferenceException(Exception innerException) - : base(message: "Invalid group reference error occurred.", innerException) - { } - } + public class InvalidGroupReferenceException : Xeption + { + public InvalidGroupReferenceException(Exception innerException) + : base( + message: "Invalid group reference error occurred.", + innerException: innerException) + { } + + public InvalidGroupReferenceException(string message, Exception innerException) + : base(message, innerException) + { } + } } \ No newline at end of file diff --git a/Taarafo.Core/Models/Groups/Exceptions/LockedGroupException.cs b/Taarafo.Core/Models/Groups/Exceptions/LockedGroupException.cs index 9850a9fb..ea7569e0 100644 --- a/Taarafo.Core/Models/Groups/Exceptions/LockedGroupException.cs +++ b/Taarafo.Core/Models/Groups/Exceptions/LockedGroupException.cs @@ -8,10 +8,16 @@ namespace Taarafo.Core.Models.Groups.Exceptions { - public class LockedGroupException : Xeption - { - public LockedGroupException(Exception innerException) - : base(message: "Locked group record exception, please try again later", innerException) - { } - } + public class LockedGroupException : Xeption + { + public LockedGroupException(Exception innerException) + : base( + message: "Locked group record exception, please try again later", + innerException: innerException) + { } + + public LockedGroupException(string message, Exception innerException) + : base(message, innerException) + { } + } } \ No newline at end of file diff --git a/Taarafo.Core/Models/Groups/Exceptions/NotFoundGroupException.cs b/Taarafo.Core/Models/Groups/Exceptions/NotFoundGroupException.cs index 207c7e18..73d3bd72 100644 --- a/Taarafo.Core/Models/Groups/Exceptions/NotFoundGroupException.cs +++ b/Taarafo.Core/Models/Groups/Exceptions/NotFoundGroupException.cs @@ -14,4 +14,4 @@ public NotFoundGroupException(Guid groupId) : base(message: $"Couldn't find group with id: {groupId}.") { } } -} +} \ No newline at end of file diff --git a/Taarafo.Core/Models/Groups/Exceptions/NullGroupException.cs b/Taarafo.Core/Models/Groups/Exceptions/NullGroupException.cs index b11f4031..0976baca 100644 --- a/Taarafo.Core/Models/Groups/Exceptions/NullGroupException.cs +++ b/Taarafo.Core/Models/Groups/Exceptions/NullGroupException.cs @@ -7,11 +7,10 @@ namespace Taarafo.Core.Models.Groups.Exceptions { - public class NullGroupException : Xeption - { - public NullGroupException() - : base(message: "Group is null.") - { } - } -} - + public class NullGroupException : Xeption + { + public NullGroupException() + : base(message: "Group is null.") + { } + } +} \ No newline at end of file diff --git a/Taarafo.Core/Services/Foundations/Groups/GroupService.Validations.cs b/Taarafo.Core/Services/Foundations/Groups/GroupService.Validations.cs index 6efeddc4..1e16c6ed 100644 --- a/Taarafo.Core/Services/Foundations/Groups/GroupService.Validations.cs +++ b/Taarafo.Core/Services/Foundations/Groups/GroupService.Validations.cs @@ -9,136 +9,137 @@ namespace Taarafo.Core.Services.Foundations.Groups { - public partial class GroupService - { - private void ValidateGroupOnAdd(Group group) - { - ValidateGroupIsNotNull(group); - - Validate( - (Rule: IsInvalid(group.Id), Parameter: nameof(Group.Id)), - (Rule: IsInvalid(group.Name), Parameter: nameof(Group.Name)), - (Rule: IsInvalid(group.Description), Parameter: nameof(Group.Description)), - (Rule: IsInvalid(group.CreatedDate), Parameter: nameof(Group.CreatedDate)), - (Rule: IsInvalid(group.UpdatedDate), Parameter: nameof(Group.UpdatedDate)), - - (Rule: IsNotSame( - firstDate: group.UpdatedDate, - secondDate: group.CreatedDate, - secondDateName: nameof(Group.CreatedDate)), - Parameter: nameof(Group.UpdatedDate)), - - (Rule: IsNotRecent(group.CreatedDate), Parameter: nameof(Group.CreatedDate))); - } - - private void ValidateGroupOnModify(Group group) - { - ValidateGroupIsNotNull(group); - - Validate( - (Rule: IsInvalid(group.Id), Parameter: nameof(Group.Id)), - (Rule: IsInvalid(group.Name), Parameter: nameof(Group.Name)), - (Rule: IsInvalid(group.Description), Parameter: nameof(Group.Description)), - (Rule: IsInvalid(group.CreatedDate), Parameter: nameof(Group.CreatedDate)), - (Rule: IsInvalid(group.UpdatedDate), Parameter: nameof(Group.UpdatedDate)), - - (Rule: IsSame( - firstDate: group.UpdatedDate, - secondDate: group.CreatedDate, - secondDateName: nameof(Group.CreatedDate)), - Parameter: nameof(Group.UpdatedDate)), - - (Rule: IsNotRecent(group.UpdatedDate), Parameter: nameof(Group.UpdatedDate))); - } - - private static void ValidateGroupIsNotNull(Group group) - { - if (group is null) - { - throw new NullGroupException(); - } - } - - private void ValidateStorageGroup(Group maybeGroup, Guid groupId) - { - if (maybeGroup is null) - { - throw new NotFoundGroupException(groupId); - } - } - - private void ValidateGroupId(Guid groupId) => - Validate((Rule: IsInvalid(groupId), Parameter: nameof(Group.Id))); - - private static dynamic IsInvalid(Guid id) => new - { - Condition = id == Guid.Empty, - Message = "Id is required" - }; - - private static dynamic IsInvalid(string text) => new - { - Condition = string.IsNullOrWhiteSpace(text), - Message = "Text is required" - }; - - private static dynamic IsInvalid(DateTimeOffset date) => new - { - Condition = date == default, - Message = "Date is required" - }; - - private static dynamic IsNotSame( - DateTimeOffset firstDate, - DateTimeOffset secondDate, - string secondDateName) => new - { - Condition = firstDate != secondDate, - Message = $"Date is not the same as {secondDateName}" - }; - - private static dynamic IsSame( - DateTimeOffset firstDate, - DateTimeOffset secondDate, - string secondDateName) => new - { - Condition = firstDate == secondDate, - Message = $"Date is the same as {secondDateName}" - }; - - private dynamic IsNotRecent(DateTimeOffset date) => new - { - Condition = IsDateNotRecent(date), - Message = "Date is not recent" - }; - - private bool IsDateNotRecent(DateTimeOffset date) - { - DateTimeOffset currentDateTime = - this.dateTimeBroker.GetCurrentDateTimeOffset(); - - TimeSpan timeDifference = currentDateTime.Subtract(date); - TimeSpan oneMinute = TimeSpan.FromMinutes(1); - - return timeDifference.Duration() > oneMinute; - } - - private static void Validate(params (dynamic Rule, string Parameter)[] validations) - { - var invalidGroupException = - new InvalidGroupException(); - - foreach ((dynamic rule, string parameter) in validations) - { - if (rule.Condition) - { - invalidGroupException.UpsertDataList( - key: parameter, - value: rule.Message); - } - } - - invalidGroupException.ThrowIfContainsErrors(); - } - } + public partial class GroupService + { + private void ValidateGroupOnAdd(Group group) + { + ValidateGroupIsNotNull(group); + + Validate( + (Rule: IsInvalid(group.Id), Parameter: nameof(Group.Id)), + (Rule: IsInvalid(group.Name), Parameter: nameof(Group.Name)), + (Rule: IsInvalid(group.Description), Parameter: nameof(Group.Description)), + (Rule: IsInvalid(group.CreatedDate), Parameter: nameof(Group.CreatedDate)), + (Rule: IsInvalid(group.UpdatedDate), Parameter: nameof(Group.UpdatedDate)), + + (Rule: IsNotSame( + firstDate: group.UpdatedDate, + secondDate: group.CreatedDate, + secondDateName: nameof(Group.CreatedDate)), + Parameter: nameof(Group.UpdatedDate)), + + (Rule: IsNotRecent(group.CreatedDate), Parameter: nameof(Group.CreatedDate))); + } + + private void ValidateGroupOnModify(Group group) + { + ValidateGroupIsNotNull(group); + + Validate( + (Rule: IsInvalid(group.Id), Parameter: nameof(Group.Id)), + (Rule: IsInvalid(group.Name), Parameter: nameof(Group.Name)), + (Rule: IsInvalid(group.Description), Parameter: nameof(Group.Description)), + (Rule: IsInvalid(group.CreatedDate), Parameter: nameof(Group.CreatedDate)), + (Rule: IsInvalid(group.UpdatedDate), Parameter: nameof(Group.UpdatedDate)), + + (Rule: IsSame( + firstDate: group.UpdatedDate, + secondDate: group.CreatedDate, + secondDateName: nameof(Group.CreatedDate)), + Parameter: nameof(Group.UpdatedDate)), + + (Rule: IsNotRecent(group.UpdatedDate), Parameter: nameof(Group.UpdatedDate))); + } + + private static void ValidateGroupIsNotNull(Group group) + { + if (group is null) + { + throw new NullGroupException(); + } + } + + private void ValidateStorageGroup(Group maybeGroup, Guid groupId) + { + if (maybeGroup is null) + { + throw new NotFoundGroupException(groupId); + } + } + + private void ValidateGroupId(Guid groupId) => + Validate((Rule: IsInvalid(groupId), Parameter: nameof(Group.Id))); + + private static dynamic IsInvalid(Guid id) => new + { + Condition = id == Guid.Empty, + Message = "Id is required" + }; + + private static dynamic IsInvalid(string text) => new + { + Condition = string.IsNullOrWhiteSpace(text), + Message = "Text is required" + }; + + private static dynamic IsInvalid(DateTimeOffset date) => new + { + Condition = date == default, + Message = "Date is required" + }; + + private static dynamic IsNotSame( + DateTimeOffset firstDate, + DateTimeOffset secondDate, + string secondDateName) => new + { + Condition = firstDate != secondDate, + Message = $"Date is not the same as {secondDateName}" + }; + + private static dynamic IsSame( + DateTimeOffset firstDate, + DateTimeOffset secondDate, + string secondDateName) => new + { + Condition = firstDate == secondDate, + Message = $"Date is the same as {secondDateName}" + }; + + private dynamic IsNotRecent(DateTimeOffset date) => new + { + Condition = IsDateNotRecent(date), + Message = "Date is not recent" + }; + + private bool IsDateNotRecent(DateTimeOffset date) + { + DateTimeOffset currentDateTime = + this.dateTimeBroker.GetCurrentDateTimeOffset(); + + TimeSpan timeDifference = currentDateTime.Subtract(date); + TimeSpan oneMinute = TimeSpan.FromMinutes(1); + + return timeDifference.Duration() > oneMinute; + + } + + private static void Validate(params (dynamic Rule, string Parameter)[] validations) + { + var invalidGroupException = + new InvalidGroupException(); + + foreach ((dynamic rule, string parameter) in validations) + { + if (rule.Condition) + { + invalidGroupException.UpsertDataList( + key: parameter, + value: rule.Message); + } + } + + invalidGroupException.ThrowIfContainsErrors(); + } + } } \ No newline at end of file diff --git a/Taarafo.Core/Services/Foundations/Groups/GroupService.cs b/Taarafo.Core/Services/Foundations/Groups/GroupService.cs index 6c98f5d2..e6980c21 100644 --- a/Taarafo.Core/Services/Foundations/Groups/GroupService.cs +++ b/Taarafo.Core/Services/Foundations/Groups/GroupService.cs @@ -13,70 +13,70 @@ namespace Taarafo.Core.Services.Foundations.Groups { - public partial class GroupService : IGroupService - { - private readonly IStorageBroker storageBroker; - private readonly IDateTimeBroker dateTimeBroker; - private readonly ILoggingBroker loggingBroker; + public partial class GroupService : IGroupService + { + private readonly IStorageBroker storageBroker; + private readonly IDateTimeBroker dateTimeBroker; + private readonly ILoggingBroker loggingBroker; - public GroupService( - IStorageBroker storageBroker, - IDateTimeBroker dateTimeBroker, - ILoggingBroker loggingBroker) - { - this.storageBroker = storageBroker; - this.dateTimeBroker = dateTimeBroker; - this.loggingBroker = loggingBroker; - } + public GroupService( + IStorageBroker storageBroker, + IDateTimeBroker dateTimeBroker, + ILoggingBroker loggingBroker) + { + this.storageBroker = storageBroker; + this.dateTimeBroker = dateTimeBroker; + this.loggingBroker = loggingBroker; + } - public ValueTask AddGroupAsync(Group group) => - TryCatch(async () => - { - ValidateGroupOnAdd(group); + public ValueTask AddGroupAsync(Group group) => + TryCatch(async () => + { + ValidateGroupOnAdd(group); - return await this.storageBroker.InsertGroupAsync(group); - }); + return await this.storageBroker.InsertGroupAsync(group); + }); - public ValueTask RetrieveGroupByIdAsync(Guid groupId) => - TryCatch(async () => - { - ValidateGroupId(groupId); + public ValueTask RetrieveGroupByIdAsync(Guid groupId) => + TryCatch(async () => + { + ValidateGroupId(groupId); - Group maybeGroup = await this.storageBroker - .SelectGroupByIdAsync(groupId); + Group maybeGroup = await this.storageBroker + .SelectGroupByIdAsync(groupId); - ValidateStorageGroup(maybeGroup, groupId); + ValidateStorageGroup(maybeGroup, groupId); - return maybeGroup; - }); + return maybeGroup; + }); - public IQueryable RetrieveAllGroups() => - TryCatch(() => this.storageBroker.SelectAllGroups()); + public IQueryable RetrieveAllGroups() => + TryCatch(() => this.storageBroker.SelectAllGroups()); - public ValueTask ModifyGroupAsync(Group group) => - TryCatch(async () => - { - ValidateGroupOnModify(group); + public ValueTask ModifyGroupAsync(Group group) => + TryCatch(async () => + { + ValidateGroupOnModify(group); - var maybeGroup = - await this.storageBroker.SelectGroupByIdAsync(group.Id); + var maybeGroup = + await this.storageBroker.SelectGroupByIdAsync(group.Id); - ValidateStorageGroup(maybeGroup, group.Id); + ValidateStorageGroup(maybeGroup, group.Id); - return await this.storageBroker.UpdateGroupAsync(group); - }); + return await this.storageBroker.UpdateGroupAsync(group); + }); - public ValueTask RemoveGroupByIdAsync(Guid groupId) => - TryCatch(async () => - { - ValidateGroupId(groupId); + public ValueTask RemoveGroupByIdAsync(Guid groupId) => + TryCatch(async () => + { + ValidateGroupId(groupId); - Group someGroup = - await this.storageBroker.SelectGroupByIdAsync(groupId); + Group someGroup = + await this.storageBroker.SelectGroupByIdAsync(groupId); - ValidateStorageGroup(someGroup, groupId); + ValidateStorageGroup(someGroup, groupId); - return await this.storageBroker.DeleteGroupAsync(someGroup); - }); - } -} + return await this.storageBroker.DeleteGroupAsync(someGroup); + }); + } +} \ No newline at end of file