public bool TrySave(DataCollection collection, out DataCollectionHashCode hashCode)
 {
     hashCode = GetByDataCollectionId(collection.Id);
     if (hashCode == null || hashCode.UpdateHashCode(collection))
     {
         if (hashCode == null)
         {
             hashCode = collection.NewDataCollectionHashCode();
         }
         Save(hashCode);
         return true;
     }
     return false;
 }
        public void Send_SubmitForOrdReApproval_command_to_bus_for_a_secondary_approved_data_collection_if_changes_have_been_made()
        {
            // Qa has made changes to the data collection so must be advised that it will return to secondary for reapproval
            var dataCollection = CreateDataCollectionWithState(DataCollectionStatus.SecondaryApproved);
            dataCollection.Title = "No changes yet";
            var vm = Builder<ApprovalConfirmationViewModel>.CreateNew()
                .With(m => m.DataCollectionId = dataCollection.Id)
                .And(m => m.State = DataCollectionStatus.SecondaryApproved)
                .Build();
            var oldHashCode = new DataCollectionHashCode();
            oldHashCode.UpdateHashCode(dataCollection);
            CreateQaUser();

            dataCollection.Title = "We've made a change";
            _dataCollectionRepository.Get(dataCollection.Id).Returns(dataCollection);
            _hashCodeRepository.GetByDataCollectionId(dataCollection.Id).Returns(oldHashCode);

			_bus.When(c => c.Send(Arg.Any<Action<SubmitForSecondaryApproval>>())).Do(a =>
            {
				var rsc = new SubmitForSecondaryApproval();
				var lambda = a.Arg<Action<SubmitForSecondaryApproval>>();

                lambda(rsc);

                Assert.That(rsc.DataCollectionId, Is.EqualTo(dataCollection.Id), "Invalid data collection id passed to the bus");
                Assert.That(rsc.ApprovedOn, Is.EqualTo(dataCollection.CurrentState.StateChangedOn).Within(1).Minutes, "Invalid approval date passed to the bus");
                Assert.That(rsc.ApprovedBy, Is.EqualTo(QaId), "Invalid approver id passed to the bus");
            });

            _controller.WithCallTo(c => c.SubmitForReapproval(vm)).ShouldRedirectTo(x => x.Index);
			_bus.Received().Send(Arg.Any<Action<SubmitForSecondaryApproval>>());
            _hashCodeRepository.Received().Delete(oldHashCode);
        }
        public void Send_SubmitForFinalApproval_command_to_bus_for_a_qa_approved_data_collection_once_secondary_approved()
        {
            // Secondary approver actions to secondary approve the data collection (should pass by to qa approver)
            var dataCollection = CreateDataCollectionWithState(DataCollectionStatus.QaApproved);
            var vm = Builder<ApprovalConfirmationViewModel>.CreateNew()
                .With(m => m.DataCollectionId = dataCollection.Id)
                .And(m => m.State = DataCollectionStatus.QaApproved)
                .And(m => m.DoesNotViolateAgreements = true)
                .And(m => m.DoesNotViolateConfidentialityAndEthics = true)
                .Build();
            var hashCode = new DataCollectionHashCode();
            hashCode.UpdateHashCode(dataCollection);
            CreateOrdApprover();

            _dataCollectionRepository.Get(dataCollection.Id).Returns(dataCollection);
            _hashCodeRepository.GetByDataCollectionId(dataCollection.Id).Returns(hashCode);

            _bus.When(c => c.Send(Arg.Any<Action<SubmitForFinalApproval>>())).Do(a =>
            {
                var rsc = new SubmitForFinalApproval();
                var lambda = a.Arg<Action<SubmitForFinalApproval>>();

                lambda(rsc);

                Assert.That(rsc.DataCollectionId, Is.EqualTo(dataCollection.Id), "Invalid data collection id passed to the bus");
                Assert.That(rsc.ApprovedOn, Is.EqualTo(dataCollection.CurrentState.StateChangedOn).Within(1).Minutes, "Invalid approval date passed to the bus");
                Assert.That(rsc.ApprovedBy, Is.EqualTo(SecondaryApproverId), "Invalid approver id passed to the bus");
            });

            _controller.WithCallTo(c => c.Confirm(vm)).ShouldRenderView("Approved")
                .WithModel<ApprovalConfirmationViewModel>(m => m.ProposedState == DataCollectionStatus.SecondaryApproved);
        }
        public void Send_PublishDataCollection_command_to_bus_for_a_secondary_approved_data_collection_once_publication_approved()
        {
            // QA Approver actions to publish the data collection
            var dataCollection = CreateDataCollectionWithState(DataCollectionStatus.SecondaryApproved);
            var vm = Builder<ApprovalConfirmationViewModel>.CreateNew()
                .With(m => m.DataCollectionId = dataCollection.Id)
                .And(m => m.State = DataCollectionStatus.SecondaryApproved)
                .And(m => m.IsPublicationApproved = true)
                .Build();
            var hashCode = new DataCollectionHashCode();
            hashCode.UpdateHashCode(dataCollection);
            CreateQaUser();

            _dataCollectionRepository.Get(dataCollection.Id).Returns(dataCollection);
            _hashCodeRepository.GetByDataCollectionId(dataCollection.Id).Returns(hashCode);

            _bus.When(c => c.Send(Arg.Any<Action<PublishDataCollection>>())).Do(a =>
            {
                var rsc = new PublishDataCollection();
                var lambda = a.Arg<Action<PublishDataCollection>>();

                lambda(rsc);

                Assert.That(rsc.DataCollectionId, Is.EqualTo(dataCollection.Id), "Invalid data collection id passed to the bus");
                Assert.That(rsc.ApprovedOn, Is.EqualTo(dataCollection.CurrentState.StateChangedOn).Within(1).Minutes, "Invalid approval date passed to the bus");
                Assert.That(rsc.ApprovedBy, Is.EqualTo(QaId), "Invalid approver id passed to the bus");
            });

            _controller.WithCallTo(c => c.Confirm(vm)).ShouldRenderView("Approved")
                .WithModel<ApprovalConfirmationViewModel>(m => m.ProposedState == DataCollectionStatus.Publishing);
        }
        public void Return_confirm_view_with_validation_error_if_qa_approver_does_not_confirm_approval_checkbox()
        {
            var dataCollection = CreateDataCollectionWithState(DataCollectionStatus.Submitted);
            var vm = Builder<ApprovalConfirmationViewModel>.CreateNew()
                .With(m => m.DataCollectionId = dataCollection.Id)
                .And(m => m.State = DataCollectionStatus.Submitted)
                .And(m => m.IsQaApproved = false)
                .Build();
            var hashCode = new DataCollectionHashCode();
            hashCode.UpdateHashCode(dataCollection);
            CreateQaUser();
            _dataCollectionRepository.Get(dataCollection.Id).Returns(dataCollection);
            _hashCodeRepository.GetByDataCollectionId(dataCollection.Id).Returns(hashCode);

            _controller.WithCallTo(c => c.Confirm(vm)).ShouldRenderDefaultView()
                .WithModel<ApprovalConfirmationViewModel>();
            Assert.That(_controller.ModelState.IsValid, Is.False);
        }