예제 #1
0
        public async Task ShouldThrowValidationExceptionOnModifyIfExamDoesntExistAndLogItAsync()
        {
            // given
            int            randomNegativeMinutes = GetNegativeRandomNumber();
            DateTimeOffset dateTime        = GetRandomDateTime();
            Exam           randomExam      = CreateRandomExam(dateTime);
            Exam           nonExistentExam = randomExam;

            nonExistentExam.CreatedDate = dateTime.AddMinutes(randomNegativeMinutes);
            Exam noExam = null;
            var  notFoundExamException = new NotFoundExamException(nonExistentExam.Id);

            var expectedExamValidationException =
                new ExamValidationException(notFoundExamException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectExamByIdAsync(nonExistentExam.Id))
            .ReturnsAsync(noExam);

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

            // when
            ValueTask <Exam> modifyExamTask =
                this.examService.ModifyExamAsync(nonExistentExam);

            // then
            await Assert.ThrowsAsync <ExamValidationException>(() =>
                                                               modifyExamTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectExamByIdAsync(nonExistentExam.Id),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
예제 #2
0
        public async Task ShouldThrowValidatonExceptionOnDeleteIfStorageExamIsInvalidAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime        = GetRandomDateTime();
            Exam           randomExam      = CreateRandomExam(dateTime);
            Guid           inputExamId     = randomExam.Id;
            Exam           inputExam       = randomExam;
            Exam           nullStorageExam = null;

            var notFoundExamException = new NotFoundExamException(inputExamId);

            var expectedExamValidationException =
                new ExamValidationException(notFoundExamException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectExamByIdAsync(inputExamId))
            .ReturnsAsync(nullStorageExam);

            // when
            ValueTask <Exam> actualExamTask =
                this.examService.RemoveExamByIdAsync(inputExamId);

            // then
            await Assert.ThrowsAsync <ExamValidationException>(() => actualExamTask.AsTask());

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnRetrieveByIdIfStorageExamIsNullAndLogItAsync()
        {
            // given
            Guid someExamId         = Guid.NewGuid();
            Exam invalidStorageExam = null;

            var notFoundExamException =
                new NotFoundExamException(someExamId);

            var expectedExamValidationException =
                new ExamValidationException(notFoundExamException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectExamByIdAsync(someExamId))
            .ReturnsAsync(invalidStorageExam);

            // when
            ValueTask <Exam> retrieveExamByIdTask =
                this.examService.RetrieveExamByIdAsync(someExamId);

            // then
            await Assert.ThrowsAsync <ExamValidationException>(() =>
                                                               retrieveExamByIdTask.AsTask());

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

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

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

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