]> source.dussan.org Git - sonarqube.git/blob
81d9de50a897e599b4a90d0c772a1ecba30e5601
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 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.ce.task.projectanalysis.step;
21
22 import com.google.common.collect.ImmutableList;
23 import java.sql.SQLException;
24 import java.util.Arrays;
25 import java.util.Random;
26 import java.util.stream.IntStream;
27 import org.junit.Test;
28 import org.mockito.InOrder;
29 import org.mockito.Mockito;
30 import org.sonar.ce.task.projectanalysis.dbmigration.ProjectAnalysisDataChange;
31 import org.sonar.ce.task.projectanalysis.dbmigration.ProjectAnalysisDataChanges;
32 import org.sonar.ce.task.step.TestComputationStepContext;
33
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.assertj.core.api.Assertions.fail;
36 import static org.mockito.Mockito.doThrow;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.when;
39
40 public class DbMigrationsStepTest {
41   private ProjectAnalysisDataChanges projectAnalysisDataChanges = mock(ProjectAnalysisDataChanges.class);
42
43   private DbMigrationsStep underTest = new DbMigrationsStep(projectAnalysisDataChanges);
44
45   @Test
46   public void execute_has_no_effect_if_there_is_no_DataChange() {
47     underTest.execute(new TestComputationStepContext());
48   }
49
50   @Test
51   public void execute_calls_execute_on_DataChange_instances_in_order_provided_by_ProjectAnalysisDataChanges() {
52     ProjectAnalysisDataChange[] dataChanges = IntStream.range(0, 5 + new Random().nextInt(5))
53       .mapToObj(i -> mock(ProjectAnalysisDataChange.class))
54       .toArray(ProjectAnalysisDataChange[]::new);
55     InOrder inOrder = Mockito.inOrder(dataChanges);
56     when(projectAnalysisDataChanges.getDataChanges()).thenReturn(Arrays.asList(dataChanges));
57
58     underTest.execute(new TestComputationStepContext());
59
60     Arrays.stream(dataChanges).forEach(t -> {
61       try {
62         inOrder.verify(t).execute();
63       } catch (SQLException e) {
64         throw new RuntimeException("mock execute method throw an exception??!!??", e);
65       }
66     });
67   }
68
69   @Test
70   public void execute_stops_executing_and_throws_ISE_at_first_failing_DataChange() throws SQLException {
71     ProjectAnalysisDataChange okMock1 = mock(ProjectAnalysisDataChange.class);
72     ProjectAnalysisDataChange okMock2 = mock(ProjectAnalysisDataChange.class);
73     ProjectAnalysisDataChange failingMock1 = mock(ProjectAnalysisDataChange.class);
74     SQLException expected = new SQLException("Faiking DataChange throwing a SQLException");
75     doThrow(expected).when(failingMock1).execute();
76     ProjectAnalysisDataChange okMock3 = mock(ProjectAnalysisDataChange.class);
77     ProjectAnalysisDataChange failingMock2 = mock(ProjectAnalysisDataChange.class);
78     doThrow(new SQLException("Faiking another failing DataChange throwing a SQLException but which should never be thrown"))
79       .when(failingMock2)
80       .execute();
81     ProjectAnalysisDataChange okMock4 = mock(ProjectAnalysisDataChange.class);
82     InOrder inOrder = Mockito.inOrder(okMock1, okMock2, failingMock1, okMock3, failingMock2, okMock4);
83     when(projectAnalysisDataChanges.getDataChanges()).thenReturn(ImmutableList.of(
84       okMock1, okMock2, failingMock1, okMock3, failingMock2, okMock4));
85
86     try {
87       underTest.execute(new TestComputationStepContext());
88       fail("A IllegalStateException should have been thrown");
89     } catch (IllegalStateException e) {
90       assertThat(e)
91         .hasCause(expected);
92       inOrder.verify(okMock1).execute();
93       inOrder.verify(okMock2).execute();
94       inOrder.verify(failingMock1).execute();
95       inOrder.verifyNoMoreInteractions();
96     }
97   }
98
99   @Test
100   public void verify_description() {
101     assertThat(underTest.getDescription()).isNotEmpty();
102   }
103 }