Skip to content

Commit

Permalink
Merge pull request #494 from hassanhabib/users/rcruzfreelance/foundat…
Browse files Browse the repository at this point in the history
…ions-event-add

FOUNDATIONS: Add Event
  • Loading branch information
glhays authored Aug 14, 2023
2 parents a572d31 + bc88505 commit e091330
Show file tree
Hide file tree
Showing 14 changed files with 1,188 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
// ---------------------------------------------------------------
// Copyright (c) Coalition of the Good-Hearted Engineers
// FREE TO USE TO CONNECT THE WORLD
// ---------------------------------------------------------------

using System;
using System.Threading.Tasks;
using EFxceptions.Models.Exceptions;
using FluentAssertions;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Moq;
using Taarafo.Core.Models.Events;
using Taarafo.Core.Models.Events.Exceptions;
using Xunit;

namespace Taarafo.Core.Tests.Unit.Services.Foundations.Events
{
public partial class EventServiceTests
{
[Fact]
public async Task ShouldThrowCriticalDependencyExceptionOnCreateIfSqlErrorOccursAndLogItAsync()
{
// given
DateTimeOffset randomDateTime = GetRandomDateTimeOffset();
Event someEvent = CreateRandomEvent(randomDateTime);
SqlException sqlException = GetSqlException();

var failedEventStorageException =
new FailedEventStorageException(sqlException);

var expectedEventDependencyException =
new EventDependencyException(failedEventStorageException);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTimeOffset())
.Throws(sqlException);

// when
ValueTask<Event> addEventTask =
this.eventService.AddEventAsync(someEvent);

EventDependencyException actualEventDependencyException =
await Assert.ThrowsAsync<EventDependencyException>(
addEventTask.AsTask);

// then
actualEventDependencyException.Should().BeEquivalentTo(
expectedEventDependencyException);

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

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

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

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

[Fact]
public async Task ShouldThrowDependencyValidationExceptionOnAddIfEventAlreadyExsitsAndLogItAsync()
{
// given
DateTimeOffset randomDateTimeOffset = GetRandomDateTimeOffset();
Event randomEvent = CreateRandomEvent(randomDateTimeOffset);
Event alreadyExistsEvent = randomEvent;
string randomMessage = GetRandomMessage();

var duplicateKeyException =
new DuplicateKeyException(randomMessage);

var alreadyExistsEventException =
new AlreadyExistsEventException(duplicateKeyException);

var expectedEventDependencyValidationException =
new EventDependencyValidationException(alreadyExistsEventException);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTimeOffset())
.Throws(duplicateKeyException);

// when
ValueTask<Event> addEventTask =
this.eventService.AddEventAsync(alreadyExistsEvent);

EventDependencyValidationException actualEventDependencyValidationException =
await Assert.ThrowsAsync<EventDependencyValidationException>(
addEventTask.AsTask);

// then
actualEventDependencyValidationException.Should().BeEquivalentTo(
expectedEventDependencyValidationException);

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

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

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

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

[Fact]
public async void ShouldThrowDependencyValidationExceptionOnAddIfReferenceErrorOccursAndLogItAsync()
{
// given
Event someEvent = CreateRandomEvent();
string randomMessage = GetRandomMessage();
string exceptionMessage = randomMessage;

var foreignKeyConstraintConflictException =
new ForeignKeyConstraintConflictException(exceptionMessage);

var invalidEventReferenceException =
new InvalidEventReferenceException(foreignKeyConstraintConflictException);

var expectedEventDependencyValidationException =
new EventDependencyValidationException(invalidEventReferenceException);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTimeOffset())
.Throws(foreignKeyConstraintConflictException);

// when
ValueTask<Event> addEventTask =
this.eventService.AddEventAsync(someEvent);

EventDependencyValidationException actualEventDependencyValidationException =
await Assert.ThrowsAsync<EventDependencyValidationException>(
addEventTask.AsTask);

// then
actualEventDependencyValidationException.Should().BeEquivalentTo(
expectedEventDependencyValidationException);

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

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

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

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

[Fact]
public async Task ShouldThrowDependencyExceptionOnAddIfDatabaseUpdateErrorOccursAndLogItAsync()
{
// given
Event someEvent = CreateRandomEvent();

var databaseUpdateException =
new DbUpdateException();

var failedEventStorageException =
new FailedEventStorageException(databaseUpdateException);

var expectedEventDependencyException =
new EventDependencyException(failedEventStorageException);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTimeOffset())
.Throws(databaseUpdateException);

// when
ValueTask<Event> addEventTask =
this.eventService.AddEventAsync(someEvent);

EventDependencyException actualEventDependencyException =
await Assert.ThrowsAsync<EventDependencyException>(
addEventTask.AsTask);

// then
actualEventDependencyException.Should().BeEquivalentTo(
expectedEventDependencyException);

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

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

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

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

[Fact]
public async Task ShouldThrowServiceExceptionOnAddIfServiceErrorOccursAndLogItAsync()
{
// given
Event someEvent = CreateRandomEvent();
var serviceException = new Exception();

var failedEventServiceException =
new FailedEventServiceException(serviceException);

var expectedEventServiceException =
new EventServiceException(failedEventServiceException);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTimeOffset())
.Throws(serviceException);

// when
ValueTask<Event> addEventTask =
this.eventService.AddEventAsync(someEvent);

EventServiceException actualEventServiceException =
await Assert.ThrowsAsync<EventServiceException>(
addEventTask.AsTask);

// then
actualEventServiceException.Should().BeEquivalentTo(
expectedEventServiceException);

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

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

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

this.dateTimeBrokerMock.VerifyNoOtherCalls();
this.storageBrokerMock.VerifyNoOtherCalls();
this.loggingBrokerMock.VerifyNoOtherCalls();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// ---------------------------------------------------------------
// Copyright (c) Coalition of the Good-Hearted Engineers
// FREE TO USE TO CONNECT THE WORLD
// ---------------------------------------------------------------

using System;
using System.Threading.Tasks;
using FluentAssertions;
using Force.DeepCloner;
using Moq;
using Taarafo.Core.Models.Events;
using Xunit;

namespace Taarafo.Core.Tests.Unit.Services.Foundations.Events
{
public partial class EventServiceTests
{
[Fact]
public async Task ShouldAddEventAsync()
{
// given
DateTimeOffset randomDateTime = GetRandomDateTimeOffset();
Event randomEvent = CreateRandomEvent(randomDateTime);
Event inputEvent = randomEvent;
Event insertedEvent = inputEvent;
Event expectedEvent = insertedEvent.DeepClone();

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTimeOffset())
.Returns(randomDateTime);

this.storageBrokerMock.Setup(broker =>
broker.InsertEventAsync(inputEvent))
.ReturnsAsync(insertedEvent);

// when
Event actualEvent =
await this.eventService.AddEventAsync(inputEvent);

// then
actualEvent.Should().BeEquivalentTo(expectedEvent);

this.storageBrokerMock.Verify(broker =>
broker.InsertEventAsync(inputEvent),
Times.Once());

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

0 comments on commit e091330

Please sign in to comment.