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.jersey.JerseyJiraRestClient;
25  
26  import javax.annotation.Nullable;
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 FuncTestCase {
34      protected URI jiraUri;
35      protected JerseyJiraRestClient client;
36      protected URI jiraRestRootUri;
37      protected URI jiraAuthRootUri;
38  	protected static final String ADMIN_USERNAME = "admin";
39  	protected static final String ADMIN_PASSWORD = "admin";
40  	protected final NullProgressMonitor pm = new NullProgressMonitor();
41  	protected static final String DEFAULT_JIRA_DUMP_FILE = "jira1-export.xml";
42  
43  	public AbstractJerseyRestClientTest() {
44      }
45  
46      public void configureJira() {
47          administration.restoreData(DEFAULT_JIRA_DUMP_FILE);
48      }
49  
50      @Override
51      protected void setUpTest() {
52          try {
53              jiraUri = UriBuilder.fromUri(environmentData.getBaseUrl().toURI())/*.path(environmentData.getContext())*/.build();
54          } catch (URISyntaxException e) {
55              throw new RuntimeException(e);
56          }
57          jiraRestRootUri = UriBuilder.fromUri(jiraUri).path("/rest/api/latest/").build();
58          jiraAuthRootUri = UriBuilder.fromUri(jiraUri).path("/rest/auth/latest/").build();
59          setClient(ADMIN_USERNAME, ADMIN_PASSWORD);
60      }
61  
62  	protected void setClient(String username, String password) {
63  		client = new JerseyJiraRestClient(jiraUri, new BasicHttpAuthenticationHandler(username, password));
64  	}
65  
66  	protected void setAnonymousMode() {
67  		client = new JerseyJiraRestClient(jiraUri, new AnonymousAuthenticationHandler());
68  	}
69  
70  	protected void setUser2() {
71  		setClient(USER2_USERNAME, USER2_PASSWORD);
72  	}
73  
74  	protected void setUser1() {
75  		setClient(USER1_USERNAME, USER1_PASSWORD);
76  	}
77  
78  
79  	@Nullable
80  	protected Transition getTransitionByName(Iterable<Transition> transitions, String transitionName) {
81  		Transition transitionFound = null;
82  		for (Transition transition : transitions) {
83  			if (transition.getName().equals(transitionName)) {
84  				transitionFound = transition;
85  				break;
86  			}
87  		}
88  		return transitionFound;
89  	}
90  
91  
92  	
93  }