public async Task ShouldThrowValidationExceptionOnDeleteWhenStorageStudentSemesterCourseIsInvalidAndLogItAsync()
        {
            // given
            DateTimeOffset        randomDateTime = GetRandomDateTime();
            StudentSemesterCourse randomStudentSemesterCourse = CreateRandomStudentSemesterCourse(randomDateTime);
            Guid inputSemasterCourseId = randomStudentSemesterCourse.SemesterCourseId;
            Guid inputStudentId        = randomStudentSemesterCourse.StudentId;
            StudentSemesterCourse nullStorageStudentSemesterCourse = null;

            var notFoundStudentSemesterCourseException =
                new NotFoundStudentSemesterCourseException(inputSemasterCourseId, inputStudentId);

            var expectedSemesterCourseValidationException =
                new StudentSemesterCourseValidationException(notFoundStudentSemesterCourseException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentSemesterCourseByIdAsync(inputSemasterCourseId, inputStudentId))
            .ReturnsAsync(nullStorageStudentSemesterCourse);

            // when
            ValueTask <StudentSemesterCourse> actualStudentSemesterCourseDeleteTask =
                this.studentSemesterCourseService.RemoveStudentSemesterCourseByIdsAsync(inputSemasterCourseId, inputStudentId);

            // then
            await Assert.ThrowsAsync <StudentSemesterCourseValidationException>(() =>
                                                                                actualStudentSemesterCourseDeleteTask.AsTask());

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Example #2
0
        private void ValidateAuditFieldsDataOnCreate(StudentSemesterCourse studentSemesterCourse)
        {
            switch (studentSemesterCourse)
            {
            case { } when studentSemesterCourse.UpdatedBy != studentSemesterCourse.CreatedBy:
                throw new InvalidStudentSemesterCourseInputException(
                          parameterName: nameof(StudentSemesterCourse.UpdatedBy),
                          parameterValue: studentSemesterCourse.UpdatedBy);

            case { } when studentSemesterCourse.UpdatedDate != studentSemesterCourse.CreatedDate:
                throw new InvalidStudentSemesterCourseInputException(
                          parameterName: nameof(StudentSemesterCourse.UpdatedDate),
                          parameterValue: studentSemesterCourse.UpdatedDate);

            case { } when IsDateNotRecent(studentSemesterCourse.CreatedDate):
                throw new InvalidStudentSemesterCourseInputException(
                          parameterName: nameof(StudentSemesterCourse.CreatedDate),
                          parameterValue: studentSemesterCourse.CreatedDate);
            }
        }
        public async Task ShouldCreateStudentSemesterCourseAsync()
        {
            // given
            DateTimeOffset        randomDateTime       = GetRandomDateTime();
            DateTimeOffset        dateTime             = randomDateTime;
            StudentSemesterCourse randomSemesterCourse = CreateRandomStudentSemesterCourse(randomDateTime);

            randomSemesterCourse.UpdatedBy   = randomSemesterCourse.CreatedBy;
            randomSemesterCourse.UpdatedDate = randomSemesterCourse.CreatedDate;
            StudentSemesterCourse inputStudentSemesterCourse    = randomSemesterCourse;
            StudentSemesterCourse storageStudentSemesterCourse  = randomSemesterCourse;
            StudentSemesterCourse expectedStudentSemesterCourse = storageStudentSemesterCourse;

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentSemesterCourseAsync(inputStudentSemesterCourse))
            .ReturnsAsync(storageStudentSemesterCourse);

            // when
            StudentSemesterCourse actualSemesterCourse =
                await this.studentSemesterCourseService.CreateStudentSemesterCourseAsync(inputStudentSemesterCourse);

            // then
            actualSemesterCourse.Should().BeEquivalentTo(expectedStudentSemesterCourse);

            // This is called within validation code
            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Once);

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public ValueTask <StudentSemesterCourse> ModifyStudentSemesterCourseAsync(
            StudentSemesterCourse studentSemesterCourse) => TryCatch(async() =>
        {
            ValidateStudentSemesterCourseOnModify(studentSemesterCourse);

            StudentSemesterCourse maybeStudentSemesterCourse =
                await this.storageBroker.SelectStudentSemesterCourseByIdAsync(
                    studentSemesterCourse.StudentId,
                    studentSemesterCourse.SemesterCourseId);

            ValidateStorageStudentSemesterCourse(
                maybeStudentSemesterCourse,
                studentSemesterCourse.StudentId,
                studentSemesterCourse.SemesterCourseId);

            ValidateAgainstStorageStudentSemesterCourseOnModify(
                inputStudentSemesterCourse: studentSemesterCourse,
                storageStudentSemesterCourse: maybeStudentSemesterCourse);

            return(await this.storageBroker.UpdateStudentSemesterCourseAsync(studentSemesterCourse));
        });
        public async void ShouldThrowValidationExceptionOnCreateWhenUpdatedDateIsNotSameToCreatedDateAndLogItAsync()
        {
            // given
            DateTimeOffset        dateTime = GetRandomDateTime();
            StudentSemesterCourse randomStudentSemesterCourse = CreateRandomStudentSemesterCourse(dateTime);
            StudentSemesterCourse inputStudentSemesterCourse  = randomStudentSemesterCourse;

            inputStudentSemesterCourse.UpdatedBy   = randomStudentSemesterCourse.CreatedBy;
            inputStudentSemesterCourse.UpdatedDate = GetRandomDateTime();

            var invalidStudentSemesterCourseInputException = new InvalidStudentSemesterCourseInputException(
                parameterName: nameof(StudentSemesterCourse.UpdatedDate),
                parameterValue: inputStudentSemesterCourse.UpdatedDate);

            var expectedStudentSemesterCourseValidationException =
                new StudentSemesterCourseValidationException(invalidStudentSemesterCourseInputException);

            // when
            ValueTask <StudentSemesterCourse> createStudentSemesterCourseTask =
                this.studentSemesterCourseService.CreateStudentSemesterCourseAsync(inputStudentSemesterCourse);

            // then
            await Assert.ThrowsAsync <StudentSemesterCourseValidationException>(() =>
                                                                                createStudentSemesterCourseTask.AsTask());

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldDeleteStudentSemesterCourseAsync()
        {
            // given
            DateTimeOffset        dateTime = GetRandomDateTime();
            StudentSemesterCourse randomStudentSemesterCourse = CreateRandomStudentSemesterCourse(dateTime);
            Guid inputSemesterCourseId = randomStudentSemesterCourse.SemesterCourseId;
            Guid inputStudentId        = randomStudentSemesterCourse.StudentId;
            StudentSemesterCourse inputStudentSemesterCourse    = randomStudentSemesterCourse;
            StudentSemesterCourse storageStudentSemesterCourse  = inputStudentSemesterCourse;
            StudentSemesterCourse expectedStudentSemesterCourse = storageStudentSemesterCourse;

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentSemesterCourseByIdAsync(inputSemesterCourseId, inputStudentId))
            .ReturnsAsync(inputStudentSemesterCourse);

            this.storageBrokerMock.Setup(broker =>
                                         broker.DeleteStudentSemesterCourseAsync(inputStudentSemesterCourse))
            .ReturnsAsync(storageStudentSemesterCourse);

            // when
            StudentSemesterCourse actualStudentSemesterCourse =
                await this.studentSemesterCourseService.DeleteStudentSemesterCourseAsync(inputSemesterCourseId, inputStudentId);

            actualStudentSemesterCourse.Should().BeEquivalentTo(expectedStudentSemesterCourse);

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

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

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