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.

ComponentDaoTest.java 102KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020
  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.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.ArrayList;
  26. import java.util.Arrays;
  27. import java.util.Collections;
  28. import java.util.Date;
  29. import java.util.List;
  30. import java.util.Map;
  31. import java.util.Optional;
  32. import java.util.Random;
  33. import java.util.Set;
  34. import java.util.function.Consumer;
  35. import java.util.function.Supplier;
  36. import java.util.stream.Collectors;
  37. import java.util.stream.IntStream;
  38. import java.util.stream.LongStream;
  39. import java.util.stream.Stream;
  40. import javax.annotation.Nullable;
  41. import org.assertj.core.api.ListAssert;
  42. import org.junit.Rule;
  43. import org.junit.Test;
  44. import org.junit.rules.ExpectedException;
  45. import org.junit.runner.RunWith;
  46. import org.sonar.api.impl.utils.AlwaysIncreasingSystem2;
  47. import org.sonar.api.resources.Qualifiers;
  48. import org.sonar.api.resources.Scopes;
  49. import org.sonar.api.utils.System2;
  50. import org.sonar.db.DbSession;
  51. import org.sonar.db.DbTester;
  52. import org.sonar.db.RowNotFoundException;
  53. import org.sonar.db.metric.MetricDto;
  54. import org.sonar.db.organization.OrganizationDto;
  55. import org.sonar.db.source.FileSourceDto;
  56. import static com.google.common.collect.ImmutableSet.of;
  57. import static com.google.common.collect.Lists.newArrayList;
  58. import static com.google.common.collect.Sets.newHashSet;
  59. import static java.util.Arrays.asList;
  60. import static java.util.Collections.emptyList;
  61. import static java.util.Collections.emptySet;
  62. import static java.util.Collections.singleton;
  63. import static java.util.Collections.singletonList;
  64. import static java.util.stream.Collectors.toSet;
  65. import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
  66. import static org.assertj.core.api.Assertions.assertThat;
  67. import static org.assertj.core.api.Assertions.entry;
  68. import static org.assertj.core.api.Assertions.tuple;
  69. import static org.sonar.api.resources.Qualifiers.APP;
  70. import static org.sonar.api.resources.Qualifiers.PROJECT;
  71. import static org.sonar.api.utils.DateUtils.parseDate;
  72. import static org.sonar.db.component.BranchType.PULL_REQUEST;
  73. import static org.sonar.db.component.ComponentTesting.newBranchDto;
  74. import static org.sonar.db.component.ComponentTesting.newDirectory;
  75. import static org.sonar.db.component.ComponentTesting.newFileDto;
  76. import static org.sonar.db.component.ComponentTesting.newModuleDto;
  77. import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
  78. import static org.sonar.db.component.ComponentTesting.newProjectCopy;
  79. import static org.sonar.db.component.ComponentTesting.newSubView;
  80. import static org.sonar.db.component.ComponentTesting.newView;
  81. import static org.sonar.db.component.ComponentTreeQuery.Strategy.CHILDREN;
  82. import static org.sonar.db.component.ComponentTreeQuery.Strategy.LEAVES;
  83. @RunWith(DataProviderRunner.class)
  84. public class ComponentDaoTest {
  85. private static final String PROJECT_UUID = "project-uuid";
  86. private static final String MODULE_UUID = "module-uuid";
  87. private static final String FILE_1_UUID = "file-1-uuid";
  88. private static final String FILE_2_UUID = "file-2-uuid";
  89. private static final String FILE_3_UUID = "file-3-uuid";
  90. private static final String A_VIEW_UUID = "view-uuid";
  91. private static final ComponentQuery ALL_PROJECTS_COMPONENT_QUERY = ComponentQuery.builder().setQualifiers("TRK").build();
  92. @Rule
  93. public ExpectedException expectedException = ExpectedException.none();
  94. private System2 system2 = AlwaysIncreasingSystem2.INSTANCE;
  95. @Rule
  96. public DbTester db = DbTester.create(system2);
  97. private Random random = new Random();
  98. private DbSession dbSession = db.getSession();
  99. private ComponentDao underTest = new ComponentDao();
  100. private static ComponentTreeQuery.Builder newTreeQuery(String baseUuid) {
  101. return ComponentTreeQuery.builder()
  102. .setBaseUuid(baseUuid)
  103. .setStrategy(CHILDREN);
  104. }
  105. @Test
  106. public void get_by_uuid() {
  107. OrganizationDto organization = db.organizations().insert();
  108. ComponentDto project = db.components().insertPrivateProject(organization, p -> p
  109. .setDbKey("org.struts:struts")
  110. .setName("Struts")
  111. .setLongName("Apache Struts"));
  112. ComponentDto anotherProject = db.components().insertPrivateProject(organization);
  113. ComponentDto result = underTest.selectByUuid(dbSession, project.uuid()).get();
  114. assertThat(result).isNotNull();
  115. assertThat(result.getOrganizationUuid()).isEqualTo(organization.getUuid());
  116. assertThat(result.uuid()).isEqualTo(project.uuid());
  117. assertThat(result.getUuidPath()).isEqualTo(".");
  118. assertThat(result.moduleUuid()).isNull();
  119. assertThat(result.moduleUuidPath()).isEqualTo("." + project.uuid() + ".");
  120. assertThat(result.projectUuid()).isEqualTo(project.uuid());
  121. assertThat(result.getDbKey()).isEqualTo("org.struts:struts");
  122. assertThat(result.path()).isNull();
  123. assertThat(result.name()).isEqualTo("Struts");
  124. assertThat(result.longName()).isEqualTo("Apache Struts");
  125. assertThat(result.qualifier()).isEqualTo("TRK");
  126. assertThat(result.scope()).isEqualTo("PRJ");
  127. assertThat(result.language()).isNull();
  128. assertThat(result.getCopyResourceUuid()).isNull();
  129. assertThat(result.isPrivate()).isTrue();
  130. assertThat(underTest.selectByUuid(dbSession, "UNKNOWN")).isEmpty();
  131. }
  132. @Test
  133. public void get_by_uuid_on_technical_project_copy() {
  134. ComponentDto view = db.components().insertView();
  135. ComponentDto project = db.components().insertPublicProject(p -> p
  136. .setDbKey("org.struts:struts")
  137. .setName("Struts")
  138. .setLongName("Apache Struts"));
  139. ComponentDto projectCopy = db.components().insertComponent(newProjectCopy(project, view));
  140. ComponentDto anotherProject = db.components().insertPrivateProject();
  141. ComponentDto anotherProjectCopy = db.components().insertComponent(newProjectCopy(anotherProject, view));
  142. ComponentDto result = underTest.selectByUuid(dbSession, projectCopy.uuid()).get();
  143. assertThat(result.uuid()).isEqualTo(projectCopy.uuid());
  144. assertThat(result.moduleUuid()).isEqualTo(view.uuid());
  145. assertThat(result.moduleUuidPath()).isEqualTo("." + view.uuid() + ".");
  146. assertThat(result.projectUuid()).isEqualTo(view.uuid());
  147. assertThat(result.getDbKey()).isEqualTo(view.getDbKey() + project.getDbKey());
  148. assertThat(result.path()).isNull();
  149. assertThat(result.name()).isEqualTo("Struts");
  150. assertThat(result.longName()).isEqualTo("Apache Struts");
  151. assertThat(result.qualifier()).isEqualTo("TRK");
  152. assertThat(result.scope()).isEqualTo("FIL");
  153. assertThat(result.language()).isNull();
  154. assertThat(result.getCopyResourceUuid()).isEqualTo(project.uuid());
  155. assertThat(result.isPrivate()).isFalse();
  156. }
  157. @Test
  158. public void selectByUuid_on_disabled_component() {
  159. ComponentDto enabledProject = db.components().insertPublicProject(p -> p.setEnabled(true));
  160. ComponentDto disabledProject = db.components().insertPublicProject(p -> p.setEnabled(false));
  161. ComponentDto result = underTest.selectByUuid(dbSession, disabledProject.uuid()).get();
  162. assertThat(result).isNotNull();
  163. assertThat(result.isEnabled()).isFalse();
  164. }
  165. @Test
  166. public void selectOrFailByUuid_fails_when_component_not_found() {
  167. db.components().insertPublicProject();
  168. expectedException.expect(RowNotFoundException.class);
  169. underTest.selectOrFailByUuid(dbSession, "unknown");
  170. }
  171. @Test
  172. public void selectByKey() {
  173. OrganizationDto organization = db.organizations().insert();
  174. ComponentDto project = db.components().insertPrivateProject(organization);
  175. ComponentDto directory = db.components().insertComponent(newDirectory(project, "src"));
  176. ComponentDto file = db.components().insertComponent(newFileDto(project, directory)
  177. .setDbKey("org.struts:struts-core:src/org/struts/RequestContext.java")
  178. .setName("RequestContext.java")
  179. .setLongName("org.struts.RequestContext")
  180. .setLanguage("java")
  181. .setPath("src/RequestContext.java"));
  182. Optional<ComponentDto> optional = underTest.selectByKey(dbSession, file.getDbKey());
  183. ComponentDto result = optional.get();
  184. assertThat(result.getOrganizationUuid()).isEqualTo(organization.getUuid());
  185. assertThat(result.uuid()).isEqualTo(file.uuid());
  186. assertThat(result.getDbKey()).isEqualTo("org.struts:struts-core:src/org/struts/RequestContext.java");
  187. assertThat(result.path()).isEqualTo("src/RequestContext.java");
  188. assertThat(result.name()).isEqualTo("RequestContext.java");
  189. assertThat(result.longName()).isEqualTo("org.struts.RequestContext");
  190. assertThat(result.qualifier()).isEqualTo("FIL");
  191. assertThat(result.scope()).isEqualTo("FIL");
  192. assertThat(result.language()).isEqualTo("java");
  193. assertThat(result.projectUuid()).isEqualTo(project.uuid());
  194. assertThat(underTest.selectByKey(dbSession, "unknown")).isEmpty();
  195. }
  196. @Test
  197. @UseDataProvider("branchBranchTypes")
  198. public void selectByKeyAndBranch(BranchType branchType) {
  199. ComponentDto project = db.components().insertMainBranch();
  200. ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch").setBranchType(branchType));
  201. ComponentDto file = db.components().insertComponent(newFileDto(branch));
  202. assertThat(underTest.selectByKeyAndBranch(dbSession, project.getKey(), "master").get().uuid()).isEqualTo(project.uuid());
  203. assertThat(underTest.selectByKeyAndBranch(dbSession, branch.getKey(), "my_branch").get().uuid()).isEqualTo(branch.uuid());
  204. assertThat(underTest.selectByKeyAndBranch(dbSession, file.getKey(), "my_branch").get().uuid()).isEqualTo(file.uuid());
  205. assertThat(underTest.selectByKeyAndBranch(dbSession, "unknown", "my_branch")).isNotPresent();
  206. assertThat(underTest.selectByKeyAndBranch(dbSession, file.getKey(), "unknown")).isNotPresent();
  207. }
  208. @DataProvider
  209. public static Object[][] branchBranchTypes() {
  210. return new Object[][]{
  211. {BranchType.SHORT},
  212. {BranchType.LONG}
  213. };
  214. }
  215. @Test
  216. public void selectByKeyAndPullRequest() {
  217. ComponentDto project = db.components().insertMainBranch();
  218. ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
  219. ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setKey("my_PR").setBranchType(PULL_REQUEST));
  220. ComponentDto pullRequestNamedAsMainBranch = db.components().insertProjectBranch(project, b -> b.setKey("master").setBranchType(PULL_REQUEST));
  221. ComponentDto pullRequestNamedAsBranch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch").setBranchType(PULL_REQUEST));
  222. ComponentDto file = db.components().insertComponent(newFileDto(pullRequest));
  223. assertThat(underTest.selectByKeyAndPullRequest(dbSession, project.getKey(), "my_PR").get().uuid()).isEqualTo(pullRequest.uuid());
  224. assertThat(underTest.selectByKeyAndBranch(dbSession, project.getKey(), "master").get().uuid()).isEqualTo(project.uuid());
  225. assertThat(underTest.selectByKeyAndPullRequest(dbSession, project.getKey(), "master").get().uuid()).isEqualTo(pullRequestNamedAsMainBranch.uuid());
  226. assertThat(underTest.selectByKeyAndBranch(dbSession, branch.getKey(), "my_branch").get().uuid()).isEqualTo(branch.uuid());
  227. assertThat(underTest.selectByKeyAndPullRequest(dbSession, branch.getKey(), "my_branch").get().uuid()).isEqualTo(pullRequestNamedAsBranch.uuid());
  228. assertThat(underTest.selectByKeyAndPullRequest(dbSession, file.getKey(), "my_PR").get().uuid()).isEqualTo(file.uuid());
  229. assertThat(underTest.selectByKeyAndPullRequest(dbSession, "unknown", "my_branch")).isNotPresent();
  230. assertThat(underTest.selectByKeyAndPullRequest(dbSession, file.getKey(), "unknown")).isNotPresent();
  231. }
  232. @Test
  233. public void selectOrFailByKey_fails_when_component_not_found() {
  234. db.components().insertPrivateProject();
  235. expectedException.expect(RowNotFoundException.class);
  236. underTest.selectOrFailByKey(dbSession, "unknown");
  237. }
  238. @Test
  239. public void get_by_key_on_disabled_component() {
  240. ComponentDto project = db.components().insertPrivateProject(p -> p.setEnabled(false));
  241. ComponentDto result = underTest.selectOrFailByKey(dbSession, project.getDbKey());
  242. assertThat(result.isEnabled()).isFalse();
  243. }
  244. @Test
  245. public void get_by_key_on_a_root_project() {
  246. ComponentDto project = db.components().insertPrivateProject();
  247. ComponentDto result = underTest.selectOrFailByKey(dbSession, project.getDbKey());
  248. assertThat(result.getDbKey()).isEqualTo(project.getDbKey());
  249. assertThat(result.uuid()).isEqualTo(project.uuid());
  250. assertThat(result.getUuidPath()).isEqualTo(project.getUuidPath());
  251. assertThat(result.getRootUuid()).isEqualTo(project.uuid());
  252. assertThat(result.projectUuid()).isEqualTo(project.uuid());
  253. }
  254. @Test
  255. public void get_by_keys() {
  256. ComponentDto project1 = db.components().insertPrivateProject();
  257. ComponentDto project2 = db.components().insertPrivateProject();
  258. List<ComponentDto> results = underTest.selectByKeys(dbSession, asList(project1.getDbKey(), project2.getDbKey()));
  259. assertThat(results)
  260. .extracting(ComponentDto::uuid, ComponentDto::getDbKey)
  261. .containsExactlyInAnyOrder(
  262. tuple(project1.uuid(), project1.getDbKey()),
  263. tuple(project2.uuid(), project2.getDbKey()));
  264. assertThat(underTest.selectByKeys(dbSession, singletonList("unknown"))).isEmpty();
  265. }
  266. @Test
  267. public void selectByKeysAndBranch() {
  268. ComponentDto project = db.components().insertMainBranch();
  269. ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
  270. ComponentDto file1 = db.components().insertComponent(newFileDto(branch));
  271. ComponentDto file2 = db.components().insertComponent(newFileDto(branch));
  272. ComponentDto anotherBranch = db.components().insertProjectBranch(project, b -> b.setKey("another_branch"));
  273. ComponentDto fileOnAnotherBranch = db.components().insertComponent(newFileDto(anotherBranch));
  274. assertThat(underTest.selectByKeysAndBranch(dbSession, asList(branch.getKey(), file1.getKey(), file2.getKey()), "my_branch")).extracting(ComponentDto::uuid)
  275. .containsExactlyInAnyOrder(branch.uuid(), file1.uuid(), file2.uuid());
  276. assertThat(underTest.selectByKeysAndBranch(dbSession, asList(file1.getKey(), file2.getKey(), fileOnAnotherBranch.getKey()), "my_branch")).extracting(ComponentDto::uuid)
  277. .containsExactlyInAnyOrder(file1.uuid(), file2.uuid());
  278. assertThat(underTest.selectByKeysAndBranch(dbSession, singletonList(fileOnAnotherBranch.getKey()), "my_branch")).isEmpty();
  279. assertThat(underTest.selectByKeysAndBranch(dbSession, singletonList(file1.getKey()), "unknown")).isEmpty();
  280. assertThat(underTest.selectByKeysAndBranch(dbSession, singletonList("unknown"), "my_branch")).isEmpty();
  281. assertThat(underTest.selectByKeysAndBranch(dbSession, singletonList(branch.getKey()), "master")).extracting(ComponentDto::uuid).containsExactlyInAnyOrder(project.uuid());
  282. }
  283. @Test
  284. public void select_by_keys_and_branches() {
  285. ComponentDto project = db.components().insertMainBranch();
  286. ComponentDto projectBranch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
  287. ComponentDto application = db.components().insertMainBranch(a -> a.setQualifier(APP));
  288. ComponentDto applicationBranch = db.components().insertProjectBranch(application, b -> b.setKey("my_branch"));
  289. assertThat(underTest.selectByKeysAndBranches(db.getSession(), ImmutableMap.of(
  290. projectBranch.getKey(), projectBranch.getBranch(),
  291. applicationBranch.getKey(), applicationBranch.getBranch())))
  292. .extracting(ComponentDto::getKey, ComponentDto::getBranch)
  293. .containsExactlyInAnyOrder(
  294. tuple(projectBranch.getKey(), "my_branch"),
  295. tuple(applicationBranch.getKey(), "my_branch"));
  296. assertThat(underTest.selectByKeysAndBranches(db.getSession(), ImmutableMap.of(
  297. projectBranch.getKey(), "unknown",
  298. "unknown", projectBranch.getBranch())))
  299. .extracting(ComponentDto::getDbKey)
  300. .isEmpty();
  301. assertThat(underTest.selectByKeysAndBranches(db.getSession(), Collections.emptyMap())).isEmpty();
  302. }
  303. @Test
  304. public void get_by_ids() {
  305. ComponentDto project1 = db.components().insertPrivateProject();
  306. ComponentDto project2 = db.components().insertPrivateProject();
  307. List<ComponentDto> results = underTest.selectByIds(dbSession, asList(project1.getId(), project2.getId()));
  308. assertThat(results)
  309. .extracting(ComponentDto::uuid, ComponentDto::getDbKey)
  310. .containsExactlyInAnyOrder(
  311. tuple(project1.uuid(), project1.getDbKey()),
  312. tuple(project2.uuid(), project2.getDbKey()));
  313. assertThat(underTest.selectByIds(dbSession, singletonList(0L))).isEmpty();
  314. }
  315. @Test
  316. public void get_by_uuids() {
  317. ComponentDto project1 = db.components().insertPrivateProject();
  318. ComponentDto project2 = db.components().insertPrivateProject();
  319. List<ComponentDto> results = underTest.selectByUuids(dbSession, asList(project1.uuid(), project2.uuid()));
  320. assertThat(results)
  321. .extracting(ComponentDto::uuid, ComponentDto::getDbKey)
  322. .containsExactlyInAnyOrder(
  323. tuple(project1.uuid(), project1.getDbKey()),
  324. tuple(project2.uuid(), project2.getDbKey()));
  325. assertThat(underTest.selectByUuids(dbSession, singletonList("unknown"))).isEmpty();
  326. }
  327. @Test
  328. public void get_by_uuids_on_removed_components() {
  329. ComponentDto project1 = db.components().insertPrivateProject();
  330. ComponentDto project2 = db.components().insertPrivateProject(p -> p.setEnabled(false));
  331. List<ComponentDto> results = underTest.selectByUuids(dbSession, asList(project1.uuid(), project2.uuid()));
  332. assertThat(results)
  333. .extracting(ComponentDto::getDbKey, ComponentDto::isEnabled)
  334. .containsExactlyInAnyOrder(
  335. tuple(project1.getDbKey(), true),
  336. tuple(project2.getDbKey(), false));
  337. }
  338. @Test
  339. public void select_existing_uuids() {
  340. ComponentDto project1 = db.components().insertPrivateProject();
  341. ComponentDto project2 = db.components().insertPrivateProject(p -> p.setEnabled(false));
  342. assertThat(underTest.selectExistingUuids(dbSession, asList(project1.uuid(), project2.uuid()))).containsExactlyInAnyOrder(project1.uuid(), project2.uuid());
  343. assertThat(underTest.selectExistingUuids(dbSession, asList(project1.uuid(), "unknown"))).containsExactlyInAnyOrder(project1.uuid());
  344. assertThat(underTest.selectExistingUuids(dbSession, singletonList("unknown"))).isEmpty();
  345. }
  346. @Test
  347. public void get_by_id() {
  348. ComponentDto project = db.components().insertPrivateProject();
  349. assertThat(underTest.selectById(dbSession, project.getId())).isNotNull();
  350. }
  351. @Test
  352. public void get_by_id_on_disabled_component() {
  353. ComponentDto enabledProject = db.components().insertPrivateProject();
  354. ComponentDto disabledProject = db.components().insertPrivateProject(p -> p.setEnabled(false));
  355. Optional<ComponentDto> result = underTest.selectById(dbSession, disabledProject.getId());
  356. assertThat(result).isPresent();
  357. assertThat(result.get().isEnabled()).isFalse();
  358. }
  359. @Test
  360. public void get_nullable_by_id() {
  361. ComponentDto project = db.components().insertPrivateProject();
  362. assertThat(underTest.selectById(dbSession, project.getId())).isPresent();
  363. assertThat(underTest.selectById(dbSession, 0L)).isEmpty();
  364. }
  365. @Test
  366. public void select_component_keys_by_qualifiers() {
  367. ComponentDto project = db.components().insertPrivateProject();
  368. ComponentDto module = db.components().insertComponent(newModuleDto(project));
  369. ComponentDto directory = db.components().insertComponent(newDirectory(module, "src"));
  370. ComponentDto file = db.components().insertComponent(newFileDto(directory));
  371. assertThat(underTest.selectComponentsByQualifiers(dbSession, newHashSet("TRK"))).extracting(ComponentDto::getDbKey).containsExactlyInAnyOrder(project.getDbKey());
  372. assertThat(underTest.selectComponentsByQualifiers(dbSession, newHashSet("BRC"))).extracting(ComponentDto::getDbKey).containsExactlyInAnyOrder(module.getDbKey());
  373. assertThat(underTest.selectComponentsByQualifiers(dbSession, newHashSet("DIR"))).extracting(ComponentDto::getDbKey).containsExactlyInAnyOrder(directory.getDbKey());
  374. assertThat(underTest.selectComponentsByQualifiers(dbSession, newHashSet("FIL"))).extracting(ComponentDto::getDbKey).containsExactlyInAnyOrder(file.getDbKey());
  375. assertThat(underTest.selectComponentsByQualifiers(dbSession, newHashSet("unknown"))).isEmpty();
  376. }
  377. @Test
  378. public void fail_with_IAE_select_component_keys_by_qualifiers_on_empty_qualifier() {
  379. expectedException.expect(IllegalArgumentException.class);
  380. expectedException.expectMessage("Qualifiers cannot be empty");
  381. underTest.selectComponentsByQualifiers(dbSession, Collections.emptySet());
  382. }
  383. @Test
  384. public void count_enabled_modules_by_project_uuid() {
  385. ComponentDto project = db.components().insertPrivateProject();
  386. ComponentDto module = db.components().insertComponent(newModuleDto(project));
  387. db.components().insertComponent(newModuleDto(module));
  388. ComponentDto subModule2 = newModuleDto(module);
  389. subModule2.setEnabled(false);
  390. db.components().insertComponent(subModule2);
  391. int result = underTest.countEnabledModulesByProjectUuid(dbSession, project.uuid());
  392. assertThat(result).isEqualTo(2);
  393. }
  394. @Test
  395. public void find_sub_projects_by_component_keys() {
  396. ComponentDto project = db.components().insertPrivateProject();
  397. ComponentDto removedProject = db.components().insertPrivateProject(p -> p.setEnabled(false));
  398. ComponentDto module = db.components().insertComponent(newModuleDto(project));
  399. ComponentDto removedModule = db.components().insertComponent(newModuleDto(project).setEnabled(false));
  400. ComponentDto subModule = db.components().insertComponent(newModuleDto(module));
  401. ComponentDto removedSubModule = db.components().insertComponent(newModuleDto(module).setEnabled(false));
  402. ComponentDto directory = db.components().insertComponent(newDirectory(subModule, "src"));
  403. ComponentDto removedDirectory = db.components().insertComponent(newDirectory(subModule, "src2").setEnabled(false));
  404. ComponentDto file = db.components().insertComponent(newFileDto(subModule, directory));
  405. ComponentDto removedFile = db.components().insertComponent(newFileDto(subModule, directory).setEnabled(false));
  406. // Sub project of a file
  407. assertThat(underTest.selectSubProjectsByComponentUuids(dbSession, singletonList(file.uuid())))
  408. .extracting(ComponentDto::getDbKey)
  409. .containsExactlyInAnyOrder(subModule.getDbKey());
  410. // Sub project of a directory
  411. assertThat(underTest.selectSubProjectsByComponentUuids(dbSession, singletonList(directory.uuid())))
  412. .extracting(ComponentDto::uuid)
  413. .containsExactlyInAnyOrder(subModule.uuid());
  414. // Sub project of a sub module
  415. assertThat(underTest.selectSubProjectsByComponentUuids(dbSession, singletonList(subModule.uuid())))
  416. .extracting(ComponentDto::uuid)
  417. .containsExactlyInAnyOrder(module.uuid());
  418. // Sub project of a module
  419. assertThat(underTest.selectSubProjectsByComponentUuids(dbSession, singletonList(module.uuid())))
  420. .extracting(ComponentDto::uuid)
  421. .containsExactlyInAnyOrder(project.uuid());
  422. // Sub project of a project
  423. assertThat(underTest.selectSubProjectsByComponentUuids(dbSession, singletonList(project.uuid())))
  424. .extracting(ComponentDto::uuid)
  425. .containsExactlyInAnyOrder(project.uuid());
  426. // SUb projects of a component and a sub module
  427. assertThat(underTest.selectSubProjectsByComponentUuids(dbSession, asList(file.uuid(), subModule.uuid())))
  428. .extracting(ComponentDto::uuid)
  429. .containsExactlyInAnyOrder(subModule.uuid(), module.uuid());
  430. assertThat(underTest.selectSubProjectsByComponentUuids(dbSession, singletonList("unknown"))).isEmpty();
  431. assertThat(underTest.selectSubProjectsByComponentUuids(dbSession, Collections.emptyList())).isEmpty();
  432. }
  433. @Test
  434. public void select_enabled_modules_tree() {
  435. ComponentDto project = db.components().insertPrivateProject();
  436. ComponentDto removedProject = db.components().insertPrivateProject(p -> p.setEnabled(false));
  437. ComponentDto module = db.components().insertComponent(newModuleDto(project));
  438. ComponentDto removedModule = db.components().insertComponent(newModuleDto(project).setEnabled(false));
  439. ComponentDto subModule = db.components().insertComponent(newModuleDto(module));
  440. ComponentDto removedSubModule = db.components().insertComponent(newModuleDto(module).setEnabled(false));
  441. ComponentDto directory = db.components().insertComponent(newDirectory(subModule, "src"));
  442. ComponentDto removedDirectory = db.components().insertComponent(newDirectory(subModule, "src2").setEnabled(false));
  443. ComponentDto file = db.components().insertComponent(newFileDto(subModule, directory));
  444. ComponentDto removedFile = db.components().insertComponent(newFileDto(subModule, directory).setEnabled(false));
  445. // From root project
  446. assertThat(underTest.selectEnabledDescendantModules(dbSession, project.uuid()))
  447. .extracting(ComponentDto::uuid)
  448. .containsExactlyInAnyOrder(project.uuid(), module.uuid(), subModule.uuid())
  449. .doesNotContain(removedModule.uuid(), removedSubModule.uuid());
  450. // From module
  451. assertThat(underTest.selectEnabledDescendantModules(dbSession, module.uuid()))
  452. .extracting(ComponentDto::uuid)
  453. .containsExactlyInAnyOrder(module.uuid(), subModule.uuid())
  454. .doesNotContain(removedModule.uuid(), removedModule.uuid());
  455. // From sub module
  456. assertThat(underTest.selectEnabledDescendantModules(dbSession, subModule.uuid()))
  457. .extracting(ComponentDto::uuid)
  458. .containsExactlyInAnyOrder(subModule.uuid());
  459. // Folder
  460. assertThat(underTest.selectEnabledDescendantModules(dbSession, directory.uuid())).isEmpty();
  461. assertThat(underTest.selectEnabledDescendantModules(dbSession, "unknown")).isEmpty();
  462. }
  463. @Test
  464. public void select_enabled_components_with_module_dto() {
  465. ComponentDto project = db.components().insertPrivateProject();
  466. ComponentDto module = db.components().insertComponent(newModuleDto(project));
  467. ComponentDto removedModule = db.components().insertComponent(newModuleDto(project).setEnabled(false));
  468. ComponentDto subModule = db.components().insertComponent(newModuleDto(module));
  469. ComponentDto removedSubModule = db.components().insertComponent(newModuleDto(module).setEnabled(false));
  470. ComponentDto directory = db.components().insertComponent(newDirectory(subModule, "src"));
  471. ComponentDto removedDirectory = db.components().insertComponent(newDirectory(subModule, "src2").setEnabled(false));
  472. ComponentDto file = db.components().insertComponent(newFileDto(subModule, directory));
  473. ComponentDto removedFile = db.components().insertComponent(newFileDto(subModule, directory).setEnabled(false));
  474. // From root project
  475. assertThat(underTest.selectEnabledComponentsWithModuleUuidFromProjectKey(dbSession, project.getDbKey()))
  476. .extracting(ComponentWithModuleUuidDto::uuid)
  477. .containsExactlyInAnyOrder(
  478. project.uuid(),
  479. module.uuid(),
  480. subModule.uuid(),
  481. directory.uuid(),
  482. file.uuid());
  483. }
  484. @Test
  485. public void select_all_modules_tree() {
  486. ComponentDto project = db.components().insertPrivateProject();
  487. ComponentDto removedProject = db.components().insertPrivateProject(p -> p.setEnabled(false));
  488. ComponentDto module = db.components().insertComponent(newModuleDto(project));
  489. ComponentDto removedModule = db.components().insertComponent(newModuleDto(project).setEnabled(false));
  490. ComponentDto subModule = db.components().insertComponent(newModuleDto(module));
  491. ComponentDto removedSubModule = db.components().insertComponent(newModuleDto(module).setEnabled(false));
  492. ComponentDto directory = db.components().insertComponent(newDirectory(subModule, "src"));
  493. ComponentDto removedDirectory = db.components().insertComponent(newDirectory(subModule, "src2").setEnabled(false));
  494. ComponentDto file = db.components().insertComponent(newFileDto(subModule, directory));
  495. ComponentDto removedFile = db.components().insertComponent(newFileDto(subModule, directory).setEnabled(false));
  496. // From root project, disabled sub module is returned
  497. assertThat(underTest.selectDescendantModules(dbSession, project.uuid()))
  498. .extracting(ComponentDto::uuid)
  499. .containsExactlyInAnyOrder(project.uuid(), module.uuid(), removedModule.uuid(), subModule.uuid(), removedSubModule.uuid());
  500. // From module, disabled sub module is returned
  501. assertThat(underTest.selectDescendantModules(dbSession, module.uuid()))
  502. .extracting(ComponentDto::uuid)
  503. .containsExactlyInAnyOrder(module.uuid(), subModule.uuid(), removedSubModule.uuid());
  504. // From removed sub module -> should not be returned
  505. assertThat(underTest.selectDescendantModules(dbSession, removedSubModule.uuid())).isEmpty();
  506. }
  507. @Test
  508. public void select_enabled_module_files_tree_from_module() {
  509. ComponentDto project = db.components().insertPrivateProject();
  510. ComponentDto module = db.components().insertComponent(newModuleDto(project));
  511. ComponentDto fileDirectlyOnModule = db.components().insertComponent(newFileDto(module));
  512. FileSourceDto fileSourceDirectlyOnModule = db.fileSources().insertFileSource(fileDirectlyOnModule);
  513. ComponentDto subModule = db.components().insertComponent(newModuleDto(module));
  514. ComponentDto directory = db.components().insertComponent(newDirectory(subModule, "src"));
  515. ComponentDto file = db.components().insertComponent(newFileDto(subModule, directory));
  516. FileSourceDto fileSource = db.fileSources().insertFileSource(file);
  517. // From root project
  518. assertThat(underTest.selectEnabledDescendantFiles(dbSession, project.uuid()))
  519. .extracting(FilePathWithHashDto::getUuid, FilePathWithHashDto::getModuleUuid, FilePathWithHashDto::getSrcHash, FilePathWithHashDto::getPath, FilePathWithHashDto::getRevision)
  520. .containsExactlyInAnyOrder(
  521. tuple(fileDirectlyOnModule.uuid(), module.uuid(), fileSourceDirectlyOnModule.getSrcHash(), fileDirectlyOnModule.path(), fileSourceDirectlyOnModule.getRevision()),
  522. tuple(file.uuid(), subModule.uuid(), fileSource.getSrcHash(), file.path(), fileSource.getRevision()));
  523. // From module
  524. assertThat(underTest.selectEnabledDescendantFiles(dbSession, module.uuid()))
  525. .extracting(FilePathWithHashDto::getUuid, FilePathWithHashDto::getModuleUuid, FilePathWithHashDto::getSrcHash, FilePathWithHashDto::getPath, FilePathWithHashDto::getRevision)
  526. .containsExactlyInAnyOrder(
  527. tuple(fileDirectlyOnModule.uuid(), module.uuid(), fileSourceDirectlyOnModule.getSrcHash(), fileDirectlyOnModule.path(), fileSourceDirectlyOnModule.getRevision()),
  528. tuple(file.uuid(), subModule.uuid(), fileSource.getSrcHash(), file.path(), fileSource.getRevision()));
  529. // From sub module
  530. assertThat(underTest.selectEnabledDescendantFiles(dbSession, subModule.uuid()))
  531. .extracting(FilePathWithHashDto::getUuid, FilePathWithHashDto::getModuleUuid, FilePathWithHashDto::getSrcHash, FilePathWithHashDto::getPath, FilePathWithHashDto::getRevision)
  532. .containsExactlyInAnyOrder(
  533. tuple(file.uuid(), subModule.uuid(), fileSource.getSrcHash(), file.path(), fileSource.getRevision()));
  534. // From directory
  535. assertThat(underTest.selectEnabledDescendantFiles(dbSession, directory.uuid())).isEmpty();
  536. assertThat(underTest.selectEnabledDescendantFiles(dbSession, "unknown")).isEmpty();
  537. }
  538. @Test
  539. public void select_enabled_module_files_tree_from_project() {
  540. ComponentDto project = db.components().insertPrivateProject();
  541. ComponentDto module = db.components().insertComponent(newModuleDto(project));
  542. ComponentDto fileDirectlyOnModule = db.components().insertComponent(newFileDto(module));
  543. FileSourceDto fileSourceDirectlyOnModule = db.fileSources().insertFileSource(fileDirectlyOnModule);
  544. ComponentDto subModule = db.components().insertComponent(newModuleDto(module));
  545. ComponentDto directory = db.components().insertComponent(newDirectory(subModule, "src"));
  546. ComponentDto file = db.components().insertComponent(newFileDto(subModule, directory));
  547. FileSourceDto fileSource = db.fileSources().insertFileSource(file);
  548. // From root project
  549. assertThat(underTest.selectEnabledFilesFromProject(dbSession, project.uuid()))
  550. .extracting(FilePathWithHashDto::getUuid, FilePathWithHashDto::getModuleUuid, FilePathWithHashDto::getSrcHash, FilePathWithHashDto::getPath, FilePathWithHashDto::getRevision)
  551. .containsExactlyInAnyOrder(
  552. tuple(fileDirectlyOnModule.uuid(), module.uuid(), fileSourceDirectlyOnModule.getSrcHash(), fileDirectlyOnModule.path(), fileSourceDirectlyOnModule.getRevision()),
  553. tuple(file.uuid(), subModule.uuid(), fileSource.getSrcHash(), file.path(), fileSource.getRevision()));
  554. // From module
  555. assertThat(underTest.selectEnabledFilesFromProject(dbSession, module.uuid())).isEmpty();
  556. // From sub module
  557. assertThat(underTest.selectEnabledFilesFromProject(dbSession, subModule.uuid())).isEmpty();
  558. // From directory
  559. assertThat(underTest.selectEnabledFilesFromProject(dbSession, directory.uuid())).isEmpty();
  560. assertThat(underTest.selectEnabledFilesFromProject(dbSession, "unknown")).isEmpty();
  561. }
  562. @Test
  563. public void select_all_components_from_project() {
  564. ComponentDto project = db.components().insertPrivateProject();
  565. ComponentDto removedProject = db.components().insertPrivateProject(p -> p.setEnabled(false));
  566. ComponentDto module = db.components().insertComponent(newModuleDto(project));
  567. ComponentDto removedModule = db.components().insertComponent(newModuleDto(project).setEnabled(false));
  568. ComponentDto subModule = db.components().insertComponent(newModuleDto(module));
  569. ComponentDto removedSubModule = db.components().insertComponent(newModuleDto(module).setEnabled(false));
  570. ComponentDto directory = db.components().insertComponent(newDirectory(subModule, "src"));
  571. ComponentDto removedDirectory = db.components().insertComponent(newDirectory(subModule, "src2").setEnabled(false));
  572. ComponentDto file = db.components().insertComponent(newFileDto(subModule, directory));
  573. ComponentDto removedFile = db.components().insertComponent(newFileDto(subModule, directory).setEnabled(false));
  574. // Removed components are included
  575. assertThat(underTest.selectAllComponentsFromProjectKey(dbSession, project.getDbKey()))
  576. .extracting(ComponentDto::getDbKey)
  577. .containsExactlyInAnyOrder(project.getDbKey(), module.getDbKey(), removedModule.getDbKey(), subModule.getDbKey(), removedSubModule.getDbKey(),
  578. directory.getDbKey(), removedDirectory.getDbKey(), file.getDbKey(), removedFile.getDbKey());
  579. assertThat(underTest.selectAllComponentsFromProjectKey(dbSession, "UNKNOWN")).isEmpty();
  580. }
  581. @Test
  582. public void select_uuids_by_key_from_project() {
  583. ComponentDto project = db.components().insertPrivateProject();
  584. ComponentDto removedProject = db.components().insertPrivateProject(p -> p.setEnabled(false));
  585. ComponentDto module = db.components().insertComponent(newModuleDto(project));
  586. ComponentDto removedModule = db.components().insertComponent(newModuleDto(project).setEnabled(false));
  587. ComponentDto subModule = db.components().insertComponent(newModuleDto(module));
  588. ComponentDto removedSubModule = db.components().insertComponent(newModuleDto(module).setEnabled(false));
  589. ComponentDto directory = db.components().insertComponent(newDirectory(subModule, "src"));
  590. ComponentDto removedDirectory = db.components().insertComponent(newDirectory(subModule, "src2").setEnabled(false));
  591. ComponentDto file = db.components().insertComponent(newFileDto(subModule, directory));
  592. ComponentDto removedFile = db.components().insertComponent(newFileDto(subModule, directory).setEnabled(false));
  593. Map<String, String> uuidsByKey = underTest.selectUuidsByKeyFromProjectKey(dbSession, project.getDbKey())
  594. .stream().collect(Collectors.toMap(KeyWithUuidDto::key, KeyWithUuidDto::uuid));
  595. assertThat(uuidsByKey).containsOnly(
  596. entry(project.getDbKey(), project.uuid()),
  597. entry(module.getDbKey(), module.uuid()),
  598. entry(removedModule.getDbKey(), removedModule.uuid()),
  599. entry(subModule.getDbKey(), subModule.uuid()),
  600. entry(removedSubModule.getDbKey(), removedSubModule.uuid()),
  601. entry(directory.getDbKey(), directory.uuid()),
  602. entry(removedDirectory.getDbKey(), removedDirectory.uuid()),
  603. entry(file.getDbKey(), file.uuid()),
  604. entry(removedFile.getDbKey(), removedFile.uuid()));
  605. }
  606. @Test
  607. public void select_enabled_modules_from_project() {
  608. ComponentDto project = db.components().insertPrivateProject();
  609. ComponentDto removedProject = db.components().insertPrivateProject(p -> p.setEnabled(false));
  610. ComponentDto module = db.components().insertComponent(newModuleDto(project));
  611. ComponentDto removedModule = db.components().insertComponent(newModuleDto(project).setEnabled(false));
  612. ComponentDto subModule = db.components().insertComponent(newModuleDto(module));
  613. ComponentDto removedSubModule = db.components().insertComponent(newModuleDto(module).setEnabled(false));
  614. ComponentDto directory = db.components().insertComponent(newDirectory(subModule, "src"));
  615. ComponentDto removedDirectory = db.components().insertComponent(newDirectory(subModule, "src2").setEnabled(false));
  616. ComponentDto file = db.components().insertComponent(newFileDto(subModule, directory));
  617. ComponentDto removedFile = db.components().insertComponent(newFileDto(subModule, directory).setEnabled(false));
  618. // Removed modules are not included
  619. assertThat(underTest.selectEnabledModulesFromProjectKey(dbSession, project.getDbKey()))
  620. .extracting(ComponentDto::getDbKey)
  621. .containsExactlyInAnyOrder(project.getDbKey(), module.getDbKey(), subModule.getDbKey());
  622. assertThat(underTest.selectEnabledModulesFromProjectKey(dbSession, "UNKNOWN")).isEmpty();
  623. }
  624. @Test
  625. public void select_views_and_sub_views_and_applications() {
  626. OrganizationDto organization = db.organizations().insert();
  627. db.components().insertView(organization, "ABCD");
  628. db.components().insertView(organization, "IJKL");
  629. ComponentDto view = db.components().insertView(organization, "EFGH");
  630. db.components().insertSubView(view, dto -> dto.setUuid("FGHI"));
  631. ComponentDto application = db.components().insertApplication(organization);
  632. assertThat(underTest.selectAllViewsAndSubViews(dbSession)).extracting(UuidWithProjectUuidDto::getUuid)
  633. .containsExactlyInAnyOrder("ABCD", "EFGH", "FGHI", "IJKL", application.uuid());
  634. assertThat(underTest.selectAllViewsAndSubViews(dbSession)).extracting(UuidWithProjectUuidDto::getProjectUuid)
  635. .containsExactlyInAnyOrder("ABCD", "EFGH", "EFGH", "IJKL", application.projectUuid());
  636. }
  637. @Test
  638. public void selectViewKeysWithEnabledCopyOfProject_returns_empty_when_set_is_empty() {
  639. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, emptySet()))
  640. .isEmpty();
  641. }
  642. @Test
  643. @UseDataProvider("oneOrMoreProjects")
  644. public void selectViewKeysWithEnabledCopyOfProject_returns_empty_when_there_is_no_view(int projectCount) {
  645. Set<String> projectUuids = IntStream.range(0, projectCount)
  646. .mapToObj(i -> randomAlphabetic(5))
  647. .collect(toSet());
  648. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, projectUuids)).isEmpty();
  649. }
  650. @DataProvider
  651. public static Object[][] oneOrMoreProjects() {
  652. return new Object[][]{
  653. {1},
  654. {1 + new Random().nextInt(10)}
  655. };
  656. }
  657. @Test
  658. @UseDataProvider("portfolioOrApplicationRootViewQualifier")
  659. public void selectViewKeysWithEnabledCopyOfProject_returns_root_view_with_direct_copy_of_project(String rootViewQualifier) {
  660. OrganizationDto organization = db.organizations().insert();
  661. ComponentDto project = insertProject(organization);
  662. ComponentDto view = insertView(organization, rootViewQualifier);
  663. insertProjectCopy(view, project);
  664. Set<String> keys = underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, singleton(project.uuid()));
  665. assertThat(keys).containsOnly(view.getDbKey());
  666. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, shuffleWithNonExistentUuids(project.uuid())))
  667. .isEqualTo(keys);
  668. }
  669. @Test
  670. @UseDataProvider("portfolioOrApplicationRootViewQualifier")
  671. public void selectViewKeysWithEnabledCopyOfProject_returns_root_views_with_direct_copy_of_projects(String rootViewQualifier) {
  672. OrganizationDto organization = db.organizations().insert();
  673. ComponentDto project1 = insertProject(organization);
  674. ComponentDto project2 = insertProject(organization);
  675. ComponentDto view = insertView(organization, rootViewQualifier);
  676. insertProjectCopy(view, project1);
  677. insertProjectCopy(view, project2);
  678. ComponentDto view2 = insertView(organization, rootViewQualifier);
  679. ComponentDto project3 = insertProject(organization);
  680. insertProjectCopy(view2, project3);
  681. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, singleton(project1.uuid())))
  682. .containsOnly(view.getDbKey());
  683. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, shuffleWithNonExistentUuids(project1.uuid())))
  684. .containsOnly(view.getDbKey());
  685. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, singleton(project2.uuid())))
  686. .containsOnly(view.getDbKey());
  687. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, shuffleWithNonExistentUuids(project2.uuid())))
  688. .containsOnly(view.getDbKey());
  689. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, singleton(project3.uuid())))
  690. .containsOnly(view2.getDbKey());
  691. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, shuffleWithNonExistentUuids(project3.uuid())))
  692. .containsOnly(view2.getDbKey());
  693. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, of(project2.uuid(), project1.uuid())))
  694. .containsOnly(view.getDbKey());
  695. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, shuffleWithNonExistentUuids(project2.uuid(), project1.uuid())))
  696. .containsOnly(view.getDbKey());
  697. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, of(project1.uuid(), project3.uuid())))
  698. .containsOnly(view.getDbKey(), view2.getDbKey());
  699. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, shuffleWithNonExistentUuids(project1.uuid(), project3.uuid())))
  700. .containsOnly(view.getDbKey(), view2.getDbKey());
  701. }
  702. @Test
  703. @UseDataProvider("portfolioOrApplicationRootViewQualifier")
  704. public void selectViewKeysWithEnabledCopyOfProject_does_not_return_root_view_with_direct_copy_of_other_project(String rootViewQualifier) {
  705. OrganizationDto organization = db.organizations().insert();
  706. ComponentDto project1 = insertProject(organization);
  707. ComponentDto project2 = insertProject(organization);
  708. ComponentDto view1 = insertView(organization, rootViewQualifier);
  709. insertProjectCopy(view1, project1);
  710. ComponentDto view2 = insertView(organization, rootViewQualifier);
  711. insertProjectCopy(view2, project2);
  712. Set<String> keys = underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, singleton(project2.uuid()));
  713. assertThat(keys).containsOnly(view2.getDbKey());
  714. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, shuffleWithNonExistentUuids(project2.uuid())))
  715. .isEqualTo(keys);
  716. }
  717. @Test
  718. @UseDataProvider("portfolioOrApplicationRootViewQualifier")
  719. public void selectViewKeysWithEnabledCopyOfProject_does_not_return_root_view_with_disabled_direct_copy_of_project(String rootViewQualifier) {
  720. OrganizationDto organization = db.organizations().insert();
  721. ComponentDto project = insertProject(organization);
  722. ComponentDto view1 = insertView(organization, rootViewQualifier);
  723. insertProjectCopy(view1, project);
  724. ComponentDto view2 = insertView(organization, rootViewQualifier);
  725. insertProjectCopy(view2, project, t -> t.setEnabled(false));
  726. Set<String> keys = underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, singleton(project.uuid()));
  727. assertThat(keys).containsOnly(view1.getDbKey());
  728. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, shuffleWithNonExistentUuids(project.uuid())))
  729. .isEqualTo(keys);
  730. }
  731. @Test
  732. @UseDataProvider("portfolioOrApplicationRootViewQualifier")
  733. public void selectViewKeysWithEnabledCopyOfProject_does_not_return_disabled_root_view_with_direct_copy_of_project(String rootViewQualifier) {
  734. OrganizationDto organization = db.organizations().insert();
  735. ComponentDto project = insertProject(organization);
  736. ComponentDto view1 = insertView(organization, rootViewQualifier, t -> t.setEnabled(false));
  737. insertProjectCopy(view1, project);
  738. ComponentDto view2 = insertView(organization, rootViewQualifier);
  739. insertProjectCopy(view2, project);
  740. Set<String> keys = underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, singleton(project.uuid()));
  741. assertThat(keys).containsOnly(view2.getDbKey());
  742. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, shuffleWithNonExistentUuids(project.uuid())))
  743. .isEqualTo(keys);
  744. }
  745. @Test
  746. @UseDataProvider("portfolioOrApplicationRootViewQualifier")
  747. public void selectViewKeysWithEnabledCopyOfProject_returns_root_view_with_indirect_copy_of_project(String rootViewQualifier) {
  748. OrganizationDto organization = db.organizations().insert();
  749. ComponentDto project = insertProject(organization);
  750. ComponentDto view = insertView(organization, rootViewQualifier);
  751. ComponentDto lowestSubview = insertSubviews(view);
  752. insertProjectCopy(lowestSubview, project);
  753. Set<String> keys = underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, singleton(project.uuid()));
  754. assertThat(keys).containsOnly(view.getDbKey());
  755. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, shuffleWithNonExistentUuids(project.uuid())))
  756. .isEqualTo(keys);
  757. }
  758. @Test
  759. @UseDataProvider("portfolioOrApplicationRootViewQualifier")
  760. public void selectViewKeysWithEnabledCopyOfProject_returns_root_views_with_indirect_copy_of_projects(String rootViewQualifier) {
  761. OrganizationDto organization = db.organizations().insert();
  762. ComponentDto project1 = insertProject(organization);
  763. ComponentDto project2 = insertProject(organization);
  764. ComponentDto view1 = insertView(organization, rootViewQualifier);
  765. ComponentDto lowestSubview1 = insertSubviews(view1);
  766. insertProjectCopy(lowestSubview1, project1);
  767. insertProjectCopy(lowestSubview1, project2);
  768. ComponentDto view2 = insertView(organization, rootViewQualifier);
  769. ComponentDto lowestSubview2 = insertSubviews(view2);
  770. ComponentDto project3 = insertProject(organization);
  771. insertProjectCopy(lowestSubview2, project3);
  772. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, singleton(project1.uuid())))
  773. .containsOnly(view1.getDbKey());
  774. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, shuffleWithNonExistentUuids(project1.uuid())))
  775. .containsOnly(view1.getDbKey());
  776. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, singleton(project2.uuid())))
  777. .containsOnly(view1.getDbKey());
  778. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, shuffleWithNonExistentUuids(project2.uuid())))
  779. .containsOnly(view1.getDbKey());
  780. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, singleton(project3.uuid())))
  781. .containsOnly(view2.getDbKey());
  782. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, shuffleWithNonExistentUuids(project3.uuid())))
  783. .containsOnly(view2.getDbKey());
  784. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, of(project2.uuid(), project1.uuid())))
  785. .containsOnly(view1.getDbKey());
  786. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, shuffleWithNonExistentUuids(project2.uuid(), project1.uuid())))
  787. .containsOnly(view1.getDbKey());
  788. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, of(project1.uuid(), project3.uuid())))
  789. .containsOnly(view1.getDbKey(), view2.getDbKey());
  790. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, shuffleWithNonExistentUuids(project1.uuid(), project3.uuid())))
  791. .containsOnly(view1.getDbKey(), view2.getDbKey());
  792. }
  793. @Test
  794. @UseDataProvider("portfolioOrApplicationRootViewQualifier")
  795. public void selectViewKeysWithEnabledCopyOfProject_does_not_return_root_view_with_indirect_copy_of_other_project(String rootViewQualifier) {
  796. OrganizationDto organization = db.organizations().insert();
  797. ComponentDto project1 = insertProject(organization);
  798. ComponentDto project2 = insertProject(organization);
  799. ComponentDto view1 = insertView(organization, rootViewQualifier);
  800. ComponentDto lowestSubview1 = insertSubviews(view1);
  801. insertProjectCopy(lowestSubview1, project1);
  802. ComponentDto view2 = insertView(organization, rootViewQualifier);
  803. ComponentDto lowestSubview2 = insertSubviews(view2);
  804. insertProjectCopy(lowestSubview2, project2);
  805. Set<String> keys = underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, singleton(project2.uuid()));
  806. assertThat(keys).containsOnly(view2.getDbKey());
  807. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, shuffleWithNonExistentUuids(project2.uuid())))
  808. .isEqualTo(keys);
  809. }
  810. @Test
  811. @UseDataProvider("portfolioOrApplicationRootViewQualifier")
  812. public void selectViewKeysWithEnabledCopyOfProject_does_not_return_root_view_with_disabled_indirect_copy_of_project(String rootViewQualifier) {
  813. OrganizationDto organization = db.organizations().insert();
  814. ComponentDto project = insertProject(organization);
  815. ComponentDto view1 = insertView(organization, rootViewQualifier);
  816. ComponentDto lowestSubview1 = insertSubviews(view1);
  817. insertProjectCopy(lowestSubview1, project);
  818. ComponentDto view2 = insertView(organization, rootViewQualifier);
  819. ComponentDto lowestSubview2 = insertSubviews(view2);
  820. insertProjectCopy(lowestSubview2, project, t -> t.setEnabled(false));
  821. Set<String> keys = underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, singleton(project.uuid()));
  822. assertThat(keys).containsOnly(view1.getDbKey());
  823. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, shuffleWithNonExistentUuids(project.uuid())))
  824. .isEqualTo(keys);
  825. }
  826. @Test
  827. @UseDataProvider("portfolioOrApplicationRootViewQualifier")
  828. public void selectViewKeysWithEnabledCopyOfProject_does_not_return_disabled_root_view_with_indirect_copy_of_project(String rootViewQualifier) {
  829. OrganizationDto organization = db.organizations().insert();
  830. ComponentDto project = insertProject(organization);
  831. ComponentDto view1 = insertView(organization, rootViewQualifier, t -> t.setEnabled(false));
  832. ComponentDto lowestSubview1 = insertSubviews(view1);
  833. insertProjectCopy(lowestSubview1, project);
  834. ComponentDto view2 = insertView(organization, rootViewQualifier);
  835. ComponentDto lowestSubview2 = insertSubviews(view2);
  836. insertProjectCopy(lowestSubview2, project);
  837. Set<String> keys = underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, singleton(project.uuid()));
  838. assertThat(keys).containsOnly(view2.getDbKey());
  839. assertThat(underTest.selectViewKeysWithEnabledCopyOfProject(dbSession, shuffleWithNonExistentUuids(project.uuid())))
  840. .isEqualTo(keys);
  841. }
  842. @DataProvider
  843. public static Object[][] portfolioOrApplicationRootViewQualifier() {
  844. return new Object[][]{
  845. {Qualifiers.VIEW},
  846. {Qualifiers.APP},
  847. };
  848. }
  849. private ComponentDto insertSubviews(ComponentDto view) {
  850. ComponentDto lowestView = view;
  851. int subviewsCount1 = 1 + random.nextInt(5);
  852. for (int i = 0; i < subviewsCount1; i++) {
  853. lowestView = db.components().insertSubView(lowestView);
  854. }
  855. return lowestView;
  856. }
  857. @SafeVarargs
  858. private final ComponentDto insertView(OrganizationDto organization, String rootViewQualifier, Consumer<ComponentDto>... dtoPopulators) {
  859. ComponentDbTester tester = db.components();
  860. if (rootViewQualifier.equals(Qualifiers.VIEW)) {
  861. return random.nextBoolean() ? tester.insertPublicPortfolio(organization, dtoPopulators) : tester.insertPrivatePortfolio(organization, dtoPopulators);
  862. }
  863. return random.nextBoolean() ? tester.insertPublicApplication(organization, dtoPopulators) : tester.insertPrivatePortfolio(organization, dtoPopulators);
  864. }
  865. private ComponentDto insertProject(OrganizationDto organization) {
  866. return random.nextBoolean() ? db.components().insertPrivateProject(organization) : db.components().insertPublicProject(organization);
  867. }
  868. @SafeVarargs
  869. private final ComponentDto insertProjectCopy(ComponentDto view, ComponentDto project, Consumer<ComponentDto>... decorators) {
  870. ComponentDto component = ComponentTesting.newProjectCopy(project, view);
  871. Arrays.stream(decorators).forEach(decorator -> decorator.accept(component));
  872. return db.components().insertComponent(component);
  873. }
  874. @Test
  875. public void select_projects_from_view() {
  876. ComponentDto project1 = db.components().insertPrivateProject();
  877. ComponentDto project2 = db.components().insertPrivateProject();
  878. ComponentDto view = db.components().insertView();
  879. db.components().insertComponent(newProjectCopy(project1, view));
  880. ComponentDto viewWithSubView = db.components().insertView();
  881. db.components().insertComponent(newProjectCopy(project2, viewWithSubView));
  882. ComponentDto subView = db.components().insertSubView(viewWithSubView);
  883. db.components().insertComponent(newProjectCopy(project1, subView));
  884. ComponentDto viewWithoutProject = db.components().insertView();
  885. assertThat(underTest.selectProjectsFromView(dbSession, view.uuid(), view.uuid())).containsExactlyInAnyOrder(project1.uuid());
  886. assertThat(underTest.selectProjectsFromView(dbSession, viewWithSubView.uuid(), viewWithSubView.uuid())).containsExactlyInAnyOrder(project1.uuid(), project2.uuid());
  887. assertThat(underTest.selectProjectsFromView(dbSession, subView.uuid(), viewWithSubView.uuid())).containsExactlyInAnyOrder(project1.uuid());
  888. assertThat(underTest.selectProjectsFromView(dbSession, viewWithoutProject.uuid(), viewWithoutProject.uuid())).isEmpty();
  889. assertThat(underTest.selectProjectsFromView(dbSession, "Unknown", "Unknown")).isEmpty();
  890. }
  891. @Test
  892. public void select_projects() {
  893. OrganizationDto organization = db.organizations().insert();
  894. ComponentDto provisionedProject = db.components().insertPrivateProject();
  895. ComponentDto provisionedView = db.components().insertView(organization);
  896. String projectUuid = db.components().insertProjectAndSnapshot(newPrivateProjectDto(organization)).getComponentUuid();
  897. String disabledProjectUuid = db.components().insertProjectAndSnapshot(newPrivateProjectDto(organization).setEnabled(false)).getComponentUuid();
  898. String viewUuid = db.components().insertProjectAndSnapshot(ComponentTesting.newView(organization)).getComponentUuid();
  899. assertThat(underTest.selectProjects(dbSession))
  900. .extracting(ComponentDto::uuid)
  901. .containsOnly(provisionedProject.uuid(), projectUuid);
  902. }
  903. @Test
  904. public void select_projects_does_not_return_branches() {
  905. OrganizationDto organization = db.organizations().insert();
  906. ComponentDto project = db.components().insertMainBranch();
  907. ComponentDto branch = db.components().insertProjectBranch(project);
  908. assertThat(underTest.selectProjects(dbSession))
  909. .extracting(ComponentDto::uuid)
  910. .containsOnly(project.uuid());
  911. }
  912. @Test
  913. public void select_projects_by_organization() {
  914. OrganizationDto organization = db.organizations().insert();
  915. ComponentDto project1 = db.components().insertPrivateProject(organization);
  916. ComponentDto module = db.components().insertComponent(newModuleDto(project1));
  917. ComponentDto directory = db.components().insertComponent(newDirectory(module, "dir"));
  918. ComponentDto file = db.components().insertComponent(newFileDto(module, directory));
  919. ComponentDto project2 = db.components().insertPrivateProject(organization);
  920. ComponentDto view = db.components().insertView(organization);
  921. ComponentDto application = db.components().insertApplication(organization);
  922. OrganizationDto otherOrganization = db.organizations().insert();
  923. ComponentDto projectOnOtherOrganization = db.components().insertPrivateProject(otherOrganization);
  924. assertThat(underTest.selectProjectsByOrganization(dbSession, organization.getUuid()))
  925. .extracting(ComponentDto::uuid)
  926. .containsExactlyInAnyOrder(project1.uuid(), project2.uuid())
  927. .doesNotContain(view.uuid(), application.uuid());
  928. }
  929. @Test
  930. public void select_projects_by_organization_does_not_return_branches() {
  931. OrganizationDto organization = db.organizations().insert();
  932. ComponentDto project = db.components().insertMainBranch(organization);
  933. ComponentDto branch = db.components().insertProjectBranch(project);
  934. assertThat(underTest.selectProjectsByOrganization(dbSession, organization.getUuid()))
  935. .extracting(ComponentDto::uuid)
  936. .containsExactlyInAnyOrder(project.uuid())
  937. .doesNotContain(branch.uuid());
  938. }
  939. @Test
  940. public void selectByQuery_provisioned() {
  941. OrganizationDto organization = db.organizations().insert();
  942. ComponentDto provisionedProject = db.components()
  943. .insertPrivateProject(organization, p -> p.setDbKey("provisioned.project").setName("Provisioned Project"));
  944. ComponentDto provisionedPortfolio = db.components().insertPrivatePortfolio(organization);
  945. SnapshotDto analyzedProject = db.components().insertProjectAndSnapshot(newPrivateProjectDto(organization));
  946. SnapshotDto analyzedDisabledProject = db.components().insertProjectAndSnapshot(newPrivateProjectDto(organization)
  947. .setEnabled(false));
  948. SnapshotDto analyzedPortfolio = db.components().insertProjectAndSnapshot(ComponentTesting.newView(organization));
  949. Supplier<ComponentQuery.Builder> query = () -> ComponentQuery.builder().setQualifiers(PROJECT).setOnProvisionedOnly(true);
  950. assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().build(), 0, 10))
  951. .extracting(ComponentDto::uuid)
  952. .containsOnly(provisionedProject.uuid());
  953. // pagination
  954. assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().build(), 2, 10)).isEmpty();
  955. // filter on qualifiers
  956. assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setQualifiers("XXX").build(), 0, 10)).isEmpty();
  957. assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(PROJECT, "XXX").build(), 0, 10))
  958. .extracting(ComponentDto::uuid)
  959. .containsOnly(provisionedProject.uuid());
  960. assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(PROJECT, Qualifiers.VIEW).build(), 0, 10))
  961. .extracting(ComponentDto::uuid)
  962. .containsOnly(provisionedProject.uuid(), provisionedPortfolio.uuid());
  963. // match key
  964. assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setNameOrKeyQuery(provisionedProject.getDbKey()).build(), 0, 10))
  965. .extracting(ComponentDto::uuid)
  966. .containsExactly(provisionedProject.uuid());
  967. assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setNameOrKeyQuery("pROvisiONed.proJEcT").setPartialMatchOnKey(true).build(), 0, 10))
  968. .extracting(ComponentDto::uuid)
  969. .containsExactly(provisionedProject.uuid());
  970. assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setNameOrKeyQuery("missing").setPartialMatchOnKey(true).build(), 0, 10)).isEmpty();
  971. assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setNameOrKeyQuery("to be escaped '\"\\%").setPartialMatchOnKey(true).build(), 0, 10))
  972. .isEmpty();
  973. // match name
  974. assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setNameOrKeyQuery("ned proj").setPartialMatchOnKey(true).build(), 0, 10))
  975. .extracting(ComponentDto::uuid)
  976. .containsExactly(provisionedProject.uuid());
  977. }
  978. @Test
  979. public void selectByQuery_onProvisionedOnly_filters_projects_with_analysis_on_branch() {
  980. Supplier<ComponentQuery.Builder> query = () -> ComponentQuery.builder()
  981. .setQualifiers(PROJECT)
  982. .setOnProvisionedOnly(true);
  983. // the project does not have any analysis
  984. OrganizationDto organization = db.organizations().insert();
  985. ComponentDto project = db.components().insertMainBranch(organization);
  986. assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().build(), 0, 10))
  987. .extracting(ComponentDto::uuid)
  988. .containsOnly(project.uuid());
  989. // the project does not have analysis of main branch but only
  990. // analysis of non-main branches
  991. ComponentDto branchWithoutAnalysis = db.components().insertProjectBranch(project);
  992. ComponentDto branchWithAnalysis = db.components().insertProjectBranch(project);
  993. db.components().insertSnapshot(branchWithAnalysis);
  994. assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().build(), 0, 10))
  995. .isEmpty();
  996. }
  997. @Test
  998. public void selectByQuery_verify_order() {
  999. OrganizationDto organization = db.organizations().insert();
  1000. Date firstDate = new Date(system2.now());
  1001. Date secondDate = new Date(system2.now());
  1002. Date thirdDate = new Date(system2.now());
  1003. ComponentDto project3 = db.components().insertPrivateProject(organization, "project3", componentDto -> componentDto.setCreatedAt(thirdDate));
  1004. ComponentDto project1 = db.components().insertPrivateProject(organization, "project1", componentDto -> componentDto.setCreatedAt(firstDate));
  1005. ComponentDto project2 = db.components().insertPrivateProject(organization, "project2", componentDto -> componentDto.setCreatedAt(secondDate));
  1006. Supplier<ComponentQuery.Builder> query = () -> ComponentQuery.builder()
  1007. .setQualifiers(PROJECT)
  1008. .setOnProvisionedOnly(true);
  1009. List<ComponentDto> results = underTest.selectByQuery(dbSession, organization.getUuid(), query.get().build(), 0, 10);
  1010. assertThat(results)
  1011. .extracting(ComponentDto::uuid)
  1012. .containsExactly(
  1013. project1.uuid(),
  1014. project2.uuid(),
  1015. project3.uuid()
  1016. );
  1017. assertThat(results)
  1018. .extracting(ComponentDto::getCreatedAt)
  1019. .containsExactly(
  1020. firstDate,
  1021. secondDate,
  1022. thirdDate
  1023. );
  1024. }
  1025. @Test
  1026. public void count_provisioned() {
  1027. OrganizationDto organization = db.organizations().insert();
  1028. db.components().insertPrivateProject(organization);
  1029. db.components().insertProjectAndSnapshot(newPrivateProjectDto(organization));
  1030. db.components().insertProjectAndSnapshot(ComponentTesting.newView(organization));
  1031. Supplier<ComponentQuery.Builder> query = () -> ComponentQuery.builder().setOnProvisionedOnly(true);
  1032. assertThat(underTest.countByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(PROJECT).build())).isEqualTo(1);
  1033. assertThat(underTest.countByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(Qualifiers.VIEW).build())).isEqualTo(0);
  1034. assertThat(underTest.countByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(PROJECT, Qualifiers.VIEW).build())).isEqualTo(1);
  1035. }
  1036. @Test
  1037. public void countByQuery_with_organization_throws_NPE_of_organizationUuid_is_null() {
  1038. expectedException.expect(NullPointerException.class);
  1039. expectedException.expectMessage("organizationUuid can't be null");
  1040. underTest.countByQuery(dbSession, null, ALL_PROJECTS_COMPONENT_QUERY);
  1041. }
  1042. @Test
  1043. public void countByQuery_throws_IAE_if_too_many_component_ids() {
  1044. Set<Long> ids = LongStream.range(0L, 1_010L).boxed().collect(toSet());
  1045. ComponentQuery.Builder query = ComponentQuery.builder()
  1046. .setQualifiers(PROJECT)
  1047. .setComponentIds(ids);
  1048. assertThatCountByQueryThrowsIAE(query, "Too many component ids in query");
  1049. }
  1050. @Test
  1051. public void countByQuery_throws_IAE_if_too_many_component_keys() {
  1052. Set<String> keys = IntStream.range(0, 1_010).mapToObj(String::valueOf).collect(toSet());
  1053. ComponentQuery.Builder query = ComponentQuery.builder()
  1054. .setQualifiers(PROJECT)
  1055. .setComponentKeys(keys);
  1056. assertThatCountByQueryThrowsIAE(query, "Too many component keys in query");
  1057. }
  1058. @Test
  1059. public void countByQuery_throws_IAE_if_too_many_component_uuids() {
  1060. Set<String> uuids = IntStream.range(0, 1_010).mapToObj(String::valueOf).collect(toSet());
  1061. ComponentQuery.Builder query = ComponentQuery.builder()
  1062. .setQualifiers(PROJECT)
  1063. .setComponentUuids(uuids);
  1064. assertThatCountByQueryThrowsIAE(query, "Too many component UUIDs in query");
  1065. }
  1066. private void assertThatCountByQueryThrowsIAE(ComponentQuery.Builder query, String expectedMessage) {
  1067. expectedException.expect(IllegalArgumentException.class);
  1068. expectedException.expectMessage(expectedMessage);
  1069. underTest.countByQuery(dbSession, query.build());
  1070. }
  1071. @Test
  1072. public void selectByProjectUuid() {
  1073. ComponentDto project = db.components().insertPrivateProject();
  1074. ComponentDto removedProject = db.components().insertPrivateProject(p -> p.setEnabled(false));
  1075. ComponentDto module = db.components().insertComponent(newModuleDto(project));
  1076. ComponentDto removedModule = db.components().insertComponent(newModuleDto(project).setEnabled(false));
  1077. ComponentDto subModule = db.components().insertComponent(newModuleDto(module));
  1078. ComponentDto removedSubModule = db.components().insertComponent(newModuleDto(module).setEnabled(false));
  1079. ComponentDto directory = db.components().insertComponent(newDirectory(subModule, "src"));
  1080. ComponentDto removedDirectory = db.components().insertComponent(newDirectory(subModule, "src2").setEnabled(false));
  1081. ComponentDto file = db.components().insertComponent(newFileDto(subModule, directory));
  1082. ComponentDto removedFile = db.components().insertComponent(newFileDto(subModule, directory).setEnabled(false));
  1083. assertThat(underTest.selectByProjectUuid(project.uuid(), dbSession))
  1084. .extracting(ComponentDto::uuid)
  1085. .containsExactlyInAnyOrder(project.uuid(), module.uuid(), removedModule.uuid(), subModule.uuid(), removedSubModule.uuid(), directory.uuid(), removedDirectory.uuid(),
  1086. file.uuid(),
  1087. removedFile.uuid());
  1088. }
  1089. @Test
  1090. public void selectForIndexing_all() {
  1091. assertSelectForIndexing(null)
  1092. .doesNotContain("DIS7")
  1093. .doesNotContain("COPY8")
  1094. .containsExactlyInAnyOrder("U1", "U2", "U3", "U4", "U5", "U6", "VW1");
  1095. }
  1096. @Test
  1097. public void selectForIndexing_project() {
  1098. assertSelectForIndexing("U1")
  1099. .doesNotContain("DIS7")
  1100. .doesNotContain("COPY8")
  1101. .containsExactlyInAnyOrder("U1", "U2", "U3", "U4");
  1102. }
  1103. private ListAssert<String> assertSelectForIndexing(@Nullable String projectUuid) {
  1104. OrganizationDto organization = db.organizations().insert();
  1105. ComponentDto project = db.components().insertPrivateProject(organization, "U1");
  1106. ComponentDto removedProject = db.components().insertPrivateProject(organization, p -> p.setEnabled(false));
  1107. ComponentDto module = db.components().insertComponent(newModuleDto("U2", project));
  1108. ComponentDto removedModule = db.components().insertComponent(newModuleDto(project).setEnabled(false));
  1109. ComponentDto directory = db.components().insertComponent(newDirectory(module, "U3", "src"));
  1110. ComponentDto removedDirectory = db.components().insertComponent(newDirectory(module, "src2").setEnabled(false));
  1111. ComponentDto file = db.components().insertComponent(newFileDto(module, directory, "U4"));
  1112. ComponentDto removedFile = db.components().insertComponent(newFileDto(module, directory).setEnabled(false));
  1113. ComponentDto view = db.components().insertView(organization, "VW1");
  1114. db.components().insertComponent(newProjectCopy("COPY8", project, view));
  1115. ComponentDto project2 = db.components().insertPrivateProject(organization, "U5");
  1116. ComponentDto moduleOnProject2 = db.components().insertComponent(newModuleDto("U6", project2));
  1117. List<ComponentDto> components = new ArrayList<>();
  1118. underTest.scrollForIndexing(dbSession, projectUuid, context -> components.add(context.getResultObject()));
  1119. return (ListAssert<String>) assertThat(components).extracting(ComponentDto::uuid);
  1120. }
  1121. @Test
  1122. public void update() {
  1123. db.components().insertPrivateProject(db.getDefaultOrganization(), "U1");
  1124. underTest.update(dbSession, new ComponentUpdateDto()
  1125. .setUuid("U1")
  1126. .setBKey("key")
  1127. .setBCopyComponentUuid("copy")
  1128. .setBChanged(true)
  1129. .setBDescription("desc")
  1130. .setBEnabled(true)
  1131. .setBUuidPath("uuid_path")
  1132. .setBLanguage("lang")
  1133. .setBLongName("longName")
  1134. .setBModuleUuid("moduleUuid")
  1135. .setBModuleUuidPath("moduleUuidPath")
  1136. .setBName("name")
  1137. .setBPath("path")
  1138. .setBQualifier("qualifier"));
  1139. dbSession.commit();
  1140. Map<String, Object> row = selectBColumnsForUuid("U1");
  1141. assertThat(row.get("bChanged")).isIn(true, /* for Oracle */1L, 1);
  1142. assertThat(row.get("bKey")).isEqualTo("key");
  1143. assertThat(row.get("bCopyComponentUuid")).isEqualTo("copy");
  1144. assertThat(row.get("bDescription")).isEqualTo("desc");
  1145. assertThat(row.get("bEnabled")).isIn(true, /* for Oracle */1L, 1);
  1146. assertThat(row.get("bUuidPath")).isEqualTo("uuid_path");
  1147. assertThat(row.get("bLanguage")).isEqualTo("lang");
  1148. assertThat(row.get("bLongName")).isEqualTo("longName");
  1149. assertThat(row.get("bModuleUuid")).isEqualTo("moduleUuid");
  1150. assertThat(row.get("bModuleUuidPath")).isEqualTo("moduleUuidPath");
  1151. assertThat(row.get("bName")).isEqualTo("name");
  1152. assertThat(row.get("bPath")).isEqualTo("path");
  1153. assertThat(row.get("bQualifier")).isEqualTo("qualifier");
  1154. }
  1155. @Test
  1156. public void updateBEnabledToFalse() {
  1157. ComponentDto dto1 = newPrivateProjectDto(db.getDefaultOrganization(), "U1");
  1158. ComponentDto dto2 = newPrivateProjectDto(db.getDefaultOrganization(), "U2");
  1159. ComponentDto dto3 = newPrivateProjectDto(db.getDefaultOrganization(), "U3");
  1160. underTest.insert(dbSession, dto1, dto2, dto3);
  1161. underTest.updateBEnabledToFalse(dbSession, asList("U1", "U2"));
  1162. dbSession.commit();
  1163. Map<String, Object> row1 = selectBColumnsForUuid("U1");
  1164. assertThat(row1.get("bChanged")).isIn(true, /* for Oracle */1L, 1);
  1165. assertThat(row1.get("bKey")).isEqualTo(dto1.getDbKey());
  1166. assertThat(row1.get("bCopyComponentUuid")).isEqualTo(dto1.getCopyResourceUuid());
  1167. assertThat(row1.get("bDescription")).isEqualTo(dto1.description());
  1168. assertThat(row1.get("bEnabled")).isIn(false, /* for Oracle */0L, 0);
  1169. assertThat(row1.get("bUuidPath")).isEqualTo(dto1.getUuidPath());
  1170. assertThat(row1.get("bLanguage")).isEqualTo(dto1.language());
  1171. assertThat(row1.get("bLongName")).isEqualTo(dto1.longName());
  1172. assertThat(row1.get("bModuleUuid")).isEqualTo(dto1.moduleUuid());
  1173. assertThat(row1.get("bModuleUuidPath")).isEqualTo(dto1.moduleUuidPath());
  1174. assertThat(row1.get("bName")).isEqualTo(dto1.name());
  1175. assertThat(row1.get("bPath")).isEqualTo(dto1.path());
  1176. assertThat(row1.get("bQualifier")).isEqualTo(dto1.qualifier());
  1177. Map<String, Object> row2 = selectBColumnsForUuid("U2");
  1178. assertThat(row2.get("bChanged")).isIn(true, /* for Oracle */1L, 1);
  1179. assertThat(row2.get("bKey")).isEqualTo(dto2.getDbKey());
  1180. assertThat(row2.get("bCopyComponentUuid")).isEqualTo(dto2.getCopyResourceUuid());
  1181. assertThat(row2.get("bDescription")).isEqualTo(dto2.description());
  1182. assertThat(row2.get("bEnabled")).isIn(false, /* for Oracle */0L, 0);
  1183. assertThat(row2.get("bUuidPath")).isEqualTo(dto2.getUuidPath());
  1184. assertThat(row2.get("bLanguage")).isEqualTo(dto2.language());
  1185. assertThat(row2.get("bLongName")).isEqualTo(dto2.longName());
  1186. assertThat(row2.get("bModuleUuid")).isEqualTo(dto2.moduleUuid());
  1187. assertThat(row2.get("bModuleUuidPath")).isEqualTo(dto2.moduleUuidPath());
  1188. assertThat(row2.get("bName")).isEqualTo(dto2.name());
  1189. assertThat(row2.get("bPath")).isEqualTo(dto2.path());
  1190. assertThat(row2.get("bQualifier")).isEqualTo(dto2.qualifier());
  1191. Map<String, Object> row3 = selectBColumnsForUuid("U3");
  1192. assertThat(row3.get("bChanged")).isIn(false, /* for Oracle */0L, 0);
  1193. }
  1194. private Map<String, Object> selectBColumnsForUuid(String uuid) {
  1195. return db.selectFirst(
  1196. "select b_changed as \"bChanged\", deprecated_kee as \"bKey\", b_copy_component_uuid as \"bCopyComponentUuid\", b_description as \"bDescription\", " +
  1197. "b_enabled as \"bEnabled\", b_uuid_path as \"bUuidPath\", b_language as \"bLanguage\", b_long_name as \"bLongName\"," +
  1198. "b_module_uuid as \"bModuleUuid\", b_module_uuid_path as \"bModuleUuidPath\", b_name as \"bName\", " +
  1199. "b_path as \"bPath\", b_qualifier as \"bQualifier\" " +
  1200. "from projects where uuid='" + uuid + "'");
  1201. }
  1202. @Test
  1203. public void update_tags() {
  1204. ComponentDto project = db.components().insertPrivateProject(p -> p.setTags(emptyList()));
  1205. underTest.updateTags(dbSession, project.setTags(newArrayList("finance", "toto", "tutu")));
  1206. dbSession.commit();
  1207. assertThat(underTest.selectOrFailByKey(dbSession, project.getDbKey()).getTags()).containsOnly("finance", "toto", "tutu");
  1208. }
  1209. @Test
  1210. public void delete() {
  1211. ComponentDto project1 = db.components().insertPrivateProject(db.getDefaultOrganization(), (t) -> t.setDbKey("PROJECT_1"));
  1212. db.components().insertPrivateProject(db.getDefaultOrganization(), (t) -> t.setDbKey("PROJECT_2"));
  1213. underTest.delete(dbSession, project1.getId());
  1214. dbSession.commit();
  1215. assertThat(underTest.selectByKey(dbSession, "PROJECT_1")).isEmpty();
  1216. assertThat(underTest.selectByKey(dbSession, "PROJECT_2")).isPresent();
  1217. }
  1218. @Test
  1219. public void selectByQuery_with_organization_throws_NPE_of_organizationUuid_is_null() {
  1220. expectedException.expect(NullPointerException.class);
  1221. expectedException.expectMessage("organizationUuid can't be null");
  1222. underTest.selectByQuery(dbSession, null, ALL_PROJECTS_COMPONENT_QUERY, 1, 1);
  1223. }
  1224. @Test
  1225. public void selectByQuery_throws_IAE_if_too_many_component_ids() {
  1226. Set<Long> ids = LongStream.range(0L, 1_010L).boxed().collect(toSet());
  1227. ComponentQuery.Builder query = ComponentQuery.builder()
  1228. .setQualifiers(PROJECT)
  1229. .setComponentIds(ids);
  1230. assertThatSelectByQueryThrowsIAE(query, "Too many component ids in query");
  1231. }
  1232. @Test
  1233. public void selectByQuery_throws_IAE_if_too_many_component_keys() {
  1234. Set<String> keys = IntStream.range(0, 1_010).mapToObj(String::valueOf).collect(toSet());
  1235. ComponentQuery.Builder query = ComponentQuery.builder()
  1236. .setQualifiers(PROJECT)
  1237. .setComponentKeys(keys);
  1238. assertThatSelectByQueryThrowsIAE(query, "Too many component keys in query");
  1239. }
  1240. @Test
  1241. public void selectByQuery_throws_IAE_if_too_many_component_uuids() {
  1242. Set<String> uuids = IntStream.range(0, 1_010).mapToObj(String::valueOf).collect(toSet());
  1243. ComponentQuery.Builder query = ComponentQuery.builder()
  1244. .setQualifiers(PROJECT)
  1245. .setComponentUuids(uuids);
  1246. assertThatSelectByQueryThrowsIAE(query, "Too many component UUIDs in query");
  1247. }
  1248. private void assertThatSelectByQueryThrowsIAE(ComponentQuery.Builder query, String expectedMessage) {
  1249. expectedException.expect(IllegalArgumentException.class);
  1250. expectedException.expectMessage(expectedMessage);
  1251. underTest.selectByQuery(dbSession, query.build(), 0, Integer.MAX_VALUE);
  1252. }
  1253. @Test
  1254. public void selectByQuery_with_paging_query_and_qualifiers() {
  1255. OrganizationDto organizationDto = db.organizations().insert();
  1256. db.components().insertProjectAndSnapshot(newPrivateProjectDto(organizationDto).setName("aaaa-name"));
  1257. db.components().insertProjectAndSnapshot(newView(organizationDto));
  1258. for (int i = 9; i >= 1; i--) {
  1259. db.components().insertProjectAndSnapshot(newPrivateProjectDto(organizationDto).setName("project-" + i));
  1260. }
  1261. ComponentQuery query = ComponentQuery.builder().setNameOrKeyQuery("oJect").setQualifiers(PROJECT).build();
  1262. List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 1, 3);
  1263. int count = underTest.countByQuery(dbSession, query);
  1264. assertThat(result).hasSize(3);
  1265. assertThat(count).isEqualTo(9);
  1266. assertThat(result).extracting(ComponentDto::name).containsExactly("project-2", "project-3", "project-4");
  1267. assertThat(result).extracting(ComponentDto::getOrganizationUuid).containsOnly(organizationDto.getUuid());
  1268. }
  1269. @Test
  1270. public void selectByQuery_with_organization_filters_on_specified_organization() {
  1271. OrganizationDto organization1 = db.organizations().insert();
  1272. OrganizationDto organization2 = db.organizations().insert();
  1273. ComponentDto project1 = db.components().insertPrivateProject(organization1);
  1274. ComponentDto project2 = db.components().insertPrivateProject(organization2);
  1275. assertThat(underTest.selectByQuery(dbSession, ALL_PROJECTS_COMPONENT_QUERY, 0, 2))
  1276. .extracting(ComponentDto::uuid)
  1277. .containsOnly(project1.uuid(), project2.uuid());
  1278. assertThat(underTest.selectByQuery(dbSession, organization1.getUuid(), ALL_PROJECTS_COMPONENT_QUERY, 0, 2))
  1279. .extracting(ComponentDto::uuid)
  1280. .containsOnly(project1.uuid());
  1281. assertThat(underTest.selectByQuery(dbSession, organization2.getUuid(), ALL_PROJECTS_COMPONENT_QUERY, 0, 2))
  1282. .extracting(ComponentDto::uuid)
  1283. .containsOnly(project2.uuid());
  1284. assertThat(underTest.selectByQuery(dbSession, "non existent organization uuid", ALL_PROJECTS_COMPONENT_QUERY, 0, 2))
  1285. .isEmpty();
  1286. }
  1287. @Test
  1288. public void selectByQuery_should_not_return_branches() {
  1289. ComponentDto main = db.components().insertMainBranch();
  1290. ComponentDto branch = db.components().insertProjectBranch(main);
  1291. assertThat(underTest.selectByQuery(dbSession, ALL_PROJECTS_COMPONENT_QUERY, 0, 2)).hasSize(1);
  1292. assertThat(underTest.selectByQuery(dbSession, ALL_PROJECTS_COMPONENT_QUERY, 0, 2).get(0).uuid()).isEqualTo(main.uuid());
  1293. }
  1294. @Test
  1295. public void countByQuery_should_not_include_branches() {
  1296. ComponentDto main = db.components().insertMainBranch();
  1297. ComponentDto branch = db.components().insertProjectBranch(main);
  1298. assertThat(underTest.countByQuery(dbSession, ALL_PROJECTS_COMPONENT_QUERY)).isEqualTo(1);
  1299. }
  1300. @Test
  1301. public void countByQuery_with_organization_filters_on_specified_organization() {
  1302. OrganizationDto organization1 = db.organizations().insert();
  1303. OrganizationDto organization2 = db.organizations().insert();
  1304. ComponentDto project1 = db.components().insertPrivateProject(organization1);
  1305. ComponentDto project2 = db.components().insertPrivateProject(organization2);
  1306. assertThat(underTest.countByQuery(dbSession, ALL_PROJECTS_COMPONENT_QUERY))
  1307. .isEqualTo(2);
  1308. assertThat(underTest.countByQuery(dbSession, organization1.getUuid(), ALL_PROJECTS_COMPONENT_QUERY))
  1309. .isEqualTo(1);
  1310. assertThat(underTest.countByQuery(dbSession, organization2.getUuid(), ALL_PROJECTS_COMPONENT_QUERY))
  1311. .isEqualTo(1);
  1312. assertThat(underTest.countByQuery(dbSession, "non existent organization uuid", ALL_PROJECTS_COMPONENT_QUERY))
  1313. .isEqualTo(0);
  1314. }
  1315. @Test
  1316. public void selectByQuery_name_with_special_characters() {
  1317. db.components().insertProjectAndSnapshot(newPrivateProjectDto(db.getDefaultOrganization()).setName("project-\\_%/-name"));
  1318. ComponentQuery query = ComponentQuery.builder().setNameOrKeyQuery("-\\_%/-").setQualifiers(PROJECT).build();
  1319. List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 0, 10);
  1320. assertThat(result).hasSize(1);
  1321. assertThat(result.get(0).name()).isEqualTo("project-\\_%/-name");
  1322. }
  1323. @Test
  1324. public void selectByQuery_key_with_special_characters() {
  1325. db.components().insertProjectAndSnapshot(newPrivateProjectDto(db.organizations().insert()).setDbKey("project-_%-key"));
  1326. db.components().insertProjectAndSnapshot(newPrivateProjectDto(db.organizations().insert()).setDbKey("project-key-that-does-not-match"));
  1327. ComponentQuery query = ComponentQuery.builder().setNameOrKeyQuery("project-_%-key").setQualifiers(PROJECT).build();
  1328. List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 0, 10);
  1329. assertThat(result).hasSize(1);
  1330. assertThat(result.get(0).getDbKey()).isEqualTo("project-_%-key");
  1331. }
  1332. @Test
  1333. public void selectByQuery_on_key_partial_match_case_insensitive() {
  1334. db.components().insertProjectAndSnapshot(newPrivateProjectDto(db.organizations().insert()).setDbKey("project-key"));
  1335. ComponentQuery query = ComponentQuery.builder()
  1336. .setNameOrKeyQuery("JECT-K")
  1337. .setPartialMatchOnKey(true)
  1338. .setQualifiers(PROJECT).build();
  1339. List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 0, 10);
  1340. assertThat(result).hasSize(1);
  1341. assertThat(result.get(0).getDbKey()).isEqualTo("project-key");
  1342. }
  1343. @Test
  1344. public void selectByQuery_filter_last_analysis_date() {
  1345. long aLongTimeAgo = 1_000_000_000L;
  1346. long recentTime = 3_000_000_000L;
  1347. ComponentDto oldProject = db.components().insertPrivateProject();
  1348. db.components().insertSnapshot(oldProject, s -> s.setCreatedAt(aLongTimeAgo));
  1349. ComponentDto recentProject = db.components().insertPrivateProject();
  1350. db.components().insertSnapshot(recentProject, s -> s.setCreatedAt(recentTime).setLast(true));
  1351. db.components().insertSnapshot(recentProject, s -> s.setCreatedAt(aLongTimeAgo).setLast(false));
  1352. // before date on main branch
  1353. assertThat(selectProjectUuidsByQuery(q -> q.setAnalyzedBefore(recentTime)))
  1354. .containsExactlyInAnyOrder(oldProject.uuid());
  1355. assertThat(selectProjectUuidsByQuery(q -> q.setAnalyzedBefore(aLongTimeAgo)))
  1356. .isEmpty();
  1357. assertThat(selectProjectUuidsByQuery(q -> q.setAnalyzedBefore(recentTime + 1_000L)))
  1358. .containsExactlyInAnyOrder(oldProject.uuid(), recentProject.uuid());
  1359. // before date on any branch
  1360. assertThat(selectProjectUuidsByQuery(q -> q.setAnyBranchAnalyzedBefore(recentTime)))
  1361. .containsExactlyInAnyOrder(oldProject.uuid());
  1362. assertThat(selectProjectUuidsByQuery(q -> q.setAnyBranchAnalyzedBefore(aLongTimeAgo)))
  1363. .isEmpty();
  1364. assertThat(selectProjectUuidsByQuery(q -> q.setAnyBranchAnalyzedBefore(recentTime + 1_000L)))
  1365. .containsExactlyInAnyOrder(oldProject.uuid(), recentProject.uuid());
  1366. // after date
  1367. assertThat(selectProjectUuidsByQuery(q -> q.setAnyBranchAnalyzedAfter(recentTime - 1_000L)))
  1368. .containsExactlyInAnyOrder(recentProject.uuid());
  1369. assertThat(selectProjectUuidsByQuery(q -> q.setAnyBranchAnalyzedAfter(recentTime + 1_000L)))
  1370. .isEmpty();
  1371. assertThat(selectProjectUuidsByQuery(q -> q.setAnyBranchAnalyzedAfter(aLongTimeAgo)))
  1372. .containsExactlyInAnyOrder(oldProject.uuid(), recentProject.uuid());
  1373. }
  1374. @Test
  1375. public void selectByQuery_filter_last_analysis_date_on_non_main_branches() {
  1376. long aLongTimeAgo = 1_000_000_000L;
  1377. long recentTime = 3_000_000_000L;
  1378. // project with only a non-main and old analyzed branch
  1379. ComponentDto oldProject = db.components().insertMainBranch();
  1380. ComponentDto oldProjectBranch = db.components().insertProjectBranch(oldProject, newBranchDto(oldProject).setBranchType(BranchType.SHORT));
  1381. db.components().insertSnapshot(oldProjectBranch, s -> s.setLast(true).setCreatedAt(aLongTimeAgo));
  1382. // project with only a old main branch and a recent non-main branch
  1383. ComponentDto recentProject = db.components().insertMainBranch();
  1384. ComponentDto recentProjectBranch = db.components().insertProjectBranch(recentProject, newBranchDto(recentProject).setBranchType(BranchType.SHORT));
  1385. db.components().insertSnapshot(recentProjectBranch, s -> s.setCreatedAt(recentTime).setLast(true));
  1386. db.components().insertSnapshot(recentProjectBranch, s -> s.setCreatedAt(aLongTimeAgo).setLast(false));
  1387. // before date on main branch only
  1388. assertThat(selectProjectUuidsByQuery(q -> q.setAnalyzedBefore(recentTime))).isEmpty();
  1389. assertThat(selectProjectUuidsByQuery(q -> q.setAnalyzedBefore(aLongTimeAgo))).isEmpty();
  1390. assertThat(selectProjectUuidsByQuery(q -> q.setAnalyzedBefore(recentTime + 1_000L))).isEmpty();
  1391. // before date on any branch
  1392. assertThat(selectProjectUuidsByQuery(q -> q.setAnyBranchAnalyzedBefore(recentTime)))
  1393. .containsExactlyInAnyOrder(oldProject.uuid());
  1394. assertThat(selectProjectUuidsByQuery(q -> q.setAnyBranchAnalyzedBefore(aLongTimeAgo)))
  1395. .isEmpty();
  1396. assertThat(selectProjectUuidsByQuery(q -> q.setAnyBranchAnalyzedBefore(recentTime + 1_000L)))
  1397. .containsExactlyInAnyOrder(oldProject.uuid(), recentProject.uuid());
  1398. // after date
  1399. assertThat(selectProjectUuidsByQuery(q -> q.setAnyBranchAnalyzedAfter(recentTime - 1_000L)))
  1400. .containsExactlyInAnyOrder(recentProject.uuid());
  1401. assertThat(selectProjectUuidsByQuery(q -> q.setAnyBranchAnalyzedAfter(recentTime + 1_000L)))
  1402. .isEmpty();
  1403. assertThat(selectProjectUuidsByQuery(q -> q.setAnyBranchAnalyzedAfter(aLongTimeAgo)))
  1404. .containsExactlyInAnyOrder(oldProject.uuid(), recentProject.uuid());
  1405. }
  1406. @Test
  1407. public void selectByQuery_filter_created_at() {
  1408. ComponentDto project1 = db.components().insertPrivateProject(p -> p.setCreatedAt(parseDate("2018-02-01")));
  1409. ComponentDto project2 = db.components().insertPrivateProject(p -> p.setCreatedAt(parseDate("2018-06-01")));
  1410. assertThat(selectProjectUuidsByQuery(q -> q.setCreatedAfter(parseDate("2017-12-01"))))
  1411. .containsExactlyInAnyOrder(project1.uuid(), project2.uuid());
  1412. assertThat(selectProjectUuidsByQuery(q -> q.setCreatedAfter(parseDate("2018-02-20"))))
  1413. .containsExactlyInAnyOrder(project2.uuid());
  1414. assertThat(selectProjectUuidsByQuery(q -> q.setCreatedAfter(parseDate("2019-01-01"))))
  1415. .isEmpty();
  1416. }
  1417. private List<String> selectProjectUuidsByQuery(Consumer<ComponentQuery.Builder> query) {
  1418. ComponentQuery.Builder builder = ComponentQuery.builder().setQualifiers(PROJECT);
  1419. query.accept(builder);
  1420. return underTest.selectByQuery(dbSession, builder.build(), 0, 5)
  1421. .stream()
  1422. .map(ComponentDto::uuid)
  1423. .collect(Collectors.toList());
  1424. }
  1425. @Test
  1426. public void selectByQuery_filter_on_visibility() {
  1427. db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization()).setDbKey("private-key"));
  1428. db.components().insertComponent(ComponentTesting.newPublicProjectDto(db.getDefaultOrganization()).setDbKey("public-key"));
  1429. ComponentQuery privateProjectsQuery = ComponentQuery.builder().setPrivate(true).setQualifiers(PROJECT).build();
  1430. ComponentQuery publicProjectsQuery = ComponentQuery.builder().setPrivate(false).setQualifiers(PROJECT).build();
  1431. ComponentQuery allProjectsQuery = ComponentQuery.builder().setPrivate(null).setQualifiers(PROJECT).build();
  1432. assertThat(underTest.selectByQuery(dbSession, privateProjectsQuery, 0, 10)).extracting(ComponentDto::getDbKey).containsExactly("private-key");
  1433. assertThat(underTest.selectByQuery(dbSession, publicProjectsQuery, 0, 10)).extracting(ComponentDto::getDbKey).containsExactly("public-key");
  1434. assertThat(underTest.selectByQuery(dbSession, allProjectsQuery, 0, 10)).extracting(ComponentDto::getDbKey).containsOnly("public-key", "private-key");
  1435. }
  1436. @Test
  1437. public void selectByQuery_on_empty_list_of_component_id() {
  1438. db.components().insertPrivateProject();
  1439. ComponentQuery dbQuery = ComponentQuery.builder().setQualifiers(PROJECT).setComponentIds(emptySet()).build();
  1440. List<ComponentDto> result = underTest.selectByQuery(dbSession, dbQuery, 0, 10);
  1441. int count = underTest.countByQuery(dbSession, dbQuery);
  1442. assertThat(result).isEmpty();
  1443. assertThat(count).isEqualTo(0);
  1444. }
  1445. @Test
  1446. public void selectByQuery_on_component_ids() {
  1447. OrganizationDto organizationDto = db.organizations().insert();
  1448. ComponentDto sonarqube = db.components().insertComponent(newPrivateProjectDto(organizationDto));
  1449. ComponentDto jdk8 = db.components().insertComponent(newPrivateProjectDto(organizationDto));
  1450. ComponentDto cLang = db.components().insertComponent(newPrivateProjectDto(organizationDto));
  1451. ComponentQuery query = ComponentQuery.builder().setQualifiers(PROJECT)
  1452. .setComponentIds(newHashSet(sonarqube.getId(), jdk8.getId())).build();
  1453. List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 0, 10);
  1454. assertThat(result).hasSize(2).extracting(ComponentDto::getId)
  1455. .containsOnlyOnce(sonarqube.getId(), jdk8.getId())
  1456. .doesNotContain(cLang.getId());
  1457. }
  1458. @Test
  1459. public void selectByQuery_on_empty_list_of_component_key() {
  1460. db.components().insertPrivateProject();
  1461. ComponentQuery dbQuery = ComponentQuery.builder().setQualifiers(PROJECT).setComponentKeys(emptySet()).build();
  1462. List<ComponentDto> result = underTest.selectByQuery(dbSession, dbQuery, 0, 10);
  1463. int count = underTest.countByQuery(dbSession, dbQuery);
  1464. assertThat(result).isEmpty();
  1465. assertThat(count).isEqualTo(0);
  1466. }
  1467. @Test
  1468. public void selectByQuery_on_component_keys() {
  1469. OrganizationDto organizationDto = db.organizations().insert();
  1470. ComponentDto sonarqube = db.components().insertComponent(newPrivateProjectDto(organizationDto));
  1471. ComponentDto jdk8 = db.components().insertComponent(newPrivateProjectDto(organizationDto));
  1472. ComponentDto cLang = db.components().insertComponent(newPrivateProjectDto(organizationDto));
  1473. ComponentQuery query = ComponentQuery.builder().setQualifiers(PROJECT)
  1474. .setComponentKeys(newHashSet(sonarqube.getDbKey(), jdk8.getDbKey())).build();
  1475. List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 0, 10);
  1476. assertThat(result).hasSize(2).extracting(ComponentDto::getDbKey)
  1477. .containsExactlyInAnyOrder(sonarqube.getDbKey(), jdk8.getDbKey())
  1478. .doesNotContain(cLang.getDbKey());
  1479. }
  1480. @Test
  1481. public void selectByQuery_on_empty_list_of_component_uuids() {
  1482. db.components().insertPrivateProject();
  1483. ComponentQuery dbQuery = ComponentQuery.builder().setQualifiers(PROJECT).setComponentUuids(emptySet()).build();
  1484. List<ComponentDto> result = underTest.selectByQuery(dbSession, dbQuery, 0, 10);
  1485. int count = underTest.countByQuery(dbSession, dbQuery);
  1486. assertThat(result).isEmpty();
  1487. assertThat(count).isEqualTo(0);
  1488. }
  1489. @Test
  1490. public void selectByQuery_on_component_uuids() {
  1491. OrganizationDto organizationDto = db.organizations().insert();
  1492. ComponentDto sonarqube = db.components().insertComponent(newPrivateProjectDto(organizationDto));
  1493. ComponentDto jdk8 = db.components().insertComponent(newPrivateProjectDto(organizationDto));
  1494. ComponentDto cLang = db.components().insertComponent(newPrivateProjectDto(organizationDto));
  1495. ComponentQuery query = ComponentQuery.builder().setQualifiers(PROJECT)
  1496. .setComponentUuids(newHashSet(sonarqube.uuid(), jdk8.uuid())).build();
  1497. List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 0, 10);
  1498. assertThat(result).hasSize(2).extracting(ComponentDto::uuid)
  1499. .containsOnlyOnce(sonarqube.uuid(), jdk8.uuid())
  1500. .doesNotContain(cLang.uuid());
  1501. }
  1502. @Test
  1503. public void selectAncestors() {
  1504. // organization
  1505. OrganizationDto organization = db.organizations().insert();
  1506. // project -> module -> file
  1507. ComponentDto project = newPrivateProjectDto(organization, PROJECT_UUID);
  1508. db.components().insertProjectAndSnapshot(project);
  1509. ComponentDto module = newModuleDto(MODULE_UUID, project);
  1510. db.components().insertComponent(module);
  1511. ComponentDto file = newFileDto(module, null, FILE_1_UUID);
  1512. db.components().insertComponent(file);
  1513. db.commit();
  1514. // ancestors of root
  1515. List<ComponentDto> ancestors = underTest.selectAncestors(dbSession, project);
  1516. assertThat(ancestors).isEmpty();
  1517. // ancestors of module
  1518. ancestors = underTest.selectAncestors(dbSession, module);
  1519. assertThat(ancestors).extracting("uuid").containsExactly(PROJECT_UUID);
  1520. // ancestors of file
  1521. ancestors = underTest.selectAncestors(dbSession, file);
  1522. assertThat(ancestors).extracting("uuid").containsExactly(PROJECT_UUID, MODULE_UUID);
  1523. }
  1524. @Test
  1525. public void select_descendants_with_children_strategy() {
  1526. // project has 2 children: module and file 1. Other files are part of module.
  1527. ComponentDto project = newPrivateProjectDto(db.organizations().insert(), PROJECT_UUID);
  1528. db.components().insertProjectAndSnapshot(project);
  1529. ComponentDto module = newModuleDto(MODULE_UUID, project);
  1530. db.components().insertComponent(module);
  1531. ComponentDto fileInProject = newFileDto(project, null, FILE_1_UUID).setDbKey("file-key-1").setName("File One");
  1532. db.components().insertComponent(fileInProject);
  1533. ComponentDto file1InModule = newFileDto(module, null, FILE_2_UUID).setDbKey("file-key-2").setName("File Two");
  1534. db.components().insertComponent(file1InModule);
  1535. ComponentDto file2InModule = newFileDto(module, null, FILE_3_UUID).setDbKey("file-key-3").setName("File Three");
  1536. db.components().insertComponent(file2InModule);
  1537. db.commit();
  1538. // test children of root
  1539. ComponentTreeQuery query = newTreeQuery(PROJECT_UUID).build();
  1540. List<ComponentDto> children = underTest.selectDescendants(dbSession, query);
  1541. assertThat(children).extracting("uuid").containsOnly(FILE_1_UUID, MODULE_UUID);
  1542. // test children of root, filtered by qualifier
  1543. query = newTreeQuery(PROJECT_UUID).setQualifiers(asList(Qualifiers.MODULE)).build();
  1544. children = underTest.selectDescendants(dbSession, query);
  1545. assertThat(children).extracting("uuid").containsOnly(MODULE_UUID);
  1546. // test children of intermediate component (module here), default ordering by
  1547. query = newTreeQuery(MODULE_UUID).build();
  1548. assertThat(underTest.selectDescendants(dbSession, query)).extracting("uuid").containsOnly(FILE_2_UUID, FILE_3_UUID);
  1549. // test children of leaf component (file here)
  1550. query = newTreeQuery(FILE_1_UUID).build();
  1551. assertThat(underTest.selectDescendants(dbSession, query)).isEmpty();
  1552. // test children of root, matching name
  1553. query = newTreeQuery(PROJECT_UUID).setNameOrKeyQuery("One").build();
  1554. assertThat(underTest.selectDescendants(dbSession, query)).extracting("uuid").containsOnly(FILE_1_UUID);
  1555. // test children of root, matching case-insensitive name
  1556. query = newTreeQuery(PROJECT_UUID).setNameOrKeyQuery("OnE").build();
  1557. assertThat(underTest.selectDescendants(dbSession, query)).extracting("uuid").containsOnly(FILE_1_UUID);
  1558. // test children of root, matching key
  1559. query = newTreeQuery(PROJECT_UUID).setNameOrKeyQuery("file-key-1").build();
  1560. assertThat(underTest.selectDescendants(dbSession, query)).extracting("uuid").containsOnly(FILE_1_UUID);
  1561. // test children of root, without matching name nor key
  1562. query = newTreeQuery(PROJECT_UUID).setNameOrKeyQuery("does-not-exist").build();
  1563. assertThat(underTest.selectDescendants(dbSession, query)).isEmpty();
  1564. // test children of intermediate component (module here), matching name
  1565. query = newTreeQuery(MODULE_UUID).setNameOrKeyQuery("Two").build();
  1566. assertThat(underTest.selectDescendants(dbSession, query)).extracting("uuid").containsOnly(FILE_2_UUID);
  1567. // test children of intermediate component (module here), without matching name
  1568. query = newTreeQuery(MODULE_UUID).setNameOrKeyQuery("does-not-exist").build();
  1569. assertThat(underTest.selectDescendants(dbSession, query)).isEmpty();
  1570. // test children of leaf component (file here)
  1571. query = newTreeQuery(FILE_1_UUID).build();
  1572. assertThat(underTest.selectDescendants(dbSession, query)).isEmpty();
  1573. // test children of leaf component (file here), matching name
  1574. query = newTreeQuery(FILE_1_UUID).setNameOrKeyQuery("Foo").build();
  1575. assertThat(underTest.selectDescendants(dbSession, query)).isEmpty();
  1576. // test filtering by scope
  1577. query = newTreeQuery(project.uuid()).setScopes(asList(Scopes.FILE)).build();
  1578. assertThat(underTest.selectDescendants(dbSession, query))
  1579. .extracting(ComponentDto::uuid)
  1580. .containsExactlyInAnyOrder(fileInProject.uuid());
  1581. query = newTreeQuery(project.uuid()).setScopes(asList(Scopes.PROJECT)).build();
  1582. assertThat(underTest.selectDescendants(dbSession, query))
  1583. .extracting(ComponentDto::uuid)
  1584. .containsExactlyInAnyOrder(module.uuid());
  1585. }
  1586. @Test
  1587. public void select_descendants_with_leaves_strategy() {
  1588. ComponentDto project = newPrivateProjectDto(db.getDefaultOrganization(), PROJECT_UUID);
  1589. db.components().insertProjectAndSnapshot(project);
  1590. db.components().insertComponent(newModuleDto("module-1-uuid", project));
  1591. db.components().insertComponent(newFileDto(project, null, "file-1-uuid"));
  1592. db.components().insertComponent(newFileDto(project, null, "file-2-uuid"));
  1593. db.commit();
  1594. ComponentTreeQuery query = newTreeQuery(PROJECT_UUID).setStrategy(LEAVES).build();
  1595. List<ComponentDto> result = underTest.selectDescendants(dbSession, query);
  1596. assertThat(result).extracting("uuid").containsOnly("file-1-uuid", "file-2-uuid", "module-1-uuid");
  1597. }
  1598. @Test
  1599. public void select_descendants_returns_empty_list_if_base_component_does_not_exist() {
  1600. ComponentTreeQuery query = newTreeQuery(PROJECT_UUID).setStrategy(CHILDREN).build();
  1601. List<ComponentDto> result = underTest.selectDescendants(dbSession, query);
  1602. assertThat(result).isEmpty();
  1603. }
  1604. @Test
  1605. public void select_descendants_of_a_view_and_filter_by_name() {
  1606. OrganizationDto organizationDto = db.organizations().insert();
  1607. ComponentDto view = newView(organizationDto, A_VIEW_UUID);
  1608. db.components().insertViewAndSnapshot(view);
  1609. // one subview
  1610. ComponentDto subView = newSubView(view, "subview-uuid", "subview-key").setName("subview name");
  1611. db.components().insertComponent(subView);
  1612. // one project and its copy linked to the view
  1613. ComponentDto project = newPrivateProjectDto(organizationDto, PROJECT_UUID).setName("project name");
  1614. db.components().insertProjectAndSnapshot(project);
  1615. db.components().insertComponent(newProjectCopy("project-copy-uuid", project, view));
  1616. ComponentTreeQuery dbQuery = newTreeQuery(A_VIEW_UUID).setNameOrKeyQuery("name").setStrategy(CHILDREN).build();
  1617. List<ComponentDto> components = underTest.selectDescendants(dbSession, dbQuery);
  1618. assertThat(components).extracting("uuid").containsOnly("project-copy-uuid", "subview-uuid");
  1619. assertThat(components).extracting("organizationUuid").containsOnly(organizationDto.getUuid());
  1620. }
  1621. @Test
  1622. public void setPrivateForRootComponentUuid_updates_private_column_to_specified_value_for_all_rows_with_specified_projectUuid() {
  1623. String uuid1 = "uuid1";
  1624. String uuid2 = "uuid2";
  1625. OrganizationDto organizationDto = db.organizations().insert();
  1626. String[] uuids = {
  1627. db.components().insertComponent(newPrivateProjectDto(organizationDto).setProjectUuid(uuid1).setPrivate(true)).uuid(),
  1628. db.components().insertComponent(newPrivateProjectDto(organizationDto).setProjectUuid(uuid1).setPrivate(false)).uuid(),
  1629. db.components().insertComponent(newPrivateProjectDto(organizationDto).setProjectUuid(uuid2).setPrivate(true)).uuid(),
  1630. db.components().insertComponent(newPrivateProjectDto(organizationDto).setProjectUuid(uuid2).setPrivate(false)).uuid(),
  1631. db.components().insertComponent(newPrivateProjectDto(organizationDto).setRootUuid(uuid1).setProjectUuid("foo").setPrivate(false)).uuid(),
  1632. };
  1633. underTest.setPrivateForRootComponentUuid(db.getSession(), uuid1, true);
  1634. assertThat(privateFlagOfUuid(uuids[0])).isTrue();
  1635. assertThat(privateFlagOfUuid(uuids[1])).isTrue();
  1636. assertThat(privateFlagOfUuid(uuids[2])).isTrue();
  1637. assertThat(privateFlagOfUuid(uuids[3])).isFalse();
  1638. assertThat(privateFlagOfUuid(uuids[4])).isFalse();
  1639. underTest.setPrivateForRootComponentUuid(db.getSession(), uuid1, false);
  1640. assertThat(privateFlagOfUuid(uuids[0])).isFalse();
  1641. assertThat(privateFlagOfUuid(uuids[1])).isFalse();
  1642. assertThat(privateFlagOfUuid(uuids[2])).isTrue();
  1643. assertThat(privateFlagOfUuid(uuids[3])).isFalse();
  1644. assertThat(privateFlagOfUuid(uuids[4])).isFalse();
  1645. underTest.setPrivateForRootComponentUuid(db.getSession(), uuid2, false);
  1646. assertThat(privateFlagOfUuid(uuids[0])).isFalse();
  1647. assertThat(privateFlagOfUuid(uuids[1])).isFalse();
  1648. assertThat(privateFlagOfUuid(uuids[2])).isFalse();
  1649. assertThat(privateFlagOfUuid(uuids[3])).isFalse();
  1650. assertThat(privateFlagOfUuid(uuids[4])).isFalse();
  1651. underTest.setPrivateForRootComponentUuid(db.getSession(), uuid2, true);
  1652. assertThat(privateFlagOfUuid(uuids[0])).isFalse();
  1653. assertThat(privateFlagOfUuid(uuids[1])).isFalse();
  1654. assertThat(privateFlagOfUuid(uuids[2])).isTrue();
  1655. assertThat(privateFlagOfUuid(uuids[3])).isTrue();
  1656. assertThat(privateFlagOfUuid(uuids[4])).isFalse();
  1657. }
  1658. @Test
  1659. public void selectPrivateProjectsWithNcloc() {
  1660. MetricDto metric = db.measures().insertMetric(m -> m.setKey("ncloc"));
  1661. OrganizationDto organizationDto = db.organizations().insert();
  1662. // project1, not the biggest branch - not returned
  1663. final ComponentDto project1 = db.components().insertMainBranch(organizationDto, b -> b.setName("foo"));
  1664. insertMeasure(20d, project1, metric);
  1665. // long branch of project1 - returned
  1666. insertMeasure(30d, db.components().insertProjectBranch(project1, b -> b.setBranchType(BranchType.LONG)), metric);
  1667. // project2 - returned
  1668. insertMeasure(10d, db.components().insertMainBranch(organizationDto, b -> b.setName("bar")), metric);
  1669. // public project - not returned
  1670. insertMeasure(11d, db.components().insertMainBranch(organizationDto, b -> b.setPrivate(false)), metric);
  1671. // different org - not returned
  1672. insertMeasure(12d, db.components().insertMainBranch(db.organizations().insert()), metric);
  1673. List<ProjectNclocDistributionDto> result = underTest.selectPrivateProjectsWithNcloc(db.getSession(), organizationDto.getUuid());
  1674. assertThat(result).extracting(ProjectNclocDistributionDto::getName).containsExactly("foo", "bar");
  1675. assertThat(result).extracting(ProjectNclocDistributionDto::getNcloc).containsExactly(30L, 10L);
  1676. }
  1677. private boolean privateFlagOfUuid(String uuid) {
  1678. return underTest.selectByUuid(db.getSession(), uuid).get().isPrivate();
  1679. }
  1680. private static Set<String> shuffleWithNonExistentUuids(String... uuids) {
  1681. return Stream.concat(
  1682. IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(i -> randomAlphabetic(9)),
  1683. Arrays.stream(uuids))
  1684. .collect(toSet());
  1685. }
  1686. private void insertMeasure(double value, ComponentDto componentDto, MetricDto metric) {
  1687. db.measures().insertLiveMeasure(componentDto, metric, m -> m.setValue(value));
  1688. }
  1689. }