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.v79;
22 import java.sql.SQLException;
23 import java.util.HashMap;
24 import java.util.stream.Collectors;
25 import javax.annotation.Nullable;
26 import org.assertj.core.groups.Tuple;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.sonar.db.CoreDbTester;
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.assertj.core.api.Assertions.tuple;
34 public class MigrateVstsProviderToAzureDevOpsTest {
36 private final static String SELECT_PROPERTIES = "SELECT prop_key, is_empty, text_value, clob_value FROM properties";
39 public CoreDbTester db = CoreDbTester.createForSchema(MigrateVstsProviderToAzureDevOpsTest.class, "properties.sql");
41 private MigrateVstsProviderToAzureDevOps underTest = new MigrateVstsProviderToAzureDevOps(db.database());
44 public void migration_must_update_the_database() throws SQLException {
45 insertProperty("sonar.pullrequest.provider", "any.value_here", null, false);
46 insertProperty("sonar.pullrequest.provider", "VSTS", null, false);
47 insertProperty("sonar.pullrequest.provider", "VSTS / TFS", null, false);
48 insertProperty("whatever.property", "nothingspecial", null, false);
49 insertProperty("whatever.property", null, "nothing.special", false);
53 assertPropertyContainsInAnyOrder(
54 tuple("sonar.pullrequest.provider", "any.value_here", null, false),
55 tuple("sonar.pullrequest.provider", "Azure DevOps Services", null, false),
56 tuple("sonar.pullrequest.provider", "Azure DevOps Services/Server", null, false), // Single change
57 tuple("whatever.property", "nothingspecial", null, false),
58 tuple("whatever.property", null, "nothing.special", false)
63 public void migration_must_be_reentrant() throws SQLException {
64 insertProperty("sonar.pullrequest.provider", "any.value_here", null, false);
65 insertProperty("sonar.pullrequest.provider", "VSTS", null, false);
66 insertProperty("sonar.pullrequest.provider", "VSTS / TFS", null, false);
67 insertProperty("whatever.property", "nothingspecial", null, false);
68 insertProperty("whatever.property", null, "nothing.special", false);
73 assertPropertyContainsInAnyOrder(
74 tuple("sonar.pullrequest.provider", "any.value_here", null, false),
75 tuple("sonar.pullrequest.provider", "Azure DevOps Services", null, false),
76 tuple("sonar.pullrequest.provider", "Azure DevOps Services/Server", null, false), // Single change
77 tuple("whatever.property", "nothingspecial", null, false),
78 tuple("whatever.property", null, "nothing.special", false)
83 public void migration_is_doing_nothing_when_no_data() throws SQLException {
84 assertThat(db.countRowsOfTable("properties")).isEqualTo(0);
86 assertThat(db.countRowsOfTable("properties")).isEqualTo(0);
89 private void insertProperty(String propKey, @Nullable String textValue, @Nullable String clobValue, boolean isEmpty) {
90 HashMap<String, Object> map = new HashMap<>();
91 map.put("PROP_KEY", propKey);
92 map.put("TEXT_VALUE", textValue);
93 map.put("CLOB_VALUE", clobValue);
94 map.put("IS_EMPTY", isEmpty);
95 db.executeInsert("PROPERTIES", map);
98 private void assertPropertyContainsInAnyOrder(Tuple... tuples) {
99 assertThat(db.select(SELECT_PROPERTIES)
101 .map(p -> new Tuple(p.get("PROP_KEY"), p.get("TEXT_VALUE"), p.get("CLOB_VALUE"), p.get("IS_EMPTY")))
102 .collect(Collectors.toList()))
103 .containsExactlyInAnyOrder(tuples);