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 it;
18  
19  import com.atlassian.jira.nimblefunctests.framework.NimbleFuncTestCase;
20  import com.atlassian.jira.rest.client.IntegrationTestUtil;
21  import com.atlassian.jira.rest.client.NullProgressMonitor;
22  import com.atlassian.jira.rest.client.auth.AnonymousAuthenticationHandler;
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.jersey.JerseyJiraRestClient;
26  
27  import javax.ws.rs.core.UriBuilder;
28  import java.net.URI;
29  import java.net.URISyntaxException;
30  
31  import static com.atlassian.jira.rest.client.internal.json.TestConstants.*;
32  
33  public abstract class AbstractJerseyRestClientTest extends NimbleFuncTestCase {
34  
35  	protected URI jiraUri;
36  	protected JerseyJiraRestClient client;
37  	protected URI jiraRestRootUri;
38  	protected URI jiraAuthRootUri;
39  	protected final NullProgressMonitor pm = new NullProgressMonitor();
40  
41  	@Override
42  	public void beforeMethod() {
43  		super.beforeMethod();
44  
45  		initUriFields();
46  		setAdmin();
47  	}
48  
49  	private void initUriFields() {
50  		try {
51  			jiraUri = UriBuilder.fromUri(environmentData.getBaseUrl().toURI()).build();
52  		} catch (URISyntaxException e) {
53  			throw new RuntimeException(e);
54  		}
55  		jiraRestRootUri = UriBuilder.fromUri(jiraUri).path(
56  				IntegrationTestUtil.TESTING_JIRA_5_OR_NEWER ? "/rest/api/2/" : "/rest/api/latest/").build();
57  		jiraAuthRootUri = UriBuilder.fromUri(jiraUri).path("/rest/auth/latest/").build();
58  	}
59  
60  	protected void setAdmin() {
61  		setClient(ADMIN_USERNAME, ADMIN_PASSWORD);
62  	}
63  
64  	protected void setClient(String username, String password) {
65  		client = new JerseyJiraRestClient(jiraUri, new BasicHttpAuthenticationHandler(username, password));
66  	}
67  
68  	protected void setAnonymousMode() {
69  		client = new JerseyJiraRestClient(jiraUri, new AnonymousAuthenticationHandler());
70  	}
71  
72  	protected void setUser2() {
73  		setClient(USER2_USERNAME, USER2_PASSWORD);
74  	}
75  
76  	protected void setUser1() {
77  		setClient(USER1_USERNAME, USER1_PASSWORD);
78  	}
79  
80  	protected boolean isJira4x4OrNewer() {
81  		return client.getMetadataClient().getServerInfo(pm).getBuildNumber() >= ServerVersionConstants.BN_JIRA_4_4;
82  	}
83  
84  	protected boolean isJira5xOrNewer() {
85  		return client.getMetadataClient().getServerInfo(pm).getBuildNumber() >= ServerVersionConstants.BN_JIRA_5;
86  	}
87  
88  	protected boolean isJira4x3OrNewer() {
89  		return client.getMetadataClient().getServerInfo(pm).getBuildNumber() >= ServerVersionConstants.BN_JIRA_4_3;
90  	}
91  
92  }