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.v63;
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.db.CoreDbTester;
30 import static org.assertj.core.api.Assertions.assertThat;
32 public class PopulateDefaultPermTemplateColumnsOfOrganizationsTest {
33 private static final String DEFAULT_TEMPLATE_PROPERTY = "sonar.permission.template.default";
34 private static final String DEFAULT_PROJECT_TEMPLATE_PROPERTY = "sonar.permission.template.TRK.default";
35 private static final String DEFAULT_VIEW_TEMPLATE_PROPERTY = "sonar.permission.template.VW.default";
36 private static final String DEFAULT_DEV_TEMPLATE_PROPERTY = "sonar.permission.template.DEV.default";
37 private static final String DEFAULT_ORGANIZATION_UUID = "def org uuid";
40 public CoreDbTester dbTester = CoreDbTester.createForSchema(PopulateDefaultPermTemplateColumnsOfOrganizationsTest.class, "properties_and_organizations.sql");
42 public ExpectedException expectedException = ExpectedException.none();
44 private PopulateDefaultPermTemplateColumnsOfOrganizations underTest = new PopulateDefaultPermTemplateColumnsOfOrganizations(dbTester.database(),
45 new DefaultOrganizationUuidProviderImpl());
48 public void fails_with_ISE_when_no_default_organization_is_set() throws SQLException {
49 expectedException.expect(IllegalStateException.class);
50 expectedException.expectMessage("Default organization uuid is missing");
56 public void fails_with_ISE_when_default_organization_does_not_exist_in_table_ORGANIZATIONS() throws SQLException {
57 insertDefaultOrganizationUuid("blabla");
59 expectedException.expect(IllegalStateException.class);
60 expectedException.expectMessage("Default organization with uuid 'blabla' does not exist in table ORGANIZATIONS");
66 public void fails_with_ISE_when_more_than_one_organization_exist() throws SQLException {
67 setupDefaultOrganization();
69 insertOrganization("other orga uuid");
71 expectedException.expect(IllegalStateException.class);
72 expectedException.expectMessage("Can not migrate DB if more than one organization exists. " +
73 "Remove all organizations but the default one which uuid is '" + DEFAULT_ORGANIZATION_UUID + "'");
79 public void do_nothing_if_global_default_template_property_does_not_exist() throws SQLException {
80 setupDefaultOrganization();
84 verifyTemplateColumns(null, null);
85 verifyPropertiesDoNotExist();
89 public void execute_sets_project_perm_template_when_global_default_template_is_defined_in_property() throws SQLException {
90 setupDefaultOrganization();
91 insertProperty(DEFAULT_TEMPLATE_PROPERTY, "foo");
95 verifyTemplateColumns("foo", null);
96 verifyPropertiesDoNotExist();
100 public void execute_sets_project_perm_template_from_project_default_template_property_over_global_property() throws SQLException {
101 setupDefaultOrganization();
102 insertProperty(DEFAULT_TEMPLATE_PROPERTY, "foo");
103 insertProperty(DEFAULT_PROJECT_TEMPLATE_PROPERTY, "bar");
107 verifyTemplateColumns("bar", null);
108 verifyPropertiesDoNotExist();
112 public void execute_sets_project_perm_template_from_global_property_and_view_perm_template_from_view_property() throws SQLException {
113 setupDefaultOrganization();
114 insertProperty(DEFAULT_TEMPLATE_PROPERTY, "foo");
115 insertProperty(DEFAULT_VIEW_TEMPLATE_PROPERTY, "bar");
119 verifyTemplateColumns("foo", "bar");
120 verifyPropertiesDoNotExist();
124 public void execute_sets_project_from_project_property_and_view_from_view_property_when_all_properties_are_defined() throws SQLException {
125 setupDefaultOrganization();
126 insertProperty(DEFAULT_TEMPLATE_PROPERTY, "foo");
127 insertProperty(DEFAULT_PROJECT_TEMPLATE_PROPERTY, "bar");
128 insertProperty(DEFAULT_VIEW_TEMPLATE_PROPERTY, "doh");
132 verifyTemplateColumns("bar", "doh");
133 verifyPropertiesDoNotExist();
137 public void execute_deletes_dev_property_when_it_is_defined() throws SQLException {
138 setupDefaultOrganization();
139 insertProperty(DEFAULT_TEMPLATE_PROPERTY, "foo");
140 insertProperty(DEFAULT_DEV_TEMPLATE_PROPERTY, "bar");
144 verifyPropertiesDoNotExist();
147 private void verifyTemplateColumns(@Nullable String project, @Nullable String view) {
148 Map<String, Object> row = dbTester.selectFirst("select " +
149 " default_perm_template_project as \"projectDefaultPermTemplate\"," +
150 " default_perm_template_view as \"viewDefaultPermTemplate\"" +
151 " from organizations where uuid='" + DEFAULT_ORGANIZATION_UUID + "'");
152 assertThat(row.get("projectDefaultPermTemplate")).isEqualTo(project);
153 assertThat(row.get("viewDefaultPermTemplate")).isEqualTo(view);
156 private void verifyPropertiesDoNotExist() {
157 verifyPropertyDoesNotExist(DEFAULT_TEMPLATE_PROPERTY);
158 verifyPropertyDoesNotExist(DEFAULT_PROJECT_TEMPLATE_PROPERTY);
159 verifyPropertyDoesNotExist(DEFAULT_VIEW_TEMPLATE_PROPERTY);
160 verifyPropertyDoesNotExist(DEFAULT_DEV_TEMPLATE_PROPERTY);
163 private void verifyPropertyDoesNotExist(String globalPermissionTemplateDefault) {
164 assertThat(dbTester.countSql("select count(*) as \"cnt\" from PROPERTIES where prop_key='" + globalPermissionTemplateDefault + "'"))
169 public void migration_is_reentrant() throws SQLException {
170 setupDefaultOrganization();
171 insertProperty(DEFAULT_TEMPLATE_PROPERTY, "foo");
178 private void setupDefaultOrganization() {
179 insertDefaultOrganizationUuid(DEFAULT_ORGANIZATION_UUID);
180 insertOrganization(DEFAULT_ORGANIZATION_UUID);
183 private void insertOrganization(String uuid) {
184 dbTester.executeInsert(
189 "CREATED_AT", "1000",
190 "UPDATED_AT", "1000");
193 private void insertDefaultOrganizationUuid(String defaultOrganizationUuid) {
194 dbTester.executeInsert(
195 "INTERNAL_PROPERTIES",
196 "KEE", "organization.default",
198 "TEXT_VALUE", defaultOrganizationUuid);
201 private void insertProperty(String key, @Nullable String value) {
202 dbTester.executeInsert(
205 "IS_EMPTY", String.valueOf(value == null),
206 "TEXT_VALUE", value);