Skip to content

Commit

Permalink
CODE RUB: Events Exceptions Upgrade v2.10.0
Browse files Browse the repository at this point in the history
Closes: #506
  • Loading branch information
glhays committed Sep 7, 2023
1 parent 7302783 commit 3847859
Show file tree
Hide file tree
Showing 24 changed files with 187 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,24 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Events
public partial class EventServiceTests
{
[Fact]
public async Task ShouldThrowCriticalDependencyExceptionOnCreateIfSqlErrorOccursAndLogItAsync()
private async Task ShouldThrowCriticalDependencyExceptionOnCreateIfSqlErrorOccursAndLogItAsync()
{
// given
DateTimeOffset randomDateTime = GetRandomDateTimeOffset();
Event someEvent = CreateRandomEvent(randomDateTime);
SqlException sqlException = GetSqlException();

SqlException sqlException =
GetSqlException();

var failedEventStorageException =
new FailedEventStorageException(sqlException);
new FailedEventStorageException(
message: "Failed event storage error occured, contact support.",
innerException: sqlException);

var expectedEventDependencyException =
new EventDependencyException(failedEventStorageException);
new EventDependencyException(
message: "Event dependency validation occurred, please try again.",
innerException: failedEventStorageException);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTimeOffset())
Expand Down Expand Up @@ -67,7 +73,7 @@ await Assert.ThrowsAsync<EventDependencyException>(
}

[Fact]
public async Task ShouldThrowDependencyValidationExceptionOnAddIfEventAlreadyExsitsAndLogItAsync()
private async Task ShouldThrowDependencyValidationExceptionOnAddIfEventAlreadyExsitsAndLogItAsync()
{
// given
DateTimeOffset randomDateTimeOffset = GetRandomDateTimeOffset();
Expand All @@ -79,10 +85,14 @@ public async Task ShouldThrowDependencyValidationExceptionOnAddIfEventAlreadyExs
new DuplicateKeyException(randomMessage);

var alreadyExistsEventException =
new AlreadyExistsEventException(duplicateKeyException);
new AlreadyExistsEventException(
message: "Event with the same id already exists.",
innerException: duplicateKeyException);

var expectedEventDependencyValidationException =
new EventDependencyValidationException(alreadyExistsEventException);
new EventDependencyValidationException(
message: "Event dependency validation occurred, please try again.",
innerException: alreadyExistsEventException);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTimeOffset())
Expand Down Expand Up @@ -119,7 +129,7 @@ await Assert.ThrowsAsync<EventDependencyValidationException>(
}

[Fact]
public async void ShouldThrowDependencyValidationExceptionOnAddIfReferenceErrorOccursAndLogItAsync()
private async void ShouldThrowDependencyValidationExceptionOnAddIfReferenceErrorOccursAndLogItAsync()
{
// given
Event someEvent = CreateRandomEvent();
Expand All @@ -130,10 +140,14 @@ public async void ShouldThrowDependencyValidationExceptionOnAddIfReferenceErrorO
new ForeignKeyConstraintConflictException(exceptionMessage);

var invalidEventReferenceException =
new InvalidEventReferenceException(foreignKeyConstraintConflictException);
new InvalidEventReferenceException(
message: "Invalid eventt reference error occurred.",
innerException: foreignKeyConstraintConflictException);

var expectedEventDependencyValidationException =
new EventDependencyValidationException(invalidEventReferenceException);
new EventDependencyValidationException(
message: "Event dependency validation occurred, please try again.",
innerException: invalidEventReferenceException);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTimeOffset())
Expand Down Expand Up @@ -170,7 +184,7 @@ await Assert.ThrowsAsync<EventDependencyValidationException>(
}

[Fact]
public async Task ShouldThrowDependencyExceptionOnAddIfDatabaseUpdateErrorOccursAndLogItAsync()
private async Task ShouldThrowDependencyExceptionOnAddIfDatabaseUpdateErrorOccursAndLogItAsync()
{
// given
Event someEvent = CreateRandomEvent();
Expand All @@ -179,10 +193,14 @@ public async Task ShouldThrowDependencyExceptionOnAddIfDatabaseUpdateErrorOccurs
new DbUpdateException();

var failedEventStorageException =
new FailedEventStorageException(databaseUpdateException);
new FailedEventStorageException(
message: "Failed event storage error occured, contact support.",
innerException: databaseUpdateException);

var expectedEventDependencyException =
new EventDependencyException(failedEventStorageException);
new EventDependencyException(
message: "Event dependency validation occurred, please try again.",
innerException: failedEventStorageException);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTimeOffset())
Expand Down Expand Up @@ -219,17 +237,21 @@ await Assert.ThrowsAsync<EventDependencyException>(
}

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

var failedEventServiceException =
new FailedEventServiceException(serviceException);
new FailedEventServiceException(
message: "Failed event service occurred, please contact support.",
innerException: serviceException);

var expectedEventServiceException =
new EventServiceException(failedEventServiceException);
new EventServiceException(
message: "Event service error occurred, contact support.",
innerException: failedEventServiceException);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTimeOffset())
Expand Down Expand Up @@ -265,4 +287,4 @@ await Assert.ThrowsAsync<EventServiceException>(
this.loggingBrokerMock.VerifyNoOtherCalls();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Events
public partial class EventServiceTests
{
[Fact]
public void ShouldThrowCriticalDependencyExceptionOnRetrieveAllWhenSqlExceptionOccursAndLogIt()
private void ShouldThrowCriticalDependencyExceptionOnRetrieveAllWhenSqlExceptionOccursAndLogIt()
{
// given
SqlException sqlException = CreateSqlException();

var failedEventStorageException =
new FailedEventStorageException(sqlException);

var expectedEventDependencyException =
new EventDependencyException(failedEventStorageException);
new EventDependencyException(
message: "Event dependency validation occurred, please try again.",
innerException: failedEventStorageException);

this.storageBrokerMock.Setup(broker =>
broker.SelectAllEvents()).Throws(sqlException);
Expand All @@ -36,30 +39,37 @@ public void ShouldThrowCriticalDependencyExceptionOnRetrieveAllWhenSqlExceptionO
Assert.Throws<EventDependencyException>(retrieveAllEventAction);

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

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

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

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

[Fact]
public void ShouldThrowServiceExceptionOnRetrieveAllIfServiceErrorOccursAndLogItAsync()
private void ShouldThrowServiceExceptionOnRetrieveAllIfServiceErrorOccursAndLogItAsync()
{
// given
string exceptionMessage = GetRandomMessage();
var serviceException = new Exception(exceptionMessage);

var serviceException =
new Exception(exceptionMessage);

var failedEventServiceException = new FailedEventServiceException(serviceException);
var failedEventServiceException =
new FailedEventServiceException(serviceException);

var expectedEventServiceException = new
EventServiceException(failedEventServiceException);
EventServiceException(
message: "Event service error occurred, contact support.",
innerException: failedEventServiceException);

this.storageBrokerMock.Setup(broker =>
broker.SelectAllEvents()).Throws(serviceException);
Expand All @@ -73,16 +83,19 @@ public void ShouldThrowServiceExceptionOnRetrieveAllIfServiceErrorOccursAndLogIt
retrieveAllEventsAction);

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

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

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

this.storageBrokerMock.VerifyNoOtherCalls();
this.loggingBrokerMock.VerifyNoOtherCalls();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Events
public partial class EventServiceTests
{
[Fact]
public async Task ShouldAddEventAsync()
private async Task ShouldAddEventAsync()
{
// given
DateTimeOffset randomDateTime = GetRandomDateTimeOffset();
Expand Down Expand Up @@ -47,4 +47,4 @@ public async Task ShouldAddEventAsync()
this.storageBrokerMock.VerifyNoOtherCalls();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Events
public partial class EventServiceTests
{
[Fact]
public void ShouldRetrieveAllEvents()
private void ShouldRetrieveAllEvents()
{
// given
IQueryable<Event> randomEvents = CreateRandomEvents();
Expand All @@ -39,4 +39,4 @@ public void ShouldRetrieveAllEvents()
this.dateTimeBrokerMock.VerifyNoOtherCalls();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Events
public partial class EventServiceTests
{
[Fact]
public async Task ShouldThrowValidationExceptionOnAddIfEventIsNullAndLogitAsync()
private async Task ShouldThrowValidationExceptionOnAddIfEventIsNullAndLogitAsync()
{
// given
Event nullEvent = null;
Expand All @@ -25,7 +25,9 @@ public async Task ShouldThrowValidationExceptionOnAddIfEventIsNullAndLogitAsync(
new NullEventException();

var expectedEventValidationException =
new EventValidationException(nullEventException);
new EventValidationException(
message: "Event validation error occurred, please try again.",
innerException: nullEventException);

// when
ValueTask<Event> addEventTask =
Expand All @@ -50,7 +52,7 @@ await Assert.ThrowsAsync<EventValidationException>(
}

[Fact]
public async Task ShouldThrowValidationExceptionOnAddIfEventIsInvalidAndLogItAsync()
private async Task ShouldThrowValidationExceptionOnAddIfEventIsInvalidAndLogItAsync()
{
// given
Guid invalidGuid = Guid.Empty;
Expand Down Expand Up @@ -85,7 +87,9 @@ public async Task ShouldThrowValidationExceptionOnAddIfEventIsInvalidAndLogItAsy
values: "Id is required");

var expectedEventValidationException =
new EventValidationException(invalidEventException);
new EventValidationException(
message: "Event validation error occurred, please try again.",
innerException: invalidEventException);

//when
ValueTask<Event> addEventTask =
Expand Down Expand Up @@ -119,7 +123,7 @@ await Assert.ThrowsAsync<EventValidationException>(

[Theory]
[MemberData(nameof(MinutesBeforeOrAfter))]
public async Task ShouldThrowValidationExceptionOnAddIfEventDateIsNotRecentAndLogItAsync(
private async Task ShouldThrowValidationExceptionOnAddIfEventDateIsNotRecentAndLogItAsync(
int minutesBeforeOrAfter)
{
// given
Expand All @@ -131,6 +135,7 @@ public async Task ShouldThrowValidationExceptionOnAddIfEventDateIsNotRecentAndLo

Event randomEvent = CreateRandomEvent(invalidDateTime);
Event invalidEvent = randomEvent;

var invalidEventException =
new InvalidEventException();

Expand All @@ -143,7 +148,9 @@ public async Task ShouldThrowValidationExceptionOnAddIfEventDateIsNotRecentAndLo
values: "Date is not recent");

var expectedEventValidationException =
new EventValidationException(invalidEventException);
new EventValidationException(
message: "Event validation error occurred, please try again.",
innerException: invalidEventException);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTimeOffset())
Expand Down Expand Up @@ -179,4 +186,4 @@ await Assert.ThrowsAsync<EventValidationException>(
this.storageBrokerMock.VerifyNoOtherCalls();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ private static Filler<Event> CreateEventFiller(DateTimeOffset dates)
return filler;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@

namespace Taarafo.Core.Models.Events.Exceptions
{
public class AlreadyExistsEventException : Xeption
{
public AlreadyExistsEventException(Exception innerException)
: base(message: "Event with the same id already exists.", innerException)
{ }
}
public class AlreadyExistsEventException : Xeption
{
public AlreadyExistsEventException(Exception innerException)
: base(
message: "Event with the same id already exists.",
innerException: innerException)
{ }

public AlreadyExistsEventException(string message, Exception innerException)
: base(message, innerException)
{ }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ namespace Taarafo.Core.Models.Events.Exceptions
public class EventDependencyException : Xeption
{
public EventDependencyException(Xeption innerException)
: base(message: "Event dependency validation occurred, please try again.", innerException)
: base(
message: "Event dependency validation occurred, please try again.",
innerException: innerException)
{ }

public EventDependencyException(string message, Xeption innerException)
: base(message, innerException)
{ }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ namespace Taarafo.Core.Models.Events.Exceptions
public class EventDependencyValidationException : Xeption
{
public EventDependencyValidationException(Xeption innerException)
: base(message: "Event dependency validation occurred, please try again.", innerException)
: base(
message: "Event dependency validation occurred, please try again.",
innerException: innerException)
{ }

public EventDependencyValidationException(string message, Xeption innerException)
: base(message, innerException)
{ }
}
}
Loading

0 comments on commit 3847859

Please sign in to comment.