Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PopulateIsAdHocOnRulesTest.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.v74;
  21. import java.sql.SQLException;
  22. import java.util.stream.Collectors;
  23. import javax.annotation.Nullable;
  24. import org.assertj.core.groups.Tuple;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.junit.rules.ExpectedException;
  28. import org.sonar.api.impl.utils.TestSystem2;
  29. import org.sonar.api.utils.System2;
  30. import org.sonar.db.CoreDbTester;
  31. import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. import static org.assertj.core.api.Assertions.tuple;
  34. public class PopulateIsAdHocOnRulesTest {
  35. private final static long PAST = 10_000_000_000L;
  36. private final static long NOW = 50_000_000_000L;
  37. @Rule
  38. public ExpectedException expectedException = ExpectedException.none();
  39. @Rule
  40. public CoreDbTester db = CoreDbTester.createForSchema(PopulateIsAdHocOnRulesTest.class, "rules.sql");
  41. private System2 system2 = new TestSystem2().setNow(NOW);
  42. private PopulateIsAdHocOnRules underTest = new PopulateIsAdHocOnRules(db.database(), system2);
  43. @Test
  44. public void set_is_ad_hoc_to_true_on_external_rules() throws SQLException {
  45. insertRule(1, true, null);
  46. insertRule(2, true, null);
  47. underTest.execute();
  48. assertRules(
  49. tuple(1L, true, true, NOW),
  50. tuple(2L, true, true, NOW));
  51. }
  52. @Test
  53. public void set_is_ad_hoc_to_false_on_none_external_rules() throws SQLException {
  54. insertRule(1, false, null);
  55. insertRule(2, false, null);
  56. underTest.execute();
  57. assertRules(
  58. tuple(1L, false, false, NOW),
  59. tuple(2L, false, false, NOW));
  60. }
  61. @Test
  62. public void does_nothing_when_is_ad_hoc_is_already_set() throws SQLException {
  63. insertRule(1, true, true);
  64. insertRule(2, false, false);
  65. underTest.execute();
  66. assertRules(
  67. tuple(1L, true, true, PAST),
  68. tuple(2L, false, false, PAST));
  69. }
  70. @Test
  71. public void migration_is_re_entrant() throws SQLException {
  72. insertRule(1, true, null);
  73. insertRule(2, false, null);
  74. underTest.execute();
  75. underTest.execute();
  76. assertRules(
  77. tuple(1L, true, true, NOW),
  78. tuple(2L, false, false, NOW));
  79. }
  80. private void assertRules(Tuple... expectedTuples) {
  81. assertThat(db.select("SELECT ID, IS_EXTERNAL, IS_AD_HOC, UPDATED_AT FROM RULES")
  82. .stream()
  83. .map(row -> new Tuple(row.get("ID"), row.get("IS_EXTERNAL"), row.get("IS_AD_HOC"), row.get("UPDATED_AT")))
  84. .collect(Collectors.toList()))
  85. .containsExactlyInAnyOrder(expectedTuples);
  86. }
  87. private void insertRule(int id, boolean isEternal, @Nullable Boolean isAdHoc) {
  88. db.executeInsert("RULES",
  89. "ID", id,
  90. "IS_EXTERNAL", isEternal,
  91. "IS_AD_HOC", isAdHoc,
  92. "PLUGIN_RULE_KEY", randomAlphanumeric(3),
  93. "PLUGIN_NAME", randomAlphanumeric(3),
  94. "SCOPE", "MAIN",
  95. "CREATED_AT", PAST,
  96. "UPDATED_AT", PAST);
  97. }
  98. }