PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/sipsorcery-core/Tests/SIPSorcery.CRM.UnitTests/ThirtySevenSignalsPersonUnitTest.cs

https://github.com/thecc4re/sipsorcery-mono
C# | 246 lines | 184 code | 21 blank | 41 comment | 0 complexity | a5e66fc7ace4d83e6ddabb5628b7bbe2 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. // ============================================================================
  2. // FileName: ThirtySevenSignalsPersonUnitTest.cs
  3. //
  4. // Description:
  5. // Unit tests for the Person class from the 37 Signals contact management system Highrise.
  6. //
  7. // Author(s):
  8. // Aaron Clauson
  9. //
  10. // History:
  11. // 13 Feb 2011 Aaron Clauson Created.
  12. //
  13. // License:
  14. // This software is licensed under the BSD License http://www.opensource.org/licenses/bsd-license.php
  15. //
  16. // Copyright (c) 2011 Aaron Clauson (aaron@sipsorcery.com), SIP Sorcery Pty Ltd
  17. // All rights reserved.
  18. //
  19. // Redistribution and use in source and binary forms, with or without modification, are permitted provided that
  20. // the following conditions are met:
  21. //
  22. // Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  23. // Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
  24. // disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of SIP Sorcery Ltd.
  25. // nor the names of its contributors may be used to endorse or promote products derived from this software without specific
  26. // prior written permission.
  27. //
  28. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  29. // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  30. // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  31. // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  32. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. // POSSIBILITY OF SUCH DAMAGE.
  35. // ============================================================================
  36. using System;
  37. using System.Collections.Generic;
  38. using System.IO;
  39. using System.Linq;
  40. using System.Text;
  41. using System.Text.RegularExpressions;
  42. using System.Xml;
  43. using System.Xml.Serialization;
  44. using Microsoft.VisualStudio.TestTools.UnitTesting;
  45. using SIPSorcery.CRM.ThirtySevenSignals;
  46. namespace SIPSorcery.CRM.UnitTests
  47. {
  48. /// <summary>
  49. /// Tests for the 37Signals person class.
  50. /// </summary>
  51. [TestClass]
  52. public class ThirtySevenSignalsPersonUnitTest
  53. {
  54. public ThirtySevenSignalsPersonUnitTest()
  55. { }
  56. private TestContext testContextInstance;
  57. public TestContext TestContext
  58. {
  59. get
  60. {
  61. return testContextInstance;
  62. }
  63. set
  64. {
  65. testContextInstance = value;
  66. }
  67. }
  68. [TestMethod]
  69. public void DeserialisationTest()
  70. {
  71. string personXML =
  72. "<person>" +
  73. "<author-id type=\"integer\">391244</author-id>" +
  74. "<background>Smart cookie</background>" +
  75. "<company-id type=\"integer\">57644826</company-id> " +
  76. "<created-at type=\"datetime\">2011-01-12T10:17:53Z</created-at> " +
  77. "<first-name>Joe</first-name>" +
  78. "<group-id type=\"integer\" nil=\"true\" />" +
  79. " <id type=\"integer\">57644825</id> " +
  80. " <last-name>Bloggs</last-name> " +
  81. " <owner-id type=\"integer\" nil=\"true\" />" +
  82. " <title>Mr</title> " +
  83. " <updated-at type=\"datetime\">2011-01-29T01:10:39Z</updated-at> " +
  84. " <visible-to>Everyone</visible-to> " +
  85. " <contact-data>" +
  86. " <phone-numbers type=\"array\">" +
  87. " <phone-number>" +
  88. " <id type=\"integer\">51564454</id> " +
  89. " <location>Work</location> " +
  90. " <number>12345678</number> " +
  91. " </phone-number>" +
  92. " </phone-numbers>" +
  93. " <addresses type=\"array\" /> " +
  94. " <web-addresses type=\"array\" />" +
  95. " <email-addresses type=\"array\" /> " +
  96. " <instant-messengers type=\"array\" /> " +
  97. " <twitter-accounts type=\"array\" />" +
  98. " </contact-data>" +
  99. " </person>";
  100. personXML = Regex.Replace(personXML, "nil=\"true\"", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"");
  101. Person person = null;
  102. using (TextReader reader = new StringReader(personXML))
  103. {
  104. XmlSerializer serializer = new XmlSerializer(typeof(Person));
  105. person = (Person)serializer.Deserialize(reader);
  106. }
  107. Assert.IsNotNull(person, "The person object was not deserialised from the XML.");
  108. Assert.AreEqual(person.ID, 57644825, "The person's id was not deserialised correctly.");
  109. Assert.AreEqual(person.AuthorID, 391244, "The person's author id was not deserialised correctly.");
  110. Assert.AreEqual(person.FirstName, "Joe", "The person's first name was not deserialised correctly.");
  111. Assert.AreEqual(person.LastName, "Bloggs", "The person's last name was not deserialised correctly.");
  112. Assert.AreEqual(person.Title, "Mr", "The person's title was not deserialised correctly.");
  113. Assert.AreEqual(person.Background, "Smart cookie", "The person's background was not deserialised correctly.");
  114. Assert.AreEqual(person.CompanyID, 57644826, "The person's company id was not deserialised correctly.");
  115. Assert.AreEqual(person.CreatedAtStr, "2011-01-12T10:17:53Z", "The person's created at date string was not deserialised correctly.");
  116. Assert.AreEqual(person.CreatedAt.ToUniversalTime().ToString(Person.DATETIME_STRING_FORMAT), "2011-01-12T10:17:53Z", "The person's created at datetime offset was not deserialised correctly.");
  117. Assert.AreEqual(person.UpdatedAtStr, "2011-01-29T01:10:39Z", "The person's updated at date string was not deserialised correctly.");
  118. Assert.AreEqual(person.UpdatedAt.ToUniversalTime().ToString(Person.DATETIME_STRING_FORMAT), "2011-01-29T01:10:39Z", "The person's updated at datetime offset was not deserialised correctly.");
  119. Assert.AreEqual(person.VisibleTo, "Everyone", "The person's visible to value was not deserialised correctly.");
  120. Assert.IsNull(person.OwnerID, "The person's owner id was not deserialised correctly.");
  121. Assert.IsNull(person.GroupID, "The person's group id was not deserialised correctly.");
  122. Assert.IsNotNull(person.ContactData, "The person's contact data was not deserialised correctly.");
  123. Assert.AreEqual(person.ContactData.PhoneNumbers.Count, 1, "The person's phone numbers did not deserialise to the correct count.");
  124. Assert.AreEqual(person.ContactData.PhoneNumbers[0].ID, 51564454, "The person's phone number id did not deserialise correctly.");
  125. Assert.AreEqual(person.ContactData.PhoneNumbers[0].Location, "Work", "The person's phone number location did not deserialise correctly.");
  126. Assert.AreEqual(person.ContactData.PhoneNumbers[0].Number, "12345678", "The person's phone number number did not deserialise correctly.");
  127. }
  128. [TestMethod]
  129. public void DeserialisationNullCompanyIDTest()
  130. {
  131. string personXML =
  132. "<person>" +
  133. "<author-id type=\"integer\">391244</author-id>" +
  134. "<background>Smart cookie</background>" +
  135. "<company-id type=\"integer\" nil=\"true\"></company-id> " +
  136. "<created-at type=\"datetime\">2011-01-12T10:17:53Z</created-at> " +
  137. "<first-name>Joe</first-name>" +
  138. "<group-id type=\"integer\" nil=\"true\" />" +
  139. " <id type=\"integer\">57644825</id> " +
  140. " <last-name>Bloggs</last-name> " +
  141. " <owner-id type=\"integer\" nil=\"true\" />" +
  142. " <title>Mr</title> " +
  143. " <updated-at type=\"datetime\">2011-01-29T01:10:39Z</updated-at> " +
  144. " <visible-to>Everyone</visible-to> " +
  145. " <contact-data>" +
  146. " <phone-numbers type=\"array\">" +
  147. " <phone-number>" +
  148. " <id type=\"integer\">51564454</id> " +
  149. " <location>Work</location> " +
  150. " <number>12345678</number> " +
  151. " </phone-number>" +
  152. " </phone-numbers>" +
  153. " <addresses type=\"array\" /> " +
  154. " <web-addresses type=\"array\" />" +
  155. " <email-addresses type=\"array\" /> " +
  156. " <instant-messengers type=\"array\" /> " +
  157. " <twitter-accounts type=\"array\" />" +
  158. " </contact-data>" +
  159. " </person>";
  160. personXML = Regex.Replace(personXML, "nil=\"true\"", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"");
  161. Person person = null;
  162. using (TextReader reader = new StringReader(personXML))
  163. {
  164. XmlSerializer serializer = new XmlSerializer(typeof(Person));
  165. person = (Person)serializer.Deserialize(reader);
  166. }
  167. Assert.IsNull(person.CompanyID, "The person's company id was not deserialised correctly.");
  168. }
  169. /// <summary>
  170. /// Test deserialising an XML array of person data.
  171. /// </summary>
  172. [TestMethod]
  173. public void DeserialisationPeopleTest()
  174. {
  175. string peopleXML =
  176. "<people>" +
  177. "<person>" +
  178. "<author-id type=\"integer\">391244</author-id>" +
  179. "<background>Smart cookie</background>" +
  180. "<company-id type=\"integer\" nil=\"true\"></company-id> " +
  181. "<created-at type=\"datetime\">2011-01-12T10:17:53Z</created-at> " +
  182. "<first-name>Joe</first-name>" +
  183. "<group-id type=\"integer\" nil=\"true\" />" +
  184. " <id type=\"integer\">57644825</id> " +
  185. " <last-name>Bloggs</last-name> " +
  186. " <owner-id type=\"integer\" nil=\"true\" />" +
  187. " <title>Mr</title> " +
  188. " <updated-at type=\"datetime\">2011-01-29T01:10:39Z</updated-at> " +
  189. " <visible-to>Everyone</visible-to> " +
  190. " <contact-data>" +
  191. " <phone-numbers type=\"array\">" +
  192. " <phone-number>" +
  193. " <id type=\"integer\">51564454</id> " +
  194. " <location>Work</location> " +
  195. " <number>12345678</number> " +
  196. " </phone-number>" +
  197. " </phone-numbers>" +
  198. " <addresses type=\"array\" /> " +
  199. " <web-addresses type=\"array\" />" +
  200. " <email-addresses type=\"array\" /> " +
  201. " <instant-messengers type=\"array\" /> " +
  202. " <twitter-accounts type=\"array\" />" +
  203. " </contact-data>" +
  204. " </person>" +
  205. "</people>";
  206. peopleXML = Regex.Replace(peopleXML, "nil=\"true\"", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"");
  207. People people = null;
  208. using (TextReader reader = new StringReader(peopleXML))
  209. {
  210. XmlSerializer serializer = new XmlSerializer(typeof(People));
  211. people = (People)serializer.Deserialize(reader);
  212. }
  213. Assert.AreEqual(1, people.PersonList.Count, "The number of people deserialised was incorrect.");
  214. Assert.AreEqual(57644825, people.PersonList[0].ID, "The person's deserialised id was incorrect.");
  215. }
  216. [TestMethod]
  217. public void SetAvatarURLTest()
  218. {
  219. string url = "https://somebiz.highrisehq.com";
  220. Person person = new Person() { ID = 12345678 };
  221. person.SetAvatarURL(url);
  222. Assert.AreEqual(String.Format(Person.AVATAR_URL_FORMAT, url, 1234, 5678), person.AvatarURL, "The person's avatar URL was not set correctly.");
  223. }
  224. }
  225. }