From 607dd68083981a43d858280899b545e55ad95b17 Mon Sep 17 00:00:00 2001 From: Kori Francis Date: Wed, 9 Mar 2016 12:21:44 -0500 Subject: [PATCH] Fixes #24, don't send update request when there's nothing to update. --- Source/Chargify.NET/ChargifyConnect.cs | 50 ++- Source/ChargifyDotNetTests/CustomerTests.cs | 395 ++++++++++---------- 2 files changed, 235 insertions(+), 210 deletions(-) diff --git a/Source/Chargify.NET/ChargifyConnect.cs b/Source/Chargify.NET/ChargifyConnect.cs index 0b97104..dbad98d 100644 --- a/Source/Chargify.NET/ChargifyConnect.cs +++ b/Source/Chargify.NET/ChargifyConnect.cs @@ -588,38 +588,48 @@ public ICustomer UpdateCustomer(ICustomer Customer) if (Customer == null) throw new ArgumentNullException("Customer"); if (Customer.ChargifyID == int.MinValue) throw new ArgumentException("Invalid chargify ID detected", "Customer.ChargifyID"); ICustomer OldCust = this.LoadCustomer(Customer.ChargifyID); + + bool isUpdateRequired = false; + // create XML for creation of customer var customerXml = new StringBuilder(GetXMLStringIfApplicable()); customerXml.Append(""); if (OldCust != null) { if (OldCust.ChargifyID != Customer.ChargifyID) throw new ArgumentException("Not unique", "Customer.SystemID"); - if (OldCust.FirstName != Customer.FirstName) customerXml.AppendFormat("{0}", HttpUtility.HtmlEncode(Customer.FirstName)); - if (OldCust.LastName != Customer.LastName) customerXml.AppendFormat("{0}", HttpUtility.HtmlEncode(Customer.LastName)); - if (OldCust.Email != Customer.Email) customerXml.AppendFormat("{0}", Customer.Email); - if (OldCust.Organization != Customer.Organization) customerXml.AppendFormat("{0}", HttpUtility.HtmlEncode(Customer.Organization)); - if (OldCust.Phone != Customer.Phone) customerXml.AppendFormat("{0}", HttpUtility.HtmlEncode(Customer.Phone)); - if (OldCust.SystemID != Customer.SystemID) customerXml.AppendFormat("{0}", Customer.SystemID); - if (OldCust.ShippingAddress != Customer.ShippingAddress) customerXml.AppendFormat("
{0}
", HttpUtility.HtmlEncode(Customer.ShippingAddress)); - if (OldCust.ShippingAddress2 != Customer.ShippingAddress2) customerXml.AppendFormat("{0}", HttpUtility.HtmlEncode(Customer.ShippingAddress2)); - if (OldCust.ShippingCity != Customer.ShippingCity) customerXml.AppendFormat("{0}", HttpUtility.HtmlEncode(Customer.ShippingCity)); - if (OldCust.ShippingState != Customer.ShippingState) customerXml.AppendFormat("{0}", HttpUtility.HtmlEncode(Customer.ShippingState)); - if (OldCust.ShippingZip != Customer.ShippingZip) customerXml.AppendFormat("{0}", Customer.ShippingZip); - if (OldCust.ShippingCountry != Customer.ShippingCountry) customerXml.AppendFormat("{0}", HttpUtility.HtmlEncode(Customer.ShippingCountry)); + if (OldCust.FirstName != Customer.FirstName) { customerXml.AppendFormat("{0}", HttpUtility.HtmlEncode(Customer.FirstName)); isUpdateRequired = true; } + if (OldCust.LastName != Customer.LastName) { customerXml.AppendFormat("{0}", HttpUtility.HtmlEncode(Customer.LastName)); isUpdateRequired = true; } + if (OldCust.Email != Customer.Email) { customerXml.AppendFormat("{0}", Customer.Email); isUpdateRequired = true; } + if (OldCust.Organization != Customer.Organization) { customerXml.AppendFormat("{0}", HttpUtility.HtmlEncode(Customer.Organization)); isUpdateRequired = true; } + if (OldCust.Phone != Customer.Phone) { customerXml.AppendFormat("{0}", HttpUtility.HtmlEncode(Customer.Phone)); isUpdateRequired = true; } + if (OldCust.SystemID != Customer.SystemID) { customerXml.AppendFormat("{0}", Customer.SystemID); isUpdateRequired = true; } + if (OldCust.ShippingAddress != Customer.ShippingAddress) { customerXml.AppendFormat("
{0}
", HttpUtility.HtmlEncode(Customer.ShippingAddress)); isUpdateRequired = true; } + if (OldCust.ShippingAddress2 != Customer.ShippingAddress2) { customerXml.AppendFormat("{0}", HttpUtility.HtmlEncode(Customer.ShippingAddress2)); isUpdateRequired = true; } + if (OldCust.ShippingCity != Customer.ShippingCity) { customerXml.AppendFormat("{0}", HttpUtility.HtmlEncode(Customer.ShippingCity)); isUpdateRequired = true; } + if (OldCust.ShippingState != Customer.ShippingState) { customerXml.AppendFormat("{0}", HttpUtility.HtmlEncode(Customer.ShippingState)); isUpdateRequired = true; } + if (OldCust.ShippingZip != Customer.ShippingZip) { customerXml.AppendFormat("{0}", Customer.ShippingZip); isUpdateRequired = true; } + if (OldCust.ShippingCountry != Customer.ShippingCountry) { customerXml.AppendFormat("{0}", HttpUtility.HtmlEncode(Customer.ShippingCountry)); isUpdateRequired = true; } } customerXml.Append("
"); - try + if (isUpdateRequired) { - // now make the request - string response = this.DoRequest(string.Format("customers/{0}.{1}", Customer.ChargifyID, GetMethodExtension()), HttpRequestMethod.Put, customerXml.ToString()); - // change the response to the object - return response.ConvertResponseTo("customer"); + try + { + // now make the request + string response = this.DoRequest(string.Format("customers/{0}.{1}", Customer.ChargifyID, GetMethodExtension()), HttpRequestMethod.Put, customerXml.ToString()); + // change the response to the object + return response.ConvertResponseTo("customer"); + } + catch (ChargifyException cex) + { + if (cex.StatusCode == HttpStatusCode.NotFound) throw new InvalidOperationException("Customer not found"); + throw; + } } - catch (ChargifyException cex) + else { - if (cex.StatusCode == HttpStatusCode.NotFound) throw new InvalidOperationException("Customer not found"); - throw; + return Customer; } } diff --git a/Source/ChargifyDotNetTests/CustomerTests.cs b/Source/ChargifyDotNetTests/CustomerTests.cs index 5320807..6ed83d4 100644 --- a/Source/ChargifyDotNetTests/CustomerTests.cs +++ b/Source/ChargifyDotNetTests/CustomerTests.cs @@ -1,190 +1,205 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using ChargifyDotNetTests.Base; -using ChargifyNET; -#if NUNIT -using NUnit.Framework; -#else -using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute; -using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; -using TestFixtureSetUp = Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute; -using SetUp = Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute; -using Microsoft.VisualStudio.TestTools.UnitTesting; -#endif - -namespace ChargifyDotNetTests -{ - [TestFixture] - public class CustomerTests : ChargifyTestBase - { - [Test] - public void Customer_CreateWithError() - { - // Arrange - Chargify.UseJSON = true; - var customer = new Customer() - { - FirstName = Faker.Name.First() - }; - - // Act - try - { - Chargify.CreateCustomer(customer); - Assert.Fail("Error was expected, but not received"); - } - catch (ChargifyException chEx) - { - Assert.IsNotNull(chEx.ErrorMessages); - Assert.AreEqual(2, chEx.ErrorMessages.Count); - Assert.IsTrue(chEx.ErrorMessages.Any(e => e.Message == "Last name: cannot be blank.")); - Assert.IsTrue(chEx.ErrorMessages.Any(e => e.Message == "Email address: cannot be blank.")); - } - Chargify.UseJSON = false; - } - - [Test] - public void Customer_CreateCustomer() - { - // Arrange - string referenceID = Guid.NewGuid().ToString(); - var customer = new Customer() - { - FirstName = Faker.Name.First(), - LastName = Faker.Name.Last(), - Email = Faker.Internet.Email(), - Phone = Faker.Phone.Number(), - Organization = Faker.Company.Name(), - SystemID = referenceID, - ShippingAddress = Faker.Address.StreetAddress(false), - ShippingAddress2 = Faker.Address.SecondaryAddress(), - ShippingCity = Faker.Address.City(), - ShippingState = Faker.Address.UsState(), - ShippingZip = Faker.Address.ZipCode(), - ShippingCountry = "US" - }; - - // Act - var createdCustomer = Chargify.CreateCustomer(customer); - - // Assert - Assert.IsNotNull(createdCustomer); - //Assert.IsInstanceOfType(createdCustomer, typeof(Customer)); - Assert.IsTrue(createdCustomer.SystemID == customer.SystemID); - Assert.IsTrue(createdCustomer.FirstName == customer.FirstName); - Assert.IsTrue(createdCustomer.LastName == customer.LastName); - Assert.IsTrue(createdCustomer.Organization == customer.Organization); - Assert.IsTrue(createdCustomer.Email == customer.Email); - Assert.IsTrue(createdCustomer.Phone == customer.Phone); - Assert.IsTrue(createdCustomer.ShippingAddress == customer.ShippingAddress); - Assert.IsTrue(createdCustomer.ShippingAddress2 == customer.ShippingAddress2); - Assert.IsTrue(createdCustomer.ShippingCity == customer.ShippingCity); - Assert.IsTrue(createdCustomer.ShippingState == customer.ShippingState); - Assert.IsTrue(createdCustomer.ShippingZip == customer.ShippingZip); - Assert.IsTrue(createdCustomer.ShippingCountry == customer.ShippingCountry); - - // Can't cleanup, Chargify doesn't support customer deletions - } - - [Test] - public void Customer_OddCharactersForOrganization() - { - // Arrange - string referenceID = Guid.NewGuid().ToString(); - var customer = new Customer() - { - FirstName = Faker.Name.First(), - LastName = Faker.Name.Last(), - Email = Faker.Internet.Email(), - Phone = Faker.Phone.Number(), - Organization = "Chargify&", - SystemID = referenceID, - ShippingAddress = Faker.Address.StreetAddress(false), - ShippingAddress2 = Faker.Address.SecondaryAddress(), - ShippingCity = Faker.Address.City(), - ShippingState = Faker.Address.UsState(), - ShippingZip = Faker.Address.ZipCode(), - ShippingCountry = "US" - }; - - // Act - var createdCustomer = Chargify.CreateCustomer(customer); - - // Assert - Assert.IsNotNull(createdCustomer); - //Assert.IsInstanceOfType(createdCustomer, typeof(Customer)); - Assert.IsTrue(createdCustomer.Organization == customer.Organization); - } - - [Test] - public void Customer_ReadSingleCustomer() - { - var customers = Chargify.GetCustomerList().Keys; - // Load test customer via reference value (ASP.NET Membership ID) - var referenceValue = customers.FirstOrDefault(systemID => !string.IsNullOrWhiteSpace(systemID)); - ICustomer customer = Chargify.LoadCustomer(referenceValue); - Assert.IsNotNull(customer); - Assert.IsTrue(string.IsNullOrWhiteSpace(customer.FirstName) == false); - Assert.IsTrue(string.IsNullOrWhiteSpace(customer.LastName) == false); - Assert.IsTrue(string.IsNullOrWhiteSpace(customer.SystemID) == false); - Assert.AreEqual(referenceValue, customer.SystemID); - - ICustomer customer1 = Chargify.LoadCustomer(customer.ChargifyID); - Assert.IsNotNull(customer1); - Assert.AreEqual(customer.FirstName, customer1.FirstName); - Assert.AreEqual(customer.LastName, customer1.LastName); - Assert.AreEqual(customer.SystemID, customer1.SystemID); - } - - /// - /// Get the list of customers - /// - [Test] - public void Customer_ListCustomers() - { - IDictionary customerList = Chargify.GetCustomerList(); - Assert.IsNotNull(customerList); - Assert.AreNotEqual(0, customerList.Count); - } - - [Test] - public void Customer_UpdateCustomer() - { - // Load our test customer - var customers = Chargify.GetCustomerList().Keys; - // Load test customer via reference value (ASP.NET Membership ID) - var referenceValue = customers.FirstOrDefault(systemID => !string.IsNullOrWhiteSpace(systemID)); - ICustomer customer = Chargify.LoadCustomer(referenceValue); - Assert.IsNotNull(customer); - - // Change the customer's "Organization" property - var oldAddress1 = customer.ShippingAddress; - var oldAddress2 = customer.ShippingAddress2; - var newAddress1 = Guid.NewGuid().ToString().Replace("-", string.Empty).ToUpperInvariant(); - var newAddress2 = Guid.NewGuid().ToString().Replace("-", string.Empty).ToUpperInvariant(); - string company = Faker.Company.Name(); - customer.Organization = company; - customer.ShippingAddress = newAddress1; - customer.ShippingAddress2 = newAddress2; - Assert.AreEqual(company, customer.Organization); - - // Update the customer and check the result - ICustomer result = Chargify.UpdateCustomer(customer); - Assert.IsNotNull(result); - Assert.AreEqual(company, result.Organization); - Assert.AreEqual(newAddress1, result.ShippingAddress); - Assert.AreEqual(newAddress2, result.ShippingAddress2); - - // Set it back to "nothing" again - customer.Organization = string.Empty; - customer.ShippingAddress = oldAddress1; - customer.ShippingAddress2 = oldAddress2; - ICustomer result1 = Chargify.UpdateCustomer(customer); - Assert.IsNotNull(result1); - Assert.AreEqual(string.Empty, result1.Organization); - Assert.AreEqual(oldAddress1, result1.ShippingAddress); - Assert.AreEqual(oldAddress2, result1.ShippingAddress2); - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using ChargifyDotNetTests.Base; +using ChargifyNET; +#if NUNIT +using NUnit.Framework; +#else +using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute; +using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; +using TestFixtureSetUp = Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute; +using SetUp = Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute; +using Microsoft.VisualStudio.TestTools.UnitTesting; +#endif + +namespace ChargifyDotNetTests +{ + [TestFixture] + public class CustomerTests : ChargifyTestBase + { + [Test] + public void Customer_CreateWithError() + { + // Arrange + Chargify.UseJSON = true; + var customer = new Customer() + { + FirstName = Faker.Name.First() + }; + + // Act + try + { + Chargify.CreateCustomer(customer); + Assert.Fail("Error was expected, but not received"); + } + catch (ChargifyException chEx) + { + Assert.IsNotNull(chEx.ErrorMessages); + Assert.AreEqual(2, chEx.ErrorMessages.Count); + Assert.IsTrue(chEx.ErrorMessages.Any(e => e.Message == "Last name: cannot be blank.")); + Assert.IsTrue(chEx.ErrorMessages.Any(e => e.Message == "Email address: cannot be blank.")); + } + Chargify.UseJSON = false; + } + + [Test] + public void Customer_CreateCustomer() + { + // Arrange + string referenceID = Guid.NewGuid().ToString(); + var customer = new Customer() + { + FirstName = Faker.Name.First(), + LastName = Faker.Name.Last(), + Email = Faker.Internet.Email(), + Phone = Faker.Phone.Number(), + Organization = Faker.Company.Name(), + SystemID = referenceID, + ShippingAddress = Faker.Address.StreetAddress(false), + ShippingAddress2 = Faker.Address.SecondaryAddress(), + ShippingCity = Faker.Address.City(), + ShippingState = Faker.Address.UsState(), + ShippingZip = Faker.Address.ZipCode(), + ShippingCountry = "US" + }; + + // Act + var createdCustomer = Chargify.CreateCustomer(customer); + + // Assert + Assert.IsNotNull(createdCustomer); + //Assert.IsInstanceOfType(createdCustomer, typeof(Customer)); + Assert.IsTrue(createdCustomer.SystemID == customer.SystemID); + Assert.IsTrue(createdCustomer.FirstName == customer.FirstName); + Assert.IsTrue(createdCustomer.LastName == customer.LastName); + Assert.IsTrue(createdCustomer.Organization == customer.Organization); + Assert.IsTrue(createdCustomer.Email == customer.Email); + Assert.IsTrue(createdCustomer.Phone == customer.Phone); + Assert.IsTrue(createdCustomer.ShippingAddress == customer.ShippingAddress); + Assert.IsTrue(createdCustomer.ShippingAddress2 == customer.ShippingAddress2); + Assert.IsTrue(createdCustomer.ShippingCity == customer.ShippingCity); + Assert.IsTrue(createdCustomer.ShippingState == customer.ShippingState); + Assert.IsTrue(createdCustomer.ShippingZip == customer.ShippingZip); + Assert.IsTrue(createdCustomer.ShippingCountry == customer.ShippingCountry); + + // Can't cleanup, Chargify doesn't support customer deletions + } + + [Test] + public void Customer_OddCharactersForOrganization() + { + // Arrange + string referenceID = Guid.NewGuid().ToString(); + var customer = new Customer() + { + FirstName = Faker.Name.First(), + LastName = Faker.Name.Last(), + Email = Faker.Internet.Email(), + Phone = Faker.Phone.Number(), + Organization = "Chargify&", + SystemID = referenceID, + ShippingAddress = Faker.Address.StreetAddress(false), + ShippingAddress2 = Faker.Address.SecondaryAddress(), + ShippingCity = Faker.Address.City(), + ShippingState = Faker.Address.UsState(), + ShippingZip = Faker.Address.ZipCode(), + ShippingCountry = "US" + }; + + // Act + var createdCustomer = Chargify.CreateCustomer(customer); + + // Assert + Assert.IsNotNull(createdCustomer); + //Assert.IsInstanceOfType(createdCustomer, typeof(Customer)); + Assert.IsTrue(createdCustomer.Organization == customer.Organization); + } + + [Test] + public void Customer_ReadSingleCustomer() + { + var customers = Chargify.GetCustomerList().Keys; + // Load test customer via reference value (ASP.NET Membership ID) + var referenceValue = customers.FirstOrDefault(systemID => !string.IsNullOrWhiteSpace(systemID)); + ICustomer customer = Chargify.LoadCustomer(referenceValue); + Assert.IsNotNull(customer); + Assert.IsTrue(string.IsNullOrWhiteSpace(customer.FirstName) == false); + Assert.IsTrue(string.IsNullOrWhiteSpace(customer.LastName) == false); + Assert.IsTrue(string.IsNullOrWhiteSpace(customer.SystemID) == false); + Assert.AreEqual(referenceValue, customer.SystemID); + + ICustomer customer1 = Chargify.LoadCustomer(customer.ChargifyID); + Assert.IsNotNull(customer1); + Assert.AreEqual(customer.FirstName, customer1.FirstName); + Assert.AreEqual(customer.LastName, customer1.LastName); + Assert.AreEqual(customer.SystemID, customer1.SystemID); + } + + /// + /// Get the list of customers + /// + [Test] + public void Customer_ListCustomers() + { + IDictionary customerList = Chargify.GetCustomerList(); + Assert.IsNotNull(customerList); + Assert.AreNotEqual(0, customerList.Count); + } + + [Test] + public void Customer_Update_Nothing() + { + // Arrange + var customers = Chargify.GetCustomerList().Keys; + var referenceValue = customers.FirstOrDefault(systemID => !string.IsNullOrWhiteSpace(systemID)); + ICustomer customer = Chargify.LoadCustomer(referenceValue); + + // Act + var updatedCustomer = Chargify.UpdateCustomer(customer); + + // Assert + Assert.IsNotNull(updatedCustomer); + } + + [Test] + public void Customer_UpdateCustomer() + { + // Load our test customer + var customers = Chargify.GetCustomerList().Keys; + // Load test customer via reference value (ASP.NET Membership ID) + var referenceValue = customers.FirstOrDefault(systemID => !string.IsNullOrWhiteSpace(systemID)); + ICustomer customer = Chargify.LoadCustomer(referenceValue); + Assert.IsNotNull(customer); + + // Change the customer's "Organization" property + var oldAddress1 = customer.ShippingAddress; + var oldAddress2 = customer.ShippingAddress2; + var newAddress1 = Guid.NewGuid().ToString().Replace("-", string.Empty).ToUpperInvariant(); + var newAddress2 = Guid.NewGuid().ToString().Replace("-", string.Empty).ToUpperInvariant(); + string company = Faker.Company.Name(); + customer.Organization = company; + customer.ShippingAddress = newAddress1; + customer.ShippingAddress2 = newAddress2; + Assert.AreEqual(company, customer.Organization); + + // Update the customer and check the result + ICustomer result = Chargify.UpdateCustomer(customer); + Assert.IsNotNull(result); + Assert.AreEqual(company, result.Organization); + Assert.AreEqual(newAddress1, result.ShippingAddress); + Assert.AreEqual(newAddress2, result.ShippingAddress2); + + // Set it back to "nothing" again + customer.Organization = string.Empty; + customer.ShippingAddress = oldAddress1; + customer.ShippingAddress2 = oldAddress2; + ICustomer result1 = Chargify.UpdateCustomer(customer); + Assert.IsNotNull(result1); + Assert.AreEqual(string.Empty, result1.Organization); + Assert.AreEqual(oldAddress1, result1.ShippingAddress); + Assert.AreEqual(oldAddress2, result1.ShippingAddress2); + } + } +}