3 * Copyright (C) 2009-2020 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.ce.task.projectanalysis.step;
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;
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;
40 public class DbMigrationsStepTest {
41 private ProjectAnalysisDataChanges projectAnalysisDataChanges = mock(ProjectAnalysisDataChanges.class);
43 private DbMigrationsStep underTest = new DbMigrationsStep(projectAnalysisDataChanges);
46 public void execute_has_no_effect_if_there_is_no_DataChange() {
47 underTest.execute(new TestComputationStepContext());
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));
58 underTest.execute(new TestComputationStepContext());
60 Arrays.stream(dataChanges).forEach(t -> {
62 inOrder.verify(t).execute();
63 } catch (SQLException e) {
64 throw new RuntimeException("mock execute method throw an exception??!!??", e);
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"))
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));
87 underTest.execute(new TestComputationStepContext());
88 fail("A IllegalStateException should have been thrown");
89 } catch (IllegalStateException e) {
92 inOrder.verify(okMock1).execute();
93 inOrder.verify(okMock2).execute();
94 inOrder.verify(failingMock1).execute();
95 inOrder.verifyNoMoreInteractions();
100 public void verify_description() {
101 assertThat(underTest.getDescription()).isNotEmpty();