You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AutoDbMigrationTest.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.platform.db.migration;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.mockito.Mockito;
  24. import org.slf4j.event.Level;
  25. import org.sonar.api.testfixtures.log.LogTester;
  26. import org.sonar.db.DbClient;
  27. import org.sonar.db.dialect.Dialect;
  28. import org.sonar.db.dialect.H2;
  29. import org.sonar.db.dialect.MsSql;
  30. import org.sonar.db.dialect.Oracle;
  31. import org.sonar.db.dialect.PostgreSql;
  32. import org.sonar.server.platform.DefaultServerUpgradeStatus;
  33. import org.sonar.server.platform.db.migration.engine.MigrationEngine;
  34. import static org.assertj.core.api.Assertions.assertThat;
  35. import static org.mockito.ArgumentMatchers.any;
  36. import static org.mockito.Mockito.mock;
  37. import static org.mockito.Mockito.verify;
  38. import static org.mockito.Mockito.verifyNoInteractions;
  39. import static org.mockito.Mockito.when;
  40. public class AutoDbMigrationTest {
  41. @Rule
  42. public LogTester logTester = new LogTester();
  43. private final DbClient dbClient = mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS);
  44. private final DefaultServerUpgradeStatus serverUpgradeStatus = mock(DefaultServerUpgradeStatus.class);
  45. private final MigrationEngine migrationEngine = mock(MigrationEngine.class);
  46. private final MutableDatabaseMigrationState mutableDatabaseMigrationState = mock(MutableDatabaseMigrationState.class);
  47. private final AutoDbMigration underTest = new AutoDbMigration(serverUpgradeStatus, migrationEngine, mutableDatabaseMigrationState);
  48. @Test
  49. public void start_runs_MigrationEngine_on_h2_if_fresh_install() {
  50. start_runs_MigrationEngine_for_dialect_if_fresh_install(new H2());
  51. }
  52. @Test
  53. public void start_runs_MigrationEngine_on_postgre_if_fresh_install() {
  54. start_runs_MigrationEngine_for_dialect_if_fresh_install(new PostgreSql());
  55. }
  56. @Test
  57. public void start_runs_MigrationEngine_on_Oracle_if_fresh_install() {
  58. start_runs_MigrationEngine_for_dialect_if_fresh_install(new Oracle());
  59. }
  60. @Test
  61. public void start_runs_MigrationEngine_on_MsSQL_if_fresh_install() {
  62. start_runs_MigrationEngine_for_dialect_if_fresh_install(new MsSql());
  63. }
  64. private void start_runs_MigrationEngine_for_dialect_if_fresh_install(Dialect dialect) {
  65. mockDialect(dialect);
  66. mockFreshInstall(true);
  67. underTest.start();
  68. verify(migrationEngine).execute(any());
  69. verifyInfoLog();
  70. }
  71. @Test
  72. public void start_does_nothing_if_not_fresh_install() {
  73. mockFreshInstall(false);
  74. underTest.start();
  75. verifyNoInteractions(migrationEngine);
  76. assertThat(logTester.logs(Level.INFO)).isEmpty();
  77. }
  78. @Test
  79. public void start_runs_MigrationEngine_if_autoDbMigration_enabled() {
  80. mockFreshInstall(false);
  81. when(serverUpgradeStatus.isUpgraded()).thenReturn(true);
  82. when(serverUpgradeStatus.isAutoDbUpgrade()).thenReturn(true);
  83. underTest.start();
  84. verify(migrationEngine).execute(any());
  85. assertThat(logTester.logs(Level.INFO)).contains("Automatically perform DB migration, as automatic database upgrade is enabled");
  86. }
  87. @Test
  88. public void start_does_nothing_if_autoDbMigration_but_no_upgrade() {
  89. mockFreshInstall(false);
  90. when(serverUpgradeStatus.isUpgraded()).thenReturn(false);
  91. when(serverUpgradeStatus.isAutoDbUpgrade()).thenReturn(true);
  92. underTest.start();
  93. verifyNoInteractions(migrationEngine);
  94. assertThat(logTester.logs(Level.INFO)).isEmpty();
  95. }
  96. @Test
  97. public void stop_has_no_effect() {
  98. underTest.stop();
  99. }
  100. private void mockFreshInstall(boolean value) {
  101. when(serverUpgradeStatus.isFreshInstall()).thenReturn(value);
  102. }
  103. private void mockDialect(Dialect dialect) {
  104. when(dbClient.getDatabase().getDialect()).thenReturn(dialect);
  105. }
  106. private void verifyInfoLog() {
  107. assertThat(logTester.logs()).hasSize(1);
  108. assertThat(logTester.logs(Level.INFO)).containsExactly("Automatically perform DB migration on fresh install");
  109. }
  110. }