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.

MigrationEngineImplTest.java 3.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.engine;
  21. import java.sql.SQLException;
  22. import java.util.List;
  23. import java.util.Optional;
  24. import org.junit.jupiter.api.BeforeEach;
  25. import org.junit.jupiter.api.Test;
  26. import org.sonar.core.platform.SpringComponentContainer;
  27. import org.sonar.server.platform.db.migration.MutableDatabaseMigrationState;
  28. import org.sonar.server.platform.db.migration.history.MigrationHistory;
  29. import org.sonar.server.platform.db.migration.step.MigrationStep;
  30. import org.sonar.server.platform.db.migration.step.MigrationSteps;
  31. import org.sonar.server.platform.db.migration.step.NoOpMigrationStatusListener;
  32. import org.sonar.server.platform.db.migration.step.RegisteredMigrationStep;
  33. import static java.util.Collections.singletonList;
  34. import static org.assertj.core.api.Assertions.assertThat;
  35. import static org.mockito.Mockito.mock;
  36. import static org.mockito.Mockito.times;
  37. import static org.mockito.Mockito.verify;
  38. import static org.mockito.Mockito.when;
  39. class MigrationEngineImplTest {
  40. private final MigrationHistory migrationHistory = mock(MigrationHistory.class);
  41. private final MutableDatabaseMigrationState databaseMigrationState = mock();
  42. private final SpringComponentContainer serverContainer = new SpringComponentContainer();
  43. private final MigrationSteps migrationSteps = mock(MigrationSteps.class);
  44. private final StepRegistry stepRegistry = new StepRegistry();
  45. private final MigrationEngineImpl underTest = new MigrationEngineImpl(migrationHistory, serverContainer, migrationSteps);
  46. @BeforeEach
  47. void before() {
  48. serverContainer.add(migrationSteps);
  49. serverContainer.add(migrationHistory);
  50. serverContainer.add(stepRegistry);
  51. serverContainer.add(databaseMigrationState);
  52. serverContainer.startComponents();
  53. }
  54. @Test
  55. void execute_execute_all_steps_of_there_is_no_last_migration_number() {
  56. when(migrationHistory.getLastMigrationNumber()).thenReturn(Optional.empty());
  57. List<RegisteredMigrationStep> steps = singletonList(new RegisteredMigrationStep(1, "doo", TestMigrationStep.class));
  58. when(migrationSteps.readAll()).thenReturn(steps);
  59. underTest.execute(new NoOpMigrationStatusListener());
  60. verify(migrationSteps, times(2)).readAll();
  61. assertThat(stepRegistry.stepRan).isTrue();
  62. }
  63. @Test
  64. void execute_execute_steps_from_last_migration_number_plus_1() {
  65. when(migrationHistory.getLastMigrationNumber()).thenReturn(Optional.of(50L));
  66. List<RegisteredMigrationStep> steps = singletonList(new RegisteredMigrationStep(1, "doo", TestMigrationStep.class));
  67. when(migrationSteps.readFrom(51)).thenReturn(steps);
  68. when(migrationSteps.readAll()).thenReturn(steps);
  69. underTest.execute(new NoOpMigrationStatusListener());
  70. verify(migrationSteps).readFrom(51);
  71. assertThat(stepRegistry.stepRan).isTrue();
  72. }
  73. private static class StepRegistry {
  74. boolean stepRan = false;
  75. }
  76. private record TestMigrationStep(StepRegistry registry) implements MigrationStep {
  77. @Override
  78. public void execute() throws SQLException {
  79. registry.stepRan = true;
  80. }
  81. }
  82. }