ProjectTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. Copyright 2011 - 2022 Adrian Popescu
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. using System.Collections.Generic;
  14. using System.Collections.Specialized;
  15. using Padi.RedmineApi.Tests.Infrastructure;
  16. using Redmine.Net.Api;
  17. using Redmine.Net.Api.Exceptions;
  18. using Redmine.Net.Api.Types;
  19. using Xunit;
  20. namespace Padi.RedmineApi.Tests.Tests.Sync
  21. {
  22. [Trait("Redmine-Net-Api", "Projects")]
  23. #if !(NET20 || NET40)
  24. [Collection("RedmineCollection")]
  25. #endif
  26. [Order(1)]
  27. public class ProjectTests
  28. {
  29. public ProjectTests(RedmineFixture fixture)
  30. {
  31. this.fixture = fixture;
  32. }
  33. private const string PROJECT_IDENTIFIER = "redmine-net-api-project-test";
  34. private const string PROJECT_NAME = "Redmine Net Api Project Test";
  35. private readonly RedmineFixture fixture;
  36. private static Project CreateTestProjectWithRequiredPropertiesSet()
  37. {
  38. var project = new Project
  39. {
  40. Name = PROJECT_NAME,
  41. Identifier = PROJECT_IDENTIFIER
  42. };
  43. return project;
  44. }
  45. private static Project CreateTestProjectWithAllPropertiesSet()
  46. {
  47. var project = new Project
  48. {
  49. Name = "Redmine Net Api Project Test All Properties",
  50. Description = "This is a test project.",
  51. Identifier = "rnaptap",
  52. HomePage = "www.redminetest.com",
  53. IsPublic = true,
  54. InheritMembers = true,
  55. EnabledModules = new List<ProjectEnabledModule>
  56. {
  57. new ProjectEnabledModule {Name = "issue_tracking"},
  58. new ProjectEnabledModule {Name = "time_tracking"}
  59. },
  60. Trackers = new List<ProjectTracker>
  61. {
  62. (ProjectTracker) IdentifiableName.Create<ProjectTracker>( 1),
  63. (ProjectTracker) IdentifiableName.Create<ProjectTracker>(2)
  64. }
  65. };
  66. return project;
  67. }
  68. private static Project CreateTestProjectWithInvalidTrackersId()
  69. {
  70. var project = new Project
  71. {
  72. Name = "Redmine Net Api Project Test Invalid Trackers",
  73. Identifier = "rnaptit",
  74. Trackers = new List<ProjectTracker>
  75. {
  76. (ProjectTracker) IdentifiableName.Create<ProjectTracker>(999999),
  77. (ProjectTracker) IdentifiableName.Create<ProjectTracker>(999998)
  78. }
  79. };
  80. return project;
  81. }
  82. private static Project CreateTestProjectWithParentSet(int parentId)
  83. {
  84. var project = new Project
  85. {
  86. Name = "Redmine Net Api Project With Parent Set",
  87. Identifier = "rnapwps",
  88. Parent = IdentifiableName.Create<IdentifiableName>(parentId)
  89. };
  90. return project;
  91. }
  92. [Fact, Order(0)]
  93. public void Should_Create_Project_With_Required_Properties()
  94. {
  95. var savedProject = fixture.RedmineManager.CreateObject(CreateTestProjectWithRequiredPropertiesSet());
  96. Assert.NotNull(savedProject);
  97. Assert.NotEqual(0, savedProject.Id);
  98. Assert.True(savedProject.Name.Equals(PROJECT_NAME), "Project name is invalid.");
  99. Assert.True(savedProject.Identifier.Equals(PROJECT_IDENTIFIER), "Project identifier is invalid.");
  100. }
  101. [Fact, Order(1)]
  102. public void Should_Create_Project_With_All_Properties_Set()
  103. {
  104. var savedProject = fixture.RedmineManager.CreateObject(CreateTestProjectWithAllPropertiesSet());
  105. Assert.NotNull(savedProject);
  106. Assert.NotEqual(0, savedProject.Id);
  107. Assert.True(savedProject.Identifier.Equals("rnaptap"), "Project identifier is invalid.");
  108. Assert.True(savedProject.Name.Equals("Redmine Net Api Project Test All Properties"),
  109. "Project name is invalid.");
  110. }
  111. [Fact, Order(2)]
  112. public void Should_Create_Project_With_Parent()
  113. {
  114. var parentProject =
  115. fixture.RedmineManager.CreateObject(new Project { Identifier = "parent-project", Name = "Parent project" });
  116. var savedProject = fixture.RedmineManager.CreateObject(CreateTestProjectWithParentSet(parentProject.Id));
  117. Assert.NotNull(savedProject);
  118. Assert.True(savedProject.Parent.Id == parentProject.Id, "Parent project is invalid.");
  119. }
  120. [Fact, Order(3)]
  121. public void Should_Get_Redmine_Net_Api_Project_Test_Project()
  122. {
  123. var project = fixture.RedmineManager.GetObject<Project>(PROJECT_IDENTIFIER, null);
  124. Assert.NotNull(project);
  125. Assert.IsType<Project>(project);
  126. Assert.Equal(project.Identifier, PROJECT_IDENTIFIER);
  127. Assert.Equal(project.Name, PROJECT_NAME);
  128. }
  129. [Fact, Order(4)]
  130. public void Should_Get_Test_Project_With_All_Properties_Set()
  131. {
  132. var project = fixture.RedmineManager.GetObject<Project>("rnaptap", new NameValueCollection
  133. {
  134. {RedmineKeys.INCLUDE, string.Join(",", RedmineKeys.TRACKERS, RedmineKeys.ENABLED_MODULES)}
  135. });
  136. Assert.NotNull(project);
  137. Assert.IsType<Project>(project);
  138. Assert.True(project.Name.Equals("Redmine Net Api Project Test All Properties"), "Project name not equal.");
  139. Assert.True(project.Identifier.Equals("rnaptap"), "Project identifier not equal.");
  140. Assert.True(project.Description.Equals("This is a test project."), "Project description not equal.");
  141. Assert.True(project.HomePage.Equals("www.redminetest.com"), "Project homepage not equal.");
  142. Assert.True(project.IsPublic.Equals(true),
  143. "Project is_public not equal. (This property is available starting with 2.6.0)");
  144. Assert.NotNull(project.Trackers);
  145. Assert.True(project.Trackers.Count == 2, "Trackers count != " + 2);
  146. Assert.NotNull(project.EnabledModules);
  147. Assert.True(project.EnabledModules.Count == 2,
  148. "Enabled modules count (" + project.EnabledModules.Count + ") != " + 2);
  149. }
  150. [Fact, Order(5)]
  151. public void Should_Update_Redmine_Net_Api_Project_Test_Project()
  152. {
  153. const string UPDATED_PROJECT_NAME = "Project created using API updated";
  154. const string UPDATED_PROJECT_DESCRIPTION = "Test project description updated";
  155. const string UPDATED_PROJECT_HOMEPAGE = "http://redmineTestsUpdated.com";
  156. const bool UPDATED_PROJECT_ISPUBLIC = true;
  157. const bool UPDATED_PROJECT_INHERIT_MEMBERS = false;
  158. var project = fixture.RedmineManager.GetObject<Project>(PROJECT_IDENTIFIER, null);
  159. project.Name = UPDATED_PROJECT_NAME;
  160. project.Description = UPDATED_PROJECT_DESCRIPTION;
  161. project.HomePage = UPDATED_PROJECT_HOMEPAGE;
  162. project.IsPublic = UPDATED_PROJECT_ISPUBLIC;
  163. project.InheritMembers = UPDATED_PROJECT_INHERIT_MEMBERS;
  164. var exception =
  165. (RedmineException)
  166. Record.Exception(() => fixture.RedmineManager.UpdateObject(PROJECT_IDENTIFIER, project));
  167. Assert.Null(exception);
  168. var updatedProject = fixture.RedmineManager.GetObject<Project>(PROJECT_IDENTIFIER, null);
  169. Assert.True(updatedProject.Name.Equals(UPDATED_PROJECT_NAME), "Project name was not updated.");
  170. Assert.True(updatedProject.Description.Equals(UPDATED_PROJECT_DESCRIPTION),
  171. "Project description was not updated.");
  172. Assert.True(updatedProject.HomePage.Equals(UPDATED_PROJECT_HOMEPAGE), "Project homepage was not updated.");
  173. Assert.True(updatedProject.IsPublic.Equals(UPDATED_PROJECT_ISPUBLIC),
  174. "Project is_public was not updated. (This property is available starting with 2.6.0)");
  175. }
  176. [Fact, Order(7)]
  177. public void Should_Throw_Exception_When_Create_Empty_Project()
  178. {
  179. Assert.Throws<RedmineException>(() => fixture.RedmineManager.CreateObject(new Project()));
  180. }
  181. [Fact, Order(8)]
  182. public void Should_Throw_Exception_When_Project_Identifier_Is_Invalid()
  183. {
  184. Assert.Throws<NotFoundException>(() => fixture.RedmineManager.GetObject<Project>("99999999", null));
  185. }
  186. [Fact, Order(9)]
  187. public void Should_Delete_Project_And_Parent_Project()
  188. {
  189. var exception =
  190. (RedmineException)Record.Exception(() => fixture.RedmineManager.DeleteObject<Project>("rnapwps"));
  191. Assert.Null(exception);
  192. Assert.Throws<NotFoundException>(() => fixture.RedmineManager.GetObject<Project>("rnapwps", null));
  193. exception =
  194. (RedmineException)
  195. Record.Exception(() => fixture.RedmineManager.DeleteObject<Project>("parent-project"));
  196. Assert.Null(exception);
  197. Assert.Throws<NotFoundException>(() => fixture.RedmineManager.GetObject<Project>("parent-project", null));
  198. }
  199. [Fact, Order(10)]
  200. public void Should_Delete_Project_With_All_Properties_Set()
  201. {
  202. var exception =
  203. (RedmineException)Record.Exception(() => fixture.RedmineManager.DeleteObject<Project>("rnaptap"));
  204. Assert.Null(exception);
  205. Assert.Throws<NotFoundException>(() => fixture.RedmineManager.GetObject<Project>("rnaptap", null));
  206. }
  207. [Fact, Order(11)]
  208. public void Should_Delete_Redmine_Net_Api_Project_Test_Project()
  209. {
  210. var exception =
  211. (RedmineException)
  212. Record.Exception(() => fixture.RedmineManager.DeleteObject<Project>(PROJECT_IDENTIFIER));
  213. Assert.Null(exception);
  214. Assert.Throws<NotFoundException>(() => fixture.RedmineManager.GetObject<Project>(PROJECT_IDENTIFIER, null));
  215. }
  216. [Fact, Order(12)]
  217. public void Should_Throw_Exception_Create_Project_Invalid_Trackers()
  218. {
  219. Assert.Throws<NotFoundException>(
  220. () => fixture.RedmineManager.CreateObject(CreateTestProjectWithInvalidTrackersId()));
  221. }
  222. }
  223. }