View Javadoc

1   /*
2    * Copyright (C) 2010 Atlassian
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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       * User representation with /latest/ before 6.0 and /2/ since 6.0
50       */
51      public static final BasicUser USER1_60;
52      /**
53       * User representation with /latest/ before 6.0 and /2/ since 6.0
54       */
55      public static final BasicUser USER2_60;
56      /**
57       * User representation with /latest/ before 6.0 and /2/ since 6.0
58       */
59      public static final BasicUser USER_ADMIN_60;
60      /**
61       * User representation with /latest/ before 6.0 and /2/ since 6.0
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                  // the client failed to receive the build number, defaulting to recent cloud version:
94                  // we should not log here as it's a static blog and that could screw log instance access
95                  // Issue: https://jdog.jira-dev.com/browse/JDEV-37921
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             // remove it when https://jdog.atlassian.com/browse/JRADEV-7691 is fixed
104             URI_INTERFIX_FOR_USER = TESTING_JIRA_5_OR_NEWER ? "2" : "latest";
105 
106             // avatar size names (changed in JIRA 6.0)
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             // users
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         // secure/useravatar?size=small&ownerId=admin&avatarId=10054
141         final StringBuilder sb = new StringBuilder("secure/useravatar?");
142 
143         // optional size name
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         // Optional user name
150         if (StringUtils.isNotBlank(userName)) {
151             sb.append("ownerId=").append(userName).append("&");
152         }
153 
154         // avatar Id
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             // resolve would remove "jira" from context path, so we must add / to the end
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 }