Merge "Doc update: Add an example on how to setup on mysql"
diff --git a/src/main/java/com/googlesource/gerrit/plugins/verifystatus/init/InitPlugin.java b/src/main/java/com/googlesource/gerrit/plugins/verifystatus/init/InitPlugin.java
index 8aae733..9efb68f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/verifystatus/init/InitPlugin.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/verifystatus/init/InitPlugin.java
@@ -21,8 +21,6 @@
 import com.google.common.collect.Sets;
 import com.google.gerrit.extensions.annotations.PluginName;
 import com.google.gerrit.lifecycle.LifecycleModule;
-import com.google.gerrit.metrics.DisabledMetricMaker;
-import com.google.gerrit.metrics.MetricMaker;
 import com.google.gerrit.pgm.init.api.ConsoleUI;
 import com.google.gerrit.pgm.init.api.InitStep;
 import com.google.gerrit.pgm.init.api.Section;
@@ -177,7 +175,6 @@
       @Override
       protected void configure() {
         bind(SchemaVersion.class).to(SchemaVersion.C);
-        bind(MetricMaker.class).to(DisabledMetricMaker.class);
       }
     });
 
diff --git a/src/main/java/com/googlesource/gerrit/plugins/verifystatus/server/GetVerifications.java b/src/main/java/com/googlesource/gerrit/plugins/verifystatus/server/GetVerifications.java
index 6e1e59c..6745b66 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/verifystatus/server/GetVerifications.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/verifystatus/server/GetVerifications.java
@@ -130,6 +130,9 @@
           String prevName = "";
           for (PatchSetVerification v : result) {
             String reporter = v.getReporter();
+            if (reporter == null) {
+              reporter = "(unknown)";
+            }
             String jobName = v.getName();
             if (!reporter.equals(prevReporter)) {
               jobs.add(v);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/verifystatus/server/schema/CiDataSourceModule.java b/src/main/java/com/googlesource/gerrit/plugins/verifystatus/server/schema/CiDataSourceModule.java
index 3bff6e3..bbd4ce5 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/verifystatus/server/schema/CiDataSourceModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/verifystatus/server/schema/CiDataSourceModule.java
@@ -23,8 +23,8 @@
   protected void configure() {
     bind(CiDataSourceType.class).annotatedWith(Names.named("h2")).to(H2.class);
     bind(CiDataSourceType.class).annotatedWith(Names.named("derby")).to(Derby.class);
-//    bind(CiDataSourceType.class).annotatedWith(Names.named("mysql")).to(MySql.class);
-//    bind(CiDataSourceType.class).annotatedWith(Names.named("oracle")).to(Oracle.class);
-//    bind(CiDataSourceType.class).annotatedWith(Names.named("postgresql")).to(PostgreSQL.class);
+    bind(CiDataSourceType.class).annotatedWith(Names.named("mysql")).to(MySql.class);
+    bind(CiDataSourceType.class).annotatedWith(Names.named("oracle")).to(Oracle.class);
+    bind(CiDataSourceType.class).annotatedWith(Names.named("postgresql")).to(PostgreSQL.class);
   }
 }
diff --git a/src/main/resources/Documentation/database.md b/src/main/resources/Documentation/database.md
index 852e54f..f45ae2d 100644
--- a/src/main/resources/Documentation/database.md
+++ b/src/main/resources/Documentation/database.md
@@ -61,16 +61,17 @@
 
 ```
 [plugin "@PLUGIN@"]
-  dbType = MYSQL
-  dbUrl = jdbc:mysql://localhost:3306/cidata
-  username = gerrit2
-  password = s3kr3t
+    dbType = mysql
+    hostname = localhost
+    database = cidata
+    username = gerrit2
+    password = secret
 ```
 
 ### <a id="supported-dbs"> @PLUGIN@ supported databases
  * H2
  * Apache Derby
- * MySQL
+ * [MySQL](mysql-setup-example.md)
  * Oracle
  * PostgreSQL
 
@@ -121,6 +122,7 @@
 SEE ALSO
 --------
 
+* [Example Mysql Setup](mysql-setup-example.md)
 * [Database Setup](../../../Documentation/database-setup.html)
 * [Automatic Site Initialization](../../../Documentation/config-auto-site-initialization.html)
 * [Database Settings](../../../Documentation/config-gerrit.html#database)
diff --git a/src/main/resources/Documentation/mysql-setup-example.md b/src/main/resources/Documentation/mysql-setup-example.md
new file mode 100644
index 0000000..cf6301f
--- /dev/null
+++ b/src/main/resources/Documentation/mysql-setup-example.md
@@ -0,0 +1,98 @@
+# MySql example
+
+This is an example of how to install a fresh Gerrit with verify-status plugin.
+
+Create initial site (without verify-status)
+-------------------------------------------
+
+```
+  java -jar gerrit-stable-2.13.war init --batch --no-auto-start --install-all-plugins -d mysite
+```
+
+  *NOTE - This will create an H2 db for gerrit.
+
+
+Install verify status plugin
+----------------------------
+
+Copy verify-status.jar to mysite/plugins folder
+
+
+Setup MySql DB
+--------------
+
+```
+CREATE USER 'gerrit2'@'localhost' IDENTIFIED BY 'secret';
+create database reviewdb;
+GRANT ALL ON reviewdb.* TO 'gerrit2'@'localhost';
+create database cidata;
+GRANT ALL ON cidata.* TO 'gerrit2'@'localhost';
+FLUSH PRIVILEGES;
+```
+
+Update gerrit config
+--------------------
+
+Add the following to etc/gerrit.config file
+
+```
+[database]
+  type = mysql
+  database = reviewdb
+  username = gerrit2
+  password = secret
+  hostname = localhost
+
+[plugin "verify-status"]
+  dbType = mysql
+  database = cidata
+  username = gerrit2
+  password = secret
+  hostname = localhost
+```
+
+Delete git repos
+----------------
+
+*NOTE* - only do this if you've change reviewdb from H2 to mysql otherwise you may get the following error
+
+```
+ Exception in thread "main" com.google.gwtorm.server.OrmException: Cannot initialize schema
+	at com.google.gerrit.server.schema.SchemaUpdater.update(SchemaUpdater.java:104)
+	at com.google.gerrit.pgm.init.BaseInit$SiteRun.upgradeSchema(BaseInit.java:367)
+	at com.google.gerrit.pgm.init.BaseInit.run(BaseInit.java:133)
+	at com.google.gerrit.pgm.util.AbstractProgram.main(AbstractProgram.java:64)
+	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
+	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
+	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
+	at java.lang.reflect.Method.invoke(Method.java:606)
+	at com.google.gerrit.launcher.GerritLauncher.invokeProgram(GerritLauncher.java:163)
+	at com.google.gerrit.launcher.GerritLauncher.mainImpl(GerritLauncher.java:104)
+	at com.google.gerrit.launcher.GerritLauncher.main(GerritLauncher.java:59)
+	at Main.main(Main.java:25)
+ Caused by: java.io.IOException: Cannot update refs/meta/config in /Users/zaro0508/work-gerrit/gerrit-213/mysite/git/All-Projects.git: LOCK_FAILURE
+	at com.google.gerrit.server.git.VersionedMetaData$1.updateRef(VersionedMetaData.java:436)
+	at com.google.gerrit.server.git.VersionedMetaData$1.createRef(VersionedMetaData.java:335)
+	at com.google.gerrit.server.git.VersionedMetaData.commitToNewRef(VersionedMetaData.java:217)
+	at com.google.gerrit.server.schema.AllProjectsCreator.initAllProjects(AllProjectsCreator.java:180)
+	at com.google.gerrit.server.schema.AllProjectsCreator.create(AllProjectsCreator.java:100)
+	at com.google.gerrit.server.schema.SchemaCreator.create(SchemaCreator.java:86)
+	at com.google.gerrit.server.schema.SchemaUpdater.update(SchemaUpdater.java:102)
+	... 11 more
+```
+
+The 1st run of init syncs the git repos to H2 DB.  Now we want to setup git repos with mysql reviewdb.  We are setting
+up a new reviewdb this time so we need to sync with new git repos. Delete and next step will create new repos.
+
+```
+ rm -rf mysite/git/*
+```
+
+Setup Db tables for verify status
+----------------------------------
+
+Rerun init to setup tables for reviewdb and cidata
+
+```
+ java -jar gerrit-stable-2.13.war init --batch --no-auto-start --install-all-plugins -d mysite
+```
diff --git a/src/test/java/com/googlesource/gerrit/plugins/verifystatus/VerifyStatusIT.java b/src/test/java/com/googlesource/gerrit/plugins/verifystatus/VerifyStatusIT.java
index 71a9134..26a5f3d 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/verifystatus/VerifyStatusIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/verifystatus/VerifyStatusIT.java
@@ -96,7 +96,7 @@
   @UseLocalDisk
   public void noVerificationTest() throws Exception {
     Result c = createChange();
-    Map<String, VerificationInfo> infos = getVerifications(c);
+    Map<String, VerificationInfo> infos = getVerifications(c, null);
     assertThat(infos).hasSize(0);
   }
 
@@ -123,7 +123,7 @@
     RestResponse r = adminRestSession.post(endPoint, in);
     r.assertNoContent();
 
-    Map<String, VerificationInfo> infos = getVerifications(c);
+    Map<String, VerificationInfo> infos = getVerifications(c, null);
     assertThat(infos).hasSize(1);
     assertVerification(Iterables.getOnlyElement(infos.values()), i);
   }
@@ -161,13 +161,41 @@
     RestResponse r = adminRestSession.post(endPoint, in);
     r.assertNoContent();
 
-    Map<String, VerificationInfo> infos = getVerifications(c);
+    Map<String, VerificationInfo> infos = getVerifications(c, null);
     assertThat(infos).hasSize(2);
   }
 
-  private Map<String, VerificationInfo> getVerifications(Result c)
+  @Test
+  public void verificationTestNullReporter() throws Exception {
+    VerifyInput in = new VerifyInput();
+    in.verifications = new HashMap<>();
+    VerificationInfo i = new VerificationInfo();
+    i.name = "job42";
+    i.value = 1;
+    i.rerun = true;
+    i.comment = "Test CI";
+    i.url = "url";
+    i.category = "bar";
+    i.duration = "1h 30min";
+    in.verifications.put("foo", i);
+
+    Result c = createChange();
+    String endPoint = url(c);
+    RestResponse r = adminRestSession.post(endPoint, in);
+    r.assertNoContent();
+
+    Map<String, VerificationInfo> infos = getVerifications(c, "CURRENT");
+    assertThat(infos).hasSize(1);
+    assertThat(Iterables.getOnlyElement(infos.values()).reporter).isNull();
+    assertVerification(Iterables.getOnlyElement(infos.values()), i);
+  }
+
+  private Map<String, VerificationInfo> getVerifications(Result c, String filter)
       throws Exception {
     String endPoint = url(c);
+    if (filter != null) {
+      endPoint += "/?filter=" + filter;
+    }
     RestResponse r = adminRestSession.get(endPoint);
     r.assertOK();