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.Before;
26 import org.junit.Test;
27 import org.sonar.api.config.internal.MapSettings;
28 import org.sonar.core.platform.SpringComponentContainer;
29 import org.sonar.server.platform.db.migration.SupportsBlueGreen;
30 import org.sonar.server.platform.db.migration.history.MigrationHistory;
31 import org.sonar.server.platform.db.migration.step.MigrationStep;
32 import org.sonar.server.platform.db.migration.step.MigrationSteps;
33 import org.sonar.server.platform.db.migration.step.MigrationStepsExecutor;
34 import org.sonar.server.platform.db.migration.step.RegisteredMigrationStep;
36 import static java.util.Collections.singletonList;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.times;
40 import static org.mockito.Mockito.verify;
41 import static org.mockito.Mockito.when;
43 public class MigrationEngineImplTest {
44 private final MigrationHistory migrationHistory = mock(MigrationHistory.class);
45 private final SpringComponentContainer serverContainer = new SpringComponentContainer();
46 private final MigrationSteps migrationSteps = mock(MigrationSteps.class);
47 private final StepRegistry stepRegistry = new StepRegistry();
48 private final MapSettings settings = new MapSettings();
49 private final MigrationEngineImpl underTest = new MigrationEngineImpl(migrationHistory, serverContainer, migrationSteps);
52 public void before() {
53 serverContainer.add(migrationSteps);
54 serverContainer.add(migrationHistory);
55 serverContainer.add(stepRegistry);
56 serverContainer.startComponents();
60 public void execute_execute_all_steps_of_there_is_no_last_migration_number() {
61 when(migrationHistory.getLastMigrationNumber()).thenReturn(Optional.empty());
62 List<RegisteredMigrationStep> steps = singletonList(new RegisteredMigrationStep(1, "doo", TestMigrationStep.class));
63 when(migrationSteps.readAll()).thenReturn(steps);
67 verify(migrationSteps, times(2)).readAll();
68 assertThat(stepRegistry.stepRan).isTrue();
72 public void execute_execute_steps_from_last_migration_number_plus_1() {
73 when(migrationHistory.getLastMigrationNumber()).thenReturn(Optional.of(50L));
74 List<RegisteredMigrationStep> steps = singletonList(new RegisteredMigrationStep(1, "doo", TestMigrationStep.class));
75 when(migrationSteps.readFrom(51)).thenReturn(steps);
76 when(migrationSteps.readAll()).thenReturn(steps);
80 verify(migrationSteps).readFrom(51);
81 assertThat(stepRegistry.stepRan).isTrue();
84 private static class NoOpExecutor implements MigrationStepsExecutor {
86 public void execute(List<RegisteredMigrationStep> steps) {
91 private static class StepRegistry {
92 boolean stepRan = false;
95 private static class TestMigrationStep implements MigrationStep {
96 private final StepRegistry registry;
98 public TestMigrationStep(StepRegistry registry) {
99 this.registry = registry;
102 public void execute() throws SQLException {
103 registry.stepRan = true;
108 private static class TestBlueGreenMigrationStep implements MigrationStep {
109 private final StepRegistry registry;
111 public TestBlueGreenMigrationStep(StepRegistry registry) {
112 this.registry = registry;
115 public void execute() throws SQLException {
116 registry.stepRan = true;