Beispiel #1
0
        public async Task ShouldGetAllCourseAttachmentsAsync()
        {
            // given
            var randomCourseAttachments = new List <CourseAttachment>();

            for (var i = 0; i <= GetRandomNumber(); i++)
            {
                CourseAttachment randomCourseAttachment = await PostCourseAttachmentAsync();

                randomCourseAttachments.Add(randomCourseAttachment);
            }

            List <CourseAttachment> inputCourseAttachments    = randomCourseAttachments;
            List <CourseAttachment> expectedCourseAttachments = inputCourseAttachments;

            // when
            List <CourseAttachment> actualCourseAttachments =
                await this.otripleSApiBroker.GetAllCourseAttachmentsAsync();

            // then
            foreach (CourseAttachment expectedCourseAttachment in expectedCourseAttachments)
            {
                CourseAttachment actualCourseAttachment =
                    actualCourseAttachments.Single(studentAttachment =>
                                                   studentAttachment.CourseId == expectedCourseAttachment.CourseId);

                actualCourseAttachment.Should().BeEquivalentTo(expectedCourseAttachment);

                await DeleteCourseAttachmentAsync(actualCourseAttachment);
            }
        }
Beispiel #2
0
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            CourseAttachment someCourseAttachment = CreateRandomCourseAttachment();
            var exception = new Exception();

            var expectedCourseAttachmentServiceException =
                new CourseAttachmentServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertCourseAttachmentAsync(It.IsAny <CourseAttachment>()))
            .ThrowsAsync(exception);

            // when
            ValueTask <CourseAttachment> addCourseAttachmentTask =
                this.courseAttachmentService.AddCourseAttachmentAsync(someCourseAttachment);

            // then
            await Assert.ThrowsAsync <CourseAttachmentServiceException>(() =>
                                                                        addCourseAttachmentTask.AsTask());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Beispiel #3
0
        public ValueTask <CourseAttachment> AddCourseAttachmentAsync(CourseAttachment courseAttachment) =>
        TryCatch(async() =>
        {
            ValidateCourseAttachmentOnAdd(courseAttachment);

            return(await storageBroker.InsertCourseAttachmentAsync(courseAttachment));
        });
 private static void ValidateCourseAttachmentIsNull(CourseAttachment courseAttachment)
 {
     if (courseAttachment is null)
     {
         throw new NullCourseAttachmentException();
     }
 }
Beispiel #5
0
        public async void ShouldThrowValidationExceptionOnAddWhenCourseAttachmentIsNullAndLogItAsync()
        {
            // given
            CourseAttachment invalidCourseAttachment = null;
            var nullCourseAttachmentException        = new NullCourseAttachmentException();

            var expectedCourseAttachmentValidationException =
                new CourseAttachmentValidationException(nullCourseAttachmentException);

            // when
            ValueTask <CourseAttachment> addCourseAttachmentTask =
                this.courseAttachmentService.AddCourseAttachmentAsync(invalidCourseAttachment);

            // then
            await Assert.ThrowsAsync <CourseAttachmentValidationException>(() =>
                                                                           addCourseAttachmentTask.AsTask());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Beispiel #6
0
        public async Task ShouldRetrieveCourseAttachmentById()
        {
            // given
            CourseAttachment randomCourseAttachment   = CreateRandomCourseAttachment();
            CourseAttachment storageCourseAttachment  = randomCourseAttachment;
            CourseAttachment expectedCourseAttachment = storageCourseAttachment;

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCourseAttachmentByIdAsync(
                                             randomCourseAttachment.CourseId,
                                             randomCourseAttachment.AttachmentId))
            .ReturnsAsync(randomCourseAttachment);

            // when
            CourseAttachment actualCourseAttachment = await
                                                      this.courseAttachmentService.RetrieveCourseAttachmentByIdAsync(
                randomCourseAttachment.CourseId,
                randomCourseAttachment.AttachmentId);

            // then
            actualCourseAttachment.Should().BeEquivalentTo(expectedCourseAttachment);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCourseAttachmentByIdAsync(
                                              randomCourseAttachment.CourseId,
                                              randomCourseAttachment.AttachmentId),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Beispiel #7
0
        public async Task ShouldAddCourseAttachmentAsync()
        {
            // given
            CourseAttachment randomCourseAttachment   = CreateRandomCourseAttachment();
            CourseAttachment inputCourseAttachment    = randomCourseAttachment;
            CourseAttachment storageCourseAttachment  = randomCourseAttachment;
            CourseAttachment expectedCourseAttachment = storageCourseAttachment;

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertCourseAttachmentAsync(inputCourseAttachment))
            .ReturnsAsync(storageCourseAttachment);

            // when
            CourseAttachment actualCourseAttachment =
                await this.courseAttachmentService.AddCourseAttachmentAsync(inputCourseAttachment);

            // then
            actualCourseAttachment.Should().BeEquivalentTo(expectedCourseAttachment);

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertCourseAttachmentAsync(inputCourseAttachment),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Beispiel #8
0
        public async void ShouldThrowValidationExceptionOnAddWhenAttachmentIdIsInvalidAndLogItAsync()
        {
            // given
            CourseAttachment randomCourseAttachment = CreateRandomCourseAttachment();
            CourseAttachment inputCourseAttachment  = randomCourseAttachment;

            inputCourseAttachment.AttachmentId = default;

            var invalidCourseAttachmentInputException = new InvalidCourseAttachmentException(
                parameterName: nameof(CourseAttachment.AttachmentId),
                parameterValue: inputCourseAttachment.AttachmentId);

            var expectedCourseAttachmentValidationException =
                new CourseAttachmentValidationException(invalidCourseAttachmentInputException);

            // when
            ValueTask <CourseAttachment> addCourseAttachmentTask =
                this.courseAttachmentService.AddCourseAttachmentAsync(inputCourseAttachment);

            // then
            await Assert.ThrowsAsync <CourseAttachmentValidationException>(() =>
                                                                           addCourseAttachmentTask.AsTask());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        private static void ValidateCourseAttachmentOnAdd(CourseAttachment courseAttachment)
        {
            ValidateCourseAttachmentIsNull(courseAttachment);

            Validate(
                (Rule: IsInvalid(courseAttachment.CourseId), Parameter: nameof(CourseAttachment.CourseId)),
                (Rule: IsInvalid(courseAttachment.AttachmentId), Parameter: nameof(CourseAttachment.AttachmentId)));
        }
Beispiel #10
0
 public int InsertCourseAttachment(CourseAttachment courseAttachment)
 {
     using (var context = new HaermsEntities())
     {
         var obj = context.CourseAttachment.Add(courseAttachment);
         context.SaveChanges();
         return(obj.AttachmentId);
     }
 }
Beispiel #11
0
        public async ValueTask <CourseAttachment> DeleteCourseAttachmentAsync(
            CourseAttachment courseAttachment)
        {
            EntityEntry <CourseAttachment> courseAttachmentEntityEntry =
                this.CourseAttachments.Remove(courseAttachment);

            await this.SaveChangesAsync();

            return(courseAttachmentEntityEntry.Entity);
        }
Beispiel #12
0
        public async ValueTask <CourseAttachment> InsertCourseAttachmentAsync(
            CourseAttachment courseAttachment)
        {
            EntityEntry <CourseAttachment> courseAttachmentEntityEntry =
                await this.CourseAttachments.AddAsync(courseAttachment);

            await this.SaveChangesAsync();

            return(courseAttachmentEntityEntry.Entity);
        }
 private static void ValidateStorageCourseAttachment(
     CourseAttachment storageCourseAttachment,
     Guid courseId,
     Guid attachmentId)
 {
     if (storageCourseAttachment is null)
     {
         throw new NotFoundCourseAttachmentException(courseId, attachmentId);
     }
 }
Beispiel #14
0
        public async ValueTask <CourseAttachment> DeleteCourseAttachmentAsync(
            CourseAttachment courseAttachment)
        {
            using var broker = new StorageBroker(this.configuration);

            EntityEntry <CourseAttachment> courseAttachmentEntityEntry =
                broker.CourseAttachments.Remove(entity: courseAttachment);

            await broker.SaveChangesAsync();

            return(courseAttachmentEntityEntry.Entity);
        }
Beispiel #15
0
        public ValueTask <CourseAttachment> RemoveCourseAttachmentByIdAsync(
            Guid courseId,
            Guid attachmentId) => TryCatch(async() =>
        {
            ValidateCourseAttachmentIds(courseId, attachmentId);

            CourseAttachment maybeCourseAttachment =
                await this.storageBroker.SelectCourseAttachmentByIdAsync(courseId, attachmentId);

            ValidateStorageCourseAttachment(maybeCourseAttachment, courseId, attachmentId);

            return(await this.storageBroker.DeleteCourseAttachmentAsync(maybeCourseAttachment));
        });
Beispiel #16
0
        //上传课件代码
        protected void UploadCourseAttachment_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e)
        {
            string savePath      = @"/TeacherUpload/CourseWare/";//此处savePath未加入,应该填入相应路径
            string teacherJobNum = "";

            if (Session["user"] is Models.Teacher t)
            {
                teacherJobNum = t.JobNumber;
                if (FileUploadCourseAttachment.HasFile)
                {
                    //上传到服务器文件夹中
                    String filename;
                    filename = FileUploadCourseAttachment.FileName;
                    //存储路径为当前路径+老师工号
                    string webSavePath = Server.MapPath("~" + savePath + "/" + teacherJobNum + "/" + filename);
                    if (!Directory.Exists(Server.MapPath("~" + savePath + "/" + teacherJobNum + "/")))
                    {
                        Directory.CreateDirectory(Server.MapPath("~" + savePath + "/" + teacherJobNum + "/"));
                    }
                    FileUploadCourseAttachment.SaveAs(webSavePath);

                    //插入到数据库中
                    var neededInsertCourseAttachment = new CourseAttachment();
                    neededInsertCourseAttachment.Name = filename;
                    //通过session获得课程id
                    string cidString = Request.QueryString["course"];
                    neededInsertCourseAttachment.CourseId   = int.Parse(cidString);
                    neededInsertCourseAttachment.Path       = savePath + teacherJobNum + "/" + filename;
                    neededInsertCourseAttachment.IssuedTime = DateTime.Now.ToLocalTime();
                    if (new CourseAttachmentServiceimpl().Create(neededInsertCourseAttachment) > 0)
                    {
                        Response.Redirect(Request.Url.ToString());
                    }
                    else
                    {
                        Response.Write("<script language='javascript'>alert('上传失败,请重试!');</script>");
                    }
                }
            }
            else
            {
                Response.Write("<script language='javascript'>alert('上传失败,请重试!');</script>");
            }
        }
Beispiel #17
0
        public async Task ShouldThrowValidationExceptionOnRemoveWhenStorageCourseAttachmentIsNotFoundAndLogItAsync()
        {
            // given
            DateTimeOffset   randomDateTime              = GetRandomDateTime();
            CourseAttachment randomCourseAttachment      = CreateRandomCourseAttachment(randomDateTime);
            Guid             someAttachmentId            = randomCourseAttachment.AttachmentId;
            Guid             someCourseId                = randomCourseAttachment.CourseId;
            CourseAttachment nullStorageCourseAttachment = null;

            var notFoundCourseAttachmentException =
                new NotFoundCourseAttachmentException(someCourseId, someAttachmentId);

            var expectedCourseValidationException =
                new CourseAttachmentValidationException(notFoundCourseAttachmentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCourseAttachmentByIdAsync(someCourseId, someAttachmentId))
            .ReturnsAsync(nullStorageCourseAttachment);

            // when
            ValueTask <CourseAttachment> removeCourseAttachmentTask =
                this.courseAttachmentService.RemoveCourseAttachmentByIdAsync(someCourseId, someAttachmentId);

            // then
            await Assert.ThrowsAsync <CourseAttachmentValidationException>(() =>
                                                                           removeCourseAttachmentTask.AsTask());

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Beispiel #18
0
        public async void ShouldThrowValidationExceptionOnAddWhenReferneceExceptionAndLogItAsync()
        {
            // given
            CourseAttachment randomCourseAttachment   = CreateRandomCourseAttachment();
            CourseAttachment someCourseAttachment     = randomCourseAttachment;
            string           randomMessage            = GetRandomMessage();
            string           exceptionMessage         = randomMessage;
            var foreignKeyConstraintConflictException = new ForeignKeyConstraintConflictException(exceptionMessage);

            var invalidCourseAttachmentReferenceException =
                new InvalidCourseAttachmentReferenceException(foreignKeyConstraintConflictException);

            var expectedCourseAttachmentValidationException =
                new CourseAttachmentValidationException(invalidCourseAttachmentReferenceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertCourseAttachmentAsync(someCourseAttachment))
            .ThrowsAsync(foreignKeyConstraintConflictException);

            // when
            ValueTask <CourseAttachment> addCourseAttachmentTask =
                this.courseAttachmentService.AddCourseAttachmentAsync(someCourseAttachment);

            // then
            await Assert.ThrowsAsync <CourseAttachmentValidationException>(() =>
                                                                           addCourseAttachmentTask.AsTask());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Beispiel #19
0
        public async void ShouldThrowValidationExceptionOnAddWhenCourseAttachmentAlreadyExistsAndLogItAsync()
        {
            // given
            CourseAttachment randomCourseAttachment        = CreateRandomCourseAttachment();
            CourseAttachment alreadyExistsCourseAttachment = randomCourseAttachment;
            string           randomMessage    = GetRandomMessage();
            string           exceptionMessage = randomMessage;
            var duplicateKeyException         = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsCourseAttachmentException =
                new AlreadyExistsCourseAttachmentException(duplicateKeyException);

            var expectedCourseAttachmentValidationException =
                new CourseAttachmentValidationException(alreadyExistsCourseAttachmentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertCourseAttachmentAsync(alreadyExistsCourseAttachment))
            .ThrowsAsync(duplicateKeyException);

            // when
            ValueTask <CourseAttachment> addCourseAttachmentTask =
                this.courseAttachmentService.AddCourseAttachmentAsync(alreadyExistsCourseAttachment);

            // then
            await Assert.ThrowsAsync <CourseAttachmentValidationException>(() =>
                                                                           addCourseAttachmentTask.AsTask());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Beispiel #20
0
        public async Task ShouldPostCourseAttachmentAsync()
        {
            // given
            CourseAttachment randomCourseAttachment = await CreateRandomCourseAttachment();

            CourseAttachment inputCourseAttachment    = randomCourseAttachment;
            CourseAttachment expectedCourseAttachment = inputCourseAttachment;

            // when
            CourseAttachment actualCourseAttachment =
                await this.otripleSApiBroker.PostCourseAttachmentAsync(inputCourseAttachment);

            CourseAttachment retrievedCourseAttachment =
                await this.otripleSApiBroker.GetCourseAttachmentByIdsAsync(
                    inputCourseAttachment.CourseId,
                    inputCourseAttachment.AttachmentId);

            // then
            actualCourseAttachment.Should().BeEquivalentTo(expectedCourseAttachment);
            retrievedCourseAttachment.Should().BeEquivalentTo(expectedCourseAttachment);
            await DeleteCourseAttachmentAsync(actualCourseAttachment);
        }
Beispiel #21
0
        public async Task ShouldDeleteCourseAttachmentAsync()
        {
            // given
            CourseAttachment randomCourseAttachment = await PostCourseAttachmentAsync();

            CourseAttachment inputCourseAttachment    = randomCourseAttachment;
            CourseAttachment expectedCourseAttachment = inputCourseAttachment;

            // when
            CourseAttachment deletedCourseAttachment =
                await DeleteCourseAttachmentAsync(inputCourseAttachment);

            ValueTask <CourseAttachment> getCourseAttachmentByIdTask =
                this.otripleSApiBroker.GetCourseAttachmentByIdsAsync(
                    inputCourseAttachment.CourseId,
                    inputCourseAttachment.AttachmentId);

            // then
            deletedCourseAttachment.Should().BeEquivalentTo(expectedCourseAttachment);

            await Assert.ThrowsAsync <HttpResponseNotFoundException>(() =>
                                                                     getCourseAttachmentByIdTask.AsTask());
        }
 public async ValueTask <CourseAttachment> PostCourseAttachmentAsync(CourseAttachment courseAttachment) =>
 await this.apiFactoryClient.PostContentAsync(CourseAttachmentsRelativeUrl, courseAttachment);
Beispiel #23
0
 private void ValidateCourseAttachmentOnCreate(CourseAttachment courseAttachment)
 {
     ValidateCourseAttachmentIsNull(courseAttachment);
     ValidateCourseAttachmentIds(courseAttachment.CourseId, courseAttachment.AttachmentId);
 }