]> source.dussan.org Git - sonarqube.git/blob
e8ee9bba79fa379989467586ae8ce82be046f85c
[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.platform.db.migration.version.v66;
21
22 import java.sql.SQLException;
23 import java.util.Map;
24 import javax.annotation.Nullable;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.sonar.api.utils.System2;
29 import org.sonar.api.utils.internal.AlwaysIncreasingSystem2;
30 import org.sonar.db.CoreDbTester;
31
32 import static org.assertj.core.api.Assertions.assertThat;
33
34 public class PopulateMainProjectBranchesTest {
35   @Rule
36   public ExpectedException expectedException = ExpectedException.none();
37
38   @Rule
39   public CoreDbTester db = CoreDbTester.createForSchema(PopulateMainProjectBranchesTest.class, "initial.sql");
40
41   private System2 system2 = new AlwaysIncreasingSystem2();
42   private PopulateMainProjectBranches underTest = new PopulateMainProjectBranches(db.database(), system2);
43
44   @Test
45   public void populate_with_existing_project() throws SQLException {
46     insertProject("project1");
47     insertProject("project2", "project1", "PRJ");
48     insertProject("project3", null, "XXX");
49
50     underTest.execute();
51
52     assertThat(db.countRowsOfTable("project_branches")).isEqualTo(1);
53
54     Map<String, Object> row = db.selectFirst("select * from project_branches");
55     assertThat(row.get("UUID")).isEqualTo("project1");
56     assertThat(row.get("PROJECT_UUID")).isEqualTo("project1");
57     assertThat(row.get("KEE")).isEqualTo(PopulateMainProjectBranches.NULL_KEY);
58     assertThat(row.get("BRANCH_TYPE")).isEqualTo("LONG");
59
60   }
61
62   @Test
63   public void do_nothing_if_no_project() throws SQLException {
64     insertProject("project2", "project1", "PRJ");
65     insertProject("project3", null, "XXX");
66
67     underTest.execute();
68     assertThat(db.countRowsOfTable("project_branches")).isEqualTo(0);
69   }
70
71   @Test
72   public void do_not_populate_if_already_exists() throws SQLException {
73     insertProject("project1");
74     insertBranch("project1");
75
76     assertThat(db.countRowsOfTable("project_branches")).isEqualTo(1);
77     underTest.execute();
78     assertThat(db.countRowsOfTable("project_branches")).isEqualTo(1);
79   }
80
81   private void insertProject(String uuid) {
82     insertProject(uuid, null, "PRJ");
83   }
84
85   private void insertProject(String uuid, @Nullable String mainBranchUuid, String scope) {
86     db.executeInsert("PROJECTS",
87       "ORGANIZATION_UUID", "default-org",
88       "KEE", uuid + "-key",
89       "UUID", uuid,
90       "PROJECT_UUID", uuid,
91       "main_branch_project_uuid", mainBranchUuid,
92       "UUID_PATH", ".",
93       "ROOT_UUID", uuid,
94       "PRIVATE", "true",
95       "qualifier", "TRK",
96       "scope", scope);
97   }
98
99   private void insertBranch(String uuid) {
100     db.executeInsert("PROJECT_BRANCHES",
101       "uuid", uuid,
102       "project_uuid", uuid,
103       "kee_type", "BRANCH",
104       "kee", PopulateMainProjectBranches.NULL_KEY,
105       "branch_type", "LONG",
106       "created_at", 0,
107       "updated_at", 0);
108   }
109 }