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 String ROLE_ADMINISTRATORS = "Administrators";
40  
41  	public static final boolean TESTING_JIRA_5_OR_NEWER;
42  	public static final int START_PROGRESS_TRANSITION_ID = 4;
43  	public static final int STOP_PROGRESS_TRANSITION_ID = 301;
44  	public static final String NUMERIC_CUSTOMFIELD_ID = "customfield_10000";
45  	public static final String NUMERIC_CUSTOMFIELD_TYPE = "com.atlassian.jira.plugin.system.customfieldtypes:float";
46  	public static final String NUMERIC_CUSTOMFIELD_TYPE_V5 = "number";
47  	private static final LocalTestEnvironmentData environmentData = new LocalTestEnvironmentData();
48  	private static final String URI_INTERFIX_FOR_USER;
49  
50  	public static final String GROUP_JIRA_ADMINISTRATORS = "jira-administrators";
51  
52  	static {
53          try {
54  			JerseyJiraRestClient client = new JerseyJiraRestClient(environmentData.getBaseUrl().toURI(), new BasicHttpAuthenticationHandler("admin", "admin"));
55  			TESTING_JIRA_5_OR_NEWER = client.getMetadataClient().getServerInfo(new NullProgressMonitor()).getBuildNumber() > ServerVersionConstants.BN_JIRA_5;
56  			// remove it when https://jdog.atlassian.com/browse/JRADEV-7691 is fixed
57  			URI_INTERFIX_FOR_USER = TESTING_JIRA_5_OR_NEWER ? "2" : "latest";
58  
59              USER1 = new BasicUser(getUserUri("wseliga"), "wseliga", "Wojciech Seliga");
60              USER2 = new BasicUser(getUserUri("user"), "user", "My Test User");
61  			USER1_LATEST = new BasicUser(getLatestUserUri("wseliga"), "wseliga", "Wojciech Seliga");
62              USER2_LATEST = new BasicUser(getLatestUserUri("user"), "user", "My Test User");
63  			USER_SLASH = new BasicUser(getUserUri("a/user/with/slash"), "a/user/with/slash", "A User with / in its username");
64  			USER_SLASH_LATEST = new BasicUser(getLatestUserUri("a/user/with/slash"), "a/user/with/slash", "A User with / in its username");
65              USER_ADMIN = new BasicUser(getUserUri("admin"), "admin", "Administrator");
66  			USER_ADMIN_LATEST = new BasicUser(getLatestUserUri("admin"), "admin", "Administrator");
67          } catch (URISyntaxException e) {
68              throw new RuntimeException(e);
69          }
70  
71      }
72  
73  	private static URI getUserUri(String username) throws URISyntaxException {
74  		return UriBuilder.fromUri(environmentData.getBaseUrl().toURI()).path("/rest/api/" +
75  				URI_INTERFIX_FOR_USER + "/user").queryParam("username", username).build();
76  	}
77  
78  	private static URI getLatestUserUri(String username) throws URISyntaxException {
79  		return UriBuilder.fromUri(environmentData.getBaseUrl().toURI()).path("/rest/api/latest/user").queryParam("username", username).build();
80  	}
81  
82      public static URI concat(URI uri, String path) {
83          return UriBuilder.fromUri(uri).path(path).build();
84      }
85  
86  	public static URI resolveURI(URI relativeUri) {
87  		try {
88  			// resolve would remove "jira" from context path, so we must add / to the end
89  			return concat(environmentData.getBaseUrl().toURI(), "/").resolve(relativeUri);
90  		} catch (URISyntaxException e) {
91  			throw new RuntimeException(e);
92  		}
93  	}
94  
95  	public static URI resolveURI(String relativeUri) {
96  		return resolveURI(TestUtil.toUri(relativeUri));
97  	}
98  
99  }