Exemplo n.º 1
0
        public async Task ShouldThrowValidationExceptionOnModifyIfStorageCreatedByNotSameAsCreatedByAndLogItAsync()
        {
            // given
            int            randomNegativeMinutes = GetNegativeRandomNumber();
            Guid           differentId           = Guid.NewGuid();
            Guid           invalidCreatedBy      = differentId;
            DateTimeOffset randomDate            = GetRandomDateTime();
            Guardian       randomGuardian        = CreateRandomGuardian(randomDate);
            Guardian       invalidGuardian       = randomGuardian;

            invalidGuardian.CreatedDate = randomDate.AddMinutes(randomNegativeMinutes);
            Guardian storageGuardian = randomGuardian.DeepClone();
            Guid     guardianId      = invalidGuardian.Id;

            invalidGuardian.CreatedBy = invalidCreatedBy;

            var invalidGuardianInputException = new InvalidGuardianException(
                parameterName: nameof(Guardian.CreatedBy),
                parameterValue: invalidGuardian.CreatedBy);

            var expectedGuardianValidationException =
                new GuardianValidationException(invalidGuardianInputException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectGuardianByIdAsync(guardianId))
            .ReturnsAsync(storageGuardian);

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

            // when
            ValueTask <Guardian> modifyGuardianTask =
                this.guardianService.ModifyGuardianAsync(invalidGuardian);

            // then
            await Assert.ThrowsAsync <GuardianValidationException>(() =>
                                                                   modifyGuardianTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectGuardianByIdAsync(invalidGuardian.Id),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldModifyGuardianAsync()
        {
            // given
            int            randomNumber                = GetRandomNumber();
            int            randomDays                  = randomNumber;
            DateTimeOffset randomDate                  = GetRandomDateTime();
            DateTimeOffset randomInputDate             = GetRandomDateTime();
            Guardian       randomGuardian              = CreateRandomGuardian(randomInputDate);
            Guardian       inputGuardian               = randomGuardian;
            Guardian       afterUpdateStorageGuardian  = inputGuardian;
            Guardian       expectedGuardian            = afterUpdateStorageGuardian;
            Guardian       beforeUpdateStorageGuardian = randomGuardian.DeepClone();

            inputGuardian.UpdatedDate = randomDate;
            Guid guardianId = inputGuardian.Id;

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectGuardianByIdAsync(guardianId))
            .ReturnsAsync(beforeUpdateStorageGuardian);

            this.storageBrokerMock.Setup(broker =>
                                         broker.UpdateGuardianAsync(inputGuardian))
            .ReturnsAsync(afterUpdateStorageGuardian);

            // when
            Guardian actualGuardian =
                await this.guardianService.ModifyGuardianAsync(inputGuardian);

            // then
            actualGuardian.Should().BeEquivalentTo(expectedGuardian);

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectGuardianByIdAsync(guardianId),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.UpdateGuardianAsync(inputGuardian),
                                          Times.Once);

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