3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.platform.db.migration.engine;
22 import java.sql.SQLException;
23 import java.util.List;
24 import java.util.Optional;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.sonar.core.platform.SpringComponentContainer;
28 import org.sonar.server.platform.db.migration.MutableDatabaseMigrationState;
29 import org.sonar.server.platform.db.migration.history.MigrationHistory;
30 import org.sonar.server.platform.db.migration.step.MigrationStep;
31 import org.sonar.server.platform.db.migration.step.MigrationSteps;
32 import org.sonar.server.platform.db.migration.step.NoOpMigrationStatusListener;
33 import org.sonar.server.platform.db.migration.step.RegisteredMigrationStep;
34 import org.sonar.server.telemetry.TelemetryDbMigrationSuccessProvider;
35 import org.sonar.server.telemetry.TelemetryDbMigrationStepsProvider;
36 import org.sonar.server.telemetry.TelemetryDbMigrationTotalTimeProvider;
38 import static java.util.Collections.singletonList;
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.times;
42 import static org.mockito.Mockito.verify;
43 import static org.mockito.Mockito.when;
45 class MigrationEngineImplTest {
46 private final MigrationHistory migrationHistory = mock(MigrationHistory.class);
47 private final MutableDatabaseMigrationState databaseMigrationState = mock();
48 private final SpringComponentContainer serverContainer = new SpringComponentContainer();
49 private final MigrationSteps migrationSteps = mock(MigrationSteps.class);
50 private final StepRegistry stepRegistry = new StepRegistry();
51 private final TelemetryDbMigrationTotalTimeProvider telemetryDbMigrationTotalTimeProvider = new TelemetryDbMigrationTotalTimeProvider();
52 private final TelemetryDbMigrationStepsProvider telemetryUpgradeStepsProvider = new TelemetryDbMigrationStepsProvider();
53 private final TelemetryDbMigrationSuccessProvider telemetryDbMigrationSuccessProvider = new TelemetryDbMigrationSuccessProvider();
54 private final MigrationEngineImpl underTest = new MigrationEngineImpl(migrationHistory, serverContainer, migrationSteps);
58 serverContainer.add(telemetryDbMigrationTotalTimeProvider);
59 serverContainer.add(telemetryUpgradeStepsProvider);
60 serverContainer.add(telemetryDbMigrationSuccessProvider);
61 serverContainer.add(migrationSteps);
62 serverContainer.add(migrationHistory);
63 serverContainer.add(stepRegistry);
64 serverContainer.add(databaseMigrationState);
65 serverContainer.startComponents();
69 void execute_execute_all_steps_of_there_is_no_last_migration_number() {
70 when(migrationHistory.getLastMigrationNumber()).thenReturn(Optional.empty());
71 List<RegisteredMigrationStep> steps = singletonList(new RegisteredMigrationStep(1, "doo", TestMigrationStep.class));
72 when(migrationSteps.readAll()).thenReturn(steps);
74 underTest.execute(new NoOpMigrationStatusListener());
76 verify(migrationSteps, times(2)).readAll();
77 assertThat(stepRegistry.stepRan).isTrue();
81 void execute_execute_steps_from_last_migration_number_plus_1() {
82 when(migrationHistory.getLastMigrationNumber()).thenReturn(Optional.of(50L));
83 List<RegisteredMigrationStep> steps = singletonList(new RegisteredMigrationStep(1, "doo", TestMigrationStep.class));
84 when(migrationSteps.readFrom(51)).thenReturn(steps);
85 when(migrationSteps.readAll()).thenReturn(steps);
87 underTest.execute(new NoOpMigrationStatusListener());
89 verify(migrationSteps).readFrom(51);
90 assertThat(stepRegistry.stepRan).isTrue();
93 private static class StepRegistry {
94 boolean stepRan = false;
97 private record TestMigrationStep(StepRegistry registry) implements MigrationStep {
100 public void execute() throws SQLException {
101 registry.stepRan = true;