You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RuleRepositoryDaoTest.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.db.rule;
  21. import java.util.Arrays;
  22. import java.util.Collection;
  23. import java.util.List;
  24. import java.util.stream.Collectors;
  25. import java.util.stream.IntStream;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.junit.rules.ExpectedException;
  29. import org.sonar.api.impl.utils.AlwaysIncreasingSystem2;
  30. import org.sonar.api.utils.System2;
  31. import org.sonar.db.DbSession;
  32. import org.sonar.db.DbTester;
  33. import static java.util.Arrays.asList;
  34. import static java.util.Collections.emptyList;
  35. import static org.assertj.core.api.Assertions.assertThat;
  36. public class RuleRepositoryDaoTest {
  37. private System2 system2 = new AlwaysIncreasingSystem2();
  38. @Rule
  39. public ExpectedException expectedException = ExpectedException.none();
  40. @Rule
  41. public DbTester dbTester = DbTester.create(system2);
  42. private RuleRepositoryDao underTest = new RuleRepositoryDao(system2);
  43. @Test
  44. public void insertOrUpdate_insert_rows_that_do_not_exist() {
  45. RuleRepositoryDto repo1 = new RuleRepositoryDto("findbugs", "java", "Findbugs");
  46. RuleRepositoryDto repo2 = new RuleRepositoryDto("sonarjava", "java", "SonarJava");
  47. RuleRepositoryDto repo3 = new RuleRepositoryDto("sonarcobol", "cobol", "SonarCobol");
  48. underTest.insertOrUpdate(dbTester.getSession(), asList(repo1, repo2, repo3));
  49. List<RuleRepositoryDto> rows = underTest.selectAll(dbTester.getSession());
  50. assertThat(rows).hasSize(3);
  51. // ordered by keys
  52. assertThat(rows.get(0)).isEqualToComparingFieldByField(repo1);
  53. assertThat(rows.get(1)).isEqualToComparingFieldByField(repo3);
  54. assertThat(rows.get(2)).isEqualToComparingFieldByField(repo2);
  55. assertThat(selectCreatedAtByKey(dbTester.getSession(), repo1.getKey()))
  56. .isEqualTo(selectCreatedAtByKey(dbTester.getSession(), repo2.getKey()))
  57. .isEqualTo(selectCreatedAtByKey(dbTester.getSession(), repo3.getKey()));
  58. }
  59. @Test
  60. public void insertOrUpdate_update_rows_that_exist() {
  61. RuleRepositoryDto repo1 = new RuleRepositoryDto("findbugs", "java", "Findbugs");
  62. RuleRepositoryDto repo2 = new RuleRepositoryDto("sonarjava", "java", "SonarJava");
  63. underTest.insertOrUpdate(dbTester.getSession(), asList(repo1, repo2));
  64. // update sonarjava, insert sonarcobol
  65. RuleRepositoryDto repo2bis = new RuleRepositoryDto("sonarjava", "java", "SonarJava");
  66. RuleRepositoryDto repo3 = new RuleRepositoryDto("sonarcobol", "cobol", "SonarCobol");
  67. underTest.insertOrUpdate(dbTester.getSession(), asList(repo2bis, repo3));
  68. List<RuleRepositoryDto> rows = underTest.selectAll(dbTester.getSession());
  69. assertThat(rows).hasSize(3);
  70. // ordered by keys
  71. assertThat(rows.get(0)).isEqualToComparingFieldByField(repo1);
  72. assertThat(rows.get(1)).isEqualToComparingFieldByField(repo3);
  73. assertThat(rows.get(2)).isEqualToComparingFieldByField(repo2bis);
  74. assertThat(selectCreatedAtByKey(dbTester.getSession(), repo1.getKey()))
  75. .isEqualTo(selectCreatedAtByKey(dbTester.getSession(), repo2.getKey()))
  76. .isLessThan(selectCreatedAtByKey(dbTester.getSession(), repo3.getKey()));
  77. }
  78. @Test
  79. public void deleteIfKeyNotIn() {
  80. RuleRepositoryDto repo1 = new RuleRepositoryDto("findbugs", "java", "Findbugs");
  81. RuleRepositoryDto repo2 = new RuleRepositoryDto("sonarjava", "java", "SonarJava");
  82. RuleRepositoryDto repo3 = new RuleRepositoryDto("sonarcobol", "cobol", "SonarCobol");
  83. underTest.insertOrUpdate(dbTester.getSession(), asList(repo1, repo2, repo3));
  84. underTest.deleteIfKeyNotIn(dbTester.getSession(), Arrays.asList(repo2.getKey(), "unknown"));
  85. assertThat(underTest.selectAll(dbTester.getSession()))
  86. .extracting(RuleRepositoryDto::getKey)
  87. .containsExactly(repo2.getKey());
  88. }
  89. @Test
  90. public void deleteIfKeyNotIn_truncates_table_if_keys_are_empty() {
  91. RuleRepositoryDto repo1 = new RuleRepositoryDto("findbugs", "java", "Findbugs");
  92. RuleRepositoryDto repo2 = new RuleRepositoryDto("sonarjava", "java", "SonarJava");
  93. underTest.insertOrUpdate(dbTester.getSession(), asList(repo1, repo2));
  94. underTest.deleteIfKeyNotIn(dbTester.getSession(), emptyList());
  95. assertThat(underTest.selectAll(dbTester.getSession())).isEmpty();
  96. }
  97. @Test
  98. public void deleteIfKeyNotIn_fails_if_more_than_1000_keys() {
  99. expectedException.expect(IllegalArgumentException.class);
  100. expectedException.expectMessage("too many rule repositories: 1100");
  101. Collection<String> keys = IntStream.range(0, 1_100).mapToObj(index -> "repo" + index).collect(Collectors.toSet());
  102. underTest.deleteIfKeyNotIn(dbTester.getSession(), keys);
  103. }
  104. @Test
  105. public void selectByLanguage() {
  106. DbSession dbSession = dbTester.getSession();
  107. RuleRepositoryDto dto1 = new RuleRepositoryDto("findbugs", "java", "Findbugs");
  108. RuleRepositoryDto dto2 = new RuleRepositoryDto("squid", "java", "Java");
  109. RuleRepositoryDto dto3 = new RuleRepositoryDto("cobol-lint", "cobol", "Cobol Lint");
  110. underTest.insertOrUpdate(dbSession, asList(dto1, dto2, dto3));
  111. assertThat(underTest.selectByLanguage(dbSession, "java")).extracting(RuleRepositoryDto::getKey)
  112. // ordered by key
  113. .containsExactly("findbugs", "squid");
  114. }
  115. @Test
  116. public void selectByLanguage_returns_empty_list_if_no_results() {
  117. DbSession dbSession = dbTester.getSession();
  118. RuleRepositoryDto dto1 = new RuleRepositoryDto("findbugs", "java", "Findbugs");
  119. underTest.insertOrUpdate(dbSession, asList(dto1));
  120. assertThat(underTest.selectByLanguage(dbSession, "missing")).hasSize(0);
  121. }
  122. private long selectCreatedAtByKey(DbSession dbSession, String key) {
  123. return (long) dbTester.selectFirst(dbSession, "select created_at as \"created_at\" from rule_repositories where kee='" + key + "'")
  124. .get("created_at");
  125. }
  126. }