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.

BranchDaoTest.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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.component;
  21. import com.tngtech.java.junit.dataprovider.DataProvider;
  22. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  23. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  24. import java.util.Map;
  25. import javax.annotation.Nullable;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.junit.runner.RunWith;
  29. import org.sonar.api.utils.System2;
  30. import org.sonar.api.utils.internal.TestSystem2;
  31. import org.sonar.db.DbSession;
  32. import org.sonar.db.DbTester;
  33. import org.sonar.db.protobuf.DbProjectBranches;
  34. import static java.util.Arrays.asList;
  35. import static java.util.Collections.singletonList;
  36. import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
  37. import static org.apache.commons.lang.StringUtils.repeat;
  38. import static org.assertj.core.api.Assertions.assertThat;
  39. import static org.assertj.core.api.Assertions.entry;
  40. @RunWith(DataProviderRunner.class)
  41. public class BranchDaoTest {
  42. private static final long NOW = 1_000L;
  43. private static final String SELECT_FROM = "select project_uuid as \"projectUuid\", uuid as \"uuid\", branch_type as \"branchType\", " +
  44. "kee as \"kee\", merge_branch_uuid as \"mergeBranchUuid\", pull_request_binary as \"pullRequestBinary\", created_at as \"createdAt\", updated_at as \"updatedAt\" " +
  45. "from project_branches ";
  46. private System2 system2 = new TestSystem2().setNow(NOW);
  47. @Rule
  48. public DbTester db = DbTester.create(system2);
  49. private DbSession dbSession = db.getSession();
  50. private BranchDao underTest = new BranchDao(system2);
  51. @Test
  52. public void insert_branch_with_only_nonnull_fields() {
  53. BranchDto dto = new BranchDto();
  54. dto.setProjectUuid("U1");
  55. dto.setUuid("U2");
  56. dto.setBranchType(BranchType.SHORT);
  57. dto.setKey("feature/foo");
  58. underTest.insert(dbSession, dto);
  59. Map<String, Object> map = db.selectFirst(dbSession, SELECT_FROM + " where uuid='" + dto.getUuid() + "'");
  60. assertThat(map).contains(
  61. entry("projectUuid", "U1"),
  62. entry("uuid", "U2"),
  63. entry("branchType", "SHORT"),
  64. entry("kee", "feature/foo"),
  65. entry("mergeBranchUuid", null),
  66. entry("pullRequestBinary", null),
  67. entry("createdAt", 1_000L),
  68. entry("updatedAt", 1_000L));
  69. }
  70. @Test
  71. public void update_main_branch_name() {
  72. BranchDto dto = new BranchDto();
  73. dto.setProjectUuid("U1");
  74. dto.setUuid("U1");
  75. dto.setBranchType(BranchType.LONG);
  76. dto.setKey("feature");
  77. underTest.insert(dbSession, dto);
  78. BranchDto dto2 = new BranchDto();
  79. dto2.setProjectUuid("U2");
  80. dto2.setUuid("U2");
  81. dto2.setBranchType(BranchType.LONG);
  82. dto2.setKey("branch");
  83. underTest.insert(dbSession, dto2);
  84. underTest.updateMainBranchName(dbSession, "U1", "master");
  85. BranchDto loaded = underTest.selectByBranchKey(dbSession, "U1", "master").get();
  86. assertThat(loaded.getMergeBranchUuid()).isNull();
  87. assertThat(loaded.getProjectUuid()).isEqualTo("U1");
  88. assertThat(loaded.getBranchType()).isEqualTo(BranchType.LONG);
  89. }
  90. @Test
  91. @UseDataProvider("nullOrEmpty")
  92. public void insert_null_or_empty_manual_baseline_analysis_uuid_is_null(@Nullable String nullOrEmpty) {
  93. BranchDto dto = new BranchDto();
  94. dto.setProjectUuid("U1");
  95. dto.setUuid("U1");
  96. dto.setBranchType(BranchType.LONG);
  97. dto.setKey("foo");
  98. dto.setManualBaseline(nullOrEmpty);
  99. underTest.insert(dbSession, dto);
  100. assertThat(underTest.selectByUuid(dbSession, dto.getUuid()).get().getManualBaseline()).isNull();
  101. }
  102. @DataProvider
  103. public static Object[][] nullOrEmpty() {
  104. return new Object[][] {
  105. {null},
  106. {""}
  107. };
  108. }
  109. @Test
  110. public void insert_manual_baseline_analysis_uuid() {
  111. String manualBaselineAnalysisUuid = randomAlphabetic(12);
  112. BranchDto dto = new BranchDto();
  113. dto.setProjectUuid("U1");
  114. dto.setUuid("U1");
  115. dto.setBranchType(BranchType.LONG);
  116. dto.setKey("foo");
  117. dto.setManualBaseline(manualBaselineAnalysisUuid);
  118. underTest.insert(dbSession, dto);
  119. assertThat(underTest.selectByUuid(dbSession, dto.getUuid()).get().getManualBaseline()).isEqualTo(manualBaselineAnalysisUuid);
  120. }
  121. @Test
  122. @UseDataProvider("oldAndNewValuesCombinations")
  123. public void update_manualBaselineAnalysisUuid_if_new_value_is_different(@Nullable String oldValue, @Nullable String newValue) {
  124. BranchDto dto = new BranchDto();
  125. dto.setProjectUuid("U1");
  126. dto.setUuid("U1");
  127. dto.setBranchType(BranchType.LONG);
  128. dto.setKey("foo");
  129. dto.setManualBaseline(oldValue);
  130. underTest.insert(dbSession, dto);
  131. BranchDto otherDtoThatShouldNotChange = new BranchDto();
  132. otherDtoThatShouldNotChange.setProjectUuid("U2");
  133. otherDtoThatShouldNotChange.setUuid("U2");
  134. otherDtoThatShouldNotChange.setBranchType(BranchType.LONG);
  135. otherDtoThatShouldNotChange.setKey("branch");
  136. otherDtoThatShouldNotChange.setManualBaseline(oldValue);
  137. underTest.insert(dbSession, otherDtoThatShouldNotChange);
  138. assertThat(underTest.updateManualBaseline(dbSession, dto.getUuid(), newValue)).isEqualTo(1);
  139. assertThat(underTest.selectByUuid(dbSession, dto.getUuid()).get().getManualBaseline())
  140. .isEqualTo(emptyToNull(newValue));
  141. assertThat(underTest.selectByUuid(dbSession, otherDtoThatShouldNotChange.getUuid()).get().getManualBaseline())
  142. .isEqualTo(emptyToNull(oldValue));
  143. }
  144. @DataProvider
  145. public static Object[][] oldAndNewValuesCombinations() {
  146. String value1 = randomAlphabetic(10);
  147. String value2 = randomAlphabetic(20);
  148. return new Object[][] {
  149. {null, value1},
  150. {"", value1},
  151. {value1, null},
  152. {value1, ""},
  153. {value1, value2},
  154. {null, null},
  155. {"", null},
  156. {value1, value1}
  157. };
  158. }
  159. @Test
  160. @UseDataProvider("nonLongBranchType")
  161. public void do_not_update_manual_baseline_of_non_long_branches(BranchType branchType) {
  162. String analysisUuid = randomAlphabetic(12);
  163. String otherAnalysisUuid = randomAlphabetic(12);
  164. BranchDto noManualBaselineDto = ComponentTesting.newBranchDto(db.components().insertPrivateProject(), branchType);
  165. BranchDto withManualBaselineDto = ComponentTesting.newBranchDto(db.components().insertPrivateProject(), branchType).setManualBaseline(analysisUuid);
  166. underTest.insert(dbSession, noManualBaselineDto);
  167. underTest.insert(dbSession, withManualBaselineDto);
  168. dbSession.commit();
  169. assertThat(underTest.updateManualBaseline(dbSession, noManualBaselineDto.getUuid(), null)).isZero();
  170. assertThat(underTest.updateManualBaseline(dbSession, noManualBaselineDto.getUuid(), otherAnalysisUuid)).isZero();
  171. assertThat(underTest.updateManualBaseline(dbSession, withManualBaselineDto.getUuid(), null)).isZero();
  172. assertThat(underTest.updateManualBaseline(dbSession, withManualBaselineDto.getUuid(), otherAnalysisUuid)).isZero();
  173. }
  174. @DataProvider
  175. public static Object[][] nonLongBranchType() {
  176. return new Object[][] {
  177. {BranchType.SHORT},
  178. {BranchType.PULL_REQUEST}
  179. };
  180. }
  181. @Test
  182. public void insert_branch_with_all_fields_and_max_length_values() {
  183. BranchDto dto = new BranchDto();
  184. dto.setProjectUuid(repeat("a", 50));
  185. dto.setUuid(repeat("b", 50));
  186. dto.setBranchType(BranchType.SHORT);
  187. dto.setKey(repeat("c", 255));
  188. dto.setMergeBranchUuid(repeat("d", 50));
  189. underTest.insert(dbSession, dto);
  190. Map<String, Object> map = db.selectFirst(dbSession, SELECT_FROM + " where uuid='" + dto.getUuid() + "'");
  191. assertThat((String) map.get("projectUuid")).contains("a").isEqualTo(dto.getProjectUuid());
  192. assertThat((String) map.get("uuid")).contains("b").isEqualTo(dto.getUuid());
  193. assertThat((String) map.get("kee")).contains("c").isEqualTo(dto.getKey());
  194. assertThat((String) map.get("mergeBranchUuid")).contains("d").isEqualTo(dto.getMergeBranchUuid());
  195. }
  196. @Test
  197. public void insert_pull_request_branch_with_only_non_null_fields() {
  198. String projectUuid = "U1";
  199. String uuid = "U2";
  200. BranchType branchType = BranchType.PULL_REQUEST;
  201. String kee = "123";
  202. BranchDto dto = new BranchDto();
  203. dto.setProjectUuid(projectUuid);
  204. dto.setUuid(uuid);
  205. dto.setBranchType(branchType);
  206. dto.setKey(kee);
  207. underTest.insert(dbSession, dto);
  208. BranchDto loaded = underTest.selectByUuid(dbSession, dto.getUuid()).get();
  209. assertThat(loaded.getProjectUuid()).isEqualTo(projectUuid);
  210. assertThat(loaded.getUuid()).isEqualTo(uuid);
  211. assertThat(loaded.getBranchType()).isEqualTo(branchType);
  212. assertThat(loaded.getKey()).isEqualTo(kee);
  213. assertThat(loaded.getMergeBranchUuid()).isNull();
  214. assertThat(loaded.getPullRequestData()).isNull();
  215. }
  216. @Test
  217. public void insert_pull_request_branch_with_all_fields() {
  218. String projectUuid = "U1";
  219. String uuid = "U2";
  220. BranchType branchType = BranchType.PULL_REQUEST;
  221. String kee = "123";
  222. String branch = "feature/pr1";
  223. String title = "Dummy Feature Title";
  224. String url = "http://example.com/pullRequests/pr1";
  225. String tokenAttributeName = "token";
  226. String tokenAttributeValue = "dummy token";
  227. DbProjectBranches.PullRequestData pullRequestData = DbProjectBranches.PullRequestData.newBuilder()
  228. .setBranch(branch)
  229. .setTitle(title)
  230. .setUrl(url)
  231. .putAttributes(tokenAttributeName, tokenAttributeValue)
  232. .build();
  233. BranchDto dto = new BranchDto();
  234. dto.setProjectUuid(projectUuid);
  235. dto.setUuid(uuid);
  236. dto.setBranchType(branchType);
  237. dto.setKey(kee);
  238. dto.setPullRequestData(pullRequestData);
  239. underTest.insert(dbSession, dto);
  240. BranchDto loaded = underTest.selectByUuid(dbSession, dto.getUuid()).get();
  241. assertThat(loaded.getProjectUuid()).isEqualTo(projectUuid);
  242. assertThat(loaded.getUuid()).isEqualTo(uuid);
  243. assertThat(loaded.getBranchType()).isEqualTo(branchType);
  244. assertThat(loaded.getKey()).isEqualTo(kee);
  245. assertThat(loaded.getMergeBranchUuid()).isNull();
  246. DbProjectBranches.PullRequestData loadedPullRequestData = loaded.getPullRequestData();
  247. assertThat(loadedPullRequestData).isNotNull();
  248. assertThat(loadedPullRequestData.getBranch()).isEqualTo(branch);
  249. assertThat(loadedPullRequestData.getTitle()).isEqualTo(title);
  250. assertThat(loadedPullRequestData.getUrl()).isEqualTo(url);
  251. assertThat(loadedPullRequestData.getAttributesMap().get(tokenAttributeName)).isEqualTo(tokenAttributeValue);
  252. }
  253. @Test
  254. public void upsert_branch() {
  255. BranchDto dto = new BranchDto();
  256. dto.setProjectUuid("U1");
  257. dto.setUuid("U2");
  258. dto.setBranchType(BranchType.LONG);
  259. dto.setKey("foo");
  260. underTest.insert(dbSession, dto);
  261. // the fields that can be updated
  262. dto.setMergeBranchUuid("U3");
  263. // the fields that can't be updated. New values are ignored.
  264. dto.setProjectUuid("ignored");
  265. dto.setBranchType(BranchType.SHORT);
  266. underTest.upsert(dbSession, dto);
  267. BranchDto loaded = underTest.selectByBranchKey(dbSession, "U1", "foo").get();
  268. assertThat(loaded.getMergeBranchUuid()).isEqualTo("U3");
  269. assertThat(loaded.getProjectUuid()).isEqualTo("U1");
  270. assertThat(loaded.getBranchType()).isEqualTo(BranchType.LONG);
  271. }
  272. @Test
  273. public void upsert_pull_request() {
  274. BranchDto dto = new BranchDto();
  275. dto.setProjectUuid("U1");
  276. dto.setUuid("U2");
  277. dto.setBranchType(BranchType.PULL_REQUEST);
  278. dto.setKey("foo");
  279. underTest.insert(dbSession, dto);
  280. // the fields that can be updated
  281. dto.setMergeBranchUuid("U3");
  282. String branch = "feature/pr1";
  283. String title = "Dummy Feature Title";
  284. String url = "http://example.com/pullRequests/pr1";
  285. String tokenAttributeName = "token";
  286. String tokenAttributeValue = "dummy token";
  287. DbProjectBranches.PullRequestData pullRequestData = DbProjectBranches.PullRequestData.newBuilder()
  288. .setBranch(branch)
  289. .setTitle(title)
  290. .setUrl(url)
  291. .putAttributes(tokenAttributeName, tokenAttributeValue)
  292. .build();
  293. dto.setPullRequestData(pullRequestData);
  294. // the fields that can't be updated. New values are ignored.
  295. dto.setProjectUuid("ignored");
  296. dto.setBranchType(BranchType.SHORT);
  297. underTest.upsert(dbSession, dto);
  298. BranchDto loaded = underTest.selectByPullRequestKey(dbSession, "U1", "foo").get();
  299. assertThat(loaded.getMergeBranchUuid()).isEqualTo("U3");
  300. assertThat(loaded.getProjectUuid()).isEqualTo("U1");
  301. assertThat(loaded.getBranchType()).isEqualTo(BranchType.PULL_REQUEST);
  302. DbProjectBranches.PullRequestData loadedPullRequestData = loaded.getPullRequestData();
  303. assertThat(loadedPullRequestData).isNotNull();
  304. assertThat(loadedPullRequestData.getBranch()).isEqualTo(branch);
  305. assertThat(loadedPullRequestData.getTitle()).isEqualTo(title);
  306. assertThat(loadedPullRequestData.getUrl()).isEqualTo(url);
  307. assertThat(loadedPullRequestData.getAttributesMap().get(tokenAttributeName)).isEqualTo(tokenAttributeValue);
  308. }
  309. @Test
  310. public void update_pull_request_data() {
  311. BranchDto dto = new BranchDto();
  312. dto.setProjectUuid("U1");
  313. dto.setUuid("U2");
  314. dto.setBranchType(BranchType.PULL_REQUEST);
  315. dto.setKey("foo");
  316. // the fields that can be updated
  317. String mergeBranchUuid = "U3";
  318. dto.setMergeBranchUuid(mergeBranchUuid + "-dummy-suffix");
  319. String branch = "feature/pr1";
  320. String title = "Dummy Feature Title";
  321. String url = "http://example.com/pullRequests/pr1";
  322. String tokenAttributeName = "token";
  323. String tokenAttributeValue = "dummy token";
  324. DbProjectBranches.PullRequestData pullRequestData = DbProjectBranches.PullRequestData.newBuilder()
  325. .setBranch(branch + "-dummy-suffix")
  326. .setTitle(title + "-dummy-suffix")
  327. .setUrl(url + "-dummy-suffix")
  328. .putAttributes(tokenAttributeName, tokenAttributeValue + "-dummy-suffix")
  329. .build();
  330. dto.setPullRequestData(pullRequestData);
  331. underTest.insert(dbSession, dto);
  332. // modify pull request data
  333. dto.setMergeBranchUuid(mergeBranchUuid);
  334. pullRequestData = DbProjectBranches.PullRequestData.newBuilder()
  335. .setBranch(branch)
  336. .setTitle(title)
  337. .setUrl(url)
  338. .putAttributes(tokenAttributeName, tokenAttributeValue)
  339. .build();
  340. dto.setPullRequestData(pullRequestData);
  341. underTest.upsert(dbSession, dto);
  342. BranchDto loaded = underTest.selectByPullRequestKey(dbSession, "U1", "foo").get();
  343. assertThat(loaded.getMergeBranchUuid()).isEqualTo(mergeBranchUuid);
  344. assertThat(loaded.getProjectUuid()).isEqualTo("U1");
  345. assertThat(loaded.getBranchType()).isEqualTo(BranchType.PULL_REQUEST);
  346. DbProjectBranches.PullRequestData loadedPullRequestData = loaded.getPullRequestData();
  347. assertThat(loadedPullRequestData).isNotNull();
  348. assertThat(loadedPullRequestData.getBranch()).isEqualTo(branch);
  349. assertThat(loadedPullRequestData.getTitle()).isEqualTo(title);
  350. assertThat(loadedPullRequestData.getUrl()).isEqualTo(url);
  351. assertThat(loadedPullRequestData.getAttributesMap().get(tokenAttributeName)).isEqualTo(tokenAttributeValue);
  352. }
  353. @Test
  354. public void selectByBranchKey() {
  355. BranchDto mainBranch = new BranchDto();
  356. mainBranch.setProjectUuid("U1");
  357. mainBranch.setUuid("U1");
  358. mainBranch.setBranchType(BranchType.LONG);
  359. mainBranch.setKey("master");
  360. underTest.insert(dbSession, mainBranch);
  361. BranchDto featureBranch = new BranchDto();
  362. featureBranch.setProjectUuid("U1");
  363. featureBranch.setUuid("U2");
  364. featureBranch.setBranchType(BranchType.SHORT);
  365. featureBranch.setKey("feature/foo");
  366. featureBranch.setMergeBranchUuid("U3");
  367. featureBranch.setManualBaseline("analysisUUID");
  368. underTest.insert(dbSession, featureBranch);
  369. // select the feature branch
  370. BranchDto loaded = underTest.selectByBranchKey(dbSession, "U1", "feature/foo").get();
  371. assertThat(loaded.getUuid()).isEqualTo(featureBranch.getUuid());
  372. assertThat(loaded.getKey()).isEqualTo(featureBranch.getKey());
  373. assertThat(loaded.getProjectUuid()).isEqualTo(featureBranch.getProjectUuid());
  374. assertThat(loaded.getBranchType()).isEqualTo(featureBranch.getBranchType());
  375. assertThat(loaded.getMergeBranchUuid()).isEqualTo(featureBranch.getMergeBranchUuid());
  376. assertThat(loaded.getManualBaseline()).isEqualTo(featureBranch.getManualBaseline());
  377. // select a branch on another project with same branch name
  378. assertThat(underTest.selectByBranchKey(dbSession, "U3", "feature/foo")).isEmpty();
  379. }
  380. @Test
  381. public void selectByPullRequestKey() {
  382. BranchDto mainBranch = new BranchDto();
  383. mainBranch.setProjectUuid("U1");
  384. mainBranch.setUuid("U1");
  385. mainBranch.setBranchType(BranchType.LONG);
  386. mainBranch.setKey("master");
  387. underTest.insert(dbSession, mainBranch);
  388. String pullRequestId = "123";
  389. BranchDto pullRequest = new BranchDto();
  390. pullRequest.setProjectUuid("U1");
  391. pullRequest.setUuid("U2");
  392. pullRequest.setBranchType(BranchType.PULL_REQUEST);
  393. pullRequest.setKey(pullRequestId);
  394. pullRequest.setMergeBranchUuid("U3");
  395. pullRequest.setManualBaseline("analysisUUID");
  396. underTest.insert(dbSession, pullRequest);
  397. // select the feature branch
  398. BranchDto loaded = underTest.selectByPullRequestKey(dbSession, "U1", pullRequestId).get();
  399. assertThat(loaded.getUuid()).isEqualTo(pullRequest.getUuid());
  400. assertThat(loaded.getKey()).isEqualTo(pullRequest.getKey());
  401. assertThat(loaded.getProjectUuid()).isEqualTo(pullRequest.getProjectUuid());
  402. assertThat(loaded.getBranchType()).isEqualTo(pullRequest.getBranchType());
  403. assertThat(loaded.getMergeBranchUuid()).isEqualTo(pullRequest.getMergeBranchUuid());
  404. assertThat(loaded.getManualBaseline()).isEqualTo(pullRequest.getManualBaseline());
  405. // select a branch on another project with same branch name
  406. assertThat(underTest.selectByPullRequestKey(dbSession, "U3", pullRequestId)).isEmpty();
  407. }
  408. @Test
  409. public void selectByUuids() {
  410. ComponentDto project = db.components().insertPrivateProject();
  411. ComponentDto branch1 = db.components().insertProjectBranch(project);
  412. ComponentDto branch2 = db.components().insertProjectBranch(project);
  413. ComponentDto branch3 = db.components().insertProjectBranch(project);
  414. assertThat(underTest.selectByUuids(db.getSession(), asList(branch1.uuid(), branch2.uuid(), branch3.uuid())))
  415. .extracting(BranchDto::getUuid)
  416. .containsExactlyInAnyOrder(branch1.uuid(), branch2.uuid(), branch3.uuid());
  417. assertThat(underTest.selectByUuids(db.getSession(), singletonList(branch1.uuid())))
  418. .extracting(BranchDto::getUuid)
  419. .containsExactlyInAnyOrder(branch1.uuid());
  420. assertThat(underTest.selectByUuids(db.getSession(), singletonList("unknown"))).isEmpty();
  421. }
  422. @Test
  423. public void selectByUuid() {
  424. ComponentDto project = db.components().insertPrivateProject();
  425. ComponentDto branch1 = db.components().insertProjectBranch(project);
  426. ComponentDto branch2 = db.components().insertProjectBranch(project);
  427. assertThat(underTest.selectByUuid(db.getSession(), branch1.uuid()).get())
  428. .extracting(BranchDto::getUuid)
  429. .isEqualTo(branch1.uuid());
  430. assertThat(underTest.selectByUuid(db.getSession(), project.uuid())).isNotPresent();
  431. assertThat(underTest.selectByUuid(db.getSession(), "unknown")).isNotPresent();
  432. }
  433. @Test
  434. public void existsNonMainBranch() {
  435. assertThat(underTest.hasNonMainBranches(dbSession)).isFalse();
  436. ComponentDto project = db.components().insertPrivateProject();
  437. assertThat(underTest.hasNonMainBranches(dbSession)).isFalse();
  438. ComponentDto branch1 = db.components().insertProjectBranch(project);
  439. assertThat(underTest.hasNonMainBranches(dbSession)).isTrue();
  440. ComponentDto branch2 = db.components().insertProjectBranch(project);
  441. assertThat(underTest.hasNonMainBranches(dbSession)).isTrue();
  442. }
  443. @Test
  444. public void countByTypeAndCreationDate() {
  445. assertThat(underTest.countByTypeAndCreationDate(dbSession, BranchType.LONG, 0L)).isEqualTo(0);
  446. ComponentDto project = db.components().insertPrivateProject();
  447. ComponentDto longBranch1 = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.LONG));
  448. ComponentDto longBranch2 = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.LONG));
  449. ComponentDto pr = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST));
  450. assertThat(underTest.countByTypeAndCreationDate(dbSession, BranchType.LONG, 0L)).isEqualTo(2);
  451. assertThat(underTest.countByTypeAndCreationDate(dbSession, BranchType.LONG, NOW)).isEqualTo(2);
  452. assertThat(underTest.countByTypeAndCreationDate(dbSession, BranchType.LONG, NOW + 100)).isEqualTo(0);
  453. assertThat(underTest.countByTypeAndCreationDate(dbSession, BranchType.PULL_REQUEST, 0L)).isEqualTo(1);
  454. assertThat(underTest.countByTypeAndCreationDate(dbSession, BranchType.PULL_REQUEST, NOW)).isEqualTo(1);
  455. assertThat(underTest.countByTypeAndCreationDate(dbSession, BranchType.PULL_REQUEST, NOW + 100)).isEqualTo(0);
  456. }
  457. private static String emptyToNull(@Nullable String newValue) {
  458. return newValue == null || newValue.isEmpty() ? null : newValue;
  459. }
  460. }