]> source.dussan.org Git - sonarqube.git/blob
90c192496ee5d32a2b765f37b6628382c0dc6b11
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.computation.task.projectanalysis.component;
21
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT;
26 import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.builder;
27
28 import java.util.Optional;
29 import javax.annotation.Nullable;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.ExpectedException;
33 import org.sonar.api.utils.System2;
34 import org.sonar.db.DbTester;
35 import org.sonar.db.component.BranchType;
36 import org.sonar.db.component.ComponentDto;
37 import org.sonar.db.component.ComponentTesting;
38 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
39 import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
40 import org.sonar.server.computation.task.projectanalysis.analysis.Project;
41 import org.sonar.server.computation.task.projectanalysis.component.Component;
42 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
43
44 public class BranchPersisterTest {
45   private final static Component MAIN = builder(PROJECT, 1).setUuid("PROJECT_UUID").setKey("PROJECT_KEY").build();
46   private final static Component BRANCH = builder(PROJECT, 1).setUuid("BRANCH_UUID").setKey("BRANCH_KEY").build();
47
48   @Rule
49   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
50   @Rule
51   public DbTester dbTester = DbTester.create(System2.INSTANCE);
52   @Rule
53   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
54   @Rule
55   public ExpectedException exception = ExpectedException.none();
56
57   BranchPersister underTest = new BranchPersister(dbTester.getDbClient(), System2.INSTANCE, treeRootHolder, analysisMetadataHolder);
58
59   @Test
60   public void fail_if_no_component_for_main_branches() {
61     analysisMetadataHolder.setBranch(createBranch(BranchType.LONG, true, null));
62     treeRootHolder.setRoot(MAIN);
63
64     exception.expect(IllegalStateException.class);
65     exception.expectMessage("Project has been deleted by end-user during analysis");
66
67     underTest.persist(dbTester.getSession());
68   }
69
70   @Test
71   public void skip_if_no_branch() {
72     analysisMetadataHolder.setBranch(null);
73     underTest.persist(dbTester.getSession());
74     dbTester.assertTableDoesNotExist("project_branch");
75   }
76
77   @Test
78   public void persist_secondary_branch() {
79     analysisMetadataHolder.setBranch(createBranch(BranchType.LONG, false, "branch"));
80     treeRootHolder.setRoot(BRANCH);
81
82     // add main branch in project table and in metadata
83     ComponentDto dto = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), MAIN.getUuid()).setDbKey(MAIN.getKey());
84     analysisMetadataHolder.setProject(Project.copyOf(dto));
85     dbTester.getDbClient().componentDao().insert(dbTester.getSession(), dto);
86
87     // this should add new columns in project and project_branches
88     underTest.persist(dbTester.getSession());
89
90     dbTester.getSession().commit();
91
92     assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(2);
93     assertThat(dbTester.countRowsOfTable("project_branches")).isEqualTo(1);
94
95   }
96
97   private static Branch createBranch(BranchType type, boolean isMain, @Nullable String name) {
98     Branch branch = mock(Branch.class);
99     when(branch.getType()).thenReturn(type);
100     when(branch.getName()).thenReturn(Optional.ofNullable(name));
101     when(branch.isMain()).thenReturn(isMain);
102     when(branch.getMergeBranchUuid()).thenReturn(Optional.empty());
103     return branch;
104   }
105 }