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 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;
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;
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();
49 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
51 public DbTester dbTester = DbTester.create(System2.INSTANCE);
53 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
55 public ExpectedException exception = ExpectedException.none();
57 BranchPersister underTest = new BranchPersister(dbTester.getDbClient(), System2.INSTANCE, treeRootHolder, analysisMetadataHolder);
60 public void fail_if_no_component_for_main_branches() {
61 analysisMetadataHolder.setBranch(createBranch(BranchType.LONG, true, null));
62 treeRootHolder.setRoot(MAIN);
64 exception.expect(IllegalStateException.class);
65 exception.expectMessage("Project has been deleted by end-user during analysis");
67 underTest.persist(dbTester.getSession());
71 public void skip_if_no_branch() {
72 analysisMetadataHolder.setBranch(null);
73 underTest.persist(dbTester.getSession());
74 dbTester.assertTableDoesNotExist("project_branch");
78 public void persist_secondary_branch() {
79 analysisMetadataHolder.setBranch(createBranch(BranchType.LONG, false, "branch"));
80 treeRootHolder.setRoot(BRANCH);
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);
87 // this should add new columns in project and project_branches
88 underTest.persist(dbTester.getSession());
90 dbTester.getSession().commit();
92 assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(2);
93 assertThat(dbTester.countRowsOfTable("project_branches")).isEqualTo(1);
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());