From 70746c3d69d26a6277abde58b357537d98e378fe Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Mon, 4 May 2015 09:14:17 +0200 Subject: [PATCH] SONAR-6366 remove operational field from JSON response remove operational field from response of /api/system/migrate_db, response from /api/system/status should be used instead --- .../platform/ws/MigrateDbSystemWsAction.java | 31 +++++++--------- .../platform/ws/example-migrate_db.json | 1 - .../ws/MigrateDbSystemWsActionTest.java | 36 +++++++++---------- 3 files changed, 30 insertions(+), 38 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/MigrateDbSystemWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/MigrateDbSystemWsAction.java index 0d38ef73cfb..97601e57f91 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/MigrateDbSystemWsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/MigrateDbSystemWsAction.java @@ -28,9 +28,7 @@ import org.sonar.core.persistence.Database; 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. @@ -43,12 +41,16 @@ public class MigrateDbSystemWsAction implements SystemWsAction { private static final String MESSAGE_STATUS_SUCCEEDED = "Migration succeeded."; private static final String MESSAGE_STATUS_FAILED = "Migration failed: %s.
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; @@ -115,9 +117,8 @@ public class MigrateDbSystemWsAction implements SystemWsAction { 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(); } @@ -125,10 +126,9 @@ public class MigrateDbSystemWsAction implements SystemWsAction { 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(); } @@ -136,10 +136,9 @@ public class MigrateDbSystemWsAction implements SystemWsAction { 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(); } @@ -160,10 +159,6 @@ public class MigrateDbSystemWsAction implements SystemWsAction { } } - private static boolean isOperational(DatabaseMigration databaseMigration) { - return databaseMigration.status() == NONE || databaseMigration.status() == SUCCEEDED; - } - private static String buildMessage(DatabaseMigration databaseMigration) { switch (databaseMigration.status()) { case NONE: diff --git a/server/sonar-server/src/main/resources/org/sonar/server/platform/ws/example-migrate_db.json b/server/sonar-server/src/main/resources/org/sonar/server/platform/ws/example-migrate_db.json index 947e9a4d55b..dd137d7cd21 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/platform/ws/example-migrate_db.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/platform/ws/example-migrate_db.json @@ -1,5 +1,4 @@ { - "operational": false, "state": "MIGRATION_RUNNING", "message": "Database migration is running.", "startedAt": "2015-02-23T18:54:23+0100" diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/MigrateDbSystemWsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/MigrateDbSystemWsActionTest.java index 051a555bb1f..e009211abf8 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/MigrateDbSystemWsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/MigrateDbSystemWsActionTest.java @@ -103,7 +103,7 @@ public class MigrateDbSystemWsActionTest { 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 @@ -118,27 +118,27 @@ public class MigrateDbSystemWsActionTest { } @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); @@ -146,11 +146,11 @@ public class MigrateDbSystemWsActionTest { 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); @@ -159,11 +159,11 @@ public class MigrateDbSystemWsActionTest { 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); @@ -172,11 +172,11 @@ public class MigrateDbSystemWsActionTest { 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); @@ -184,11 +184,11 @@ public class MigrateDbSystemWsActionTest { 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); @@ -197,24 +197,22 @@ public class MigrateDbSystemWsActionTest { 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 + ".
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) + "\"" + -- 2.39.5