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

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