1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.atlassian.jira.rest.client;
18
19 import com.atlassian.jira.functest.framework.Administration;
20 import com.atlassian.jira.rest.client.api.RestClientException;
21 import com.atlassian.jira.rest.client.api.domain.BasicUser;
22 import com.atlassian.jira.rest.client.api.domain.User;
23 import com.atlassian.jira.rest.client.auth.BasicHttpAuthenticationHandler;
24 import com.atlassian.jira.rest.client.internal.ServerVersionConstants;
25 import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;
26 import com.atlassian.jira.testkit.client.Backdoor;
27 import com.atlassian.jira.webtests.util.LocalTestEnvironmentData;
28 import com.google.common.collect.ImmutableMap;
29 import org.apache.commons.lang.StringUtils;
30
31 import javax.annotation.Nullable;
32 import javax.ws.rs.core.UriBuilder;
33 import java.net.URI;
34 import java.net.URISyntaxException;
35 import java.util.Map;
36
37 public class IntegrationTestUtil {
38 public static final User USER_ADMIN_FULL;
39 public static final BasicUser USER_ADMIN;
40 public static final BasicUser USER_ADMIN_LATEST;
41 public static final User USER1_FULL;
42 public static final BasicUser USER1;
43 public static final BasicUser USER2;
44 public static final BasicUser USER1_LATEST;
45 public static final BasicUser USER2_LATEST;
46 public static final BasicUser USER_SLASH;
47 public static final BasicUser USER_SLASH_LATEST;
48
49
50
51 public static final BasicUser USER1_60;
52
53
54
55 public static final BasicUser USER2_60;
56
57
58
59 public static final BasicUser USER_ADMIN_60;
60
61
62
63 public static final BasicUser USER_SLASH_60;
64
65 public static final String ROLE_ADMINISTRATORS = "Administrators";
66
67 public static final boolean TESTING_JIRA_5_OR_NEWER;
68 public static final boolean TESTING_JIRA_6_OR_NEWER;
69 public static final int START_PROGRESS_TRANSITION_ID = 4;
70 public static final int STOP_PROGRESS_TRANSITION_ID = 301;
71 public static final String NUMERIC_CUSTOMFIELD_ID = "customfield_10000";
72 public static final String NUMERIC_CUSTOMFIELD_TYPE = "com.atlassian.jira.plugin.system.customfieldtypes:float";
73 public static final String NUMERIC_CUSTOMFIELD_TYPE_V5 = "number";
74 public static final String TEXT_CUSTOMFIELD_ID = "customfield_10011";
75 private static final LocalTestEnvironmentData environmentData = new LocalTestEnvironmentData();
76 private static final String URI_INTERFIX_FOR_USER;
77 private static final Backdoor backdoor = new Backdoor(environmentData);
78
79 public static final String GROUP_JIRA_ADMINISTRATORS = "jira-administrators";
80 public static final int CURRENT_BUILD_NUMBER;
81 public static final ImmutableMap<String, String> AVATAR_SIZE_TO_NAME_MAP;
82
83 static {
84 try {
85 final com.atlassian.jira.rest.client.api.JiraRestClientFactory clientFactory = new AsynchronousJiraRestClientFactory();
86 final com.atlassian.jira.rest.client.api.JiraRestClient client = clientFactory.create(environmentData.getBaseUrl()
87 .toURI(), new BasicHttpAuthenticationHandler("admin", "admin"));
88
89 int determinedBuildNumber;
90 try {
91 determinedBuildNumber = client.getMetadataClient().getServerInfo().claim().getBuildNumber();
92 } catch (RestClientException e) {
93
94
95
96 determinedBuildNumber = 100029;
97 }
98
99 CURRENT_BUILD_NUMBER = determinedBuildNumber;
100
101 TESTING_JIRA_5_OR_NEWER = CURRENT_BUILD_NUMBER > ServerVersionConstants.BN_JIRA_5;
102 TESTING_JIRA_6_OR_NEWER = CURRENT_BUILD_NUMBER > ServerVersionConstants.BN_JIRA_6;
103
104 URI_INTERFIX_FOR_USER = TESTING_JIRA_5_OR_NEWER ? "2" : "latest";
105
106
107 if (CURRENT_BUILD_NUMBER >= 6060) {
108 AVATAR_SIZE_TO_NAME_MAP = ImmutableMap.<String, String>builder().put("16x16", "xsmall")
109 .put("24x24", "small").put("32x32", "medium").put("48x48", "").build();
110 } else {
111 AVATAR_SIZE_TO_NAME_MAP = ImmutableMap.of("16x16", "small", "48x48", "");
112 }
113
114
115 USER1_FULL = new User(getUserUri("wseliga"), "wseliga", "Wojciech Seliga", "wojciech.seliga@spartez.com", null, buildUserAvatarUris(null, 10082L), null);
116 USER1 = new BasicUser(USER1_FULL.getSelf(), USER1_FULL.getName(), USER1_FULL.getDisplayName());
117 USER1_LATEST = new BasicUser(getLatestUserUri("wseliga"), "wseliga", "Wojciech Seliga");
118 USER1_60 = TESTING_JIRA_6_OR_NEWER ? USER1 : USER1_LATEST;
119
120 USER2 = new BasicUser(getUserUri("user"), "user", "My Test User");
121 USER2_LATEST = new BasicUser(getLatestUserUri("user"), "user", "My Test User");
122 USER2_60 = TESTING_JIRA_6_OR_NEWER ? USER2 : USER2_LATEST;
123
124 USER_SLASH = new BasicUser(getUserUri("a/user/with/slash"), "a/user/with/slash", "A User with / in its username");
125 USER_SLASH_LATEST = new BasicUser(getLatestUserUri("a/user/with/slash"), "a/user/with/slash", "A User with / in its username");
126 USER_SLASH_60 = TESTING_JIRA_6_OR_NEWER ? USER_SLASH : USER_SLASH_LATEST;
127
128
129 USER_ADMIN_FULL = new User(getUserUri("admin"), "admin", "Administrator", "wojciech.seliga@spartez.com", null, buildUserAvatarUris("admin", 10054L), null);
130 USER_ADMIN = new BasicUser(USER_ADMIN_FULL.getSelf(), USER_ADMIN_FULL.getName(), USER_ADMIN_FULL.getDisplayName());
131 USER_ADMIN_LATEST = new BasicUser(getLatestUserUri("admin"), "admin", "Administrator");
132 USER_ADMIN_60 = TESTING_JIRA_6_OR_NEWER ? USER_ADMIN : USER_ADMIN_LATEST;
133 } catch (URISyntaxException e) {
134 throw new RuntimeException(e);
135 }
136
137 }
138
139 public static URI buildUserAvatarUri(@Nullable String userName, Long avatarId, String size) {
140
141 final StringBuilder sb = new StringBuilder("secure/useravatar?");
142
143
144 final String sizeName = AVATAR_SIZE_TO_NAME_MAP.get(size);
145 if (StringUtils.isNotBlank(sizeName)) {
146 sb.append("size=").append(sizeName).append("&");
147 }
148
149
150 if (StringUtils.isNotBlank(userName)) {
151 sb.append("ownerId=").append(userName).append("&");
152 }
153
154
155 sb.append("avatarId=").append(avatarId);
156 return resolveURI(sb.toString());
157 }
158
159 public static Map<String, URI> buildUserAvatarUris(@Nullable String user, Long avatarId) {
160 final ImmutableMap.Builder<String, URI> builder = ImmutableMap.builder();
161 for (String size : AVATAR_SIZE_TO_NAME_MAP.keySet()) {
162 builder.put(size, buildUserAvatarUri(user, avatarId, size));
163 }
164 return builder.build();
165 }
166
167 public static URI getUserUri(String username) {
168 try {
169 return UriBuilder.fromUri(environmentData.getBaseUrl().toURI()).path("/rest/api/" +
170 URI_INTERFIX_FOR_USER + "/user").queryParam("username", username).build();
171 } catch (URISyntaxException e) {
172 throw new RuntimeException(e);
173 }
174 }
175
176 private static URI getLatestUserUri(String username) throws URISyntaxException {
177 return UriBuilder.fromUri(environmentData.getBaseUrl().toURI()).path("/rest/api/latest/user")
178 .queryParam("username", username).build();
179 }
180
181 public static URI concat(URI uri, String path) {
182 return UriBuilder.fromUri(uri).path(path).build();
183 }
184
185 public static URI resolveURI(URI relativeUri) {
186 try {
187
188 return concat(environmentData.getBaseUrl().toURI(), "/").resolve(relativeUri);
189 } catch (URISyntaxException e) {
190 throw new RuntimeException(e);
191 }
192 }
193
194 public static URI resolveURI(String relativeUri) {
195 return resolveURI(TestUtil.toUri(relativeUri));
196 }
197
198 public static void restoreAppropriateJiraData(String filename, Administration administration) {
199 String pathtoFile = administration.getBuildNumber() >= 70107 ? "cloud/" + filename : filename;
200 backdoor.restoreDataFromResource(pathtoFile);
201 }
202 }