public void whenAXeroApiValidationExceptionIsThrownThenABadRequestIsReturned()
        {
            var IXEROLogicMock = new Mock<IExportingContactsToXEROLogic>();

            List<int> testCustomerIdList = new List<int>();
            testCustomerIdList.Add(1);

            List<string> testCustomerIdList2 = new List<string>();
            testCustomerIdList2.Add("1");

            IXEROLogicMock.Setup(x => x.CreateContacts(It.IsAny<List<ContactViewModel>>()))
              .Throws(new XeroApiValidationException("test", testCustomerIdList2));

            xeroWebAPIController.XeroLogic = IXEROLogicMock.Object;

            List<ContactViewModel> testContactsList = new List<ContactViewModel>();
            ContactViewModel contactViewModelTest = new ContactViewModel()
            {
                name = "Test Name"
            };

            testContactsList.Add(contactViewModelTest);

            xeroWebAPIController.Request = new HttpRequestMessage();
            xeroWebAPIController.Configuration = new HttpConfiguration();
            xeroWebAPIController.ContactsToCreateList = testContactsList;

            HttpResponseMessage response = xeroWebAPIController.PostCustomersToXero(testCustomerIdList);

            IXEROLogicMock.Verify(x => x.CreateContacts(It.IsAny<List<ContactViewModel>>()), Times.Once());
            Assert.AreEqual(response.StatusCode, HttpStatusCode.BadRequest);
        }
        public void whenAContactNameAlreadyExistInXeroAContactNameAlreadyExistExceptionIsThrown()
        {
            ContactViewModel testCustomer = new ContactViewModel
            {
                name = "24 Locks"
            };

            List<ContactViewModel> testListOfCustomers = new List<ContactViewModel>();

            testListOfCustomers.Add(testCustomer);

            ExportingContactsToXeroLogic.CreateContacts(testListOfCustomers);
        }
        public void whenAContactIsCreatedThatIsInactiveItIsSetToInactiveInXero()
        {
            ContactViewModel toCreateContact = new ContactViewModel();
            toCreateContact = giveANewContactViewModel();
            toCreateContact.isArchived = true;
            List<ContactViewModel> testListOfCustomers = new List<ContactViewModel>();

            testListOfCustomers.Add(toCreateContact);

            List<ContactViewModel> resultOfCreateContacts = ExportingContactsToXeroLogic.CreateContacts(testListOfCustomers);
            ContactViewModel resultOfCreateContact = resultOfCreateContacts[0];

            Assert.AreEqual(resultOfCreateContact.isArchived, true);
        }
        public ContactViewModel Convert(SUPPLIER supplierToConvert)
        {
            ContactViewModel contactViewModel = new ContactViewModel();
            contactViewModel.xeroContactId = supplierToConvert.ext_key;
            contactViewModel.accountNumber = supplierToConvert.supplier1;
            if (string.IsNullOrEmpty(supplierToConvert.ext_key1))
            {
                //if(string.IsNullOrEmpty(supplierToConvert.name.Trim()))
                //{
                //    throw new SupplierNameIsEmptyException("The name of supplier " + supplierToConvert.supplier1 + " is empty. Please check the date and try again.");
                //}
                if(!(DoesNameAlreadyExistInXero(supplierToConvert.name.Trim())))
                {
                    contactViewModel.name = supplierToConvert.name.Trim();
                }
                else
                {
                    throw new SupplierNameAlreadyExistException("The name of supplier " + supplierToConvert.supplier1 + " is already used as a name for a contact in Xero. The name of a contact must be unique. Please check the data and try agian.");
                }
            }
            else
            {
                contactViewModel.name = supplierToConvert.ext_key1;
            }

            contactViewModel.address1 = supplierToConvert.address1.Trim();
            contactViewModel.address2 = supplierToConvert.address2.Trim();
            contactViewModel.address3 = supplierToConvert.address3.Trim();
            contactViewModel.postalcode = supplierToConvert.postcode.Trim();
            contactViewModel.addressType = "STREET";
            contactViewModel.phone = supplierToConvert.phone.Trim();
            contactViewModel.mobileNumber = supplierToConvert.mphone.Trim();
            contactViewModel.faxNumber = supplierToConvert.fax.Trim();
            contactViewModel.defaultCurrency = supplierToConvert.currency.Trim();
            contactViewModel.contactName = supplierToConvert.contact.Trim();
            contactViewModel.isArchived = supplierToConvert.rec_del.GetValueOrDefault();

            try
            {
                contactViewModel.email = GetSplitedEmailAddress(supplierToConvert.email, supplierToConvert.supplier1);
            }
            catch (NotVailidEmailException)
            {
                throw;
            }

            return contactViewModel;
        }
        public void whenPostCustomersToXeroIsCalledWithValidCustomerIdListThenHttpStatusCodeOkIsReturned()
        {
            List<int> testCustomerIdList = new List<int>();

            testCustomerIdList.Add(1);
            testCustomerIdList.Add(3);

            List<ContactViewModel> testContactsList = new List<ContactViewModel>();
            ContactViewModel contactViewModelTest = new ContactViewModel()
            {
                name = "Test Name",
                accountNumber = "3",
                xeroContactId = "test"
            };

            testContactsList.Add(contactViewModelTest);

            var IXEROLogicMock = new Mock<IExportingContactsToXEROLogic>();

            IXEROLogicMock.Setup(xl => xl.CreateContacts(It.IsAny<List<ContactViewModel>>())).Returns(testContactsList);
            IXEROLogicMock.Setup(xl => xl.UpdateContacts(It.IsAny<List<ContactViewModel>>())).Returns(testContactsList);

            xeroWebAPIController.XeroLogic = IXEROLogicMock.Object;

            xeroWebAPIController.Request = new HttpRequestMessage();
            xeroWebAPIController.Configuration = new HttpConfiguration();

            HttpResponseMessage response = xeroWebAPIController.PostCustomersToXero(testCustomerIdList);

            IXEROLogicMock.Verify(x => x.CreateContacts(It.IsAny<List<ContactViewModel>>()), Times.Once());
            IXEROLogicMock.Verify(x => x.UpdateContacts(It.IsAny<List<ContactViewModel>>()), Times.Once());
            Assert.AreEqual(response.StatusCode, HttpStatusCode.OK);
        }
 private InvoiceViewModel giveInvoice()
 {
     Random randomGenerator = new Random();
     int randomNumber = randomGenerator.Next(1, 99999);
     int randomNumber2 = randomGenerator.Next(1, 99999);
     InvoiceViewModel invoiceViewModel = new InvoiceViewModel();
     ContactViewModel contactViewModel = new ContactViewModel();
     invoiceViewModel.typeOfInvoice = "ACCREC";
     contactViewModel.name = "test";
     invoiceViewModel.Contact = contactViewModel;
     invoiceViewModel.Description = "test";
     invoiceViewModel.BrandingThemeId = "7889a0ac-262a-40e3-8a63-9a769b1a18af";
     invoiceViewModel.Number = randomNumber.ToString();
     invoiceViewModel.Date = DateTime.Now;
     invoiceViewModel.DueDate = DateTime.Now;
     invoiceViewModel.AccountCode = "200";
     invoiceViewModel.CurrencyCode = "NZD";
     invoiceViewModel.DiscountRate = 0;
     invoiceViewModel.ItemCode = "GB3-White";
     invoiceViewModel.Quantity = 2;
     invoiceViewModel.Reference = "test";
     invoiceViewModel.TaxType = "15% GST on Income";
     invoiceViewModel.TrackingName = "Region";
     invoiceViewModel.TrackingOption = "HUT";
     invoiceViewModel.UnitAmount = 2;
     return invoiceViewModel;
 }
        public ContactViewModel convert(CUSTOMER customerToConvert)
        {
            ContactViewModel contactViewModel = new ContactViewModel();
            contactViewModel.xeroContactId = customerToConvert.ext_ref;
            contactViewModel.accountNumber = customerToConvert.CUSTOMER_ID.ToString();

            if (string.IsNullOrEmpty(customerToConvert.ext_key1))
            {
                if ((!(String.IsNullOrEmpty(customerToConvert.fname)) || (!(String.IsNullOrEmpty(customerToConvert.sname)))))
                {
                    if (!(DoesNameAlreadyExistInXero(customerToConvert.fname.Trim() + " " + customerToConvert.sname.Trim())))
                    {
                        contactViewModel.name = customerToConvert.fname.Trim() + " " + customerToConvert.sname.Trim();
                        contactViewModel.name = contactViewModel.name.Trim();
                    }
                    else
                    {
                        throw new CustomerNameAlreadyExistException("The name of customer with customer number " + customerToConvert.CUSTOMER_ID + " is already used as a contact name in Xero. The name of a contact must be unique. Please check the data and try agian.");
                    }
                }
                else if (!(String.IsNullOrEmpty(customerToConvert.company)))
                {
                    if(!(DoesNameAlreadyExistInXero(customerToConvert.company.Trim())))
                    {
                        contactViewModel.name = customerToConvert.company.Trim();
                    }
                    else
                    {
                        throw new CustomerNameAlreadyExistException("The name of customer with customer number " + customerToConvert.CUSTOMER_ID + " is already used as a contact name in Xero.  is already used as a name for a contact in Xero. The name of a contact must be unique. Please check the data and try agian.");
                    }
                }
                else
                {
                    throw new CustomerDoesNotExistException("Customer has no company name, firstname or lastname");
                }
            }
            else
            {
                contactViewModel.name = customerToConvert.ext_key1;
            }

            contactViewModel.fname = customerToConvert.fname.Trim();
            contactViewModel.sname = customerToConvert.sname.Trim();
            contactViewModel.address1 = customerToConvert.address1.Trim();
            contactViewModel.address2 = customerToConvert.address2.Trim();
            contactViewModel.address3 = customerToConvert.address3.Trim();
            contactViewModel.city = customerToConvert.city.Trim();
            contactViewModel.postalcode = customerToConvert.postalcode.Trim();
            contactViewModel.addressType = "STREET";
            contactViewModel.region = customerToConvert.region.Trim();
            contactViewModel.phone = customerToConvert.phone.Trim();
            contactViewModel.mobileNumber = customerToConvert.mphone.Trim();
            contactViewModel.faxNumber = customerToConvert.fax.Trim();
            if (customerToConvert.status.Trim() == "X")
            {
                contactViewModel.isArchived = true;
            }
            else
            {
                contactViewModel.isArchived = false;
            }

            try
            {
                contactViewModel.email = GetSplitedEmailAddress(customerToConvert.email, customerToConvert.CUSTOMER_ID);
            }
            catch (NotVailidEmailException)
            {
                throw;
            }

            return contactViewModel;
        }
        private ContactViewModel giveAUpdatedContactViewModel(ContactViewModel contactViewModelToUpdate)
        {
            int randomInt = randomGenerator.Next(100000);
            contactViewModelToUpdate.name = "testName" + randomInt;
            contactViewModelToUpdate.fname = "testFname" + randomInt;
            contactViewModelToUpdate.sname = "testSname" + randomInt;
            contactViewModelToUpdate.address1 = "testAddress1" + randomInt;
            contactViewModelToUpdate.address2 = "testAddress2" + randomInt;
            contactViewModelToUpdate.address3 = "testAddress3" + randomInt;
            contactViewModelToUpdate.city = "testCity" + randomInt;
            contactViewModelToUpdate.postalcode = "testPostalcode" + randomInt;
            contactViewModelToUpdate.faxNumber = "testFaxNumber" + randomInt;
            contactViewModelToUpdate.phone = "testPhone" + randomInt;
            contactViewModelToUpdate.mobileNumber = "testMobileNumber" + randomInt;
            contactViewModelToUpdate.accountNumber = "" + randomInt;
            contactViewModelToUpdate.defaultCurrency = "USD";
            contactViewModelToUpdate.isArchived = false;
            contactViewModelToUpdate.email = "test" + randomInt + "@mail.com";
            contactViewModelToUpdate.region = "testRegion" + randomInt;
            contactViewModelToUpdate.contactName = "TestName" + randomInt;

            return contactViewModelToUpdate;
        }
        private ContactViewModel giveANewContactViewModel()
        {
            int randomInt = randomGenerator.Next(1000000);

            ContactViewModel contactViewModel = new ContactViewModel()
            {
                name = "testName" + randomInt,
                fname = "testFname" + randomInt,
                sname = "testSname" + randomInt,
                address1 = "testAddress1" + randomInt,
                address2 = "testAddress2" + randomInt,
                address3 = "testAddress3" + randomInt,
                city = "testCity" + randomInt,
                postalcode = "testPostalcode" + randomInt,
                faxNumber = "testFaxNumber" + randomInt,
                phone = "testPhone" + randomInt,
                mobileNumber = "testMobileNumber" + randomInt,
                accountNumber = "" + randomInt,
                defaultCurrency = "USD",
                isArchived = false,
                email = "test" + randomInt + "@mail.com",
                region = "testRegion" + randomInt,
                contactName = "TestName" + randomInt,
                addressType = "STREET"
            };
            return contactViewModel;
        }
        public void whenVallidContactIsGivenToUpdateTheResultContactHasTheSameValues()
        {
            ContactViewModel toCreateContact = new ContactViewModel();
            toCreateContact = giveANewContactViewModel();

            List<ContactViewModel> testListOfCreateContact = new List<ContactViewModel>();

            testListOfCreateContact.Add(toCreateContact);

            var resultOfCreate = ExportingContactsToXeroLogic.CreateContacts(testListOfCreateContact);

            ContactViewModel toUpdateContact = new ContactViewModel();
            toUpdateContact = giveAUpdatedContactViewModel(resultOfCreate[0]);

            List<ContactViewModel> testListOfUpdateContact = new List<ContactViewModel>();
            testListOfUpdateContact.Add(toUpdateContact);

            List<ContactViewModel> resultOfUpdateContacts = ExportingContactsToXeroLogic.UpdateContacts(testListOfUpdateContact);
            ContactViewModel resultOfUpdateContact = resultOfUpdateContacts[0];

            Assert.AreEqual(resultOfUpdateContact.accountNumber, toUpdateContact.accountNumber);
            Assert.AreEqual(resultOfUpdateContact.address1, toUpdateContact.address1);
            Assert.AreEqual(resultOfUpdateContact.address2, toUpdateContact.address2);
            Assert.AreEqual(resultOfUpdateContact.city, toUpdateContact.city);
            Assert.AreEqual(resultOfUpdateContact.defaultCurrency, toUpdateContact.defaultCurrency);
            Assert.AreEqual(resultOfUpdateContact.email, toUpdateContact.email);
            Assert.AreEqual(resultOfUpdateContact.faxNumber, toUpdateContact.faxNumber);
            Assert.AreEqual(resultOfUpdateContact.fname, toUpdateContact.fname);
            Assert.AreEqual(resultOfUpdateContact.isArchived, toUpdateContact.isArchived);
            Assert.AreEqual(resultOfUpdateContact.mobileNumber, toUpdateContact.mobileNumber);
            Assert.AreEqual(resultOfUpdateContact.name, toUpdateContact.name);
            Assert.AreEqual(resultOfUpdateContact.phone, toUpdateContact.phone);
            Assert.AreEqual(resultOfUpdateContact.postalcode, toUpdateContact.postalcode);
            Assert.AreEqual(resultOfUpdateContact.region, toUpdateContact.region);
            Assert.AreEqual(resultOfUpdateContact.sname, toUpdateContact.sname);
            Assert.AreEqual(resultOfUpdateContact.addressType, toUpdateContact.addressType);
            Assert.AreEqual(resultOfUpdateContact.xeroContactId, toUpdateContact.xeroContactId);
        }
            public void whenXeroAiLimitIsReachedThenABadGatewayIsReturned()
            {
                var IXEROLogicMock = new Mock<IExportingContactsToXEROLogic>();

                IXEROLogicMock.Setup(x => x.CreateContacts(It.IsAny<List<ContactViewModel>>()))
                  .Throws(new XeroApiLimitException());

                xeroWebAPIController.XeroLogic = IXEROLogicMock.Object;

                List<string> testSupplerCodeList = new List<string>();
                testSupplerCodeList.Add("ALIE");

                List<ContactViewModel> testContactsList = new List<ContactViewModel>();
                ContactViewModel contactViewModelTest = new ContactViewModel()
                {
                    name = "Test Name"
                };

                testContactsList.Add(contactViewModelTest);

                xeroWebAPIController.Request = new HttpRequestMessage();
                xeroWebAPIController.Configuration = new HttpConfiguration();
                xeroWebAPIController.ContactsToCreateList = testContactsList;

                HttpResponseMessage response = xeroWebAPIController.PostSuppliersToXero(testSupplerCodeList);

                IXEROLogicMock.Verify(x => x.CreateContacts(It.IsAny<List<ContactViewModel>>()), Times.Once());
                Assert.AreEqual(response.StatusCode, HttpStatusCode.BadGateway);
            }
        public Contact Convert(ContactViewModel contactViewModel)
        {
            Contact contact = new Contact();

            Address address = new Address();

            address.AddressLine1 = contactViewModel.address1;
            address.AddressLine2 = contactViewModel.address2;
            address.AddressLine3 = contactViewModel.address3;
            address.City = contactViewModel.city;
            address.PostalCode = contactViewModel.postalcode;
            address.Region = contactViewModel.region;

            if (contactViewModel.addressType == "STREET")
            {
                address.AddressType = AddressType.Street;
            }

            List<Address> addressList = new List<Address>();
            addressList.Add(address);

            Phone phone = new Phone
            {
                PhoneType = Xero.Api.Core.Model.Types.PhoneType.Default,
                PhoneNumber = contactViewModel.phone
            };

            Phone fax = new Phone
            {
                PhoneType = Xero.Api.Core.Model.Types.PhoneType.Fax,
                PhoneNumber = contactViewModel.faxNumber
            };

            Phone mobile = new Phone
            {
                PhoneType = Xero.Api.Core.Model.Types.PhoneType.Mobile,
                PhoneNumber = contactViewModel.mobileNumber
            };

            List<Phone> phones = new List<Phone>();
            phones.Add(phone);
            phones.Add(fax);
            phones.Add(mobile);

            DateTime now = DateTime.Now;

            if (!(contactViewModel.contactName == null))
            {
                string[] splitedContactPerson = contactViewModel.contactName.Split(' ');
                string contactPersonLastName;
                ContactPerson contactPerson = new ContactPerson();

                if (splitedContactPerson.Count() > 0 && (!(string.IsNullOrEmpty(splitedContactPerson[0]))))
                {
                    contactPerson.FirstName = splitedContactPerson[0];
                    if (splitedContactPerson.Count() > 1)
                    {
                        contactPersonLastName = "";

                        for (int i = 1; i < splitedContactPerson.Count(); i++)
                        {
                            contactPersonLastName += splitedContactPerson[i];
                        }

                        contactPerson.LastName = contactPersonLastName;
                    }
                }

                if (!(string.IsNullOrEmpty(contactPerson.FirstName)))
                {
                    List<ContactPerson> contactPersonList = new List<ContactPerson>();
                    contactPersonList.Add(contactPerson);
                    contact.ContactPersons = contactPersonList;
                }
            }

            if (String.IsNullOrEmpty(contactViewModel.defaultCurrency))
            {
                contact.DefaultCurrency = "NZD";
            }
            else
            {
                contact.DefaultCurrency = contactViewModel.defaultCurrency;
            }

            if (contactViewModel.isArchived == true)
            {
                contact.ContactStatus = ContactStatus.Archived;
            }
            else
            {
                contact.ContactStatus = ContactStatus.Active;
            }

            if (!(String.IsNullOrEmpty(contactViewModel.xeroContactId)))
            {
                contact.Id = Guid.Parse(contactViewModel.xeroContactId);
            }

            if (!(string.IsNullOrEmpty(contactViewModel.name.Trim())))
            {
                contact.Name = contactViewModel.name;
            }
            else
            {
                if (!(string.IsNullOrEmpty(contactViewModel.accountNumber.Trim())))
                {
                    throw new ContactWithNoFNameSnameOrNameException("The name of contact with accountNumber " + contactViewModel.accountNumber + "is empty.");
                }
                else
                {
                    throw new ContactWithNoFNameSnameOrNameException("The name of a contact can not be empty.");
                }
            }

            contact.AccountNumber = contactViewModel.accountNumber;
            contact.EmailAddress = contactViewModel.email;
            contact.FirstName = contactViewModel.fname;
            contact.LastName = contactViewModel.sname;
            contact.Addresses = addressList;
            contact.Phones = phones;
            contact.UpdatedDateUtc = now;
            return contact;
        }
        public ContactViewModel Convert(Contact contactToConvert)
        {
            ContactViewModel contactViewModel = new ContactViewModel();

            contactViewModel.accountNumber = contactToConvert.AccountNumber;
            foreach (var address in contactToConvert.Addresses)
            {
                if(address.AddressType == Xero.Api.Core.Model.Types.AddressType.Street)
                {
                    contactViewModel.address1 = address.AddressLine1;
                    contactViewModel.address2 = address.AddressLine2;
                    contactViewModel.address3 = address.AddressLine3;
                    contactViewModel.addressType = "STREET";
                    contactViewModel.city = address.City;
                    contactViewModel.postalcode = address.PostalCode;
                    contactViewModel.region = address.Region;
                }

            }

            if (contactToConvert.ContactPersons.Count > 0)
            {
                if (contactToConvert.ContactPersons.FirstOrDefault().LastName == null)
                {
                    contactViewModel.contactName = contactToConvert.ContactPersons.FirstOrDefault().FirstName;
                }
                else
                {
                    contactViewModel.contactName = contactToConvert.ContactPersons.FirstOrDefault().FirstName + contactToConvert.ContactPersons.FirstOrDefault().LastName;
                }
            }

            contactViewModel.defaultCurrency = contactToConvert.DefaultCurrency;
            contactViewModel.email = contactToConvert.EmailAddress;

            foreach (var phoneNumber in contactToConvert.Phones)
            {
                if (phoneNumber.PhoneType == Xero.Api.Core.Model.Types.PhoneType.Fax)
                {
                    contactViewModel.faxNumber = phoneNumber.PhoneNumber;
                }
                else if (phoneNumber.PhoneType == Xero.Api.Core.Model.Types.PhoneType.Mobile)
                {
                    contactViewModel.mobileNumber = phoneNumber.PhoneNumber;
                }
                else if (phoneNumber.PhoneType == Xero.Api.Core.Model.Types.PhoneType.Default)
                {
                    contactViewModel.phone = phoneNumber.PhoneNumber;
                }
            }

            contactViewModel.sname = contactToConvert.LastName;
            contactViewModel.fname = contactToConvert.FirstName;
            contactViewModel.name = contactToConvert.Name;
            contactViewModel.xeroContactId = contactToConvert.Id.ToString();

            if (contactToConvert.ContactStatus == Xero.Api.Core.Model.Status.ContactStatus.Archived)
            {
                contactViewModel.isArchived = true;
            }

            return contactViewModel;
        }