]> source.dussan.org Git - sonarqube.git/blob
c1af7bacb2f86766ef38422dd6a2dbfdf63ff4b9
[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 org.junit.Before;
23 import org.junit.Test;
24 import org.sonar.server.platform.db.migration.MutableDatabaseMigrationState;
25 import org.sonar.server.platform.db.migration.history.MigrationHistory;
26 import org.sonar.server.platform.db.migration.step.MigrationStep;
27 import org.sonar.server.platform.db.migration.step.MigrationSteps;
28 import org.sonar.server.platform.db.migration.step.MigrationStepsExecutorImpl;
29 import org.sonar.server.platform.db.migration.step.RegisteredMigrationStep;
30 import org.sonar.server.telemetry.TelemetryDbMigrationSuccessProvider;
31 import org.sonar.server.telemetry.TelemetryDbMigrationStepsProvider;
32 import org.sonar.server.telemetry.TelemetryDbMigrationTotalTimeProvider;
33
34 import static java.util.Arrays.asList;
35 import static java.util.Collections.emptyList;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.when;
39
40 public class MigrationContainerPopulatorImplTest {
41   private final SimpleMigrationContainer migrationContainer = new SimpleMigrationContainer();
42   private final MigrationSteps migrationSteps = mock(MigrationSteps.class);
43   private final MigrationContainerPopulatorImpl underTest = new MigrationContainerPopulatorImpl();
44
45   @Before
46   public void setUp() {
47     migrationContainer.add(migrationSteps);
48   }
49
50   @Test
51   public void populateContainer_adds_MigrationStepsExecutorImpl() {
52     when(migrationSteps.readAll()).thenReturn(emptyList());
53
54     // add MigrationStepsExecutorImpl's dependencies
55     migrationContainer.add(mock(MigrationHistory.class));
56     migrationContainer.add(mock(MutableDatabaseMigrationState.class));
57     migrationContainer.add(mock(TelemetryDbMigrationStepsProvider.class));
58     migrationContainer.add(mock(TelemetryDbMigrationTotalTimeProvider.class));
59     migrationContainer.add(mock(TelemetryDbMigrationSuccessProvider.class));
60
61     migrationContainer.startComponents();
62     underTest.populateContainer(migrationContainer);
63
64     assertThat(migrationContainer.getComponentByType(MigrationStepsExecutorImpl.class)).isNotNull();
65   }
66
67   @Test
68   public void populateContainer_adds_classes_of_all_steps_defined_in_MigrationSteps() {
69     when(migrationSteps.readAll()).thenReturn(asList(
70       new RegisteredMigrationStep(1, "foo", MigrationStep1.class),
71       new RegisteredMigrationStep(2, "bar", MigrationStep2.class),
72       new RegisteredMigrationStep(3, "dor", MigrationStep3.class)));
73
74     migrationContainer.startComponents();
75     underTest.populateContainer(migrationContainer);
76
77     assertThat(migrationContainer.getComponentsByType(MigrationStep1.class)).isNotNull();
78     assertThat(migrationContainer.getComponentsByType(MigrationStep2.class)).isNotNull();
79     assertThat(migrationContainer.getComponentsByType(MigrationStep3.class)).isNotNull();
80   }
81
82   @Test
83   public void populateCotnainer_does_not_fail_if_same_class_is_used_for_more_than_one_migration() {
84     when(migrationSteps.readAll()).thenReturn(asList(
85       new RegisteredMigrationStep(1, "foo", MigrationStep1.class),
86       new RegisteredMigrationStep(2, "bar", MigrationStep2.class),
87       new RegisteredMigrationStep(3, "bar2", MigrationStep2.class),
88       new RegisteredMigrationStep(4, "foo2", MigrationStep1.class),
89       new RegisteredMigrationStep(5, "dor", MigrationStep3.class)));
90
91     migrationContainer.startComponents();
92     underTest.populateContainer(migrationContainer);
93
94     assertThat(migrationContainer.getComponentsByType(MigrationStep1.class)).isNotNull();
95     assertThat(migrationContainer.getComponentsByType(MigrationStep2.class)).isNotNull();
96     assertThat(migrationContainer.getComponentsByType(MigrationStep3.class)).isNotNull();
97   }
98
99   private static abstract class NoopMigrationStep implements MigrationStep {
100     @Override
101     public void execute() {
102       throw new UnsupportedOperationException("execute not implemented");
103     }
104   }
105
106   public static final class MigrationStep1 extends NoopMigrationStep {
107
108   }
109
110   public static final class MigrationStep2 extends NoopMigrationStep {
111
112   }
113
114   public static final class MigrationStep3 extends NoopMigrationStep {
115
116   }
117
118   public static final class Clazz1 {
119
120   }
121
122   public static final class Clazz2 {
123
124   }
125
126   public static final class Clazz3 {
127
128   }
129 }