public void ShouldNotUpdateContactManagerWhenContactLastUpdatedIdIncorrect()
        {
            var contactProxy           = new ContactProxy();
            var contactAggregateProxy  = new ContactAggregateProxy();
            var contactAggregate       = GetNewContactAggregate();
            var contactManager         = GetContact();
            var contactManagerResponse = contactProxy.InsertContact(contactManager);

            var contact         = GetContact();
            var contactResponse = contactProxy.InsertContact(contact);

            contactAggregate.Id                    = contactResponse.DataObject.InsertedContactId;
            contactAggregate.LastUpdatedId         = contactResponse.DataObject.LastUpdatedId.ToLower();
            contactAggregate.ContactManager.Id     = contactManagerResponse.DataObject.InsertedContactId;
            contactAggregate.Company.LastUpdatedId = contactManagerResponse.DataObject.LastUpdatedId;

            var aggregateResponse = contactAggregateProxy.UpdateContactAggregate(contactAggregate, contactResponse.DataObject.InsertedContactId);

            Assert.IsFalse(aggregateResponse.IsSuccessfull);
            Assert.AreEqual(HttpStatusCode.BadRequest, aggregateResponse.StatusCode);
            Assert.AreEqual("\"Record to be updated has changed since last read.\"", aggregateResponse.RawResponse);

            var updatedContact = contactProxy.GetContact(contactManagerResponse.DataObject.InsertedContactId);

            Assert.AreEqual(contactManagerResponse.DataObject.LastUpdatedId, updatedContact.DataObject.LastUpdatedId);
        }
        public void ShouldUpdateContactAndAddNewCompanyAndContactManager()
        {
            var contactProxy          = new ContactProxy();
            var contactAggregateProxy = new ContactAggregateProxy();
            var contact         = GetContact();
            var contactResponse = contactProxy.InsertContact(contact);

            Assert.IsTrue(contactResponse.IsSuccessfull);

            var contactAggregate = GetNewContactAggregate();

            var contactAggregateResponse = contactAggregateProxy.UpdateContactAggregate(contactAggregate, contactResponse.DataObject.InsertedContactId);

            Assert.IsTrue(contactAggregateResponse.IsSuccessfull, "Failed to update existing contact and add new company and contact manager");
            Assert.IsNotNull(contactAggregateResponse.DataObject);
            Assert.AreEqual(contactResponse.DataObject.InsertedContactId, contactAggregateResponse.DataObject.UpdatedContactId);
            Assert.IsTrue(contactAggregateResponse.DataObject.CompanyId > 0, "Invalid CompanyId returned from UpdateContactAggregate when inserting new company");
            Assert.IsTrue(contactAggregateResponse.DataObject.CompanyLastUpdatedId.Length > 0, "Invalid CompanyLastUpdatedId returned from UpdateContactAggregate when inserting new company");
            Assert.IsTrue(contactAggregateResponse.DataObject.ContactManagerId > 0, "Invalid ContactManagerId returned from UpdateContactAggregate when inserting new contact manager");
            Assert.IsTrue(contactAggregateResponse.DataObject.ContactManagerLastUpdatedId.Length > 0, "Invalid ContactManagerLastUpdatedId returned from UpdateContactAggregate when inserting new contact manager");

            var updatedContact = contactProxy.GetContact(contactResponse.DataObject.InsertedContactId);

            AssertUpdatedContact(contactAggregate, updatedContact);

            var insertedAggregate =
                contactAggregateProxy.GetContactAggregate(contactResponse.DataObject.InsertedContactId);

            Assert.IsTrue(insertedAggregate.IsSuccessfull);
            Assert.IsNotNull(insertedAggregate.DataObject);
            Assert.IsNotNull(insertedAggregate.DataObject.Company, "New company not associated with existing contact");
            Assert.IsNotNull(insertedAggregate.DataObject.ContactManager, "New contact manager not associated with existing contact");
            Assert.AreEqual(contactAggregate.Company.Name, insertedAggregate.DataObject.Company.Name);
            Assert.AreEqual(contactAggregate.ContactManager.GivenName, insertedAggregate.DataObject.ContactManager.GivenName);
        }
        public void ShouldAddNewContactWithNewCompanyAndContactManager()
        {
            var contactAggregateProxy = new ContactAggregateProxy();
            var contactProxy          = new ContactProxy();
            var companyProxy          = new CompanyProxy();
            var contactAggregate      = GetNewContactAggregate();
            var Response = contactAggregateProxy.InsertContactAggregate(contactAggregate);

            Assert.IsTrue(Response.IsSuccessfull, "Contact aggregate insert failed.");
            Assert.IsNotNull(Response.DataObject);
            Assert.IsTrue(Response.DataObject.InsertedContactId > 0, "Invalid InsertedContactId returned from InsertContactAggregate");
            Assert.IsTrue(Response.DataObject.LastUpdatedId.Length > 0, "Invalid LastUpdatedId returned");
            Assert.IsTrue(Response.DataObject.LastModified > DateTime.UtcNow.AddMinutes(-5), "Invalid LastModified returned");
            Assert.IsTrue(Response.DataObject.CompanyId > 0, "Invalid CompanyId returned from InsertContactAggregate when inserting new company");
            Assert.IsTrue(Response.DataObject.CompanyLastUpdatedId.Length > 0, "Invalid CompanyLastUpdatedId returned from InsertContactAggregate when inserting new company");
            Assert.IsTrue(Response.DataObject.ContactManagerId > 0, "Invalid ContactManagerId returned from InsertContactAggregate when inserting new contact manager");
            Assert.IsTrue(Response.DataObject.ContactManagerLastUpdatedId.Length > 0, "Invalid ContactManagerLastUpdatedId returned from InsertContactAggregate when inserting new contact manager");

            var contactResponse = contactProxy.GetContact(Response.DataObject.InsertedContactId);

            Assert.IsTrue(contactResponse.IsSuccessfull, "Contact not found");
            Assert.IsNotNull(contactResponse.DataObject);
            Assert.IsNotNull(contactResponse.DataObject.CompanyId);
            Assert.IsNotNull(contactResponse.DataObject.ContactManagerId);

            var contactManagerResponse = contactProxy.GetContact(contactResponse.DataObject.ContactManagerId.Value);

            Assert.IsTrue(contactManagerResponse.IsSuccessfull, "Contact manager not found");
            Assert.AreEqual(contactAggregate.ContactManager.FamilyName, contactManagerResponse.DataObject.FamilyName);

            var companyResponse = companyProxy.GetCompany(contactResponse.DataObject.CompanyId.Value);

            Assert.IsTrue(companyResponse.IsSuccessfull, "Company not found");
            Assert.AreEqual(contactAggregate.Company.Name, companyResponse.DataObject.Name);
        }
        public void ShouldAddNewContactAndUpdateCompanyAndContactManager()
        {
            var contactAggregateProxy = new ContactAggregateProxy();
            var contactProxy          = new ContactProxy();
            var companyProxy          = new CompanyProxy();

            var companyDetail   = GetCompany();
            var companyResponse = companyProxy.InsertCompany(companyDetail);

            var contactManager = GetContact();

            var contactManagerResponse = contactProxy.InsertContact(contactManager);
            var contactAggregate       = GetNewContactAggregate();

            contactAggregate.Company.Id                   = companyResponse.DataObject.InsertedCompanyId;
            contactAggregate.Company.LastUpdatedId        = companyResponse.DataObject.LastUpdatedId;
            contactAggregate.ContactManager.Id            = contactManagerResponse.DataObject.InsertedContactId;
            contactAggregate.ContactManager.LastUpdatedId = contactManagerResponse.DataObject.LastUpdatedId;

            var contactAggregateResponse = contactAggregateProxy.InsertContactAggregate(contactAggregate);

            Assert.IsTrue(contactAggregateResponse.IsSuccessfull, "Contact aggregate with new contact and updated company and contact manager failed");
            Assert.IsNotNull(contactAggregateResponse.DataObject);
            Assert.IsTrue(contactAggregateResponse.DataObject.CompanyId > 0, "Invalid CompanyId returned from InsertContactAggregate when updating company");
            Assert.IsTrue(contactAggregateResponse.DataObject.CompanyLastUpdatedId.Length > 0, "Invalid CompanyLastUpdatedId returned from InsertContactAggregate when updating company");
            Assert.IsTrue(contactAggregateResponse.DataObject.ContactManagerId > 0, "Invalid ContactManagerId returned from InsertContactAggregate when updating contact manager");
            Assert.IsTrue(contactAggregateResponse.DataObject.ContactManagerLastUpdatedId.Length > 0, "Invalid ContactManagerLastUpdatedId returned from InsertContactAggregate when updating contact manager");

            var dbContactAggregate =
                contactAggregateProxy.GetContactAggregate(contactAggregateResponse.DataObject.InsertedContactId);

            Assert.IsTrue(dbContactAggregate.IsSuccessfull);
            Assert.IsNotNull(dbContactAggregate.DataObject);
            Assert.IsNotNull(dbContactAggregate.DataObject.ContactManager.Id);
            Assert.IsNotNull(dbContactAggregate.DataObject.Company.Id);
            Assert.AreEqual(companyResponse.DataObject.InsertedCompanyId, dbContactAggregate.DataObject.Company.Id.Value, "Existing company not attached to contact aggregate");
            Assert.AreEqual(contactManagerResponse.DataObject.InsertedContactId, dbContactAggregate.DataObject.ContactManager.Id.Value, "Existing contact (manager) not attached to contact aggregate");

            var dbContactManager = contactProxy.GetContact(dbContactAggregate.DataObject.ContactManager.Id.Value);

            Assert.IsTrue(dbContactManager.IsSuccessfull);
            Assert.IsNotNull(dbContactManager.DataObject);

            AssertContactManager(dbContactAggregate.DataObject.ContactManager, dbContactManager.DataObject);
            Assert.AreEqual(dbContactAggregate.DataObject.ContactManager.LastUpdatedId,
                            dbContactManager.DataObject.LastUpdatedId, "LastUpdatedId not updated for contact manager");

            var dbCompany = companyProxy.GetCompany(dbContactAggregate.DataObject.Company.Id.Value);

            Assert.IsTrue(dbCompany.IsSuccessfull);
            Assert.IsNotNull(dbCompany.DataObject);

            AssertCompany(dbContactAggregate.DataObject.Company, dbCompany.DataObject);
            Assert.AreEqual(dbContactAggregate.DataObject.Company.LastUpdatedId, dbCompany.DataObject.LastUpdatedId,
                            "LastUpdatedId not updated for company");
        }
        public void ShouldNotModifyExistingContactFieldsNotContainedInAggregateModel()
        {
            var contactProxy          = new ContactProxy();
            var contactAggregateProxy = new ContactAggregateProxy();
            var contactAggregate      = GetNewContactAggregate();

            var contact         = GetCompleteContact();
            var contactResponse = contactProxy.InsertContact(contact);

            contactAggregate.Id            = contactResponse.DataObject.InsertedContactId;
            contactAggregate.LastUpdatedId = contactResponse.DataObject.LastUpdatedId;

            var aggregateResponse = contactAggregateProxy.UpdateContactAggregate(contactAggregate, contactResponse.DataObject.InsertedContactId);

            Assert.IsTrue(aggregateResponse.IsSuccessfull);

            var updatedContactResponse = contactProxy.GetContact(contactResponse.DataObject.InsertedContactId);

            Assert.IsTrue(updatedContactResponse.IsSuccessfull);
            Assert.IsNotNull(updatedContactResponse.DataObject);
            var updatedContact = updatedContactResponse.DataObject;

            Assert.AreEqual(contact.AutoSendStatement, updatedContact.AutoSendStatement);
            Assert.AreEqual(contact.BpayDetails.BillerCode, updatedContact.BpayDetails.BillerCode);
            Assert.AreEqual(contact.BpayDetails.CRN, updatedContact.BpayDetails.CRN);
            Assert.AreEqual(contact.ChequeDetails.AcceptCheque, updatedContact.ChequeDetails.AcceptCheque);
            Assert.AreEqual(contact.ChequeDetails.ChequePayableTo, updatedContact.ChequeDetails.ChequePayableTo);
            Assert.AreEqual(contact.CustomField1, updatedContact.CustomField1);
            Assert.AreEqual(contact.CustomField2, updatedContact.CustomField2);
            Assert.AreEqual(contact.DefaultPurchaseDiscount, updatedContact.DefaultPurchaseDiscount);
            Assert.AreEqual(contact.DefaultSaleDiscount, updatedContact.DefaultSaleDiscount);
            Assert.AreEqual(contact.DirectDepositDetails.AcceptDirectDeposit, updatedContact.DirectDepositDetails.AcceptDirectDeposit);
            Assert.AreEqual(contact.DirectDepositDetails.AccountBSB, updatedContact.DirectDepositDetails.AccountBSB);
            Assert.AreEqual(contact.DirectDepositDetails.AccountName, updatedContact.DirectDepositDetails.AccountName);
            Assert.AreEqual(contact.DirectDepositDetails.AccountNumber, updatedContact.DirectDepositDetails.AccountNumber);
            Assert.AreEqual(contact.HomePhone, updatedContact.HomePhone);
            Assert.AreEqual(contact.LinkedInProfile, updatedContact.LinkedInProfile);
            Assert.AreEqual(contact.OtherAddress.City, updatedContact.OtherAddress.City);
            Assert.AreEqual(contact.OtherAddress.Country, updatedContact.OtherAddress.Country);
            Assert.AreEqual(contact.OtherAddress.Postcode, updatedContact.OtherAddress.Postcode);
            Assert.AreEqual(contact.OtherAddress.State, updatedContact.OtherAddress.State);
            Assert.AreEqual(contact.OtherAddress.Street, updatedContact.OtherAddress.Street);
            Assert.AreEqual(contact.OtherPhone, updatedContact.OtherPhone);
            Assert.AreEqual(contact.PurchaseTradingTerms.TradingTermsInterval, updatedContact.PurchaseTradingTerms.TradingTermsInterval);
            Assert.AreEqual(contact.PurchaseTradingTerms.TradingTermsIntervalType, updatedContact.PurchaseTradingTerms.TradingTermsIntervalType);
            Assert.AreEqual(contact.PurchaseTradingTerms.TradingTermsType, updatedContact.PurchaseTradingTerms.TradingTermsType);
            Assert.AreEqual(contact.SaleTradingTerms.TradingTermsInterval, updatedContact.SaleTradingTerms.TradingTermsInterval);
            Assert.AreEqual(contact.SaleTradingTerms.TradingTermsIntervalType, updatedContact.SaleTradingTerms.TradingTermsIntervalType);
            Assert.AreEqual(contact.SaleTradingTerms.TradingTermsType, updatedContact.SaleTradingTerms.TradingTermsType);
            Assert.AreEqual(contact.SkypeId, updatedContact.SkypeId);
            Assert.AreEqual(contact.TwitterId, updatedContact.TwitterId);
            Assert.AreEqual(contact.WebsiteUrl, updatedContact.WebsiteUrl);
        }
        public void ShouldAddNewContactAndUpdateCompanyAndContactManager()
        {
            var contactAggregateProxy = new ContactAggregateProxy();
            var contactProxy = new ContactProxy();
            var companyProxy = new CompanyProxy();

            var companyDetail = GetCompany();
            var companyResponse = companyProxy.InsertCompany(companyDetail);

            var contactManager = GetContact();

            var contactManagerResponse = contactProxy.InsertContact(contactManager);
            var contactAggregate = GetNewContactAggregate();
            contactAggregate.Company.Id = companyResponse.DataObject.InsertedCompanyId;
            contactAggregate.Company.LastUpdatedId = companyResponse.DataObject.LastUpdatedId;
            contactAggregate.ContactManager.Id = contactManagerResponse.DataObject.InsertedContactId;
            contactAggregate.ContactManager.LastUpdatedId = contactManagerResponse.DataObject.LastUpdatedId;

            var contactAggregateResponse = contactAggregateProxy.InsertContactAggregate(contactAggregate);
            Assert.IsTrue(contactAggregateResponse.IsSuccessfull, "Contact aggregate with new contact and updated company and contact manager failed");
            Assert.IsNotNull(contactAggregateResponse.DataObject);
            Assert.IsTrue(contactAggregateResponse.DataObject.CompanyId > 0, "Invalid CompanyId returned from InsertContactAggregate when updating company");
            Assert.IsTrue(contactAggregateResponse.DataObject.CompanyLastUpdatedId.Length > 0, "Invalid CompanyLastUpdatedId returned from InsertContactAggregate when updating company");
            Assert.IsTrue(contactAggregateResponse.DataObject.ContactManagerId > 0, "Invalid ContactManagerId returned from InsertContactAggregate when updating contact manager");
            Assert.IsTrue(contactAggregateResponse.DataObject.ContactManagerLastUpdatedId.Length > 0, "Invalid ContactManagerLastUpdatedId returned from InsertContactAggregate when updating contact manager");

            var dbContactAggregate =
                contactAggregateProxy.GetContactAggregate(contactAggregateResponse.DataObject.InsertedContactId);
            Assert.IsTrue(dbContactAggregate.IsSuccessfull);
            Assert.IsNotNull(dbContactAggregate.DataObject);
            Assert.IsNotNull(dbContactAggregate.DataObject.ContactManager.Id);
            Assert.IsNotNull(dbContactAggregate.DataObject.Company.Id);
            Assert.AreEqual(companyResponse.DataObject.InsertedCompanyId, dbContactAggregate.DataObject.Company.Id.Value, "Existing company not attached to contact aggregate");
            Assert.AreEqual(contactManagerResponse.DataObject.InsertedContactId, dbContactAggregate.DataObject.ContactManager.Id.Value, "Existing contact (manager) not attached to contact aggregate");

            var dbContactManager = contactProxy.GetContact(dbContactAggregate.DataObject.ContactManager.Id.Value);
            Assert.IsTrue(dbContactManager.IsSuccessfull);
            Assert.IsNotNull(dbContactManager.DataObject);

            AssertContactManager(dbContactAggregate.DataObject.ContactManager, dbContactManager.DataObject);
            Assert.AreEqual(dbContactAggregate.DataObject.ContactManager.LastUpdatedId,
                dbContactManager.DataObject.LastUpdatedId, "LastUpdatedId not updated for contact manager");

            var dbCompany = companyProxy.GetCompany(dbContactAggregate.DataObject.Company.Id.Value);
            Assert.IsTrue(dbCompany.IsSuccessfull);
            Assert.IsNotNull(dbCompany.DataObject);

            AssertCompany(dbContactAggregate.DataObject.Company, dbCompany.DataObject);
            Assert.AreEqual(dbContactAggregate.DataObject.Company.LastUpdatedId, dbCompany.DataObject.LastUpdatedId,
                "LastUpdatedId not updated for company");
        }
        public void ShouldFailAggregateInsertIfCompanyLastUpdatedIdIncorrect()
        {
            var companyProxy          = new CompanyProxy();
            var contactAggregateProxy = new ContactAggregateProxy();
            var contactAggregate      = GetNewContactAggregate();
            var companyDetail         = GetCompany();
            var companyResponse       = companyProxy.InsertCompany(companyDetail);

            contactAggregate.Company.Id            = companyResponse.DataObject.InsertedCompanyId;
            contactAggregate.Company.LastUpdatedId = companyResponse.DataObject.LastUpdatedId.ToLower();

            var aggregateResponse = contactAggregateProxy.InsertContactAggregate(contactAggregate);

            Assert.IsFalse(aggregateResponse.IsSuccessfull);
            Assert.AreEqual(HttpStatusCode.BadRequest, aggregateResponse.StatusCode);
            Assert.AreEqual("\"Record to be updated has changed since last read.\"", aggregateResponse.RawResponse);
        }
        public void ShouldUpdateContactAndUpdateCompanyAndContactManager()
        {
            var contactProxy          = new ContactProxy();
            var companyProxy          = new CompanyProxy();
            var contactAggregateProxy = new ContactAggregateProxy();
            var contact         = GetContact();
            var contactResponse = contactProxy.InsertContact(contact);

            Assert.IsTrue(contactResponse.IsSuccessfull);
            var contactManager         = GetContact();
            var contactManagerResponse = contactProxy.InsertContact(contactManager);

            Assert.IsTrue(contactManagerResponse.IsSuccessfull);
            var company         = GetCompany();
            var companyResponse = companyProxy.InsertCompany(company);

            Assert.IsTrue(companyResponse.IsSuccessfull);

            var contactAggregate = GetNewContactAggregate();

            contactAggregate.Id = contactResponse.DataObject.InsertedContactId;
            contactAggregate.ContactManager.Id            = contactManagerResponse.DataObject.InsertedContactId;
            contactAggregate.ContactManager.LastUpdatedId = contactManagerResponse.DataObject.LastUpdatedId;
            contactAggregate.Company.Id            = companyResponse.DataObject.InsertedCompanyId;
            contactAggregate.Company.LastUpdatedId = companyResponse.DataObject.LastUpdatedId;

            var contactAggregateUpdateResponse = contactAggregateProxy.UpdateContactAggregate(contactAggregate,
                                                                                              contactResponse.DataObject.InsertedContactId);

            Assert.IsTrue(contactAggregateUpdateResponse.IsSuccessfull, "Updating contact and company and contact manager in aggregate failed");
            Assert.IsNotNull(contactAggregateUpdateResponse.DataObject);
            Assert.IsTrue(contactAggregateUpdateResponse.DataObject.CompanyId > 0, "Invalid CompanyId returned from UpdateContactAggregate when updating company");
            Assert.IsTrue(contactAggregateUpdateResponse.DataObject.CompanyLastUpdatedId.Length > 0, "Invalid CompanyLastUpdatedId returned from UpdateContactAggregate when updating company");
            Assert.IsTrue(contactAggregateUpdateResponse.DataObject.ContactManagerId > 0, "Invalid ContactManagerId returned from UpdateContactAggregate when updating contact manager");
            Assert.IsTrue(contactAggregateUpdateResponse.DataObject.ContactManagerLastUpdatedId.Length > 0, "Invalid ContactManagerLastUpdatedId returned from UpdateContactAggregate when updating contact manager");

            var updatedContactAggregateResponse =
                contactAggregateProxy.GetContactAggregate(contactResponse.DataObject.InsertedContactId);

            Assert.IsTrue(updatedContactAggregateResponse.IsSuccessfull, "Updating contact and company and contact manager in aggregate failed");
            Assert.IsNotNull(updatedContactAggregateResponse.DataObject);
            var updatedAggregate = updatedContactAggregateResponse.DataObject;

            Assert.IsNotNull(updatedAggregate.Company, "Company not associated with updated aggregate");
            Assert.IsNotNull(updatedAggregate.ContactManager, "Contact manager not associated with updated aggregate");
            Assert.IsNotNull(updatedAggregate.Company.Id);
            Assert.IsNotNull(updatedAggregate.ContactManager.Id);

            var updatedContact = contactProxy.GetContact(contactResponse.DataObject.InsertedContactId);

            AssertUpdatedContact(contactAggregate, updatedContact);

            var updatedCompany = companyProxy.GetCompany(updatedAggregate.Company.Id.Value);

            Assert.IsTrue(updatedCompany.IsSuccessfull);
            Assert.IsNotNull(updatedCompany.DataObject);

            AssertCompany(contactAggregate.Company, updatedCompany.DataObject);

            var updatedContactManager = contactProxy.GetContact(updatedAggregate.ContactManager.Id.Value);

            Assert.IsTrue(updatedCompany.IsSuccessfull);
            Assert.IsNotNull(updatedCompany.DataObject);
            AssertContactManager(contactAggregate.ContactManager, updatedContactManager.DataObject);
        }
        public void ShouldAddNewContactWithNewCompanyAndContactManager()
        {
            var contactAggregateProxy = new ContactAggregateProxy();
            var contactProxy = new ContactProxy();
            var companyProxy = new CompanyProxy();
            var contactAggregate = GetNewContactAggregate();
            var Response = contactAggregateProxy.InsertContactAggregate(contactAggregate);

            Assert.IsTrue(Response.IsSuccessfull, "Contact aggregate insert failed.");
            Assert.IsNotNull(Response.DataObject);
            Assert.IsTrue(Response.DataObject.InsertedContactId > 0, "Invalid InsertedContactId returned from InsertContactAggregate");
            Assert.IsTrue(Response.DataObject.LastUpdatedId.Length > 0, "Invalid LastUpdatedId returned");
            Assert.IsTrue(Response.DataObject.LastModified > DateTime.UtcNow.AddMinutes(-5), "Invalid LastModified returned");
            Assert.IsTrue(Response.DataObject.CompanyId > 0, "Invalid CompanyId returned from InsertContactAggregate when inserting new company");
            Assert.IsTrue(Response.DataObject.CompanyLastUpdatedId.Length > 0, "Invalid CompanyLastUpdatedId returned from InsertContactAggregate when inserting new company");
            Assert.IsTrue(Response.DataObject.ContactManagerId > 0, "Invalid ContactManagerId returned from InsertContactAggregate when inserting new contact manager");
            Assert.IsTrue(Response.DataObject.ContactManagerLastUpdatedId.Length > 0, "Invalid ContactManagerLastUpdatedId returned from InsertContactAggregate when inserting new contact manager");

            var contactResponse = contactProxy.GetContact(Response.DataObject.InsertedContactId);
            Assert.IsTrue(contactResponse.IsSuccessfull, "Contact not found");
            Assert.IsNotNull(contactResponse.DataObject);
            Assert.IsNotNull(contactResponse.DataObject.CompanyId);
            Assert.IsNotNull(contactResponse.DataObject.ContactManagerId);

            var contactManagerResponse = contactProxy.GetContact(contactResponse.DataObject.ContactManagerId.Value);
            Assert.IsTrue(contactManagerResponse.IsSuccessfull, "Contact manager not found");
            Assert.AreEqual(contactAggregate.ContactManager.FamilyName, contactManagerResponse.DataObject.FamilyName);

            var companyResponse = companyProxy.GetCompany(contactResponse.DataObject.CompanyId.Value);
            Assert.IsTrue(companyResponse.IsSuccessfull, "Company not found");
            Assert.AreEqual(contactAggregate.Company.Name, companyResponse.DataObject.Name);
        }
        public void ShouldUpdateContactAndUpdateCompanyAndContactManager()
        {
            var contactProxy = new ContactProxy();
            var companyProxy = new CompanyProxy();
            var contactAggregateProxy = new ContactAggregateProxy();
            var contact = GetContact();
            var contactResponse = contactProxy.InsertContact(contact);
            Assert.IsTrue(contactResponse.IsSuccessfull);
            var contactManager = GetContact();
            var contactManagerResponse = contactProxy.InsertContact(contactManager);
            Assert.IsTrue(contactManagerResponse.IsSuccessfull);
            var company = GetCompany();
            var companyResponse = companyProxy.InsertCompany(company);
            Assert.IsTrue(companyResponse.IsSuccessfull);

            var contactAggregate = GetNewContactAggregate();
            contactAggregate.Id = contactResponse.DataObject.InsertedContactId;
            contactAggregate.ContactManager.Id = contactManagerResponse.DataObject.InsertedContactId;
            contactAggregate.ContactManager.LastUpdatedId = contactManagerResponse.DataObject.LastUpdatedId;
            contactAggregate.Company.Id = companyResponse.DataObject.InsertedCompanyId;
            contactAggregate.Company.LastUpdatedId = companyResponse.DataObject.LastUpdatedId;

            var contactAggregateUpdateResponse = contactAggregateProxy.UpdateContactAggregate(contactAggregate,
                contactResponse.DataObject.InsertedContactId);
            Assert.IsTrue(contactAggregateUpdateResponse.IsSuccessfull, "Updating contact and company and contact manager in aggregate failed");
            Assert.IsNotNull(contactAggregateUpdateResponse.DataObject);
            Assert.IsTrue(contactAggregateUpdateResponse.DataObject.CompanyId > 0, "Invalid CompanyId returned from UpdateContactAggregate when updating company");
            Assert.IsTrue(contactAggregateUpdateResponse.DataObject.CompanyLastUpdatedId.Length > 0, "Invalid CompanyLastUpdatedId returned from UpdateContactAggregate when updating company");
            Assert.IsTrue(contactAggregateUpdateResponse.DataObject.ContactManagerId > 0, "Invalid ContactManagerId returned from UpdateContactAggregate when updating contact manager");
            Assert.IsTrue(contactAggregateUpdateResponse.DataObject.ContactManagerLastUpdatedId.Length > 0, "Invalid ContactManagerLastUpdatedId returned from UpdateContactAggregate when updating contact manager");

            var updatedContactAggregateResponse =
                contactAggregateProxy.GetContactAggregate(contactResponse.DataObject.InsertedContactId);
            Assert.IsTrue(updatedContactAggregateResponse.IsSuccessfull, "Updating contact and company and contact manager in aggregate failed");
            Assert.IsNotNull(updatedContactAggregateResponse.DataObject);
            var updatedAggregate = updatedContactAggregateResponse.DataObject;
            Assert.IsNotNull(updatedAggregate.Company, "Company not associated with updated aggregate");
            Assert.IsNotNull(updatedAggregate.ContactManager, "Contact manager not associated with updated aggregate");
            Assert.IsNotNull(updatedAggregate.Company.Id);
            Assert.IsNotNull(updatedAggregate.ContactManager.Id);

            var updatedContact = contactProxy.GetContact(contactResponse.DataObject.InsertedContactId);
            AssertUpdatedContact(contactAggregate, updatedContact);

            var updatedCompany = companyProxy.GetCompany(updatedAggregate.Company.Id.Value);
            Assert.IsTrue(updatedCompany.IsSuccessfull);
            Assert.IsNotNull(updatedCompany.DataObject);

            AssertCompany(contactAggregate.Company, updatedCompany.DataObject);

            var updatedContactManager = contactProxy.GetContact(updatedAggregate.ContactManager.Id.Value);
            Assert.IsTrue(updatedCompany.IsSuccessfull);
            Assert.IsNotNull(updatedCompany.DataObject);
            AssertContactManager(contactAggregate.ContactManager, updatedContactManager.DataObject);
        }
        public void ShouldUpdateContactAndAddNewCompanyAndContactManager()
        {
            var contactProxy = new ContactProxy();
            var contactAggregateProxy = new ContactAggregateProxy();
            var contact = GetContact();
            var contactResponse = contactProxy.InsertContact(contact);
            Assert.IsTrue(contactResponse.IsSuccessfull);

            var contactAggregate = GetNewContactAggregate();

            var contactAggregateResponse = contactAggregateProxy.UpdateContactAggregate(contactAggregate, contactResponse.DataObject.InsertedContactId);
            Assert.IsTrue(contactAggregateResponse.IsSuccessfull, "Failed to update existing contact and add new company and contact manager");
            Assert.IsNotNull(contactAggregateResponse.DataObject);
            Assert.AreEqual(contactResponse.DataObject.InsertedContactId, contactAggregateResponse.DataObject.UpdatedContactId);
            Assert.IsTrue(contactAggregateResponse.DataObject.CompanyId > 0, "Invalid CompanyId returned from UpdateContactAggregate when inserting new company");
            Assert.IsTrue(contactAggregateResponse.DataObject.CompanyLastUpdatedId.Length > 0, "Invalid CompanyLastUpdatedId returned from UpdateContactAggregate when inserting new company");
            Assert.IsTrue(contactAggregateResponse.DataObject.ContactManagerId > 0, "Invalid ContactManagerId returned from UpdateContactAggregate when inserting new contact manager");
            Assert.IsTrue(contactAggregateResponse.DataObject.ContactManagerLastUpdatedId.Length > 0, "Invalid ContactManagerLastUpdatedId returned from UpdateContactAggregate when inserting new contact manager");

            var updatedContact = contactProxy.GetContact(contactResponse.DataObject.InsertedContactId);
            AssertUpdatedContact(contactAggregate, updatedContact);

            var insertedAggregate =
                contactAggregateProxy.GetContactAggregate(contactResponse.DataObject.InsertedContactId);
            Assert.IsTrue(insertedAggregate.IsSuccessfull);
            Assert.IsNotNull(insertedAggregate.DataObject);
            Assert.IsNotNull(insertedAggregate.DataObject.Company, "New company not associated with existing contact");
            Assert.IsNotNull(insertedAggregate.DataObject.ContactManager, "New contact manager not associated with existing contact");
            Assert.AreEqual(contactAggregate.Company.Name, insertedAggregate.DataObject.Company.Name);
            Assert.AreEqual(contactAggregate.ContactManager.GivenName, insertedAggregate.DataObject.ContactManager.GivenName);
        }
        public void ShouldNotUpdateContactManagerWhenContactLastUpdatedIdIncorrect()
        {
            var contactProxy = new ContactProxy();
            var contactAggregateProxy = new ContactAggregateProxy();
            var contactAggregate = GetNewContactAggregate();
            var contactManager = GetContact();
            var contactManagerResponse = contactProxy.InsertContact(contactManager);

            var contact = GetContact();
            var contactResponse = contactProxy.InsertContact(contact);

            contactAggregate.Id = contactResponse.DataObject.InsertedContactId;
            contactAggregate.LastUpdatedId = contactResponse.DataObject.LastUpdatedId.ToLower();
            contactAggregate.ContactManager.Id = contactManagerResponse.DataObject.InsertedContactId;
            contactAggregate.Company.LastUpdatedId = contactManagerResponse.DataObject.LastUpdatedId;

            var aggregateResponse = contactAggregateProxy.UpdateContactAggregate(contactAggregate, contactResponse.DataObject.InsertedContactId);
            Assert.IsFalse(aggregateResponse.IsSuccessfull);
            Assert.AreEqual(HttpStatusCode.BadRequest, aggregateResponse.StatusCode);
            Assert.AreEqual("\"Record to be updated has changed since last read.\"", aggregateResponse.RawResponse);

            var updatedContact = contactProxy.GetContact(contactManagerResponse.DataObject.InsertedContactId);
            Assert.AreEqual(contactManagerResponse.DataObject.LastUpdatedId, updatedContact.DataObject.LastUpdatedId);
        }
        public void ShouldNotModifyExistingContactFieldsNotContainedInAggregateModel()
        {
            var contactProxy = new ContactProxy();
            var contactAggregateProxy = new ContactAggregateProxy();
            var contactAggregate = GetNewContactAggregate();

            var contact = GetCompleteContact();
            var contactResponse = contactProxy.InsertContact(contact);

            contactAggregate.Id = contactResponse.DataObject.InsertedContactId;
            contactAggregate.LastUpdatedId = contactResponse.DataObject.LastUpdatedId;

            var aggregateResponse = contactAggregateProxy.UpdateContactAggregate(contactAggregate, contactResponse.DataObject.InsertedContactId);
            Assert.IsTrue(aggregateResponse.IsSuccessfull);

            var updatedContactResponse = contactProxy.GetContact(contactResponse.DataObject.InsertedContactId);
            Assert.IsTrue(updatedContactResponse.IsSuccessfull);
            Assert.IsNotNull(updatedContactResponse.DataObject);
            var updatedContact = updatedContactResponse.DataObject;
            Assert.AreEqual(contact.AutoSendStatement, updatedContact.AutoSendStatement);
            Assert.AreEqual(contact.BpayDetails.BillerCode, updatedContact.BpayDetails.BillerCode);
            Assert.AreEqual(contact.BpayDetails.CRN, updatedContact.BpayDetails.CRN);
            Assert.AreEqual(contact.ChequeDetails.AcceptCheque, updatedContact.ChequeDetails.AcceptCheque);
            Assert.AreEqual(contact.ChequeDetails.ChequePayableTo, updatedContact.ChequeDetails.ChequePayableTo);
            Assert.AreEqual(contact.CustomField1, updatedContact.CustomField1);
            Assert.AreEqual(contact.CustomField2, updatedContact.CustomField2);
            Assert.AreEqual(contact.DefaultPurchaseDiscount, updatedContact.DefaultPurchaseDiscount);
            Assert.AreEqual(contact.DefaultSaleDiscount, updatedContact.DefaultSaleDiscount);
            Assert.AreEqual(contact.DirectDepositDetails.AcceptDirectDeposit, updatedContact.DirectDepositDetails.AcceptDirectDeposit);
            Assert.AreEqual(contact.DirectDepositDetails.AccountBSB, updatedContact.DirectDepositDetails.AccountBSB);
            Assert.AreEqual(contact.DirectDepositDetails.AccountName, updatedContact.DirectDepositDetails.AccountName);
            Assert.AreEqual(contact.DirectDepositDetails.AccountNumber, updatedContact.DirectDepositDetails.AccountNumber);
            Assert.AreEqual(contact.HomePhone, updatedContact.HomePhone);
            Assert.AreEqual(contact.LinkedInProfile, updatedContact.LinkedInProfile);
            Assert.AreEqual(contact.OtherAddress.City, updatedContact.OtherAddress.City);
            Assert.AreEqual(contact.OtherAddress.Country, updatedContact.OtherAddress.Country);
            Assert.AreEqual(contact.OtherAddress.Postcode, updatedContact.OtherAddress.Postcode);
            Assert.AreEqual(contact.OtherAddress.State, updatedContact.OtherAddress.State);
            Assert.AreEqual(contact.OtherAddress.Street, updatedContact.OtherAddress.Street);
            Assert.AreEqual(contact.OtherPhone, updatedContact.OtherPhone);
            Assert.AreEqual(contact.PurchaseTradingTerms.TradingTermsInterval, updatedContact.PurchaseTradingTerms.TradingTermsInterval);
            Assert.AreEqual(contact.PurchaseTradingTerms.TradingTermsIntervalType, updatedContact.PurchaseTradingTerms.TradingTermsIntervalType);
            Assert.AreEqual(contact.PurchaseTradingTerms.TradingTermsType, updatedContact.PurchaseTradingTerms.TradingTermsType);
            Assert.AreEqual(contact.SaleTradingTerms.TradingTermsInterval, updatedContact.SaleTradingTerms.TradingTermsInterval);
            Assert.AreEqual(contact.SaleTradingTerms.TradingTermsIntervalType, updatedContact.SaleTradingTerms.TradingTermsIntervalType);
            Assert.AreEqual(contact.SaleTradingTerms.TradingTermsType, updatedContact.SaleTradingTerms.TradingTermsType);
            Assert.AreEqual(contact.SkypeId, updatedContact.SkypeId);
            Assert.AreEqual(contact.TwitterId, updatedContact.TwitterId);
            Assert.AreEqual(contact.WebsiteUrl, updatedContact.WebsiteUrl);
        }
        public void ShouldFailAggregateInsertIfCompanyLastUpdatedIdIncorrect()
        {
            var companyProxy = new CompanyProxy();
            var contactAggregateProxy = new ContactAggregateProxy();
            var contactAggregate = GetNewContactAggregate();
            var companyDetail = GetCompany();
            var companyResponse = companyProxy.InsertCompany(companyDetail);

            contactAggregate.Company.Id = companyResponse.DataObject.InsertedCompanyId;
            contactAggregate.Company.LastUpdatedId = companyResponse.DataObject.LastUpdatedId.ToLower();

            var aggregateResponse = contactAggregateProxy.InsertContactAggregate(contactAggregate);
            Assert.IsFalse(aggregateResponse.IsSuccessfull);
            Assert.AreEqual(HttpStatusCode.BadRequest, aggregateResponse.StatusCode);
            Assert.AreEqual("\"Record to be updated has changed since last read.\"", aggregateResponse.RawResponse);
        }