public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt()
        {
            // given
            var exception = new Exception();

            var expectedStudentGuardianServiceException =
                new StudentGuardianServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAllStudentGuardians())
            .Throws(exception);

            // when . then
            Assert.Throws <StudentGuardianServiceException>(() =>
                                                            this.studentGuardianService.RetrieveAllStudentGuardians());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        private StudentGuardianServiceException CreateAndLogServiceException(Exception exception)
        {
            var StudentGuardianServiceException = new StudentGuardianServiceException(exception);

            this.loggingBroker.LogError(StudentGuardianServiceException);

            return(StudentGuardianServiceException);
        }
        public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync()
        {
            // given
            int             randomNegativeNumber  = GetNegativeRandomNumber();
            DateTimeOffset  randomDateTime        = GetRandomDateTime();
            StudentGuardian randomStudentGuardian = CreateRandomStudentGuardian(randomDateTime);
            StudentGuardian someStudentGuardian   = randomStudentGuardian;

            someStudentGuardian.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var serviceException = new Exception();

            var failedStudentGuardianServiceException =
                new FailedStudentGuardianServiceException(serviceException);

            var expectedStudentGuardianServiceException =
                new StudentGuardianServiceException(failedStudentGuardianServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentGuardianByIdAsync
                                             (someStudentGuardian.StudentId, someStudentGuardian.GuardianId))
            .ThrowsAsync(serviceException);

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

            // when
            ValueTask <StudentGuardian> modifyStudentGuardianTask =
                this.studentGuardianService.ModifyStudentGuardianAsync(someStudentGuardian);

            // then
            await Assert.ThrowsAsync <StudentGuardianServiceException>(() =>
                                                                       modifyStudentGuardianTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentGuardianByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset  dateTime = GetRandomDateTime();
            StudentGuardian randomStudentGuardian = CreateRandomStudentGuardian(dateTime);
            StudentGuardian inputStudentGuardian  = randomStudentGuardian;

            inputStudentGuardian.UpdatedBy = inputStudentGuardian.CreatedBy;
            var serviceException = new Exception();

            var failedStudentGuardianServiceException =
                new FailedStudentGuardianServiceException(serviceException);

            var expectedStudentGuardianServiceException =
                new StudentGuardianServiceException(failedStudentGuardianServiceException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentGuardianAsync(inputStudentGuardian))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <StudentGuardian> addStudentGuardianTask =
                this.studentGuardianService.AddStudentGuardianAsync(inputStudentGuardian);

            // then
            await Assert.ThrowsAsync <StudentGuardianServiceException>(() =>
                                                                       addStudentGuardianTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #5
0
        public async Task ShouldThrowServiceExceptionOnRetrieveWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid someStudentId    = Guid.NewGuid();
            Guid someGuardianId   = Guid.NewGuid();
            var  serviceException = new Exception();

            var failedStudentGuardianServiceException =
                new FailedStudentGuardianServiceException(serviceException);

            var expectedStudentGuardianServiceException =
                new StudentGuardianServiceException(failedStudentGuardianServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentGuardianByIdAsync(someStudentId, someGuardianId))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <StudentGuardian> retrieveStudentGuardianByIdTask =
                this.studentGuardianService.RetrieveStudentGuardianByIdAsync(someStudentId, someGuardianId);

            // then
            await Assert.ThrowsAsync <StudentGuardianServiceException>(() =>
                                                                       retrieveStudentGuardianByIdTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentGuardianByIdAsync(someStudentId, someGuardianId),
                                          Times.Once);

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #6
0
        public async Task ShouldThrowServiceExceptionOnDeleteWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomStudentGuardianId = Guid.NewGuid();
            Guid randomStudentId         = Guid.NewGuid();
            Guid inputStudentGuardianId  = randomStudentGuardianId;
            Guid inputStudentId          = randomStudentId;
            var  exception = new Exception();

            var expectedStudentGuardianException =
                new StudentGuardianServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentGuardianByIdAsync(inputStudentGuardianId, inputStudentId))
            .ThrowsAsync(exception);

            // when
            ValueTask <StudentGuardian> deleteStudentGuardianTask =
                this.studentGuardianService.DeleteStudentGuardianAsync(inputStudentGuardianId, inputStudentId);

            // then
            await Assert.ThrowsAsync <StudentGuardianServiceException>(() =>
                                                                       deleteStudentGuardianTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentGuardianByIdAsync(inputStudentGuardianId, inputStudentId),
                                          Times.Once);

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