public async Task ShouldGetAllExamAttachmentsAsync()
        {
            // given
            var randomExamAttachments = new List <ExamAttachment>();

            for (var i = 0; i <= GetRandomNumber(); i++)
            {
                ExamAttachment randomExamAttachment = await PostExamAttachmentAsync();

                randomExamAttachments.Add(randomExamAttachment);
            }

            List <ExamAttachment> inputExamAttachments    = randomExamAttachments;
            List <ExamAttachment> expectedExamAttachments = inputExamAttachments;

            // when
            List <ExamAttachment> actualExamAttachments =
                await this.otripleSApiBroker.GetAllExamAttachmentsAsync();

            // then
            foreach (ExamAttachment expectedExamAttachment in expectedExamAttachments)
            {
                ExamAttachment actualExamAttachment =
                    actualExamAttachments.Single(studentAttachment =>
                                                 studentAttachment.ExamId == expectedExamAttachment.ExamId);

                actualExamAttachment.Should().BeEquivalentTo(expectedExamAttachment);

                await DeleteExamAttachmentAsync(actualExamAttachment);
            }
        }
예제 #2
0
        public async Task ShouldRetrieveExamAttachmentByIdAsync()
        {
            // given
            ExamAttachment randomExamAttachment   = CreateRandomExamAttachment();
            ExamAttachment storageExamAttachment  = randomExamAttachment;
            ExamAttachment expectedExamAttachment = storageExamAttachment;

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectExamAttachmentByIdAsync(
                                             randomExamAttachment.ExamId,
                                             randomExamAttachment.AttachmentId))
            .ReturnsAsync(randomExamAttachment);

            // when
            ExamAttachment actualExamAttachment =
                await this.examAttachmentService.RetrieveExamAttachmentByIdAsync(
                    randomExamAttachment.ExamId,
                    randomExamAttachment.AttachmentId);

            // then
            actualExamAttachment.Should().BeEquivalentTo(expectedExamAttachment);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectExamAttachmentByIdAsync(
                                              randomExamAttachment.ExamId,
                                              randomExamAttachment.AttachmentId),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
예제 #3
0
        public async void ShouldThrowValidationExceptionOnAddWhenAttachmentIdIsInvalidAndLogItAsync()
        {
            // given
            ExamAttachment randomExamAttachment = CreateRandomExamAttachment();
            ExamAttachment inputExamAttachment  = randomExamAttachment;

            inputExamAttachment.AttachmentId = default;

            var invalidExamAttachmentInputException = new InvalidExamAttachmentException(
                parameterName: nameof(ExamAttachment.AttachmentId),
                parameterValue: inputExamAttachment.AttachmentId);

            var expectedExamAttachmentValidationException =
                new ExamAttachmentValidationException(invalidExamAttachmentInputException);

            // when
            ValueTask <ExamAttachment> addExamAttachmentTask =
                this.examAttachmentService.AddExamAttachmentAsync(inputExamAttachment);

            // then
            await Assert.ThrowsAsync <ExamAttachmentValidationException>(() =>
                                                                         addExamAttachmentTask.AsTask());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowDependencyExceptionOnAddWhenDbExceptionOccursAndLogItAsync()
        {
            // given
            ExamAttachment randomExamAttachment    = CreateRandomExamAttachment();
            ExamAttachment someExamAttachment      = randomExamAttachment;
            var            databaseUpdateException = new DbUpdateException();

            var expectedExamAttachmentDependencyException =
                new ExamAttachmentDependencyException(databaseUpdateException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertExamAttachmentAsync(someExamAttachment))
            .ThrowsAsync(databaseUpdateException);

            // when
            ValueTask <ExamAttachment> addExamAttachmentTask =
                this.examAttachmentService.AddExamAttachmentAsync(someExamAttachment);

            // then
            await Assert.ThrowsAsync <ExamAttachmentDependencyException>(() =>
                                                                         addExamAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertExamAttachmentAsync(someExamAttachment),
                                          Times.Once);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
예제 #5
0
        public ValueTask <ExamAttachment> AddExamAttachmentAsync(ExamAttachment examAttachment) =>
        TryCatch(async() =>
        {
            ValidateExamAttachmentOnAdd(examAttachment);

            return(await this.storageBroker.InsertExamAttachmentAsync(examAttachment));
        });
예제 #6
0
        public async void ShouldThrowValidationExceptionOnAddWhenExamAttachmentIsNullAndLogItAsync()
        {
            // given
            ExamAttachment invalidExamAttachment = null;

            var nullExamAttachmentException = new NullExamAttachmentException();

            var expectedExamAttachmentValidationException =
                new ExamAttachmentValidationException(nullExamAttachmentException);

            // when
            ValueTask <ExamAttachment> addExamAttachmentTask =
                this.examAttachmentService.AddExamAttachmentAsync(invalidExamAttachment);

            // then
            await Assert.ThrowsAsync <ExamAttachmentValidationException>(() =>
                                                                         addExamAttachmentTask.AsTask());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
예제 #7
0
 private static void ValidateExamAttachmentIsNull(ExamAttachment examAttachment)
 {
     if (examAttachment is null)
     {
         throw new NullExamAttachmentException();
     }
 }
예제 #8
0
 private static void ValidateStorageExamAttachment(
     ExamAttachment storageExamAttachment,
     Guid examId, Guid attachmentId)
 {
     if (storageExamAttachment is null)
     {
         throw new NotFoundExamAttachmentException(examId, attachmentId);
     }
 }
예제 #9
0
        public async ValueTask <ExamAttachment> DeleteExamAttachmentAsync(
            ExamAttachment examAttachment)
        {
            EntityEntry <ExamAttachment> examAttachmentEntityEntry =
                this.ExamAttachments.Remove(examAttachment);

            await this.SaveChangesAsync();

            return(examAttachmentEntityEntry.Entity);
        }
예제 #10
0
        public async ValueTask <ExamAttachment> InsertExamAttachmentAsync(
            ExamAttachment examAttachment)
        {
            EntityEntry <ExamAttachment> examAttachmentEntityEntry =
                await this.ExamAttachments.AddAsync(examAttachment);

            await this.SaveChangesAsync();

            return(examAttachmentEntityEntry.Entity);
        }
예제 #11
0
        public async ValueTask <ExamAttachment> DeleteExamAttachmentAsync(
            ExamAttachment examAttachment)
        {
            using var broker = new StorageBroker(this.configuration);

            EntityEntry <ExamAttachment> examAttachmentEntityEntry =
                broker.ExamAttachments.Remove(entity: examAttachment);

            await broker.SaveChangesAsync();

            return(examAttachmentEntityEntry.Entity);
        }
예제 #12
0
        public ActionResult FileUpload()
        {
            var httpRequest = System.Web.HttpContext.Current.Request;

            HttpFileCollection uploadFiles = httpRequest.Files;

            var docFiles = new List <string>();

            string newFileName = "";

            string ext = "";

            List <ExamAttachment> listAttachment = new List <ExamAttachment>();

            if (httpRequest.Files.Count > 0)
            {
                for (int i = 0; i < uploadFiles.Count; i++)
                {
                    HttpPostedFile postedFile = uploadFiles[i];

                    ext = Path.GetExtension(postedFile.FileName);

                    newFileName = DateTime.Now.ToString("MMddyyyHHmmSS");

                    var filePath = Server.MapPath(@"\FileUploads\" + newFileName + ext);

                    postedFile.SaveAs(filePath);

                    using (var db = new PhilscanExcellenceEntities())
                    {
                        ExamAttachment newAttachment = new ExamAttachment
                        {
                            ID        = Guid.NewGuid(),
                            Extension = ext,
                            FileName  = postedFile.FileName,
                            FileSize  = (postedFile.ContentLength / 1024).ToString(),
                            Path      = newFileName + ext,
                            Status    = 1
                        };

                        db.Entry(newAttachment).State = EntityState.Added;

                        db.SaveChanges();

                        listAttachment.Add(newAttachment);
                    }
                }
            }

            return(Json(new { uploadFile = listAttachment.FirstOrDefault() }));
        }
예제 #13
0
        public ValueTask <ExamAttachment> RetrieveExamAttachmentByIdAsync(
            Guid examId,
            Guid attachmentId) =>
        TryCatch(async() =>
        {
            ValidateExamAttachmentIds(examId, attachmentId);

            ExamAttachment maybeExamAttachment =
                await this.storageBroker.SelectExamAttachmentByIdAsync(examId, attachmentId);

            ValidateStorageExamAttachment(maybeExamAttachment, examId, attachmentId);

            return(maybeExamAttachment);
        });
예제 #14
0
        public async Task ShouldThrowValidationExceptionOnRemoveWhenStorageExamAttachmentIsInvalidAndLogItAsync()
        {
            // given
            DateTimeOffset randomDateTime            = GetRandomDateTime();
            ExamAttachment randomExamAttachment      = CreateRandomExamAttachment(randomDateTime);
            Guid           inputAttachmentId         = randomExamAttachment.AttachmentId;
            Guid           inputExamId               = randomExamAttachment.ExamId;
            ExamAttachment nullStorageExamAttachment = null;

            var notFoundExamAttachmentException =
                new NotFoundExamAttachmentException(inputExamId, inputAttachmentId);

            var expectedExamValidationException =
                new ExamAttachmentValidationException(notFoundExamAttachmentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectExamAttachmentByIdAsync(inputExamId, inputAttachmentId))
            .ReturnsAsync(nullStorageExamAttachment);

            // when
            ValueTask <ExamAttachment> removeExamAttachmentTask =
                this.examAttachmentService.RemoveExamAttachmentByIdAsync(inputExamId, inputAttachmentId);

            // then
            await Assert.ThrowsAsync <ExamAttachmentValidationException>(() =>
                                                                         removeExamAttachmentTask.AsTask());

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldRemoveExamAttachmentAsync()
        {
            // given
            var            randomExamId         = Guid.NewGuid();
            var            randomAttachmentId   = Guid.NewGuid();
            Guid           inputExamId          = randomExamId;
            Guid           inputAttachmentId    = randomAttachmentId;
            ExamAttachment randomExamAttachment = CreateRandomExamAttachment();

            randomExamAttachment.ExamId       = inputExamId;
            randomExamAttachment.AttachmentId = inputAttachmentId;
            ExamAttachment storageExamAttachment  = randomExamAttachment;
            ExamAttachment expectedExamAttachment = storageExamAttachment;

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectExamAttachmentByIdAsync(inputExamId, inputAttachmentId))
            .ReturnsAsync(storageExamAttachment);

            this.storageBrokerMock.Setup(broker =>
                                         broker.DeleteExamAttachmentAsync(storageExamAttachment))
            .ReturnsAsync(expectedExamAttachment);

            // when
            ExamAttachment actualExamAttachment =
                await this.examAttachmentService.RemoveExamAttachmentByIdAsync(
                    inputExamId, inputAttachmentId);

            // then
            actualExamAttachment.Should().BeEquivalentTo(expectedExamAttachment);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectExamAttachmentByIdAsync(inputExamId, inputAttachmentId),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.DeleteExamAttachmentAsync(storageExamAttachment),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldPostExamAttachmentAsync()
        {
            // given
            ExamAttachment randomExamAttachment = await CreateRandomExamAttachment();

            ExamAttachment inputExamAttachment    = randomExamAttachment;
            ExamAttachment expectedExamAttachment = inputExamAttachment;

            // when
            ExamAttachment actualExamAttachment =
                await this.otripleSApiBroker.PostExamAttachmentAsync(inputExamAttachment);

            ExamAttachment retrievedExamAttachment =
                await this.otripleSApiBroker.GetExamAttachmentByIdsAsync(
                    inputExamAttachment.ExamId,
                    inputExamAttachment.AttachmentId);

            // then
            actualExamAttachment.Should().BeEquivalentTo(expectedExamAttachment);
            retrievedExamAttachment.Should().BeEquivalentTo(expectedExamAttachment);
            await DeleteExamAttachmentAsync(actualExamAttachment);
        }
예제 #17
0
        public async void ShouldThrowValidationExceptionOnAddWhenExamAttachmentAlreadyExistsAndLogItAsync()
        {
            // given
            ExamAttachment randomExamAttachment        = CreateRandomExamAttachment();
            ExamAttachment alreadyExistsExamAttachment = randomExamAttachment;
            string         randomMessage         = GetRandomMessage();
            string         exceptionMessage      = randomMessage;
            var            duplicateKeyException = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsExamAttachmentException =
                new AlreadyExistsExamAttachmentException(duplicateKeyException);

            var expectedExamAttachmentValidationException =
                new ExamAttachmentValidationException(alreadyExistsExamAttachmentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertExamAttachmentAsync(alreadyExistsExamAttachment))
            .ThrowsAsync(duplicateKeyException);

            // when
            ValueTask <ExamAttachment> addExamAttachmentTask =
                this.examAttachmentService.AddExamAttachmentAsync(alreadyExistsExamAttachment);

            // then
            await Assert.ThrowsAsync <ExamAttachmentValidationException>(() =>
                                                                         addExamAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertExamAttachmentAsync(alreadyExistsExamAttachment),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
예제 #18
0
        public async void ShouldThrowValidationExceptionOnAddWhenReferneceExceptionAndLogItAsync()
        {
            // given
            ExamAttachment randomExamAttachment  = CreateRandomExamAttachment();
            ExamAttachment invalidExamAttachment = randomExamAttachment;
            string         randomMessage         = GetRandomMessage();
            string         exceptionMessage      = randomMessage;
            var            foreignKeyConstraintConflictException = new ForeignKeyConstraintConflictException(exceptionMessage);

            var invalidExamAttachmentReferenceException =
                new InvalidExamAttachmentReferenceException(foreignKeyConstraintConflictException);

            var expectedExamAttachmentValidationException =
                new ExamAttachmentValidationException(invalidExamAttachmentReferenceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertExamAttachmentAsync(invalidExamAttachment))
            .ThrowsAsync(foreignKeyConstraintConflictException);

            // when
            ValueTask <ExamAttachment> addExamAttachmentTask =
                this.examAttachmentService.AddExamAttachmentAsync(invalidExamAttachment);

            // then
            await Assert.ThrowsAsync <ExamAttachmentValidationException>(() =>
                                                                         addExamAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertExamAttachmentAsync(invalidExamAttachment),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldDeleteExamAttachmentAsync()
        {
            // given
            ExamAttachment randomExamAttachment = await PostExamAttachmentAsync();

            ExamAttachment inputExamAttachment    = randomExamAttachment;
            ExamAttachment expectedExamAttachment = inputExamAttachment;

            // when
            ExamAttachment deletedExamAttachment =
                await DeleteExamAttachmentAsync(inputExamAttachment);

            ValueTask <ExamAttachment> getExamAttachmentByIdTask =
                this.otripleSApiBroker.GetExamAttachmentByIdsAsync(
                    inputExamAttachment.ExamId,
                    inputExamAttachment.AttachmentId);

            // then
            deletedExamAttachment.Should().BeEquivalentTo(expectedExamAttachment);

            await Assert.ThrowsAsync <HttpResponseNotFoundException>(() =>
                                                                     getExamAttachmentByIdTask.AsTask());
        }
 public async ValueTask <ExamAttachment> PostExamAttachmentAsync(ExamAttachment examAttachment) =>
 await this.apiFactoryClient.PostContentAsync(ExamAttachmentsRelativeUrl, examAttachment);
예제 #21
0
 public void ValidateExamAttachmentOnAdd(ExamAttachment examAttachment)
 {
     ValidateExamAttachmentIsNull(examAttachment);
     ValidateExamAttachmentIds(examAttachment.ExamId, examAttachment.AttachmentId);
 }