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

            var expectedStudentRegistrationServiceException =
                new StudentRegistrationServiceException(exception);

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

            // when . then
            Assert.Throws <StudentRegistrationServiceException>(() =>
                                                                this.studentRegistrationService.RetrieveAllStudentRegistrations());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 2
0
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            StudentRegistration someStudentRegistration = CreateRandomStudentRegistration();
            var exception = new Exception();

            var expectedStudentRegistrationServiceException =
                new StudentRegistrationServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentRegistrationAsync(It.IsAny <StudentRegistration>()))
            .ThrowsAsync(exception);

            // when
            ValueTask <StudentRegistration> addStudentRegistrationTask =
                this.studentRegistrationService.AddStudentRegistrationAsync(someStudentRegistration);

            // then
            await Assert.ThrowsAsync <StudentRegistrationServiceException>(() =>
                                                                           addStudentRegistrationTask.AsTask());

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

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

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

            this.loggingBroker.LogError(StudentRegistrationServiceException);

            return(StudentRegistrationServiceException);
        }
Esempio n. 4
0
        public async Task ShouldThrowServiceExceptionOnRemoveWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomStudentId      = Guid.NewGuid();
            Guid inputStudentId       = randomStudentId;
            Guid randomRegistrationId = Guid.NewGuid();
            Guid inputRegistrationId  = randomRegistrationId;
            StudentRegistration someStudentRegistration = CreateRandomStudentRegistration();
            var serviceException = new Exception();

            var failedStudentRegistrationServiceException =
                new FailedStudentRegistrationServiceException(serviceException);

            var expectedStudentRegistrationServiceException =
                new StudentRegistrationServiceException(
                    failedStudentRegistrationServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentRegistrationByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()))
            .ReturnsAsync(someStudentRegistration);

            this.storageBrokerMock.Setup(broker =>
                                         broker.DeleteStudentRegistrationAsync(It.IsAny <StudentRegistration>()))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <StudentRegistration> deleteStudentRegistrationTask =
                this.studentRegistrationService.RemoveStudentRegistrationByIdsAsync(
                    inputStudentId,
                    inputRegistrationId);

            // then
            await Assert.ThrowsAsync <StudentRegistrationServiceException>(() =>
                                                                           deleteStudentRegistrationTask.AsTask());

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnRetrieveWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomStudentId      = Guid.NewGuid();
            Guid inputStudentId       = randomStudentId;
            Guid randomRegistrationId = Guid.NewGuid();
            Guid inputRegistrationId  = randomRegistrationId;
            var  exception            = new Exception();

            var expectedStudentRegistrationServiceException =
                new StudentRegistrationServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentRegistrationByIdAsync(inputStudentId, inputRegistrationId))
            .ThrowsAsync(exception);

            // when
            ValueTask <StudentRegistration> retrieveStudentRegistrationByIdTask =
                this.studentRegistrationService.RetrieveStudentRegistrationByIdAsync(inputStudentId, inputRegistrationId);

            // then
            await Assert.ThrowsAsync <StudentRegistrationServiceException>(() =>
                                                                           retrieveStudentRegistrationByIdTask.AsTask());

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

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

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

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