public async Task ShouldThrowValidationExceptionOnModifyIfAttendanceDoesntExistAndLogItAsync()
        {
            // given
            int            randomNegativeMinutes = GetNegativeRandomNumber();
            DateTimeOffset dateTime              = GetRandomDateTime();
            Attendance     randomAttendance      = CreateRandomAttendance(dateTime);
            Attendance     nonExistentAttendance = randomAttendance;

            nonExistentAttendance.CreatedDate = dateTime.AddMinutes(randomNegativeMinutes);
            Attendance noAttendance = null;
            var        notFoundAttendanceException = new NotFoundAttendanceException(nonExistentAttendance.Id);

            var expectedAttendanceValidationException =
                new AttendanceValidationException(notFoundAttendanceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAttendanceByIdAsync(nonExistentAttendance.Id))
            .ReturnsAsync(noAttendance);

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

            // when
            ValueTask <Attendance> modifyAttendanceTask =
                this.attendanceService.ModifyAttendanceAsync(nonExistentAttendance);

            // then
            await Assert.ThrowsAsync <AttendanceValidationException>(() =>
                                                                     modifyAttendanceTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectAttendanceByIdAsync(nonExistentAttendance.Id),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowValidatonExceptionOnDeleteWhenStorageAttendanceIsInvalidAndLogItAsync()
        {
            // given
            DateTimeOffset dateTimeOffset        = GetRandomDateTime();
            Attendance     randomAttendance      = CreateRandomAttendance(dateTime: dateTimeOffset);
            Guid           inputAttendanceId     = randomAttendance.Id;
            Attendance     inputAttendance       = randomAttendance;
            Attendance     nullStorageAttendance = null;

            var notFoundAttendanceException = new NotFoundAttendanceException(inputAttendanceId);

            var expectedAttendanceValidationException =
                new AttendanceValidationException(notFoundAttendanceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAttendanceByIdAsync(inputAttendanceId))
            .ReturnsAsync(nullStorageAttendance);

            // when
            ValueTask <Attendance> actualAttendanceTask =
                this.attendanceService.RemoveAttendanceByIdAsync(inputAttendanceId);

            // then
            await Assert.ThrowsAsync <AttendanceValidationException>(() => actualAttendanceTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectAttendanceByIdAsync(inputAttendanceId),
                                          Times.Once);

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Exemplo n.º 3
0
        public async Task ShouldThrowValidationExceptionOnRetrieveWhenStorageAttendanceIsNullAndLogItAsync()
        {
            // given
            DateTimeOffset dateTimeOffset          = GetRandomDateTime();
            Guid           randomAttendanceId      = Guid.NewGuid();
            Guid           inputAttendanceId       = randomAttendanceId;
            Attendance     nullAttendance          = default;
            var            nullAttendanceException = new NotFoundAttendanceException(attendanceId: inputAttendanceId);

            var expectedValidationException =
                new AttendanceValidationException(nullAttendanceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAttendanceByIdAsync(inputAttendanceId))
            .ReturnsAsync(nullAttendance);

            // when
            ValueTask <Attendance> retrieveAttendanceTask =
                this.attendanceService.RetrieveAttendanceByIdAsync(inputAttendanceId);

            // then
            await Assert.ThrowsAsync <AttendanceValidationException>(() =>
                                                                     retrieveAttendanceTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectAttendanceByIdAsync(inputAttendanceId),
                                          Times.Once);

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

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