-
Notifications
You must be signed in to change notification settings - Fork 15
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 #203 from The-Standard-Organization/users/glhays/f…
…oundations-repository-retrievebyid FOUNDATIONS: Repository - `RetrieveById`
- Loading branch information
Showing
9 changed files
with
327 additions
and
9 deletions.
There are no files selected for viewing
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
114 changes: 114 additions & 0 deletions
114
....Unit/Services/Foundations/Repositories/RepositoryServiceTests.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,114 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers | ||
// ---------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using GitFyle.Core.Api.Models.Foundations.Repositories; | ||
using GitFyle.Core.Api.Models.Foundations.Repositories.Exceptions; | ||
using Moq; | ||
|
||
namespace GitFyle.Core.Api.Tests.Unit.Services.Foundations.Repositories | ||
{ | ||
public partial class RepositoryServiceTests | ||
{ | ||
[Fact] | ||
public async Task ShouldThrowCriticalDependencyExceptionOnRetrieveByIdIfSQLErrorOccursAndLogItAsync() | ||
{ | ||
// given | ||
Guid someGuid = Guid.NewGuid(); | ||
var sqlException = CreateSqlException(); | ||
|
||
var failedStorageRepositoryException = | ||
new FailedStorageRepositoryException( | ||
message: "Failed storage repository error occurred, contact support.", | ||
innerException: sqlException); | ||
|
||
var expectedRepositoryDependencyException = | ||
new RepositoryDependencyException( | ||
message: "Repository dependency error occurred, contact support.", | ||
innerException: failedStorageRepositoryException); | ||
|
||
this.storageBrokerMock.Setup(broker => | ||
broker.SelectRepositoryByIdAsync(It.IsAny<Guid>())) | ||
.ThrowsAsync(sqlException); | ||
|
||
// when | ||
ValueTask<Repository> retrieveRepositoryByIdTask = | ||
this.repositoryService.RetrieveRepositoryByIdAsync(someGuid); | ||
|
||
// then | ||
await Assert.ThrowsAsync<RepositoryDependencyException>( | ||
retrieveRepositoryByIdTask.AsTask); | ||
|
||
this.storageBrokerMock.Verify(broker => | ||
broker.SelectRepositoryByIdAsync(It.IsAny<Guid>()), | ||
Times.Once); | ||
|
||
this.loggingBrokerMock.Verify(broker => | ||
broker.LogCriticalAsync(It.Is(SameExceptionAs( | ||
expectedRepositoryDependencyException))), | ||
Times.Once); | ||
|
||
this.dateTimeBrokerMock.Verify(broker => | ||
broker.GetCurrentDateTimeOffsetAsync(), | ||
Times.Never); | ||
|
||
this.storageBrokerMock.VerifyNoOtherCalls(); | ||
this.loggingBrokerMock.VerifyNoOtherCalls(); | ||
this.dateTimeBrokerMock.VerifyNoOtherCalls(); | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldThrowServiceExceptionOnRetrieveByIdIfServiceErrorOccursAndLogItAsync() | ||
{ | ||
//given | ||
var someRepositoryId = Guid.NewGuid(); | ||
var serviceException = new Exception(); | ||
|
||
var failedServiceRepositoryException = | ||
new FailedServiceRepositoryException( | ||
message: "Failed service repository error occurred, contact support.", | ||
innerException: serviceException); | ||
|
||
var expectedRepositoryServiceException = | ||
new RepositoryServiceException( | ||
message: "Service error occurred, contact support.", | ||
innerException: failedServiceRepositoryException); | ||
|
||
this.storageBrokerMock.Setup(broker => | ||
broker.SelectRepositoryByIdAsync(It.IsAny<Guid>())) | ||
.ThrowsAsync(serviceException); | ||
|
||
//when | ||
ValueTask<Repository> retrieveRepositoryByIdTask = | ||
this.repositoryService.RetrieveRepositoryByIdAsync(someRepositoryId); | ||
|
||
RepositoryServiceException actualRepositoryServiceException = | ||
await Assert.ThrowsAsync<RepositoryServiceException>( | ||
retrieveRepositoryByIdTask.AsTask); | ||
|
||
//then | ||
actualRepositoryServiceException.Should().BeEquivalentTo( | ||
expectedRepositoryServiceException); | ||
|
||
this.storageBrokerMock.Verify(broker => | ||
broker.SelectRepositoryByIdAsync(It.IsAny<Guid>()), | ||
Times.Once); | ||
|
||
this.loggingBrokerMock.Verify(broker => | ||
broker.LogErrorAsync(It.Is(SameExceptionAs( | ||
expectedRepositoryServiceException))), | ||
Times.Once); | ||
|
||
this.dateTimeBrokerMock.Verify(broker => | ||
broker.GetCurrentDateTimeOffsetAsync(), | ||
Times.Never); | ||
|
||
this.storageBrokerMock.VerifyNoOtherCalls(); | ||
this.loggingBrokerMock.VerifyNoOtherCalls(); | ||
this.dateTimeBrokerMock.VerifyNoOtherCalls(); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...Tests.Unit/Services/Foundations/Repositories/RepositoryServiceTests.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,44 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers | ||
// ---------------------------------------------------------------------------------- | ||
|
||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Force.DeepCloner; | ||
using GitFyle.Core.Api.Models.Foundations.Repositories; | ||
using Moq; | ||
|
||
namespace GitFyle.Core.Api.Tests.Unit.Services.Foundations.Repositories | ||
{ | ||
public partial class RepositoryServiceTests | ||
{ | ||
[Fact] | ||
public async Task ShouldRetrieveRepositoryByIdAsync() | ||
{ | ||
// given | ||
Repository randomRepository = CreateRandomRepository(); | ||
Repository storageRepository = randomRepository; | ||
Repository expectedRepository = storageRepository.DeepClone(); | ||
|
||
this.storageBrokerMock.Setup(broker => | ||
broker.SelectRepositoryByIdAsync(randomRepository.Id)) | ||
.ReturnsAsync(storageRepository); | ||
|
||
// when | ||
Repository actualRepository = | ||
await this.repositoryService.RetrieveRepositoryByIdAsync( | ||
randomRepository.Id); | ||
|
||
// then | ||
actualRepository.Should().BeEquivalentTo(expectedRepository); | ||
|
||
this.storageBrokerMock.Verify(broker => | ||
broker.SelectRepositoryByIdAsync(randomRepository.Id), | ||
Times.Once); | ||
|
||
this.storageBrokerMock.VerifyNoOtherCalls(); | ||
this.loggingBrokerMock.VerifyNoOtherCalls(); | ||
this.dateTimeBrokerMock.VerifyNoOtherCalls(); | ||
} | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
...Unit/Services/Foundations/Repositories/RepositoryServiceTests.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,112 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers | ||
// ---------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using GitFyle.Core.Api.Models.Foundations.Repositories; | ||
using GitFyle.Core.Api.Models.Foundations.Repositories.Exceptions; | ||
using Moq; | ||
|
||
namespace GitFyle.Core.Api.Tests.Unit.Services.Foundations.Repositories | ||
{ | ||
public partial class RepositoryServiceTests | ||
{ | ||
[Fact] | ||
public async Task ShouldThrowValidationExceptionOnRetrieveByIdWhenRepositoryIdIsInvalidAndLogItAsync() | ||
{ | ||
// given | ||
var invalidRepositoryId = Guid.Empty; | ||
|
||
var invalidRepositoryException = | ||
new InvalidRepositoryException( | ||
message: "Repository is invalid, fix the errors and try again."); | ||
|
||
invalidRepositoryException.AddData( | ||
key: nameof(Repository.Id), | ||
values: "Id is invalid"); | ||
|
||
var expectedRepositoryValidationException = | ||
new RepositoryValidationException( | ||
message: "Repository validation error occurred, fix errors and try again.", | ||
innerException: invalidRepositoryException); | ||
|
||
// when | ||
ValueTask<Repository> retrieveRepositoryByIdTask = | ||
this.repositoryService.RetrieveRepositoryByIdAsync(invalidRepositoryId); | ||
|
||
RepositoryValidationException actualRepositoryValidationException = | ||
await Assert.ThrowsAsync<RepositoryValidationException>( | ||
retrieveRepositoryByIdTask.AsTask); | ||
|
||
// then | ||
actualRepositoryValidationException.Should().BeEquivalentTo( | ||
expectedRepositoryValidationException); | ||
|
||
this.loggingBrokerMock.Verify(broker => | ||
broker.LogErrorAsync(It.Is(SameExceptionAs( | ||
expectedRepositoryValidationException))), | ||
Times.Once); | ||
|
||
this.storageBrokerMock.Verify(broker => | ||
broker.SelectRepositoryByIdAsync(It.IsAny<Guid>()), | ||
Times.Never); | ||
|
||
this.dateTimeBrokerMock.Verify(broker => | ||
broker.GetCurrentDateTimeOffsetAsync(), | ||
Times.Never); | ||
|
||
this.loggingBrokerMock.VerifyNoOtherCalls(); | ||
this.storageBrokerMock.VerifyNoOtherCalls(); | ||
this.dateTimeBrokerMock.VerifyNoOtherCalls(); | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldThrowValidationExceptionOnRetrieveByIdIfRepositoryIdNotFoundAndLogitAsync() | ||
{ | ||
//given | ||
var someRepositoryId = Guid.NewGuid(); | ||
Repository nullRepository = null; | ||
var innerException = new Exception(); | ||
|
||
var notFoundRepositoryException = | ||
new NotFoundRepositoryException( | ||
message: $"Repository not found with id: {someRepositoryId}"); | ||
|
||
var expectedRepositoryValidationException = | ||
new RepositoryValidationException( | ||
message: "Repository validation error occurred, fix errors and try again.", | ||
innerException: notFoundRepositoryException); | ||
|
||
this.storageBrokerMock.Setup(broker => | ||
broker.SelectRepositoryByIdAsync(someRepositoryId)) | ||
.ReturnsAsync(nullRepository); | ||
|
||
// when | ||
ValueTask<Repository> retrieveRepositoryByIdTask = | ||
this.repositoryService.RetrieveRepositoryByIdAsync(someRepositoryId); | ||
|
||
RepositoryValidationException actualRepositoryValidationException = | ||
await Assert.ThrowsAsync<RepositoryValidationException>( | ||
retrieveRepositoryByIdTask.AsTask); | ||
|
||
// then | ||
actualRepositoryValidationException.Should().BeEquivalentTo( | ||
expectedRepositoryValidationException); | ||
|
||
this.storageBrokerMock.Verify(broker => | ||
broker.SelectRepositoryByIdAsync(someRepositoryId), | ||
Times.Once); | ||
|
||
this.loggingBrokerMock.Verify(broker => | ||
broker.LogErrorAsync(It.Is(SameExceptionAs( | ||
expectedRepositoryValidationException))), | ||
Times.Once); | ||
|
||
this.storageBrokerMock.VerifyNoOtherCalls(); | ||
this.loggingBrokerMock.VerifyNoOtherCalls(); | ||
this.dateTimeBrokerMock.VerifyNoOtherCalls(); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
GitFyle.Core.Api/Models/Foundations/Repositories/Exceptions/NotFoundRepositoryException.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,15 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers | ||
// ---------------------------------------------------------------------------------- | ||
|
||
using Xeptions; | ||
|
||
namespace GitFyle.Core.Api.Models.Foundations.Repositories.Exceptions | ||
{ | ||
public class NotFoundRepositoryException : Xeption | ||
{ | ||
public NotFoundRepositoryException(string message) | ||
: base(message) | ||
{ } | ||
} | ||
} |
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
Oops, something went wrong.