import org.sonar.core.persistence.DatabaseVersion;
import org.sonar.server.db.migrations.DatabaseMigration;
-import static org.sonar.server.db.migrations.DatabaseMigration.Status.NONE;
import static org.sonar.server.db.migrations.DatabaseMigration.Status.RUNNING;
-import static org.sonar.server.db.migrations.DatabaseMigration.Status.SUCCEEDED;
/**
* Implementation of the {@code migrate_db} action for the System WebService.
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_NOT_SUPPORTED = "NOT_SUPPORTED";
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;
private void writeNotSupportedResponse(Response response) {
JsonWriter jsonWriter = response.newJsonWriter();
jsonWriter.beginObject()
- .prop("operational", false)
- .prop("state", STATUS_NOT_SUPPORTED)
- .prop("message", "Upgrade is not supported on embedded database.")
+ .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("operational", false)
- .prop("state", statusToJson(RUNNING))
- .prop("message", MESSAGE_STATUS_RUNNING)
- .propDateTime("startedAt", databaseMigration.startedAt())
+ .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("operational", isOperational(databaseMigration))
- .prop("state", statusToJson(databaseMigration.status()))
- .prop("message", buildMessage(databaseMigration))
- .propDateTime("startedAt", databaseMigration.startedAt())
+ .prop(PROPERTY_STATE, statusToJson(databaseMigration.status()))
+ .prop(PROPERTY_MESSAGE, buildMessage(databaseMigration))
+ .propDateTime(PROPERTY_STARTED_AT, databaseMigration.startedAt())
.endObject();
jsonWriter.close();
}
}
}
- private static boolean isOperational(DatabaseMigration databaseMigration) {
- return databaseMigration.status() == NONE || databaseMigration.status() == SUCCEEDED;
- }
-
private static String buildMessage(DatabaseMigration databaseMigration) {
switch (databaseMigration.status()) {
case NONE:
underTest.handle(request, response);
- assertJson(response.outputAsString()).isSimilarTo(expectedResponse(true, STATUS_NONE, UPTODATE_MSG));
+ 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 msg_is_operational_and_state_from_databasemigration_when_databaseversion_greater_than_currentversion() throws Exception {
+ 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(true, STATUS_NONE, UPTODATE_MSG));
+ assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_NONE, UPTODATE_MSG));
}
@Test
- public void msg_is_not_operational_and_state_is_NONE_with_specific_msg_when_version_is_less_than_current_version_and_dialect_does_not_support_migration() throws Exception {
+ 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(false, STATUS_NOT_SUPPORTED, MIG_NOT_SUPPORTED_MSG));
+ assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_NOT_SUPPORTED, MIG_NOT_SUPPORTED_MSG));
}
@Test
- public void msg_is_not_operational_and_state_from_databasemigration_when_dbmigration_status_is_RUNNING() throws Exception {
+ 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);
underTest.handle(request, response);
- assertJson(response.outputAsString()).isSimilarTo(expectedResponse(false, STATUS_RUNNING, RUNNING_MSG, SOME_DATE));
+ assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_RUNNING, RUNNING_MSG, SOME_DATE));
}
@Test
- public void msg_is_not_operational_and_state_from_databasemigration_and_msg_includes_error_when_dbmigration_status_is_FAILED() throws Exception {
+ 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);
underTest.handle(request, response);
- assertJson(response.outputAsString()).isSimilarTo(expectedResponse(false, STATUS_FAILED, failedMsg(SOME_THROWABLE_MSG), SOME_DATE));
+ assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_FAILED, failedMsg(SOME_THROWABLE_MSG), SOME_DATE));
}
@Test
- public void msg_is_not_operational_and_state_from_databasemigration_and_msg_has_default_msg_when_dbmigration_status_is_FAILED() throws Exception {
+ 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);
underTest.handle(request, response);
- assertJson(response.outputAsString()).isSimilarTo(expectedResponse(false, STATUS_FAILED, failedMsg(DEFAULT_ERROR_MSG), SOME_DATE));
+ assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_FAILED, failedMsg(DEFAULT_ERROR_MSG), SOME_DATE));
}
@Test
- public void msg_is_operational_and_state_from_databasemigration_and_msg_has_default_msg_when_dbmigration_status_is_FAILED() throws Exception {
+ 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);
underTest.handle(request, response);
- assertJson(response.outputAsString()).isSimilarTo(expectedResponse(true, STATUS_SUCCEEDED, MIG_SUCCESS_MSG, SOME_DATE));
+ assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_SUCCEEDED, MIG_SUCCESS_MSG, SOME_DATE));
}
@Test
- public void start_migration_and_return_msg_is_not_operational_and_state_from_databasemigration_when_dbmigration_status_is_NONE() throws Exception {
+ 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);
underTest.handle(request, response);
verify(databaseMigration).startIt();
- assertJson(response.outputAsString()).isSimilarTo(expectedResponse(false, STATUS_RUNNING, RUNNING_MSG, SOME_DATE));
+ 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(boolean operational, String status, String msg) {
+ private static String expectedResponse(String status, String msg) {
return "{" +
- "\"operational\":" + operational + "," +
"\"state\":\"" + status + "\"," +
"\"message\":\"" + msg + "\"" +
"}";
}
- private static String expectedResponse(boolean operational, String status, String msg, Date date) {
+ private static String expectedResponse(String status, String msg, Date date) {
return "{" +
- "\"operational\":" + operational + "," +
"\"state\":\"" + status + "\"," +
"\"message\":\"" + msg + "\"," +
"\"startedAt\":\"" + DateUtils.formatDateTime(date) + "\"" +