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