]> source.dussan.org Git - sonarqube.git/blob
40bc5fb99b537e1868b13df8aea2cb830a264e31
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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.step;
21
22 import java.util.Random;
23 import org.junit.Test;
24 import org.mockito.InOrder;
25 import org.sonar.server.platform.db.migration.version.DbVersion;
26
27 import static org.assertj.core.api.Assertions.assertThat;
28 import static org.assertj.core.api.Assertions.assertThatThrownBy;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.Mockito.doNothing;
31 import static org.mockito.Mockito.inOrder;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.verifyZeroInteractions;
34 import static org.mockito.Mockito.when;
35
36 public class MigrationStepsProviderTest {
37
38   private InternalMigrationStepRegistry internalMigrationStepRegistry = mock(InternalMigrationStepRegistry.class);
39   private MigrationStepsProvider underTest = new MigrationStepsProvider();
40
41   @Test
42   public void provide_throws_ISE_with_registry_build_throws_ISE_because_it_is_empty() {
43     IllegalStateException expected = new IllegalStateException("faking ISE because registry is empty");
44     when(internalMigrationStepRegistry.build()).thenThrow(expected);
45
46     assertThatThrownBy(() -> underTest.provide(internalMigrationStepRegistry))
47       .isInstanceOf(expected.getClass())
48       .hasMessage(expected.getMessage());
49   }
50
51   @Test
52   public void provide_calls_DbVersion_addStep_in_order_and_only_once() {
53     DbVersion dbVersion1 = newMockFailingOnSecondBuildCall();
54     DbVersion dbVersion2 = newMockFailingOnSecondBuildCall();
55     DbVersion dbVersion3 = newMockFailingOnSecondBuildCall();
56     InOrder inOrder = inOrder(dbVersion1, dbVersion2, dbVersion3);
57     MigrationSteps expected = mock(MigrationSteps.class);
58     when(internalMigrationStepRegistry.build()).thenReturn(expected);
59
60     assertThat(underTest.provide(internalMigrationStepRegistry, dbVersion1, dbVersion2, dbVersion3))
61       .isSameAs(expected);
62
63     inOrder.verify(dbVersion1).addSteps(internalMigrationStepRegistry);
64     inOrder.verify(dbVersion2).addSteps(internalMigrationStepRegistry);
65     inOrder.verify(dbVersion3).addSteps(internalMigrationStepRegistry);
66     inOrder.verifyNoMoreInteractions();
67
68     // calling a second time with another argument, it's just ignored
69     DbVersion dbVersion4 = newMockFailingOnSecondBuildCall();
70     assertThat(underTest.provide(internalMigrationStepRegistry, dbVersion4)).isSameAs(expected);
71     verifyZeroInteractions(dbVersion4);
72   }
73
74   @Test
75   public void provide_always_returns_the_same_MigrationSteps_instance_and_calls_registry_build_only_once() {
76     MigrationSteps migrationSteps = mock(MigrationSteps.class);
77     when(internalMigrationStepRegistry.build())
78       .thenReturn(migrationSteps)
79       .thenThrow(new RuntimeException("method build should not be called twice"));
80
81     for (int i = 0; i < Math.abs(new Random().nextInt(50)) + 1; i++) {
82       assertThat(underTest.provide(internalMigrationStepRegistry)).isSameAs(migrationSteps);
83     }
84
85   }
86
87   private static DbVersion newMockFailingOnSecondBuildCall() {
88     DbVersion res = mock(DbVersion.class);
89     doNothing()
90       .doThrow(new RuntimeException("addStep should not be called twice"))
91       .when(res)
92       .addSteps(any(MigrationStepRegistry.class));
93     return res;
94   }
95 }