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