Exemple #1
0
        public async Task ShouldThrowValidationExceptionOnModifyWhenExamIsInvalidAndLogItAsyn()
        {
            // given
            var invalidExam          = new Exam();
            var invalidExamException = new InvalidExamException();

            invalidExamException.AddData(
                key: nameof(Exam.Id),
                values: "Id is required");

            invalidExamException.AddData(
                key: nameof(Exam.CreatedBy),
                values: "Id is required");

            invalidExamException.AddData(
                key: nameof(Exam.CreatedDate),
                values: "Date is required");

            invalidExamException.AddData(
                key: nameof(Exam.UpdatedBy),
                values: "Id is required");

            invalidExamException.UpsertDataList(
                key: nameof(Exam.UpdatedDate),
                value: "Date is required");

            invalidExamException.UpsertDataList(
                key: nameof(Exam.UpdatedDate),
                value: $"Date is same as {nameof(Exam.CreatedDate)}");

            var expectedExamValidationException =
                new ExamValidationException(invalidExamException);

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

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

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #2
0
        public async Task ShouldThrowValidationExceptionOnModifyIfStorageUpdatedDateSameAsUpdatedDateAndLogItAsync()
        {
            // given
            int            randomNegativeMinutes = GetNegativeRandomNumber();
            int            minutesInThePast      = randomNegativeMinutes;
            DateTimeOffset randomDate            = GetRandomDateTime();
            Exam           randomExam            = CreateRandomExam(randomDate);

            randomExam.CreatedDate = randomExam.CreatedDate.AddMinutes(minutesInThePast);
            Exam invalidExam = randomExam;

            invalidExam.UpdatedDate = randomDate;
            Exam storageExam = randomExam.DeepClone();
            Guid ExamId      = invalidExam.Id;

            var invalidExamException = new InvalidExamException();

            invalidExamException.AddData(
                key: nameof(Exam.UpdatedDate),
                values: $"Date is same as {nameof(Exam.UpdatedDate)}");

            var expectedExamValidationException =
                new ExamValidationException(invalidExamException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectExamByIdAsync(ExamId))
            .ReturnsAsync(storageExam);

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

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

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

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnAddIfExamIsInvalidAndLogItAsync()
        {
            // given
            var invalidExam          = new Exam();
            var invalidExamException = new InvalidExamException();

            invalidExamException.AddData(
                key: nameof(Exam.Id),
                values: "Id is required");

            invalidExamException.AddData(
                key: nameof(Exam.CreatedBy),
                values: "Id is required");

            invalidExamException.AddData(
                key: nameof(Exam.CreatedDate),
                values: "Date is required");

            invalidExamException.AddData(
                key: nameof(Exam.UpdatedBy),
                values: "Id is required");

            invalidExamException.AddData(
                key: nameof(Exam.UpdatedDate),
                values: "Date is required");

            var expectedExamValidationException =
                new ExamValidationException(invalidExamException);

            // when
            ValueTask <Exam> createExamTask =
                this.examService.AddExamAsync(invalidExam);

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

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #4
0
        public async void ShouldThrowValidationExceptionOnAddWhenCreatedDateIsNotRecentAndLogItAsync(
            int minutes)
        {
            // given
            DateTimeOffset dateTime   = GetRandomDateTime();
            Exam           randomExam = CreateRandomExam(dateTime);
            Exam           inputExam  = randomExam;

            inputExam.UpdatedBy   = inputExam.CreatedBy;
            inputExam.CreatedDate = dateTime.AddMinutes(minutes);
            inputExam.UpdatedDate = inputExam.CreatedDate;

            var invalidExamException = new InvalidExamException();

            invalidExamException.AddData(
                key: nameof(Exam.CreatedDate),
                values: "Date is not recent");

            var expectedExamValidationException =
                new ExamValidationException(invalidExamException);

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

            // when
            ValueTask <Exam> createExamTask =
                this.examService.AddExamAsync(inputExam);

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

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #5
0
        public async void ShouldThrowValidationExceptionOnModifyWhenUpdatedDateIsSameAsCreatedDateAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime   = GetRandomDateTime();
            Exam           randomExam = CreateRandomExam(dateTime);
            Exam           inputExam  = randomExam;

            var invalidExamException = new InvalidExamException();

            invalidExamException.AddData(
                key: nameof(Exam.UpdatedDate),
                values: $"Date is same as {nameof(Exam.CreatedDate)}");

            var expectedExamValidationException =
                new ExamValidationException(invalidExamException);

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

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

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

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Exemple #6
0
        public async void ShouldThrowValidationExceptionOnAddWhenUpdatedByIsNotSameToCreatedByAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime   = GetRandomDateTime();
            Exam           randomExam = CreateRandomExam(dateTime);
            Exam           inputExam  = randomExam;

            inputExam.UpdatedBy = Guid.NewGuid();

            var invalidExamException = new InvalidExamException();

            invalidExamException.AddData(
                key: nameof(Exam.UpdatedBy),
                values: $"Id is not same as {nameof(Exam.CreatedBy)}");

            var expectedExamValidationException =
                new ExamValidationException(invalidExamException);

            // when
            ValueTask <Exam> createExamTask =
                this.examService.AddExamAsync(inputExam);

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

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #7
0
        public async void ShouldThrowValidationExceptionOnRetrieveByIdWhenIdIsInvalidAndLogItAsync()
        {
            // given
            Guid randomExamId = default;
            Guid inputExamId  = randomExamId;

            var invalidExamInputException = new InvalidExamException();

            invalidExamInputException.AddData(
                key: nameof(Exam.Id),
                values: "Id is required");

            var expectedExamValidationException =
                new ExamValidationException(invalidExamInputException);

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

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

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #8
0
        public async Task ShouldThrowValidatonExceptionOnDeleteIfIdIsInvalidAndLogItAsync()
        {
            // given
            Guid randomExamId = Guid.Empty;
            Guid inputExamId  = randomExamId;

            var invalidExamInputException = new InvalidExamException();

            invalidExamInputException.AddData(
                key: nameof(Exam.Id),
                values: "Id is required");

            var expectedExamValidationException =
                new ExamValidationException(invalidExamInputException);

            // 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(It.IsAny <Guid>()),
                                          Times.Never);

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

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