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.rest.client.auth.BasicHttpAuthenticationHandler;
20  import com.atlassian.jira.rest.client.domain.BasicUser;
21  import com.atlassian.jira.rest.client.internal.ServerVersionConstants;
22  import com.atlassian.jira.rest.client.internal.jersey.JerseyJiraRestClient;
23  import com.atlassian.jira.webtests.util.LocalTestEnvironmentData;
24  
25  import javax.ws.rs.core.UriBuilder;
26  import java.net.URI;
27  import java.net.URISyntaxException;
28  
29  public class IntegrationTestUtil {
30  	public static final BasicUser USER_ADMIN;
31  	public static final BasicUser USER_ADMIN_LATEST;
32      public static final BasicUser USER1;
33      public static final BasicUser USER2;
34  	public static final BasicUser USER1_LATEST;
35      public static final BasicUser USER2_LATEST;
36  	public static final BasicUser USER_SLASH;
37  	public static final BasicUser USER_SLASH_LATEST;
38  
39  	public static final boolean TESTING_JIRA_5_OR_NEWER;
40  	public static final int START_PROGRESS_TRANSITION_ID = 4;
41  	public static final int STOP_PROGRESS_TRANSITION_ID = 301;
42  	public static final String NUMERIC_CUSTOMFIELD_ID = "customfield_10000";
43  	public static final String NUMERIC_CUSTOMFIELD_TYPE = "com.atlassian.jira.plugin.system.customfieldtypes:float";
44  	public static final String NUMERIC_CUSTOMFIELD_TYPE_V5 = "number";
45  	private static final LocalTestEnvironmentData environmentData = new LocalTestEnvironmentData();
46  	private static final String URI_INTERFIX_FOR_USER;
47  
48  
49  	static {
50          try {
51  			JerseyJiraRestClient client = new JerseyJiraRestClient(environmentData.getBaseUrl().toURI(), new BasicHttpAuthenticationHandler("admin", "admin"));
52  			TESTING_JIRA_5_OR_NEWER = client.getMetadataClient().getServerInfo(new NullProgressMonitor()).getBuildNumber() > ServerVersionConstants.BN_JIRA_5;
53  			// remove it when https://jdog.atlassian.com/browse/JRADEV-7691 is fixed
54  			URI_INTERFIX_FOR_USER = TESTING_JIRA_5_OR_NEWER ? "2" : "latest";
55  
56              USER1 = new BasicUser(getUserUri("wseliga"), "wseliga", "Wojciech Seliga");
57              USER2 = new BasicUser(getUserUri("user"), "user", "My Test User");
58  			USER1_LATEST = new BasicUser(getLatestUserUri("wseliga"), "wseliga", "Wojciech Seliga");
59              USER2_LATEST = new BasicUser(getLatestUserUri("user"), "user", "My Test User");
60  			USER_SLASH = new BasicUser(getUserUri("a/user/with/slash"), "a/user/with/slash", "A User with / in its username");
61  			USER_SLASH_LATEST = new BasicUser(getLatestUserUri("a/user/with/slash"), "a/user/with/slash", "A User with / in its username");
62              USER_ADMIN = new BasicUser(getUserUri("admin"), "admin", "Administrator");
63  			USER_ADMIN_LATEST = new BasicUser(getLatestUserUri("admin"), "admin", "Administrator");
64          } catch (URISyntaxException e) {
65              throw new RuntimeException(e);
66          }
67  
68      }
69  
70  	private static URI getUserUri(String username) throws URISyntaxException {
71  		return UriBuilder.fromUri(environmentData.getBaseUrl().toURI()).path("/rest/api/" +
72  				URI_INTERFIX_FOR_USER + "/user").queryParam("username", username).build();
73  	}
74  
75  	private static URI getLatestUserUri(String username) throws URISyntaxException {
76  		return UriBuilder.fromUri(environmentData.getBaseUrl().toURI()).path("/rest/api/latest/user").queryParam("username", username).build();
77  	}
78  
79      public static URI concat(URI uri, String path) {
80          return UriBuilder.fromUri(uri).path(path).build();
81      }
82  
83  }