Merge "Add tests to JiraItsFacade"
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraRestApi.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraRestApi.java
index ba18017..9d7589d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraRestApi.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraRestApi.java
@@ -19,6 +19,7 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URL;
@@ -28,13 +29,12 @@
/** Jira Rest Client. */
public class JiraRestApi<T> {
- private static final String BASE_PREFIX = "/rest/api/2";
+ private static final String BASE_PREFIX = "rest/api/2";
private final URL baseUrl;
private final String auth;
private final Gson gson;
private final Class<T> classOfT;
- private final String classPrefix;
private T data;
private int responseCode;
@@ -45,12 +45,12 @@
* @param user username of the jira user
* @param pass password of the jira user
*/
- JiraRestApi(URL url, String user, String pass, Class<T> classOfT, String classPrefix) {
+ JiraRestApi(URL url, String user, String pass, Class<T> classOfT, String classPrefix)
+ throws MalformedURLException {
this.auth = Base64.getEncoder().encodeToString((user + ":" + pass).getBytes());
- this.baseUrl = url;
+ this.baseUrl = new URL(url, BASE_PREFIX + classPrefix);
this.gson = new Gson();
this.classOfT = classOfT;
- this.classPrefix = classPrefix;
}
public int getResponseCode() {
@@ -80,6 +80,10 @@
return doGet(spec, passCode, null);
}
+ URL getBaseUrl() {
+ return baseUrl;
+ }
+
/** Do a simple POST request. */
public boolean doPost(String spec, String jsonInput, int passCode) throws IOException {
HttpURLConnection conn = prepHttpConnection(spec, true);
@@ -93,7 +97,7 @@
private HttpURLConnection prepHttpConnection(String spec, boolean isPostRequest)
throws IOException {
- URL url = new URL(baseUrl, BASE_PREFIX + classPrefix + spec);
+ URL url = new URL(baseUrl, spec);
ProxySelector proxySelector = ProxySelector.getDefault();
Proxy proxy = HttpSupport.proxyFor(proxySelector, url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraRestApiProvider.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraRestApiProvider.java
index 3bcf230..3c4da25 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraRestApiProvider.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraRestApiProvider.java
@@ -9,24 +9,25 @@
private final String pass;
public JiraRestApiProvider(String url, String user, String pass) throws MalformedURLException {
- this.url = new URL(url);
+ this.url = new URL(url + (url.endsWith("/") ? "" : "/"));
this.user = user;
this.pass = pass;
}
- public <T> JiraRestApi<T> get(Class<T> classOfT, String classPrefix) {
+ public <T> JiraRestApi<T> get(Class<T> classOfT, String classPrefix)
+ throws MalformedURLException {
return new JiraRestApi<>(url, user, pass, classOfT, classPrefix);
}
- public JiraRestApi<JiraIssue> getIssue() {
+ public JiraRestApi<JiraIssue> getIssue() throws MalformedURLException {
return get(JiraIssue.class, "/issue");
}
- public JiraRestApi<JiraServerInfo> getServerInfo() {
+ public JiraRestApi<JiraServerInfo> getServerInfo() throws MalformedURLException {
return get(JiraServerInfo.class, "/serverInfo");
}
- public JiraRestApi<JiraProject[]> getProjects() {
+ public JiraRestApi<JiraProject[]> getProjects() throws MalformedURLException {
return get(JiraProject[].class, "/project");
}
}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraRestApiTest.java b/src/test/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraRestApiTest.java
new file mode 100644
index 0000000..77fb4bf
--- /dev/null
+++ b/src/test/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraRestApiTest.java
@@ -0,0 +1,38 @@
+package com.googlesource.gerrit.plugins.its.jira.restapi;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import org.junit.Test;
+
+public class JiraRestApiTest {
+
+ @Test
+ public void testJiraServerInfoForNonRootJiraUrl() throws Exception {
+ String nonRootJiraUrl = "http://jira.mycompany.com/myroot/";
+ JiraRestApi<JiraServerInfo> serverInfo =
+ new JiraRestApiProvider(nonRootJiraUrl, "", "").getServerInfo();
+
+ String jiraApiUrl = serverInfo.getBaseUrl().toString();
+ assertThat(jiraApiUrl).startsWith(nonRootJiraUrl);
+ }
+
+ @Test
+ public void testJiraServerInfoForNonRootJiraUrlNotEndingWithSlash() throws Exception {
+ String nonRootJiraUrl = "http://jira.mycompany.com/myroot";
+ JiraRestApi<JiraServerInfo> serverInfo =
+ new JiraRestApiProvider(nonRootJiraUrl, "", "").getServerInfo();
+
+ String jiraApiUrl = serverInfo.getBaseUrl().toString();
+ assertThat(jiraApiUrl).startsWith(nonRootJiraUrl);
+ }
+
+ @Test
+ public void testJiraServerInfoForRootJiraUrl() throws Exception {
+ String rootJiraUrl = "http://jira.mycompany.com";
+ JiraRestApi<JiraServerInfo> serverInfo =
+ new JiraRestApiProvider(rootJiraUrl, "", "").getServerInfo();
+
+ String jiraApiUrl = serverInfo.getBaseUrl().toString();
+ assertThat(jiraApiUrl).startsWith(rootJiraUrl);
+ }
+}