Example #1
0
        public void SaveMedicalVendorInvoiceThrowsExceptionWhenSavingInvoiceFails()
        {
            var medicalVendorInvoice = new PhysicianInvoice();

            Expect.Call(_mockedValidator.IsValid(medicalVendorInvoice)).Return(true);
            ExpectGetDataAccessAdapterAndDispose(2);
            ExpectFetchEntityCollection();
            ExpectTransaction("MedicalVendorInvoiceRepository.SaveMedicalVendorInvoice", false);
            ExpectSaveEntity(false, true);
            Expect.Call(_medicalVendorInvoiceFactory.CreateMedicalVendorInvoiceEntity(medicalVendorInvoice))
            .Return(new PhysicianInvoiceEntity());

            _mocks.ReplayAll();
            _medicalVendorInvoiceRepository.SaveMedicalVendorInvoice(medicalVendorInvoice);
            _mocks.VerifyAll();
        }
 public void CreateMedicalVendorInvoiceEntityThrowsExceptionWhenGivenNullInvoice()
 {
     _mocks.ReplayAll();
     _medicalVendorInvoiceFactory.CreateMedicalVendorInvoiceEntity(null);
     _mocks.VerifyAll();
 }
        public void SaveMedicalVendorInvoice(PhysicianInvoice medicalVendorInvoice)
        {
            if (medicalVendorInvoice == null)
            {
                throw new ArgumentNullException("medicalVendorInvoice", "The given MedicalVendorInvoice cannot be null.");
            }

            if (!_validator.IsValid(medicalVendorInvoice))
            {
                throw new InvalidObjectException <PhysicianInvoice>(_validator);
            }

            // TODO: TEST (Integration)
            var medicalVendorInvoiceEntities = new EntityCollection <PhysicianInvoiceEntity>();
            IRelationPredicateBucket bucket  = new RelationPredicateBucket
                                                   (PhysicianInvoiceFields.PhysicianId == medicalVendorInvoice.PhysicianId &
                                                   PhysicianInvoiceFields.PayPeriodStartDate <= medicalVendorInvoice.PayPeriodStartDate &
                                                   PhysicianInvoiceFields.PayPeriodEndDate >= medicalVendorInvoice.PayPeriodStartDate);

            bucket.PredicateExpression.AddWithOr(
                PhysicianInvoiceFields.PhysicianId == medicalVendorInvoice.PhysicianId &
                PhysicianInvoiceFields.PayPeriodStartDate <= medicalVendorInvoice.PayPeriodEndDate &
                PhysicianInvoiceFields.PayPeriodEndDate >= medicalVendorInvoice.PayPeriodEndDate);
            using (var myAdapter = PersistenceLayer.GetDataAccessAdapter())
            {
                myAdapter.FetchEntityCollection(medicalVendorInvoiceEntities, bucket);
            }
            if (!medicalVendorInvoiceEntities.IsEmpty())
            {
                throw new OverlappingInvoiceException(medicalVendorInvoice);
            }

            PhysicianInvoiceEntity medicalVendorInvoiceEntity = _medicalVendorInvoiceFactory.
                                                                CreateMedicalVendorInvoiceEntity(medicalVendorInvoice);

            using (var myAdapter = PersistenceLayer.GetDataAccessAdapter())
            {
                myAdapter.StartTransaction(IsolationLevel.ReadCommitted, "MedicalVendorInvoiceRepository.SaveMedicalVendorInvoice");
                try
                {
                    if (!myAdapter.SaveEntity(medicalVendorInvoiceEntity, true))
                    {
                        throw new PersistenceFailureException();
                    }

                    var medicalVendorInvoiceItemEntities = _medicalVendorInvoiceItemFactory.
                                                           CreateMedicalVendorInvoiceItemEntities(medicalVendorInvoiceEntity.PhysicianInvoiceId,
                                                                                                  medicalVendorInvoice.MedicalVendorInvoiceItems);

                    if (myAdapter.SaveEntityCollection(medicalVendorInvoiceItemEntities) == 0)
                    {
                        throw new PersistenceFailureException();
                    }
                    myAdapter.Commit();
                }
                catch (Exception)
                {
                    myAdapter.Rollback();
                    throw;
                }
            }
        }