]> source.dussan.org Git - sonarqube.git/blob
92b0c04112cb8a807011045b079dbbc0c15f9530
[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.ce.task.projectanalysis.step;
21
22 import org.junit.Test;
23 import org.sonar.api.utils.System2;
24 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
25 import org.sonar.ce.task.projectanalysis.component.BranchPersister;
26 import org.sonar.ce.task.projectanalysis.component.Component;
27 import org.sonar.ce.task.projectanalysis.component.MutableDisabledComponentsHolder;
28 import org.sonar.ce.task.projectanalysis.component.ProjectPersister;
29 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
30 import org.sonar.ce.task.step.TestComputationStepContext;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.component.ComponentDao;
34
35 import static java.util.Collections.emptyList;
36 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
37 import static org.assertj.core.api.Assertions.assertThatThrownBy;
38 import static org.mockito.ArgumentMatchers.any;
39 import static org.mockito.ArgumentMatchers.eq;
40 import static org.mockito.Mockito.doReturn;
41 import static org.mockito.Mockito.mock;
42
43 public class PersistComponentsStepTest {
44
45   @Test
46   public void should_fail_if_project_is_not_stored_in_database_yet() {
47     TreeRootHolder treeRootHolder = mock(TreeRootHolder.class);
48     Component component = mock(Component.class);
49     DbClient dbClient = mock(DbClient.class);
50     ComponentDao componentDao = mock(ComponentDao.class);
51     String projectKey = randomAlphabetic(20);
52
53     doReturn(component).when(treeRootHolder).getRoot();
54     doReturn(projectKey).when(component).getDbKey();
55     doReturn(componentDao).when(dbClient).componentDao();
56     doReturn(emptyList()).when(componentDao).selectAllComponentsFromProjectKey(any(DbSession.class), eq(projectKey));
57
58     assertThatThrownBy(() -> {
59       new PersistComponentsStep(
60         dbClient,
61         treeRootHolder,
62         System2.INSTANCE,
63         mock(MutableDisabledComponentsHolder.class),
64         mock(AnalysisMetadataHolder.class),
65         mock(BranchPersister.class),
66         mock(ProjectPersister.class)).execute(new TestComputationStepContext());
67     })
68       .isInstanceOf(IllegalStateException.class)
69       .hasMessageContaining("The project '" + projectKey + "' is not stored in the database, during a project analysis");
70   }
71 }