3 * Copyright (C) 2009-2019 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.v65;
22 import java.sql.SQLException;
24 import java.util.Random;
25 import java.util.stream.IntStream;
26 import javax.annotation.Nullable;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.sonar.api.utils.System2;
31 import org.sonar.api.utils.internal.AlwaysIncreasingSystem2;
32 import org.sonar.db.CoreDbTester;
34 import static java.lang.String.format;
35 import static org.assertj.core.api.Assertions.assertThat;
37 public class PopulateOrgQProfilesTest {
40 public ExpectedException expectedException = ExpectedException.none();
43 public CoreDbTester db = CoreDbTester.createForSchema(PopulateOrgQProfilesTest.class, "initial.sql");
45 private System2 system2 = new AlwaysIncreasingSystem2();
46 private PopulateOrgQProfiles underTest = new PopulateOrgQProfiles(db.database(), system2);
49 public void migration_is_reentrant() throws SQLException {
50 insertRulesProfile("ORG_1", "java", "u1", null, true, 1_000L, 1_100L);
51 insertRulesProfile("ORG_2", "js", "u2", "u1", true, 1_000L, 1_100L);
53 // org1 is already processed
54 insertOrgQProfile("u1", "ORG_1", "RPU1");
58 assertThat(countRows()).isEqualTo(2);
59 Map<String, Object> qprofile1 = selectOrgQProfile("u1", "ORG_1");
60 Map<String, Object> qprofile2 = selectOrgQProfile("u2", "ORG_2");
62 assertThat(qprofile1.get("UUID")).isEqualTo("u1");
63 assertThat(qprofile1.get("ORGANIZATION_UUID")).isEqualTo("ORG_1");
64 assertThat(qprofile1.get("RULES_PROFILE_UUID")).isEqualTo("RPU1"); // Ok if not overridden ?
65 assertThat(qprofile1.get("PARENT_UUID")).isNull();
67 assertThat(qprofile2.get("UUID")).isEqualTo("u2");
68 assertThat(qprofile2.get("ORGANIZATION_UUID")).isEqualTo("ORG_2");
69 assertThat(qprofile2.get("RULES_PROFILE_UUID")).isEqualTo("u2");
70 assertThat(qprofile2.get("PARENT_UUID")).isEqualTo("u1");
71 assertThat(qprofile2.get("LAST_USED")).isEqualTo(1_000L);
72 assertThat(qprofile2.get("USER_UPDATED_AT")).isEqualTo(1_100L);
76 public void migration_must_create_as_much_as_rules_profile() throws SQLException {
77 Random random = new Random();
78 int nbRulesProfile = 100 + random.nextInt(100);
79 IntStream.range(0, nbRulesProfile).forEach(
80 i -> insertRulesProfile("ORG_" + i, "java", "uuid" + i, random.nextBoolean() ? "ORG_" + random.nextInt(i + 1) : null, random.nextBoolean(), null, null));
84 assertThat(countRows()).isEqualTo(nbRulesProfile);
87 private int countRows() {
88 return db.countRowsOfTable("org_qprofiles");
91 private void insertRulesProfile(String orgUuid, String language, String uuid, String parentKee, boolean isDefault, @Nullable Long lastUsed, @Nullable Long userUpdatedAt) {
92 db.executeInsert("RULES_PROFILES",
93 "NAME", "name_" + uuid,
95 "ORGANIZATION_UUID", orgUuid,
96 "PARENT_KEE", parentKee,
98 "IS_DEFAULT", isDefault,
100 "LAST_USED", lastUsed,
101 "USER_UPDATED_AT", userUpdatedAt);
104 private void insertOrgQProfile(String uuid, String orgUuid, String rulesProfileUuid) {
105 db.executeInsert("ORG_QPROFILES",
106 "ORGANIZATION_UUID", orgUuid,
107 "RULES_PROFILE_UUID", rulesProfileUuid,
109 "CREATED_AT", system2.now(),
110 "UPDATED_AT", system2.now());
113 private Map<String, Object> selectOrgQProfile(String uuid, String orgUuid) {
114 return db.selectFirst(format("select * from org_qprofiles where uuid='%s' and organization_uuid='%s'", uuid, orgUuid));