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.platform.db.migration.version.v66;
22 import java.sql.SQLException;
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;
32 import static org.assertj.core.api.Assertions.assertThat;
34 public class PopulateMainProjectBranchesTest {
36 public ExpectedException expectedException = ExpectedException.none();
39 public CoreDbTester db = CoreDbTester.createForSchema(PopulateMainProjectBranchesTest.class, "initial.sql");
41 private System2 system2 = new AlwaysIncreasingSystem2();
42 private PopulateMainProjectBranches underTest = new PopulateMainProjectBranches(db.database(), system2);
45 public void populate_with_existing_project() throws SQLException {
46 insertProject("project1");
47 insertProject("project2", "project1", "PRJ");
48 insertProject("project3", null, "XXX");
52 assertThat(db.countRowsOfTable("project_branches")).isEqualTo(1);
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");
63 public void do_nothing_if_no_project() throws SQLException {
64 insertProject("project2", "project1", "PRJ");
65 insertProject("project3", null, "XXX");
68 assertThat(db.countRowsOfTable("project_branches")).isEqualTo(0);
72 public void do_not_populate_if_already_exists() throws SQLException {
73 insertProject("project1");
74 insertBranch("project1");
76 assertThat(db.countRowsOfTable("project_branches")).isEqualTo(1);
78 assertThat(db.countRowsOfTable("project_branches")).isEqualTo(1);
81 private void insertProject(String uuid) {
82 insertProject(uuid, null, "PRJ");
85 private void insertProject(String uuid, @Nullable String mainBranchUuid, String scope) {
86 db.executeInsert("PROJECTS",
87 "ORGANIZATION_UUID", "default-org",
91 "main_branch_project_uuid", mainBranchUuid,
99 private void insertBranch(String uuid) {
100 db.executeInsert("PROJECT_BRANCHES",
102 "project_uuid", uuid,
103 "kee_type", "BRANCH",
104 "kee", PopulateMainProjectBranches.NULL_KEY,
105 "branch_type", "LONG",