public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            GuardianAttachment randomGuardianAttachment = CreateRandomGuardianAttachment();
            GuardianAttachment someGuardianAttachment   = randomGuardianAttachment;
            var exception = new Exception();

            var expectedGuardianAttachmentServiceException =
                new GuardianAttachmentServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertGuardianAttachmentAsync(someGuardianAttachment))
            .ThrowsAsync(exception);

            // when
            ValueTask <GuardianAttachment> addGuardianAttachmentTask =
                this.guardianAttachmentService.AddGuardianAttachmentAsync(someGuardianAttachment);

            // then
            await Assert.ThrowsAsync <GuardianAttachmentServiceException>(() =>
                                                                          addGuardianAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertGuardianAttachmentAsync(someGuardianAttachment),
                                          Times.Once);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #2
0
        public async void ShouldThrowValidationExceptionOnAddWhenGuardianAttachmentIsNullAndLogItAsync()
        {
            // given
            GuardianAttachment randomGuardianAttachment = default;
            GuardianAttachment nullGuardianAttachment   = randomGuardianAttachment;
            var nullGuardianAttachmentException         = new NullGuardianAttachmentException();

            var expectedGuardianAttachmentValidationException =
                new GuardianAttachmentValidationException(nullGuardianAttachmentException);

            // when
            ValueTask <GuardianAttachment> addGuardianAttachmentTask =
                this.guardianAttachmentService.AddGuardianAttachmentAsync(nullGuardianAttachment);

            // then
            await Assert.ThrowsAsync <GuardianAttachmentValidationException>(() =>
                                                                             addGuardianAttachmentTask.AsTask());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #3
0
        public async Task ShouldAddGuardianAttachmentAsync()
        {
            // given
            GuardianAttachment randomGuardianAttachment   = CreateRandomGuardianAttachment();
            GuardianAttachment inputGuardianAttachment    = randomGuardianAttachment;
            GuardianAttachment storageGuardianAttachment  = randomGuardianAttachment;
            GuardianAttachment expectedGuardianAttachment = storageGuardianAttachment;

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertGuardianAttachmentAsync(inputGuardianAttachment))
            .ReturnsAsync(storageGuardianAttachment);

            // when
            GuardianAttachment actualGuardianAttachment =
                await this.guardianAttachmentService.AddGuardianAttachmentAsync(inputGuardianAttachment);

            // then
            actualGuardianAttachment.Should().BeEquivalentTo(expectedGuardianAttachment);

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertGuardianAttachmentAsync(inputGuardianAttachment),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #4
0
 private static void ValidateGuardianAttachmentIsNull(GuardianAttachment guardianAttachment)
 {
     if (guardianAttachment is null)
     {
         throw new NullGuardianAttachmentException();
     }
 }
Exemple #5
0
        public async Task ShouldRetrieveGuardianAttachmentById()
        {
            // given
            GuardianAttachment randomGuardianAttachment   = CreateRandomGuardianAttachment();
            GuardianAttachment storageGuardianAttachment  = randomGuardianAttachment;
            GuardianAttachment expectedGuardianAttachment = storageGuardianAttachment;

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectGuardianAttachmentByIdAsync
                                             (randomGuardianAttachment.GuardianId, randomGuardianAttachment.AttachmentId))
            .Returns(new ValueTask <GuardianAttachment>(randomGuardianAttachment));

            // when
            GuardianAttachment actualGuardianAttachment = await
                                                          this.guardianAttachmentService.RetrieveGuardianAttachmentByIdAsync(
                randomGuardianAttachment.GuardianId, randomGuardianAttachment.AttachmentId);

            // then
            actualGuardianAttachment.Should().BeEquivalentTo(expectedGuardianAttachment);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectGuardianAttachmentByIdAsync
                                              (randomGuardianAttachment.GuardianId, randomGuardianAttachment.AttachmentId),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
Exemple #6
0
        public ValueTask <GuardianAttachment> AddGuardianAttachmentAsync(GuardianAttachment guardianAttachment) =>
        TryCatch(async() =>
        {
            ValidateGuardianAttachmentOnCreate(guardianAttachment);

            return(await this.storageBroker.InsertGuardianAttachmentAsync(guardianAttachment));
        });
Exemple #7
0
        public async void ShouldThrowValidationExceptionOnAddWhenAttachmentIdIsInvalidAndLogItAsync()
        {
            // given
            GuardianAttachment randomGuardianAttachment = CreateRandomGuardianAttachment();
            GuardianAttachment inputGuardianAttachment  = randomGuardianAttachment;

            inputGuardianAttachment.AttachmentId = default;

            var invalidGuardianAttachmentInputException = new InvalidGuardianAttachmentException(
                parameterName: nameof(GuardianAttachment.AttachmentId),
                parameterValue: inputGuardianAttachment.AttachmentId);

            var expectedGuardianAttachmentValidationException =
                new GuardianAttachmentValidationException(invalidGuardianAttachmentInputException);

            // when
            ValueTask <GuardianAttachment> addGuardianAttachmentTask =
                this.guardianAttachmentService.AddGuardianAttachmentAsync(inputGuardianAttachment);

            // then
            await Assert.ThrowsAsync <GuardianAttachmentValidationException>(() =>
                                                                             addGuardianAttachmentTask.AsTask());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #8
0
 private static void ValidateStorageGuardianAttachment(
     GuardianAttachment storageStudentAttachment,
     Guid guardianId, Guid attachmentId)
 {
     if (storageStudentAttachment is null)
     {
         throw new NotFoundGuardianAttachmentException(guardianId, attachmentId);
     }
 }
Exemple #9
0
        public async ValueTask <GuardianAttachment> DeleteGuardianAttachmentAsync(
            GuardianAttachment guradianAttachment)
        {
            EntityEntry <GuardianAttachment> guradianAttachmentEntityEntry =
                this.GuardianAttachments.Remove(guradianAttachment);

            await this.SaveChangesAsync();

            return(guradianAttachmentEntityEntry.Entity);
        }
Exemple #10
0
        public async ValueTask <GuardianAttachment> InsertGuardianAttachmentAsync(
            GuardianAttachment guradianAttachment)
        {
            EntityEntry <GuardianAttachment> guradianAttachmentEntityEntry =
                await this.GuardianAttachments.AddAsync(guradianAttachment);

            await this.SaveChangesAsync();

            return(guradianAttachmentEntityEntry.Entity);
        }
Exemple #11
0
        public async ValueTask <GuardianAttachment> DeleteGuardianAttachmentAsync(
            GuardianAttachment guardianAttachment)
        {
            using var broker = new StorageBroker(this.configuration);

            EntityEntry <GuardianAttachment> guardianAttachmentEntityEntry =
                broker.GuardianAttachments.Remove(entity: guardianAttachment);

            await broker.SaveChangesAsync();

            return(guardianAttachmentEntityEntry.Entity);
        }
Exemple #12
0
        public ValueTask <GuardianAttachment> RemoveGuardianAttachmentByIdAsync(Guid guardianId, Guid attachmentId) =>
        TryCatch(async() =>
        {
            ValidateGuardianAttachmentIdIsNull(guardianId, attachmentId);

            GuardianAttachment mayBeGuardianAttachment =
                await this.storageBroker.SelectGuardianAttachmentByIdAsync(guardianId, attachmentId);

            ValidateStorageGuardianAttachment(mayBeGuardianAttachment, guardianId, attachmentId);

            return(await this.storageBroker.DeleteGuardianAttachmentAsync(mayBeGuardianAttachment));
        });
        public async Task ShouldThrowValidationExceptionOnRemoveWhenStorageGuardianAttachmentIsInvalidAndLogItAsync()
        {
            // given
            DateTimeOffset     randomDateTime           = GetRandomDateTime();
            GuardianAttachment randomGuardianAttachment = CreateRandomGuardianAttachment(randomDateTime);
            Guid inputAttachmentId = randomGuardianAttachment.AttachmentId;
            Guid inputGuardianId   = randomGuardianAttachment.GuardianId;
            GuardianAttachment nullStorageGuardianAttachment = null;

            var notFoundGuardianAttachmentException =
                new NotFoundGuardianAttachmentException(inputGuardianId, inputAttachmentId);

            var expectedSemesterCourseValidationException =
                new GuardianAttachmentValidationException(notFoundGuardianAttachmentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectGuardianAttachmentByIdAsync(inputGuardianId, inputAttachmentId))
            .ReturnsAsync(nullStorageGuardianAttachment);

            // when
            ValueTask <GuardianAttachment> removeGuardianAttachmentTask =
                this.guardianAttachmentService.RemoveGuardianAttachmentByIdAsync(inputGuardianId, inputAttachmentId);

            // then
            await Assert.ThrowsAsync <GuardianAttachmentValidationException>(() =>
                                                                             removeGuardianAttachmentTask.AsTask());

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
Exemple #14
0
        public async Task ShouldRemoveGuardianAttachmentAsync()
        {
            // given
            var                randomGuardianId         = Guid.NewGuid();
            var                randomAttachmentId       = Guid.NewGuid();
            Guid               inputGuardianId          = randomGuardianId;
            Guid               inputAttachmentId        = randomAttachmentId;
            DateTimeOffset     inputDateTime            = GetRandomDateTime();
            GuardianAttachment randomGuardianAttachment = CreateRandomGuardianAttachment(inputDateTime);

            randomGuardianAttachment.GuardianId   = inputGuardianId;
            randomGuardianAttachment.AttachmentId = inputAttachmentId;
            GuardianAttachment storageGuardianAttachment  = randomGuardianAttachment;
            GuardianAttachment expectedGuardianAttachment = storageGuardianAttachment;

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectGuardianAttachmentByIdAsync(inputGuardianId, inputAttachmentId))
            .ReturnsAsync(storageGuardianAttachment);

            this.storageBrokerMock.Setup(broker =>
                                         broker.DeleteGuardianAttachmentAsync(storageGuardianAttachment))
            .ReturnsAsync(expectedGuardianAttachment);

            // when
            GuardianAttachment actualGuardianAttachment =
                await this.guardianAttachmentService.RemoveGuardianAttachmentByIdAsync(
                    inputGuardianId, inputAttachmentId);

            // then
            actualGuardianAttachment.Should().BeEquivalentTo(expectedGuardianAttachment);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectGuardianAttachmentByIdAsync(inputGuardianId, inputAttachmentId),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.DeleteGuardianAttachmentAsync(storageGuardianAttachment),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #15
0
        public async void ShouldThrowValidationExceptionOnAddWhenReferneceExceptionAndLogItAsync()
        {
            // given
            GuardianAttachment randomGuardianAttachment  = CreateRandomGuardianAttachment();
            GuardianAttachment invalidGuardianAttachment = randomGuardianAttachment;
            string             randomMessage             = GetRandomMessage();
            string             exceptionMessage          = randomMessage;
            var foreignKeyConstraintConflictException    = new ForeignKeyConstraintConflictException(exceptionMessage);

            var invalidGuardianAttachmentReferenceException =
                new InvalidGuardianAttachmentReferenceException(foreignKeyConstraintConflictException);

            var expectedGuardianAttachmentValidationException =
                new GuardianAttachmentValidationException(invalidGuardianAttachmentReferenceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertGuardianAttachmentAsync(invalidGuardianAttachment))
            .ThrowsAsync(foreignKeyConstraintConflictException);

            // when
            ValueTask <GuardianAttachment> addGuardianAttachmentTask =
                this.guardianAttachmentService.AddGuardianAttachmentAsync(invalidGuardianAttachment);

            // then
            await Assert.ThrowsAsync <GuardianAttachmentValidationException>(() =>
                                                                             addGuardianAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertGuardianAttachmentAsync(invalidGuardianAttachment),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
Exemple #16
0
        public async void ShouldThrowValidationExceptionOnAddWhenGuardianAttachmentAlreadyExistsAndLogItAsync()
        {
            // given
            GuardianAttachment randomGuardianAttachment        = CreateRandomGuardianAttachment();
            GuardianAttachment alreadyExistsGuardianAttachment = randomGuardianAttachment;
            string             randomMessage    = GetRandomMessage();
            string             exceptionMessage = randomMessage;
            var duplicateKeyException           = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsGuardianAttachmentException =
                new AlreadyExistsGuardianAttachmentException(duplicateKeyException);

            var expectedGuardianAttachmentValidationException =
                new GuardianAttachmentValidationException(alreadyExistsGuardianAttachmentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertGuardianAttachmentAsync(alreadyExistsGuardianAttachment))
            .ThrowsAsync(duplicateKeyException);

            // when
            ValueTask <GuardianAttachment> addGuardianAttachmentTask =
                this.guardianAttachmentService.AddGuardianAttachmentAsync(alreadyExistsGuardianAttachment);

            // then
            await Assert.ThrowsAsync <GuardianAttachmentValidationException>(() =>
                                                                             addGuardianAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertGuardianAttachmentAsync(alreadyExistsGuardianAttachment),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
Exemple #17
0
 private static void ValidateGuardianAttachmentOnCreate(GuardianAttachment guardianAttachment)
 {
     ValidateGuardianAttachmentIsNull(guardianAttachment);
     ValidateGuardianAttachmentIdIsNull(guardianAttachment.GuardianId, guardianAttachment.AttachmentId);
 }