PageRenderTime 33ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/jira-project/jira-functional-tests/jira-func-tests/src/main/java/com/atlassian/jira/functest/framework/backdoor/ApplicationRoleControl.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 260 lines | 203 code | 50 blank | 7 comment | 0 complexity | 0095d051d3db1cd1391834c50984198d MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.functest.framework.backdoor;
  2. import com.atlassian.jira.testkit.client.RestApiClient;
  3. import com.atlassian.jira.testkit.client.restclient.Response;
  4. import com.atlassian.jira.webtests.util.JIRAEnvironmentData;
  5. import com.google.common.base.Function;
  6. import com.google.common.collect.ImmutableList;
  7. import com.google.common.collect.Lists;
  8. import com.google.common.collect.Maps;
  9. import com.sun.jersey.api.client.ClientResponse;
  10. import com.sun.jersey.api.client.GenericType;
  11. import com.sun.jersey.api.client.WebResource;
  12. import org.apache.commons.lang3.builder.ToStringBuilder;
  13. import org.codehaus.jackson.annotate.JsonIgnoreProperties;
  14. import org.codehaus.jackson.annotate.JsonProperty;
  15. import javax.ws.rs.core.MediaType;
  16. import java.util.List;
  17. import java.util.Map;
  18. import static com.atlassian.jira.test.util.lic.ApplicationLicenseConstants.CORE_KEY;
  19. import static com.atlassian.jira.test.util.lic.ApplicationLicenseConstants.SOFTWARE_KEY;
  20. import static java.util.Collections.singleton;
  21. public class ApplicationRoleControl extends RestApiClient<ApplicationRoleControl> {
  22. public ApplicationRoleControl(JIRAEnvironmentData environmentData) {
  23. super(environmentData);
  24. }
  25. public List<ApplicationRoleBean> getRoles() {
  26. return createApplicationRoleResource().get(ApplicationRoleBean.LIST);
  27. }
  28. public Map<String, ApplicationRoleBean> getRolesMap() {
  29. return Maps.uniqueIndex(getRoles(), ApplicationRoleBean.GET_KEY);
  30. }
  31. public Response<List<ApplicationRoleBean>> getRolesResponse() {
  32. return toResponse(() ->
  33. {
  34. return createApplicationRoleResource().get(ClientResponse.class);
  35. }, ApplicationRoleBean.LIST);
  36. }
  37. public ApplicationRoleBean getRole(String role) {
  38. return createApplicationRoleResource().path(role).get(ApplicationRoleBean.class);
  39. }
  40. public ApplicationRoleBean getCore() {
  41. return getRole(CORE_KEY);
  42. }
  43. public ApplicationRoleBean getSoftware() {
  44. return getRole(SOFTWARE_KEY);
  45. }
  46. public Response<ApplicationRoleBean> getRoleResponse(final String role) {
  47. return toResponse(() ->
  48. {
  49. return createApplicationRoleResource().path(role).get(ClientResponse.class);
  50. }, ApplicationRoleBean.class);
  51. }
  52. public ApplicationRoleBean putRole(String role, String... groups) {
  53. return createApplicationRoleResource().path(role)
  54. .type(MediaType.APPLICATION_JSON_TYPE)
  55. .put(ApplicationRoleBean.class, new ApplicationRoleBean(groups));
  56. }
  57. public ApplicationRoleBean putRoleWithDefaults(String role, Iterable<String> groups, Iterable<String> defaultGroups) {
  58. return createApplicationRoleResource().path(role)
  59. .type(MediaType.APPLICATION_JSON_TYPE)
  60. .put(ApplicationRoleBean.class, new ApplicationRoleBean(groups)
  61. .setDefaultGroups(ImmutableList.copyOf(defaultGroups)));
  62. }
  63. public ApplicationRoleBean putRoleSelectedByDefault(final String role, final boolean selectedByDefault) {
  64. return createApplicationRoleResource().path(role)
  65. .type(MediaType.APPLICATION_JSON_TYPE)
  66. .put(ApplicationRoleBean.class, new ApplicationRoleBean(selectedByDefault));
  67. }
  68. public ApplicationRoleBean putRoleWithDefaultsSelectedByDefault(final String role, final boolean selectedByDefault,
  69. final Iterable<String> groups, final Iterable<String> defaultGroups) {
  70. return createApplicationRoleResource().path(role)
  71. .type(MediaType.APPLICATION_JSON_TYPE)
  72. .put(ApplicationRoleBean.class, new ApplicationRoleBean(groups)
  73. .setSelectedByDefault(selectedByDefault)
  74. .setDefaultGroups(ImmutableList.copyOf(defaultGroups)));
  75. }
  76. /**
  77. * Sets the application role with the passed group. The passed group will also become one of the defaults.
  78. *
  79. * @param role the role to update.
  80. * @param group the group to add to to role.
  81. * @return the actual data from the server.
  82. */
  83. public ApplicationRoleBean putRoleAndSetDefault(String role, String group) {
  84. return putRoleWithDefaults(role, singleton(group), singleton(group));
  85. }
  86. public Response<ApplicationRoleBean> putRoleResponse(final String role, final String... groups) {
  87. return toResponse(() ->
  88. {
  89. return createApplicationRoleResource().path(role)
  90. .type(MediaType.APPLICATION_JSON_TYPE)
  91. .put(ClientResponse.class, new ApplicationRoleBean(groups));
  92. }, ApplicationRoleBean.class);
  93. }
  94. public Response<ApplicationRoleBean> putSelectedByDefaultResponse(final String role, final boolean selectedByDefault) {
  95. return toResponse(() ->
  96. {
  97. return createApplicationRoleResource().path(role)
  98. .type(MediaType.APPLICATION_JSON_TYPE)
  99. .put(ClientResponse.class, new ApplicationRoleBean(selectedByDefault));
  100. }, ApplicationRoleBean.class);
  101. }
  102. private WebResource createApplicationRoleResource() {
  103. return createResource().path("applicationrole");
  104. }
  105. @JsonIgnoreProperties(ignoreUnknown = true)
  106. public static class ApplicationRoleBean {
  107. private static final GenericType<List<ApplicationRoleBean>> LIST = new GenericType<List<ApplicationRoleBean>>() {
  108. };
  109. private static final Function<ApplicationRoleBean, String> GET_KEY = new Function<ApplicationRoleBean, String>() {
  110. @Override
  111. public String apply(final ApplicationRoleBean input) {
  112. return input.key;
  113. }
  114. };
  115. @JsonProperty
  116. private String name;
  117. @JsonProperty
  118. private String key;
  119. @JsonProperty
  120. private List<String> groups;
  121. @JsonProperty
  122. private List<String> defaultGroups;
  123. @JsonProperty
  124. private boolean selectedByDefault;
  125. @JsonProperty
  126. private Integer numberOfSeats;
  127. @JsonProperty
  128. private Integer remainingSeats;
  129. @JsonProperty
  130. private Integer userCount;
  131. @JsonProperty
  132. private Boolean hasUnlimitedSeats;
  133. public ApplicationRoleBean() {
  134. groups = Lists.newArrayList();
  135. }
  136. private ApplicationRoleBean(String... groups) {
  137. this.groups = ImmutableList.copyOf(groups);
  138. }
  139. private ApplicationRoleBean(Iterable<String> groups) {
  140. this.groups = ImmutableList.copyOf(groups);
  141. }
  142. private ApplicationRoleBean(boolean selectedByDefault) {
  143. this.selectedByDefault = selectedByDefault;
  144. }
  145. @Override
  146. public String toString() {
  147. return new ToStringBuilder(this)
  148. .append("name", name)
  149. .append("id", key)
  150. .append("groups", groups)
  151. .append("selectedByDefault", selectedByDefault)
  152. .append("numberOfSeats", numberOfSeats)
  153. .append("remainingSeats", remainingSeats)
  154. .append("userCount", userCount)
  155. .append("hasUnlimitedSeats", hasUnlimitedSeats)
  156. .toString();
  157. }
  158. public String getName() {
  159. return name;
  160. }
  161. public String getKey() {
  162. return key;
  163. }
  164. public List<String> getGroups() {
  165. return groups;
  166. }
  167. public List<String> getDefaultGroups() {
  168. return defaultGroups;
  169. }
  170. public boolean isSelectedByDefault() {
  171. return selectedByDefault;
  172. }
  173. public Integer getNumberOfSeats() {
  174. return numberOfSeats;
  175. }
  176. public Integer getRemainingSeats() {
  177. return remainingSeats;
  178. }
  179. public Integer getUserCount() {
  180. return userCount;
  181. }
  182. public Boolean getHasUnlimitedSeats() {
  183. return hasUnlimitedSeats;
  184. }
  185. public ApplicationRoleBean setDefaultGroups(List<String> defaultGroups) {
  186. this.defaultGroups = ImmutableList.copyOf(defaultGroups);
  187. return this;
  188. }
  189. public ApplicationRoleBean setSelectedByDefault(final boolean selectedByDefault) {
  190. this.selectedByDefault = selectedByDefault;
  191. return this;
  192. }
  193. public ApplicationRoleBean setNumberOfSeats(final Integer numberOfSeats) {
  194. this.numberOfSeats = numberOfSeats;
  195. return this;
  196. }
  197. public ApplicationRoleBean setRemainingSeats(final Integer remainingSeats) {
  198. this.remainingSeats = remainingSeats;
  199. return this;
  200. }
  201. public ApplicationRoleBean setUserCount(final Integer userCount) {
  202. this.userCount = userCount;
  203. return this;
  204. }
  205. public ApplicationRoleBean setHasUnlimitedSeats(final boolean hasUnlimitedSeats) {
  206. this.hasUnlimitedSeats = hasUnlimitedSeats;
  207. return this;
  208. }
  209. }
  210. }