public async Task ShouldThrowValidationExceptionOnModifyWhenStudentIdIsInvalidAndLogItAsync()
        {
            //given
            Guid           invalidStudentId   = Guid.Empty;
            DateTimeOffset dateTime           = GetRandomDateTime();
            StudentExam    randomStudentExam  = CreateRandomStudentExam(dateTime);
            StudentExam    invalidStudentExam = randomStudentExam;

            invalidStudentExam.StudentId = invalidStudentId;

            var invalidStudentExamException = new InvalidStudentExamException(
                parameterName: nameof(StudentExam.StudentId),
                parameterValue: invalidStudentExam.StudentId);

            var expectedStudentExamValidationException =
                new StudentExamValidationException(invalidStudentExamException);

            //when
            ValueTask <StudentExam> modifyStudentExamTask =
                this.studentExamService.ModifyStudentExamAsync(invalidStudentExam);

            //then
            await Assert.ThrowsAsync <StudentExamValidationException>(() =>
                                                                      modifyStudentExamTask.AsTask());

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnModifyWhenUpdatedDateIsSameAsCreatedDateAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime          = GetRandomDateTime();
            StudentExam    randomStudentExam = CreateRandomStudentExam(dateTime);
            StudentExam    inputStudentExam  = randomStudentExam;

            var invalidStudentExamException = new InvalidStudentExamException(
                parameterName: nameof(StudentExam.UpdatedDate),
                parameterValue: inputStudentExam.UpdatedDate);

            var expectedStudentExamValidationException =
                new StudentExamValidationException(invalidStudentExamException);

            // when
            ValueTask <StudentExam> modifyStudentExamTask =
                this.studentExamService.ModifyStudentExamAsync(inputStudentExam);

            // then
            await Assert.ThrowsAsync <StudentExamValidationException>(() =>
                                                                      modifyStudentExamTask.AsTask());

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowValidationExceptionOnModifyIfStorageCreatedDateNotSameAsCreateDateAndLogItAsync()
        {
            // given
            int            randomNumber       = GetRandomNumber();
            int            randomMinutes      = randomNumber;
            DateTimeOffset randomDate         = GetRandomDateTime();
            StudentExam    randomStudentExam  = CreateRandomStudentExam(randomDate);
            StudentExam    invalidStudentExam = randomStudentExam;

            invalidStudentExam.UpdatedDate = randomDate;
            StudentExam storageStudentExam = randomStudentExam.DeepClone();
            Guid        studentId          = invalidStudentExam.StudentId;
            Guid        semesterCourseId   = invalidStudentExam.ExamId;

            invalidStudentExam.CreatedDate = storageStudentExam.CreatedDate.AddMinutes(randomNumber);

            var invalidStudentExamException = new InvalidStudentExamException(
                parameterName: nameof(StudentExam.CreatedDate),
                parameterValue: invalidStudentExam.CreatedDate);

            var expectedStudentExamValidationException =
                new StudentExamValidationException(invalidStudentExamException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentExamByIdAsync(invalidStudentExam.Id))
            .ReturnsAsync(storageStudentExam);

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

            // when
            ValueTask <StudentExam> modifyStudentExamTask =
                this.studentExamService.ModifyStudentExamAsync(invalidStudentExam);

            // then
            await Assert.ThrowsAsync <StudentExamValidationException>(() =>
                                                                      modifyStudentExamTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentExamByIdAsync(invalidStudentExam.Id),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Example #4
0
        public async void ShouldThrowValidationExceptionOnRetrieveWhenIdIsInvalidAndLogItAsync()
        {
            // given
            Guid randomStudentExamId = default;
            Guid inputStudentExamId  = randomStudentExamId;

            var invalidStudentExamException = new InvalidStudentExamException(
                parameterName: nameof(StudentExam.Id),
                parameterValue: inputStudentExamId);

            var expectedStudentExamValidationException =
                new StudentExamValidationException(invalidStudentExamException);

            // when
            ValueTask <StudentExam> retrieveStudentExamByIdTask =
                this.studentExamService.RetrieveStudentExamByIdAsync(inputStudentExamId);

            // then
            await Assert.ThrowsAsync <StudentExamValidationException>(() =>
                                                                      retrieveStudentExamByIdTask.AsTask());

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

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

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

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