public async Task ShouldThrowValidatonExceptionOnRetrieveByIdIfIdIsInvalidAndLogItAsync()
        {
            // given
            Guid invalidTeacherId        = Guid.Empty;
            var  invalidTeacherException = new InvalidTeacherException();

            invalidTeacherException.AddData(
                key: nameof(Teacher.Id),
                values: "Id is required");

            var expectedTeacherValidationException =
                new TeacherValidationException(invalidTeacherException);

            // when
            ValueTask <Teacher> actualTeacherTask =
                this.teacherService.RetrieveTeacherByIdAsync(invalidTeacherId);

            // then
            await Assert.ThrowsAsync <TeacherValidationException>(() => actualTeacherTask.AsTask());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 2
0
        public async Task ShouldThrowValidationExceptionOnModifyIfStorageCreatedByNotSameAsCreatedByAndLogItAsync()
        {
            // given
            int            randomNegativeMinutes = GetNegativeRandomNumber();
            Guid           differentId           = Guid.NewGuid();
            Guid           invalidCreatedBy      = differentId;
            DateTimeOffset randomDate            = GetRandomDateTime();
            Teacher        randomTeacher         = CreateRandomTeacher(randomDate);
            Teacher        invalidTeacher        = randomTeacher;

            invalidTeacher.CreatedDate = randomDate.AddMinutes(randomNegativeMinutes);
            Teacher storageTeacher = randomTeacher.DeepClone();
            Guid    studentId      = invalidTeacher.Id;

            invalidTeacher.CreatedBy = invalidCreatedBy;

            var invalidTeacherException = new InvalidTeacherException();

            invalidTeacherException.AddData(
                key: nameof(Teacher.CreatedBy),
                values: $"Id is not the same as {nameof(Teacher.CreatedBy)}");

            var expectedTeacherValidationException =
                new TeacherValidationException(invalidTeacherException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectTeacherByIdAsync(studentId))
            .ReturnsAsync(storageTeacher);

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

            // when
            ValueTask <Teacher> modifyTeacherTask =
                this.teacherService.ModifyTeacherAsync(invalidTeacher);

            // then
            await Assert.ThrowsAsync <TeacherValidationException>(() =>
                                                                  modifyTeacherTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectTeacherByIdAsync(invalidTeacher.Id),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 3
0
        public async void ShouldThrowValidationExceptionOnCreateWhenCreatedDateIsNotRecentAndLogItAsync(
            int minutes)
        {
            // given
            DateTimeOffset dateTime      = GetRandomDateTime();
            Teacher        randomTeacher = CreateRandomTeacher(dateTime);
            Teacher        inputTeacher  = randomTeacher;

            inputTeacher.UpdatedBy   = inputTeacher.CreatedBy;
            inputTeacher.CreatedDate = dateTime.AddMinutes(minutes);
            inputTeacher.UpdatedDate = inputTeacher.CreatedDate;
            var invalidTeacherException = new InvalidTeacherException();

            invalidTeacherException.AddData(
                key: nameof(Teacher.CreatedDate),
                values: $"Date is not recent");

            var expectedTeacherValidationException =
                new TeacherValidationException(invalidTeacherException);

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

            // when
            ValueTask <Teacher> createTeacherTask =
                this.teacherService.CreateTeacherAsync(inputTeacher);

            // then
            await Assert.ThrowsAsync <TeacherValidationException>(() =>
                                                                  createTeacherTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnModifyWhenUpdatedDateIsSameToCreatedDateAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime                = GetRandomDateTime();
            Teacher        randomTeacher           = CreateRandomTeacher(dateTime);
            Teacher        invalidTeacher          = randomTeacher;
            var            invalidTeacherException = new InvalidTeacherException();

            invalidTeacherException.AddData(
                key: nameof(Teacher.UpdatedDate),
                values: $"UpdatedDate is the same as {nameof(Teacher.CreatedDate)}");

            var expectedTeacherValidationException =
                new TeacherValidationException(invalidTeacherException);

            // when
            ValueTask <Teacher> modifyTeacherTask =
                this.teacherService.ModifyTeacherAsync(invalidTeacher);

            // then
            await Assert.ThrowsAsync <TeacherValidationException>(() =>
                                                                  modifyTeacherTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 5
0
        public async void ShouldThrowValidationExceptionOnModifyIfTeacherIsInvalidAndLogItAsync(
            string invalidText)
        {
            // given
            var invalidTeacher = new Teacher
            {
                UserId         = invalidText,
                EmployeeNumber = invalidText,
                FirstName      = invalidText,
                MiddleName     = invalidText,
                LastName       = invalidText
            };

            var invalidTeacherException = new InvalidTeacherException();

            invalidTeacherException.AddData(
                key: nameof(Teacher.Id),
                values: "Id is required");

            invalidTeacherException.AddData(
                key: nameof(Teacher.UserId),
                values: "Text is required");

            invalidTeacherException.AddData(
                key: nameof(Teacher.EmployeeNumber),
                values: "Text is required");

            invalidTeacherException.AddData(
                key: nameof(Teacher.FirstName),
                values: "Text is required");

            invalidTeacherException.AddData(
                key: nameof(Teacher.MiddleName),
                values: "Text is required");

            invalidTeacherException.AddData(
                key: nameof(Teacher.LastName),
                values: "Text is required");

            invalidTeacherException.AddData(
                key: nameof(Teacher.CreatedDate),
                values: "Date is required");

            invalidTeacherException.AddData(
                key: nameof(Teacher.UpdatedDate),
                "Date is required",
                $"Date is the same as {nameof(Teacher.CreatedDate)}");

            invalidTeacherException.AddData(
                key: nameof(Teacher.CreatedBy),
                values: "Id is required");

            invalidTeacherException.AddData(
                key: nameof(Teacher.UpdatedBy),
                values: "Id is required");

            var expectedTeacherValidationException =
                new TeacherValidationException(invalidTeacherException);

            // when
            ValueTask <Teacher> createTeacherTask =
                this.teacherService.ModifyTeacherAsync(invalidTeacher);

            // then
            await Assert.ThrowsAsync <TeacherValidationException>(() =>
                                                                  createTeacherTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }