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.functest.framework.FuncTestCase;
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.domain.Transition;
25  import com.atlassian.jira.rest.client.internal.ServerVersionConstants;
26  import com.atlassian.jira.rest.client.internal.jersey.JerseyJiraRestClient;
27  
28  import javax.annotation.Nullable;
29  import javax.ws.rs.core.UriBuilder;
30  import java.net.URI;
31  import java.net.URISyntaxException;
32  
33  import static com.atlassian.jira.rest.client.internal.json.TestConstants.*;
34  
35  public abstract class AbstractJerseyRestClientTest extends FuncTestCase {
36      protected URI jiraUri;
37      protected JerseyJiraRestClient client;
38      protected URI jiraRestRootUri;
39      protected URI jiraAuthRootUri;
40  	protected static final String ADMIN_USERNAME = "admin";
41  	protected static final String ADMIN_PASSWORD = "admin";
42  	protected final NullProgressMonitor pm = new NullProgressMonitor();
43  	protected static final String DEFAULT_JIRA_DUMP_FILE = "jira1-export.xml";
44  
45  	public AbstractJerseyRestClientTest() {
46      }
47  
48      public void configureJira() {
49          administration.restoreData(DEFAULT_JIRA_DUMP_FILE);
50      }
51  
52      @Override
53      protected void setUpTest() {
54          try {
55              jiraUri = UriBuilder.fromUri(environmentData.getBaseUrl().toURI())/*.path(environmentData.getContext())*/.build();
56          } catch (URISyntaxException e) {
57              throw new RuntimeException(e);
58          }
59          jiraRestRootUri = UriBuilder.fromUri(jiraUri).path(
60  				IntegrationTestUtil.TESTING_JIRA_5_OR_NEWER ? "/rest/api/2/" : "/rest/api/latest/").build();
61          jiraAuthRootUri = UriBuilder.fromUri(jiraUri).path("/rest/auth/latest/").build();
62  		setAdmin();
63  	}
64  
65  	protected void setAdmin() {
66  		setClient(ADMIN_USERNAME, ADMIN_PASSWORD);
67  	}
68  
69  	protected void setClient(String username, String password) {
70  		client = new JerseyJiraRestClient(jiraUri, new BasicHttpAuthenticationHandler(username, password));
71  	}
72  
73  	protected void setAnonymousMode() {
74  		client = new JerseyJiraRestClient(jiraUri, new AnonymousAuthenticationHandler());
75  	}
76  
77  	protected void setUser2() {
78  		setClient(USER2_USERNAME, USER2_PASSWORD);
79  	}
80  
81  	protected void setUser1() {
82  		setClient(USER1_USERNAME, USER1_PASSWORD);
83  	}
84  
85  
86  	@Nullable
87  	protected Transition getTransitionByName(Iterable<Transition> transitions, String transitionName) {
88  		Transition transitionFound = null;
89  		for (Transition transition : transitions) {
90  			if (transition.getName().equals(transitionName)) {
91  				transitionFound = transition;
92  				break;
93  			}
94  		}
95  		return transitionFound;
96  	}
97  
98  
99  	protected boolean isJira4x4OrNewer() {
100 		return client.getMetadataClient().getServerInfo(pm).getBuildNumber() >= 646;
101 	}
102 
103 	protected boolean isJira5xOrNewer() {
104 		return client.getMetadataClient().getServerInfo(pm).getBuildNumber() >= ServerVersionConstants.BN_JIRA_5;
105 	}
106 
107 }