public async Task Handler_ShouldReturnTagRequirements_IsInUse_AfterPreservationStarted()
        {
            using (var context = new PreservationContext(_dbContextOptions, _plantProvider, _eventDispatcher, _currentUserProvider))
            {
                var tag = context.Tags.Include(t => t.Requirements).Single();
                tag.StartPreservation();
                context.SaveChangesAsync().Wait();
            }

            using (var context = new PreservationContext(_dbContextOptions, _plantProvider, _eventDispatcher, _currentUserProvider))
            {
                var query = new GetTagRequirementsQuery(_tagId, false, false);
                var dut   = new GetTagRequirementsQueryHandler(context);

                var result = await dut.Handle(query, default);

                Assert.IsNotNull(result);
                Assert.AreEqual(ResultType.Ok, result.ResultType);

                foreach (var requirement in result.Data)
                {
                    Assert.IsTrue(requirement.IsInUse);
                }
            }
        }
        public async Task Handler_ShouldReturnTagRequirementsWithOrderedFields()
        {
            using (var context = new PreservationContext(_dbContextOptions, _plantProviderMock.Object, _eventDispatcher, _currentUserProvider))
            {
                var query = new GetTagRequirementsQuery(_tagId, false, false);
                var dut   = new GetTagRequirementsQueryHandler(context);

                var result = await dut.Handle(query, default);

                Assert.IsNotNull(result);
                Assert.AreEqual(ResultType.Ok, result.ResultType);

                var requirementWithTwoCheckBoxes = result.Data.Single(r => r.Id == _requirementWithTwoCheckBoxesId);
                Assert.AreEqual(2, requirementWithTwoCheckBoxes.Fields.Count);

                Assert.AreEqual(_firstCbFieldId, requirementWithTwoCheckBoxes.Fields.ElementAt(0).Id);
                Assert.AreEqual(_secondCbFieldId, requirementWithTwoCheckBoxes.Fields.ElementAt(1).Id);

                var requirementWithThreeNumbers = result.Data.Single(r => r.Id == _requirementWithThreeNumberShowPrevId);
                Assert.AreEqual(3, requirementWithThreeNumbers.Fields.Count);

                Assert.AreEqual(_firstNumberFieldId, requirementWithThreeNumbers.Fields.ElementAt(0).Id);
                Assert.AreEqual(_secondNumberFieldId, requirementWithThreeNumbers.Fields.ElementAt(1).Id);
                Assert.AreEqual(_thirdNumberFieldId, requirementWithThreeNumbers.Fields.ElementAt(2).Id);
            }
        }
        public async Task Handler_ShouldReturnTagRequirements_AfterPreservationStarted()
        {
            using (var context = new PreservationContext(_dbContextOptions, _plantProvider, _eventDispatcher, _currentUserProvider))
            {
                var tag = context.Tags.Include(t => t.Requirements).Single();
                tag.StartPreservation();
                context.SaveChangesAsync().Wait();
            }

            using (var context = new PreservationContext(_dbContextOptions, _plantProvider, _eventDispatcher, _currentUserProvider))
            {
                _timeProvider.ElapseWeeks(_requestTimeAfterPreservationStartedInWeeks);

                var query = new GetTagRequirementsQuery(_tagId, false, false);
                var dut   = new GetTagRequirementsQueryHandler(context);

                var result = await dut.Handle(query, default);

                Assert.IsNotNull(result);
                Assert.AreEqual(ResultType.Ok, result.ResultType);

                foreach (var requirement in result.Data)
                {
                    Assert.IsTrue(requirement.NextDueTimeUtc.HasValue);
                    Assert.IsNotNull(requirement.NextDueTimeUtc.Value);
                    Assert.IsNotNull(requirement.NextDueAsYearAndWeek);
                    Assert.AreEqual(_interval, requirement.IntervalWeeks);
                    Assert.AreEqual(_interval - _requestTimeAfterPreservationStartedInWeeks, requirement.NextDueWeeks);
                }

                AssertRequirements(result.Data);
            }
        }
        public async Task Handler_ShouldReturnTagRequirementsWithNumbers_AfterRecordingNumberFields()
        {
            var numberFieldWithNaId     = _thirdNumberFieldId;
            var numberFieldWithDoubleId = _secondNumberFieldId;
            var number = 1282.91;

            using (var context = new PreservationContext(_dbContextOptions, _plantProviderMock.Object, _eventDispatcher, _currentUserProvider))
            {
                var tag = context.Tags.Include(t => t.Requirements).Single();
                tag.StartPreservation();
                context.SaveChangesAsync().Wait();

                var requirementDefinition = context.RequirementDefinitions.Include(rd => rd.Fields)
                                            .Single(rd => rd.Id == _requirementDefinitionWithThreeNumberShowPrevId);
                var requirement = context.TagRequirements.Single(r => r.Id == _requirementWithThreeNumberShowPrevId);

                requirement.RecordNumberIsNaValues(
                    new List <int>
                {
                    numberFieldWithNaId
                },
                    requirementDefinition);
                requirement.RecordNumberValues(
                    new Dictionary <int, double?>
                {
                    { numberFieldWithDoubleId, number }
                },
                    requirementDefinition);
                context.SaveChangesAsync().Wait();
            }

            using (var context = new PreservationContext(_dbContextOptions, _plantProviderMock.Object, _eventDispatcher, _currentUserProvider))
            {
                var query = new GetTagRequirementsQuery(_tagId, false, false);
                var dut   = new GetTagRequirementsQueryHandler(context);

                var result = await dut.Handle(query, default);

                Assert.IsNotNull(result);
                Assert.AreEqual(ResultType.Ok, result.ResultType);

                var requirementWithThreeNumberShowPrev = result.Data.Single(r => r.Id == _requirementWithThreeNumberShowPrevId);
                Assert.AreEqual(3, requirementWithThreeNumberShowPrev.Fields.Count);

                var fieldWithNa = requirementWithThreeNumberShowPrev.Fields.Single(f => f.Id == numberFieldWithNaId);
                AssertNaNumberInCurrentValue(fieldWithNa);

                var fieldWithDouble = requirementWithThreeNumberShowPrev.Fields.Single(f => f.Id == numberFieldWithDoubleId);
                AssertNumberInCurrentValue(fieldWithDouble, number);
            }
        }
        public async Task Handler_ShouldReturnNotFound_WhenTagNotFound()
        {
            using (var context = new PreservationContext(_dbContextOptions, _plantProviderMock.Object, _eventDispatcher, _currentUserProvider))
            {
                var query = new GetTagRequirementsQuery(0, false, false);
                var dut   = new GetTagRequirementsQueryHandler(context);

                var result = await dut.Handle(query, default);

                Assert.IsNotNull(result);
                Assert.AreEqual(ResultType.NotFound, result.ResultType);
                Assert.IsNull(result.Data);
            }
        }
        public async Task Handler_ShouldReturnPreviousValues_AfterRecordingNumberFieldsAndPreserving()
        {
            var number = 1.91;

            using (var context = new PreservationContext(_dbContextOptions, _plantProviderMock.Object, _eventDispatcher, _currentUserProvider))
            {
                var tag = context.Tags.Include(t => t.Requirements).Single();
                tag.StartPreservation();
                context.SaveChangesAsync().Wait();

                var requirementDefinition = context.RequirementDefinitions.Include(rd => rd.Fields)
                                            .Single(rd => rd.Id == _requirementDefinitionWithThreeNumberShowPrevId);
                var requirement = context.TagRequirements.Single(r => r.Id == _requirementWithThreeNumberShowPrevId);

                requirement.RecordNumberValues(
                    new Dictionary <int, double?>
                {
                    { _firstNumberFieldId, number },
                    { _secondNumberFieldId, number },
                    { _thirdNumberFieldId, number }
                },
                    requirementDefinition);
                context.SaveChangesAsync().Wait();

                _timeProvider.ElapseWeeks(_interval);
                tag.Preserve(new Mock <Person>().Object);
                context.SaveChangesAsync().Wait();
            }

            using (var context = new PreservationContext(_dbContextOptions, _plantProvider, _eventDispatcher, _currentUserProvider))
            {
                var query = new GetTagRequirementsQuery(_tagId, false, false);
                var dut   = new GetTagRequirementsQueryHandler(context);

                var result = await dut.Handle(query, default);

                Assert.IsNotNull(result);
                Assert.AreEqual(ResultType.Ok, result.ResultType);

                var requirementWithThreeNumberShowPrev = result.Data.Single(r => r.Id == _requirementWithThreeNumberShowPrevId);
                Assert.AreEqual(3, requirementWithThreeNumberShowPrev.Fields.Count);

                foreach (var fieldDto in requirementWithThreeNumberShowPrev.Fields)
                {
                    AssertNumberInPreviousValue(fieldDto, number);
                }
            }
        }
        public async Task Handler_ShouldReturnTagRequirements_Voided_and_Unvoided()
        {
            using (var context = new PreservationContext(_dbContextOptions, _plantProvider, _eventDispatcher, _currentUserProvider))
            {
                var query = new GetTagRequirementsQuery(_tagId, true, false);
                var dut   = new GetTagRequirementsQueryHandler(context);

                var result = await dut.Handle(query, default);

                Assert.IsNotNull(result);
                Assert.AreEqual(ResultType.Ok, result.ResultType);

                Assert.IsTrue(result.Data.FindIndex(req => req.IsVoided) > -1);

                AssertRequirements(result.Data);
            }
        }
        public async Task Handler_ShouldReturnTagRequirements_NotInUse_BeforePreservationStarted()
        {
            using (var context = new PreservationContext(_dbContextOptions, _plantProvider, _eventDispatcher, _currentUserProvider))
            {
                var query = new GetTagRequirementsQuery(_tagId, false, false);
                var dut   = new GetTagRequirementsQueryHandler(context);

                var result = await dut.Handle(query, default);

                Assert.IsNotNull(result);
                Assert.AreEqual(ResultType.Ok, result.ResultType);

                foreach (var requirement in result.Data)
                {
                    Assert.IsFalse(requirement.IsInUse);
                }
            }
        }
        public async Task Handler_ShouldReturnTagRequirementsWithCheckBoxFieldAndComment_AfterRecordingCheckBoxFieldAndComment()
        {
            var cbFieldId = _firstCbFieldId;

            using (var context = new PreservationContext(_dbContextOptions, _plantProviderMock.Object, _eventDispatcher, _currentUserProvider))
            {
                var tag = context.Tags.Include(t => t.Requirements).Single();
                tag.StartPreservation();
                context.SaveChangesAsync().Wait();

                var requirementDefinition = context.RequirementDefinitions.Include(rd => rd.Fields)
                                            .Single(rd => rd.Id == _requirementDefinitionWithTwoCheckBoxesId);
                var requirement = context.TagRequirements.Single(r => r.Id == _requirementWithTwoCheckBoxesId);
                requirement.RecordCheckBoxValues(
                    new Dictionary <int, bool> {
                    { cbFieldId, true }
                },
                    requirementDefinition);
                requirement.SetComment("CommentABC");
                context.SaveChangesAsync().Wait();
            }

            using (var context = new PreservationContext(_dbContextOptions, _plantProviderMock.Object, _eventDispatcher, _currentUserProvider))
            {
                var query = new GetTagRequirementsQuery(_tagId, false, false);
                var dut   = new GetTagRequirementsQueryHandler(context);

                var result = await dut.Handle(query, default);

                Assert.IsNotNull(result);
                Assert.AreEqual(ResultType.Ok, result.ResultType);

                var requirementWithTwoCheckBoxes = result.Data.Single(r => r.Id == _requirementWithTwoCheckBoxesId);
                Assert.AreEqual("CommentABC", requirementWithTwoCheckBoxes.Comment);
                Assert.AreEqual(2, requirementWithTwoCheckBoxes.Fields.Count);

                var field = requirementWithTwoCheckBoxes.Fields.Single(f => f.Id == cbFieldId);
                Assert.IsNotNull(field);
                Assert.IsNotNull(field.CurrentValue);
                Assert.IsInstanceOfType(field.CurrentValue, typeof(CheckBoxDetailsDto));
            }
        }
        public async Task Handler_ShouldReturnTagRequirementsWithAttachment_AfterRecordingAttachment()
        {
            var attachmentField      = _attachmentFieldId;
            var fieldValueAttachment = new FieldValueAttachment(TestPlant, Guid.Empty, "FilA.txt");

            using (var context = new PreservationContext(_dbContextOptions, _plantProviderMock.Object, _eventDispatcher, _currentUserProvider))
            {
                var tag = context.Tags.Include(t => t.Requirements).Single();
                tag.StartPreservation();
                context.SaveChangesAsync().Wait();

                var requirementDefinition = context.RequirementDefinitions.Include(rd => rd.Fields)
                                            .Single(rd => rd.Id == _requirementDefinitionWithOneAttachmentId);
                var requirement = context.TagRequirements.Single(r => r.Id == _requirementWithOneAttachmentId);

                requirement.RecordAttachment(
                    fieldValueAttachment,
                    attachmentField,
                    requirementDefinition);
                context.SaveChangesAsync().Wait();
            }

            using (var context = new PreservationContext(_dbContextOptions, _plantProviderMock.Object, _eventDispatcher, _currentUserProvider))
            {
                var query = new GetTagRequirementsQuery(_tagId, false, false);
                var dut   = new GetTagRequirementsQueryHandler(context);

                var result = await dut.Handle(query, default);

                Assert.IsNotNull(result);
                Assert.AreEqual(ResultType.Ok, result.ResultType);

                var requirementWithAttachment = result.Data.Single(r => r.Id == _requirementWithOneAttachmentId);
                Assert.AreEqual(1, requirementWithAttachment.Fields.Count);

                var fieldWithAttachment = requirementWithAttachment.Fields.Single(f => f.Id == attachmentField);
                AssertAttachmentField(fieldWithAttachment, fieldValueAttachment);
            }
        }
        public async Task Handler_ShouldReturnTagRequirements_NoDueDates_BeforePreservationStarted()
        {
            using (var context = new PreservationContext(_dbContextOptions, _plantProvider, _eventDispatcher, _currentUserProvider))
            {
                var query = new GetTagRequirementsQuery(_tagId, false, false);
                var dut   = new GetTagRequirementsQueryHandler(context);

                var result = await dut.Handle(query, default);

                Assert.IsNotNull(result);
                Assert.AreEqual(ResultType.Ok, result.ResultType);

                foreach (var requirement in result.Data)
                {
                    Assert.IsFalse(requirement.NextDueTimeUtc.HasValue);
                    Assert.IsNull(requirement.NextDueAsYearAndWeek);
                    Assert.IsFalse(requirement.ReadyToBePreserved);
                    Assert.AreEqual(_interval, requirement.IntervalWeeks);
                    Assert.IsNull(requirement.NextDueWeeks);
                }

                AssertRequirements(result.Data);
            }
        }