Implement create-version-from-property action

Change-Id: I7dfbbe15077fea4c3d2a56d8f395e71530dbf794
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraClient.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraClient.java
index 4a77ca3..5fd69ba 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraClient.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraClient.java
@@ -30,6 +30,7 @@
 import com.googlesource.gerrit.plugins.its.jira.restapi.JiraRestApiProvider;
 import com.googlesource.gerrit.plugins.its.jira.restapi.JiraServerInfo;
 import com.googlesource.gerrit.plugins.its.jira.restapi.JiraTransition;
+import com.googlesource.gerrit.plugins.its.jira.restapi.JiraVersion;
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.List;
@@ -101,6 +102,13 @@
     }
   }
 
+  public void createVersion(String projectKey, String version) throws IOException {
+    log.debug("Trying to create version {} on project {}", version, projectKey);
+    JiraVersion jiraVersion = JiraVersion.builder().project(projectKey).name(version).build();
+    apiBuilder.getVersions().doPost("", gson.toJson(jiraVersion), HTTP_CREATED);
+    log.debug("Version {} created on project {}", version, projectKey);
+  }
+
   /**
    * @param issueKey Jira Issue key
    * @param transition JiraTransition.Item to perform
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraItsFacade.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraItsFacade.java
index c14f2e6..4d066d5 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraItsFacade.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraItsFacade.java
@@ -102,6 +102,16 @@
   }
 
   @Override
+  public void createVersion(String projectKey, String version) throws IOException {
+    execute(
+        () -> {
+          log.debug("Creating version {} on project {}", version, projectKey);
+          jiraClient.createVersion(projectKey, version);
+          return projectKey;
+        });
+  }
+
+  @Override
   public boolean exists(String issueKey) throws IOException {
     return execute(() -> jiraClient.issueExists(issueKey));
   }
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 d09441a..7ca8434 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
@@ -45,4 +45,8 @@
   public JiraRestApi<JiraProject[]> getProjects() {
     return get(JiraProject[].class, "/project");
   }
+
+  public JiraRestApi<JiraVersion[]> getVersions() {
+    return get(JiraVersion[].class, "/version");
+  }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraVersion.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraVersion.java
new file mode 100644
index 0000000..60a0b67
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraVersion.java
@@ -0,0 +1,135 @@
+// Copyright (C) 2018 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.its.jira.restapi;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/** Represents a version in JIRA. */
+public class JiraVersion {
+
+  private final String description;
+  private final String name;
+  private final boolean archived;
+  private final boolean released;
+  private final String releaseDate;
+  private final String project;
+  private final Long projectId;
+
+  private JiraVersion(
+      String description,
+      String name,
+      boolean archived,
+      boolean released,
+      Date releaseDate,
+      String project,
+      Long projectId) {
+    this.description = description;
+    this.name = name;
+    this.archived = archived;
+    this.released = released;
+    if (releaseDate == null) {
+      this.releaseDate = null;
+    } else {
+      this.releaseDate = new SimpleDateFormat("yyyy-MM-dd").format(releaseDate);
+    }
+    this.project = project;
+    this.projectId = projectId;
+  }
+
+  public String getDescription() {
+    return description;
+  }
+
+  public String getName() {
+    return name;
+  }
+
+  public boolean isArchived() {
+    return archived;
+  }
+
+  public boolean isReleased() {
+    return released;
+  }
+
+  public String getReleaseDate() {
+    return releaseDate;
+  }
+
+  public String getProject() {
+    return project;
+  }
+
+  public Long getProjectId() {
+    return projectId;
+  }
+
+  public static Builder builder() {
+    return new Builder();
+  }
+
+  public static class Builder {
+    private String description;
+    private String name;
+    private boolean archived;
+    private boolean released;
+    private Date releaseDate;
+    private String project;
+    private Long projectId;
+
+    private Builder() {}
+
+    public Builder description(String description) {
+      this.description = description;
+      return this;
+    }
+
+    public Builder name(String name) {
+      this.name = name;
+      return this;
+    }
+
+    public Builder archived(boolean archived) {
+      this.archived = archived;
+      return this;
+    }
+
+    public Builder released(boolean released) {
+      this.released = released;
+      return this;
+    }
+
+    public Builder releaseDate(Date releaseDate) {
+      this.releaseDate = releaseDate;
+      return this;
+    }
+
+    public Builder project(String project) {
+      this.project = project;
+      return this;
+    }
+
+    public Builder projectId(Long projectId) {
+      this.projectId = projectId;
+      return this;
+    }
+
+    public JiraVersion build() {
+      return new JiraVersion(
+          description, name, archived, released, releaseDate, project, projectId);
+    }
+  }
+}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraItsFacadeTest.java b/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraItsFacadeTest.java
index 6d645c8..b5bae41 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraItsFacadeTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraItsFacadeTest.java
@@ -36,6 +36,7 @@
   private static final String ACTION = "action";
   private static final String COMMENT = "comment";
   private static final String ISSUE_KEY = "issueKey";
+  private static final String PROJECT_KEY = "projectKey";
 
   @Mock private JiraClient jiraClient;
 
@@ -89,6 +90,13 @@
   }
 
   @Test
+  public void createVersion() throws IOException {
+    jiraFacade = new JiraItsFacade(jiraClient);
+    jiraFacade.createVersion(PROJECT_KEY, "1.0");
+    verify(jiraClient).createVersion(PROJECT_KEY, "1.0");
+  }
+
+  @Test
   public void exists() throws IOException {
     jiraFacade = new JiraItsFacade(jiraClient);
     jiraFacade.exists(ISSUE_KEY);