Пример #1
0
        public async Task ShouldThrowValidationExceptionOnModifyIfStorageUpdatedDateSameAsUpdatedDateAndLogItAsync()
        {
            // given
            int            randomNegativeMinutes = GetNegativeRandomNumber();
            int            minutesInThePast      = randomNegativeMinutes;
            DateTimeOffset randomDate            = GetRandomDateTime();
            Exam           randomExam            = CreateRandomExam(randomDate);

            randomExam.CreatedDate = randomExam.CreatedDate.AddMinutes(minutesInThePast);
            Exam invalidExam = randomExam;

            invalidExam.UpdatedDate = randomDate;
            Exam storageExam = randomExam.DeepClone();
            Guid ExamId      = invalidExam.Id;

            var invalidExamException = new InvalidExamException();

            invalidExamException.AddData(
                key: nameof(Exam.UpdatedDate),
                values: $"Date is same as {nameof(Exam.UpdatedDate)}");

            var expectedExamValidationException =
                new ExamValidationException(invalidExamException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectExamByIdAsync(ExamId))
            .ReturnsAsync(storageExam);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Returns(randomDate);

            // when
            ValueTask <Exam> modifyExamTask =
                this.examService.ModifyExamAsync(invalidExam);

            // then
            await Assert.ThrowsAsync <ExamValidationException>(() =>
                                                               modifyExamTask.AsTask());

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectExamByIdAsync(invalidExam.Id),
                                          Times.Once);

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
Пример #2
0
        public async Task ShouldThrowValidationExceptionOnModifyIfStorageCreatedByNotSameAsCreatedByAndLogItAsync()
        {
            // given
            int            randomNegativeMinutes = GetNegativeRandomNumber();
            Guid           differentId           = Guid.NewGuid();
            Guid           invalidCreatedBy      = differentId;
            DateTimeOffset randomDate            = GetRandomDateTime();
            Exam           randomExam            = CreateRandomExam(randomDate);
            Exam           invalidExam           = randomExam;

            invalidExam.CreatedDate = randomDate.AddMinutes(randomNegativeMinutes);
            Exam storageExam = randomExam.DeepClone();
            Guid ExamId      = invalidExam.Id;

            invalidExam.CreatedBy = invalidCreatedBy;

            var invalidExamInputException = new InvalidExamInputException(
                parameterName: nameof(Exam.CreatedBy),
                parameterValue: invalidExam.CreatedBy);

            var expectedExamValidationException =
                new ExamValidationException(invalidExamInputException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectExamByIdAsync(ExamId))
            .ReturnsAsync(storageExam);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Returns(randomDate);

            // when
            ValueTask <Exam> modifyExamTask =
                this.examService.ModifyExamAsync(invalidExam);

            // then
            await Assert.ThrowsAsync <ExamValidationException>(() =>
                                                               modifyExamTask.AsTask());

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectExamByIdAsync(invalidExam.Id),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Пример #3
0
        public async Task ShouldModifyExamAsync()
        {
            // given
            int            randomNumber            = GetRandomNumber();
            int            randomDays              = randomNumber;
            DateTimeOffset randomDate              = GetRandomDateTime();
            DateTimeOffset randomInputDate         = GetRandomDateTime();
            Exam           randomExam              = CreateRandomExam(randomInputDate);
            Exam           inputExam               = randomExam;
            Exam           afterUpdateStorageExam  = inputExam;
            Exam           expectedExam            = afterUpdateStorageExam;
            Exam           beforeUpdateStorageExam = randomExam.DeepClone();

            inputExam.UpdatedDate = randomDate;
            Guid examId = inputExam.Id;

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Returns(randomDate);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectExamByIdAsync(examId))
            .ReturnsAsync(beforeUpdateStorageExam);

            this.storageBrokerMock.Setup(broker =>
                                         broker.UpdateExamAsync(inputExam))
            .ReturnsAsync(afterUpdateStorageExam);

            // when
            Exam actualExam =
                await this.examService.ModifyExamAsync(inputExam);

            // then
            actualExam.Should().BeEquivalentTo(expectedExam);

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectExamByIdAsync(examId),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.UpdateExamAsync(inputExam),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Пример #4
0
        public async Task <IActionResult> AddExamTooked(string userId, string examId)
        {
            User userInDatabase = await _userRepository.FindById(userId);

            if (userInDatabase == null)
            {
                return(NotFound("Usuário não encontrado"));
            }

            Exam examInDb = await _examRepo.GetExamById(examId);

            if (examInDb == null)
            {
                return(NotFound("Exame para adicionar ao usuário não encontrado"));
            }

            await userInDatabase.AddExam(examInDb.DeepClone());

            await _userRepository.UpdateUser(userInDatabase);

            return(NoContent());
        }
 public async Task UpdateAsync(Exam exam)
 {
     await RemoveAsync(exam);
     await AddAsync(exam.DeepClone());
 }