You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GroupTester.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonarqube.ws.tester;
  21. import java.util.List;
  22. import java.util.Set;
  23. import java.util.concurrent.atomic.AtomicInteger;
  24. import java.util.function.Consumer;
  25. import java.util.stream.Collectors;
  26. import org.assertj.core.api.Assertions;
  27. import org.sonarqube.ws.UserGroups;
  28. import org.sonarqube.ws.Users;
  29. import org.sonarqube.ws.Users.GroupsWsResponse.Group;
  30. import org.sonarqube.ws.client.usergroups.AddUserRequest;
  31. import org.sonarqube.ws.client.usergroups.CreateRequest;
  32. import org.sonarqube.ws.client.usergroups.DeleteRequest;
  33. import org.sonarqube.ws.client.usergroups.SearchRequest;
  34. import org.sonarqube.ws.client.users.GroupsRequest;
  35. import static java.util.Arrays.stream;
  36. public class GroupTester {
  37. private static final AtomicInteger ID_GENERATOR = new AtomicInteger();
  38. private final TesterSession session;
  39. GroupTester(TesterSession session) {
  40. this.session = session;
  41. }
  42. @SafeVarargs
  43. public final UserGroups.Group generate(Consumer<CreateRequest>... populators) {
  44. int id = ID_GENERATOR.getAndIncrement();
  45. CreateRequest request = new CreateRequest()
  46. .setName("Group" + id)
  47. .setDescription("Description " + id);
  48. stream(populators).forEach(p -> p.accept(request));
  49. return session.wsClient().userGroups().create(request).getGroup();
  50. }
  51. public List<UserGroups.Group> getGroups(String partialGroupName) {
  52. SearchRequest searchRequest = new SearchRequest();
  53. searchRequest.setQ(partialGroupName);
  54. return session.wsClient().userGroups().search(searchRequest).getGroupsList();
  55. }
  56. public List<Group> getGroupsOfUser(String userLogin) {
  57. GroupsRequest request = new GroupsRequest().setLogin(userLogin);
  58. Users.GroupsWsResponse response = session.users().service().groups(request);
  59. return response.getGroupsList();
  60. }
  61. public GroupTester addMemberToGroups(String userLogin, String... groups) {
  62. for (String group : groups) {
  63. AddUserRequest request = new AddUserRequest()
  64. .setLogin(userLogin)
  65. .setName(group);
  66. session.wsClient().userGroups().addUser(request);
  67. }
  68. return this;
  69. }
  70. public GroupTester assertThatUserIsOnlyMemberOf(String userLogin, String... expectedGroups) {
  71. Set<String> groups = getGroupsOfUser(userLogin).stream()
  72. .map(Group::getName)
  73. .collect(Collectors.toSet());
  74. Assertions.assertThat(groups).containsExactlyInAnyOrder(expectedGroups);
  75. return this;
  76. }
  77. public GroupTester deleteAllGenerated() {
  78. List<String> allGroups = session.wsClient().userGroups().search(new SearchRequest()).getGroupsList().stream().map(UserGroups.Group::getName)
  79. .collect(Collectors.toList());
  80. allGroups.stream()
  81. .filter(g -> g.matches("Group\\d+$"))
  82. .forEach(g -> session.wsClient().userGroups().delete(new DeleteRequest().setName(g)));
  83. return this;
  84. }
  85. public GroupTester delete(UserGroups.Group... groups) {
  86. List<String> allGroups = session.wsClient().userGroups().search(new SearchRequest()).getGroupsList().stream().map(UserGroups.Group::getName)
  87. .collect(Collectors.toList());
  88. stream(groups)
  89. .filter(g -> allGroups.contains(g.getName()))
  90. .forEach(g -> session.wsClient().userGroups().delete(new DeleteRequest().setName(g.getName())));
  91. return this;
  92. }
  93. }