public async Task ShouldThrowValidationExceptionOnModifyWhenGuardianIdIsInvalidAndLogItAsync()
        {
            //given
            Guid            invalidStudentGuardianId = Guid.Empty;
            DateTimeOffset  dateTime = GetRandomDateTime();
            StudentGuardian randomStudentGuardian  = CreateRandomStudentGuardian(dateTime);
            StudentGuardian invalidStudentGuardian = randomStudentGuardian;

            invalidStudentGuardian.GuardianId = invalidStudentGuardianId;

            var invalidStudentGuardianInputException = new InvalidStudentGuardiantException(
                parameterName: nameof(StudentGuardian.GuardianId),
                parameterValue: invalidStudentGuardian.GuardianId);

            var expectedStudentGuardianValidationException =
                new StudentGuardianValidationException(invalidStudentGuardianInputException);

            //when
            ValueTask <StudentGuardian> modifyStudentGuardianTask =
                this.studentGuardianService.ModifyStudentGuardianAsync(invalidStudentGuardian);

            //then
            await Assert.ThrowsAsync <StudentGuardianValidationException>(() =>
                                                                          modifyStudentGuardianTask.AsTask());

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnModifyWhenUpdatedDateIsSameAsCreatedDateAndLogItAsync()
        {
            // given
            DateTimeOffset  dateTime = GetRandomDateTime();
            StudentGuardian randomStudentGuardian = CreateRandomStudentGuardian(dateTime);
            StudentGuardian inputStudentGuardian  = randomStudentGuardian;

            var invalidStudentGuardianInputException = new InvalidStudentGuardiantException(
                parameterName: nameof(StudentGuardian.UpdatedDate),
                parameterValue: inputStudentGuardian.UpdatedDate);

            var expectedStudentGuardianValidationException =
                new StudentGuardianValidationException(invalidStudentGuardianInputException);

            // when
            ValueTask <StudentGuardian> modifyStudentGuardianTask =
                this.studentGuardianService.ModifyStudentGuardianAsync(inputStudentGuardian);

            // then
            await Assert.ThrowsAsync <StudentGuardianValidationException>(() =>
                                                                          modifyStudentGuardianTask.AsTask());

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Ejemplo n.º 3
0
        public async Task ShouldThrowValidatonExceptionOnRetrieveWhenGuardianIdIsInvalidAndLogItAsync()
        {
            // given
            Guid randomGuardianId = default;
            Guid randomStudentId  = Guid.NewGuid();
            Guid inputGuardianId  = randomGuardianId;
            Guid inputStudentId   = randomStudentId;

            var invalidStudentGuardianInputException = new InvalidStudentGuardiantException(
                parameterName: nameof(StudentGuardian.GuardianId),
                parameterValue: inputGuardianId);

            var expectedStudentGuardianValidationException =
                new StudentGuardianValidationException(invalidStudentGuardianInputException);

            // when
            ValueTask <StudentGuardian> actualStudentGuardianTask =
                this.studentGuardianService.RetrieveStudentGuardianByIdAsync(inputStudentId, inputGuardianId);

            // then
            await Assert.ThrowsAsync <StudentGuardianValidationException>(() => actualStudentGuardianTask.AsTask());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowValidationExceptionOnModifyIfStorageCreatedDateNotSameAsCreateDateAndLogItAsync()
        {
            // given
            int             randomNumber           = GetRandomNumber();
            int             randomMinutes          = randomNumber;
            DateTimeOffset  randomDate             = GetRandomDateTime();
            StudentGuardian randomStudentGuardian  = CreateRandomStudentGuardian(randomDate);
            StudentGuardian invalidStudentGuardian = randomStudentGuardian;

            invalidStudentGuardian.UpdatedDate = randomDate;
            StudentGuardian storageStudentGuardian = randomStudentGuardian.DeepClone();
            Guid            studentId        = invalidStudentGuardian.StudentId;
            Guid            semesterCourseId = invalidStudentGuardian.GuardianId;

            invalidStudentGuardian.CreatedDate = storageStudentGuardian.CreatedDate.AddMinutes(randomNumber);

            var invalidStudentGuardianInputException = new InvalidStudentGuardiantException(
                parameterName: nameof(StudentGuardian.CreatedDate),
                parameterValue: invalidStudentGuardian.CreatedDate);

            var expectedStudentGuardianValidationException =
                new StudentGuardianValidationException(invalidStudentGuardianInputException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentGuardianByIdAsync(studentId, semesterCourseId))
            .ReturnsAsync(storageStudentGuardian);

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

            // when
            ValueTask <StudentGuardian> modifyStudentGuardianTask =
                this.studentGuardianService.ModifyStudentGuardianAsync(invalidStudentGuardian);

            // then
            await Assert.ThrowsAsync <StudentGuardianValidationException>(() =>
                                                                          modifyStudentGuardianTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentGuardianByIdAsync(
                                              invalidStudentGuardian.StudentId,
                                              invalidStudentGuardian.GuardianId),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnAddWhenCreatedDateIsNotRecentAndLogItAsync(
            int invalidMinutes)
        {
            // given
            DateTimeOffset  dateTime = GetRandomDateTime();
            StudentGuardian randomStudentGuardian = CreateRandomStudentGuardian(dateTime);
            StudentGuardian inputStudentGuardian  = randomStudentGuardian;

            inputStudentGuardian.UpdatedBy   = inputStudentGuardian.CreatedBy;
            inputStudentGuardian.CreatedDate = dateTime.AddMinutes(invalidMinutes);
            inputStudentGuardian.UpdatedDate = inputStudentGuardian.CreatedDate;

            var invalidStudentGuardianInputException =
                new InvalidStudentGuardiantException(
                    parameterName: nameof(StudentGuardian.CreatedDate),
                    parameterValue: inputStudentGuardian.CreatedDate);

            var expectedStudentGuardianValidationException =
                new StudentGuardianValidationException(invalidStudentGuardianInputException);

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

            // when
            ValueTask <StudentGuardian> createStudentGuardianTask =
                this.studentGuardianService.AddStudentGuardianAsync(inputStudentGuardian);

            // then
            await Assert.ThrowsAsync <StudentGuardianValidationException>(() =>
                                                                          createStudentGuardianTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnAddWhenStudentIdIsInvalidAndLogItAsync()
        {
            // given
            DateTimeOffset  dateTime = GetRandomDateTime();
            StudentGuardian randomStudentGuardian = CreateRandomStudentGuardian(dateTime);
            StudentGuardian inputStudentGuardian  = randomStudentGuardian;

            inputStudentGuardian.StudentId = default;

            var invalidStudentGuardianInputException = new InvalidStudentGuardiantException(
                parameterName: nameof(StudentGuardian.StudentId),
                parameterValue: inputStudentGuardian.StudentId);

            var expectedStudentGuardianValidationException =
                new StudentGuardianValidationException(invalidStudentGuardianInputException);

            // when
            ValueTask <StudentGuardian> addStudentGuardianTask =
                this.studentGuardianService.AddStudentGuardianAsync(inputStudentGuardian);

            // then
            await Assert.ThrowsAsync <StudentGuardianValidationException>(() =>
                                                                          addStudentGuardianTask.AsTask());

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

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

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