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.

PropertiesDaoTest.java 55KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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.property;
  21. import com.google.common.collect.ImmutableMap;
  22. import com.tngtech.java.junit.dataprovider.DataProvider;
  23. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  24. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  25. import java.util.Arrays;
  26. import java.util.Iterator;
  27. import java.util.LinkedHashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.Set;
  31. import java.util.function.Consumer;
  32. import javax.annotation.Nullable;
  33. import org.junit.Rule;
  34. import org.junit.Test;
  35. import org.junit.rules.ExpectedException;
  36. import org.junit.runner.RunWith;
  37. import org.sonar.api.impl.utils.AlwaysIncreasingSystem2;
  38. import org.sonar.api.web.UserRole;
  39. import org.sonar.db.DbClient;
  40. import org.sonar.db.DbSession;
  41. import org.sonar.db.DbTester;
  42. import org.sonar.db.EmailSubscriberDto;
  43. import org.sonar.db.component.ComponentDto;
  44. import org.sonar.db.component.ComponentTesting;
  45. import org.sonar.db.organization.OrganizationDto;
  46. import org.sonar.db.user.UserDto;
  47. import static com.google.common.collect.ImmutableSet.of;
  48. import static com.google.common.collect.Lists.newArrayList;
  49. import static com.google.common.collect.Sets.newHashSet;
  50. import static java.util.Collections.singletonList;
  51. import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
  52. import static org.assertj.core.api.Assertions.assertThat;
  53. import static org.assertj.core.groups.Tuple.tuple;
  54. import static org.sonar.db.property.PropertyTesting.newComponentPropertyDto;
  55. import static org.sonar.db.property.PropertyTesting.newGlobalPropertyDto;
  56. import static org.sonar.db.property.PropertyTesting.newPropertyDto;
  57. import static org.sonar.db.property.PropertyTesting.newUserPropertyDto;
  58. @RunWith(DataProviderRunner.class)
  59. public class PropertiesDaoTest {
  60. private static final String VALUE_SIZE_4000 = String.format("%1$4000.4000s", "*");
  61. private static final String VALUE_SIZE_4001 = VALUE_SIZE_4000 + "P";
  62. private static final long INITIAL_DATE = 1_444_000L;
  63. private AlwaysIncreasingSystem2 system2 = new AlwaysIncreasingSystem2(INITIAL_DATE, 1);
  64. @Rule
  65. public ExpectedException thrown = ExpectedException.none();
  66. @Rule
  67. public DbTester db = DbTester.create(system2);
  68. private DbClient dbClient = db.getDbClient();
  69. private DbSession session = db.getSession();
  70. private PropertiesDao underTest = db.getDbClient().propertiesDao();
  71. @Test
  72. public void shouldFindUsersForNotification() {
  73. ComponentDto project1 = insertPrivateProject("uuid_45");
  74. ComponentDto project2 = insertPrivateProject("uuid_56");
  75. UserDto user1 = db.users().insertUser(u -> u.setLogin("user1"));
  76. UserDto user2 = db.users().insertUser(u -> u.setLogin("user2"));
  77. UserDto user3 = db.users().insertUser(u -> u.setLogin("user3"));
  78. insertProperty("notification.NewViolations.Email", "true", project1.getId(), user2.getId());
  79. insertProperty("notification.NewViolations.Twitter", "true", null, user3.getId());
  80. insertProperty("notification.NewViolations.Twitter", "true", project2.getId(), user1.getId());
  81. insertProperty("notification.NewViolations.Twitter", "true", project1.getId(), user2.getId());
  82. insertProperty("notification.NewViolations.Twitter", "true", project2.getId(), user3.getId());
  83. db.users().insertProjectPermissionOnUser(user2, UserRole.USER, project1);
  84. db.users().insertProjectPermissionOnUser(user3, UserRole.USER, project2);
  85. db.users().insertProjectPermissionOnUser(user1, UserRole.USER, project2);
  86. assertThat(underTest.findUsersForNotification("NewViolations", "Email", null))
  87. .isEmpty();
  88. assertThat(underTest.findUsersForNotification("NewViolations", "Email", "uuid_78"))
  89. .isEmpty();
  90. assertThat(underTest.findUsersForNotification("NewViolations", "Email", project1.getKey()))
  91. .containsOnly(new Subscriber("user2", false));
  92. assertThat(underTest.findUsersForNotification("NewViolations", "Email", project2.getKey()))
  93. .isEmpty();
  94. assertThat(underTest.findUsersForNotification("NewViolations", "Twitter", null))
  95. .containsOnly(new Subscriber("user3", true));
  96. assertThat(underTest.findUsersForNotification("NewViolations", "Twitter", "uuid_78"))
  97. .containsOnly(new Subscriber("user3", true));
  98. assertThat(underTest.findUsersForNotification("NewViolations", "Twitter", project1.getKey()))
  99. .containsOnly(new Subscriber("user2", false), new Subscriber("user3", true));
  100. assertThat(underTest.findUsersForNotification("NewViolations", "Twitter", project2.getKey()))
  101. .containsOnly(new Subscriber("user1", false), new Subscriber("user3", true), new Subscriber("user3", false));
  102. }
  103. @Test
  104. public void hasNotificationSubscribers() {
  105. int userId1 = db.users().insertUser(u -> u.setLogin("user1")).getId();
  106. int userId2 = db.users().insertUser(u -> u.setLogin("user2")).getId();
  107. String projectUuid = randomAlphabetic(8);
  108. Long projectId = db.components().insertPrivateProject(db.getDefaultOrganization(), projectUuid).getId();
  109. // global subscription
  110. insertProperty("notification.DispatcherWithGlobalSubscribers.Email", "true", null, userId2);
  111. // project subscription
  112. insertProperty("notification.DispatcherWithProjectSubscribers.Email", "true", projectId, userId1);
  113. insertProperty("notification.DispatcherWithGlobalAndProjectSubscribers.Email", "true", 56L, userId1);
  114. insertProperty("notification.DispatcherWithGlobalAndProjectSubscribers.Email", "true", projectId, userId1);
  115. // global subscription
  116. insertProperty("notification.DispatcherWithGlobalAndProjectSubscribers.Email", "true", null, userId2);
  117. // Nobody is subscribed
  118. assertThat(underTest.hasProjectNotificationSubscribersForDispatchers(projectUuid, singletonList("NotSexyDispatcher")))
  119. .isFalse();
  120. // Global subscribers
  121. assertThat(underTest.hasProjectNotificationSubscribersForDispatchers(projectUuid, singletonList("DispatcherWithGlobalSubscribers")))
  122. .isTrue();
  123. // Project subscribers
  124. assertThat(underTest.hasProjectNotificationSubscribersForDispatchers(projectUuid, singletonList("DispatcherWithProjectSubscribers")))
  125. .isTrue();
  126. assertThat(underTest.hasProjectNotificationSubscribersForDispatchers("PROJECT_B", singletonList("DispatcherWithProjectSubscribers")))
  127. .isFalse();
  128. // Global + Project subscribers
  129. assertThat(underTest.hasProjectNotificationSubscribersForDispatchers(projectUuid, singletonList("DispatcherWithGlobalAndProjectSubscribers")))
  130. .isTrue();
  131. assertThat(underTest.hasProjectNotificationSubscribersForDispatchers("PROJECT_B", singletonList("DispatcherWithGlobalAndProjectSubscribers")))
  132. .isTrue();
  133. }
  134. @Test
  135. public void findEmailRecipientsForNotification_returns_empty_on_empty_properties_table() {
  136. db.users().insertUser();
  137. String dispatcherKey = randomAlphabetic(5);
  138. String channelKey = randomAlphabetic(6);
  139. String projectKey = randomAlphabetic(7);
  140. Set<EmailSubscriberDto> subscribers = underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, projectKey);
  141. assertThat(subscribers).isEmpty();
  142. }
  143. @Test
  144. public void findEmailRecipientsForNotification_with_logins_returns_empty_on_empty_properties_table() {
  145. db.users().insertUser();
  146. String dispatcherKey = randomAlphabetic(5);
  147. String channelKey = randomAlphabetic(6);
  148. String projectKey = randomAlphabetic(7);
  149. Set<String> logins = of("user1", "user2");
  150. Set<EmailSubscriberDto> subscribers = underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, projectKey, logins);
  151. assertThat(subscribers).isEmpty();
  152. }
  153. @Test
  154. public void findEmailRecipientsForNotification_finds_only_globally_subscribed_users_if_projectKey_is_null() {
  155. int userId1 = db.users().insertUser(withEmail("user1")).getId();
  156. int userId2 = db.users().insertUser(withEmail("user2")).getId();
  157. int userId3 = db.users().insertUser(withEmail("user3")).getId();
  158. int userId4 = db.users().insertUser(withEmail("user4")).getId();
  159. long projectId = insertPrivateProject("PROJECT_A").getId();
  160. String dispatcherKey = randomAlphabetic(5);
  161. String otherDispatcherKey = randomAlphabetic(6);
  162. String channelKey = randomAlphabetic(7);
  163. String otherChannelKey = randomAlphabetic(8);
  164. // user1 subscribed only globally
  165. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userId1);
  166. // user2 subscribed on project and globally
  167. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userId2);
  168. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectId, userId2);
  169. // user3 subscribed on project only
  170. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectId, userId3);
  171. // user4 did not subscribe
  172. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "false", projectId, userId4);
  173. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, null))
  174. .containsOnly(EmailSubscriberDto.create("user1", true, emailOf("user1")), EmailSubscriberDto.create("user2", true, emailOf("user2")));
  175. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, otherChannelKey, null))
  176. .isEmpty();
  177. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), otherDispatcherKey, channelKey, null))
  178. .isEmpty();
  179. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), channelKey, dispatcherKey, null))
  180. .isEmpty();
  181. }
  182. @Test
  183. public void findEmailRecipientsForNotification_with_logins_finds_only_globally_subscribed_specified_users_if_projectKey_is_null() {
  184. int userId1 = db.users().insertUser(withEmail("user1")).getId();
  185. int userId2 = db.users().insertUser(withEmail("user2")).getId();
  186. int userId3 = db.users().insertUser(withEmail("user3")).getId();
  187. int userId4 = db.users().insertUser(withEmail("user4")).getId();
  188. long projectId = insertPrivateProject("PROJECT_A").getId();
  189. String dispatcherKey = randomAlphabetic(5);
  190. String otherDispatcherKey = randomAlphabetic(6);
  191. String channelKey = randomAlphabetic(7);
  192. String otherChannelKey = randomAlphabetic(8);
  193. // user1 subscribed only globally
  194. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userId1);
  195. // user2 subscribed on project and globally
  196. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userId2);
  197. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectId, userId2);
  198. // user3 subscribed on project only
  199. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectId, userId3);
  200. // user4 did not subscribe
  201. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "false", projectId, userId4);
  202. Set<String> allLogins = of("user1", "user2", "user3", "user4");
  203. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, null, allLogins))
  204. .containsOnly(EmailSubscriberDto.create("user1", true, emailOf("user1")), EmailSubscriberDto.create("user2", true, emailOf("user2")));
  205. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, null, of("user1", "user2")))
  206. .containsOnly(EmailSubscriberDto.create("user1", true, emailOf("user1")), EmailSubscriberDto.create("user2", true, emailOf("user2")));
  207. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, null, of("user2")))
  208. .containsOnly(EmailSubscriberDto.create("user2", true, emailOf("user2")));
  209. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, null, of("user1")))
  210. .containsOnly(EmailSubscriberDto.create("user1", true, emailOf("user1")));
  211. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, null, of()))
  212. .isEmpty();
  213. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, otherChannelKey, null, allLogins))
  214. .isEmpty();
  215. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), otherDispatcherKey, channelKey, null, allLogins))
  216. .isEmpty();
  217. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), channelKey, dispatcherKey, null, allLogins))
  218. .isEmpty();
  219. }
  220. @Test
  221. public void findEmailRecipientsForNotification_finds_global_and_project_subscribed_users_when_projectKey_is_non_null() {
  222. int userId1 = db.users().insertUser(withEmail("user1")).getId();
  223. int userId2 = db.users().insertUser(withEmail("user2")).getId();
  224. int userId3 = db.users().insertUser(withEmail("user3")).getId();
  225. int userId4 = db.users().insertUser(withEmail("user4")).getId();
  226. String projectKey = randomAlphabetic(3);
  227. String otherProjectKey = randomAlphabetic(4);
  228. long projectId = insertPrivateProject(projectKey).getId();
  229. String dispatcherKey = randomAlphabetic(5);
  230. String otherDispatcherKey = randomAlphabetic(6);
  231. String channelKey = randomAlphabetic(7);
  232. String otherChannelKey = randomAlphabetic(8);
  233. // user1 subscribed only globally
  234. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userId1);
  235. // user2 subscribed on project and globally
  236. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userId2);
  237. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectId, userId2);
  238. // user3 subscribed on project only
  239. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectId, userId3);
  240. // user4 did not subscribe
  241. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "false", projectId, userId4);
  242. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, projectKey))
  243. .containsOnly(
  244. EmailSubscriberDto.create("user1", true, emailOf("user1")),
  245. EmailSubscriberDto.create("user2", true, emailOf("user2")), EmailSubscriberDto.create("user2", false, "user2@foo"),
  246. EmailSubscriberDto.create("user3", false, emailOf("user3")));
  247. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, otherProjectKey))
  248. .containsOnly(
  249. EmailSubscriberDto.create("user1", true, emailOf("user1")),
  250. EmailSubscriberDto.create("user2", true, emailOf("user2")));
  251. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, otherChannelKey, otherProjectKey))
  252. .isEmpty();
  253. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), otherDispatcherKey, channelKey, otherProjectKey))
  254. .isEmpty();
  255. }
  256. @Test
  257. public void findEmailRecipientsForNotification_with_logins_finds_global_and_project_subscribed_specified_users_when_projectKey_is_non_null() {
  258. int userId1 = db.users().insertUser(withEmail("user1")).getId();
  259. int userId2 = db.users().insertUser(withEmail("user2")).getId();
  260. int userId3 = db.users().insertUser(withEmail("user3")).getId();
  261. int userId4 = db.users().insertUser(withEmail("user4")).getId();
  262. String projectKey = randomAlphabetic(3);
  263. String otherProjectKey = randomAlphabetic(4);
  264. long projectId = insertPrivateProject(projectKey).getId();
  265. String dispatcherKey = randomAlphabetic(5);
  266. String otherDispatcherKey = randomAlphabetic(6);
  267. String channelKey = randomAlphabetic(7);
  268. String otherChannelKey = randomAlphabetic(8);
  269. // user1 subscribed only globally
  270. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userId1);
  271. // user2 subscribed on project and globally
  272. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userId2);
  273. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectId, userId2);
  274. // user3 subscribed on project only
  275. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectId, userId3);
  276. // user4 did not subscribe
  277. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "false", projectId, userId4);
  278. Set<String> allLogins = of("user1", "user2", "user3", "user4");
  279. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, projectKey, allLogins))
  280. .containsOnly(
  281. EmailSubscriberDto.create("user1", true, emailOf("user1")),
  282. EmailSubscriberDto.create("user2", true, emailOf("user2")), EmailSubscriberDto.create("user2", false, "user2@foo"),
  283. EmailSubscriberDto.create("user3", false, emailOf("user3")));
  284. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, projectKey, of("user1")))
  285. .containsOnly(
  286. EmailSubscriberDto.create("user1", true, emailOf("user1")));
  287. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, projectKey, of("user2")))
  288. .containsOnly(
  289. EmailSubscriberDto.create("user2", true, emailOf("user2")), EmailSubscriberDto.create("user2", false, "user2@foo"));
  290. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, projectKey, of("user3")))
  291. .containsOnly(EmailSubscriberDto.create("user3", false, emailOf("user3")));
  292. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, projectKey, of()))
  293. .isEmpty();
  294. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, otherProjectKey, allLogins))
  295. .containsOnly(
  296. EmailSubscriberDto.create("user1", true, emailOf("user1")),
  297. EmailSubscriberDto.create("user2", true, emailOf("user2")));
  298. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, otherChannelKey, otherProjectKey, allLogins))
  299. .isEmpty();
  300. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), otherDispatcherKey, channelKey, otherProjectKey, allLogins))
  301. .isEmpty();
  302. }
  303. @Test
  304. public void findEmailRecipientsForNotification_ignores_subscribed_users_without_email() {
  305. int userId1 = db.users().insertUser(withEmail("user1")).getId();
  306. int userId2 = db.users().insertUser(noEmail("user2")).getId();
  307. int userId3 = db.users().insertUser(withEmail("user3")).getId();
  308. int userId4 = db.users().insertUser(noEmail("user4")).getId();
  309. String projectKey = randomAlphabetic(3);
  310. long projectId = insertPrivateProject(projectKey).getId();
  311. String dispatcherKey = randomAlphabetic(4);
  312. String channelKey = randomAlphabetic(5);
  313. // user1 and user2 subscribed on project and globally
  314. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userId1);
  315. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectId, userId1);
  316. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userId2);
  317. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectId, userId2);
  318. // user3 and user4 subscribed only globally
  319. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userId3);
  320. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userId4);
  321. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, projectKey))
  322. .containsOnly(
  323. EmailSubscriberDto.create("user1", true, emailOf("user1")), EmailSubscriberDto.create("user1", false, emailOf("user1")),
  324. EmailSubscriberDto.create("user3", true, emailOf("user3")));
  325. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, null))
  326. .containsOnly(
  327. EmailSubscriberDto.create("user1", true, emailOf("user1")),
  328. EmailSubscriberDto.create("user3", true, emailOf("user3")));
  329. }
  330. @Test
  331. public void findEmailRecipientsForNotification_with_logins_ignores_subscribed_users_without_email() {
  332. int userId1 = db.users().insertUser(withEmail("user1")).getId();
  333. int userId2 = db.users().insertUser(noEmail("user2")).getId();
  334. int userId3 = db.users().insertUser(withEmail("user3")).getId();
  335. int userId4 = db.users().insertUser(noEmail("user4")).getId();
  336. Set<String> allLogins = of("user1", "user2", "user3");
  337. String projectKey = randomAlphabetic(3);
  338. long projectId = insertPrivateProject(projectKey).getId();
  339. String dispatcherKey = randomAlphabetic(4);
  340. String channelKey = randomAlphabetic(5);
  341. // user1 and user2 subscribed on project and globally
  342. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userId1);
  343. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectId, userId1);
  344. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userId2);
  345. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", projectId, userId2);
  346. // user3 and user4 subscribed only globally
  347. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userId3);
  348. insertProperty(propertyKeyOf(dispatcherKey, channelKey), "true", null, userId4);
  349. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, projectKey, allLogins))
  350. .containsOnly(
  351. EmailSubscriberDto.create("user1", true, emailOf("user1")), EmailSubscriberDto.create("user1", false, emailOf("user1")),
  352. EmailSubscriberDto.create("user3", true, emailOf("user3")));
  353. assertThat(underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, null, allLogins))
  354. .containsOnly(
  355. EmailSubscriberDto.create("user1", true, emailOf("user1")),
  356. EmailSubscriberDto.create("user3", true, emailOf("user3")));
  357. }
  358. @Test
  359. public void selectGlobalProperties() {
  360. // global
  361. long id1 = insertProperty("global.one", "one", null, null);
  362. long id2 = insertProperty("global.two", "two", null, null);
  363. List<PropertyDto> properties = underTest.selectGlobalProperties();
  364. assertThat(properties.size())
  365. .isEqualTo(2);
  366. assertThatDto(findByKey(properties, "global.one"))
  367. .hasKey("global.one")
  368. .hasNoUserId()
  369. .hasNoResourceId()
  370. .hasValue("one");
  371. assertThatDto(findByKey(properties, "global.two"))
  372. .hasKey("global.two")
  373. .hasNoResourceId()
  374. .hasNoUserId()
  375. .hasValue("two");
  376. }
  377. @Test
  378. @UseDataProvider("allValuesForSelect")
  379. public void selectGlobalProperties_supports_all_values(String dbValue, String expected) {
  380. insertProperty("global.one", dbValue, null, null);
  381. List<PropertyDto> dtos = underTest.selectGlobalProperties();
  382. assertThat(dtos)
  383. .hasSize(1);
  384. assertThatDto(dtos.iterator().next())
  385. .hasKey("global.one")
  386. .hasNoResourceId()
  387. .hasNoUserId()
  388. .hasValue(expected);
  389. }
  390. @Test
  391. public void selectGlobalProperty() {
  392. // global
  393. insertProperty("global.one", "one", null, null);
  394. insertProperty("global.two", "two", null, null);
  395. // project
  396. insertProperty("project.one", "one", 10L, null);
  397. // user
  398. insertProperty("user.one", "one", null, 100);
  399. assertThatDto(underTest.selectGlobalProperty("global.one"))
  400. .hasNoResourceId()
  401. .hasNoUserId()
  402. .hasValue("one");
  403. assertThat(underTest.selectGlobalProperty("project.one")).isNull();
  404. assertThat(underTest.selectGlobalProperty("user.one")).isNull();
  405. assertThat(underTest.selectGlobalProperty("unexisting")).isNull();
  406. }
  407. @Test
  408. @UseDataProvider("allValuesForSelect")
  409. public void selectGlobalProperty_supports_all_values(String dbValue, String expected) {
  410. insertProperty("global.one", dbValue, null, null);
  411. assertThatDto(underTest.selectGlobalProperty("global.one"))
  412. .hasNoResourceId()
  413. .hasNoUserId()
  414. .hasValue(expected);
  415. }
  416. @Test
  417. public void selectProjectProperties() {
  418. ComponentDto projectDto = insertPrivateProject("A");
  419. long projectId = projectDto.getId();
  420. // global
  421. insertProperty("global.one", "one", null, null);
  422. insertProperty("global.two", "two", null, null);
  423. // project
  424. insertProperty("project.one", "Pone", projectId, null);
  425. insertProperty("project.two", "Ptwo", projectId, null);
  426. List<PropertyDto> dtos = underTest.selectProjectProperties(projectDto.getDbKey());
  427. assertThat(dtos)
  428. .hasSize(2);
  429. assertThatDto(findByKey(dtos, "project.one"))
  430. .hasKey("project.one")
  431. .hasResourceId(projectId)
  432. .hasValue("Pone");
  433. assertThatDto(findByKey(dtos, "project.two"))
  434. .hasKey("project.two")
  435. .hasResourceId(projectId)
  436. .hasValue("Ptwo");
  437. }
  438. @Test
  439. @UseDataProvider("allValuesForSelect")
  440. public void selectProjectProperties_supports_all_values(String dbValue, String expected) {
  441. ComponentDto projectDto = insertPrivateProject("A");
  442. insertProperty("project.one", dbValue, projectDto.getId(), null);
  443. List<PropertyDto> dtos = underTest.selectProjectProperties(projectDto.getDbKey());
  444. assertThat(dtos).hasSize(1);
  445. assertThatDto(dtos.iterator().next())
  446. .hasKey("project.one")
  447. .hasResourceId(projectDto.getId())
  448. .hasValue(expected);
  449. }
  450. @DataProvider
  451. public static Object[][] allValuesForSelect() {
  452. return new Object[][]{
  453. {null, ""},
  454. {"", ""},
  455. {"some value", "some value"},
  456. {VALUE_SIZE_4000, VALUE_SIZE_4000},
  457. {VALUE_SIZE_4001, VALUE_SIZE_4001}
  458. };
  459. }
  460. @Test
  461. public void selectProjectProperty() {
  462. insertProperty("project.one", "one", 10L, null);
  463. PropertyDto property = underTest.selectProjectProperty(10L, "project.one");
  464. assertThatDto(property)
  465. .hasKey("project.one")
  466. .hasResourceId(10L)
  467. .hasNoUserId()
  468. .hasValue("one");
  469. }
  470. @Test
  471. public void select_by_query() {
  472. // global
  473. insertProperty("global.one", "one", null, null);
  474. insertProperty("global.two", "two", null, null);
  475. // struts
  476. insertProperty("struts.one", "one", 10L, null);
  477. // commons
  478. insertProperty("commonslang.one", "one", 11L, null);
  479. // user
  480. insertProperty("user.one", "one", null, 100);
  481. insertProperty("user.two", "two", 10L, 100);
  482. // other
  483. insertProperty("other.one", "one", 12L, null);
  484. List<PropertyDto> results = underTest.selectByQuery(PropertyQuery.builder().setKey("user.two").setComponentId(10L).setUserId(100).build(), db.getSession());
  485. assertThat(results).hasSize(1);
  486. assertThat(results.get(0).getValue()).isEqualTo("two");
  487. results = underTest.selectByQuery(PropertyQuery.builder().setKey("user.one").setUserId(100).build(), db.getSession());
  488. assertThat(results).hasSize(1);
  489. assertThat(results.get(0).getValue()).isEqualTo("one");
  490. }
  491. @Test
  492. public void select_global_properties_by_keys() {
  493. insertPrivateProject("A");
  494. int userId = db.users().insertUser(u -> u.setLogin("B")).getId();
  495. String key = "key";
  496. String anotherKey = "anotherKey";
  497. insertProperty(key, "value", null, null);
  498. insertProperty(key, "value", 10L, null);
  499. insertProperty(key, "value", null, userId);
  500. insertProperty(anotherKey, "value", null, null);
  501. insertProperty("key1", "value", null, null);
  502. insertProperty("key2", "value", null, null);
  503. insertProperty("key3", "value", null, null);
  504. assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet(key)))
  505. .extracting("key")
  506. .containsExactly(key);
  507. assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet(key, anotherKey)))
  508. .extracting("key")
  509. .containsExactly(key, anotherKey);
  510. assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet(key, anotherKey, "unknown")))
  511. .extracting("key")
  512. .containsExactly(key, anotherKey);
  513. assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet("key2", "key1", "key3")))
  514. .extracting("key")
  515. .containsExactly("key1", "key2", "key3");
  516. assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet("unknown")))
  517. .isEmpty();
  518. }
  519. @Test
  520. public void select_component_properties_by_ids() {
  521. ComponentDto project = db.components().insertPrivateProject();
  522. ComponentDto project2 = db.components().insertPrivateProject();
  523. UserDto user = db.users().insertUser();
  524. String key = "key";
  525. String anotherKey = "anotherKey";
  526. insertProperties(
  527. newGlobalPropertyDto().setKey(key),
  528. newComponentPropertyDto(project).setKey(key),
  529. newComponentPropertyDto(project2).setKey(key),
  530. newComponentPropertyDto(project2).setKey(anotherKey),
  531. newUserPropertyDto(user).setKey(key));
  532. assertThat(underTest.selectPropertiesByComponentIds(session, newHashSet(project.getId())))
  533. .extracting("key", "resourceId").containsOnly(tuple(key, project.getId()));
  534. assertThat(underTest.selectPropertiesByComponentIds(session, newHashSet(project.getId(), project2.getId())))
  535. .extracting("key", "resourceId").containsOnly(
  536. tuple(key, project.getId()),
  537. tuple(key, project2.getId()),
  538. tuple(anotherKey, project2.getId()));
  539. assertThat(underTest.selectPropertiesByComponentIds(session, newHashSet(123456789L))).isEmpty();
  540. }
  541. @Test
  542. public void select_properties_by_keys_and_component_ids() {
  543. ComponentDto project = db.components().insertPrivateProject();
  544. ComponentDto project2 = db.components().insertPrivateProject();
  545. UserDto user = db.users().insertUser();
  546. String key = "key";
  547. String anotherKey = "anotherKey";
  548. insertProperties(
  549. newGlobalPropertyDto().setKey(key),
  550. newComponentPropertyDto(project).setKey(key),
  551. newComponentPropertyDto(project2).setKey(key),
  552. newComponentPropertyDto(project2).setKey(anotherKey),
  553. newUserPropertyDto(user).setKey(key));
  554. assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet(key), newHashSet(project.getId())))
  555. .extracting("key", "resourceId").containsOnly(tuple(key, project.getId()));
  556. assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet(key), newHashSet(project.getId(), project2.getId())))
  557. .extracting("key", "resourceId").containsOnly(
  558. tuple(key, project.getId()),
  559. tuple(key, project2.getId()));
  560. assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet(key, anotherKey), newHashSet(project.getId(), project2.getId())))
  561. .extracting("key", "resourceId").containsOnly(
  562. tuple(key, project.getId()),
  563. tuple(key, project2.getId()),
  564. tuple(anotherKey, project2.getId()));
  565. assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet("unknown"), newHashSet(project.getId()))).isEmpty();
  566. assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet("key"), newHashSet(123456789L))).isEmpty();
  567. assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet("unknown"), newHashSet(123456789L))).isEmpty();
  568. }
  569. @Test
  570. public void select_by_key_and_matching_value() {
  571. ComponentDto project1 = db.components().insertPrivateProject();
  572. ComponentDto project2 = db.components().insertPrivateProject();
  573. db.properties().insertProperties(
  574. newComponentPropertyDto("key", "value", project1),
  575. newComponentPropertyDto("key", "value", project2),
  576. newGlobalPropertyDto("key", "value"),
  577. newComponentPropertyDto("another key", "value", project1));
  578. assertThat(underTest.selectByKeyAndMatchingValue(db.getSession(), "key", "value"))
  579. .extracting(PropertyDto::getValue, PropertyDto::getResourceId)
  580. .containsExactlyInAnyOrder(
  581. tuple("value", project1.getId()),
  582. tuple("value", project2.getId()),
  583. tuple("value", null));
  584. }
  585. @Test
  586. public void selectByKeyAndUserIdAndComponentQualifier() {
  587. UserDto user1 = db.users().insertUser();
  588. UserDto user2 = db.users().insertUser();
  589. ComponentDto project1 = db.components().insertPrivateProject();
  590. ComponentDto file1 = db.components().insertComponent(ComponentTesting.newFileDto(project1));
  591. ComponentDto project2 = db.components().insertPrivateProject();
  592. db.properties().insertProperties(
  593. newPropertyDto("key", "1", project1, user1),
  594. newPropertyDto("key", "2", project2, user1),
  595. newPropertyDto("key", "3", file1, user1),
  596. newPropertyDto("another key", "4", project1, user1),
  597. newPropertyDto("key", "5", project1, user2),
  598. newGlobalPropertyDto("key", "global"));
  599. assertThat(underTest.selectByKeyAndUserIdAndComponentQualifier(db.getSession(), "key", user1.getId(), "TRK"))
  600. .extracting(PropertyDto::getValue).containsExactlyInAnyOrder("1", "2");
  601. assertThat(underTest.selectByKeyAndUserIdAndComponentQualifier(db.getSession(), "key", user1.getId(), "FIL"))
  602. .extracting(PropertyDto::getValue).containsExactlyInAnyOrder("3");
  603. assertThat(underTest.selectByKeyAndUserIdAndComponentQualifier(db.getSession(), "key", user2.getId(), "FIL")).isEmpty();
  604. }
  605. @Test
  606. public void saveProperty_inserts_global_properties_when_they_do_not_exist_in_db() {
  607. underTest.saveProperty(new PropertyDto().setKey("global.null").setValue(null));
  608. underTest.saveProperty(new PropertyDto().setKey("global.empty").setValue(""));
  609. underTest.saveProperty(new PropertyDto().setKey("global.text").setValue("some text"));
  610. underTest.saveProperty(new PropertyDto().setKey("global.4000").setValue(VALUE_SIZE_4000));
  611. underTest.saveProperty(new PropertyDto().setKey("global.clob").setValue(VALUE_SIZE_4001));
  612. assertThatPropertiesRow("global.null")
  613. .hasNoResourceId()
  614. .hasNoUserId()
  615. .isEmpty()
  616. .hasCreatedAt(INITIAL_DATE + 2);
  617. assertThatPropertiesRow("global.empty")
  618. .hasNoResourceId()
  619. .hasNoUserId()
  620. .isEmpty()
  621. .hasCreatedAt(INITIAL_DATE + 3);
  622. assertThatPropertiesRow("global.text")
  623. .hasNoResourceId()
  624. .hasNoUserId()
  625. .hasTextValue("some text")
  626. .hasCreatedAt(INITIAL_DATE + 4);
  627. assertThatPropertiesRow("global.4000")
  628. .hasNoResourceId()
  629. .hasNoUserId()
  630. .hasTextValue(VALUE_SIZE_4000)
  631. .hasCreatedAt(INITIAL_DATE + 5);
  632. assertThatPropertiesRow("global.clob")
  633. .hasNoResourceId()
  634. .hasNoUserId()
  635. .hasClobValue(VALUE_SIZE_4001)
  636. .hasCreatedAt(INITIAL_DATE + 6);
  637. }
  638. @Test
  639. public void saveProperty_inserts_component_properties_when_they_do_not_exist_in_db() {
  640. long resourceId = 12;
  641. underTest.saveProperty(new PropertyDto().setKey("component.null").setResourceId(resourceId).setValue(null));
  642. underTest.saveProperty(new PropertyDto().setKey("component.empty").setResourceId(resourceId).setValue(""));
  643. underTest.saveProperty(new PropertyDto().setKey("component.text").setResourceId(resourceId).setValue("some text"));
  644. underTest.saveProperty(new PropertyDto().setKey("component.4000").setResourceId(resourceId).setValue(VALUE_SIZE_4000));
  645. underTest.saveProperty(new PropertyDto().setKey("component.clob").setResourceId(resourceId).setValue(VALUE_SIZE_4001));
  646. assertThatPropertiesRow("component.null")
  647. .hasResourceId(resourceId)
  648. .hasNoUserId()
  649. .isEmpty()
  650. .hasCreatedAt(INITIAL_DATE + 2);
  651. assertThatPropertiesRow("component.empty")
  652. .hasResourceId(resourceId)
  653. .hasNoUserId()
  654. .isEmpty()
  655. .hasCreatedAt(INITIAL_DATE + 3);
  656. assertThatPropertiesRow("component.text")
  657. .hasResourceId(resourceId)
  658. .hasNoUserId()
  659. .hasTextValue("some text")
  660. .hasCreatedAt(INITIAL_DATE + 4);
  661. assertThatPropertiesRow("component.4000")
  662. .hasResourceId(resourceId)
  663. .hasNoUserId()
  664. .hasTextValue(VALUE_SIZE_4000)
  665. .hasCreatedAt(INITIAL_DATE + 5);
  666. assertThatPropertiesRow("component.clob")
  667. .hasResourceId(resourceId)
  668. .hasNoUserId()
  669. .hasClobValue(VALUE_SIZE_4001)
  670. .hasCreatedAt(INITIAL_DATE + 6);
  671. }
  672. @Test
  673. public void saveProperty_inserts_user_properties_when_they_do_not_exist_in_db() {
  674. int userId = 100;
  675. underTest.saveProperty(new PropertyDto().setKey("user.null").setUserId(userId).setValue(null));
  676. underTest.saveProperty(new PropertyDto().setKey("user.empty").setUserId(userId).setValue(""));
  677. underTest.saveProperty(new PropertyDto().setKey("user.text").setUserId(userId).setValue("some text"));
  678. underTest.saveProperty(new PropertyDto().setKey("user.4000").setUserId(userId).setValue(VALUE_SIZE_4000));
  679. underTest.saveProperty(new PropertyDto().setKey("user.clob").setUserId(userId).setValue(VALUE_SIZE_4001));
  680. assertThatPropertiesRow("user.null")
  681. .hasNoResourceId()
  682. .hasUserId(userId)
  683. .isEmpty()
  684. .hasCreatedAt(INITIAL_DATE + 2);
  685. assertThatPropertiesRow("user.empty")
  686. .hasNoResourceId()
  687. .hasUserId(userId)
  688. .isEmpty()
  689. .hasCreatedAt(INITIAL_DATE + 3);
  690. assertThatPropertiesRow("user.text")
  691. .hasNoResourceId()
  692. .hasUserId(userId)
  693. .hasTextValue("some text")
  694. .hasCreatedAt(INITIAL_DATE + 4);
  695. assertThatPropertiesRow("user.4000")
  696. .hasNoResourceId()
  697. .hasUserId(userId)
  698. .hasTextValue(VALUE_SIZE_4000)
  699. .hasCreatedAt(INITIAL_DATE + 5);
  700. assertThatPropertiesRow("user.clob")
  701. .hasNoResourceId()
  702. .hasUserId(userId)
  703. .hasClobValue(VALUE_SIZE_4001)
  704. .hasCreatedAt(INITIAL_DATE + 6);
  705. }
  706. @Test
  707. @UseDataProvider("valueUpdatesDataProvider")
  708. public void saveProperty_deletes_then_inserts_global_properties_when_they_exist_in_db(@Nullable String oldValue, @Nullable String newValue) {
  709. long id = insertProperty("global", oldValue, null, null);
  710. underTest.saveProperty(new PropertyDto().setKey("global").setValue(newValue));
  711. assertThatPropertiesRow(id)
  712. .doesNotExist();
  713. PropertiesRowAssert propertiesRowAssert = assertThatPropertiesRow("global")
  714. .hasNoResourceId()
  715. .hasNoUserId()
  716. .hasCreatedAt(INITIAL_DATE + 3);
  717. if (newValue == null || newValue.isEmpty()) {
  718. propertiesRowAssert.isEmpty();
  719. } else if (newValue.length() > 4000) {
  720. propertiesRowAssert.hasClobValue(newValue);
  721. } else {
  722. propertiesRowAssert.hasTextValue(newValue);
  723. }
  724. }
  725. @Test
  726. @UseDataProvider("valueUpdatesDataProvider")
  727. public void saveProperty_deletes_then_inserts_component_properties_when_they_exist_in_db(@Nullable String oldValue, @Nullable String newValue) {
  728. long resourceId = 999L;
  729. long id = insertProperty("global", oldValue, resourceId, null);
  730. underTest.saveProperty(new PropertyDto().setKey("global").setResourceId(resourceId).setValue(newValue));
  731. assertThatPropertiesRow(id)
  732. .doesNotExist();
  733. PropertiesRowAssert propertiesRowAssert = assertThatPropertiesRow("global")
  734. .hasResourceId(resourceId)
  735. .hasNoUserId()
  736. .hasCreatedAt(INITIAL_DATE + 3);
  737. if (newValue == null || newValue.isEmpty()) {
  738. propertiesRowAssert.isEmpty();
  739. } else if (newValue.length() > 4000) {
  740. propertiesRowAssert.hasClobValue(newValue);
  741. } else {
  742. propertiesRowAssert.hasTextValue(newValue);
  743. }
  744. }
  745. @Test
  746. @UseDataProvider("valueUpdatesDataProvider")
  747. public void saveProperty_deletes_then_inserts_user_properties_when_they_exist_in_db(@Nullable String oldValue, @Nullable String newValue) {
  748. int userId = 90;
  749. long id = insertProperty("global", oldValue, null, userId);
  750. underTest.saveProperty(new PropertyDto().setKey("global").setUserId(userId).setValue(newValue));
  751. assertThatPropertiesRow(id)
  752. .doesNotExist();
  753. PropertiesRowAssert propertiesRowAssert = assertThatPropertiesRow("global")
  754. .hasNoResourceId()
  755. .hasUserId(userId)
  756. .hasCreatedAt(INITIAL_DATE + 3);
  757. if (newValue == null || newValue.isEmpty()) {
  758. propertiesRowAssert.isEmpty();
  759. } else if (newValue.length() > 4000) {
  760. propertiesRowAssert.hasClobValue(newValue);
  761. } else {
  762. propertiesRowAssert.hasTextValue(newValue);
  763. }
  764. }
  765. @DataProvider
  766. public static Object[][] valueUpdatesDataProvider() {
  767. return new Object[][]{
  768. {null, null},
  769. {null, ""},
  770. {null, "some value"},
  771. {null, VALUE_SIZE_4000},
  772. {null, VALUE_SIZE_4001},
  773. {"", null},
  774. {"", ""},
  775. {"", "some value"},
  776. {"", VALUE_SIZE_4000},
  777. {"", VALUE_SIZE_4001},
  778. {"a value", null},
  779. {"a value", ""},
  780. {"a value", "a value"},
  781. {"a value", "some value"},
  782. {"a value", VALUE_SIZE_4000},
  783. {"a value", VALUE_SIZE_4001},
  784. {VALUE_SIZE_4000, null},
  785. {VALUE_SIZE_4000, ""},
  786. {VALUE_SIZE_4000, "a value"},
  787. {VALUE_SIZE_4000, VALUE_SIZE_4000},
  788. {VALUE_SIZE_4000, VALUE_SIZE_4000.substring(1) + "a"},
  789. {VALUE_SIZE_4000, VALUE_SIZE_4001},
  790. {VALUE_SIZE_4001, null},
  791. {VALUE_SIZE_4001, ""},
  792. {VALUE_SIZE_4001, "a value"},
  793. {VALUE_SIZE_4001, VALUE_SIZE_4000},
  794. {VALUE_SIZE_4001, VALUE_SIZE_4001},
  795. {VALUE_SIZE_4001, VALUE_SIZE_4001 + "dfsdfs"},
  796. };
  797. }
  798. @Test
  799. public void delete_project_property() {
  800. long projectId1 = insertPrivateProject("A").getId();
  801. long projectId2 = insertPrivateProject("B").getId();
  802. long projectId3 = insertPrivateProject("C").getId();
  803. long id1 = insertProperty("global.one", "one", null, null);
  804. long id2 = insertProperty("global.two", "two", null, null);
  805. long id3 = insertProperty("struts.one", "one", projectId1, null);
  806. long id4 = insertProperty("commonslang.one", "one", projectId2, null);
  807. long id5 = insertProperty("user.one", "one", null, 100);
  808. long id6 = insertProperty("user.two", "two", null, 100);
  809. long id7 = insertProperty("other.one", "one", projectId3, null);
  810. underTest.deleteProjectProperty("struts.one", projectId1);
  811. assertThatPropertiesRow(id1)
  812. .hasKey("global.one")
  813. .hasNoResourceId()
  814. .hasNoUserId()
  815. .hasTextValue("one");
  816. assertThatPropertiesRow(id2)
  817. .hasKey("global.two")
  818. .hasNoResourceId()
  819. .hasNoUserId()
  820. .hasTextValue("two");
  821. assertThatPropertiesRow(id3)
  822. .doesNotExist();
  823. assertThatPropertiesRow(id4)
  824. .hasKey("commonslang.one")
  825. .hasResourceId(projectId2)
  826. .hasNoUserId()
  827. .hasTextValue("one");
  828. assertThatPropertiesRow(id5)
  829. .hasKey("user.one")
  830. .hasNoResourceId()
  831. .hasUserId(100)
  832. .hasTextValue("one");
  833. assertThatPropertiesRow(id6)
  834. .hasKey("user.two")
  835. .hasNoResourceId()
  836. .hasUserId(100)
  837. .hasTextValue("two");
  838. assertThatPropertiesRow(id7)
  839. .hasKey("other.one")
  840. .hasResourceId(projectId3)
  841. .hasNoUserId()
  842. .hasTextValue("one");
  843. }
  844. @Test
  845. public void delete_project_properties() {
  846. long id1 = insertProperty("sonar.profile.java", "Sonar Way", 1L, null);
  847. long id2 = insertProperty("sonar.profile.java", "Sonar Way", 2L, null);
  848. long id3 = insertProperty("sonar.profile.java", "Sonar Way", null, null);
  849. long id4 = insertProperty("sonar.profile.js", "Sonar Way", 1L, null);
  850. long id5 = insertProperty("sonar.profile.js", "Sonar Way", 2L, null);
  851. long id6 = insertProperty("sonar.profile.js", "Sonar Way", null, null);
  852. underTest.deleteProjectProperties("sonar.profile.java", "Sonar Way");
  853. assertThatPropertiesRow(id1)
  854. .doesNotExist();
  855. assertThatPropertiesRow(id2)
  856. .doesNotExist();
  857. assertThatPropertiesRow(id3)
  858. .hasKey("sonar.profile.java")
  859. .hasNoResourceId()
  860. .hasNoUserId()
  861. .hasTextValue("Sonar Way");
  862. assertThatPropertiesRow(id4)
  863. .hasKey("sonar.profile.js")
  864. .hasResourceId(1)
  865. .hasNoUserId()
  866. .hasTextValue("Sonar Way");
  867. assertThatPropertiesRow(id5)
  868. .hasKey("sonar.profile.js")
  869. .hasResourceId(2)
  870. .hasNoUserId()
  871. .hasTextValue("Sonar Way");
  872. assertThatPropertiesRow(id6)
  873. .hasKey("sonar.profile.js")
  874. .hasNoResourceId()
  875. .hasNoUserId()
  876. .hasTextValue("Sonar Way");
  877. }
  878. @Test
  879. public void deleteGlobalProperty() {
  880. // global
  881. long id1 = insertProperty("global.key", "new_global", null, null);
  882. long id2 = insertProperty("to_be_deleted", "xxx", null, null);
  883. // project - do not delete this project property that has the same key
  884. long id3 = insertProperty("to_be_deleted", "new_project", 10L, null);
  885. // user
  886. long id4 = insertProperty("user.key", "new_user", null, 100);
  887. underTest.deleteGlobalProperty("to_be_deleted");
  888. assertThatPropertiesRow(id1)
  889. .hasKey("global.key")
  890. .hasNoUserId()
  891. .hasNoResourceId()
  892. .hasTextValue("new_global");
  893. assertThatPropertiesRow(id2)
  894. .doesNotExist();
  895. assertThatPropertiesRow("to_be_deleted", null, null)
  896. .doesNotExist();
  897. assertThatPropertiesRow(id3)
  898. .hasKey("to_be_deleted")
  899. .hasResourceId(10)
  900. .hasNoUserId()
  901. .hasTextValue("new_project");
  902. assertThatPropertiesRow(id4)
  903. .hasKey("user.key")
  904. .hasNoResourceId()
  905. .hasUserId(100)
  906. .hasTextValue("new_user");
  907. }
  908. @Test
  909. public void delete_by_organization_and_user() {
  910. OrganizationDto organization = db.organizations().insert();
  911. OrganizationDto anotherOrganization = db.organizations().insert();
  912. ComponentDto project = db.components().insertPrivateProject(organization);
  913. ComponentDto anotherProject = db.components().insertPrivateProject(anotherOrganization);
  914. UserDto user = db.users().insertUser();
  915. UserDto anotherUser = db.users().insertUser();
  916. insertProperty("KEY_11", "VALUE", project.getId(), user.getId());
  917. insertProperty("KEY_12", "VALUE", project.getId(), user.getId());
  918. insertProperty("KEY_11", "VALUE", project.getId(), anotherUser.getId());
  919. insertProperty("KEY_11", "VALUE", anotherProject.getId(), user.getId());
  920. underTest.deleteByOrganizationAndUser(session, organization.getUuid(), user.getId());
  921. assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setComponentId(project.getId()).build(), session))
  922. .hasSize(1)
  923. .extracting(PropertyDto::getUserId).containsOnly(anotherUser.getId());
  924. assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setComponentId(anotherProject.getId()).build(), session)).extracting(PropertyDto::getUserId)
  925. .hasSize(1).containsOnly(user.getId());
  926. }
  927. @Test
  928. public void delete_by_organization_and_matching_login() {
  929. OrganizationDto organization = db.organizations().insert();
  930. OrganizationDto anotherOrganization = db.organizations().insert();
  931. ComponentDto project = db.components().insertPrivateProject(organization);
  932. ComponentDto anotherProject = db.components().insertPrivateProject(anotherOrganization);
  933. UserDto user = db.users().insertUser();
  934. UserDto anotherUser = db.users().insertUser();
  935. insertProperty("KEY_11", user.getLogin(), project.getId(), null);
  936. insertProperty("KEY_12", user.getLogin(), project.getId(), null);
  937. insertProperty("KEY_11", anotherUser.getLogin(), project.getId(), null);
  938. insertProperty("KEY_11", user.getLogin(), anotherProject.getId(), null);
  939. underTest.deleteByOrganizationAndMatchingLogin(session, organization.getUuid(), user.getLogin(), newArrayList("KEY_11", "KEY_12"));
  940. assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setComponentId(project.getId()).build(), session))
  941. .hasSize(1)
  942. .extracting(PropertyDto::getValue).containsOnly(anotherUser.getLogin());
  943. assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setComponentId(anotherProject.getId()).build(), session)).extracting(PropertyDto::getValue)
  944. .hasSize(1).containsOnly(user.getLogin());
  945. }
  946. @Test
  947. public void delete_by_key_and_value() {
  948. ComponentDto project = db.components().insertPrivateProject();
  949. ComponentDto anotherProject = db.components().insertPrivateProject();
  950. insertProperty("KEY", "VALUE", null, null);
  951. insertProperty("KEY", "VALUE", project.getId(), null);
  952. insertProperty("KEY", "VALUE", null, 100);
  953. insertProperty("KEY", "VALUE", project.getId(), 100);
  954. insertProperty("KEY", "VALUE", anotherProject.getId(), null);
  955. // Should not be removed
  956. insertProperty("KEY", "ANOTHER_VALUE", null, null);
  957. insertProperty("ANOTHER_KEY", "VALUE", project.getId(), 100);
  958. underTest.deleteByKeyAndValue(session, "KEY", "VALUE");
  959. db.commit();
  960. assertThat(db.select("select prop_key as \"key\", text_value as \"value\", resource_id as \"projectId\", user_id as \"userId\" from properties"))
  961. .extracting((row) -> row.get("key"), (row) -> row.get("value"), (row) -> row.get("projectId"), (row) -> row.get("userId"))
  962. .containsOnly(tuple("KEY", "ANOTHER_VALUE", null, null), tuple("ANOTHER_KEY", "VALUE", project.getId(), 100L));
  963. }
  964. @Test
  965. public void saveGlobalProperties_insert_property_if_does_not_exist_in_db() {
  966. underTest.saveGlobalProperties(mapOf(
  967. "null_value_property", null,
  968. "empty_value_property", "",
  969. "text_value_property", "dfdsfsd",
  970. "4000_char_value_property", VALUE_SIZE_4000,
  971. "clob_value_property", VALUE_SIZE_4001));
  972. assertThatPropertiesRow("null_value_property")
  973. .hasNoResourceId()
  974. .hasNoUserId()
  975. .isEmpty()
  976. .hasCreatedAt(INITIAL_DATE + 2);
  977. assertThatPropertiesRow("empty_value_property")
  978. .hasNoResourceId()
  979. .hasNoUserId()
  980. .isEmpty()
  981. .hasCreatedAt(INITIAL_DATE + 3);
  982. assertThatPropertiesRow("text_value_property")
  983. .hasNoResourceId()
  984. .hasNoUserId()
  985. .hasTextValue("dfdsfsd")
  986. .hasCreatedAt(INITIAL_DATE + 4);
  987. assertThatPropertiesRow("4000_char_value_property")
  988. .hasNoResourceId()
  989. .hasNoUserId()
  990. .hasTextValue(VALUE_SIZE_4000)
  991. .hasCreatedAt(INITIAL_DATE + 5);
  992. assertThatPropertiesRow("clob_value_property")
  993. .hasNoResourceId()
  994. .hasNoUserId()
  995. .hasClobValue(VALUE_SIZE_4001)
  996. .hasCreatedAt(INITIAL_DATE + 6);
  997. }
  998. @Test
  999. public void saveGlobalProperties_delete_and_insert_new_value_when_property_exists_in_db() {
  1000. long id = insertProperty("to_be_updated", "old_value", null, null);
  1001. underTest.saveGlobalProperties(ImmutableMap.of("to_be_updated", "new value"));
  1002. assertThatPropertiesRow(id)
  1003. .doesNotExist();
  1004. assertThatPropertiesRow("to_be_updated")
  1005. .hasNoResourceId()
  1006. .hasNoUserId()
  1007. .hasTextValue("new value")
  1008. .hasCreatedAt(INITIAL_DATE + 3);
  1009. }
  1010. private static Map<String, String> mapOf(String... values) {
  1011. // use LinkedHashMap to keep order of array
  1012. Map<String, String> res = new LinkedHashMap<>(values.length / 2);
  1013. Iterator<String> iterator = Arrays.asList(values).iterator();
  1014. while (iterator.hasNext()) {
  1015. res.put(iterator.next(), iterator.next());
  1016. }
  1017. return res;
  1018. }
  1019. @Test
  1020. public void renamePropertyKey_updates_global_component_and_user_properties() {
  1021. long id1 = insertProperty("foo", "bar", null, null);
  1022. long id2 = insertProperty("old_name", "doc1", null, null);
  1023. long id3 = insertProperty("old_name", "doc2", 15L, null);
  1024. long id4 = insertProperty("old_name", "doc3", 16L, null);
  1025. long id5 = insertProperty("old_name", "doc4", null, 100);
  1026. long id6 = insertProperty("old_name", "doc5", null, 101);
  1027. underTest.renamePropertyKey("old_name", "new_name");
  1028. assertThatPropertiesRow(id1)
  1029. .hasKey("foo")
  1030. .hasNoUserId()
  1031. .hasNoResourceId()
  1032. .hasTextValue("bar")
  1033. .hasCreatedAt(INITIAL_DATE + 2);
  1034. assertThatPropertiesRow(id2)
  1035. .hasKey("new_name")
  1036. .hasNoResourceId()
  1037. .hasNoUserId()
  1038. .hasTextValue("doc1")
  1039. .hasCreatedAt(INITIAL_DATE + 3);
  1040. assertThatPropertiesRow(id3)
  1041. .hasKey("new_name")
  1042. .hasResourceId(15)
  1043. .hasNoUserId()
  1044. .hasTextValue("doc2")
  1045. .hasCreatedAt(INITIAL_DATE + 4);
  1046. assertThatPropertiesRow(id4)
  1047. .hasKey("new_name")
  1048. .hasResourceId(16)
  1049. .hasNoUserId()
  1050. .hasTextValue("doc3")
  1051. .hasCreatedAt(INITIAL_DATE + 5);
  1052. assertThatPropertiesRow(id5)
  1053. .hasKey("new_name")
  1054. .hasNoResourceId()
  1055. .hasUserId(100)
  1056. .hasTextValue("doc4")
  1057. .hasCreatedAt(INITIAL_DATE + 6);
  1058. assertThatPropertiesRow(id6)
  1059. .hasKey("new_name")
  1060. .hasNoResourceId()
  1061. .hasUserId(101)
  1062. .hasTextValue("doc5")
  1063. .hasCreatedAt(INITIAL_DATE + 7);
  1064. }
  1065. @Test
  1066. public void rename_to_same_key_has_no_effect() {
  1067. long id = insertProperty("foo", "bar", null, null);
  1068. assertThatPropertiesRow(id)
  1069. .hasCreatedAt(INITIAL_DATE + 2);
  1070. underTest.renamePropertyKey("foo", "foo");
  1071. assertThatPropertiesRow(id)
  1072. .hasKey("foo")
  1073. .hasNoUserId()
  1074. .hasNoResourceId()
  1075. .hasTextValue("bar")
  1076. .hasCreatedAt(INITIAL_DATE + 2);
  1077. }
  1078. @Test
  1079. public void should_not_rename_with_empty_key() {
  1080. thrown.expect(IllegalArgumentException.class);
  1081. underTest.renamePropertyKey("foo", "");
  1082. }
  1083. @Test
  1084. public void should_not_rename_an_empty_key() {
  1085. thrown.expect(IllegalArgumentException.class);
  1086. underTest.renamePropertyKey(null, "foo");
  1087. }
  1088. private PropertyDto findByKey(List<PropertyDto> properties, String key) {
  1089. for (PropertyDto property : properties) {
  1090. if (key.equals(property.getKey())) {
  1091. return property;
  1092. }
  1093. }
  1094. return null;
  1095. }
  1096. private void insertProperties(PropertyDto... properties) {
  1097. for (PropertyDto propertyDto : properties) {
  1098. underTest.saveProperty(session, propertyDto);
  1099. }
  1100. session.commit();
  1101. }
  1102. private long insertProperty(String key, @Nullable String value, @Nullable Long resourceId, @Nullable Integer userId) {
  1103. PropertyDto dto = new PropertyDto().setKey(key)
  1104. .setResourceId(resourceId)
  1105. .setUserId(userId)
  1106. .setValue(value);
  1107. db.properties().insertProperty(dto);
  1108. return (long) db.selectFirst(session, "select id as \"id\" from properties" +
  1109. " where prop_key='" + key + "'" +
  1110. " and user_id" + (userId == null ? " is null" : "='" + userId + "'") +
  1111. " and resource_id" + (resourceId == null ? " is null" : "='" + resourceId + "'")).get("id");
  1112. }
  1113. private ComponentDto insertPrivateProject(String projectKey) {
  1114. return db.components().insertPrivateProject(db.getDefaultOrganization(), t -> t.setDbKey(projectKey));
  1115. }
  1116. private static Consumer<UserDto> withEmail(String login) {
  1117. return u -> u.setLogin(login).setEmail(emailOf(login));
  1118. }
  1119. private static String emailOf(String login) {
  1120. return login + "@foo";
  1121. }
  1122. private static Consumer<UserDto> noEmail(String login) {
  1123. return u -> u.setLogin(login).setEmail(null);
  1124. }
  1125. private static String propertyKeyOf(String dispatcherKey, String channelKey) {
  1126. return String.format("notification.%s.%s", dispatcherKey, channelKey);
  1127. }
  1128. private static PropertyDtoAssert assertThatDto(@Nullable PropertyDto dto) {
  1129. return new PropertyDtoAssert(dto);
  1130. }
  1131. private PropertiesRowAssert assertThatPropertiesRow(String key, @Nullable Integer userId, @Nullable Integer componentId) {
  1132. return new PropertiesRowAssert(db, key, userId, componentId);
  1133. }
  1134. private PropertiesRowAssert assertThatPropertiesRow(String key) {
  1135. return new PropertiesRowAssert(db, key);
  1136. }
  1137. private PropertiesRowAssert assertThatPropertiesRow(long id) {
  1138. return new PropertiesRowAssert(db, id);
  1139. }
  1140. }