PageRenderTime 51ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/1.31/src/Tests/Platform/OrganizationApiTests.cs

#
C# | 308 lines | 236 code | 52 blank | 20 comment | 7 complexity | a3d8e3aa8bec448d98883271679c3cda MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1
  1. /****************************************************************************************************
  2. Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
  3. Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ****************************************************************************************************/
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Data.Linq;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Text;
  21. using System.Transactions;
  22. using System.Web.UI;
  23. using System.Web.UI.WebControls;
  24. using System.Xml;
  25. using System.Xml.Schema;
  26. using BaoJianSoft.Common;
  27. using BaoJianSoft.Common.Data;
  28. using BaoJianSoft.Common.Validation;
  29. using BaoJianSoft.Platform;
  30. using BaoJianSoft.Platform.Linq;
  31. using BaoJianSoft.RapidWeb;
  32. using BaoJianSoft.RapidWeb.Controls;
  33. using BaoJianSoft.RapidWeb.DynamicPages;
  34. using NUnit.Framework;
  35. namespace BaoJianSoft.Tests.Platform
  36. {
  37. [TestFixture]
  38. public class OrganizationApiTests
  39. {
  40. List<Guid> createdOrganizationIds = new List<Guid>();
  41. List<Guid> createdOrganizationTypeIds = new List<Guid>();
  42. [TearDown]
  43. public void TearDown()
  44. {
  45. using (MembershipDataContext ctx = DataContextFactory.Create<MembershipDataContext>())
  46. {
  47. var organizationsToDelete = ctx.Organizations.Where(org => createdOrganizationIds.ToArray().Contains(org.OrganizationId));
  48. var organizationTypesToDelete = ctx.OrganizationTypes.Where(orgType => createdOrganizationTypeIds.ToArray().Contains(orgType.OrganizationTypeId));
  49. ctx.Organizations.DeleteAllOnSubmit(organizationsToDelete);
  50. ctx.OrganizationTypes.DeleteAllOnSubmit(organizationTypesToDelete);
  51. ctx.SubmitChanges();
  52. }
  53. }
  54. [Test, Description("Basic organization type CRUD test.")]
  55. public void BasicOrganizationTypeTest()
  56. {
  57. IOrganizationApi organizationApi = SpringContext.Current.GetObject<IOrganizationApi>();
  58. OrganizationTypeObject department = new OrganizationTypeObject { Name = "department", Domain = "Inc", Description = "department-desc" };
  59. organizationApi.Save(department);
  60. createdOrganizationTypeIds.Add(department.OrganizationTypeId);
  61. OrganizationTypeObject customer = new OrganizationTypeObject { Name = "customer", Domain = "Customer", Description = "customer-desc" };
  62. organizationApi.Save(customer);
  63. createdOrganizationTypeIds.Add(customer.OrganizationTypeId);
  64. department = organizationApi.GetOrganizationType(department.OrganizationTypeId);
  65. Assert.AreEqual("department", department.Name);
  66. Assert.AreEqual("department-desc", department.Description);
  67. customer = organizationApi.GetOrganizationType("customer");
  68. Assert.AreEqual("customer", customer.Name);
  69. Assert.AreEqual("customer-desc", customer.Description);
  70. var organizationTypes = organizationApi.FindOrganizationTypes().ToList();
  71. Assert.AreEqual(3, organizationTypes.Count());
  72. Assert.AreEqual(1, organizationTypes.Where(ct => ct.Name == "customer").Count());
  73. Assert.AreEqual(1, organizationTypes.Where(ct => ct.Name == "department").Count());
  74. department.Name = "departmentX";
  75. department.Description = "departmentX-desc";
  76. organizationApi.Save(department);
  77. department = organizationApi.GetOrganizationType(department.OrganizationTypeId);
  78. Assert.AreEqual("departmentX", department.Name);
  79. Assert.AreEqual("departmentX-desc", department.Description);
  80. Assert.IsNotNull(organizationApi.GetOrganizationType("customer"));
  81. organizationTypes = organizationApi.FindOrganizationTypes().ToList();
  82. Assert.AreEqual(3, organizationTypes.Count());
  83. Assert.AreEqual(1, organizationTypes.Where(ct => ct.Name == "customer").Count());
  84. Assert.AreEqual(1, organizationTypes.Where(ct => ct.Name == "departmentX").Count());
  85. customer.DeleteStatus = DeleteStatus.Deleted;
  86. organizationApi.Save(customer);
  87. organizationTypes = organizationApi.FindOrganizationTypes().Where(orgType => orgType.DeleteStatus == DeleteStatus.NotDeleted).ToList();
  88. Assert.AreEqual(2, organizationTypes.Count());
  89. Assert.AreEqual(1, organizationTypes.Where(ct => ct.Name == "departmentX").Count());
  90. department = organizationApi.GetOrganizationType(department.OrganizationTypeId);
  91. Assert.AreEqual("departmentX", department.Name);
  92. Assert.AreEqual("departmentX-desc", department.Description);
  93. department.DeleteStatus = DeleteStatus.Deleted;
  94. organizationApi.Save(department);
  95. organizationTypes = organizationApi.FindOrganizationTypes().Where(orgType => orgType.DeleteStatus == DeleteStatus.NotDeleted).ToList();
  96. Assert.AreEqual(1, organizationTypes.Count());
  97. }
  98. [Test, Description("Basic organization CRUD test.")]
  99. public void BasicOrganizationTest()
  100. {
  101. IOrganizationApi organizationApi = SpringContext.Current.GetObject<IOrganizationApi>();
  102. OrganizationTypeObject department = new OrganizationTypeObject { Name = "department", Domain = "Inc", Description = "department-desc" };
  103. organizationApi.Save(department);
  104. createdOrganizationTypeIds.Add(department.OrganizationTypeId);
  105. OrganizationObject shOrganization = new OrganizationObject
  106. {
  107. OrganizationCode = "sh",
  108. OrganizationName = "sh-department",
  109. OrganizationTypeId = department.OrganizationTypeId,
  110. Status = OrganizationStatus.Enabled,
  111. Description = "sh-desc"
  112. };
  113. organizationApi.Save(shOrganization);
  114. createdOrganizationIds.Add(shOrganization.OrganizationId);
  115. OrganizationObject cdOrganization = new OrganizationObject
  116. {
  117. OrganizationCode = "cd",
  118. OrganizationName = "cd-department",
  119. OrganizationTypeId = department.OrganizationTypeId,
  120. Status = OrganizationStatus.Enabled,
  121. Description = "cd-desc"
  122. };
  123. organizationApi.Save(cdOrganization);
  124. createdOrganizationIds.Add(cdOrganization.OrganizationId);
  125. shOrganization = organizationApi.GetOrganization(shOrganization.OrganizationId);
  126. Assert.AreEqual("sh", shOrganization.OrganizationCode);
  127. Assert.AreEqual("sh-department", shOrganization.OrganizationName);
  128. Assert.IsFalse(shOrganization.ParentOrganizationId.HasValue);
  129. Assert.AreEqual(OrganizationStatus.Enabled, shOrganization.Status);
  130. Assert.AreEqual("sh-desc", shOrganization.Description);
  131. cdOrganization = organizationApi.GetOrganization(cdOrganization.OrganizationId);
  132. Assert.IsNotNull(cdOrganization);
  133. shOrganization.OrganizationName = "021-depart";
  134. shOrganization.Description = "021-desc";
  135. organizationApi.Save(shOrganization);
  136. shOrganization = organizationApi.GetOrganization(shOrganization.OrganizationId);
  137. Assert.AreEqual("021-depart", shOrganization.OrganizationName);
  138. Assert.IsFalse(shOrganization.ParentOrganizationId.HasValue);
  139. Assert.AreEqual(OrganizationStatus.Enabled, shOrganization.Status);
  140. Assert.AreEqual("021-desc", shOrganization.Description);
  141. shOrganization = organizationApi.GetOrganizationByName("021-depart");
  142. Assert.AreEqual("021-depart", shOrganization.OrganizationName);
  143. shOrganization = organizationApi.GetOrganizationByCode("sh");
  144. Assert.AreEqual("021-depart", shOrganization.OrganizationName);
  145. }
  146. [Test, Description("Save organization test.")]
  147. public void SaveOrganizationTest()
  148. {
  149. IOrganizationApi organizationApi = SpringContext.Current.GetObject<IOrganizationApi>();
  150. OrganizationTypeObject department = new OrganizationTypeObject { Name = "department", Domain = "Inc", Description = "department-desc" };
  151. organizationApi.Save(department);
  152. createdOrganizationTypeIds.Add(department.OrganizationTypeId);
  153. OrganizationObject shanghai = new OrganizationObject
  154. {
  155. OrganizationCode = "sh",
  156. OrganizationName = "sh-department",
  157. OrganizationTypeId = department.OrganizationTypeId,
  158. Status = OrganizationStatus.Enabled,
  159. Description = "sh-desc"
  160. };
  161. organizationApi.Save(shanghai);
  162. createdOrganizationIds.Add(shanghai.OrganizationId);
  163. shanghai = organizationApi.GetOrganization(shanghai.OrganizationId);
  164. shanghai.OrganizationName = "shanghai organization";
  165. shanghai.Description = "shanghai organization description";
  166. organizationApi.Save(shanghai);
  167. shanghai = organizationApi.GetOrganization(shanghai.OrganizationId);
  168. Assert.AreEqual("shanghai organization", shanghai.OrganizationName);
  169. Assert.AreEqual("shanghai organization description", shanghai.Description);
  170. }
  171. [Test, Description("Setting wrong child organization status on creating test.")]
  172. [ExpectedException(typeof(ValidationException))]
  173. public void WrongChildOrganizationStatusOnCreatingTest()
  174. {
  175. IOrganizationApi organizationApi = SpringContext.Current.GetObject<IOrganizationApi>();
  176. OrganizationTypeObject department = new OrganizationTypeObject { Name = "department", Domain = "Inc", Description = "department-desc" };
  177. organizationApi.Save(department);
  178. createdOrganizationTypeIds.Add(department.OrganizationTypeId);
  179. OrganizationObject shOrganization = new OrganizationObject
  180. {
  181. OrganizationCode = "sh",
  182. OrganizationName = "sh-department",
  183. OrganizationTypeId = department.OrganizationTypeId,
  184. Status = OrganizationStatus.Disabled,
  185. Description = "sh-desc"
  186. };
  187. organizationApi.Save(shOrganization);
  188. createdOrganizationIds.Add(shOrganization.OrganizationId);
  189. OrganizationObject cdOrganization = new OrganizationObject
  190. {
  191. OrganizationCode = "cd",
  192. OrganizationName = "cd-department",
  193. OrganizationTypeId = department.OrganizationTypeId,
  194. Status = OrganizationStatus.Enabled,
  195. ParentOrganizationId = shOrganization.OrganizationId,
  196. Description = "cd-desc"
  197. };
  198. organizationApi.Save(cdOrganization);
  199. }
  200. [Test, Description("Organization status update test.")]
  201. public void UpdateOrganizationStatusTest()
  202. {
  203. IOrganizationApi organizationApi = SpringContext.Current.GetObject<IOrganizationApi>();
  204. OrganizationTypeObject department = new OrganizationTypeObject { Name = "department", Domain = "Inc", Description = "department-desc" };
  205. organizationApi.Save(department);
  206. createdOrganizationTypeIds.Add(department.OrganizationTypeId);
  207. OrganizationObject shOrganization = new OrganizationObject
  208. {
  209. OrganizationCode = "sh",
  210. OrganizationName = "sh-department",
  211. OrganizationTypeId = department.OrganizationTypeId,
  212. Status = OrganizationStatus.Enabled,
  213. Description = "sh-desc"
  214. };
  215. organizationApi.Save(shOrganization);
  216. createdOrganizationIds.Add(shOrganization.OrganizationId);
  217. OrganizationObject cdOrganization = new OrganizationObject
  218. {
  219. OrganizationCode = "cd",
  220. OrganizationName = "cd-department",
  221. OrganizationTypeId = department.OrganizationTypeId,
  222. Status = OrganizationStatus.Enabled,
  223. ParentOrganizationId = shOrganization.OrganizationId,
  224. Description = "cd-desc"
  225. };
  226. organizationApi.Save(cdOrganization);
  227. createdOrganizationIds.Add(cdOrganization.OrganizationId);
  228. // update parent's status from Enabled to Disabled will affect its children
  229. shOrganization.Status = OrganizationStatus.Disabled;
  230. organizationApi.Save(shOrganization);
  231. shOrganization = organizationApi.GetOrganization(shOrganization.OrganizationId);
  232. cdOrganization = organizationApi.GetOrganization(cdOrganization.OrganizationId);
  233. Assert.AreEqual(OrganizationStatus.Disabled, shOrganization.Status);
  234. Assert.AreEqual(OrganizationStatus.Disabled, cdOrganization.Status);
  235. // update parent's status from Disabled to Enabled won't affect its children
  236. shOrganization.Status = OrganizationStatus.Enabled;
  237. organizationApi.Save(shOrganization);
  238. shOrganization = organizationApi.GetOrganization(shOrganization.OrganizationId);
  239. cdOrganization = organizationApi.GetOrganization(cdOrganization.OrganizationId);
  240. Assert.AreEqual(OrganizationStatus.Enabled, shOrganization.Status);
  241. Assert.AreEqual(OrganizationStatus.Disabled, cdOrganization.Status);
  242. cdOrganization.Status = OrganizationStatus.Enabled;
  243. organizationApi.Save(cdOrganization);
  244. // update child's status from Enabled to Disabled won't affect its parent
  245. cdOrganization.Status = OrganizationStatus.Disabled;
  246. organizationApi.Save(cdOrganization);
  247. shOrganization = organizationApi.GetOrganization(shOrganization.OrganizationId);
  248. cdOrganization = organizationApi.GetOrganization(cdOrganization.OrganizationId);
  249. Assert.AreEqual(OrganizationStatus.Enabled, shOrganization.Status);
  250. Assert.AreEqual(OrganizationStatus.Disabled, cdOrganization.Status);
  251. }
  252. }
  253. }