3 * Copyright (C) 2009-2020 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.v81;
22 import java.sql.SQLException;
23 import java.util.stream.Collectors;
24 import org.junit.Before;
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.db.CoreDbTester;
31 import static org.assertj.core.api.Assertions.assertThat;
33 public class MigrateSlbsAndLlbsToCommonTypeTest {
35 private static final String BRANCHES_TABLE = "project_branches";
36 private static final String MAIN_BRANCH_1 = "825644c3-06b2-44b5-833e-13169a5379ad";
37 private static final String MAIN_BRANCH_2 = "47a53ace-0297-4a4e-bcab-07a65e3eeac4";
38 private static final String PR_1 = "ad85f2ee-7271-407a-b9f7-b0a80343e001";
39 private static final String LLB_1 = "08905714-2bfd-48a6-b19d-7801bc4d1ca4";
40 private static final String SLB_1 = "a2020607-4134-45f9-87ea-2cdaa84bf386";
41 private static final String PR_2 = "834eba05-e4f7-4214-bfec-c7823e101487";
42 private static final String LLB_2 = "fea036fe-e830-4d87-85ee-7aaa21729019";
43 private static final String SLB_2 = "20166755-953e-4b8e-8c1d-67d812e42418";
46 public CoreDbTester dbTester = CoreDbTester.createForSchema(MigrateSlbsAndLlbsToCommonTypeTest.class, "schema.sql");
49 public ExpectedException expectedException = ExpectedException.none();
51 private MigrateSlbsAndLlbsToCommonType underTest = new MigrateSlbsAndLlbsToCommonType(dbTester.database(), System2.INSTANCE);
55 insertMainBranch(MAIN_BRANCH_1);
56 insertBranch(MAIN_BRANCH_1, PR_1, "PULL_REQUEST");
57 insertBranch(MAIN_BRANCH_1, LLB_1, "LONG");
58 insertBranch(MAIN_BRANCH_1, SLB_1, "SHORT");
60 insertMainBranch(MAIN_BRANCH_2);
61 insertBranch(MAIN_BRANCH_1, PR_2, "PULL_REQUEST");
62 insertBranch(MAIN_BRANCH_1, LLB_2, "LONG");
63 insertBranch(MAIN_BRANCH_1, SLB_2, "SHORT");
67 public void execute() throws SQLException {
70 verifyMigrationResult();
74 public void migration_is_re_entrant() throws SQLException {
78 verifyMigrationResult();
81 private void verifyMigrationResult() {
82 assertThat(dbTester.countRowsOfTable(BRANCHES_TABLE)).isEqualTo(8);
83 assertThat(dbTester.countSql("select count(*) from project_branches where branch_type = 'LONG' or branch_type = 'SHORT'")).isEqualTo(0);
84 assertThat(dbTester.select("select uuid from " + BRANCHES_TABLE + " where branch_type = 'BRANCH'")
86 .map(e -> e.get("UUID"))
87 .collect(Collectors.toSet())).containsExactlyInAnyOrder(MAIN_BRANCH_1, MAIN_BRANCH_2, SLB_1, SLB_2, LLB_1, LLB_2);
88 assertThat(dbTester.select("select uuid from " + BRANCHES_TABLE + " where branch_type = 'PULL_REQUEST'")
90 .map(e -> e.get("UUID"))
91 .collect(Collectors.toSet())).containsExactlyInAnyOrder(PR_1, PR_2);
94 private void insertBranch(String mainBranchUuid, String uuid, String type) {
95 dbTester.executeInsert(
98 "PROJECT_UUID", mainBranchUuid,
99 "KEE", "pb-key-" + uuid,
102 "MERGE_BRANCH_UUID", "mb-uuid-" + mainBranchUuid,
103 "MANUAL_BASELINE_ANALYSIS_UUID", null,
104 "CREATED_AT", System2.INSTANCE.now(),
105 "UPDATED_AT", System2.INSTANCE.now());
108 private void insertMainBranch(String mainBranchUuid) {
109 insertBranch(mainBranchUuid, mainBranchUuid, "LONG");