Exemple #1
0
        public async void ShouldThrowValidationExceptionOnAddWhenCalendarEntryIsNullAndLogItAsync()
        {
            // given
            CalendarEntry randomCalendarEntry = null;
            CalendarEntry nullCalendarEntry   = randomCalendarEntry;

            var nullCalendarEntryException = new NullCalendarEntryException();

            var expectedCalendarEntryValidationException =
                new CalendarEntryValidationException(nullCalendarEntryException);

            // when
            ValueTask <CalendarEntry> registerCalendarEntryTask =
                this.calendarEntryService.AddCalendarEntryAsync(nullCalendarEntry);

            // then
            await Assert.ThrowsAsync <CalendarEntryValidationException>(() =>
                                                                        registerCalendarEntryTask.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.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowValidationExceptionOnModifyWhenCalendarEntryIsNullAndLogItAsync()
        {
            //given
            CalendarEntry invalidCalendarEntry       = null;
            var           nullCalendarEntryException = new NullCalendarEntryException();

            var expectedCalendarEntryValidationException =
                new CalendarEntryValidationException(nullCalendarEntryException);

            //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 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();
        }
        private CalendarEntryValidationException CreateAndLogValidationException(Exception exception)
        {
            var calendarEntryValidationException = new CalendarEntryValidationException(exception);

            this.loggingBroker.LogError(calendarEntryValidationException);

            return(calendarEntryValidationException);
        }
        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();
        }
Exemple #7
0
        public async void ShouldThrowValidationExceptionOnAddWhenCalendarEntryAlreadyExistsAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime                   = GetRandomDateTime();
            CalendarEntry  randomCalendarEntry        = CreateRandomCalendarEntry(dateTime);
            CalendarEntry  alreadyExistsCalendarEntry = randomCalendarEntry;

            alreadyExistsCalendarEntry.UpdatedBy = alreadyExistsCalendarEntry.CreatedBy;
            string randomMessage         = GetRandomMessage();
            string exceptionMessage      = randomMessage;
            var    duplicateKeyException = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsCalendarEntryException =
                new AlreadyExistsCalendarEntryException(duplicateKeyException);

            var expectedCalendarEntryValidationException =
                new CalendarEntryValidationException(alreadyExistsCalendarEntryException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertCalendarEntryAsync(alreadyExistsCalendarEntry))
            .ThrowsAsync(duplicateKeyException);

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

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

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

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertCalendarEntryAsync(alreadyExistsCalendarEntry),
                                          Times.Once);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowValidationExceptionOnModifyIfCalendarEntryDoesntExistAndLogItAsync()
        {
            // given
            int            randomNegativeMinutes = GetNegativeRandomNumber();
            DateTimeOffset dateTime                 = GetRandomDateTime();
            CalendarEntry  randomCalendarEntry      = CreateRandomCalendarEntry(dateTime);
            CalendarEntry  nonExistentCalendarEntry = randomCalendarEntry;

            nonExistentCalendarEntry.CreatedDate = dateTime.AddMinutes(randomNegativeMinutes);
            CalendarEntry noCalendarEntry = null;

            var notFoundCalendarEntryException =
                new NotFoundCalendarEntryException(nonExistentCalendarEntry.Id);

            var expectedCalendarEntryValidationException =
                new CalendarEntryValidationException(notFoundCalendarEntryException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCalendarEntryByIdAsync(nonExistentCalendarEntry.Id))
            .ReturnsAsync(noCalendarEntry);

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

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

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

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCalendarEntryByIdAsync(nonExistentCalendarEntry.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();
        }
Exemple #10
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();
        }
Exemple #11
0
        public async Task ShouldThrowValidatonExceptionOnRemoveWhenStorageCalendarEntryIsInvalidAndLogItAsync()
        {
            // given
            Guid          randomCalendarEntryId       = Guid.NewGuid();
            Guid          inputCalendarEntryId        = randomCalendarEntryId;
            CalendarEntry invalidStorageCalendarEntry = null;

            var notFoundCalendarEntryException =
                new NotFoundCalendarEntryException(inputCalendarEntryId);

            var expectedCalendarEntryValidationException =
                new CalendarEntryValidationException(notFoundCalendarEntryException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCalendarEntryByIdAsync(inputCalendarEntryId))
            .ReturnsAsync(invalidStorageCalendarEntry);

            // when
            ValueTask <CalendarEntry> deleteCalendarEntryByIdTask =
                this.calendarEntryService.RemoveCalendarEntryByIdAsync(inputCalendarEntryId);

            // then
            await Assert.ThrowsAsync <CalendarEntryValidationException>(() =>
                                                                        deleteCalendarEntryByIdTask.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(inputCalendarEntryId),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #12
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();
        }
Exemple #13
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();
        }