]> source.dussan.org Git - sonarqube.git/blob
d23648a4082746ae9026abcefb22c2b32de88fac
[sonarqube.git] /
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
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;
35
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;
42
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);
50
51   @Before
52   public void before() {
53     serverContainer.add(migrationSteps);
54     serverContainer.add(migrationHistory);
55     serverContainer.add(stepRegistry);
56     serverContainer.startComponents();
57   }
58
59   @Test
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);
64
65     underTest.execute();
66
67     verify(migrationSteps, times(2)).readAll();
68     assertThat(stepRegistry.stepRan).isTrue();
69   }
70
71   @Test
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);
77
78     underTest.execute();
79
80     verify(migrationSteps).readFrom(51);
81     assertThat(stepRegistry.stepRan).isTrue();
82   }
83
84   private static class NoOpExecutor implements MigrationStepsExecutor {
85     @Override
86     public void execute(List<RegisteredMigrationStep> steps) {
87       // no op
88     }
89   }
90
91   private static class StepRegistry {
92     boolean stepRan = false;
93   }
94
95   private static class TestMigrationStep implements MigrationStep {
96     private final StepRegistry registry;
97
98     public TestMigrationStep(StepRegistry registry) {
99       this.registry = registry;
100     }
101     @Override
102     public void execute() throws SQLException {
103       registry.stepRan = true;
104     }
105   }
106
107   @SupportsBlueGreen
108   private static class TestBlueGreenMigrationStep implements MigrationStep {
109     private final StepRegistry registry;
110
111     public TestBlueGreenMigrationStep(StepRegistry registry) {
112       this.registry = registry;
113     }
114     @Override
115     public void execute() throws SQLException {
116       registry.stepRan = true;
117     }
118   }
119 }