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.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;
34 import static java.util.Collections.singletonList;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.times;
38 import static org.mockito.Mockito.verify;
39 import static org.mockito.Mockito.when;
41 class MigrationEngineImplTest {
42 private final MigrationHistory migrationHistory = mock(MigrationHistory.class);
43 private final SpringComponentContainer serverContainer = new SpringComponentContainer();
44 private final MigrationSteps migrationSteps = mock(MigrationSteps.class);
45 private final StepRegistry stepRegistry = new StepRegistry();
46 private final MigrationEngineImpl underTest = new MigrationEngineImpl(migrationHistory, serverContainer, migrationSteps);
50 serverContainer.add(migrationSteps);
51 serverContainer.add(migrationHistory);
52 serverContainer.add(stepRegistry);
53 serverContainer.startComponents();
57 void execute_execute_all_steps_of_there_is_no_last_migration_number() {
58 when(migrationHistory.getLastMigrationNumber()).thenReturn(Optional.empty());
59 List<RegisteredMigrationStep> steps = singletonList(new RegisteredMigrationStep(1, "doo", TestMigrationStep.class));
60 when(migrationSteps.readAll()).thenReturn(steps);
62 underTest.execute(new NoOpMigrationStatusListener());
64 verify(migrationSteps, times(2)).readAll();
65 assertThat(stepRegistry.stepRan).isTrue();
69 void execute_execute_steps_from_last_migration_number_plus_1() {
70 when(migrationHistory.getLastMigrationNumber()).thenReturn(Optional.of(50L));
71 List<RegisteredMigrationStep> steps = singletonList(new RegisteredMigrationStep(1, "doo", TestMigrationStep.class));
72 when(migrationSteps.readFrom(51)).thenReturn(steps);
73 when(migrationSteps.readAll()).thenReturn(steps);
75 underTest.execute(new NoOpMigrationStatusListener());
77 verify(migrationSteps).readFrom(51);
78 assertThat(stepRegistry.stepRan).isTrue();
81 private static class StepRegistry {
82 boolean stepRan = false;
85 private record TestMigrationStep(StepRegistry registry) implements MigrationStep {
88 public void execute() throws SQLException {
89 registry.stepRan = true;