public async void ShouldThrowValidationExceptionOnRetrieveByIdWhenIdIsInvalidAndLogItAsync() { //given Guid randomCourseId = default; Guid inputCourseId = randomCourseId; var invalidCourseException = new InvalidCourseException( parameterName: nameof(Course.Id), parameterValue: inputCourseId); var expectedCourseValidationException = new CourseValidationException(invalidCourseException); //when ValueTask <Course> retrieveCourseByIdTask = this.courseService.RetrieveCourseByIdAsync(inputCourseId); //then await Assert.ThrowsAsync <CourseValidationException>(() => retrieveCourseByIdTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCourseValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCourseByIdAsync(It.IsAny <Guid>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnModifyWhenUpdatedDateIsSameAsCreatedDateAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Course randomCourse = CreateRandomCourse(dateTime); Course inputCourse = randomCourse; var invalidCourseException = new InvalidCourseException( parameterName: nameof(Course.UpdatedDate), parameterValue: inputCourse.UpdatedDate); var expectedCourseValidationException = new CourseValidationException(invalidCourseException); // when ValueTask <Course> modifyCourseTask = this.courseService.ModifyCourseAsync(inputCourse); // then await Assert.ThrowsAsync <CourseValidationException>(() => modifyCourseTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCourseValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.UpdateCourseAsync(It.IsAny <Course>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidationExceptionOnModifyIfStorageCreatedByNotSameAsCreatedByAndLogItAsync() { // given int randomNegativeMinutes = GetNegativeRandomNumber(); Guid differentId = Guid.NewGuid(); Guid invalidCreatedBy = differentId; DateTimeOffset randomDate = GetRandomDateTime(); Course randomCourse = CreateRandomCourse(randomDate); Course invalidCourse = randomCourse; invalidCourse.CreatedDate = randomDate.AddMinutes(randomNegativeMinutes); Course storageCourse = randomCourse.DeepClone(); Guid courseId = invalidCourse.Id; invalidCourse.CreatedBy = invalidCreatedBy; var invalidCourseException = new InvalidCourseException( parameterName: nameof(Course.CreatedBy), parameterValue: invalidCourse.CreatedBy); var expectedCourseValidationException = new CourseValidationException(invalidCourseException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDate); this.storageBrokerMock.Setup(broker => broker.SelectCourseByIdAsync(courseId)) .ReturnsAsync(storageCourse); // when ValueTask <Course> modifyCourseTask = this.courseService.ModifyCourseAsync(invalidCourse); // then await Assert.ThrowsAsync <CourseValidationException>(() => modifyCourseTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCourseByIdAsync(invalidCourse.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCourseValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.UpdateCourseAsync(It.IsAny <Course>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnCreateWhenCreatedDateIsNotRecentAndLogItAsync( int minutes) { // given DateTimeOffset dateTime = GetRandomDateTime(); Course randomCourse = CreateRandomCourse(dateTime); Course inputCourse = randomCourse; inputCourse.UpdatedBy = inputCourse.CreatedBy; inputCourse.CreatedDate = dateTime.AddMinutes(minutes); inputCourse.UpdatedDate = inputCourse.CreatedDate; var invalidCourseException = new InvalidCourseException(); invalidCourseException.AddData( key: nameof(Course.CreatedDate), values: $"Date is not recent"); var expectedCourseValidationException = new CourseValidationException(invalidCourseException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); // when ValueTask <Course> createCourseTask = this.courseService.CreateCourseAsync(inputCourse); // then await Assert.ThrowsAsync <CourseValidationException>(() => createCourseTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCourseValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCourseByIdAsync(It.IsAny <Guid>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnCreateIfCourseStatusIsInvalidAndLogItAsync() { // given DateTimeOffset randomDateTime = GetRandomDateTime(); Course randomCourse = CreateRandomCourse(randomDateTime); Course invalidCourse = randomCourse; invalidCourse.Status = GetInvalidEnum <CourseStatus>(); var invalidCourseException = new InvalidCourseException(); invalidCourseException.AddData( key: nameof(Course.Status), values: "Value is not recognized"); var expectedCourseValidationException = new CourseValidationException(invalidCourseException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()). Returns(randomDateTime); // when ValueTask <Course> createCourseTask = this.courseService.CreateCourseAsync(invalidCourse); // then await Assert.ThrowsAsync <CourseValidationException>(() => createCourseTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCourseValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertCourseAsync(It.IsAny <Course>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidationExceptionOnModifyWhenCourseDescriptionIsInvalidAndLogItAsync( string invalidCourseDescription) { // given Course randomCourse = CreateRandomCourse(DateTime.Now); Course invalidCourse = randomCourse; invalidCourse.Description = invalidCourseDescription; var invalidCourseException = new InvalidCourseException( parameterName: nameof(Course.Description), parameterValue: invalidCourse.Description); var expectedCourseValidationException = new CourseValidationException(invalidCourseException); // when ValueTask <Course> modifyCourseTask = this.courseService.ModifyCourseAsync(invalidCourse); // then await Assert.ThrowsAsync <CourseValidationException>(() => modifyCourseTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCourseValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.UpdateCourseAsync(It.IsAny <Course>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnCreateWhenCourseIsInvalidAndLogItAsync( string invalidText) { // given var invalidCourse = new Course { Name = invalidText }; var invalidCourseException = new InvalidCourseException(); invalidCourseException.AddData( key: nameof(Course.Id), values: "Id is required"); invalidCourseException.AddData( key: nameof(Course.Name), values: "Text is required"); invalidCourseException.AddData( key: nameof(Course.Description), values: "Text is required"); invalidCourseException.AddData( key: nameof(Course.CreatedBy), values: "Id is required"); invalidCourseException.AddData( key: nameof(Course.UpdatedBy), values: "Id is required"); invalidCourseException.AddData( key: nameof(Course.CreatedDate), values: "Date is required"); invalidCourseException.AddData( key: nameof(Course.UpdatedDate), values: "Date is required"); var expectedCourseValidationException = new CourseValidationException(invalidCourseException); // when ValueTask <Course> createCourseTask = this.courseService.CreateCourseAsync(invalidCourse); // then await Assert.ThrowsAsync <CourseValidationException>(() => createCourseTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameValidationExceptionAs( expectedCourseValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCourseByIdAsync(It.IsAny <Guid>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }