public async Task ShouldThrowValidationExceptionOnModifyWhenCalendarEntryLabelIsInvalidAndLogItAsync(
            string invalidCalendarEntryLabel)
        {
            // given
            CalendarEntry randomCalendarEntry  = CreateRandomCalendarEntry(DateTime.Now);
            CalendarEntry invalidCalendarEntry = randomCalendarEntry;

            invalidCalendarEntry.Label = invalidCalendarEntryLabel;

            var invalidCalendarEntryInputException = new InvalidCalendarEntryException(
                parameterName: nameof(CalendarEntry.Label),
                parameterValue: invalidCalendarEntry.Label);

            var expectedCalendarEntryValidationException =
                new CalendarEntryValidationException(invalidCalendarEntryInputException);

            // when
            ValueTask <CalendarEntry> modifyCalendarEntryTask =
                this.calendarEntryService.ModifyCalendarEntryAsync(invalidCalendarEntry);

            // then
            await Assert.ThrowsAsync <CalendarEntryValidationException>(() =>
                                                                        modifyCalendarEntryTask.AsTask());

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnModifyWhenUpdatedDateIsSameAsCreatedDateAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime            = GetRandomDateTime();
            CalendarEntry  randomCalendarEntry = CreateRandomCalendarEntry(dateTime);
            CalendarEntry  inputCalendarEntry  = randomCalendarEntry;

            var invalidCalendarEntryInputException = new InvalidCalendarEntryException(
                parameterName: nameof(CalendarEntry.UpdatedDate),
                parameterValue: inputCalendarEntry.UpdatedDate);

            var expectedCalendarEntryValidationException =
                new CalendarEntryValidationException(invalidCalendarEntryInputException);

            // when
            ValueTask <CalendarEntry> modifyCalendarEntryTask =
                this.calendarEntryService.ModifyCalendarEntryAsync(inputCalendarEntry);

            // then
            await Assert.ThrowsAsync <CalendarEntryValidationException>(() =>
                                                                        modifyCalendarEntryTask.AsTask());

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowValidationExceptionOnModifyIfStorageCreatedByNotSameAsCreatedByAndLogItAsync()
        {
            // given
            int            randomNegativeMinutes = GetNegativeRandomNumber();
            Guid           differentId           = Guid.NewGuid();
            Guid           invalidCreatedBy      = differentId;
            DateTimeOffset randomDate            = GetRandomDateTime();
            CalendarEntry  randomCalendarEntry   = CreateRandomCalendarEntry(randomDate);
            CalendarEntry  invalidCalendarEntry  = randomCalendarEntry;

            invalidCalendarEntry.CreatedDate = randomDate.AddMinutes(randomNegativeMinutes);
            CalendarEntry storageCalendarEntry = randomCalendarEntry.DeepClone();
            Guid          calendarEntryId      = invalidCalendarEntry.Id;

            invalidCalendarEntry.CreatedBy = invalidCreatedBy;

            var invalidCalendarEntryInputException = new InvalidCalendarEntryException(
                parameterName: nameof(CalendarEntry.CreatedBy),
                parameterValue: invalidCalendarEntry.CreatedBy);

            var expectedCalendarEntryValidationException =
                new CalendarEntryValidationException(invalidCalendarEntryInputException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCalendarEntryByIdAsync(calendarEntryId))
            .ReturnsAsync(storageCalendarEntry);

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

            // when
            ValueTask <CalendarEntry> modifyCalendarEntryTask =
                this.calendarEntryService.ModifyCalendarEntryAsync(invalidCalendarEntry);

            // then
            await Assert.ThrowsAsync <CalendarEntryValidationException>(() =>
                                                                        modifyCalendarEntryTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCalendarEntryByIdAsync(invalidCalendarEntry.Id),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnModifyWhenEndDateIsBeforeStartDateAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime            = GetRandomDateTime();
            CalendarEntry  randomCalendarEntry = CreateRandomCalendarEntry(dateTime);
            CalendarEntry  inputCalendarEntry  = randomCalendarEntry;
            int            randomMinutes       = GetRandomNumber();

            inputCalendarEntry.UpdatedDate = dateTime.AddMinutes(randomMinutes);
            inputCalendarEntry.StartDate   = dateTime.AddMinutes(randomMinutes);
            var invalidCalendarEntryException = new InvalidCalendarEntryException();

            invalidCalendarEntryException.AddData(
                key: nameof(CalendarEntry.EndDate),
                values: $"Date is before StartDate");

            var expectedCalendarEntryValidationException =
                new CalendarEntryValidationException(invalidCalendarEntryException);

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

            // when
            ValueTask <CalendarEntry> modifyCalendarEntryTask =
                this.calendarEntryService.ModifyCalendarEntryAsync(inputCalendarEntry);

            // then
            await Assert.ThrowsAsync <CalendarEntryValidationException>(() =>
                                                                        modifyCalendarEntryTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 5
0
        public async void ShouldThrowValidationExceptionOnCreateWhenCreatedDateIsNotRecentAndLogItAsync(
            int minutes)
        {
            // given
            DateTimeOffset randomDate           = GetRandomDateTime();
            CalendarEntry  randomCalendarEntry  = CreateRandomCalendarEntry(randomDate);
            CalendarEntry  invalidCalendarEntry = randomCalendarEntry;

            invalidCalendarEntry.CreatedDate = randomDate.AddMinutes(minutes);
            invalidCalendarEntry.UpdatedDate = invalidCalendarEntry.CreatedDate;
            var invalidCalendarEntryException = new InvalidCalendarEntryException();

            invalidCalendarEntryException.AddData(
                key: nameof(CalendarEntry.CreatedDate),
                values: $"Date is not recent");

            var expectedCalendarEntryValidationException =
                new CalendarEntryValidationException(invalidCalendarEntryException);

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

            // when
            ValueTask <CalendarEntry> createCalendarEntryTask =
                this.calendarEntryService.AddCalendarEntryAsync(invalidCalendarEntry);

            // then
            await Assert.ThrowsAsync <CalendarEntryValidationException>(() =>
                                                                        createCalendarEntryTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 6
0
        public async Task ShouldThrowValidatonExceptionOnRemoveWhenIdIsInvalidAndLogItAsync()
        {
            // given
            Guid invalidCalendarEntryId = Guid.Empty;

            var invalidCalendarEntryException = new InvalidCalendarEntryException(
                parameterName: nameof(CalendarEntry.Id),
                parameterValue: invalidCalendarEntryId);

            var expectedCalendarEntryValidationException =
                new CalendarEntryValidationException(invalidCalendarEntryException);

            // when
            ValueTask <CalendarEntry> deleteCalendarEntryTask =
                this.calendarEntryService.RemoveCalendarEntryByIdAsync(invalidCalendarEntryId);

            // then
            await Assert.ThrowsAsync <CalendarEntryValidationException>(() => deleteCalendarEntryTask.AsTask());

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

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 7
0
        public async Task ShouldThrowValidationExceptionOnAddWhenCalendarEntryDescriptionIsInvalidAndLogItAsync(
            string invalidCalendarEntryDescription)
        {
            // given
            DateTimeOffset dateTime             = GetRandomDateTime();
            CalendarEntry  randomCalendarEntry  = CreateRandomCalendarEntry(dateTime);
            CalendarEntry  invalidCalendarEntry = randomCalendarEntry;

            invalidCalendarEntry.Description = invalidCalendarEntryDescription;

            var invalidCalendarEntryException = new InvalidCalendarEntryException(
                parameterName: nameof(CalendarEntry.Description),
                parameterValue: invalidCalendarEntry.Description);

            var expectedCalendarEntryValidationException =
                new CalendarEntryValidationException(invalidCalendarEntryException);

            // when
            ValueTask <CalendarEntry> createCalendarEntryTask =
                this.calendarEntryService.AddCalendarEntryAsync(invalidCalendarEntry);

            // then
            await Assert.ThrowsAsync <CalendarEntryValidationException>(() =>
                                                                        createCalendarEntryTask.AsTask());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnModifyIfCalendarEntryIsInvalidAndLogItAsync(
            string invalidText)
        {
            // given
            var invalidCalendarEntry = new CalendarEntry
            {
                Label       = invalidText,
                Description = invalidText
            };

            var invalidCalendarEntryException = new InvalidCalendarEntryException();

            invalidCalendarEntryException.AddData(
                key: nameof(CalendarEntry.Id),
                values: "Id is required");

            invalidCalendarEntryException.AddData(
                key: nameof(CalendarEntry.Label),
                values: "Text is required");

            invalidCalendarEntryException.AddData(
                key: nameof(CalendarEntry.Description),
                values: "Text is required");

            invalidCalendarEntryException.AddData(
                key: nameof(CalendarEntry.StartDate),
                values: "Date is required");

            invalidCalendarEntryException.AddData(
                key: nameof(CalendarEntry.EndDate),
                values: "Date is required");

            invalidCalendarEntryException.AddData(
                key: nameof(CalendarEntry.RemindAtDateTime),
                values: "Date is required");

            invalidCalendarEntryException.AddData(
                key: nameof(CalendarEntry.CreatedDate),
                values: "Date is required");

            invalidCalendarEntryException.AddData(
                key: nameof(CalendarEntry.UpdatedDate),
                values: new string[] {
                "Date is required",
                $"Date is the same as {nameof(CalendarEntry.CreatedDate)}"
            });

            invalidCalendarEntryException.AddData(
                key: nameof(CalendarEntry.CreatedBy),
                values: "Id is required");

            invalidCalendarEntryException.AddData(
                key: nameof(CalendarEntry.UpdatedBy),
                values: "Id is required");

            var expectedCalendarEntryValidationException =
                new CalendarEntryValidationException(invalidCalendarEntryException);

            // when
            ValueTask <CalendarEntry> createCalendarEntryTask =
                this.calendarEntryService.ModifyCalendarEntryAsync(invalidCalendarEntry);

            // then
            await Assert.ThrowsAsync <CalendarEntryValidationException>(() =>
                                                                        createCalendarEntryTask.AsTask());

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

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

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