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.

QProfileChangeDaoTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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.qualityprofile;
  21. import java.util.List;
  22. import java.util.Map;
  23. import javax.annotation.Nullable;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.junit.rules.ExpectedException;
  27. import org.sonar.api.impl.utils.AlwaysIncreasingSystem2;
  28. import org.sonar.api.utils.System2;
  29. import org.sonar.core.util.SequenceUuidFactory;
  30. import org.sonar.core.util.UuidFactory;
  31. import org.sonar.db.DbSession;
  32. import org.sonar.db.DbTester;
  33. import static java.util.Arrays.asList;
  34. import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
  35. public class QProfileChangeDaoTest {
  36. private System2 system2 = new AlwaysIncreasingSystem2();
  37. @Rule
  38. public ExpectedException expectedException = ExpectedException.none();
  39. @Rule
  40. public DbTester db = DbTester.create(system2);
  41. private DbSession dbSession = db.getSession();
  42. private UuidFactory uuidFactory = new SequenceUuidFactory();
  43. private QProfileChangeDao underTest = new QProfileChangeDao(system2, uuidFactory);
  44. @Test
  45. public void insert() {
  46. QProfileChangeDto dto = insertChange("P1", "ACTIVATED", "marcel_uuid", "some_data");
  47. verifyInserted(dto);
  48. }
  49. /**
  50. * user_login and data can be null
  51. */
  52. @Test
  53. public void test_insert_with_null_fields() {
  54. QProfileChangeDto dto = insertChange("P1", "ACTIVATED", null, null);
  55. verifyInserted(dto);
  56. }
  57. private void verifyInserted(QProfileChangeDto dto) {
  58. QProfileChangeDto reloaded = selectChangeByUuid(dto.getUuid());
  59. assertThat(reloaded.getUuid()).isEqualTo(dto.getUuid());
  60. assertThat(reloaded.getChangeType()).isEqualTo(dto.getChangeType());
  61. assertThat(reloaded.getData()).isEqualTo(dto.getData());
  62. assertThat(reloaded.getUserUuid()).isEqualTo(dto.getUserUuid());
  63. assertThat(reloaded.getRulesProfileUuid()).isEqualTo(dto.getRulesProfileUuid());
  64. assertThat(reloaded.getCreatedAt()).isPositive();
  65. }
  66. @Test
  67. public void insert_throws_ISE_if_date_is_already_set() {
  68. expectedException.expect(IllegalStateException.class);
  69. expectedException.expectMessage("Date of QProfileChangeDto must be set by DAO only. Got 123.");
  70. underTest.insert(dbSession, new QProfileChangeDto().setCreatedAt(123L));
  71. }
  72. @Test
  73. public void selectByQuery_returns_empty_list_if_profile_does_not_exist() {
  74. List<QProfileChangeDto> changes = underTest.selectByQuery(dbSession, new QProfileChangeQuery("P1"));
  75. assertThat(changes).isEmpty();
  76. }
  77. @Test
  78. public void selectByQuery_returns_changes_ordered_by_descending_date() {
  79. QProfileDto profile1 = db.qualityProfiles().insert(db.getDefaultOrganization());
  80. QProfileDto profile2 = db.qualityProfiles().insert(db.getDefaultOrganization());
  81. QProfileChangeDto change1OnP1 = insertChange(profile1, "ACTIVATED", null, null);
  82. QProfileChangeDto change2OnP1 = insertChange(profile1, "ACTIVATED", null, null);
  83. QProfileChangeDto changeOnP2 = insertChange(profile2, "ACTIVATED", null, null);
  84. List<QProfileChangeDto> changes = underTest.selectByQuery(dbSession, new QProfileChangeQuery(profile1.getKee()));
  85. assertThat(changes)
  86. .extracting(QProfileChangeDto::getUuid)
  87. .containsExactly(change2OnP1.getUuid(), change1OnP1.getUuid());
  88. }
  89. @Test
  90. public void selectByQuery_supports_pagination_of_changes() {
  91. QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization());
  92. QProfileChangeDto change1 = insertChange(profile, "ACTIVATED", null, null);
  93. QProfileChangeDto change2 = insertChange(profile, "ACTIVATED", null, null);
  94. QProfileChangeDto change3 = insertChange(profile, "ACTIVATED", null, null);
  95. QProfileChangeDto change4 = insertChange(profile, "ACTIVATED", null, null);
  96. QProfileChangeQuery query = new QProfileChangeQuery(profile.getKee());
  97. query.setOffset(2);
  98. query.setLimit(1);
  99. List<QProfileChangeDto> changes = underTest.selectByQuery(dbSession, query);
  100. assertThat(changes)
  101. .extracting(QProfileChangeDto::getUuid)
  102. .containsExactly(change2.getUuid());
  103. }
  104. @Test
  105. public void selectByQuery_returns_changes_after_given_date() {
  106. QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization());
  107. QProfileChangeDto change1 = insertChange(profile, "ACTIVATED", null, null);
  108. QProfileChangeDto change2 = insertChange(profile, "ACTIVATED", null, null);
  109. QProfileChangeDto change3 = insertChange(profile, "ACTIVATED", null, null);
  110. QProfileChangeQuery query = new QProfileChangeQuery(profile.getKee());
  111. query.setFromIncluded(change1.getCreatedAt() + 1);
  112. assertThat(underTest.selectByQuery(dbSession, query))
  113. .extracting(QProfileChangeDto::getUuid)
  114. .containsExactly(change3.getUuid(), change2.getUuid());
  115. }
  116. @Test
  117. public void selectByQuery_returns_changes_before_given_date() {
  118. QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization());
  119. QProfileChangeDto change1 = insertChange(profile, "ACTIVATED", null, null);
  120. QProfileChangeDto change2 = insertChange(profile, "ACTIVATED", null, null);
  121. QProfileChangeDto change3 = insertChange(profile, "ACTIVATED", null, null);
  122. QProfileChangeQuery query = new QProfileChangeQuery(profile.getKee());
  123. query.setToExcluded(change2.getCreatedAt() + 1);
  124. assertThat(underTest.selectByQuery(dbSession, query))
  125. .extracting(QProfileChangeDto::getUuid)
  126. .containsExactly(change2.getUuid(), change1.getUuid());
  127. }
  128. @Test
  129. public void selectByQuery_returns_changes_in_a_range_of_dates() {
  130. QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization());
  131. QProfileChangeDto change1 = insertChange(profile, "ACTIVATED", null, null);
  132. QProfileChangeDto change2 = insertChange(profile, "ACTIVATED", null, null);
  133. QProfileChangeDto change3 = insertChange(profile, "ACTIVATED", null, null);
  134. QProfileChangeDto change4 = insertChange(profile, "ACTIVATED", null, null);
  135. QProfileChangeQuery query = new QProfileChangeQuery(profile.getKee());
  136. query.setFromIncluded(change1.getCreatedAt() + 1);
  137. query.setToExcluded(change4.getCreatedAt());
  138. assertThat(underTest.selectByQuery(dbSession, query))
  139. .extracting(QProfileChangeDto::getUuid)
  140. .containsExactly(change3.getUuid(), change2.getUuid());
  141. }
  142. @Test
  143. public void test_selectByQuery_mapping() {
  144. QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization());
  145. QProfileChangeDto inserted = insertChange(profile, "ACTIVATED", "theLogin", "theData");
  146. List<QProfileChangeDto> result = underTest.selectByQuery(dbSession, new QProfileChangeQuery(profile.getKee()));
  147. assertThat(result).hasSize(1);
  148. QProfileChangeDto change = result.get(0);
  149. assertThat(change.getRulesProfileUuid()).isEqualTo(inserted.getRulesProfileUuid());
  150. assertThat(change.getUserUuid()).isEqualTo(inserted.getUserUuid());
  151. assertThat(change.getData()).isEqualTo(inserted.getData());
  152. assertThat(change.getChangeType()).isEqualTo(inserted.getChangeType());
  153. assertThat(change.getUuid()).isEqualTo(inserted.getUuid());
  154. assertThat(change.getCreatedAt()).isEqualTo(inserted.getCreatedAt());
  155. }
  156. @Test
  157. public void countByQuery() {
  158. QProfileDto profile1 = db.qualityProfiles().insert(db.getDefaultOrganization());
  159. QProfileDto profile2 = db.qualityProfiles().insert(db.getDefaultOrganization());
  160. long start = system2.now();
  161. insertChange(profile1, "ACTIVATED", null, null);
  162. insertChange(profile1, "ACTIVATED", null, null);
  163. insertChange(profile2, "ACTIVATED", null, null);
  164. long end = system2.now();
  165. assertThat(underTest.countByQuery(dbSession, new QProfileChangeQuery(profile1.getKee()))).isEqualTo(2);
  166. assertThat(underTest.countByQuery(dbSession, new QProfileChangeQuery(profile2.getKee()))).isEqualTo(1);
  167. assertThat(underTest.countByQuery(dbSession, new QProfileChangeQuery("does_not_exist"))).isEqualTo(0);
  168. QProfileChangeQuery query = new QProfileChangeQuery(profile1.getKee());
  169. query.setToExcluded(start);
  170. assertThat(underTest.countByQuery(dbSession, query)).isEqualTo(0);
  171. QProfileChangeQuery query2 = new QProfileChangeQuery(profile1.getKee());
  172. query2.setToExcluded(end);
  173. assertThat(underTest.countByQuery(dbSession, query2)).isEqualTo(2);
  174. }
  175. @Test
  176. public void deleteByRulesProfileUuids() {
  177. QProfileDto profile1 = db.qualityProfiles().insert(db.getDefaultOrganization());
  178. QProfileDto profile2 = db.qualityProfiles().insert(db.getDefaultOrganization());
  179. insertChange(profile1, "ACTIVATED", null, null);
  180. insertChange(profile1, "ACTIVATED", null, null);
  181. insertChange(profile2, "ACTIVATED", null, null);
  182. underTest.deleteByRulesProfileUuids(dbSession, asList(profile1.getRulesProfileUuid()));
  183. assertThat(underTest.countByQuery(dbSession, new QProfileChangeQuery(profile1.getKee()))).isEqualTo(0);
  184. assertThat(underTest.countByQuery(dbSession, new QProfileChangeQuery(profile2.getKee()))).isEqualTo(1);
  185. }
  186. @Test
  187. public void deleteByProfileKeys_does_nothing_if_row_with_specified_key_does_not_exist() {
  188. QProfileDto profile1 = db.qualityProfiles().insert(db.getDefaultOrganization());
  189. insertChange(profile1.getRulesProfileUuid(), "ACTIVATED", null, null);
  190. underTest.deleteByRulesProfileUuids(dbSession, asList("does not exist"));
  191. assertThat(underTest.countByQuery(dbSession, new QProfileChangeQuery(profile1.getKee()))).isEqualTo(1);
  192. }
  193. private QProfileChangeDto insertChange(QProfileDto profile, String type, @Nullable String login, @Nullable String data) {
  194. return insertChange(profile.getRulesProfileUuid(), type, login, data);
  195. }
  196. private QProfileChangeDto insertChange(String rulesProfileUuid, String type, @Nullable String userUuid, @Nullable String data) {
  197. QProfileChangeDto dto = new QProfileChangeDto()
  198. .setRulesProfileUuid(rulesProfileUuid)
  199. .setUserUuid(userUuid)
  200. .setChangeType(type)
  201. .setData(data);
  202. underTest.insert(dbSession, dto);
  203. return dto;
  204. }
  205. private QProfileChangeDto selectChangeByUuid(String uuid) {
  206. Map<String, Object> map = db.selectFirst(dbSession,
  207. "select kee as \"uuid\", rules_profile_uuid as \"rulesProfileUuid\", created_at as \"createdAt\", user_uuid as \"userUuid\", change_type as \"changeType\", change_data as \"changeData\" from qprofile_changes where kee='"
  208. + uuid + "'");
  209. return new QProfileChangeDto()
  210. .setUuid((String) map.get("uuid"))
  211. .setRulesProfileUuid((String) map.get("rulesProfileUuid"))
  212. .setCreatedAt((long) map.get("createdAt"))
  213. .setUserUuid((String) map.get("userUuid"))
  214. .setChangeType((String) map.get("changeType"))
  215. .setData((String) map.get("changeData"));
  216. }
  217. }