]> source.dussan.org Git - sonarqube.git/blob
29433a7dc19d18095ab49660c8da7d6e1b7cda95
[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.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;
33
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.assertj.core.api.Assertions.tuple;
36
37 public class PopulateMainProjectBranchesTest {
38
39   private final static long PAST = 10_000_000_000L;
40   private final static long NOW = 50_000_000_000L;
41
42   @Rule
43   public ExpectedException expectedException = ExpectedException.none();
44
45   @Rule
46   public CoreDbTester db = CoreDbTester.createForSchema(PopulateMainProjectBranchesTest.class, "initial.sql");
47
48   private System2 system2 = new TestSystem2().setNow(NOW);
49   private PopulateMainProjectBranches underTest = new PopulateMainProjectBranches(db.database(), system2);
50
51   @Test
52   public void migrate() throws SQLException {
53     String project = insertProject();
54
55     underTest.execute();
56
57     assertProjectBranches(tuple("master", project, project, "BRANCH", "LONG", NOW, NOW));
58   }
59
60   @Test
61   public void does_nothing_on_non_projects() throws SQLException {
62     insertProject(null, "BRC");
63     insertProject(null, "VW");
64
65     underTest.execute();
66
67     assertThat(db.countRowsOfTable("project_branches")).isZero();
68   }
69
70   @Test
71   public void does_nothing_on_empty_table() throws SQLException {
72     underTest.execute();
73
74     assertThat(db.countRowsOfTable("project_branches")).isZero();
75   }
76
77   @Test
78   public void does_nothing_if_already_migrated() throws SQLException {
79     String project = insertProject();
80     insertMainBranch(project);
81
82     underTest.execute();
83
84     assertProjectBranches(tuple("master", project, project, "BRANCH", "LONG", PAST, PAST));
85   }
86
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")
89       .stream()
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);
93   }
94
95   private String insertProject() {
96     return insertProject(null, "PRJ");
97   }
98
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",
104       "UUID", uuid,
105       "PROJECT_UUID", uuid,
106       "main_branch_project_uuid", mainBranchUuid,
107       "UUID_PATH", ".",
108       "ROOT_UUID", uuid,
109       "PRIVATE", "true",
110       "qualifier", "TRK",
111       "scope", scope);
112     return uuid;
113   }
114
115   private void insertMainBranch(String uuid) {
116     db.executeInsert("PROJECT_BRANCHES",
117       "uuid", uuid,
118       "project_uuid", uuid,
119       "kee_type", "BRANCH",
120       "kee", "master",
121       "branch_type", "LONG",
122       "created_at", PAST,
123       "updated_at", PAST);
124   }
125 }