diff options
author | Steve Marion <steve.marion@sonarsource.com> | 2024-05-06 18:03:36 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-05-08 20:02:44 +0000 |
commit | cb1f5878464822e4e0caf660c9689c255f8cdb66 (patch) | |
tree | f4ebcb430c79e218bcc29e5aafbc3ecb03d80ca3 /server/sonar-webserver-webapi | |
parent | aedbebebed4b2048458a8a7b104f74de23cd2f22 (diff) | |
download | sonarqube-cb1f5878464822e4e0caf660c9689c255f8cdb66.tar.gz sonarqube-cb1f5878464822e4e0caf660c9689c255f8cdb66.zip |
SONAR-22141 implement simple linear ETA algorithm for db-migration.
Diffstat (limited to 'server/sonar-webserver-webapi')
3 files changed, 34 insertions, 29 deletions
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/DbMigrationJsonWriter.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/DbMigrationJsonWriter.java index 29bbe25079c..75380100545 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/DbMigrationJsonWriter.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/DbMigrationJsonWriter.java @@ -19,6 +19,7 @@ */ package org.sonar.server.platform.ws; +import java.util.Date; import org.sonar.api.utils.text.JsonWriter; import org.sonar.server.platform.db.migration.DatabaseMigrationState; @@ -40,13 +41,13 @@ public class DbMigrationJsonWriter { json.beginObject() .prop(FIELD_STATE, databaseMigrationState.getStatus().toString()) .prop(FIELD_MESSAGE, writeMessageIncludingError(databaseMigrationState)) - .propDateTime(FIELD_STARTED_AT, databaseMigrationState.getStartedAt()) + .propDateTime(FIELD_STARTED_AT, databaseMigrationState.getStartedAt().map(Date::from).orElse(null)) .endObject(); } private static String writeMessageIncludingError(DatabaseMigrationState state) { if (state.getStatus() == FAILED) { - Throwable error = state.getError(); + Throwable error = state.getError().orElse(null); return String.format(state.getStatus().getMessage(), error != null ? error.getMessage() : "No failure error"); } else { return state.getStatus().getMessage(); @@ -64,7 +65,7 @@ public class DbMigrationJsonWriter { json.beginObject() .prop(FIELD_STATE, RUNNING.toString()) .prop(FIELD_MESSAGE, RUNNING.getMessage()) - .propDateTime(FIELD_STARTED_AT, databaseMigrationState.getStartedAt()) + .propDateTime(FIELD_STARTED_AT, databaseMigrationState.getStartedAt().map(Date::from).orElse(null)) .endObject(); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/DbMigrationStatusActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/DbMigrationStatusActionTest.java index 63b9bd4ccf7..c2afd80b44e 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/DbMigrationStatusActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/DbMigrationStatusActionTest.java @@ -23,14 +23,15 @@ import com.google.common.collect.ImmutableList; import com.tngtech.java.junit.dataprovider.DataProvider; import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; +import java.time.Instant; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; import java.util.Arrays; -import java.util.Date; import java.util.Optional; import javax.annotation.Nullable; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.sonar.api.utils.DateUtils; import org.sonar.db.Database; import org.sonar.db.dialect.Dialect; import org.sonar.server.platform.db.migration.DatabaseMigrationState; @@ -56,9 +57,10 @@ import static org.sonar.test.JsonAssert.assertJson; @RunWith(DataProviderRunner.class) public class DbMigrationStatusActionTest { - private static final Date SOME_DATE = new Date(); + private static final Instant SOME_DATE = Instant.now(); private static final String SOME_THROWABLE_MSG = "blablabla pop !"; private static final String DEFAULT_ERROR_MSG = "No failure error"; + public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ"); private DatabaseVersion databaseVersion = mock(DatabaseVersion.class); private Database database = mock(Database.class); @@ -77,7 +79,7 @@ public class DbMigrationStatusActionTest { public void verify_example() { when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(RUNNING); - when(migrationState.getStartedAt()).thenReturn(DateUtils.parseDateTime("2015-02-23T18:54:23+0100")); + when(migrationState.getStartedAt()).thenReturn(Optional.of(DATE_TIME_FORMATTER.parse("2015-02-23T18:54:23+0100", Instant::from))); TestResponse response = tester.newRequest().execute(); @@ -143,7 +145,7 @@ public class DbMigrationStatusActionTest { when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(RUNNING); - when(migrationState.getStartedAt()).thenReturn(SOME_DATE); + when(migrationState.getStartedAt()).thenReturn(Optional.of(SOME_DATE)); TestResponse response = tester.newRequest().execute(); @@ -156,8 +158,8 @@ public class DbMigrationStatusActionTest { when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(FAILED); - when(migrationState.getStartedAt()).thenReturn(SOME_DATE); - when(migrationState.getError()).thenReturn(new UnsupportedOperationException(SOME_THROWABLE_MSG)); + when(migrationState.getStartedAt()).thenReturn(Optional.of(SOME_DATE)); + when(migrationState.getError()).thenReturn(Optional.of(new UnsupportedOperationException(SOME_THROWABLE_MSG))); TestResponse response = tester.newRequest().execute(); @@ -170,8 +172,8 @@ public class DbMigrationStatusActionTest { when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(FAILED); - when(migrationState.getStartedAt()).thenReturn(SOME_DATE); - when(migrationState.getError()).thenReturn(null); // no failure throwable caught + when(migrationState.getStartedAt()).thenReturn(Optional.of(SOME_DATE)); + when(migrationState.getError()).thenReturn(Optional.empty()); // no failure throwable caught TestResponse response = tester.newRequest().execute(); @@ -184,7 +186,7 @@ public class DbMigrationStatusActionTest { when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(SUCCEEDED); - when(migrationState.getStartedAt()).thenReturn(SOME_DATE); + when(migrationState.getStartedAt()).thenReturn(Optional.of(SOME_DATE)); TestResponse response = tester.newRequest().execute(); @@ -197,7 +199,7 @@ public class DbMigrationStatusActionTest { when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(NONE); - when(migrationState.getStartedAt()).thenReturn(SOME_DATE); + when(migrationState.getStartedAt()).thenReturn(Optional.of(SOME_DATE)); TestResponse response = tester.newRequest().execute(); @@ -223,11 +225,11 @@ public class DbMigrationStatusActionTest { "}"; } - private static String expectedResponse(String status, String msg, Date date) { + private static String expectedResponse(String status, String msg, Instant date) { return "{" + "\"state\":\"" + status + "\"," + "\"message\":\"" + msg + "\"," + - "\"startedAt\":\"" + DateUtils.formatDateTime(date) + "\"" + + "\"startedAt\":\"" + DATE_TIME_FORMATTER.format(date.atZone(ZoneOffset.systemDefault())) + "\"" + "}"; } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/MigrateDbActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/MigrateDbActionTest.java index 7e6d2cb7ee8..8513ff7ad88 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/MigrateDbActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/MigrateDbActionTest.java @@ -23,14 +23,15 @@ import com.google.common.collect.ImmutableList; import com.tngtech.java.junit.dataprovider.DataProvider; import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; +import java.time.Instant; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; import java.util.Arrays; -import java.util.Date; import java.util.Optional; import javax.annotation.Nullable; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.sonar.api.utils.DateUtils; import org.sonar.db.Database; import org.sonar.db.dialect.Dialect; import org.sonar.server.platform.db.migration.DatabaseMigration; @@ -57,7 +58,7 @@ import static org.sonar.test.JsonAssert.assertJson; @RunWith(DataProviderRunner.class) public class MigrateDbActionTest { - private static final Date SOME_DATE = new Date(); + private static final Instant SOME_DATE = Instant.now(); private static final String SOME_THROWABLE_MSG = "blablabla pop !"; private static final String DEFAULT_ERROR_MSG = "No failure error"; @@ -71,6 +72,7 @@ public class MigrateDbActionTest { 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."; + public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ"); private DatabaseVersion databaseVersion = mock(DatabaseVersion.class); private Database database = mock(Database.class); @@ -100,7 +102,7 @@ public class MigrateDbActionTest { public void verify_example() { when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(RUNNING); - when(migrationState.getStartedAt()).thenReturn(DateUtils.parseDateTime("2015-02-23T18:54:23+0100")); + when(migrationState.getStartedAt()).thenReturn(Optional.of(DATE_TIME_FORMATTER.parse("2015-02-23T18:54:23+0100", Instant::from))); TestResponse response = tester.newRequest().execute(); @@ -154,7 +156,7 @@ public class MigrateDbActionTest { when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(RUNNING); - when(migrationState.getStartedAt()).thenReturn(SOME_DATE); + when(migrationState.getStartedAt()).thenReturn(Optional.of(SOME_DATE)); TestResponse response = tester.newRequest().execute(); @@ -167,8 +169,8 @@ public class MigrateDbActionTest { when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(FAILED); - when(migrationState.getStartedAt()).thenReturn(SOME_DATE); - when(migrationState.getError()).thenReturn(new UnsupportedOperationException(SOME_THROWABLE_MSG)); + when(migrationState.getStartedAt()).thenReturn(Optional.of(SOME_DATE)); + when(migrationState.getError()).thenReturn(Optional.of(new UnsupportedOperationException(SOME_THROWABLE_MSG))); TestResponse response = tester.newRequest().execute(); @@ -181,8 +183,8 @@ public class MigrateDbActionTest { when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(FAILED); - when(migrationState.getStartedAt()).thenReturn(SOME_DATE); - when(migrationState.getError()).thenReturn(null); // no failure throwable caught + when(migrationState.getStartedAt()).thenReturn(Optional.of(SOME_DATE)); + when(migrationState.getError()).thenReturn(Optional.empty()); // no failure throwable caught TestResponse response = tester.newRequest().execute(); @@ -195,7 +197,7 @@ public class MigrateDbActionTest { when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(SUCCEEDED); - when(migrationState.getStartedAt()).thenReturn(SOME_DATE); + when(migrationState.getStartedAt()).thenReturn(Optional.of(SOME_DATE)); TestResponse response = tester.newRequest().execute(); @@ -208,7 +210,7 @@ public class MigrateDbActionTest { when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(NONE); - when(migrationState.getStartedAt()).thenReturn(SOME_DATE); + when(migrationState.getStartedAt()).thenReturn(Optional.of(SOME_DATE)); TestResponse response = tester.newRequest().execute(); @@ -235,11 +237,11 @@ public class MigrateDbActionTest { "}"; } - private static String expectedResponse(String status, String msg, Date date) { + private static String expectedResponse(String status, String msg, Instant date) { return "{" + "\"state\":\"" + status + "\"," + "\"message\":\"" + msg + "\"," + - "\"startedAt\":\"" + DateUtils.formatDateTime(date) + "\"" + + "\"startedAt\":\"" + DATE_TIME_FORMATTER.format(date.atZone(ZoneOffset.UTC)) + "\"" + "}"; } } |