public ValueTask <StudentAttachment> AddStudentAttachmentAsync(StudentAttachment studentAttachment) =>
        TryCatch(async() =>
        {
            ValidateStudentAttachmentOnCreate(studentAttachment);

            return(await this.storageBroker.InsertStudentAttachmentAsync(studentAttachment));
        });
        public async void ShouldThrowValidationExceptionOnAddWhenAttachmentIdIsInvalidAndLogItAsync()
        {
            // given
            StudentAttachment randomStudentAttachment = CreateRandomStudentAttachment();
            StudentAttachment inputStudentAttachment  = randomStudentAttachment;

            inputStudentAttachment.AttachmentId = default;

            var invalidStudentAttachmentInputException = new InvalidStudentAttachmentException(
                parameterName: nameof(StudentAttachment.AttachmentId),
                parameterValue: inputStudentAttachment.AttachmentId);

            var expectedStudentAttachmentValidationException =
                new StudentAttachmentValidationException(invalidStudentAttachmentInputException);

            // when
            ValueTask <StudentAttachment> addStudentAttachmentTask =
                this.studentAttachmentService.AddStudentAttachmentAsync(inputStudentAttachment);

            // then
            await Assert.ThrowsAsync <StudentAttachmentValidationException>(() =>
                                                                            addStudentAttachmentTask.AsTask());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemplo n.º 3
0
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            StudentAttachment randomStudentAttachment = CreateRandomStudentAttachment();
            StudentAttachment inputStudentAttachment  = randomStudentAttachment;
            var exception = new Exception();

            var expectedStudentAttachmentServiceException =
                new StudentAttachmentServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentAttachmentAsync(inputStudentAttachment))
            .ThrowsAsync(exception);

            // when
            ValueTask <StudentAttachment> addStudentAttachmentTask =
                this.studentAttachmentService.AddStudentAttachmentAsync(inputStudentAttachment);

            // then
            await Assert.ThrowsAsync <StudentAttachmentServiceException>(() =>
                                                                         addStudentAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertStudentAttachmentAsync(inputStudentAttachment),
                                          Times.Once);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemplo n.º 4
0
        public async Task ShouldRetrieveStudentAttachmentById()
        {
            // given
            StudentAttachment randomStudentAttachment   = CreateRandomStudentAttachment();
            StudentAttachment storageStudentAttachment  = randomStudentAttachment;
            StudentAttachment expectedStudentAttachment = storageStudentAttachment;

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentAttachmentByIdAsync
                                             (randomStudentAttachment.StudentId, randomStudentAttachment.AttachmentId))
            .ReturnsAsync(storageStudentAttachment);

            // when
            StudentAttachment actualStudentAttachment = await
                                                        this.studentAttachmentService.RetrieveStudentAttachmentByIdAsync(
                randomStudentAttachment.StudentId, randomStudentAttachment.AttachmentId);

            // then
            actualStudentAttachment.Should().BeEquivalentTo(expectedStudentAttachment);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentAttachmentByIdAsync
                                              (randomStudentAttachment.StudentId, randomStudentAttachment.AttachmentId),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnAddWhenStudentAttachmentIsNullAndLogItAsync()
        {
            // given
            StudentAttachment randomStudentAttachment = default;
            StudentAttachment nullStudentAttachment   = randomStudentAttachment;
            var nullStudentAttachmentException        = new NullStudentAttachmentException();

            var expectedStudentAttachmentValidationException =
                new StudentAttachmentValidationException(nullStudentAttachmentException);

            // when
            ValueTask <StudentAttachment> addStudentAttachmentTask =
                this.studentAttachmentService.AddStudentAttachmentAsync(nullStudentAttachment);

            // then
            await Assert.ThrowsAsync <StudentAttachmentValidationException>(() =>
                                                                            addStudentAttachmentTask.AsTask());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldAddStudentAttachmentAsync()
        {
            // given
            StudentAttachment randomStudentAttachment   = CreateRandomStudentAttachment();
            StudentAttachment inputStudentAttachment    = randomStudentAttachment;
            StudentAttachment storageStudentAttachment  = randomStudentAttachment;
            StudentAttachment expectedStudentAttachment = storageStudentAttachment;

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentAttachmentAsync(inputStudentAttachment))
            .ReturnsAsync(storageStudentAttachment);

            // when
            StudentAttachment actualStudentAttachment =
                await this.studentAttachmentService.AddStudentAttachmentAsync(inputStudentAttachment);

            // then
            actualStudentAttachment.Should().BeEquivalentTo(expectedStudentAttachment);

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertStudentAttachmentAsync(inputStudentAttachment),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemplo n.º 7
0
        public async ValueTask <StudentAttachment> DeleteStudentAttachmentAsync(
            StudentAttachment studentAttachment)
        {
            EntityEntry <StudentAttachment> studentAttachmentEntityEntry =
                this.StudentAttachments.Remove(studentAttachment);

            await this.SaveChangesAsync();

            return(studentAttachmentEntityEntry.Entity);
        }
Exemplo n.º 8
0
        public async ValueTask <StudentAttachment> InsertStudentAttachmentAsync(
            StudentAttachment studentAttachment)
        {
            EntityEntry <StudentAttachment> studentAttachmentEntityEntry =
                await this.StudentAttachments.AddAsync(studentAttachment);

            await this.SaveChangesAsync();

            return(studentAttachmentEntityEntry.Entity);
        }
        public ValueTask <StudentAttachment> RemoveStudentAttachmentByIdAsync(Guid studentId, Guid attachmentId) =>
        TryCatch(async() =>
        {
            ValidateStudentAttachmentIdIsNull(studentId, attachmentId);

            StudentAttachment mayBeStudentAttachment =
                await this.storageBroker.SelectStudentAttachmentByIdAsync(studentId, attachmentId);

            ValidateStorageStudentAttachment(mayBeStudentAttachment, studentId, attachmentId);

            return(await this.storageBroker.DeleteStudentAttachmentAsync(mayBeStudentAttachment));
        });
Exemplo n.º 10
0
        public async Task ShouldThrowValidationExceptionOnRemoveWhenStorageStudentAttachmentIsInvalidAndLogItAsync()
        {
            // given
            DateTimeOffset    randomDateTime          = GetRandomDateTime();
            StudentAttachment randomStudentAttachment = CreateRandomStudentAttachment(randomDateTime);
            Guid inputAttachmentId = randomStudentAttachment.AttachmentId;
            Guid inputStudentId    = randomStudentAttachment.StudentId;
            StudentAttachment nullStorageStudentAttachment = null;

            var notFoundStudentAttachmentException =
                new NotFoundStudentAttachmentException(inputStudentId, inputAttachmentId);

            var expectedSemesterCourseValidationException =
                new StudentAttachmentValidationException(notFoundStudentAttachmentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentAttachmentByIdAsync(inputStudentId, inputAttachmentId))
            .ReturnsAsync(nullStorageStudentAttachment);

            // when
            ValueTask <StudentAttachment> removeStudentAttachmentTask =
                this.studentAttachmentService.RemoveStudentAttachmentByIdAsync(inputStudentId, inputAttachmentId);

            // then
            await Assert.ThrowsAsync <StudentAttachmentValidationException>(() =>
                                                                            removeStudentAttachmentTask.AsTask());

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldRemoveStudentAttachmentAsync()
        {
            // given
            var               randomStudentId         = Guid.NewGuid();
            var               randomAttachmentId      = Guid.NewGuid();
            Guid              inputStudentId          = randomStudentId;
            Guid              inputAttachmentId       = randomAttachmentId;
            DateTimeOffset    inputDateTime           = GetRandomDateTime();
            StudentAttachment randomStudentAttachment = CreateRandomStudentAttachment(inputDateTime);

            randomStudentAttachment.StudentId    = inputStudentId;
            randomStudentAttachment.AttachmentId = inputAttachmentId;
            StudentAttachment storageStudentAttachment  = randomStudentAttachment;
            StudentAttachment expectedStudentAttachment = storageStudentAttachment;

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentAttachmentByIdAsync(inputStudentId, inputAttachmentId))
            .ReturnsAsync(storageStudentAttachment);

            this.storageBrokerMock.Setup(broker =>
                                         broker.DeleteStudentAttachmentAsync(storageStudentAttachment))
            .ReturnsAsync(expectedStudentAttachment);

            // when
            StudentAttachment actualStudentAttachment =
                await this.studentAttachmentService.RemoveStudentAttachmentByIdAsync(
                    inputStudentId, inputAttachmentId);

            // then
            actualStudentAttachment.Should().BeEquivalentTo(expectedStudentAttachment);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentAttachmentByIdAsync(inputStudentId, inputAttachmentId),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.DeleteStudentAttachmentAsync(storageStudentAttachment),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnAddWhenReferneceExceptionAndLogItAsync()
        {
            // given
            StudentAttachment randomStudentAttachment  = CreateRandomStudentAttachment();
            StudentAttachment invalidStudentAttachment = randomStudentAttachment;
            string            randomMessage            = GetRandomMessage();
            string            exceptionMessage         = randomMessage;
            var foreignKeyConstraintConflictException  = new ForeignKeyConstraintConflictException(exceptionMessage);

            var invalidStudentAttachmentReferenceException =
                new InvalidStudentAttachmentReferenceException(foreignKeyConstraintConflictException);

            var expectedStudentAttachmentValidationException =
                new StudentAttachmentValidationException(invalidStudentAttachmentReferenceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentAttachmentAsync(invalidStudentAttachment))
            .ThrowsAsync(foreignKeyConstraintConflictException);

            // when
            ValueTask <StudentAttachment> addStudentAttachmentTask =
                this.studentAttachmentService.AddStudentAttachmentAsync(invalidStudentAttachment);

            // then
            await Assert.ThrowsAsync <StudentAttachmentValidationException>(() =>
                                                                            addStudentAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertStudentAttachmentAsync(invalidStudentAttachment),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnAddWhenStudentAttachmentAlreadyExistsAndLogItAsync()
        {
            // given
            StudentAttachment randomStudentAttachment        = CreateRandomStudentAttachment();
            StudentAttachment alreadyExistsStudentAttachment = randomStudentAttachment;
            string            randomMessage    = GetRandomMessage();
            string            exceptionMessage = randomMessage;
            var duplicateKeyException          = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsStudentAttachmentException =
                new AlreadyExistsStudentAttachmentException(duplicateKeyException);

            var expectedStudentAttachmentValidationException =
                new StudentAttachmentValidationException(alreadyExistsStudentAttachmentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentAttachmentAsync(alreadyExistsStudentAttachment))
            .ThrowsAsync(duplicateKeyException);

            // when
            ValueTask <StudentAttachment> addStudentAttachmentTask =
                this.studentAttachmentService.AddStudentAttachmentAsync(alreadyExistsStudentAttachment);

            // then
            await Assert.ThrowsAsync <StudentAttachmentValidationException>(() =>
                                                                            addStudentAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertStudentAttachmentAsync(alreadyExistsStudentAttachment),
                                          Times.Once);

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