コード例 #1
0
        public async Task ShouldThrowValidationExceptionOnModifyIfStorageCreatedByNotSameAsCreatedByAndLogItAsync()
        {
            // given
            int            randomNegativeMinutes = GetNegativeRandomNumber();
            Guid           differentId           = Guid.NewGuid();
            Guid           invalidCreatedBy      = differentId;
            DateTimeOffset randomDate            = GetRandomDateTime();
            Fee            randomFee             = CreateRandomFee(randomDate);

            randomFee.UpdatedDate = GetRandomDateTime();
            Fee invalidFee = randomFee;

            invalidFee.CreatedDate = randomDate.AddMinutes(randomNegativeMinutes);
            Fee  storageFee = randomFee.DeepClone();
            Guid feeId      = invalidFee.Id;

            invalidFee.CreatedBy = invalidCreatedBy;
            var invalidFeeException = new InvalidFeeException();

            invalidFeeException.AddData(
                key: nameof(Fee.CreatedBy),
                values: $"Id is not the same as {nameof(Fee.CreatedBy)}");

            var expectedFeeValidationException =
                new FeeValidationException(invalidFeeException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectFeeByIdAsync(feeId))
            .ReturnsAsync(storageFee);

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

            // when
            ValueTask <Fee> modifyFeeTask =
                this.feeService.ModifyFeeAsync(invalidFee);

            // then
            await Assert.ThrowsAsync <FeeValidationException>(() =>
                                                              modifyFeeTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectFeeByIdAsync(invalidFee.Id),
                                          Times.Once);

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
コード例 #2
0
        public async Task ShouldThrowValidationExceptionOnModifyIfStorageUpdatedDateSameAsUpdatedDateAndLogItAsync()
        {
            // given
            int            randomNegativeMinutes = GetNegativeRandomNumber();
            int            minutesInThePast      = randomNegativeMinutes;
            DateTimeOffset randomDate            = GetRandomDateTime();
            Fee            randomFee             = CreateRandomFee(randomDate);

            randomFee.CreatedDate = randomFee.CreatedDate.AddMinutes(minutesInThePast);
            Fee invalidFee = randomFee;

            invalidFee.UpdatedDate = randomDate;
            Fee  storageFee = randomFee.DeepClone();
            Guid feeId      = invalidFee.Id;

            var invalidFeeInputException = new InvalidFeeException(
                parameterName: nameof(Fee.UpdatedDate),
                parameterValue: invalidFee.UpdatedDate);

            var expectedFeeValidationException =
                new FeeValidationException(invalidFeeInputException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectFeeByIdAsync(feeId))
            .ReturnsAsync(storageFee);

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

            // when
            ValueTask <Fee> modifyFeeTask =
                this.feeService.ModifyFeeAsync(invalidFee);

            // then
            await Assert.ThrowsAsync <FeeValidationException>(() =>
                                                              modifyFeeTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectFeeByIdAsync(invalidFee.Id),
                                          Times.Once);

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
コード例 #3
0
        public async void ShouldThrowValidationExceptionOnModifyIfUpdatedDateIsNotRecentAndLogItAsync(
            int minutes)
        {
            // given
            DateTimeOffset dateTime  = GetRandomDateTime();
            Fee            randomFee = CreateRandomFee(dateTime);
            Fee            inputFee  = randomFee;

            inputFee.UpdatedBy   = inputFee.CreatedBy;
            inputFee.UpdatedDate = dateTime.AddMinutes(minutes);
            var invalidFeeInputException = new InvalidFeeException();

            invalidFeeInputException.AddData(
                key: nameof(Fee.UpdatedDate),
                values: "Date is not recent");

            var expectedFeeValidationException =
                new FeeValidationException(invalidFeeInputException);

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

            // when
            ValueTask <Fee> modifyFeeTask =
                this.feeService.ModifyFeeAsync(inputFee);

            // then
            await Assert.ThrowsAsync <FeeValidationException>(() =>
                                                              modifyFeeTask.AsTask());

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

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
コード例 #4
0
        public async void ShouldThrowValidationExceptionOnAddIfUpdatedDateIsNotSameToCreatedDateAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime   = GetRandomDateTime();
            Fee            randomFee  = CreateRandomFee(dateTime);
            Fee            invalidFee = randomFee;

            invalidFee.UpdatedBy   = randomFee.CreatedBy;
            invalidFee.UpdatedDate = GetRandomDateTime();
            var invalidFeeInputException = new InvalidFeeException();

            invalidFeeInputException.AddData(
                key: nameof(Fee.UpdatedDate),
                values: $"Date is not the same as {nameof(Fee.CreatedDate)}");

            var expectedFeeValidationException =
                new FeeValidationException(invalidFeeInputException);

            // when
            ValueTask <Fee> addFeeTask =
                this.feeService.AddFeeAsync(invalidFee);

            // then
            await Assert.ThrowsAsync <FeeValidationException>(() =>
                                                              addFeeTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
コード例 #5
0
        public async Task ShouldThrowValidationExceptionOnModifyWhenFeeIdIsInvalidAndLogItAsync()
        {
            //given
            Guid           invalidFeeId = Guid.Empty;
            DateTimeOffset dateTime     = GetRandomDateTime();
            Fee            randomFee    = CreateRandomFee(dateTime);
            Fee            invalidFee   = randomFee;

            invalidFee.Id = invalidFeeId;

            var invalidFeeException = new InvalidFeeException(
                parameterName: nameof(Fee.Id),
                parameterValue: invalidFee.Id);

            var expectedFeeValidationException =
                new FeeValidationException(invalidFeeException);

            //when
            ValueTask <Fee> modifyFeeTask =
                this.feeService.ModifyFeeAsync(invalidFee);

            //then
            await Assert.ThrowsAsync <FeeValidationException>(() =>
                                                              modifyFeeTask.AsTask());

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
コード例 #6
0
        public async void ShouldThrowValidationExceptionOnRetrieveByIdWhenIdIsInvalidAndLogItAsync()
        {
            //given
            Guid randomFeeId         = default;
            Guid inputFeeId          = randomFeeId;
            var  invalidFeeException = new InvalidFeeException();

            invalidFeeException.AddData(
                key: nameof(Fee.Id),
                values: "Id is required");

            var expectedFeeValidationException =
                new FeeValidationException(invalidFeeException);

            //when
            ValueTask <Fee> retrieveFeeByIdTask =
                this.feeService.RetrieveFeeByIdAsync(inputFeeId);

            //then
            await Assert.ThrowsAsync <FeeValidationException>(() => retrieveFeeByIdTask.AsTask());

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
コード例 #7
0
        public async Task ShouldThrowValidationExceptionOnAddWhenFeeLabelIsInvalidAndLogItAsync
            (string invalidFeeLabel)
        {
            // given
            DateTimeOffset dateTime   = GetRandomDateTime();
            Fee            randomFee  = CreateRandomFee(dateTime);
            Fee            invalidFee = randomFee;

            invalidFee.Label = invalidFeeLabel;

            var invalidFeeException = new InvalidFeeException(
                parameterName: nameof(Fee.Label),
                parameterValue: invalidFee.Label);

            var expectedFeeValidationException =
                new FeeValidationException(invalidFeeException);

            // when
            ValueTask <Fee> createFeeTask =
                this.feeService.AddFeeAsync(invalidFee);

            // then
            await Assert.ThrowsAsync <FeeValidationException>(() =>
                                                              createFeeTask.AsTask());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
コード例 #8
0
        public async Task ShouldThrowValidatonExceptionOnRemoveWhenIdIsInvalidAndLogItAsync()
        {
            // given
            Guid randomFeeId = default;
            Guid inputFeeId  = randomFeeId;

            var invalidFeeInputException = new InvalidFeeException(
                parameterName: nameof(Fee.Id),
                parameterValue: inputFeeId);

            var expectedFeeValidationException =
                new FeeValidationException(invalidFeeInputException);

            // when
            ValueTask <Fee> actualFeeTask =
                this.feeService.RemoveFeeByIdAsync(inputFeeId);

            // then
            await Assert.ThrowsAsync <FeeValidationException>(() => actualFeeTask.AsTask());

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
コード例 #9
0
        public async Task ShouldThrowValidationExceptionOnModifyIfFeeIsInvalidAndLogItAsync(string invalidText)
        {
            //given
            var invalidFee = new Fee
            {
                Label = invalidText
            };

            var invalidFeeException = new InvalidFeeException();

            invalidFeeException.AddData(
                key: nameof(Fee.Id),
                values: "Id is required");

            invalidFeeException.AddData(
                key: nameof(Fee.Label),
                values: "Text is required");

            invalidFeeException.AddData(
                key: nameof(Fee.CreatedBy),
                values: "Id is required");

            invalidFeeException.AddData(
                key: nameof(Fee.UpdatedBy),
                values: "Id is required");

            invalidFeeException.AddData(
                key: nameof(Fee.CreatedDate),
                values: "Date is required");

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

            invalidFeeException.UpsertDataList(
                key: nameof(Fee.UpdatedDate),
                value: "Date is required");

            var expectedFeeValidationException =
                new FeeValidationException(invalidFeeException);

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

            //when
            ValueTask <Fee> modifyFeeTask =
                this.feeService.ModifyFeeAsync(invalidFee);

            //then
            await Assert.ThrowsAsync <FeeValidationException>(() =>
                                                              modifyFeeTask.AsTask());

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

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

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

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

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