GroupTests.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Collections.Generic;
  2. using System.Collections.Specialized;
  3. using Padi.RedmineApi.Tests.Infrastructure;
  4. using Redmine.Net.Api;
  5. using Redmine.Net.Api.Exceptions;
  6. using Redmine.Net.Api.Types;
  7. using Xunit;
  8. namespace Padi.RedmineApi.Tests.Tests.Sync
  9. {
  10. [Trait("Redmine-Net-Api", "Groups")]
  11. #if !(NET20 || NET40)
  12. [Collection("RedmineCollection")]
  13. #endif
  14. public class GroupTests
  15. {
  16. public GroupTests(RedmineFixture fixture)
  17. {
  18. this.fixture = fixture;
  19. }
  20. private readonly RedmineFixture fixture;
  21. private const string GROUP_ID = "57";
  22. private const int NUMBER_OF_MEMBERSHIPS = 1;
  23. private const int NUMBER_OF_USERS = 2;
  24. [Fact, Order(1)]
  25. public void Should_Add_Group()
  26. {
  27. const string NEW_GROUP_NAME = "Developers1";
  28. const int NEW_GROUP_USER_ID = 8;
  29. var group = new Group();
  30. group.Name = NEW_GROUP_NAME;
  31. group.Users = new List<GroupUser> { (GroupUser)IdentifiableName.Create<GroupUser>(NEW_GROUP_USER_ID )};
  32. Group savedGroup = null;
  33. var exception =
  34. (RedmineException)Record.Exception(() => savedGroup = fixture.RedmineManager.CreateObject(group));
  35. Assert.Null(exception);
  36. Assert.NotNull(savedGroup);
  37. Assert.True(group.Name.Equals(savedGroup.Name), "Group name is not valid.");
  38. }
  39. [Fact, Order(2)]
  40. public void Should_Update_Group()
  41. {
  42. const string UPDATED_GROUP_ID = "58";
  43. const string UPDATED_GROUP_NAME = "Best Developers";
  44. const int UPDATED_GROUP_USER_ID = 2;
  45. var group = fixture.RedmineManager.GetObject<Group>(UPDATED_GROUP_ID,
  46. new NameValueCollection { { RedmineKeys.INCLUDE, RedmineKeys.USERS } });
  47. group.Name = UPDATED_GROUP_NAME;
  48. group.Users.Add((GroupUser)IdentifiableName.Create<GroupUser>(UPDATED_GROUP_USER_ID));
  49. fixture.RedmineManager.UpdateObject(UPDATED_GROUP_ID, group);
  50. var updatedGroup = fixture.RedmineManager.GetObject<Group>(UPDATED_GROUP_ID,
  51. new NameValueCollection { { RedmineKeys.INCLUDE, RedmineKeys.USERS } });
  52. Assert.NotNull(updatedGroup);
  53. Assert.True(updatedGroup.Name.Equals(UPDATED_GROUP_NAME), "Group name was not updated.");
  54. Assert.NotNull(updatedGroup.Users);
  55. // Assert.True(updatedGroup.Users.Find(u => u.Id == UPDATED_GROUP_USER_ID) != null,
  56. //"User was not added to group.");
  57. }
  58. [Fact, Order(3)]
  59. public void Should_Get_All_Groups()
  60. {
  61. const int NUMBER_OF_GROUPS = 3;
  62. var groups = fixture.RedmineManager.GetObjects<Group>();
  63. Assert.NotNull(groups);
  64. Assert.True(groups.Count == NUMBER_OF_GROUPS, "Number of groups ( " + groups.Count + " ) != " + NUMBER_OF_GROUPS);
  65. }
  66. [Fact, Order(4)]
  67. public void Should_Get_Group_With_All_Associated_Data()
  68. {
  69. var group = fixture.RedmineManager.GetObject<Group>(GROUP_ID,
  70. new NameValueCollection { { RedmineKeys.INCLUDE, RedmineKeys.MEMBERSHIPS + "," + RedmineKeys.USERS } });
  71. Assert.NotNull(group);
  72. Assert.True(group.Memberships.Count == NUMBER_OF_MEMBERSHIPS,
  73. "Number of memberships != " + NUMBER_OF_MEMBERSHIPS);
  74. Assert.True(group.Users.Count == NUMBER_OF_USERS, "Number of users ( " + group.Users.Count + " ) != " + NUMBER_OF_USERS);
  75. Assert.True(group.Name.Equals("Test"), "Group name is not valid.");
  76. }
  77. [Fact, Order(5)]
  78. public void Should_Get_Group_With_Memberships()
  79. {
  80. var group = fixture.RedmineManager.GetObject<Group>(GROUP_ID,
  81. new NameValueCollection { { RedmineKeys.INCLUDE, RedmineKeys.MEMBERSHIPS } });
  82. Assert.NotNull(group);
  83. Assert.True(group.Memberships.Count == NUMBER_OF_MEMBERSHIPS,
  84. "Number of memberships ( " + group.Memberships.Count + " ) != " + NUMBER_OF_MEMBERSHIPS);
  85. }
  86. [Fact, Order(6)]
  87. public void Should_Get_Group_With_Users()
  88. {
  89. var group = fixture.RedmineManager.GetObject<Group>(GROUP_ID,
  90. new NameValueCollection { { RedmineKeys.INCLUDE, RedmineKeys.USERS } });
  91. Assert.NotNull(group);
  92. Assert.True(group.Users.Count == NUMBER_OF_USERS, "Number of users ( " + group.Users.Count + " ) != " + NUMBER_OF_USERS);
  93. }
  94. [Fact, Order(99)]
  95. public void Should_Delete_Group()
  96. {
  97. const string DELETED_GROUP_ID = "63";
  98. var exception =
  99. (RedmineException)
  100. Record.Exception(() => fixture.RedmineManager.DeleteObject<Group>(DELETED_GROUP_ID));
  101. Assert.Null(exception);
  102. Assert.Throws<NotFoundException>(() => fixture.RedmineManager.GetObject<Group>(DELETED_GROUP_ID, null));
  103. }
  104. }
  105. }