diff options
author | Matteo Mara <matteo.mara@sonarsource.com> | 2024-05-10 17:05:39 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-05-31 20:02:42 +0000 |
commit | 66e3e8dcbe046e9c1511c235805981602e8a97b4 (patch) | |
tree | b8ecf0d7041f148873c387ca310782a5984fb783 /server/sonar-db-migration | |
parent | a1281d54a5cb50984f98a8987f77fad4b86ed4e7 (diff) | |
download | sonarqube-66e3e8dcbe046e9c1511c235805981602e8a97b4.tar.gz sonarqube-66e3e8dcbe046e9c1511c235805981602e8a97b4.zip |
SONAR-22141 migrations-status response shows estimated finish date only when migration is running
Diffstat (limited to 'server/sonar-db-migration')
3 files changed, 51 insertions, 4 deletions
diff --git a/server/sonar-db-migration/build.gradle b/server/sonar-db-migration/build.gradle index c1c37924fe7..e2d0de9954c 100644 --- a/server/sonar-db-migration/build.gradle +++ b/server/sonar-db-migration/build.gradle @@ -25,6 +25,7 @@ dependencies { testImplementation 'junit:junit' testImplementation 'org.assertj:assertj-core' testImplementation 'org.junit.jupiter:junit-jupiter-api' + testImplementation 'org.junit.jupiter:junit-jupiter-params' testImplementation 'org.mindrot:jbcrypt' testImplementation 'org.mockito:mockito-core' testImplementation 'org.xmlunit:xmlunit-core' diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/DatabaseMigrationStateImpl.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/DatabaseMigrationStateImpl.java index 0cd28f0c813..bb5db089acf 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/DatabaseMigrationStateImpl.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/DatabaseMigrationStateImpl.java @@ -88,7 +88,7 @@ public class DatabaseMigrationStateImpl implements MutableDatabaseMigrationState @Override public Optional<Instant> getExpectedFinishDate(Instant now) { - if (startedAt == null || totalMigrations == 0 || completedMigrations == 0) { + if (this.getStatus() != Status.RUNNING || startedAt == null || totalMigrations == 0 || completedMigrations == 0) { return Optional.empty(); } Duration elapsed = Duration.between(startedAt, now); diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/DatabaseMigrationStateImplTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/DatabaseMigrationStateImplTest.java index 3abaab81772..9d14ee2077d 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/DatabaseMigrationStateImplTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/DatabaseMigrationStateImplTest.java @@ -23,6 +23,8 @@ import java.time.Instant; import java.time.temporal.ChronoUnit; import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.within; @@ -69,13 +71,13 @@ class DatabaseMigrationStateImplTest { assertThat(underTest.getError()).get().isSameAs(expected); } - + @Test void incrementCompletedMigrations_shouldIncrementCompletedMigrations() { assertThat(underTest.getCompletedMigrations()).isZero(); - + underTest.incrementCompletedMigrations(); - + assertThat(underTest.getCompletedMigrations()).isEqualTo(1); } @@ -103,6 +105,7 @@ class DatabaseMigrationStateImplTest { underTest.setStartedAt(startDate); underTest.setTotalMigrations(2); + underTest.setStatus(DatabaseMigrationState.Status.RUNNING); assertThat(underTest.getExpectedFinishDate(later)).isEmpty(); } @@ -116,8 +119,51 @@ class DatabaseMigrationStateImplTest { underTest.setStartedAt(startDate); underTest.setTotalMigrations(2); underTest.incrementCompletedMigrations(); + underTest.setStatus(DatabaseMigrationState.Status.RUNNING); assertThat(underTest.getExpectedFinishDate(later)).get(InstanceOfAssertFactories.INSTANT) .isCloseTo(expectedEnd, within(1, ChronoUnit.SECONDS)); } + + @ParameterizedTest + @EnumSource(value = DatabaseMigrationStateImpl.Status.class, + names = {"SUCCEEDED", "FAILED", "MIGRATION_REQUIRED", "NONE", "STATUS_NOT_SUPPORTED"}) + void getExpectedFinishDate_whenStatusIsNotRunning_shouldReturnEmptyOptional(DatabaseMigrationState.Status status) { + Instant startDate = Instant.now(); + Instant later = startDate.plus(1, ChronoUnit.MINUTES); + + underTest.setStartedAt(startDate); + underTest.setTotalMigrations(1); + underTest.incrementCompletedMigrations(); + underTest.setStatus(status); + + assertThat(underTest.getExpectedFinishDate(later)).isEmpty(); + } + + @Test + void getExpectedFinishDate_whenTotalMigrationsAreZero_shouldReturnEmptyOptional() { + Instant startDate = Instant.now(); + Instant later = startDate.plus(1, ChronoUnit.MINUTES); + + underTest.setStartedAt(startDate); + underTest.setTotalMigrations(0); + underTest.incrementCompletedMigrations(); + underTest.setStatus(DatabaseMigrationState.Status.RUNNING); + + assertThat(underTest.getExpectedFinishDate(later)).isEmpty(); + } + + @Test + void getExpectedFinishDate_whenStartedAtIsNull_shouldReturnEmptyOptional() { + Instant startDate = Instant.now(); + Instant later = startDate.plus(1, ChronoUnit.MINUTES); + + underTest.setStartedAt(null); + underTest.setTotalMigrations(1); + underTest.incrementCompletedMigrations(); + underTest.setStatus(DatabaseMigrationState.Status.RUNNING); + + assertThat(underTest.getExpectedFinishDate(later)).isEmpty(); + } + } |