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;
23 import java.util.stream.Collectors;
24 import javax.annotation.Nullable;
25 import org.assertj.core.groups.Tuple;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.sonar.api.utils.System2;
30 import org.sonar.api.utils.internal.TestSystem2;
31 import org.sonar.core.util.Uuids;
32 import org.sonar.db.CoreDbTester;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.assertj.core.api.Assertions.tuple;
37 public class PopulateMainProjectBranchesTest {
39 private final static long PAST = 10_000_000_000L;
40 private final static long NOW = 50_000_000_000L;
43 public ExpectedException expectedException = ExpectedException.none();
46 public CoreDbTester db = CoreDbTester.createForSchema(PopulateMainProjectBranchesTest.class, "initial.sql");
48 private System2 system2 = new TestSystem2().setNow(NOW);
49 private PopulateMainProjectBranches underTest = new PopulateMainProjectBranches(db.database(), system2);
52 public void migrate() throws SQLException {
53 String project = insertProject();
57 assertProjectBranches(tuple("master", project, project, "BRANCH", "LONG", NOW, NOW));
61 public void does_nothing_on_non_projects() throws SQLException {
62 insertProject(null, "BRC");
63 insertProject(null, "VW");
67 assertThat(db.countRowsOfTable("project_branches")).isZero();
71 public void does_nothing_on_empty_table() throws SQLException {
74 assertThat(db.countRowsOfTable("project_branches")).isZero();
78 public void does_nothing_if_already_migrated() throws SQLException {
79 String project = insertProject();
80 insertMainBranch(project);
84 assertProjectBranches(tuple("master", project, project, "BRANCH", "LONG", PAST, PAST));
87 private void assertProjectBranches(Tuple... expectedTuples) {
88 assertThat(db.select("SELECT KEE, UUID, PROJECT_UUID, KEE_TYPE, BRANCH_TYPE, CREATED_AT, UPDATED_AT FROM PROJECT_BRANCHES")
90 .map(map -> new Tuple(map.get("KEE"), map.get("UUID"), map.get("PROJECT_UUID"), map.get("KEE_TYPE"), map.get("BRANCH_TYPE"), map.get("CREATED_AT"), map.get("UPDATED_AT")))
91 .collect(Collectors.toList()))
92 .containsExactlyInAnyOrder(expectedTuples);
95 private String insertProject() {
96 return insertProject(null, "PRJ");
99 private String insertProject(@Nullable String mainBranchUuid, String scope) {
100 String uuid = Uuids.createFast();
101 db.executeInsert("PROJECTS",
102 "ORGANIZATION_UUID", "default-org",
103 "KEE", uuid + "-key",
105 "PROJECT_UUID", uuid,
106 "main_branch_project_uuid", mainBranchUuid,
115 private void insertMainBranch(String uuid) {
116 db.executeInsert("PROJECT_BRANCHES",
118 "project_uuid", uuid,
119 "kee_type", "BRANCH",
121 "branch_type", "LONG",