-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #488 from GulchexrasGithub/users/GulchexrasGithub/…
…foundations/groupmembership-retrieve-byid FOUNDATIONS: Retrieve GroupMembership By Id
- Loading branch information
Showing
8 changed files
with
300 additions
and
0 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
...vices/Foundations/GroupMemberships/GroupMembershipServiceTests.Exceptions.RetrieveById.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// --------------------------------------------------------------- | ||
// Copyright (c) Coalition of the Good-Hearted Engineers | ||
// FREE TO USE TO CONNECT THE WORLD | ||
// --------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Microsoft.Data.SqlClient; | ||
using Moq; | ||
using Taarafo.Core.Models.GroupMemberships; | ||
using Taarafo.Core.Models.GroupMemberships.Exceptions; | ||
using Xunit; | ||
|
||
namespace Taarafo.Core.Tests.Unit.Services.Foundations.GroupMemberships | ||
{ | ||
public partial class GroupMembershipServiceTests | ||
{ | ||
[Fact] | ||
public async Task ShouldThrowCriticalDependencyExceptionOnRetrieveByIdIfSqlErrorOccursAndLogItAsync() | ||
{ | ||
// given | ||
Guid someId = Guid.NewGuid(); | ||
SqlException sqlException = GetSqlException(); | ||
|
||
var failedGroupMembershipStorageException = | ||
new FailedGroupMembershipStorageException(sqlException); | ||
|
||
var expectedGroupMembershipDependencyException = | ||
new GroupMembershipDependencyException(failedGroupMembershipStorageException); | ||
|
||
this.storageBrokerMock.Setup(broker => | ||
broker.SelectGroupMembershipByIdAsync(It.IsAny<Guid>())) | ||
.ThrowsAsync(sqlException); | ||
|
||
// when | ||
ValueTask<GroupMembership> retrieveGroupMembershipByIdTask = | ||
this.groupMembershipService.RetrieveGroupMembershipByIdAsync(someId); | ||
|
||
GroupMembershipDependencyException actualGroupMembershipDependencyException = | ||
await Assert.ThrowsAsync<GroupMembershipDependencyException>( | ||
retrieveGroupMembershipByIdTask.AsTask); | ||
|
||
// then | ||
actualGroupMembershipDependencyException.Should().BeEquivalentTo( | ||
expectedGroupMembershipDependencyException); | ||
|
||
this.storageBrokerMock.Verify(broker => | ||
broker.SelectGroupMembershipByIdAsync(It.IsAny<Guid>()), | ||
Times.Once); | ||
|
||
this.loggingBrokerMock.Verify(broker => | ||
broker.LogCritical(It.Is(SameExceptionAs( | ||
expectedGroupMembershipDependencyException))), | ||
Times.Once); | ||
|
||
this.storageBrokerMock.VerifyNoOtherCalls(); | ||
this.loggingBrokerMock.VerifyNoOtherCalls(); | ||
this.dateTimeBrokerMock.VerifyNoOtherCalls(); | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldThrowServiceExceptionOnRetrieveByIdIfServiceErrorOccursAndLogItAsync() | ||
{ | ||
// given | ||
Guid someId = Guid.NewGuid(); | ||
var serviceException = new Exception(); | ||
|
||
var failedGroupMembershipServiceException = | ||
new FailedGroupMembershipServiceException(serviceException); | ||
|
||
var expectedGroupMembershipServiceException = | ||
new GroupMembershipServiceException(failedGroupMembershipServiceException); | ||
|
||
this.storageBrokerMock.Setup(broker => | ||
broker.SelectGroupMembershipByIdAsync(It.IsAny<Guid>())) | ||
.ThrowsAsync(serviceException); | ||
|
||
// when | ||
ValueTask<GroupMembership> retrieveGroupMembershipByIdTask = | ||
this.groupMembershipService.RetrieveGroupMembershipByIdAsync(someId); | ||
|
||
GroupMembershipServiceException actualGroupMembershipServiceException = | ||
await Assert.ThrowsAsync<GroupMembershipServiceException>( | ||
retrieveGroupMembershipByIdTask.AsTask); | ||
|
||
// then | ||
actualGroupMembershipServiceException.Should().BeEquivalentTo( | ||
expectedGroupMembershipServiceException); | ||
|
||
this.storageBrokerMock.Verify(broker => | ||
broker.SelectGroupMembershipByIdAsync(It.IsAny<Guid>()), | ||
Times.Once); | ||
|
||
this.loggingBrokerMock.Verify(broker => | ||
broker.LogError(It.Is(SameExceptionAs( | ||
expectedGroupMembershipServiceException))), | ||
Times.Once); | ||
|
||
this.storageBrokerMock.VerifyNoOtherCalls(); | ||
this.loggingBrokerMock.VerifyNoOtherCalls(); | ||
this.dateTimeBrokerMock.VerifyNoOtherCalls(); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...t/Services/Foundations/GroupMemberships/GroupMembershipServiceTests.Logic.RetrieveById.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// --------------------------------------------------------------- | ||
// Copyright (c) Coalition of the Good-Hearted Engineers | ||
// FREE TO USE TO CONNECT THE WORLD | ||
// --------------------------------------------------------------- | ||
|
||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Force.DeepCloner; | ||
using Moq; | ||
using Taarafo.Core.Models.GroupMemberships; | ||
using Xunit; | ||
|
||
namespace Taarafo.Core.Tests.Unit.Services.Foundations.GroupMemberships | ||
{ | ||
public partial class GroupMembershipServiceTests | ||
{ | ||
[Fact] | ||
public async Task ShouldRetrieveGroupMembershipByIdAsync() | ||
{ | ||
// given | ||
GroupMembership randomGroupMembership = CreateRandomGroupMembership(); | ||
GroupMembership storageGroupMembership = randomGroupMembership; | ||
GroupMembership expectedGroupMembership = storageGroupMembership.DeepClone(); | ||
|
||
this.storageBrokerMock.Setup(broker => | ||
broker.SelectGroupMembershipByIdAsync(randomGroupMembership.Id)) | ||
.ReturnsAsync(storageGroupMembership); | ||
|
||
// when | ||
GroupMembership actualGroupMembership = | ||
await this.groupMembershipService.RetrieveGroupMembershipByIdAsync(randomGroupMembership.Id); | ||
|
||
// then | ||
actualGroupMembership.Should().BeEquivalentTo(expectedGroupMembership); | ||
|
||
this.storageBrokerMock.Verify(broker => | ||
broker.SelectGroupMembershipByIdAsync(randomGroupMembership.Id), | ||
Times.Once); | ||
|
||
this.storageBrokerMock.VerifyNoOtherCalls(); | ||
this.dateTimeBrokerMock.VerifyNoOtherCalls(); | ||
this.loggingBrokerMock.VerifyNoOtherCalls(); | ||
} | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
...ices/Foundations/GroupMemberships/GroupMembershipServiceTests.Validations.RetrieveById.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// --------------------------------------------------------------- | ||
// Copyright (c) Coalition of the Good-Hearted Engineers | ||
// FREE TO USE TO CONNECT THE WORLD | ||
// --------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Moq; | ||
using Taarafo.Core.Models.GroupMemberships; | ||
using Taarafo.Core.Models.GroupMemberships.Exceptions; | ||
using Xunit; | ||
|
||
namespace Taarafo.Core.Tests.Unit.Services.Foundations.GroupMemberships | ||
{ | ||
public partial class GroupMembershipServiceTests | ||
{ | ||
[Fact] | ||
public async Task ShouldThrowValidationExceptionOnRetrieveByIdIfIdIsInvalidAndLogItAsync() | ||
{ | ||
// given | ||
var invalidGroupMembershipId = Guid.Empty; | ||
|
||
var invalidGroupMembershipException = | ||
new InvalidGroupMembershipException(); | ||
invalidGroupMembershipException.AddData( | ||
key: nameof(GroupMembership.Id), | ||
values: "Id is required"); | ||
|
||
var expectedGroupMembershipValidationException = | ||
new GroupMembershipValidationException(invalidGroupMembershipException); | ||
|
||
// when | ||
ValueTask<GroupMembership> retrieveGroupMembershipByIdTask = | ||
this.groupMembershipService.RetrieveGroupMembershipByIdAsync(invalidGroupMembershipId); | ||
|
||
GroupMembershipValidationException actualGroupMembershipValidationException = | ||
await Assert.ThrowsAsync<GroupMembershipValidationException>( | ||
retrieveGroupMembershipByIdTask.AsTask); | ||
|
||
// then | ||
actualGroupMembershipValidationException.Should().BeEquivalentTo( | ||
expectedGroupMembershipValidationException); | ||
|
||
this.loggingBrokerMock.Verify(broker => | ||
broker.LogError(It.Is(SameExceptionAs( | ||
expectedGroupMembershipValidationException))), | ||
Times.Once); | ||
|
||
this.storageBrokerMock.Verify(broker => | ||
broker.SelectGroupMembershipByIdAsync(It.IsAny<Guid>()), | ||
Times.Never); | ||
|
||
this.loggingBrokerMock.VerifyNoOtherCalls(); | ||
this.storageBrokerMock.VerifyNoOtherCalls(); | ||
this.dateTimeBrokerMock.VerifyNoOtherCalls(); | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldThrowNotFoundExceptionOnRetrieveByIdIfGroupMembershipIsNotFoundAndLogItAsync() | ||
{ | ||
//given | ||
Guid someGroupMembershipId = Guid.NewGuid(); | ||
GroupMembership noGroupMembership = null; | ||
|
||
var notFoundGroupMembershipException = | ||
new NotFoundGroupMembershipException(someGroupMembershipId); | ||
|
||
var expectedGroupMembershipValidationException = | ||
new GroupMembershipValidationException(notFoundGroupMembershipException); | ||
|
||
this.storageBrokerMock.Setup(broker => | ||
broker.SelectGroupMembershipByIdAsync(It.IsAny<Guid>())) | ||
.ReturnsAsync(noGroupMembership); | ||
|
||
//when | ||
ValueTask<GroupMembership> retrieveGroupMembershipByIdTask = | ||
this.groupMembershipService.RetrieveGroupMembershipByIdAsync(someGroupMembershipId); | ||
|
||
GroupMembershipValidationException actualGroupMembershipValidationException = | ||
await Assert.ThrowsAsync<GroupMembershipValidationException>( | ||
retrieveGroupMembershipByIdTask.AsTask); | ||
|
||
// then | ||
actualGroupMembershipValidationException.Should().BeEquivalentTo( | ||
expectedGroupMembershipValidationException); | ||
|
||
this.storageBrokerMock.Verify(broker => | ||
broker.SelectGroupMembershipByIdAsync(It.IsAny<Guid>()), | ||
Times.Once()); | ||
|
||
this.loggingBrokerMock.Verify(broker => | ||
broker.LogError(It.Is(SameExceptionAs( | ||
expectedGroupMembershipValidationException))), | ||
Times.Once); | ||
|
||
this.storageBrokerMock.VerifyNoOtherCalls(); | ||
this.loggingBrokerMock.VerifyNoOtherCalls(); | ||
this.dateTimeBrokerMock.VerifyNoOtherCalls(); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
Taarafo.Core/Models/GroupMemberships/Exceptions/NotFoundGroupMembershipException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// --------------------------------------------------------------- | ||
// Copyright (c) Coalition of the Good-Hearted Engineers | ||
// FREE TO USE TO CONNECT THE WORLD | ||
// --------------------------------------------------------------- | ||
|
||
using System; | ||
using Xeptions; | ||
|
||
namespace Taarafo.Core.Models.GroupMemberships.Exceptions | ||
{ | ||
public class NotFoundGroupMembershipException : Xeption | ||
{ | ||
public NotFoundGroupMembershipException(Guid groupMembershipId) | ||
: base(message: $"Couldn't find GroupMembership with id: {groupMembershipId}.") | ||
{ } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters