public async void ShouldThrowValidationExceptionOnAddWhenCourseAttachmentIsNullAndLogItAsync() { // given CourseAttachment invalidCourseAttachment = null; var nullCourseAttachmentException = new NullCourseAttachmentException(); var expectedCourseAttachmentValidationException = new CourseAttachmentValidationException(nullCourseAttachmentException); // when ValueTask <CourseAttachment> addCourseAttachmentTask = this.courseAttachmentService.AddCourseAttachmentAsync(invalidCourseAttachment); // then await Assert.ThrowsAsync <CourseAttachmentValidationException>(() => addCourseAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCourseAttachmentValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertCourseAttachmentAsync(It.IsAny <CourseAttachment>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnAddWhenAttachmentIdIsInvalidAndLogItAsync() { // given CourseAttachment randomCourseAttachment = CreateRandomCourseAttachment(); CourseAttachment inputCourseAttachment = randomCourseAttachment; inputCourseAttachment.AttachmentId = default; var invalidCourseAttachmentInputException = new InvalidCourseAttachmentException( parameterName: nameof(CourseAttachment.AttachmentId), parameterValue: inputCourseAttachment.AttachmentId); var expectedCourseAttachmentValidationException = new CourseAttachmentValidationException(invalidCourseAttachmentInputException); // when ValueTask <CourseAttachment> addCourseAttachmentTask = this.courseAttachmentService.AddCourseAttachmentAsync(inputCourseAttachment); // then await Assert.ThrowsAsync <CourseAttachmentValidationException>(() => addCourseAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCourseAttachmentValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertCourseAttachmentAsync(It.IsAny <CourseAttachment>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
private CourseAttachmentValidationException CreateAndLogValidationException(Exception exception) { var courseAttachmentValidationException = new CourseAttachmentValidationException(exception); this.loggingBroker.LogError(courseAttachmentValidationException); return(courseAttachmentValidationException); }
public async Task ShouldThrowValidatonExceptionOnRemoveWhenIdsAreInvalidAndLogItAsync() { // given Guid invalidCourseId = Guid.Empty; Guid invalidAttachmentId = Guid.Empty; var invalidCourseAttachmentInputException = new InvalidCourseAttachmentException(); invalidCourseAttachmentInputException.AddData( key: nameof(CourseAttachment.CourseId), values: "Id is required"); invalidCourseAttachmentInputException.AddData( key: nameof(CourseAttachment.AttachmentId), values: "Id is required"); var expectedCourseAttachmentValidationException = new CourseAttachmentValidationException(invalidCourseAttachmentInputException); // when ValueTask <CourseAttachment> removeCourseAttachmentTask = this.courseAttachmentService.RemoveCourseAttachmentByIdAsync( invalidCourseId, invalidAttachmentId); // then await Assert.ThrowsAsync <CourseAttachmentValidationException>(() => removeCourseAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameValidationExceptionAs( expectedCourseAttachmentValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCourseAttachmentByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()), Times.Never); this.storageBrokerMock.Verify(broker => broker.DeleteCourseAttachmentAsync(It.IsAny <CourseAttachment>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidationExceptionOnRemoveWhenStorageCourseAttachmentIsNotFoundAndLogItAsync() { // given DateTimeOffset randomDateTime = GetRandomDateTime(); CourseAttachment randomCourseAttachment = CreateRandomCourseAttachment(randomDateTime); Guid someAttachmentId = randomCourseAttachment.AttachmentId; Guid someCourseId = randomCourseAttachment.CourseId; CourseAttachment nullStorageCourseAttachment = null; var notFoundCourseAttachmentException = new NotFoundCourseAttachmentException(someCourseId, someAttachmentId); var expectedCourseValidationException = new CourseAttachmentValidationException(notFoundCourseAttachmentException); this.storageBrokerMock.Setup(broker => broker.SelectCourseAttachmentByIdAsync(someCourseId, someAttachmentId)) .ReturnsAsync(nullStorageCourseAttachment); // when ValueTask <CourseAttachment> removeCourseAttachmentTask = this.courseAttachmentService.RemoveCourseAttachmentByIdAsync(someCourseId, someAttachmentId); // then await Assert.ThrowsAsync <CourseAttachmentValidationException>(() => removeCourseAttachmentTask.AsTask()); this.storageBrokerMock.Verify(broker => broker.SelectCourseAttachmentByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCourseValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteCourseAttachmentAsync(It.IsAny <CourseAttachment>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnAddWhenCourseAttachmentAlreadyExistsAndLogItAsync() { // given CourseAttachment randomCourseAttachment = CreateRandomCourseAttachment(); CourseAttachment alreadyExistsCourseAttachment = randomCourseAttachment; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var duplicateKeyException = new DuplicateKeyException(exceptionMessage); var alreadyExistsCourseAttachmentException = new AlreadyExistsCourseAttachmentException(duplicateKeyException); var expectedCourseAttachmentValidationException = new CourseAttachmentValidationException(alreadyExistsCourseAttachmentException); this.storageBrokerMock.Setup(broker => broker.InsertCourseAttachmentAsync(alreadyExistsCourseAttachment)) .ThrowsAsync(duplicateKeyException); // when ValueTask <CourseAttachment> addCourseAttachmentTask = this.courseAttachmentService.AddCourseAttachmentAsync(alreadyExistsCourseAttachment); // then await Assert.ThrowsAsync <CourseAttachmentValidationException>(() => addCourseAttachmentTask.AsTask()); this.storageBrokerMock.Verify(broker => broker.InsertCourseAttachmentAsync(It.IsAny <CourseAttachment>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCourseAttachmentValidationException))), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnAddWhenReferneceExceptionAndLogItAsync() { // given CourseAttachment randomCourseAttachment = CreateRandomCourseAttachment(); CourseAttachment someCourseAttachment = randomCourseAttachment; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var foreignKeyConstraintConflictException = new ForeignKeyConstraintConflictException(exceptionMessage); var invalidCourseAttachmentReferenceException = new InvalidCourseAttachmentReferenceException(foreignKeyConstraintConflictException); var expectedCourseAttachmentValidationException = new CourseAttachmentValidationException(invalidCourseAttachmentReferenceException); this.storageBrokerMock.Setup(broker => broker.InsertCourseAttachmentAsync(someCourseAttachment)) .ThrowsAsync(foreignKeyConstraintConflictException); // when ValueTask <CourseAttachment> addCourseAttachmentTask = this.courseAttachmentService.AddCourseAttachmentAsync(someCourseAttachment); // then await Assert.ThrowsAsync <CourseAttachmentValidationException>(() => addCourseAttachmentTask.AsTask()); this.storageBrokerMock.Verify(broker => broker.InsertCourseAttachmentAsync(It.IsAny <CourseAttachment>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCourseAttachmentValidationException))), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidatonExceptionOnRemoveWhenAttachmentIdIsInvalidAndLogItAsync() { // given Guid randomAttachmentId = default; Guid randomCourseId = Guid.NewGuid(); Guid invalidAttachmentId = randomAttachmentId; Guid inputCourseId = randomCourseId; var invalidCourseAttachmentInputException = new InvalidCourseAttachmentException( parameterName: nameof(CourseAttachment.AttachmentId), parameterValue: invalidAttachmentId); var expectedCourseAttachmentValidationException = new CourseAttachmentValidationException(invalidCourseAttachmentInputException); // when ValueTask <CourseAttachment> removeCourseAttachmentTask = this.courseAttachmentService.RemoveCourseAttachmentByIdAsync(inputCourseId, invalidAttachmentId); // then await Assert.ThrowsAsync <CourseAttachmentValidationException>(() => removeCourseAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCourseAttachmentValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCourseAttachmentByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()), Times.Never); this.storageBrokerMock.Verify(broker => broker.DeleteCourseAttachmentAsync(It.IsAny <CourseAttachment>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }