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

CODE RUB: Upgrade Student Exceptions Add to The new Standard #519

Merged
merged 22 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
035ff78
ShouldThrowDependencyValidationExceptionOnRegisterIfStudentAlreadyExi…
Mehdi-Aghaei Oct 14, 2021
a4fc11f
ShouldThrowDependencyValidationExceptionOnRegisterWhenStudentAlreadyE…
Mehdi-Aghaei Oct 14, 2021
c2105e0
ShouldThrowDependencyValidationExceptionOnRegisterWhenStudentAlreadyE…
Mehdi-Aghaei Oct 14, 2021
54b6cb2
ShouldThrowDependencyValidationExceptionOnRegisterWhenStudentAlreadyE…
Mehdi-Aghaei Oct 14, 2021
adcd737
CODE RUB: Move Duplicate key exception to right place remove extra va…
Mehdi-Aghaei Oct 15, 2021
95b634d
CODE RUB : add message parameter
Mehdi-Aghaei Oct 15, 2021
3dd65e9
CODE RUB: Code Clean up
Mehdi-Aghaei Oct 15, 2021
b9800cf
Merge branch 'hassanhabib:master' into users/mehdiaghaei/foundations-…
Mehdi-Aghaei Oct 15, 2021
38e2662
ShouldThrowCriticalDependencyExceptionOnRegisterIfSqlErrorOccursAndLo…
Mehdi-Aghaei Oct 15, 2021
3d5945e
ShouldThrowCriticalDependencyExceptionOnRegisterIfSqlErrorOccursAndLo…
Mehdi-Aghaei Oct 15, 2021
b1f019b
ShouldThrowDependencyExceptionOnRegisterIfDatabaseUpdateErrorOccursAn…
Mehdi-Aghaei Oct 15, 2021
98ee45a
ShouldThrowDependencyExceptionOnRegisterIfDatabaseUpdateErrorOccursAn…
Mehdi-Aghaei Oct 17, 2021
de51212
ShouldThrowCriticalDependencyExceptionOnRetrieveIfSqlErrorOccursAndLo…
Mehdi-Aghaei Oct 17, 2021
f9f9bdc
ShouldThrowDependencyExceptionOnDeleteIfDatabaseUpdateErrorOccursAndL…
Mehdi-Aghaei Oct 17, 2021
1af6688
ShouldThrowCriticalDependencyExceptionOnModifyIfSqlErrorOccursAndLogI…
Mehdi-Aghaei Oct 17, 2021
bf41a28
CODE RUB: Extra space
Mehdi-Aghaei Oct 17, 2021
ff692e8
CODE RUB: Merge
Mehdi-Aghaei Oct 17, 2021
535d37a
ShouldThrowCriticalDependencyExceptionOnRegisterIfSqlErrorOccursAndLo…
Mehdi-Aghaei Oct 17, 2021
2b9b7f6
CODE RUB: Clean Up Solution
Mehdi-Aghaei Oct 18, 2021
2d05d55
CODE RUB : Cleanup
Mehdi-Aghaei Oct 18, 2021
4ca572f
CODE RUB: Cleanup
Mehdi-Aghaei Oct 18, 2021
8189539
CODE RUB: Remove Extra Space
Mehdi-Aghaei Oct 18, 2021
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
Expand Up @@ -5,6 +5,7 @@

using System;
using System.Threading.Tasks;
using EFxceptions.Models.Exceptions;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Moq;
Expand All @@ -17,14 +18,17 @@ namespace OtripleS.Web.Api.Tests.Unit.Services.Foundations.Students
public partial class StudentServiceTests
{
[Fact]
public async Task ShouldThrowDependencyExceptionOnRegisterIfSqlExceptionOccursAndLogItAsync()
public async Task ShouldThrowCriticalDependencyExceptionOnRegisterIfSqlErrorOccursAndLogItAsync()
{
// given
Student someStudent = CreateRandomStudent();
SqlException sqlException = GetSqlException();

var failedStudentStorageException =
new FailedStudentStorageException(sqlException);

var expectedStudentDependencyException =
new StudentDependencyException(sqlException);
new StudentDependencyException(failedStudentStorageException);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTime())
Expand All @@ -45,10 +49,56 @@ public async Task ShouldThrowDependencyExceptionOnRegisterIfSqlExceptionOccursAn
this.loggingBrokerMock.Verify(broker =>
broker.LogCritical(It.Is(SameExceptionAs(
expectedStudentDependencyException))),
Times.Once);

this.storageBrokerMock.Verify(broker =>
broker.InsertStudentAsync(someStudent),
Times.Never);

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

[Fact]
public async Task ShouldThrowDependencyValidationExceptionOnRegisterWhenStudentAlreadyExistsAndLogItAsync()
{
// given
Student someStudent = CreateRandomStudent();
string someMessage = GetRandomMessage();

var duplicateKeyException =
new DuplicateKeyException(someMessage);

var alreadyExistsStudentException =
new AlreadyExistsStudentException(duplicateKeyException);

var expectedStudentDependencyValidationException =
new StudentDependencyValidationException(alreadyExistsStudentException);

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

// when
ValueTask<Student> registerStudentTask =
this.studentService.RegisterStudentAsync(someStudent);

// then
await Assert.ThrowsAsync<StudentDependencyValidationException>(() =>
registerStudentTask.AsTask());

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

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

this.storageBrokerMock.Verify(broker =>
broker.InsertStudentAsync(It.IsAny<Student>()),
broker.InsertStudentAsync(someStudent),
Times.Never);

this.dateTimeBrokerMock.VerifyNoOtherCalls();
Expand All @@ -57,14 +107,17 @@ public async Task ShouldThrowDependencyExceptionOnRegisterIfSqlExceptionOccursAn
}

[Fact]
public async Task ShouldThrowDependencyExceptionOnRegisterIfDbExceptionOccursAndLogItAsync()
public async Task ShouldThrowDependencyExceptionOnRegisterIfDatabaseUpdateErrorOccursAndLogItAsync()
{
// given
Student someStudent = CreateRandomStudent();
var databaseUpdateException = new DbUpdateException();

Mehdi-Aghaei marked this conversation as resolved.
Show resolved Hide resolved
var failedStudentStorageException =
new FailedStudentStorageException(databaseUpdateException);

var expectedStudentDependencyException =
new StudentDependencyException(databaseUpdateException);
new StudentDependencyException(failedStudentStorageException);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTime())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace OtripleS.Web.Api.Tests.Unit.Services.Foundations.Students
public partial class StudentServiceTests
{
[Fact]
public async Task ShouldThrowDependencyExceptionOnModifyIfSqlExceptionOccursAndLogItAsync()
public async Task ShouldThrowCriticalDependencyExceptionOnModifyIfSqlErrorOccursAndLogItAsync()
{
// given
Student someStudent = CreateRandomStudent();
Expand All @@ -28,8 +28,11 @@ public async Task ShouldThrowDependencyExceptionOnModifyIfSqlExceptionOccursAndL

SqlException sqlException = GetSqlException();

var failedStudentStorageException =
new FailedStudentStorageException(sqlException);

var expectedStudentDependencyException =
new StudentDependencyException(sqlException);
new StudentDependencyException(failedStudentStorageException);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTime())
Expand Down Expand Up @@ -73,8 +76,11 @@ public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateExceptionOccur

var databaseUpdateException = new DbUpdateException();

var failedStudentStorageException =
new FailedStudentStorageException(databaseUpdateException);

var expectedStudentDependencyException =
new StudentDependencyException(databaseUpdateException);
new StudentDependencyException(failedStudentStorageException);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTime())
Expand Down Expand Up @@ -116,10 +122,10 @@ public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExc
someStudent.UpdatedDate =
someStudent.CreatedDate.AddDays(randomDays);

var databaseUpdateConcurrencyException =
var databaseUpdateConcurrencyException =
new DbUpdateConcurrencyException();
var lockedStudentException =

var lockedStudentException =
new LockedStudentException(databaseUpdateConcurrencyException);

var expectedStudentDependencyException =
Expand Down Expand Up @@ -161,10 +167,10 @@ public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAnd
// given
Student someStudent = CreateRandomStudent();
int randomDays = GetRandomNumber();
someStudent.UpdatedDate =

someStudent.UpdatedDate =
someStudent.CreatedDate.AddDays(randomDays);

var serviceException = new Exception();

var failedStudentServiceException =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ public async Task ShouldThrowDependencyExceptionOnDeleteIfSqlExceptionOccursAndL
Guid someStudentId = Guid.NewGuid();
SqlException sqlException = GetSqlException();

var failedStudentStorageException =
new FailedStudentStorageException(sqlException);

var expectedStudentDependencyException =
new StudentDependencyException(sqlException);
new StudentDependencyException(failedStudentStorageException);

this.storageBrokerMock.Setup(broker =>
broker.SelectStudentByIdAsync(It.IsAny<Guid>()))
Expand Down Expand Up @@ -59,8 +62,11 @@ public async Task ShouldThrowDependencyExceptionOnDeleteIfDatabaseUpdateExceptio
Guid someStudentId = Guid.NewGuid();
var databaseUpdateException = new DbUpdateException();

var failedStudentStorageException =
new FailedStudentStorageException(databaseUpdateException);

var expectedStudentDependencyException =
new StudentDependencyException(databaseUpdateException);
new StudentDependencyException(failedStudentStorageException);

this.storageBrokerMock.Setup(broker =>
broker.SelectStudentByIdAsync(It.IsAny<Guid>()))
Expand Down Expand Up @@ -94,10 +100,10 @@ public async Task ShouldThrowDependencyExceptionOnDeleteIfDatabaseUpdateConcurre
// given
Guid someStudentId = Guid.NewGuid();

var databaseUpdateConcurrencyException =
var databaseUpdateConcurrencyException =
new DbUpdateConcurrencyException();

var lockedStudentException =
var lockedStudentException =
new LockedStudentException(databaseUpdateConcurrencyException);

var expectedStudentDependencyException =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ public async Task ShouldThrowDependencyExceptionOnRetrieveIfSqlExceptionOccursAn
Guid someStudentId = Guid.NewGuid();
var sqlException = GetSqlException();

var failedStudentStorageException =
new FailedStudentStorageException(sqlException);

var expectedStudentDependencyException =
new StudentDependencyException(sqlException);
new StudentDependencyException(failedStudentStorageException);

this.storageBrokerMock.Setup(broker =>
broker.SelectStudentByIdAsync(It.IsAny<Guid>()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

using System;
using System.Threading.Tasks;
using EFxceptions.Models.Exceptions;
using Moq;
using OtripleS.Web.Api.Models.Students;
using OtripleS.Web.Api.Models.Students.Exceptions;
Expand Down Expand Up @@ -274,56 +273,5 @@ public async void ShouldThrowValidationExceptionOnRegisterWhenUpdatedDateIsNotSa
this.loggingBrokerMock.VerifyNoOtherCalls();
this.storageBrokerMock.VerifyNoOtherCalls();
}

[Fact]
public async void ShouldThrowValidationExceptionOnRegisterWhenStudentAlreadyExistsAndLogItAsync()
{
// given
DateTimeOffset dateTime = GetRandomDateTime();
Student randomStudent = CreateRandomStudent(dateTime);
Student alreadyExistsStudent = randomStudent;
alreadyExistsStudent.UpdatedBy = alreadyExistsStudent.CreatedBy;
string randomMessage = GetRandomMessage();
string exceptionMessage = randomMessage;
var duplicateKeyException = new DuplicateKeyException(exceptionMessage);

var alreadyExistsStudentException =
new AlreadyExistsStudentException(duplicateKeyException);

var expectedStudentValidationException =
new StudentValidationException(alreadyExistsStudentException);

this.dateTimeBrokerMock.Setup(broker =>
broker.GetCurrentDateTime())
.Returns(dateTime);

this.storageBrokerMock.Setup(broker =>
broker.InsertStudentAsync(alreadyExistsStudent))
.ThrowsAsync(duplicateKeyException);

// when
ValueTask<Student> registerStudentTask =
this.studentService.RegisterStudentAsync(alreadyExistsStudent);

// then
await Assert.ThrowsAsync<StudentValidationException>(() =>
registerStudentTask.AsTask());

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

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

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

this.dateTimeBrokerMock.VerifyNoOtherCalls();
this.storageBrokerMock.VerifyNoOtherCalls();
this.loggingBrokerMock.VerifyNoOtherCalls();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
// ---------------------------------------------------------------

using System;
using Xeptions;

namespace OtripleS.Web.Api.Models.Students.Exceptions
{
public class AlreadyExistsStudentException : Exception
public class AlreadyExistsStudentException : Xeption
{
public AlreadyExistsStudentException(Exception innerException)
: base(message: "Student with the same id already exists.", innerException) { }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using Xeptions;

namespace OtripleS.Web.Api.Models.Students.Exceptions
{
public class FailedStudentStorageException : Xeption
{
public FailedStudentStorageException(Exception innerException)
: base(message: "Failed post storage error occurred, contact suppport.", innerException)
{ }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// ---------------------------------------------------------------
// Copyright (c) Coalition of the Good-Hearted Engineers
// FREE TO USE AS LONG AS SOFTWARE FUNDS ARE DONATED TO THE POOR
// ---------------------------------------------------------------

using Xeptions;

namespace OtripleS.Web.Api.Models.Students.Exceptions
{
public class StudentDependencyValidationException : Xeption
{
public StudentDependencyValidationException(Xeption innerException)
: base(message: "Student dependency validation error occurred, please try again.", innerException)
{ }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// FREE TO USE AS LONG AS SOFTWARE FUNDS ARE DONATED TO THE POOR
// ---------------------------------------------------------------

using System;
using Xeptions;

namespace OtripleS.Web.Api.Models.Teachers.Exceptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.EntityFrameworkCore;
using OtripleS.Web.Api.Models.Students;
using OtripleS.Web.Api.Models.Students.Exceptions;
using Xeptions;

namespace OtripleS.Web.Api.Services.Foundations.Students
{
Expand Down Expand Up @@ -39,14 +40,17 @@ private async ValueTask<Student> TryCatch(ReturningStudentFunction returningStud
}
catch (SqlException sqlException)
{
throw CreateAndLogCriticalDependencyException(sqlException);
var failedStudentStorageException =
new FailedStudentStorageException(sqlException);

throw CreateAndLogCriticalDependencyException(failedStudentStorageException);
}
catch (DuplicateKeyException duplicateKeyException)
{
var alreadyExistsStudentException =
new AlreadyExistsStudentException(duplicateKeyException);

throw CreateAndLogValidationException(alreadyExistsStudentException);
throw CreateAndLogDependencyValidationException(alreadyExistsStudentException);
}
catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
{
Expand All @@ -56,7 +60,10 @@ private async ValueTask<Student> TryCatch(ReturningStudentFunction returningStud
}
catch (DbUpdateException dbUpdateException)
{
throw CreateAndLogDependencyException(dbUpdateException);
var failedStudentStorageException =
new FailedStudentStorageException(dbUpdateException);

throw CreateAndLogDependencyException(failedStudentStorageException);
}
catch (Exception exception)
{
Expand Down Expand Up @@ -117,5 +124,15 @@ private StudentValidationException CreateAndLogValidationException(Exception exc

return studentValidationException;
}

private StudentDependencyValidationException CreateAndLogDependencyValidationException(Xeption exception)
{
var studentDependencyValidationException =
new StudentDependencyValidationException(exception);

this.loggingBroker.LogError(studentDependencyValidationException);

return studentDependencyValidationException;
}
}
}
Loading