Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FOUNDATIONS: Contributor-AddAsync #226

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
34a3a31
ShouldAddContributorAsync -> FAIL
SlimAhmad Oct 15, 2024
21e8f64
ShouldAddContributorAsync -> PASS
SlimAhmad Oct 15, 2024
e4af4bc
ShouldThrowValidationExceptionOnAddIfContributorIsNullAndLogItAsync -…
SlimAhmad Oct 15, 2024
318e758
ShouldThrowValidationExceptionOnAddIfContributorIsNullAndLogItAsync -…
SlimAhmad Oct 15, 2024
41e9090
ShouldThrowValidationExceptionOnAddIfContributorIsInvalidAndLogItAsyn…
SlimAhmad Oct 15, 2024
3d12b43
ShouldThrowValidationExceptionOnAddIfContributorIsInvalidAndLogItAsyn…
SlimAhmad Oct 15, 2024
7f997ad
ShouldThrowValidationExceptionOnAddIfContributorHasInvalidLengthPrope…
SlimAhmad Oct 15, 2024
d724239
ShouldThrowValidationExceptionOnAddIfContributorHasInvalidLengthPrope…
SlimAhmad Oct 15, 2024
5968422
CODERUB: Added extra fields in test
SlimAhmad Oct 15, 2024
866f92e
ShouldThrowValidationExceptionOnAddIfAuditPropertiesIsNotTheSameAndLo…
SlimAhmad Oct 15, 2024
d682d10
ShouldThrowValidationExceptionOnAddIfAuditPropertiesIsNotTheSameAndLo…
SlimAhmad Oct 15, 2024
2a5d0bb
ShouldThrowValidationExceptionOnAddIfCreatedDateIsNotRecentAndLogItAs…
SlimAhmad Oct 15, 2024
6961b89
ShouldThrowValidationExceptionOnAddIfCreatedDateIsNotRecentAndLogItAs…
SlimAhmad Oct 15, 2024
945b506
ShouldThrowCriticalDependencyExceptionOnAddIfSqlErrorOccurredAndLogIt…
SlimAhmad Oct 15, 2024
cd59805
ShouldThrowCriticalDependencyExceptionOnAddIfSqlErrorOccurredAndLogIt…
SlimAhmad Oct 15, 2024
ddad91f
Revert "ShouldThrowCriticalDependencyExceptionOnAddIfSqlErrorOccurred…
SlimAhmad Oct 15, 2024
d1d2390
Reapply "ShouldThrowCriticalDependencyExceptionOnAddIfSqlErrorOccurre…
SlimAhmad Oct 15, 2024
f5cbbcf
ShouldThrowDependencyValidationExceptionOnAddIfContributorAlreadyExis…
SlimAhmad Oct 15, 2024
bf2c44e
ShouldThrowDependencyExceptionOnAddIfDependencyErrorOccurredAndLogItA…
SlimAhmad Oct 15, 2024
0d5cdef
ShouldThrowDependencyExceptionOnAddIfDependencyErrorOccurredAndLogItA…
SlimAhmad Oct 15, 2024
503f876
ShouldThrowServiceExceptionOnAddIfServiceErrorOccurredAndLogItAsync -…
SlimAhmad Oct 15, 2024
1067246
ShouldThrowServiceExceptionOnAddIfServiceErrorOccurredAndLogItAsync -…
SlimAhmad Oct 15, 2024
e27bab7
CODERUB: Added contributorService to program.cs and minor fix
SlimAhmad Oct 15, 2024
9955396
CODERUB: Added line break
SlimAhmad Oct 15, 2024
c637e69
CODERUB: Merged conflicts
SlimAhmad Oct 21, 2024
debc493
Merge branch 'main' into users/slimahmad/foundations/contributor-AddA…
cjdutoit Nov 10, 2024
17c3290
CODERUB: Added new line
SlimAhmad Nov 11, 2024
a9c86eb
Merge branch 'users/slimahmad/foundations/contributor-AddAsync' of ht…
SlimAhmad Nov 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System;
using System.Threading.Tasks;
using EFxceptions.Models.Exceptions;
using FluentAssertions;
using GitFyle.Core.Api.Models.Foundations.Contributors;
using GitFyle.Core.Api.Models.Foundations.Contributors.Exceptions;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Moq;

namespace GitFyle.Core.Api.Tests.Unit.Services.Foundations.Contributors
{
public partial class ContributorServiceTests
{
[Fact]
public async Task ShouldThrowCriticalDependencyExceptionOnAddIfSqlErrorOccurredAndLogItAsync()
{
// given
Contributor someContributor = CreateRandomContributor();
SqlException sqlException = CreateSqlException();

var failedStorageContributorException =
new FailedStorageContributorException(
message: "Failed storage contributor error occurred, contact support.",
innerException: sqlException);

var expectedContributorDependencyException =
new ContributorDependencyException(
message: "Contributor dependency error occurred, contact support.",
innerException: failedStorageContributorException);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTimeOffsetAsync())
.ThrowsAsync(sqlException);

// when
ValueTask<Contributor> addContributorTask =
this.contributorService.AddContributorAsync(
someContributor);

ContributorDependencyException actualContributorDependencyException =
await Assert.ThrowsAsync<ContributorDependencyException>(
testCode: addContributorTask.AsTask);

// then
actualContributorDependencyException.Should().BeEquivalentTo(
expectedContributorDependencyException);

this.dateTimeBrokerMock.Verify(broker =>
broker.GetCurrentDateTimeOffsetAsync(),
Times.Once);

this.loggingBrokerMock.Verify(broker =>
broker.LogCriticalAsync(It.Is(SameExceptionAs(
expectedContributorDependencyException))),
Times.Once);

this.storageBrokerMock.Verify(broker =>
broker.InsertContributorAsync(It.IsAny<Contributor>()),
Times.Never);

this.dateTimeBrokerMock.VerifyNoOtherCalls();
this.loggingBrokerMock.VerifyNoOtherCalls();
this.storageBrokerMock.VerifyNoOtherCalls();
}

[Fact]
public async Task ShouldThrowDependencyValidationExceptionOnAddIfContributorAlreadyExistsAndLogItAsync()
{
// given
Contributor someContributor = CreateRandomContributor();

var duplicateKeyException =
new DuplicateKeyException(
message: "Duplicate key error occurred");

var alreadyExistsContributorException =
new AlreadyExistsContributorException(
message: "Contributor already exists error occurred.",
innerException: duplicateKeyException,
data: duplicateKeyException.Data);

var expectedContributorDependencyValidationException =
new ContributorDependencyValidationException(
message: "Contributor dependency validation error occurred, fix errors and try again.",
innerException: alreadyExistsContributorException,
data: alreadyExistsContributorException.Data);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTimeOffsetAsync())
.ThrowsAsync(duplicateKeyException);

// when
ValueTask<Contributor> addContributorTask =
this.contributorService.AddContributorAsync(
someContributor);

ContributorDependencyValidationException actualContributorDependencyValidationException =
await Assert.ThrowsAsync<ContributorDependencyValidationException>(
testCode: addContributorTask.AsTask);

// then
actualContributorDependencyValidationException.Should().BeEquivalentTo(
expectedContributorDependencyValidationException);

this.dateTimeBrokerMock.Verify(broker =>
broker.GetCurrentDateTimeOffsetAsync(),
Times.Once);

this.loggingBrokerMock.Verify(broker =>
broker.LogErrorAsync(It.Is(SameExceptionAs(
expectedContributorDependencyValidationException))),
Times.Once);

this.storageBrokerMock.Verify(broker =>
broker.InsertContributorAsync(It.IsAny<Contributor>()),
Times.Never);

this.dateTimeBrokerMock.VerifyNoOtherCalls();
this.loggingBrokerMock.VerifyNoOtherCalls();
this.storageBrokerMock.VerifyNoOtherCalls();
}

[Fact]
public async Task ShouldThrowDependencyExceptionOnAddIfDependencyErrorOccurredAndLogItAsync()
{
// given
Contributor someContributor = CreateRandomContributor();
var dbUpdateException = new DbUpdateException();

var failedOperationContributorException =
new FailedOperationContributorException(
message: "Failed operation contributor error occurred, contact support.",
innerException: dbUpdateException);

var expectedContributorDependencyException =
new ContributorDependencyException(
message: "Contributor dependency error occurred, contact support.",
innerException: failedOperationContributorException);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTimeOffsetAsync())
.ThrowsAsync(dbUpdateException);

// when
ValueTask<Contributor> addContributorTask =
this.contributorService.AddContributorAsync(
someContributor);

ContributorDependencyException actualContributorDependencyException =
await Assert.ThrowsAsync<ContributorDependencyException>(
testCode: addContributorTask.AsTask);

// then
actualContributorDependencyException.Should().BeEquivalentTo(
expectedContributorDependencyException);

this.dateTimeBrokerMock.Verify(broker =>
broker.GetCurrentDateTimeOffsetAsync(),
Times.Once);

this.loggingBrokerMock.Verify(broker =>
broker.LogErrorAsync(It.Is(SameExceptionAs(
expectedContributorDependencyException))),
Times.Once);

this.storageBrokerMock.Verify(broker =>
broker.InsertContributorAsync(It.IsAny<Contributor>()),
Times.Never);

this.dateTimeBrokerMock.VerifyNoOtherCalls();
this.loggingBrokerMock.VerifyNoOtherCalls();
this.storageBrokerMock.VerifyNoOtherCalls();
}

[Fact]
public async Task ShouldThrowServiceExceptionOnAddIfServiceErrorOccurredAndLogItAsync()
{
// given
Contributor randomContributor = CreateRandomContributor();
var serviceException = new Exception();

var failedServiceContributorException =
new FailedServiceContributorException(
message: "Failed service contributor error occurred, contact support.",
innerException: serviceException);

var expectedContributorServiceException =
new ContributorServiceException(
message: "Service error occurred, contact support.",
innerException: failedServiceContributorException);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTimeOffsetAsync())
.ThrowsAsync(serviceException);

// when
ValueTask<Contributor> addContributorTask =
this.contributorService.AddContributorAsync(
randomContributor);

ContributorServiceException actualContributorServiceException =
await Assert.ThrowsAsync<ContributorServiceException>(
testCode: addContributorTask.AsTask);

// then
actualContributorServiceException.Should().BeEquivalentTo(
expectedContributorServiceException);

this.dateTimeBrokerMock.Verify(broker =>
broker.GetCurrentDateTimeOffsetAsync(),
Times.Once);

this.loggingBrokerMock.Verify(broker =>
broker.LogErrorAsync(It.Is(SameExceptionAs(
expectedContributorServiceException))),
Times.Once);

this.storageBrokerMock.Verify(broker =>
broker.InsertContributorAsync(It.IsAny<Contributor>()),
Times.Never);

this.dateTimeBrokerMock.VerifyNoOtherCalls();
this.loggingBrokerMock.VerifyNoOtherCalls();
this.storageBrokerMock.VerifyNoOtherCalls();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System;
using System.Threading.Tasks;
using FluentAssertions;
using Force.DeepCloner;
using GitFyle.Core.Api.Models.Foundations.Contributors;
using Moq;

namespace GitFyle.Core.Api.Tests.Unit.Services.Foundations.Contributors
{
public partial class ContributorServiceTests
{
[Fact]
public async Task ShouldAddContributorAsync()
{
// given
DateTimeOffset randomDateTime = GetRandomDateTimeOffset();
DateTimeOffset now = randomDateTime;
Contributor randomContributor = CreateRandomContributor(dateTimeOffset: now);
Contributor inputContributor = randomContributor;
Contributor insertedContributor = inputContributor.DeepClone();
Contributor expectedContributor = insertedContributor.DeepClone();

this.storageBrokerMock.Setup(broker =>
broker.InsertContributorAsync(inputContributor))
.ReturnsAsync(insertedContributor);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTimeOffsetAsync())
.ReturnsAsync(now);

// when
Contributor actualContributor =
await this.contributorService.AddContributorAsync(inputContributor);

// then
actualContributor.Should().BeEquivalentTo(expectedContributor);

this.dateTimeBrokerMock.Verify(broker =>
broker.GetCurrentDateTimeOffsetAsync(),
Times.Once);

this.storageBrokerMock.Verify(broker =>
broker.InsertContributorAsync(inputContributor),
Times.Once);

this.storageBrokerMock.VerifyNoOtherCalls();
this.loggingBrokerMock.VerifyNoOtherCalls();
this.dateTimeBrokerMock.VerifyNoOtherCalls();
}
}
}
Loading
Loading