コード例 #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 Task ShouldModifyFeeAsync()
        {
            // given
            int            randomNumber           = GetRandomNumber();
            int            randomDays             = randomNumber;
            DateTimeOffset randomDate             = GetRandomDateTime();
            DateTimeOffset randomInputDate        = GetRandomDateTime();
            Fee            randomFee              = CreateRandomFee(randomInputDate);
            Fee            inputFee               = randomFee;
            Fee            afterUpdateStorageFee  = inputFee;
            Fee            expectedFee            = afterUpdateStorageFee;
            Fee            beforeUpdateStorageFee = randomFee.DeepClone();

            inputFee.UpdatedDate = randomDate;
            Guid feeId = inputFee.Id;

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

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.UpdateFeeAsync(inputFee))
            .ReturnsAsync(afterUpdateStorageFee);

            // when
            Fee actualFee =
                await this.feeService.ModifyFeeAsync(inputFee);

            // then
            actualFee.Should().BeEquivalentTo(expectedFee);

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

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.UpdateFeeAsync(inputFee),
                                          Times.Once);

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