Exemple #1
0
        public async Task ShouldThrowServiceExceptionOnRetrieveAllIfServiceErrorOccursAndLogItAsync()
        {
            // given
            var serviceException = new Exception();

            var failedStudentServiceExcption =
                new FailedStudentServiceException(serviceException);

            var expectedStudentServiceException =
                new StudentServiceException(failedStudentServiceExcption);

            this.apiBrokerMock.Setup(broker =>
                                     broker.GetAllStudentsAsync())
            .ThrowsAsync(serviceException);

            // when
            ValueTask <List <Student> > retrievedStudentTask =
                this.studentService.RetrieveAllStudentsAsync();

            // then
            await Assert.ThrowsAsync <StudentServiceException>(() =>
                                                               retrievedStudentTask.AsTask());

            this.apiBrokerMock.Verify(broker =>
                                      broker.GetAllStudentsAsync(),
                                      Times.Once);

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

            this.apiBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnDeleteWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomStudentId = Guid.NewGuid();
            Guid inputStudentId  = randomStudentId;
            var  exception       = new Exception();

            var expectedStudentServiceException =
                new StudentServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentByIdAsync(inputStudentId))
            .ThrowsAsync(exception);

            // when
            ValueTask <Student> deleteStudentTask =
                this.studentService.DeleteStudentAsync(inputStudentId);

            // then
            await Assert.ThrowsAsync <StudentServiceException>(() =>
                                                               deleteStudentTask.AsTask());

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt()
        {
            // given
            var exception = new Exception();

            var expectedStudentServiceException =
                new StudentServiceException(exception);

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

            // when . then
            Assert.Throws <StudentServiceException>(() =>
                                                    this.studentService.RetrieveAllStudents());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnRegisterIfServiceFailureOcurredAndLogItAsync()
        {
            // given
            Student someStudent      = CreateRandomStudent();
            var     serviceException = new Exception();

            var expectedStudentServiceException =
                new StudentServiceException(serviceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentAsync(someStudent))
            .ThrowsAsync(serviceException);

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

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowStudentServiceExceptionnOnRetrieveIfGenericExceptionThrownAndLogItAsync()
        {
            //given
            Guid someId = Guid.NewGuid();

            var serviceException         = new Exception();
            var expectedServiceException = new StudentServiceException(serviceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentByIdAsync(someId))
            .ThrowsAsync(serviceException);

            //when
            ValueTask <Student> retrieveStudentTask =
                this.studentService.RetrieveStudentAsync(someId);

            //then
            await Assert.ThrowsAsync <StudentServiceException>(() => retrieveStudentTask.AsTask());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        private StudentServiceException CreateAndLogServiceException(Exception exception)
        {
            var studentServiceException = new StudentServiceException(exception);
            this.loggingBroker.LogError(studentServiceException);

            return studentServiceException;
        }
        public static TheoryData DependencyExceptions()
        {
            var innerException = new Exception();

            var studentDependencyException =
                new StudentDependencyException(innerException);

            var studentServiceException =
                new StudentServiceException(innerException);

            return(new TheoryData <Exception>
            {
                studentDependencyException,
                studentServiceException
            });
        }
        public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync()
        {
            // given
            Student someStudent = CreateRandomStudent();
            int     randomDays  = GetRandomNumber();

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

            var serviceException = new Exception();

            var failedStudentServiceException =
                new FailedStudentServiceException(serviceException);

            var expectedStudentServiceException =
                new StudentServiceException(failedStudentServiceException);

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

            // when
            ValueTask <Student> modifyStudentTask =
                this.studentService.ModifyStudentAsync(someStudent);

            // then
            await Assert.ThrowsAsync <StudentServiceException>(() =>
                                                               modifyStudentTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnRegisterWhenExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime      = GetRandomDateTime();
            Student        randomStudent = CreateRandomStudent(dateTime);
            Student        inputStudent  = randomStudent;

            inputStudent.UpdatedBy = inputStudent.CreatedBy;
            var exception = new Exception();

            var expectedStudentServiceException =
                new StudentServiceException(exception);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentAsync(inputStudent))
            .ThrowsAsync(exception);

            // when
            ValueTask <Student> registerStudentByIdTask =
                this.studentService.RegisterStudentAsync(inputStudent);

            // then
            await Assert.ThrowsAsync <StudentServiceException>(() =>
                                                               registerStudentByIdTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset randomDateTime = GetRandomDateTime();
            Student        randomStudent  = CreateRandomStudent();
            Student        someStudent    = randomStudent;

            someStudent.UpdatedDate = randomDateTime;
            var serviceException = new Exception();

            var expectedStudentServiceException =
                new StudentServiceException(serviceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentByIdAsync(someStudent.Id))
            .ThrowsAsync(serviceException);

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

            // when
            ValueTask <Student> modifyStudentTask =
                this.studentService.ModifyStudentAsync(someStudent);

            // then
            await Assert.ThrowsAsync <StudentServiceException>(() =>
                                                               modifyStudentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentByIdAsync(someStudent.Id),
                                          Times.Once);

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

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