public async ValueTask <AssignmentAttachment> PostAssignmentAttachmentAsync( AssignmentAttachment assignmentAttachment) { return(await this.apiFactoryClient.PostContentAsync( AssignmentAttachmentsRelativeUrl, assignmentAttachment)); }
public async void ShouldThrowValidationExceptionOnAddWhenAssignmentAttachmentIsNullAndLogItAsync() { // given AssignmentAttachment invalidAssignmentAttachment = null; var nullAssignmentAttachmentException = new NullAssignmentAttachmentException(); var expectedAssignmentAttachmentValidationException = new AssignmentAttachmentValidationException(nullAssignmentAttachmentException); // when ValueTask <AssignmentAttachment> addAssignmentAttachmentTask = this.assignmentAttachmentService.AddAssignmentAttachmentAsync(invalidAssignmentAttachment); // then await Assert.ThrowsAsync <AssignmentAttachmentValidationException>(() => addAssignmentAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedAssignmentAttachmentValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertAssignmentAttachmentAsync(It.IsAny <AssignmentAttachment>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldAddAttachmentAttachmentAsync() { // given AssignmentAttachment randomAssignmentAttachment = CreateRandomAssignmentAttachment(); AssignmentAttachment inputAssignmentAttachment = randomAssignmentAttachment; AssignmentAttachment storageAssignmentAttachment = randomAssignmentAttachment; AssignmentAttachment expectedAssignmentAttachment = storageAssignmentAttachment; this.storageBrokerMock.Setup(broker => broker.InsertAssignmentAttachmentAsync(inputAssignmentAttachment)) .ReturnsAsync(storageAssignmentAttachment); // when AssignmentAttachment actualAttachmentAttachment = await this.assignmentAttachmentService.AddAssignmentAttachmentAsync(inputAssignmentAttachment); // then actualAttachmentAttachment.Should().BeEquivalentTo(expectedAssignmentAttachment); this.storageBrokerMock.Verify(broker => broker.InsertAssignmentAttachmentAsync(inputAssignmentAttachment), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldGetAllAssignmentAttachmentsAsync() { // given var randomAssignmentAttachments = new List <AssignmentAttachment>(); for (var i = 0; i <= GetRandomNumber(); i++) { AssignmentAttachment randomAssignmentAttachment = await PostAssignmentAttachmentAsync(); randomAssignmentAttachments.Add(randomAssignmentAttachment); } List <AssignmentAttachment> inputAssignmentAttachments = randomAssignmentAttachments; List <AssignmentAttachment> expectedAssignmentAttachments = inputAssignmentAttachments; // when List <AssignmentAttachment> actualAssignmentAttachments = await this.otripleSApiBroker.GetAllAssignmentAttachmentsAsync(); // then foreach (AssignmentAttachment expectedAssignmentAttachment in expectedAssignmentAttachments) { AssignmentAttachment actualAssignmentAttachment = actualAssignmentAttachments.Single(studentAttachment => studentAttachment.AssignmentId == expectedAssignmentAttachment.AssignmentId); actualAssignmentAttachment.Should().BeEquivalentTo(expectedAssignmentAttachment); await DeleteAssignmentAttachmentAsync(actualAssignmentAttachment); } }
private static void ValidateAssignmentAttachmentIsNull(AssignmentAttachment assignmentContact) { if (assignmentContact is null) { throw new NullAssignmentAttachmentException(); } }
public ValueTask <AssignmentAttachment> AddAssignmentAttachmentAsync( AssignmentAttachment assignmentAttachment) => TryCatch(async() => { ValidateAssignmentAttachmentOnCreate(assignmentAttachment); return(await this.storageBroker.InsertAssignmentAttachmentAsync(assignmentAttachment)); });
public async Task ShouldRetrieveAssignmentAttachmentByIdAsync() { // given AssignmentAttachment randomAssignmentAttachment = CreateRandomAssignmentAttachment(); AssignmentAttachment storageAssignmentAttachment = randomAssignmentAttachment; AssignmentAttachment expectedAssignmentAttachment = storageAssignmentAttachment; this.storageBrokerMock.Setup(broker => broker.SelectAssignmentAttachmentByIdAsync( randomAssignmentAttachment.AssignmentId, randomAssignmentAttachment.AttachmentId)) .ReturnsAsync(randomAssignmentAttachment); // when AssignmentAttachment actualAssignmentAttachment = await this.assignmentAttachmentService.RetrieveAssignmentAttachmentByIdAsync( randomAssignmentAttachment.AssignmentId, randomAssignmentAttachment.AttachmentId); // then actualAssignmentAttachment.Should().BeEquivalentTo(expectedAssignmentAttachment); this.storageBrokerMock.Verify(broker => broker.SelectAssignmentAttachmentByIdAsync( randomAssignmentAttachment.AssignmentId, randomAssignmentAttachment.AttachmentId), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnAddWhenAttachmentIdIsInvalidAndLogItAsync() { // given AssignmentAttachment randomAssignmentAttachment = CreateRandomAssignmentAttachment(); AssignmentAttachment inputAssignmentAttachment = randomAssignmentAttachment; inputAssignmentAttachment.AttachmentId = default; var invalidAssignmentAttachmentInputException = new InvalidAssignmentAttachmentException( parameterName: nameof(AssignmentAttachment.AttachmentId), parameterValue: inputAssignmentAttachment.AttachmentId); var expectedAssignmentAttachmentValidationException = new AssignmentAttachmentValidationException(invalidAssignmentAttachmentInputException); // when ValueTask <AssignmentAttachment> addAssignmentAttachmentTask = this.assignmentAttachmentService.AddAssignmentAttachmentAsync(inputAssignmentAttachment); // then await Assert.ThrowsAsync <AssignmentAttachmentValidationException>(() => addAssignmentAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedAssignmentAttachmentValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertAssignmentAttachmentAsync(It.IsAny <AssignmentAttachment>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync() { // given AssignmentAttachment randomAssignmentAttachment = CreateRandomAssignmentAttachment(); AssignmentAttachment inputAssignmentAttachment = randomAssignmentAttachment; var exception = new Exception(); var expectedAssignmentAttachmentServiceException = new AssignmentAttachmentServiceException(exception); this.storageBrokerMock.Setup(broker => broker.InsertAssignmentAttachmentAsync(inputAssignmentAttachment)) .ThrowsAsync(exception); // when ValueTask <AssignmentAttachment> addAssignmentAttachmentTask = this.assignmentAttachmentService.AddAssignmentAttachmentAsync(inputAssignmentAttachment); // then await Assert.ThrowsAsync <AssignmentAttachmentServiceException>(() => addAssignmentAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedAssignmentAttachmentServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertAssignmentAttachmentAsync(inputAssignmentAttachment), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
private static void ValidateAssignmentAttachmentOnCreate(AssignmentAttachment assignmentAttachment) { ValidateAssignmentAttachmentIsNull(assignmentAttachment); ValidateAssignmentAttachmentIds( assignmentAttachment.AssignmentId, assignmentAttachment.AttachmentId); }
private static void ValidateStorageAssignmentAttachment( AssignmentAttachment storageAssignmentAttachment, Guid assignmentId, Guid attachmentId) { if (storageAssignmentAttachment == null) { throw new NotFoundAssignmentAttachmentException(assignmentId, attachmentId); } }
public async ValueTask <AssignmentAttachment> InsertAssignmentAttachmentAsync( AssignmentAttachment assignmentAttachment) { EntityEntry <AssignmentAttachment> assignmentAttachmentEntityEntry = await this.AssignmentAttachments.AddAsync(assignmentAttachment); await this.SaveChangesAsync(); return(assignmentAttachmentEntityEntry.Entity); }
public async ValueTask <AssignmentAttachment> DeleteAssignmentAttachmentAsync( AssignmentAttachment assignmentAttachment) { EntityEntry <AssignmentAttachment> assignmentAttachmentEntityEntry = this.AssignmentAttachments.Remove(assignmentAttachment); await this.SaveChangesAsync(); return(assignmentAttachmentEntityEntry.Entity); }
public async ValueTask <AssignmentAttachment> DeleteAssignmentAttachmentAsync( AssignmentAttachment assignmentAttachment) { using var broker = new StorageBroker(this.configuration); EntityEntry <AssignmentAttachment> assignmentAttachmentEntityEntry = broker.AssignmentAttachments.Remove(entity: assignmentAttachment); await broker.SaveChangesAsync(); return(assignmentAttachmentEntityEntry.Entity); }
public ValueTask <AssignmentAttachment> RemoveAssignmentAttachmentByIdAsync( Guid assignmentId, Guid attachmentId) => TryCatch(async() => { ValidateAssignmentAttachmentIds(assignmentId, attachmentId); AssignmentAttachment maybeAssignmentAttachment = await this.storageBroker.SelectAssignmentAttachmentByIdAsync(assignmentId, attachmentId); ValidateStorageAssignmentAttachment(maybeAssignmentAttachment, assignmentId, attachmentId); return(await this.storageBroker.DeleteAssignmentAttachmentAsync(maybeAssignmentAttachment)); });
public async Task ShouldThrowValidationExceptionOnRemoveWhenStorageAssignmentAttachmentIsInvalidAndLogItAsync() { // given DateTimeOffset randomDateTime = GetRandomDateTime(); AssignmentAttachment randomAssignmentAttachment = CreateRandomAssignmentAttachment(randomDateTime); Guid inputAttachmentId = randomAssignmentAttachment.AttachmentId; Guid inputAssignmentId = randomAssignmentAttachment.AssignmentId; AssignmentAttachment nullStorageAssignmentAttachment = null; var notFoundAssignmentAttachmentException = new NotFoundAssignmentAttachmentException(inputAssignmentId, inputAttachmentId); var expectedAssignmentValidationException = new AssignmentAttachmentValidationException(notFoundAssignmentAttachmentException); this.storageBrokerMock.Setup(broker => broker.SelectAssignmentAttachmentByIdAsync(inputAssignmentId, inputAttachmentId)) .ReturnsAsync(nullStorageAssignmentAttachment); // when ValueTask <AssignmentAttachment> removeAssignmentAttachmentTask = this.assignmentAttachmentService.RemoveAssignmentAttachmentByIdAsync( inputAssignmentId, inputAttachmentId); // then await Assert.ThrowsAsync <AssignmentAttachmentValidationException>(() => removeAssignmentAttachmentTask.AsTask()); this.storageBrokerMock.Verify(broker => broker.SelectAssignmentAttachmentByIdAsync( It.IsAny <Guid>(), It.IsAny <Guid>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedAssignmentValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteAssignmentAttachmentAsync( It.IsAny <AssignmentAttachment>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
public async Task <IActionResult> DeleteAttachment(int id) { AssignmentAttachment entity = await _db.AssignmentAttachments.SingleOrDefaultAsync(x => x.Id == id); if (entity == null) { return(NotFound()); } if (entity.UserId != _userService.UserId) { return(Forbid()); } _db.AssignmentAttachments.Remove(entity); await _db.SaveChangesAsync(); return(Ok()); }
public async Task ShouldRemoveAssignmentAttachmentAsync() { // given var randomAssignmentId = Guid.NewGuid(); var randomAttachmentId = Guid.NewGuid(); Guid inputAssignmentId = randomAssignmentId; Guid inputAttachmentId = randomAttachmentId; AssignmentAttachment randomAssignmentAttachment = CreateRandomAssignmentAttachment(); randomAssignmentAttachment.AssignmentId = inputAssignmentId; randomAssignmentAttachment.AttachmentId = inputAttachmentId; AssignmentAttachment storageAssignmentAttachment = randomAssignmentAttachment; AssignmentAttachment expectedAssignmentAttachment = storageAssignmentAttachment; this.storageBrokerMock.Setup(broker => broker.SelectAssignmentAttachmentByIdAsync(inputAssignmentId, inputAttachmentId)) .ReturnsAsync(storageAssignmentAttachment); this.storageBrokerMock.Setup(broker => broker.DeleteAssignmentAttachmentAsync(storageAssignmentAttachment)) .ReturnsAsync(expectedAssignmentAttachment); // when AssignmentAttachment actualAssignmentAttachment = await this.assignmentAttachmentService.RemoveAssignmentAttachmentByIdAsync(inputAssignmentId, inputAttachmentId); // then actualAssignmentAttachment.Should().BeEquivalentTo(expectedAssignmentAttachment); this.storageBrokerMock.Verify(broker => broker.SelectAssignmentAttachmentByIdAsync(inputAssignmentId, inputAttachmentId), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteAssignmentAttachmentAsync(storageAssignmentAttachment), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task <IActionResult> AddAttachment(IFormFile file, int assignmentId) { Assignment assignment = await _assignmentService.GetByIdAsync(assignmentId); if (assignment == null) { return(NotFound()); } if (assignment.TeacherId != _userService.UserId) { return(Forbid()); } AssignmentAttachment attachment = await _attachmentService.AddAsync(file, AttachmentType.Assignment) as AssignmentAttachment; attachment.AssignmentId = assignmentId; await _db.SaveChangesAsync(); return(Ok(_mapper.Map <AttachmentDto>(attachment))); }
public async void ShouldThrowValidationExceptionOnAddWhenReferneceExceptionAndLogItAsync() { // given AssignmentAttachment randomAssignmentAttachment = CreateRandomAssignmentAttachment(); AssignmentAttachment invalidAssignmentAttachment = randomAssignmentAttachment; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var foreignKeyConstraintConflictException = new ForeignKeyConstraintConflictException(exceptionMessage); var invalidAssignmentAttachmentReferenceException = new InvalidAssignmentAttachmentReferenceException(foreignKeyConstraintConflictException); var expectedAssignmentAttachmentValidationException = new AssignmentAttachmentValidationException(invalidAssignmentAttachmentReferenceException); this.storageBrokerMock.Setup(broker => broker.InsertAssignmentAttachmentAsync(invalidAssignmentAttachment)) .ThrowsAsync(foreignKeyConstraintConflictException); // when ValueTask <AssignmentAttachment> addAssignmentAttachmentTask = this.assignmentAttachmentService.AddAssignmentAttachmentAsync(invalidAssignmentAttachment); // then await Assert.ThrowsAsync <AssignmentAttachmentValidationException>(() => addAssignmentAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedAssignmentAttachmentValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertAssignmentAttachmentAsync(invalidAssignmentAttachment), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldPostAssignmentAttachmentAsync() { // given AssignmentAttachment randomAssignmentAttachment = await CreateRandomAssignmentAttachment(); AssignmentAttachment inputAssignmentAttachment = randomAssignmentAttachment; AssignmentAttachment expectedAssignmentAttachment = inputAssignmentAttachment; // when AssignmentAttachment actualAssignmentAttachment = await this.otripleSApiBroker.PostAssignmentAttachmentAsync(inputAssignmentAttachment); AssignmentAttachment retrievedAssignmentAttachment = await this.otripleSApiBroker.GetAssignmentAttachmentByIdsAsync( inputAssignmentAttachment.AssignmentId, inputAssignmentAttachment.AttachmentId); // then actualAssignmentAttachment.Should().BeEquivalentTo(expectedAssignmentAttachment); retrievedAssignmentAttachment.Should().BeEquivalentTo(expectedAssignmentAttachment); await DeleteAssignmentAttachmentAsync(actualAssignmentAttachment); }
public async void ShouldThrowValidationExceptionOnAddWhenAssignmentAttachmentAlreadyExistsAndLogItAsync() { // given AssignmentAttachment randomAssignmentAttachment = CreateRandomAssignmentAttachment(); AssignmentAttachment alreadyExistsAssignmentAttachment = randomAssignmentAttachment; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var duplicateKeyException = new DuplicateKeyException(exceptionMessage); var alreadyExistsAssignmentAttachmentException = new AlreadyExistsAssignmentAttachmentException(duplicateKeyException); var expectedAssignmentAttachmentValidationException = new AssignmentAttachmentValidationException(alreadyExistsAssignmentAttachmentException); this.storageBrokerMock.Setup(broker => broker.InsertAssignmentAttachmentAsync(alreadyExistsAssignmentAttachment)) .ThrowsAsync(duplicateKeyException); // when ValueTask <AssignmentAttachment> addAssignmentAttachmentTask = this.assignmentAttachmentService.AddAssignmentAttachmentAsync(alreadyExistsAssignmentAttachment); // then await Assert.ThrowsAsync <AssignmentAttachmentValidationException>(() => addAssignmentAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedAssignmentAttachmentValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertAssignmentAttachmentAsync(alreadyExistsAssignmentAttachment), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldDeleteAssignmentAttachmentAsync() { // given AssignmentAttachment randomAssignmentAttachment = await PostAssignmentAttachmentAsync(); AssignmentAttachment inputAssignmentAttachment = randomAssignmentAttachment; AssignmentAttachment expectedAssignmentAttachment = inputAssignmentAttachment; // when AssignmentAttachment deletedAssignmentAttachment = await DeleteAssignmentAttachmentAsync(inputAssignmentAttachment); ValueTask <AssignmentAttachment> getAssignmentAttachmentByIdTask = this.otripleSApiBroker.GetAssignmentAttachmentByIdsAsync( inputAssignmentAttachment.AssignmentId, inputAssignmentAttachment.AttachmentId); // then deletedAssignmentAttachment.Should().BeEquivalentTo(expectedAssignmentAttachment); await Assert.ThrowsAsync <HttpResponseNotFoundException>(() => getAssignmentAttachmentByIdTask.AsTask()); }
public async Task ShouldThrowDependencyExceptionOnAddWhenDbExceptionOccursAndLogItAsync() { // given AssignmentAttachment someAssignmentAttachment = CreateRandomAssignmentAttachment(); var databaseUpdateException = new DbUpdateException(); var failedAssigmentAttachmentStorageException = new FailedAssignmentAttachmentStorageException(databaseUpdateException); var expectedAssignmentAttachmentDependencyException = new AssignmentAttachmentDependencyException(failedAssigmentAttachmentStorageException); this.storageBrokerMock.Setup(broker => broker.InsertAssignmentAttachmentAsync(It.IsAny <AssignmentAttachment>())) .ThrowsAsync(databaseUpdateException); // when ValueTask <AssignmentAttachment> addAssignmentAttachmentTask = this.assignmentAttachmentService.AddAssignmentAttachmentAsync(someAssignmentAttachment); // then await Assert.ThrowsAsync <AssignmentAttachmentDependencyException>(() => addAssignmentAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedAssignmentAttachmentDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertAssignmentAttachmentAsync(It.IsAny <AssignmentAttachment>()), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }