Beispiel #1
0
        public async void ShouldThrowValidationExceptionOnAddWhenTeacherAttachmentIsNullAndLogItAsync()
        {
            // given
            TeacherAttachment invalidTeacherAttachment = null;

            var nullTeacherAttachmentException = new NullTeacherAttachmentException();

            var expectedTeacherAttachmentValidationException =
                new TeacherAttachmentValidationException(nullTeacherAttachmentException);

            // when
            ValueTask <TeacherAttachment> addTeacherAttachmentTask =
                this.teacherAttachmentService.AddTeacherAttachmentAsync(invalidTeacherAttachment);

            // then
            await Assert.ThrowsAsync <TeacherAttachmentValidationException>(() =>
                                                                            addTeacherAttachmentTask.AsTask());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldRetrieveTeacherAttachmentByIdAsync()
        {
            // given
            TeacherAttachment randomTeacherAttachment   = CreateRandomTeacherAttachment();
            TeacherAttachment storageTeacherAttachment  = randomTeacherAttachment;
            TeacherAttachment expectedTeacherAttachment = storageTeacherAttachment;

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectTeacherAttachmentByIdAsync
                                             (randomTeacherAttachment.TeacherId, randomTeacherAttachment.AttachmentId))
            .ReturnsAsync(storageTeacherAttachment);

            // when
            TeacherAttachment actualTeacherAttachment = await
                                                        this.teacherAttachmentService.RetrieveTeacherAttachmentByIdAsync(
                randomTeacherAttachment.TeacherId, randomTeacherAttachment.AttachmentId);

            // then
            actualTeacherAttachment.Should().BeEquivalentTo(expectedTeacherAttachment);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectTeacherAttachmentByIdAsync
                                              (randomTeacherAttachment.TeacherId, randomTeacherAttachment.AttachmentId),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldAddTeacherAttachmentAsync()
        {
            // given
            TeacherAttachment randomTeacherAttachment   = CreateRandomTeacherAttachment();
            TeacherAttachment inputTeacherAttachment    = randomTeacherAttachment;
            TeacherAttachment storageTeacherAttachment  = randomTeacherAttachment;
            TeacherAttachment expectedTeacherAttachment = storageTeacherAttachment;

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertTeacherAttachmentAsync(inputTeacherAttachment))
            .ReturnsAsync(storageTeacherAttachment);

            // when
            TeacherAttachment actualTeacherAttachment =
                await this.teacherAttachmentService.AddTeacherAttachmentAsync(inputTeacherAttachment);

            // then
            actualTeacherAttachment.Should().BeEquivalentTo(expectedTeacherAttachment);

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertTeacherAttachmentAsync(inputTeacherAttachment),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnAddWhenAttachmentIdIsInvalidAndLogItAsync()
        {
            // given
            TeacherAttachment randomTeacherAttachment = CreateRandomTeacherAttachment();
            TeacherAttachment inputTeacherAttachment  = randomTeacherAttachment;

            inputTeacherAttachment.AttachmentId = default;

            var invalidTeacherAttachmentInputException = new InvalidTeacherAttachmentException(
                parameterName: nameof(TeacherAttachment.AttachmentId),
                parameterValue: inputTeacherAttachment.AttachmentId);

            var expectedTeacherAttachmentValidationException =
                new TeacherAttachmentValidationException(invalidTeacherAttachmentInputException);

            // when
            ValueTask <TeacherAttachment> addTeacherAttachmentTask =
                this.teacherAttachmentService.AddTeacherAttachmentAsync(inputTeacherAttachment);

            // then
            await Assert.ThrowsAsync <TeacherAttachmentValidationException>(() =>
                                                                            addTeacherAttachmentTask.AsTask());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
 private static void ValidateTeacherAttachmentIsNull(TeacherAttachment teacherContact)
 {
     if (teacherContact is null)
     {
         throw new NullTeacherAttachmentException();
     }
 }
        public ValueTask <TeacherAttachment> AddTeacherAttachmentAsync(TeacherAttachment teacherAttachment) =>
        TryCatch(async() =>
        {
            ValidateTeacherAttachmentOnCreate(teacherAttachment);

            return(await this.storageBroker.InsertTeacherAttachmentAsync(teacherAttachment));
        });
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            TeacherAttachment randomTeacherAttachment = CreateRandomTeacherAttachment();
            TeacherAttachment inputTeacherAttachment  = randomTeacherAttachment;
            var exception = new Exception();
            var expectedTeacherAttachmentServiceException = new TeacherAttachmentServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertTeacherAttachmentAsync(inputTeacherAttachment))
            .ThrowsAsync(exception);

            // when
            ValueTask <TeacherAttachment> addTeacherAttachmentTask =
                this.teacherAttachmentService.AddTeacherAttachmentAsync(inputTeacherAttachment);

            // then
            await Assert.ThrowsAsync <TeacherAttachmentServiceException>(() =>
                                                                         addTeacherAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertTeacherAttachmentAsync(inputTeacherAttachment),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldGetAllTeacherAttachmentsAsync()
        {
            // given
            var randomTeacherAttachments = new List <TeacherAttachment>();

            for (var i = 0; i <= GetRandomNumber(); i++)
            {
                TeacherAttachment randomTeacherAttachment = await PostTeacherAttachmentAsync();

                randomTeacherAttachments.Add(randomTeacherAttachment);
            }

            List <TeacherAttachment> inputTeacherAttachments    = randomTeacherAttachments;
            List <TeacherAttachment> expectedTeacherAttachments = inputTeacherAttachments;

            // when
            List <TeacherAttachment> actualTeacherAttachments =
                await this.otripleSApiBroker.GetAllTeacherAttachmentsAsync();

            // then
            foreach (TeacherAttachment expectedTeacherAttachment in expectedTeacherAttachments)
            {
                TeacherAttachment actualTeacherAttachment =
                    actualTeacherAttachments.Single(studentAttachment =>
                                                    studentAttachment.TeacherId == expectedTeacherAttachment.TeacherId);

                actualTeacherAttachment.Should().BeEquivalentTo(expectedTeacherAttachment);

                await DeleteTeacherAttachmentAsync(actualTeacherAttachment);
            }
        }
        public async ValueTask <TeacherAttachment> InsertTeacherAttachmentAsync(
            TeacherAttachment teacherAttachment)
        {
            EntityEntry <TeacherAttachment> teacherAttachmentEntityEntry =
                await this.TeacherAttachments.AddAsync(teacherAttachment);

            await this.SaveChangesAsync();

            return(teacherAttachmentEntityEntry.Entity);
        }
        public async ValueTask <TeacherAttachment> DeleteTeacherAttachmentAsync(
            TeacherAttachment teacherAttachment)
        {
            EntityEntry <TeacherAttachment> teacherAttachmentEntityEntry =
                this.TeacherAttachments.Remove(teacherAttachment);

            await this.SaveChangesAsync();

            return(teacherAttachmentEntityEntry.Entity);
        }
 private static void ValidateStorageTeacherAttachment(
     TeacherAttachment storageTeacherAttachment,
     Guid studentId,
     Guid attachmentId)
 {
     if (storageTeacherAttachment is null)
     {
         throw new NotFoundTeacherAttachmentException(studentId, attachmentId);
     }
 }
        public ValueTask <TeacherAttachment> RemoveTeacherAttachmentByIdAsync(Guid teacherId, Guid attachmentId) =>
        TryCatch(async() =>
        {
            ValidateTeacherAttachmentIds(teacherId, attachmentId);

            TeacherAttachment maybeTeacherAttachment =
                await this.storageBroker.SelectTeacherAttachmentByIdAsync(teacherId, attachmentId);

            ValidateStorageTeacherAttachment(maybeTeacherAttachment, teacherId, attachmentId);

            return(await this.storageBroker.DeleteTeacherAttachmentAsync(maybeTeacherAttachment));
        });
Beispiel #13
0
        public async ValueTask <TeacherAttachment> DeleteTeacherAttachmentAsync(
            TeacherAttachment teacherAttachment)
        {
            using var broker = new StorageBroker(this.configuration);

            EntityEntry <TeacherAttachment> teacherAttachmentEntityEntry =
                broker.TeacherAttachments.Remove(entity: teacherAttachment);

            await broker.SaveChangesAsync();

            return(teacherAttachmentEntityEntry.Entity);
        }
        public async Task ShouldThrowValidationExceptionOnRemoveWhenStorageTeacherAttachmentIsInvalidAndLogItAsync()
        {
            // given
            DateTimeOffset    randomDateTime          = GetRandomDateTime();
            TeacherAttachment randomTeacherAttachment = CreateRandomTeacherAttachment(randomDateTime);
            Guid inputAttachmentId = randomTeacherAttachment.AttachmentId;
            Guid inputTeacherId    = randomTeacherAttachment.TeacherId;
            TeacherAttachment nullStorageTeacherAttachment = null;

            var notFoundTeacherAttachmentException =
                new NotFoundTeacherAttachmentException(inputTeacherId, inputAttachmentId);

            var expectedTeacherValidationException =
                new TeacherAttachmentValidationException(notFoundTeacherAttachmentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectTeacherAttachmentByIdAsync(inputTeacherId, inputAttachmentId))
            .ReturnsAsync(nullStorageTeacherAttachment);

            // when
            ValueTask <TeacherAttachment> removeTeacherAttachmentTask =
                this.teacherAttachmentService.RemoveTeacherAttachmentByIdAsync(inputTeacherId, inputAttachmentId);

            // then
            await Assert.ThrowsAsync <TeacherAttachmentValidationException>(() =>
                                                                            removeTeacherAttachmentTask.AsTask());

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Beispiel #15
0
        public async void ShouldThrowValidationExceptionOnAddWhenTeacherAttachmentAlreadyExistsAndLogItAsync()
        {
            // given
            TeacherAttachment randomTeacherAttachment        = CreateRandomTeacherAttachment();
            TeacherAttachment alreadyExistsTeacherAttachment = randomTeacherAttachment;
            string            randomMessage    = GetRandomMessage();
            string            exceptionMessage = randomMessage;
            var duplicateKeyException          = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsTeacherAttachmentException =
                new AlreadyExistsTeacherAttachmentException(duplicateKeyException);

            var expectedTeacherAttachmentValidationException =
                new TeacherAttachmentValidationException(alreadyExistsTeacherAttachmentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertTeacherAttachmentAsync(alreadyExistsTeacherAttachment))
            .ThrowsAsync(duplicateKeyException);

            // when
            ValueTask <TeacherAttachment> addTeacherAttachmentTask =
                this.teacherAttachmentService.AddTeacherAttachmentAsync(alreadyExistsTeacherAttachment);

            // then
            await Assert.ThrowsAsync <TeacherAttachmentValidationException>(() =>
                                                                            addTeacherAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertTeacherAttachmentAsync(alreadyExistsTeacherAttachment),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Beispiel #16
0
        public async void ShouldThrowValidationExceptionOnAddWhenReferneceExceptionAndLogItAsync()
        {
            // given
            TeacherAttachment randomTeacherAttachment  = CreateRandomTeacherAttachment();
            TeacherAttachment invalidTeacherAttachment = randomTeacherAttachment;
            string            randomMessage            = GetRandomMessage();
            string            exceptionMessage         = randomMessage;
            var foreignKeyConstraintConflictException  = new ForeignKeyConstraintConflictException(exceptionMessage);

            var invalidTeacherAttachmentReferenceException =
                new InvalidTeacherAttachmentReferenceException(foreignKeyConstraintConflictException);

            var expectedTeacherAttachmentValidationException =
                new TeacherAttachmentValidationException(invalidTeacherAttachmentReferenceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertTeacherAttachmentAsync(invalidTeacherAttachment))
            .ThrowsAsync(foreignKeyConstraintConflictException);

            // when
            ValueTask <TeacherAttachment> addTeacherAttachmentTask =
                this.teacherAttachmentService.AddTeacherAttachmentAsync(invalidTeacherAttachment);

            // then
            await Assert.ThrowsAsync <TeacherAttachmentValidationException>(() =>
                                                                            addTeacherAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertTeacherAttachmentAsync(invalidTeacherAttachment),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldPostTeacherAttachmentAsync()
        {
            // given
            TeacherAttachment randomTeacherAttachment = await CreateRandomTeacherAttachment();

            TeacherAttachment inputTeacherAttachment    = randomTeacherAttachment;
            TeacherAttachment expectedTeacherAttachment = inputTeacherAttachment;

            // when
            TeacherAttachment actualTeacherAttachment =
                await this.otripleSApiBroker.PostTeacherAttachmentAsync(inputTeacherAttachment);

            TeacherAttachment retrievedTeacherAttachment =
                await this.otripleSApiBroker.GetTeacherAttachmentByIdsAsync(
                    inputTeacherAttachment.TeacherId,
                    inputTeacherAttachment.AttachmentId);

            // then
            actualTeacherAttachment.Should().BeEquivalentTo(expectedTeacherAttachment);
            retrievedTeacherAttachment.Should().BeEquivalentTo(expectedTeacherAttachment);
            await DeleteTeacherAttachmentAsync(actualTeacherAttachment);
        }
        public async Task ShouldDeleteTeacherAttachmentAsync()
        {
            // given
            TeacherAttachment randomTeacherAttachment = await PostTeacherAttachmentAsync();

            TeacherAttachment inputTeacherAttachment    = randomTeacherAttachment;
            TeacherAttachment expectedTeacherAttachment = inputTeacherAttachment;

            // when
            TeacherAttachment deletedTeacherAttachment =
                await DeleteTeacherAttachmentAsync(inputTeacherAttachment);

            ValueTask <TeacherAttachment> getTeacherAttachmentByIdTask =
                this.otripleSApiBroker.GetTeacherAttachmentByIdsAsync(
                    inputTeacherAttachment.TeacherId,
                    inputTeacherAttachment.AttachmentId);

            // then
            deletedTeacherAttachment.Should().BeEquivalentTo(expectedTeacherAttachment);

            await Assert.ThrowsAsync <HttpResponseNotFoundException>(() =>
                                                                     getTeacherAttachmentByIdTask.AsTask());
        }
 private static void ValidateTeacherAttachmentOnCreate(TeacherAttachment teacherAttachment)
 {
     ValidateTeacherAttachmentIsNull(teacherAttachment);
     ValidateTeacherAttachmentIds(teacherAttachment.TeacherId, teacherAttachment.AttachmentId);
 }
Beispiel #20
0
 public async ValueTask <TeacherAttachment> PostTeacherAttachmentAsync(TeacherAttachment teacherAttachment) =>
 await this.apiFactoryClient.PostContentAsync(TeacherAttachmentsRelativeUrl, teacherAttachment);