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 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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 java.util.Map;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.sonar.api.utils.System2;
  25. import org.sonar.api.utils.internal.TestSystem2;
  26. import org.sonar.db.DbSession;
  27. import org.sonar.db.DbTester;
  28. import org.sonar.db.protobuf.DbProjectBranches;
  29. import static java.util.Arrays.asList;
  30. import static java.util.Collections.singletonList;
  31. import static org.apache.commons.lang.StringUtils.repeat;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. import static org.assertj.core.api.Assertions.entry;
  34. public class BranchDaoTest {
  35. private static final long NOW = 1_000L;
  36. private static final String SELECT_FROM = "select project_uuid as \"projectUuid\", uuid as \"uuid\", branch_type as \"branchType\", " +
  37. "kee as \"kee\", merge_branch_uuid as \"mergeBranchUuid\", pull_request_binary as \"pullRequestBinary\", created_at as \"createdAt\", updated_at as \"updatedAt\" " +
  38. "from project_branches ";
  39. private System2 system2 = new TestSystem2().setNow(NOW);
  40. @Rule
  41. public DbTester db = DbTester.create(system2);
  42. private DbSession dbSession = db.getSession();
  43. private BranchDao underTest = new BranchDao(system2);
  44. @Test
  45. public void insert_branch_with_only_nonnull_fields() {
  46. BranchDto dto = new BranchDto();
  47. dto.setProjectUuid("U1");
  48. dto.setUuid("U2");
  49. dto.setBranchType(BranchType.SHORT);
  50. dto.setKey("feature/foo");
  51. underTest.insert(dbSession, dto);
  52. Map<String, Object> map = db.selectFirst(dbSession, SELECT_FROM + " where uuid='" + dto.getUuid() + "'");
  53. assertThat(map).contains(
  54. entry("projectUuid", "U1"),
  55. entry("uuid", "U2"),
  56. entry("branchType", "SHORT"),
  57. entry("kee", "feature/foo"),
  58. entry("mergeBranchUuid", null),
  59. entry("pullRequestBinary", null),
  60. entry("createdAt", 1_000L),
  61. entry("updatedAt", 1_000L));
  62. }
  63. @Test
  64. public void update_main_branch_name() {
  65. BranchDto dto = new BranchDto();
  66. dto.setProjectUuid("U1");
  67. dto.setUuid("U1");
  68. dto.setBranchType(BranchType.LONG);
  69. dto.setKey("feature");
  70. underTest.insert(dbSession, dto);
  71. BranchDto dto2 = new BranchDto();
  72. dto2.setProjectUuid("U2");
  73. dto2.setUuid("U2");
  74. dto2.setBranchType(BranchType.LONG);
  75. dto2.setKey("branch");
  76. underTest.insert(dbSession, dto2);
  77. underTest.updateMainBranchName(dbSession, "U1", "master");
  78. BranchDto loaded = underTest.selectByBranchKey(dbSession, "U1", "master").get();
  79. assertThat(loaded.getMergeBranchUuid()).isNull();
  80. assertThat(loaded.getProjectUuid()).isEqualTo("U1");
  81. assertThat(loaded.getBranchType()).isEqualTo(BranchType.LONG);
  82. }
  83. @Test
  84. public void insert_branch_with_all_fields_and_max_length_values() {
  85. BranchDto dto = new BranchDto();
  86. dto.setProjectUuid(repeat("a", 50));
  87. dto.setUuid(repeat("b", 50));
  88. dto.setBranchType(BranchType.SHORT);
  89. dto.setKey(repeat("c", 255));
  90. dto.setMergeBranchUuid(repeat("d", 50));
  91. underTest.insert(dbSession, dto);
  92. Map<String, Object> map = db.selectFirst(dbSession, SELECT_FROM + " where uuid='" + dto.getUuid() + "'");
  93. assertThat((String) map.get("projectUuid")).contains("a").isEqualTo(dto.getProjectUuid());
  94. assertThat((String) map.get("uuid")).contains("b").isEqualTo(dto.getUuid());
  95. assertThat((String) map.get("kee")).contains("c").isEqualTo(dto.getKey());
  96. assertThat((String) map.get("mergeBranchUuid")).contains("d").isEqualTo(dto.getMergeBranchUuid());
  97. }
  98. @Test
  99. public void insert_pull_request_branch_with_only_non_null_fields() {
  100. String projectUuid = "U1";
  101. String uuid = "U2";
  102. BranchType branchType = BranchType.PULL_REQUEST;
  103. String kee = "123";
  104. BranchDto dto = new BranchDto();
  105. dto.setProjectUuid(projectUuid);
  106. dto.setUuid(uuid);
  107. dto.setBranchType(branchType);
  108. dto.setKey(kee);
  109. underTest.insert(dbSession, dto);
  110. BranchDto loaded = underTest.selectByUuid(dbSession, dto.getUuid()).get();
  111. assertThat(loaded.getProjectUuid()).isEqualTo(projectUuid);
  112. assertThat(loaded.getUuid()).isEqualTo(uuid);
  113. assertThat(loaded.getBranchType()).isEqualTo(branchType);
  114. assertThat(loaded.getKey()).isEqualTo(kee);
  115. assertThat(loaded.getMergeBranchUuid()).isNull();
  116. assertThat(loaded.getPullRequestData()).isNull();
  117. }
  118. @Test
  119. public void insert_pull_request_branch_with_all_fields() {
  120. String projectUuid = "U1";
  121. String uuid = "U2";
  122. BranchType branchType = BranchType.PULL_REQUEST;
  123. String kee = "123";
  124. String branch = "feature/pr1";
  125. String title = "Dummy Feature Title";
  126. String url = "http://example.com/pullRequests/pr1";
  127. String tokenAttributeName = "token";
  128. String tokenAttributeValue = "dummy token";
  129. DbProjectBranches.PullRequestData pullRequestData = DbProjectBranches.PullRequestData.newBuilder()
  130. .setBranch(branch)
  131. .setTitle(title)
  132. .setUrl(url)
  133. .putAttributes(tokenAttributeName, tokenAttributeValue)
  134. .build();
  135. BranchDto dto = new BranchDto();
  136. dto.setProjectUuid(projectUuid);
  137. dto.setUuid(uuid);
  138. dto.setBranchType(branchType);
  139. dto.setKey(kee);
  140. dto.setPullRequestData(pullRequestData);
  141. underTest.insert(dbSession, dto);
  142. BranchDto loaded = underTest.selectByUuid(dbSession, dto.getUuid()).get();
  143. assertThat(loaded.getProjectUuid()).isEqualTo(projectUuid);
  144. assertThat(loaded.getUuid()).isEqualTo(uuid);
  145. assertThat(loaded.getBranchType()).isEqualTo(branchType);
  146. assertThat(loaded.getKey()).isEqualTo(kee);
  147. assertThat(loaded.getMergeBranchUuid()).isNull();
  148. DbProjectBranches.PullRequestData loadedPullRequestData = loaded.getPullRequestData();
  149. assertThat(loadedPullRequestData).isNotNull();
  150. assertThat(loadedPullRequestData.getBranch()).isEqualTo(branch);
  151. assertThat(loadedPullRequestData.getTitle()).isEqualTo(title);
  152. assertThat(loadedPullRequestData.getUrl()).isEqualTo(url);
  153. assertThat(loadedPullRequestData.getAttributesMap().get(tokenAttributeName)).isEqualTo(tokenAttributeValue);
  154. }
  155. @Test
  156. public void upsert_branch() {
  157. BranchDto dto = new BranchDto();
  158. dto.setProjectUuid("U1");
  159. dto.setUuid("U2");
  160. dto.setBranchType(BranchType.LONG);
  161. dto.setKey("foo");
  162. underTest.insert(dbSession, dto);
  163. // the fields that can be updated
  164. dto.setMergeBranchUuid("U3");
  165. // the fields that can't be updated. New values are ignored.
  166. dto.setProjectUuid("ignored");
  167. dto.setBranchType(BranchType.SHORT);
  168. underTest.upsert(dbSession, dto);
  169. BranchDto loaded = underTest.selectByBranchKey(dbSession, "U1", "foo").get();
  170. assertThat(loaded.getMergeBranchUuid()).isEqualTo("U3");
  171. assertThat(loaded.getProjectUuid()).isEqualTo("U1");
  172. assertThat(loaded.getBranchType()).isEqualTo(BranchType.LONG);
  173. }
  174. @Test
  175. public void upsert_pull_request() {
  176. BranchDto dto = new BranchDto();
  177. dto.setProjectUuid("U1");
  178. dto.setUuid("U2");
  179. dto.setBranchType(BranchType.PULL_REQUEST);
  180. dto.setKey("foo");
  181. underTest.insert(dbSession, dto);
  182. // the fields that can be updated
  183. dto.setMergeBranchUuid("U3");
  184. String branch = "feature/pr1";
  185. String title = "Dummy Feature Title";
  186. String url = "http://example.com/pullRequests/pr1";
  187. String tokenAttributeName = "token";
  188. String tokenAttributeValue = "dummy token";
  189. DbProjectBranches.PullRequestData pullRequestData = DbProjectBranches.PullRequestData.newBuilder()
  190. .setBranch(branch)
  191. .setTitle(title)
  192. .setUrl(url)
  193. .putAttributes(tokenAttributeName, tokenAttributeValue)
  194. .build();
  195. dto.setPullRequestData(pullRequestData);
  196. // the fields that can't be updated. New values are ignored.
  197. dto.setProjectUuid("ignored");
  198. dto.setBranchType(BranchType.SHORT);
  199. underTest.upsert(dbSession, dto);
  200. BranchDto loaded = underTest.selectByPullRequestKey(dbSession, "U1", "foo").get();
  201. assertThat(loaded.getMergeBranchUuid()).isEqualTo("U3");
  202. assertThat(loaded.getProjectUuid()).isEqualTo("U1");
  203. assertThat(loaded.getBranchType()).isEqualTo(BranchType.PULL_REQUEST);
  204. DbProjectBranches.PullRequestData loadedPullRequestData = loaded.getPullRequestData();
  205. assertThat(loadedPullRequestData).isNotNull();
  206. assertThat(loadedPullRequestData.getBranch()).isEqualTo(branch);
  207. assertThat(loadedPullRequestData.getTitle()).isEqualTo(title);
  208. assertThat(loadedPullRequestData.getUrl()).isEqualTo(url);
  209. assertThat(loadedPullRequestData.getAttributesMap().get(tokenAttributeName)).isEqualTo(tokenAttributeValue);
  210. }
  211. @Test
  212. public void update_pull_request_data() {
  213. BranchDto dto = new BranchDto();
  214. dto.setProjectUuid("U1");
  215. dto.setUuid("U2");
  216. dto.setBranchType(BranchType.PULL_REQUEST);
  217. dto.setKey("foo");
  218. // the fields that can be updated
  219. String mergeBranchUuid = "U3";
  220. dto.setMergeBranchUuid(mergeBranchUuid + "-dummy-suffix");
  221. String branch = "feature/pr1";
  222. String title = "Dummy Feature Title";
  223. String url = "http://example.com/pullRequests/pr1";
  224. String tokenAttributeName = "token";
  225. String tokenAttributeValue = "dummy token";
  226. DbProjectBranches.PullRequestData pullRequestData = DbProjectBranches.PullRequestData.newBuilder()
  227. .setBranch(branch + "-dummy-suffix")
  228. .setTitle(title + "-dummy-suffix")
  229. .setUrl(url + "-dummy-suffix")
  230. .putAttributes(tokenAttributeName, tokenAttributeValue + "-dummy-suffix")
  231. .build();
  232. dto.setPullRequestData(pullRequestData);
  233. underTest.insert(dbSession, dto);
  234. // modify pull request data
  235. dto.setMergeBranchUuid(mergeBranchUuid);
  236. pullRequestData = DbProjectBranches.PullRequestData.newBuilder()
  237. .setBranch(branch)
  238. .setTitle(title)
  239. .setUrl(url)
  240. .putAttributes(tokenAttributeName, tokenAttributeValue)
  241. .build();
  242. dto.setPullRequestData(pullRequestData);
  243. underTest.upsert(dbSession, dto);
  244. BranchDto loaded = underTest.selectByPullRequestKey(dbSession, "U1", "foo").get();
  245. assertThat(loaded.getMergeBranchUuid()).isEqualTo(mergeBranchUuid);
  246. assertThat(loaded.getProjectUuid()).isEqualTo("U1");
  247. assertThat(loaded.getBranchType()).isEqualTo(BranchType.PULL_REQUEST);
  248. DbProjectBranches.PullRequestData loadedPullRequestData = loaded.getPullRequestData();
  249. assertThat(loadedPullRequestData).isNotNull();
  250. assertThat(loadedPullRequestData.getBranch()).isEqualTo(branch);
  251. assertThat(loadedPullRequestData.getTitle()).isEqualTo(title);
  252. assertThat(loadedPullRequestData.getUrl()).isEqualTo(url);
  253. assertThat(loadedPullRequestData.getAttributesMap().get(tokenAttributeName)).isEqualTo(tokenAttributeValue);
  254. }
  255. @Test
  256. public void selectByBranchKey() {
  257. BranchDto mainBranch = new BranchDto();
  258. mainBranch.setProjectUuid("U1");
  259. mainBranch.setUuid("U1");
  260. mainBranch.setBranchType(BranchType.LONG);
  261. mainBranch.setKey("master");
  262. underTest.insert(dbSession, mainBranch);
  263. BranchDto featureBranch = new BranchDto();
  264. featureBranch.setProjectUuid("U1");
  265. featureBranch.setUuid("U2");
  266. featureBranch.setBranchType(BranchType.SHORT);
  267. featureBranch.setKey("feature/foo");
  268. featureBranch.setMergeBranchUuid("U3");
  269. underTest.insert(dbSession, featureBranch);
  270. // select the feature branch
  271. BranchDto loaded = underTest.selectByBranchKey(dbSession, "U1", "feature/foo").get();
  272. assertThat(loaded.getUuid()).isEqualTo(featureBranch.getUuid());
  273. assertThat(loaded.getKey()).isEqualTo(featureBranch.getKey());
  274. assertThat(loaded.getProjectUuid()).isEqualTo(featureBranch.getProjectUuid());
  275. assertThat(loaded.getBranchType()).isEqualTo(featureBranch.getBranchType());
  276. assertThat(loaded.getMergeBranchUuid()).isEqualTo(featureBranch.getMergeBranchUuid());
  277. // select a branch on another project with same branch name
  278. assertThat(underTest.selectByBranchKey(dbSession, "U3", "feature/foo")).isEmpty();
  279. }
  280. @Test
  281. public void selectByPullRequestKey() {
  282. BranchDto mainBranch = new BranchDto();
  283. mainBranch.setProjectUuid("U1");
  284. mainBranch.setUuid("U1");
  285. mainBranch.setBranchType(BranchType.LONG);
  286. mainBranch.setKey("master");
  287. underTest.insert(dbSession, mainBranch);
  288. String pullRequestId = "123";
  289. BranchDto pullRequest = new BranchDto();
  290. pullRequest.setProjectUuid("U1");
  291. pullRequest.setUuid("U2");
  292. pullRequest.setBranchType(BranchType.PULL_REQUEST);
  293. pullRequest.setKey(pullRequestId);
  294. pullRequest.setMergeBranchUuid("U3");
  295. underTest.insert(dbSession, pullRequest);
  296. // select the feature branch
  297. BranchDto loaded = underTest.selectByPullRequestKey(dbSession, "U1", pullRequestId).get();
  298. assertThat(loaded.getUuid()).isEqualTo(pullRequest.getUuid());
  299. assertThat(loaded.getKey()).isEqualTo(pullRequest.getKey());
  300. assertThat(loaded.getProjectUuid()).isEqualTo(pullRequest.getProjectUuid());
  301. assertThat(loaded.getBranchType()).isEqualTo(pullRequest.getBranchType());
  302. assertThat(loaded.getMergeBranchUuid()).isEqualTo(pullRequest.getMergeBranchUuid());
  303. // select a branch on another project with same branch name
  304. assertThat(underTest.selectByPullRequestKey(dbSession, "U3", pullRequestId)).isEmpty();
  305. }
  306. @Test
  307. public void selectByUuids() {
  308. ComponentDto project = db.components().insertPrivateProject();
  309. ComponentDto branch1 = db.components().insertProjectBranch(project);
  310. ComponentDto branch2 = db.components().insertProjectBranch(project);
  311. ComponentDto branch3 = db.components().insertProjectBranch(project);
  312. assertThat(underTest.selectByUuids(db.getSession(), asList(branch1.uuid(), branch2.uuid(), branch3.uuid())))
  313. .extracting(BranchDto::getUuid)
  314. .containsExactlyInAnyOrder(branch1.uuid(), branch2.uuid(), branch3.uuid());
  315. assertThat(underTest.selectByUuids(db.getSession(), singletonList(branch1.uuid())))
  316. .extracting(BranchDto::getUuid)
  317. .containsExactlyInAnyOrder(branch1.uuid());
  318. assertThat(underTest.selectByUuids(db.getSession(), singletonList("unknown"))).isEmpty();
  319. }
  320. @Test
  321. public void selectByUuid() {
  322. ComponentDto project = db.components().insertPrivateProject();
  323. ComponentDto branch1 = db.components().insertProjectBranch(project);
  324. ComponentDto branch2 = db.components().insertProjectBranch(project);
  325. assertThat(underTest.selectByUuid(db.getSession(), branch1.uuid()).get())
  326. .extracting(BranchDto::getUuid)
  327. .containsExactlyInAnyOrder(branch1.uuid());
  328. assertThat(underTest.selectByUuid(db.getSession(), project.uuid())).isNotPresent();
  329. assertThat(underTest.selectByUuid(db.getSession(), "unknown")).isNotPresent();
  330. }
  331. @Test
  332. public void existsNonMainBranch() {
  333. assertThat(underTest.hasNonMainBranches(dbSession)).isFalse();
  334. ComponentDto project = db.components().insertPrivateProject();
  335. assertThat(underTest.hasNonMainBranches(dbSession)).isFalse();
  336. ComponentDto branch1 = db.components().insertProjectBranch(project);
  337. assertThat(underTest.hasNonMainBranches(dbSession)).isTrue();
  338. ComponentDto branch2 = db.components().insertProjectBranch(project);
  339. assertThat(underTest.hasNonMainBranches(dbSession)).isTrue();
  340. }
  341. }