]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6366 rename MigrateDbSystemAction to MigrateDbAction
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Mon, 20 Jul 2015 08:56:11 +0000 (10:56 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 21 Jul 2015 07:22:20 +0000 (09:22 +0200)
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelSafeMode.java
server/sonar-server/src/main/java/org/sonar/server/platform/ws/MigrateDbAction.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/platform/ws/MigrateDbSystemAction.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/platform/ws/MigrateDbActionTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/platform/ws/MigrateDbSystemActionTest.java [deleted file]

index e07555a824e82e0322d5643bd82b63bea35a5885..26c3a75f8a9e3bc2b8a9bd49395f8e50532b08ad 100644 (file)
@@ -187,7 +187,7 @@ import org.sonar.server.platform.monitoring.SonarQubeMonitor;
 import org.sonar.server.platform.monitoring.SystemMonitor;
 import org.sonar.server.platform.ws.InfoAction;
 import org.sonar.server.platform.ws.L10nWs;
-import org.sonar.server.platform.ws.MigrateDbSystemAction;
+import org.sonar.server.platform.ws.MigrateDbAction;
 import org.sonar.server.platform.ws.RestartAction;
 import org.sonar.server.platform.ws.ServerWs;
 import org.sonar.server.platform.ws.StatusAction;
@@ -694,7 +694,7 @@ public class PlatformLevel4 extends PlatformLevel {
       RestartAction.class,
       InfoAction.class,
       UpgradesAction.class,
-      MigrateDbSystemAction.class,
+      MigrateDbAction.class,
       StatusAction.class,
       SystemWs.class,
       SystemMonitor.class,
index 3cb81131d810a2a194e51d45487a8d3499966e1d..c1445a403b69dea35f32f97908b3511c000711b1 100644 (file)
@@ -19,7 +19,7 @@
  */
 package org.sonar.server.platform.platformlevel;
 
-import org.sonar.server.platform.ws.MigrateDbSystemAction;
+import org.sonar.server.platform.ws.MigrateDbAction;
 import org.sonar.server.platform.ws.StatusAction;
 import org.sonar.server.platform.ws.SystemWs;
 import org.sonar.server.ws.ListingWs;
@@ -35,7 +35,7 @@ public class PlatformLevelSafeMode extends PlatformLevel {
     add(
       // Server WS
       StatusAction.class,
-      MigrateDbSystemAction.class,
+      MigrateDbAction.class,
       SystemWs.class,
 
       // Listing WS
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/MigrateDbAction.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/MigrateDbAction.java
new file mode 100644 (file)
index 0000000..206d12a
--- /dev/null
@@ -0,0 +1,183 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.ws;
+
+import com.google.common.io.Resources;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.db.Database;
+import org.sonar.db.version.DatabaseMigration;
+import org.sonar.db.version.DatabaseVersion;
+
+import static org.sonar.db.version.DatabaseMigration.Status.RUNNING;
+
+/**
+ * Implementation of the {@code migrate_db} action for the System WebService.
+ */
+public class MigrateDbAction implements SystemWsAction {
+
+  private static final String UNSUPPORTED_DATABASE_MIGRATION_STATUS = "Unsupported DatabaseMigration status";
+  private static final String MESSAGE_STATUS_NONE = "Database is up-to-date, no migration needed.";
+  private static final String MESSAGE_STATUS_RUNNING = "Database migration is running.";
+  private static final String MESSAGE_STATUS_SUCCEEDED = "Migration succeeded.";
+  private static final String MESSAGE_STATUS_FAILED = "Migration failed: %s.<br/> Please check logs.";
+
+  private static final String STATUS_NO_MIGRATION = "NO_MIGRATION";
+  private static final String STATUS_NOT_SUPPORTED = "NOT_SUPPORTED";
+  private static final String STATUS_MIGRATION_RUNNING = "MIGRATION_RUNNING";
+  private static final String STATUS_MIGRATION_FAILED = "MIGRATION_FAILED";
+  private static final String STATUS_MIGRATION_SUCCEEDED = "MIGRATION_SUCCEEDED";
+
+  private static final String PROPERTY_STATE = "state";
+  private static final String PROPERTY_MESSAGE = "message";
+  private static final String PROPERTY_STARTED_AT = "startedAt";
+
+  private final DatabaseVersion databaseVersion;
+  private final DatabaseMigration databaseMigration;
+  private final Database database;
+
+  public MigrateDbAction(DatabaseVersion databaseVersion, Database database, DatabaseMigration databaseMigration) {
+    this.databaseVersion = databaseVersion;
+    this.database = database;
+    this.databaseMigration = databaseMigration;
+  }
+
+  @Override
+  public void define(WebService.NewController controller) {
+    controller.createAction("migrate_db")
+      .setDescription("Migrate the database to match the current version of SonarQube." +
+        "<br/>" +
+        "Sending a POST request to this URL starts the DB migration. " +
+        "It is strongly advised to <strong>make a database backup</strong> before invoking this WS." +
+        "<br/>" +
+        "State values are:" +
+        "<ul>" +
+        "<li>NO_MIGRATION: DB is up to date with current version of SonarQube.</li>" +
+        "<li>NOT_SUPPORTED: Migration is not supported on embedded databases.</li>" +
+        "<li>MIGRATION_RUNNING: DB migration is under go.</li>" +
+        "<li>MIGRATION_SUCCEEDED: DB migration has run and has been successful.</li>" +
+        "<li>MIGRATION_FAILED: DB migration has run and failed. SonarQube must be restarted in order to retry a " +
+        "DB migration (optionally after DB has been restored from backup).</li>" +
+        "</ul>")
+      .setSince("5.2")
+      .setPost(true)
+      .setHandler(this)
+      .setResponseExample(Resources.getResource(this.getClass(), "example-migrate_db.json"));
+  }
+
+  @Override
+  public void handle(Request request, Response response) throws Exception {
+    Integer currentVersion = databaseVersion.getVersion();
+    if (currentVersion == null) {
+      throw new IllegalStateException("Version can not be retrieved from Database. Database is either blank or corrupted");
+    }
+
+    if (currentVersion >= DatabaseVersion.LAST_VERSION) {
+      writeResponse(response, databaseMigration);
+    } else if (!database.getDialect().supportsMigration()) {
+      writeNotSupportedResponse(response);
+    } else {
+      switch (databaseMigration.status()) {
+        case RUNNING:
+        case FAILED:
+        case SUCCEEDED:
+          writeResponse(response, databaseMigration);
+          break;
+        case NONE:
+          databaseMigration.startIt();
+          writeNoneResponse(response, databaseMigration);
+          break;
+        default:
+          throw new IllegalArgumentException(UNSUPPORTED_DATABASE_MIGRATION_STATUS);
+      }
+    }
+  }
+
+  private static void writeNotSupportedResponse(Response response) {
+    JsonWriter jsonWriter = response.newJsonWriter();
+    jsonWriter.beginObject()
+      .prop(PROPERTY_STATE, STATUS_NOT_SUPPORTED)
+      .prop(PROPERTY_MESSAGE, "Upgrade is not supported on embedded database.")
+      .endObject();
+    jsonWriter.close();
+  }
+
+  private void writeNoneResponse(Response response, DatabaseMigration databaseMigration) {
+    JsonWriter jsonWriter = response.newJsonWriter();
+    jsonWriter.beginObject()
+      .prop(PROPERTY_STATE, statusToJson(RUNNING))
+      .prop(PROPERTY_MESSAGE, MESSAGE_STATUS_RUNNING)
+      .propDateTime(PROPERTY_STARTED_AT, databaseMigration.startedAt())
+      .endObject();
+    jsonWriter.close();
+  }
+
+  private void writeResponse(Response response, DatabaseMigration databaseMigration) {
+    JsonWriter jsonWriter = response.newJsonWriter();
+    jsonWriter.beginObject()
+      .prop(PROPERTY_STATE, statusToJson(databaseMigration.status()))
+      .prop(PROPERTY_MESSAGE, buildMessage(databaseMigration))
+      .propDateTime(PROPERTY_STARTED_AT, databaseMigration.startedAt())
+      .endObject();
+    jsonWriter.close();
+  }
+
+  private String statusToJson(DatabaseMigration.Status status) {
+    switch (status) {
+      case NONE:
+        return STATUS_NO_MIGRATION;
+      case RUNNING:
+        return STATUS_MIGRATION_RUNNING;
+      case FAILED:
+        return STATUS_MIGRATION_FAILED;
+      case SUCCEEDED:
+        return STATUS_MIGRATION_SUCCEEDED;
+      default:
+        throw new IllegalArgumentException(
+          "Unsupported DatabaseMigration.Status " + status + " can not be converted to JSON value");
+    }
+  }
+
+  private static String buildMessage(DatabaseMigration databaseMigration) {
+    switch (databaseMigration.status()) {
+      case NONE:
+        return MESSAGE_STATUS_NONE;
+      case RUNNING:
+        return MESSAGE_STATUS_RUNNING;
+      case SUCCEEDED:
+        return MESSAGE_STATUS_SUCCEEDED;
+      case FAILED:
+        return String.format(MESSAGE_STATUS_FAILED, failureMessage(databaseMigration));
+      default:
+        return UNSUPPORTED_DATABASE_MIGRATION_STATUS;
+    }
+  }
+
+  private static String failureMessage(DatabaseMigration databaseMigration) {
+    Throwable failureError = databaseMigration.failureError();
+    if (failureError == null) {
+      return "No failure error";
+    }
+    return failureError.getMessage();
+  }
+
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/MigrateDbSystemAction.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/MigrateDbSystemAction.java
deleted file mode 100644 (file)
index 9a01368..0000000
+++ /dev/null
@@ -1,183 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.server.platform.ws;
-
-import com.google.common.io.Resources;
-import org.sonar.api.server.ws.Request;
-import org.sonar.api.server.ws.Response;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.db.Database;
-import org.sonar.db.version.DatabaseMigration;
-import org.sonar.db.version.DatabaseVersion;
-
-import static org.sonar.db.version.DatabaseMigration.Status.RUNNING;
-
-/**
- * Implementation of the {@code migrate_db} action for the System WebService.
- */
-public class MigrateDbSystemAction implements SystemWsAction {
-
-  private static final String UNSUPPORTED_DATABASE_MIGRATION_STATUS = "Unsupported DatabaseMigration status";
-  private static final String MESSAGE_STATUS_NONE = "Database is up-to-date, no migration needed.";
-  private static final String MESSAGE_STATUS_RUNNING = "Database migration is running.";
-  private static final String MESSAGE_STATUS_SUCCEEDED = "Migration succeeded.";
-  private static final String MESSAGE_STATUS_FAILED = "Migration failed: %s.<br/> Please check logs.";
-
-  private static final String STATUS_NO_MIGRATION = "NO_MIGRATION";
-  private static final String STATUS_NOT_SUPPORTED = "NOT_SUPPORTED";
-  private static final String STATUS_MIGRATION_RUNNING = "MIGRATION_RUNNING";
-  private static final String STATUS_MIGRATION_FAILED = "MIGRATION_FAILED";
-  private static final String STATUS_MIGRATION_SUCCEEDED = "MIGRATION_SUCCEEDED";
-
-  private static final String PROPERTY_STATE = "state";
-  private static final String PROPERTY_MESSAGE = "message";
-  private static final String PROPERTY_STARTED_AT = "startedAt";
-
-  private final DatabaseVersion databaseVersion;
-  private final DatabaseMigration databaseMigration;
-  private final Database database;
-
-  public MigrateDbSystemAction(DatabaseVersion databaseVersion, Database database, DatabaseMigration databaseMigration) {
-    this.databaseVersion = databaseVersion;
-    this.database = database;
-    this.databaseMigration = databaseMigration;
-  }
-
-  @Override
-  public void define(WebService.NewController controller) {
-    controller.createAction("migrate_db")
-      .setDescription("Migrate the database to match the current version of SonarQube." +
-        "<br/>" +
-        "Sending a POST request to this URL starts the DB migration. " +
-        "It is strongly advised to <strong>make a database backup</strong> before invoking this WS." +
-        "<br/>" +
-        "State values are:" +
-        "<ul>" +
-        "<li>NO_MIGRATION: DB is up to date with current version of SonarQube.</li>" +
-        "<li>NOT_SUPPORTED: Migration is not supported on embedded databases.</li>" +
-        "<li>MIGRATION_RUNNING: DB migration is under go.</li>" +
-        "<li>MIGRATION_SUCCEEDED: DB migration has run and has been successful.</li>" +
-        "<li>MIGRATION_FAILED: DB migration has run and failed. SonarQube must be restarted in order to retry a " +
-        "DB migration (optionally after DB has been restored from backup).</li>" +
-        "</ul>")
-      .setSince("5.2")
-      .setPost(true)
-      .setHandler(this)
-      .setResponseExample(Resources.getResource(this.getClass(), "example-migrate_db.json"));
-  }
-
-  @Override
-  public void handle(Request request, Response response) throws Exception {
-    Integer currentVersion = databaseVersion.getVersion();
-    if (currentVersion == null) {
-      throw new IllegalStateException("Version can not be retrieved from Database. Database is either blank or corrupted");
-    }
-
-    if (currentVersion >= DatabaseVersion.LAST_VERSION) {
-      writeResponse(response, databaseMigration);
-    } else if (!database.getDialect().supportsMigration()) {
-      writeNotSupportedResponse(response);
-    } else {
-      switch (databaseMigration.status()) {
-        case RUNNING:
-        case FAILED:
-        case SUCCEEDED:
-          writeResponse(response, databaseMigration);
-          break;
-        case NONE:
-          databaseMigration.startIt();
-          writeNoneResponse(response, databaseMigration);
-          break;
-        default:
-          throw new IllegalArgumentException(UNSUPPORTED_DATABASE_MIGRATION_STATUS);
-      }
-    }
-  }
-
-  private static void writeNotSupportedResponse(Response response) {
-    JsonWriter jsonWriter = response.newJsonWriter();
-    jsonWriter.beginObject()
-      .prop(PROPERTY_STATE, STATUS_NOT_SUPPORTED)
-      .prop(PROPERTY_MESSAGE, "Upgrade is not supported on embedded database.")
-      .endObject();
-    jsonWriter.close();
-  }
-
-  private void writeNoneResponse(Response response, DatabaseMigration databaseMigration) {
-    JsonWriter jsonWriter = response.newJsonWriter();
-    jsonWriter.beginObject()
-      .prop(PROPERTY_STATE, statusToJson(RUNNING))
-      .prop(PROPERTY_MESSAGE, MESSAGE_STATUS_RUNNING)
-      .propDateTime(PROPERTY_STARTED_AT, databaseMigration.startedAt())
-      .endObject();
-    jsonWriter.close();
-  }
-
-  private void writeResponse(Response response, DatabaseMigration databaseMigration) {
-    JsonWriter jsonWriter = response.newJsonWriter();
-    jsonWriter.beginObject()
-      .prop(PROPERTY_STATE, statusToJson(databaseMigration.status()))
-      .prop(PROPERTY_MESSAGE, buildMessage(databaseMigration))
-      .propDateTime(PROPERTY_STARTED_AT, databaseMigration.startedAt())
-      .endObject();
-    jsonWriter.close();
-  }
-
-  private String statusToJson(DatabaseMigration.Status status) {
-    switch (status) {
-      case NONE:
-        return STATUS_NO_MIGRATION;
-      case RUNNING:
-        return STATUS_MIGRATION_RUNNING;
-      case FAILED:
-        return STATUS_MIGRATION_FAILED;
-      case SUCCEEDED:
-        return STATUS_MIGRATION_SUCCEEDED;
-      default:
-        throw new IllegalArgumentException(
-          "Unsupported DatabaseMigration.Status " + status + " can not be converted to JSON value");
-    }
-  }
-
-  private static String buildMessage(DatabaseMigration databaseMigration) {
-    switch (databaseMigration.status()) {
-      case NONE:
-        return MESSAGE_STATUS_NONE;
-      case RUNNING:
-        return MESSAGE_STATUS_RUNNING;
-      case SUCCEEDED:
-        return MESSAGE_STATUS_SUCCEEDED;
-      case FAILED:
-        return String.format(MESSAGE_STATUS_FAILED, failureMessage(databaseMigration));
-      default:
-        return UNSUPPORTED_DATABASE_MIGRATION_STATUS;
-    }
-  }
-
-  private static String failureMessage(DatabaseMigration databaseMigration) {
-    Throwable failureError = databaseMigration.failureError();
-    if (failureError == null) {
-      return "No failure error";
-    }
-    return failureError.getMessage();
-  }
-
-}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/MigrateDbActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/MigrateDbActionTest.java
new file mode 100644 (file)
index 0000000..ecf87a7
--- /dev/null
@@ -0,0 +1,221 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.ws;
+
+import com.google.common.collect.ImmutableList;
+import java.util.Arrays;
+import java.util.Date;
+import javax.annotation.Nullable;
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.utils.DateUtils;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.Dialect;
+import org.sonar.db.version.DatabaseMigration;
+import org.sonar.db.version.DatabaseMigration.Status;
+import org.sonar.db.version.DatabaseVersion;
+import org.sonar.server.ws.WsTester;
+
+import static com.google.common.base.Predicates.in;
+import static com.google.common.base.Predicates.not;
+import static com.google.common.collect.Iterables.filter;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.sonar.db.version.DatabaseMigration.Status.FAILED;
+import static org.sonar.db.version.DatabaseMigration.Status.NONE;
+import static org.sonar.db.version.DatabaseMigration.Status.RUNNING;
+import static org.sonar.db.version.DatabaseMigration.Status.SUCCEEDED;
+import static org.sonar.test.JsonAssert.assertJson;
+
+public class MigrateDbActionTest {
+
+  private static final String UPTODATE_MSG = "Database is up-to-date, no migration needed.";
+  private static final String MIG_NOT_SUPPORTED_MSG = "Upgrade is not supported on embedded database.";
+  private static final String RUNNING_MSG = "Database migration is running.";
+  private static final Date SOME_DATE = new Date();
+  private static final String SOME_THROWABLE_MSG = "blablabla pop !";
+  private static final String DEFAULT_ERROR_MSG = "No failure error";
+  private static final String MIG_SUCCESS_MSG = "Migration succeeded.";
+  private static final int CURRENT_VERSION = DatabaseVersion.LAST_VERSION;
+  private static final int OLD_VERSION = CURRENT_VERSION - 1;
+  private static final int NEWER_VERSION = CURRENT_VERSION + 1;
+  private static final String STATUS_NONE = "NO_MIGRATION";
+  private static final String STATUS_NOT_SUPPORTED = "NOT_SUPPORTED";
+  private static final String STATUS_RUNNING = "MIGRATION_RUNNING";
+  private static final String STATUS_SUCCEEDED = "MIGRATION_SUCCEEDED";
+  private static final String STATUS_FAILED = "MIGRATION_FAILED";
+
+  DatabaseVersion databaseVersion = mock(DatabaseVersion.class);
+  Database database = mock(Database.class);
+  Dialect dialect = mock(Dialect.class);
+  DatabaseMigration databaseMigration = mock(DatabaseMigration.class);
+  MigrateDbAction underTest = new MigrateDbAction(databaseVersion, database, databaseMigration);
+
+  Request request = mock(Request.class);
+  WsTester.TestResponse response = new WsTester.TestResponse();
+
+  @Before
+  public void wireMocksTogether() {
+    when(database.getDialect()).thenReturn(dialect);
+  }
+
+  @Test(expected = IllegalStateException.class)
+  public void ISE_is_thrown_when_version_can_not_be_retrieved_from_database() throws Exception {
+    when(databaseVersion.getVersion()).thenReturn(null);
+
+    underTest.handle(request, response);
+  }
+
+  @Test
+  public void verify_example() throws Exception {
+    when(databaseVersion.getVersion()).thenReturn(OLD_VERSION);
+    when(dialect.supportsMigration()).thenReturn(true);
+    when(databaseMigration.status()).thenReturn(RUNNING);
+    when(databaseMigration.startedAt()).thenReturn(DateUtils.parseDateTime("2015-02-23T18:54:23+0100"));
+    underTest.handle(request, response);
+
+    assertJson(response.outputAsString()).isSimilarTo(getClass().getResource("example-migrate_db.json"));
+  }
+
+  @Test
+  public void msg_is_operational_and_state_from_databasemigration_when_databaseversion_is_equal_to_currentversion() throws Exception {
+    when(databaseVersion.getVersion()).thenReturn(CURRENT_VERSION);
+    when(databaseMigration.status()).thenReturn(NONE);
+
+    underTest.handle(request, response);
+
+    assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_NONE, UPTODATE_MSG));
+  }
+
+  // this test will raise a IllegalArgumentException when an unsupported value is added to the Status enum
+  @Test
+  public void defensive_test_all_values_of_Status_must_be_supported() throws Exception {
+    for (Status status : filter(Arrays.asList(Status.values()), not(in(ImmutableList.of(NONE, RUNNING, FAILED, SUCCEEDED))))) {
+      when(databaseVersion.getVersion()).thenReturn(CURRENT_VERSION);
+      when(databaseMigration.status()).thenReturn(status);
+
+      underTest.handle(request, response);
+    }
+  }
+
+  @Test
+  public void state_from_databasemigration_when_databaseversion_greater_than_currentversion() throws Exception {
+    when(databaseVersion.getVersion()).thenReturn(NEWER_VERSION);
+    when(databaseMigration.status()).thenReturn(NONE);
+
+    underTest.handle(request, response);
+
+    assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_NONE, UPTODATE_MSG));
+  }
+
+  @Test
+  public void state_is_NONE_with_specific_msg_when_version_is_less_than_current_version_and_dialect_does_not_support_migration() throws Exception {
+    when(databaseVersion.getVersion()).thenReturn(OLD_VERSION);
+    when(dialect.supportsMigration()).thenReturn(false);
+
+    underTest.handle(request, response);
+
+    assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_NOT_SUPPORTED, MIG_NOT_SUPPORTED_MSG));
+  }
+
+  @Test
+  public void state_from_databasemigration_when_dbmigration_status_is_RUNNING() throws Exception {
+    when(databaseVersion.getVersion()).thenReturn(OLD_VERSION);
+    when(dialect.supportsMigration()).thenReturn(true);
+    when(databaseMigration.status()).thenReturn(RUNNING);
+    when(databaseMigration.startedAt()).thenReturn(SOME_DATE);
+
+    underTest.handle(request, response);
+
+    assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_RUNNING, RUNNING_MSG, SOME_DATE));
+  }
+
+  @Test
+  public void state_from_databasemigration_and_msg_includes_error_when_dbmigration_status_is_FAILED() throws Exception {
+    when(databaseVersion.getVersion()).thenReturn(OLD_VERSION);
+    when(dialect.supportsMigration()).thenReturn(true);
+    when(databaseMigration.status()).thenReturn(FAILED);
+    when(databaseMigration.startedAt()).thenReturn(SOME_DATE);
+    when(databaseMigration.failureError()).thenReturn(new UnsupportedOperationException(SOME_THROWABLE_MSG));
+
+    underTest.handle(request, response);
+
+    assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_FAILED, failedMsg(SOME_THROWABLE_MSG), SOME_DATE));
+  }
+
+  @Test
+  public void state_from_databasemigration_and_msg_has_default_msg_when_dbmigration_status_is_FAILED() throws Exception {
+    when(databaseVersion.getVersion()).thenReturn(OLD_VERSION);
+    when(dialect.supportsMigration()).thenReturn(true);
+    when(databaseMigration.status()).thenReturn(FAILED);
+    when(databaseMigration.startedAt()).thenReturn(SOME_DATE);
+    when(databaseMigration.failureError()).thenReturn(null); // no failure throwable caught
+
+    underTest.handle(request, response);
+
+    assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_FAILED, failedMsg(DEFAULT_ERROR_MSG), SOME_DATE));
+  }
+
+  @Test
+  public void state_from_databasemigration_and_msg_has_default_msg_when_dbmigration_status_is_SUCCEEDED() throws Exception {
+    when(databaseVersion.getVersion()).thenReturn(OLD_VERSION);
+    when(dialect.supportsMigration()).thenReturn(true);
+    when(databaseMigration.status()).thenReturn(SUCCEEDED);
+    when(databaseMigration.startedAt()).thenReturn(SOME_DATE);
+
+    underTest.handle(request, response);
+
+    assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_SUCCEEDED, MIG_SUCCESS_MSG, SOME_DATE));
+  }
+
+  @Test
+  public void start_migration_and_return_state_from_databasemigration_when_dbmigration_status_is_NONE() throws Exception {
+    when(databaseVersion.getVersion()).thenReturn(OLD_VERSION);
+    when(dialect.supportsMigration()).thenReturn(true);
+    when(databaseMigration.status()).thenReturn(NONE);
+    when(databaseMigration.startedAt()).thenReturn(SOME_DATE);
+
+    underTest.handle(request, response);
+
+    verify(databaseMigration).startIt();
+    assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_RUNNING, RUNNING_MSG, SOME_DATE));
+  }
+
+  private static String failedMsg(@Nullable String t) {
+    return "Migration failed: " + t + ".<br/> Please check logs.";
+  }
+
+  private static String expectedResponse(String status, String msg) {
+    return "{" +
+      "\"state\":\"" + status + "\"," +
+      "\"message\":\"" + msg + "\"" +
+      "}";
+  }
+
+  private static String expectedResponse(String status, String msg, Date date) {
+    return "{" +
+      "\"state\":\"" + status + "\"," +
+      "\"message\":\"" + msg + "\"," +
+      "\"startedAt\":\"" + DateUtils.formatDateTime(date) + "\"" +
+      "}";
+  }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/MigrateDbSystemActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/MigrateDbSystemActionTest.java
deleted file mode 100644 (file)
index 69a5a15..0000000
+++ /dev/null
@@ -1,221 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.server.platform.ws;
-
-import com.google.common.collect.ImmutableList;
-import java.util.Arrays;
-import java.util.Date;
-import javax.annotation.Nullable;
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.api.server.ws.Request;
-import org.sonar.api.utils.DateUtils;
-import org.sonar.db.Database;
-import org.sonar.db.dialect.Dialect;
-import org.sonar.db.version.DatabaseMigration;
-import org.sonar.db.version.DatabaseMigration.Status;
-import org.sonar.db.version.DatabaseVersion;
-import org.sonar.server.ws.WsTester;
-
-import static com.google.common.base.Predicates.in;
-import static com.google.common.base.Predicates.not;
-import static com.google.common.collect.Iterables.filter;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-import static org.sonar.db.version.DatabaseMigration.Status.FAILED;
-import static org.sonar.db.version.DatabaseMigration.Status.NONE;
-import static org.sonar.db.version.DatabaseMigration.Status.RUNNING;
-import static org.sonar.db.version.DatabaseMigration.Status.SUCCEEDED;
-import static org.sonar.test.JsonAssert.assertJson;
-
-public class MigrateDbSystemActionTest {
-
-  private static final String UPTODATE_MSG = "Database is up-to-date, no migration needed.";
-  private static final String MIG_NOT_SUPPORTED_MSG = "Upgrade is not supported on embedded database.";
-  private static final String RUNNING_MSG = "Database migration is running.";
-  private static final Date SOME_DATE = new Date();
-  private static final String SOME_THROWABLE_MSG = "blablabla pop !";
-  private static final String DEFAULT_ERROR_MSG = "No failure error";
-  private static final String MIG_SUCCESS_MSG = "Migration succeeded.";
-  private static final int CURRENT_VERSION = DatabaseVersion.LAST_VERSION;
-  private static final int OLD_VERSION = CURRENT_VERSION - 1;
-  private static final int NEWER_VERSION = CURRENT_VERSION + 1;
-  private static final String STATUS_NONE = "NO_MIGRATION";
-  private static final String STATUS_NOT_SUPPORTED = "NOT_SUPPORTED";
-  private static final String STATUS_RUNNING = "MIGRATION_RUNNING";
-  private static final String STATUS_SUCCEEDED = "MIGRATION_SUCCEEDED";
-  private static final String STATUS_FAILED = "MIGRATION_FAILED";
-
-  DatabaseVersion databaseVersion = mock(DatabaseVersion.class);
-  Database database = mock(Database.class);
-  Dialect dialect = mock(Dialect.class);
-  DatabaseMigration databaseMigration = mock(DatabaseMigration.class);
-  MigrateDbSystemAction underTest = new MigrateDbSystemAction(databaseVersion, database, databaseMigration);
-
-  Request request = mock(Request.class);
-  WsTester.TestResponse response = new WsTester.TestResponse();
-
-  @Before
-  public void wireMocksTogether() {
-    when(database.getDialect()).thenReturn(dialect);
-  }
-
-  @Test(expected = IllegalStateException.class)
-  public void ISE_is_thrown_when_version_can_not_be_retrieved_from_database() throws Exception {
-    when(databaseVersion.getVersion()).thenReturn(null);
-
-    underTest.handle(request, response);
-  }
-
-  @Test
-  public void verify_example() throws Exception {
-    when(databaseVersion.getVersion()).thenReturn(OLD_VERSION);
-    when(dialect.supportsMigration()).thenReturn(true);
-    when(databaseMigration.status()).thenReturn(RUNNING);
-    when(databaseMigration.startedAt()).thenReturn(DateUtils.parseDateTime("2015-02-23T18:54:23+0100"));
-    underTest.handle(request, response);
-
-    assertJson(response.outputAsString()).isSimilarTo(getClass().getResource("example-migrate_db.json"));
-  }
-
-  @Test
-  public void msg_is_operational_and_state_from_databasemigration_when_databaseversion_is_equal_to_currentversion() throws Exception {
-    when(databaseVersion.getVersion()).thenReturn(CURRENT_VERSION);
-    when(databaseMigration.status()).thenReturn(NONE);
-
-    underTest.handle(request, response);
-
-    assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_NONE, UPTODATE_MSG));
-  }
-
-  // this test will raise a IllegalArgumentException when an unsupported value is added to the Status enum
-  @Test
-  public void defensive_test_all_values_of_Status_must_be_supported() throws Exception {
-    for (Status status : filter(Arrays.asList(Status.values()), not(in(ImmutableList.of(NONE, RUNNING, FAILED, SUCCEEDED))))) {
-      when(databaseVersion.getVersion()).thenReturn(CURRENT_VERSION);
-      when(databaseMigration.status()).thenReturn(status);
-
-      underTest.handle(request, response);
-    }
-  }
-
-  @Test
-  public void state_from_databasemigration_when_databaseversion_greater_than_currentversion() throws Exception {
-    when(databaseVersion.getVersion()).thenReturn(NEWER_VERSION);
-    when(databaseMigration.status()).thenReturn(NONE);
-
-    underTest.handle(request, response);
-
-    assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_NONE, UPTODATE_MSG));
-  }
-
-  @Test
-  public void state_is_NONE_with_specific_msg_when_version_is_less_than_current_version_and_dialect_does_not_support_migration() throws Exception {
-    when(databaseVersion.getVersion()).thenReturn(OLD_VERSION);
-    when(dialect.supportsMigration()).thenReturn(false);
-
-    underTest.handle(request, response);
-
-    assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_NOT_SUPPORTED, MIG_NOT_SUPPORTED_MSG));
-  }
-
-  @Test
-  public void state_from_databasemigration_when_dbmigration_status_is_RUNNING() throws Exception {
-    when(databaseVersion.getVersion()).thenReturn(OLD_VERSION);
-    when(dialect.supportsMigration()).thenReturn(true);
-    when(databaseMigration.status()).thenReturn(RUNNING);
-    when(databaseMigration.startedAt()).thenReturn(SOME_DATE);
-
-    underTest.handle(request, response);
-
-    assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_RUNNING, RUNNING_MSG, SOME_DATE));
-  }
-
-  @Test
-  public void state_from_databasemigration_and_msg_includes_error_when_dbmigration_status_is_FAILED() throws Exception {
-    when(databaseVersion.getVersion()).thenReturn(OLD_VERSION);
-    when(dialect.supportsMigration()).thenReturn(true);
-    when(databaseMigration.status()).thenReturn(FAILED);
-    when(databaseMigration.startedAt()).thenReturn(SOME_DATE);
-    when(databaseMigration.failureError()).thenReturn(new UnsupportedOperationException(SOME_THROWABLE_MSG));
-
-    underTest.handle(request, response);
-
-    assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_FAILED, failedMsg(SOME_THROWABLE_MSG), SOME_DATE));
-  }
-
-  @Test
-  public void state_from_databasemigration_and_msg_has_default_msg_when_dbmigration_status_is_FAILED() throws Exception {
-    when(databaseVersion.getVersion()).thenReturn(OLD_VERSION);
-    when(dialect.supportsMigration()).thenReturn(true);
-    when(databaseMigration.status()).thenReturn(FAILED);
-    when(databaseMigration.startedAt()).thenReturn(SOME_DATE);
-    when(databaseMigration.failureError()).thenReturn(null); // no failure throwable caught
-
-    underTest.handle(request, response);
-
-    assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_FAILED, failedMsg(DEFAULT_ERROR_MSG), SOME_DATE));
-  }
-
-  @Test
-  public void state_from_databasemigration_and_msg_has_default_msg_when_dbmigration_status_is_SUCCEEDED() throws Exception {
-    when(databaseVersion.getVersion()).thenReturn(OLD_VERSION);
-    when(dialect.supportsMigration()).thenReturn(true);
-    when(databaseMigration.status()).thenReturn(SUCCEEDED);
-    when(databaseMigration.startedAt()).thenReturn(SOME_DATE);
-
-    underTest.handle(request, response);
-
-    assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_SUCCEEDED, MIG_SUCCESS_MSG, SOME_DATE));
-  }
-
-  @Test
-  public void start_migration_and_return_state_from_databasemigration_when_dbmigration_status_is_NONE() throws Exception {
-    when(databaseVersion.getVersion()).thenReturn(OLD_VERSION);
-    when(dialect.supportsMigration()).thenReturn(true);
-    when(databaseMigration.status()).thenReturn(NONE);
-    when(databaseMigration.startedAt()).thenReturn(SOME_DATE);
-
-    underTest.handle(request, response);
-
-    verify(databaseMigration).startIt();
-    assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_RUNNING, RUNNING_MSG, SOME_DATE));
-  }
-
-  private static String failedMsg(@Nullable String t) {
-    return "Migration failed: " + t + ".<br/> Please check logs.";
-  }
-
-  private static String expectedResponse(String status, String msg) {
-    return "{" +
-      "\"state\":\"" + status + "\"," +
-      "\"message\":\"" + msg + "\"" +
-      "}";
-  }
-
-  private static String expectedResponse(String status, String msg, Date date) {
-    return "{" +
-      "\"state\":\"" + status + "\"," +
-      "\"message\":\"" + msg + "\"," +
-      "\"startedAt\":\"" + DateUtils.formatDateTime(date) + "\"" +
-      "}";
-  }
-}