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.

SearchActionIT.java 100KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191
  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.server.hotspot.ws;
  21. import com.google.common.collect.Ordering;
  22. import com.google.common.collect.Sets;
  23. import com.tngtech.java.junit.dataprovider.DataProvider;
  24. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  25. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  26. import java.util.Arrays;
  27. import java.util.Collection;
  28. import java.util.Collections;
  29. import java.util.Comparator;
  30. import java.util.List;
  31. import java.util.Map;
  32. import java.util.Random;
  33. import java.util.Set;
  34. import java.util.function.Consumer;
  35. import java.util.function.Function;
  36. import java.util.stream.Collectors;
  37. import java.util.stream.IntStream;
  38. import java.util.stream.Stream;
  39. import javax.annotation.Nullable;
  40. import org.junit.Rule;
  41. import org.junit.Test;
  42. import org.junit.runner.RunWith;
  43. import org.sonar.api.impl.utils.TestSystem2;
  44. import org.sonar.api.issue.Issue;
  45. import org.sonar.api.rule.RuleKey;
  46. import org.sonar.api.rules.RuleType;
  47. import org.sonar.api.server.ws.WebService;
  48. import org.sonar.api.utils.System2;
  49. import org.sonar.db.DbClient;
  50. import org.sonar.db.DbTester;
  51. import org.sonar.db.component.BranchDto;
  52. import org.sonar.db.component.BranchType;
  53. import org.sonar.db.component.ComponentDto;
  54. import org.sonar.db.component.ComponentTesting;
  55. import org.sonar.db.component.ProjectData;
  56. import org.sonar.db.issue.IssueDto;
  57. import org.sonar.db.project.ProjectDto;
  58. import org.sonar.db.protobuf.DbCommons;
  59. import org.sonar.db.protobuf.DbIssues;
  60. import org.sonar.db.rule.RuleDto;
  61. import org.sonar.db.rule.RuleTesting;
  62. import org.sonar.server.component.ComponentFinder;
  63. import org.sonar.server.component.TestComponentFinder;
  64. import org.sonar.server.es.EsTester;
  65. import org.sonar.server.exceptions.ForbiddenException;
  66. import org.sonar.server.exceptions.NotFoundException;
  67. import org.sonar.server.issue.TextRangeResponseFormatter;
  68. import org.sonar.server.issue.index.AsyncIssueIndexing;
  69. import org.sonar.server.issue.index.IssueIndex;
  70. import org.sonar.server.issue.index.IssueIndexSyncProgressChecker;
  71. import org.sonar.server.issue.index.IssueIndexer;
  72. import org.sonar.server.issue.index.IssueIteratorFactory;
  73. import org.sonar.server.permission.index.PermissionIndexer;
  74. import org.sonar.server.permission.index.WebAuthorizationTypeSupport;
  75. import org.sonar.server.security.SecurityStandards;
  76. import org.sonar.server.security.SecurityStandards.SQCategory;
  77. import org.sonar.server.tester.UserSessionRule;
  78. import org.sonar.server.view.index.ViewIndexer;
  79. import org.sonar.server.ws.TestRequest;
  80. import org.sonar.server.ws.WsActionTester;
  81. import org.sonarqube.ws.Common;
  82. import org.sonarqube.ws.Hotspots.Component;
  83. import org.sonarqube.ws.Hotspots.SearchWsResponse;
  84. import static com.google.common.collect.ImmutableSet.of;
  85. import static java.util.Collections.singleton;
  86. import static java.util.stream.Collectors.joining;
  87. import static java.util.stream.Collectors.toList;
  88. import static java.util.stream.Collectors.toSet;
  89. import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
  90. import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
  91. import static org.assertj.core.api.Assertions.assertThat;
  92. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  93. import static org.assertj.core.groups.Tuple.tuple;
  94. import static org.mockito.ArgumentMatchers.any;
  95. import static org.mockito.ArgumentMatchers.eq;
  96. import static org.mockito.Mockito.mock;
  97. import static org.mockito.Mockito.times;
  98. import static org.mockito.Mockito.verify;
  99. import static org.sonar.api.issue.Issue.RESOLUTION_ACKNOWLEDGED;
  100. import static org.sonar.api.issue.Issue.RESOLUTION_FIXED;
  101. import static org.sonar.api.issue.Issue.RESOLUTION_SAFE;
  102. import static org.sonar.api.issue.Issue.STATUSES;
  103. import static org.sonar.api.issue.Issue.STATUS_CLOSED;
  104. import static org.sonar.api.issue.Issue.STATUS_REVIEWED;
  105. import static org.sonar.api.issue.Issue.STATUS_TO_REVIEW;
  106. import static org.sonar.api.rules.RuleType.SECURITY_HOTSPOT;
  107. import static org.sonar.api.utils.DateUtils.formatDateTime;
  108. import static org.sonar.api.web.UserRole.USER;
  109. import static org.sonar.db.component.ComponentTesting.newDirectory;
  110. import static org.sonar.db.component.ComponentTesting.newDirectoryOnBranch;
  111. import static org.sonar.db.component.ComponentTesting.newFileDto;
  112. import static org.sonar.db.issue.IssueTesting.newCodeReferenceIssue;
  113. import static org.sonar.db.issue.IssueTesting.newIssue;
  114. import static org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH;
  115. @SuppressWarnings("ALL")
  116. @RunWith(DataProviderRunner.class)
  117. public class SearchActionIT {
  118. private static final String PARAM_PROJECT_KEY = "projectKey";
  119. private static final String PARAM_STATUS = "status";
  120. private static final String PARAM_RESOLUTION = "resolution";
  121. private static final String PARAM_HOTSPOTS = "hotspots";
  122. private static final String PARAM_BRANCH = "branch";
  123. private static final String PARAM_PULL_REQUEST = "pullRequest";
  124. private static final String PARAM_IN_NEW_CODE_PERIOD = "inNewCodePeriod";
  125. private static final String PARAM_ONLY_MINE = "onlyMine";
  126. private static final String PARAM_PCI_DSS_32 = "pciDss-3.2";
  127. private static final String PARAM_PCI_DSS_40 = "pciDss-4.0";
  128. private static final String PARAM_OWASP_ASVS_40 = "owaspAsvs-4.0";
  129. private static final String PARAM_OWASP_ASVS_LEVEL = "owaspAsvsLevel";
  130. private static final String PARAM_OWASP_TOP_10_2017 = "owaspTop10";
  131. private static final String PARAM_OWASP_TOP_10_2021 = "owaspTop10-2021";
  132. private static final String PARAM_SANS_TOP_25 = "sansTop25";
  133. private static final String PARAM_SONARSOURCE_SECURITY = "sonarsourceSecurity";
  134. private static final String PARAM_CWE = "cwe";
  135. private static final String PARAM_FILES = "files";
  136. private static final Random RANDOM = new Random();
  137. private static final int ONE_MINUTE = 60_000;
  138. private static final List<String> RESOLUTION_TYPES = List.of(RESOLUTION_FIXED, RESOLUTION_SAFE, RESOLUTION_ACKNOWLEDGED);
  139. @Rule
  140. public DbTester dbTester = DbTester.create(System2.INSTANCE);
  141. @Rule
  142. public EsTester es = EsTester.create();
  143. @Rule
  144. public UserSessionRule userSessionRule = UserSessionRule.standalone();
  145. private final TestSystem2 system2 = new TestSystem2();
  146. private final DbClient dbClient = dbTester.getDbClient();
  147. private final IssueIndex issueIndex = new IssueIndex(es.client(), System2.INSTANCE, userSessionRule, new WebAuthorizationTypeSupport(userSessionRule));
  148. private final IssueIndexer issueIndexer = new IssueIndexer(es.client(), dbClient, new IssueIteratorFactory(dbClient), mock(AsyncIssueIndexing.class));
  149. private final ViewIndexer viewIndexer = new ViewIndexer(dbClient, es.client());
  150. private final PermissionIndexer permissionIndexer = new PermissionIndexer(dbClient, es.client(), issueIndexer);
  151. private final HotspotWsResponseFormatter responseFormatter = new HotspotWsResponseFormatter(new TextRangeResponseFormatter());
  152. private final IssueIndexSyncProgressChecker issueIndexSyncProgressChecker = mock(IssueIndexSyncProgressChecker.class);
  153. private final ComponentFinder componentFinder = TestComponentFinder.from(dbTester);
  154. private final SearchAction underTest = new SearchAction(dbClient, userSessionRule, issueIndex,
  155. issueIndexSyncProgressChecker, responseFormatter, system2, componentFinder);
  156. private final WsActionTester actionTester = new WsActionTester(underTest);
  157. @Test
  158. public void verify_ws_def() {
  159. WebService.Param onlyMineParam = actionTester.getDef().param(PARAM_ONLY_MINE);
  160. WebService.Param pciDss32Param = actionTester.getDef().param(PARAM_PCI_DSS_32);
  161. WebService.Param pciDss40Param = actionTester.getDef().param(PARAM_PCI_DSS_40);
  162. WebService.Param owasAsvs40Param = actionTester.getDef().param(PARAM_OWASP_ASVS_40);
  163. WebService.Param owaspTop10Param = actionTester.getDef().param(PARAM_OWASP_TOP_10_2017);
  164. WebService.Param sansTop25Param = actionTester.getDef().param(PARAM_SANS_TOP_25);
  165. WebService.Param sonarsourceSecurityParam = actionTester.getDef().param(PARAM_SONARSOURCE_SECURITY);
  166. WebService.Param filesParam = actionTester.getDef().param(PARAM_FILES);
  167. assertThat(actionTester.getDef().isInternal()).isFalse();
  168. assertThat(onlyMineParam).isNotNull();
  169. assertThat(onlyMineParam.isRequired()).isFalse();
  170. assertThat(actionTester.getDef().param(PARAM_ONLY_MINE).possibleValues())
  171. .containsExactlyInAnyOrder("yes", "no", "true", "false");
  172. assertThat(pciDss32Param).isNotNull();
  173. assertThat(pciDss32Param.isRequired()).isFalse();
  174. assertThat(pciDss40Param).isNotNull();
  175. assertThat(pciDss40Param.isRequired()).isFalse();
  176. assertThat(owasAsvs40Param).isNotNull();
  177. assertThat(owasAsvs40Param.isRequired()).isFalse();
  178. assertThat(owaspTop10Param).isNotNull();
  179. assertThat(owaspTop10Param.isRequired()).isFalse();
  180. assertThat(sansTop25Param).isNotNull();
  181. assertThat(sansTop25Param.isRequired()).isFalse();
  182. assertThat(sonarsourceSecurityParam).isNotNull();
  183. assertThat(sonarsourceSecurityParam.isRequired()).isFalse();
  184. assertThat(filesParam).isNotNull();
  185. }
  186. @Test
  187. public void fails_with_IAE_if_parameters_projectKey_and_hotspots_are_missing() {
  188. TestRequest request = actionTester.newRequest();
  189. assertThatThrownBy(request::execute)
  190. .isInstanceOf(IllegalArgumentException.class)
  191. .hasMessage("A value must be provided for either parameter 'projectKey' or parameter 'hotspots'");
  192. }
  193. @Test
  194. public void fail_with_IAE_if_parameter_branch_is_used_without_parameter_projectKey() {
  195. TestRequest request = actionTester.newRequest()
  196. .setParam(PARAM_HOTSPOTS, randomAlphabetic(2))
  197. .setParam(PARAM_BRANCH, randomAlphabetic(1));
  198. assertThatThrownBy(request::execute)
  199. .isInstanceOf(IllegalArgumentException.class)
  200. .hasMessage("Parameter 'branch' must be used with parameter 'projectKey'");
  201. }
  202. @Test
  203. public void fail_with_IAE_if_parameter_pullRequest_is_used_without_parameter_projectKey() {
  204. TestRequest request = actionTester.newRequest()
  205. .setParam(PARAM_HOTSPOTS, randomAlphabetic(2))
  206. .setParam(PARAM_PULL_REQUEST, randomAlphabetic(1));
  207. assertThatThrownBy(request::execute)
  208. .isInstanceOf(IllegalArgumentException.class)
  209. .hasMessage("Parameter 'pullRequest' must be used with parameter 'projectKey'");
  210. }
  211. @Test
  212. public void fail_with_IAE_if_both_parameters_pullRequest_and_branch_are_provided() {
  213. TestRequest request = actionTester.newRequest()
  214. .setParam(PARAM_PROJECT_KEY, randomAlphabetic(2))
  215. .setParam(PARAM_BRANCH, randomAlphabetic(1))
  216. .setParam(PARAM_PULL_REQUEST, randomAlphabetic(1));
  217. assertThatThrownBy(request::execute)
  218. .isInstanceOf(IllegalArgumentException.class)
  219. .hasMessage("Only one of parameters 'branch' and 'pullRequest' can be provided");
  220. }
  221. @Test
  222. @UseDataProvider("badStatuses")
  223. public void fails_with_IAE_if_status_parameter_is_neither_TO_REVIEW_or_REVIEWED(String badStatus) {
  224. TestRequest request = actionTester.newRequest()
  225. .setParam(PARAM_PROJECT_KEY, randomAlphabetic(13))
  226. .setParam(PARAM_STATUS, badStatus);
  227. assertThatThrownBy(request::execute)
  228. .isInstanceOf(IllegalArgumentException.class)
  229. .hasMessage("Value of parameter 'status' (" + badStatus + ") must be one of: [TO_REVIEW, REVIEWED]");
  230. }
  231. @DataProvider
  232. public static Object[][] badStatuses() {
  233. return Stream.concat(
  234. Issue.STATUSES.stream(),
  235. Stream.of(randomAlphabetic(3)))
  236. .filter(t -> !STATUS_REVIEWED.equals(t))
  237. .filter(t -> !STATUS_TO_REVIEW.equals(t))
  238. .map(t -> new Object[] {t})
  239. .toArray(Object[][]::new);
  240. }
  241. @Test
  242. @UseDataProvider("validStatusesAndResolutions")
  243. public void fail_with_IAE_if_parameter_status_is_specified_with_hotspots_parameter(String status, @Nullable String notUsed) {
  244. TestRequest request = actionTester.newRequest()
  245. .setParam(PARAM_HOTSPOTS, randomAlphabetic(12))
  246. .setParam(PARAM_STATUS, status);
  247. assertThatThrownBy(request::execute)
  248. .isInstanceOf(IllegalArgumentException.class)
  249. .hasMessage("Parameter 'status' can't be used with parameter 'hotspots'");
  250. }
  251. @Test
  252. @UseDataProvider("badResolutions")
  253. public void fails_with_IAE_if_resolution_parameter_is_neither_FIXED_nor_SAFE(String badResolution) {
  254. TestRequest request = actionTester.newRequest()
  255. .setParam(PARAM_PROJECT_KEY, randomAlphabetic(13))
  256. .setParam(PARAM_STATUS, STATUS_TO_REVIEW)
  257. .setParam(PARAM_RESOLUTION, badResolution);
  258. assertThatThrownBy(request::execute)
  259. .isInstanceOf(IllegalArgumentException.class)
  260. .hasMessage("Value of parameter 'resolution' (" + badResolution + ") must be one of: [FIXED, SAFE, ACKNOWLEDGED]");
  261. }
  262. @DataProvider
  263. public static Object[][] badResolutions() {
  264. return Stream.of(
  265. Issue.RESOLUTIONS.stream(),
  266. Issue.SECURITY_HOTSPOT_RESOLUTIONS.stream(),
  267. Stream.of(randomAlphabetic(4)))
  268. .flatMap(t -> t)
  269. .filter(t -> !RESOLUTION_TYPES.contains(t))
  270. .map(t -> new Object[] {t})
  271. .toArray(Object[][]::new);
  272. }
  273. @Test
  274. @UseDataProvider("fixedOrSafeResolution")
  275. public void fails_with_IAE_if_resolution_is_provided_with_status_TO_REVIEW(String resolution) {
  276. TestRequest request = actionTester.newRequest()
  277. .setParam(PARAM_PROJECT_KEY, randomAlphabetic(13))
  278. .setParam(PARAM_STATUS, STATUS_TO_REVIEW)
  279. .setParam(PARAM_RESOLUTION, resolution);
  280. assertThatThrownBy(request::execute)
  281. .isInstanceOf(IllegalArgumentException.class)
  282. .hasMessage("Value '" + resolution + "' of parameter 'resolution' can only be provided if value of parameter 'status' is 'REVIEWED'");
  283. }
  284. @Test
  285. @UseDataProvider("fixedOrSafeResolution")
  286. public void fails_with_IAE_if_resolution_is_provided_with_hotspots_parameter(String resolution) {
  287. TestRequest request = actionTester.newRequest()
  288. .setParam(PARAM_HOTSPOTS, randomAlphabetic(13))
  289. .setParam(PARAM_RESOLUTION, resolution);
  290. assertThatThrownBy(request::execute)
  291. .isInstanceOf(IllegalArgumentException.class)
  292. .hasMessage("Parameter 'resolution' can't be used with parameter 'hotspots'");
  293. }
  294. @DataProvider
  295. public static Object[][] fixedOrSafeResolution() {
  296. return new Object[][] {
  297. {RESOLUTION_SAFE},
  298. {RESOLUTION_FIXED}
  299. };
  300. }
  301. @Test
  302. public void fails_with_NotFoundException_if_project_does_not_exist() {
  303. String key = randomAlphabetic(12);
  304. TestRequest request = actionTester.newRequest()
  305. .setParam(PARAM_PROJECT_KEY, key);
  306. assertThatThrownBy(request::execute)
  307. .isInstanceOf(NotFoundException.class)
  308. .hasMessage("Project '%s' not found", key);
  309. }
  310. @Test
  311. public void fails_with_NotFoundException_if_project_is_neither_a_project_nor_an_application() {
  312. ComponentDto project = dbTester.components().insertPrivateProject().getMainBranchComponent();
  313. ComponentDto directory = dbTester.components().insertComponent(ComponentTesting.newDirectory(project, "foo"));
  314. ComponentDto file = dbTester.components().insertComponent(ComponentTesting.newFileDto(project));
  315. ComponentDto portfolio = dbTester.components().insertPrivatePortfolio();
  316. TestRequest request = actionTester.newRequest();
  317. for (ComponentDto component : Arrays.asList(directory, file, portfolio)) {
  318. request.setParam(PARAM_PROJECT_KEY, component.getKey());
  319. assertThatThrownBy(request::execute)
  320. .isInstanceOf(NotFoundException.class)
  321. .hasMessage("Project '%s' not found", component.getKey());
  322. }
  323. }
  324. @Test
  325. public void fails_with_ForbiddenException_if_project_is_private_and_not_allowed() {
  326. ProjectData projectData = dbTester.components().insertPrivateProject();
  327. ComponentDto project = projectData.getMainBranchComponent();
  328. userSessionRule.registerProjects(projectData.getProjectDto());
  329. TestRequest request = newRequest(project);
  330. assertThatThrownBy(request::execute)
  331. .isInstanceOf(ForbiddenException.class)
  332. .hasMessage("Insufficient privileges");
  333. }
  334. @Test
  335. public void fails_with_ForbiddenException_if_application_is_private_and_not_allowed() {
  336. ProjectData projectData = dbTester.components().insertPrivateApplication();
  337. ComponentDto application = projectData.getMainBranchComponent();
  338. userSessionRule.registerProjects(projectData.getProjectDto());
  339. TestRequest request = newRequest(application);
  340. assertThatThrownBy(request::execute)
  341. .isInstanceOf(ForbiddenException.class)
  342. .hasMessage("Insufficient privileges");
  343. }
  344. @Test
  345. public void succeeds_on_public_project() {
  346. ProjectData projectData = dbTester.components().insertPublicProject();
  347. ComponentDto project = projectData.getMainBranchComponent();
  348. userSessionRule.registerProjects(projectData.getProjectDto());
  349. SearchWsResponse response = newRequest(project)
  350. .executeProtobuf(SearchWsResponse.class);
  351. assertThat(response.getHotspotsList()).isEmpty();
  352. assertThat(response.getComponentsList()).isEmpty();
  353. }
  354. @Test
  355. public void succeeds_on_public_application() {
  356. ProjectData applicationData = dbTester.components().insertPublicApplication();
  357. ComponentDto application = applicationData.getMainBranchComponent();
  358. userSessionRule.registerApplication(applicationData.getProjectDto());
  359. SearchWsResponse response = newRequest(application)
  360. .executeProtobuf(SearchWsResponse.class);
  361. assertThat(response.getHotspotsList()).isEmpty();
  362. assertThat(response.getComponentsList()).isEmpty();
  363. }
  364. @Test
  365. public void succeeds_on_private_project_with_permission() {
  366. ProjectData projectData = dbTester.components().insertPrivateProject();
  367. ComponentDto project = projectData.getMainBranchComponent();
  368. userSessionRule.registerProjects(projectData.getProjectDto());
  369. userSessionRule.logIn().addProjectPermission(USER, projectData.getProjectDto());
  370. SearchWsResponse response = newRequest(project).executeProtobuf(SearchWsResponse.class);
  371. assertThat(response.getHotspotsList()).isEmpty();
  372. assertThat(response.getComponentsList()).isEmpty();
  373. }
  374. @Test
  375. public void succeeds_on_private_application_with_permission() {
  376. ProjectData applicationData = dbTester.components().insertPrivateApplication();
  377. ComponentDto application = applicationData.getMainBranchComponent();
  378. userSessionRule.logIn().registerApplication(applicationData.getProjectDto()).addProjectPermission(USER, applicationData.getProjectDto());
  379. SearchWsResponse response = newRequest(application).executeProtobuf(SearchWsResponse.class);
  380. assertThat(response.getHotspotsList()).isEmpty();
  381. assertThat(response.getComponentsList()).isEmpty();
  382. }
  383. @Test
  384. public void does_not_fail_if_rule_of_hotspot_does_not_exist_in_DB() {
  385. ProjectData projectData = dbTester.components().insertPublicProject();
  386. ComponentDto project = projectData.getMainBranchComponent();
  387. userSessionRule.registerProjects(projectData.getProjectDto());
  388. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  389. indexPermissions();
  390. IssueDto[] hotspots = IntStream.range(0, 1 + RANDOM.nextInt(10))
  391. .mapToObj(i -> {
  392. RuleDto rule = newRule(SECURITY_HOTSPOT);
  393. return insertHotspot(project, file, rule);
  394. })
  395. .toArray(IssueDto[]::new);
  396. indexIssues();
  397. IssueDto hotspotWithoutRule = hotspots[RANDOM.nextInt(hotspots.length)];
  398. dbTester.executeUpdateSql("delete from rules where uuid=?", hotspotWithoutRule.getRuleUuid());
  399. SearchWsResponse response = newRequest(project)
  400. .executeProtobuf(SearchWsResponse.class);
  401. assertThat(response.getHotspotsList())
  402. .extracting(SearchWsResponse.Hotspot::getKey)
  403. .containsOnly(Arrays.stream(hotspots)
  404. .map(IssueDto::getKey)
  405. .filter(key -> !key.equals(hotspotWithoutRule.getKey()))
  406. .toArray(String[]::new));
  407. }
  408. @Test
  409. public void returns_no_hotspot_component_nor_rule_when_project_has_no_hotspot() {
  410. ProjectData projectData = dbTester.components().insertPublicProject();
  411. ComponentDto project = projectData.getMainBranchComponent();
  412. userSessionRule.registerProjects(projectData.getProjectDto());
  413. indexPermissions();
  414. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  415. Arrays.stream(RuleType.values())
  416. .filter(t -> t != SECURITY_HOTSPOT)
  417. .forEach(ruleType -> {
  418. RuleDto rule = newRule(ruleType);
  419. dbTester.issues().insert(rule, project, file, t -> t.setType(ruleType));
  420. });
  421. indexIssues();
  422. SearchWsResponse response = newRequest(project)
  423. .executeProtobuf(SearchWsResponse.class);
  424. assertThat(response.getHotspotsList()).isEmpty();
  425. }
  426. @Test
  427. public void returns_hotspot_components_when_project_has_hotspots() {
  428. ProjectData projectData = dbTester.components().insertPublicProject();
  429. ComponentDto project = projectData.getMainBranchComponent();
  430. userSessionRule.registerProjects(projectData.getProjectDto());
  431. indexPermissions();
  432. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  433. ComponentDto fileWithHotspot = dbTester.components().insertComponent(newFileDto(project));
  434. Arrays.stream(RuleType.values())
  435. .filter(t -> t != SECURITY_HOTSPOT)
  436. .forEach(ruleType -> {
  437. RuleDto rule = newRule(ruleType);
  438. dbTester.issues().insert(rule, project, file, t -> t.setType(ruleType));
  439. });
  440. IssueDto[] hotspots = IntStream.range(0, 1 + RANDOM.nextInt(10))
  441. .mapToObj(i -> {
  442. RuleDto rule = newRule(SECURITY_HOTSPOT);
  443. return insertHotspot(project, fileWithHotspot, rule);
  444. })
  445. .toArray(IssueDto[]::new);
  446. indexIssues();
  447. SearchWsResponse response = newRequest(project)
  448. .executeProtobuf(SearchWsResponse.class);
  449. assertThat(response.getHotspotsList())
  450. .extracting(SearchWsResponse.Hotspot::getKey)
  451. .containsOnly(Arrays.stream(hotspots).map(IssueDto::getKey).toArray(String[]::new));
  452. assertThat(response.getComponentsList())
  453. .extracting(Component::getKey)
  454. .containsOnly(project.getKey(), fileWithHotspot.getKey());
  455. }
  456. @Test
  457. public void returns_single_component_when_all_hotspots_are_on_project() {
  458. ProjectData projectData = dbTester.components().insertPublicProject();
  459. ComponentDto project = projectData.getMainBranchComponent();
  460. userSessionRule.registerProjects(projectData.getProjectDto());
  461. indexPermissions();
  462. IssueDto[] hotspots = IntStream.range(0, 1 + RANDOM.nextInt(10))
  463. .mapToObj(i -> {
  464. RuleDto rule = newRule(SECURITY_HOTSPOT);
  465. return insertHotspot(project, project, rule);
  466. })
  467. .toArray(IssueDto[]::new);
  468. indexIssues();
  469. SearchWsResponse response = newRequest(project)
  470. .executeProtobuf(SearchWsResponse.class);
  471. assertThat(response.getHotspotsList())
  472. .extracting(SearchWsResponse.Hotspot::getKey)
  473. .containsOnly(Arrays.stream(hotspots).map(IssueDto::getKey).toArray(String[]::new));
  474. assertThat(response.getComponentsList())
  475. .extracting(Component::getKey)
  476. .containsOnly(project.getKey());
  477. }
  478. @Test
  479. public void returns_hotspots_of_specified_project() {
  480. ProjectData projectData1 = dbTester.components().insertPublicProject();
  481. ComponentDto project1 = projectData1.getMainBranchComponent();
  482. ProjectData projectData2 = dbTester.components().insertPublicProject();
  483. ComponentDto project2 = projectData2.getMainBranchComponent();
  484. userSessionRule.registerProjects(projectData1.getProjectDto(), projectData2.getProjectDto());
  485. indexPermissions();
  486. ComponentDto file1 = dbTester.components().insertComponent(newFileDto(project1));
  487. ComponentDto file2 = dbTester.components().insertComponent(newFileDto(project2));
  488. IssueDto[] hotspots2 = IntStream.range(0, 1 + RANDOM.nextInt(10))
  489. .mapToObj(i -> {
  490. RuleDto rule = newRule(SECURITY_HOTSPOT);
  491. insertHotspot(project1, file1, rule);
  492. return insertHotspot(project2, file2, rule);
  493. })
  494. .toArray(IssueDto[]::new);
  495. indexIssues();
  496. SearchWsResponse responseProject1 = newRequest(project1)
  497. .executeProtobuf(SearchWsResponse.class);
  498. assertThat(responseProject1.getHotspotsList())
  499. .extracting(SearchWsResponse.Hotspot::getKey)
  500. .doesNotContainAnyElementsOf(Arrays.stream(hotspots2).map(IssueDto::getKey).collect(toList()));
  501. assertThat(responseProject1.getComponentsList())
  502. .extracting(Component::getKey)
  503. .containsOnly(project1.getKey(), file1.getKey());
  504. SearchWsResponse responseProject2 = newRequest(project2)
  505. .executeProtobuf(SearchWsResponse.class);
  506. assertThat(responseProject2.getHotspotsList())
  507. .extracting(SearchWsResponse.Hotspot::getKey)
  508. .containsOnly(Arrays.stream(hotspots2).map(IssueDto::getKey).toArray(String[]::new));
  509. assertThat(responseProject2.getComponentsList())
  510. .extracting(Component::getKey)
  511. .containsOnly(project2.getKey(), file2.getKey());
  512. }
  513. @Test
  514. public void returns_only_hotspots_to_review_or_reviewed_of_project() {
  515. ProjectData projectData = dbTester.components().insertPublicProject();
  516. ComponentDto project = projectData.getMainBranchComponent();
  517. userSessionRule.registerProjects(projectData.getProjectDto());
  518. indexPermissions();
  519. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  520. IssueDto[] hotspots = STATUSES.stream()
  521. .map(status -> {
  522. RuleDto rule = newRule(SECURITY_HOTSPOT);
  523. return insertHotspot(rule, project, file, t -> t.setStatus(status));
  524. })
  525. .toArray(IssueDto[]::new);
  526. indexIssues();
  527. String[] expectedKeys = Arrays.stream(hotspots)
  528. .filter(t -> STATUS_REVIEWED.equals(t.getStatus()) || STATUS_TO_REVIEW.equals(t.getStatus()))
  529. .map(IssueDto::getKey)
  530. .toArray(String[]::new);
  531. SearchWsResponse response = newRequest(project)
  532. .executeProtobuf(SearchWsResponse.class);
  533. assertThat(response.getHotspotsList())
  534. .extracting(SearchWsResponse.Hotspot::getKey)
  535. .containsOnly(expectedKeys);
  536. }
  537. @Test
  538. public void returns_hotspots_of_specified_application() {
  539. ProjectData application1 = dbTester.components().insertPublicApplication();
  540. ProjectData application2 = dbTester.components().insertPublicApplication();
  541. ProjectData project1 = dbTester.components().insertPublicProject();
  542. ProjectData project2 = dbTester.components().insertPublicProject();
  543. dbTester.components().insertComponent(ComponentTesting.newProjectCopy(project1, application1));
  544. dbTester.components().insertComponent(ComponentTesting.newProjectCopy(project2, application2));
  545. indexViews();
  546. userSessionRule.registerApplication(application1.getProjectDto(), project1.getProjectDto())
  547. .registerApplication(application2.getProjectDto(), project2.getProjectDto());
  548. indexPermissions();
  549. ComponentDto file1 = dbTester.components().insertComponent(newFileDto(project1.getMainBranchComponent()));
  550. ComponentDto file2 = dbTester.components().insertComponent(newFileDto(project2.getMainBranchComponent()));
  551. IssueDto[] hotspots2 = IntStream.range(0, 1 + RANDOM.nextInt(10))
  552. .mapToObj(i -> {
  553. RuleDto rule = newRule(SECURITY_HOTSPOT);
  554. insertHotspot(project1.getMainBranchComponent(), file1, rule);
  555. return insertHotspot(project2.getMainBranchComponent(), file2, rule);
  556. })
  557. .toArray(IssueDto[]::new);
  558. indexIssues();
  559. SearchWsResponse responseApplication1 = newRequest(application1.getMainBranchComponent())
  560. .executeProtobuf(SearchWsResponse.class);
  561. assertThat(responseApplication1.getHotspotsList())
  562. .extracting(SearchWsResponse.Hotspot::getKey)
  563. .doesNotContainAnyElementsOf(Arrays.stream(hotspots2).map(IssueDto::getKey).collect(toList()));
  564. assertThat(responseApplication1.getComponentsList())
  565. .extracting(Component::getKey)
  566. .containsOnly(project1.projectKey(), file1.getKey());
  567. SearchWsResponse responseApplication2 = newRequest(application2.getMainBranchComponent())
  568. .executeProtobuf(SearchWsResponse.class);
  569. assertThat(responseApplication2.getHotspotsList())
  570. .extracting(SearchWsResponse.Hotspot::getKey)
  571. .containsOnly(Arrays.stream(hotspots2).map(IssueDto::getKey).toArray(String[]::new));
  572. assertThat(responseApplication2.getComponentsList())
  573. .extracting(Component::getKey)
  574. .containsOnly(project2.projectKey(), file2.getKey());
  575. }
  576. @Test
  577. public void returns_hotspots_of_specified_application_branch() {
  578. ProjectData applicationData = dbTester.components().insertPublicApplication();
  579. ComponentDto application = applicationData.getMainBranchComponent();
  580. ComponentDto applicationBranch = dbTester.components().insertProjectBranch(application, b -> b.setKey("appBranch"));
  581. ProjectData projectData1 = dbTester.components().insertPublicProject();
  582. ComponentDto project1 = projectData1.getMainBranchComponent();
  583. ProjectData projectData2 = dbTester.components().insertPublicProject();
  584. ComponentDto project2 = projectData2.getMainBranchComponent();
  585. dbTester.components().insertComponent(ComponentTesting.newProjectCopy(project1, application));
  586. dbTester.components().insertComponent(ComponentTesting.newProjectCopy(project2, applicationBranch));
  587. indexViews();
  588. userSessionRule.registerApplication(applicationData.getProjectDto(), projectData1.getProjectDto(), projectData2.getProjectDto());
  589. indexPermissions();
  590. ComponentDto file1 = dbTester.components().insertComponent(newFileDto(project1));
  591. ComponentDto file2 = dbTester.components().insertComponent(newFileDto(project2));
  592. IssueDto[] hotspots2 = IntStream.range(0, 1 + RANDOM.nextInt(10))
  593. .mapToObj(i -> {
  594. RuleDto rule = newRule(SECURITY_HOTSPOT);
  595. insertHotspot(project1, file1, rule);
  596. return insertHotspot(project2, file2, rule);
  597. })
  598. .toArray(IssueDto[]::new);
  599. indexIssues();
  600. SearchWsResponse responseApplication = newRequest(application)
  601. .executeProtobuf(SearchWsResponse.class);
  602. assertThat(responseApplication.getHotspotsList())
  603. .extracting(SearchWsResponse.Hotspot::getKey)
  604. .doesNotContainAnyElementsOf(Arrays.stream(hotspots2).map(IssueDto::getKey).collect(toList()));
  605. assertThat(responseApplication.getComponentsList())
  606. .extracting(Component::getKey)
  607. .containsOnly(project1.getKey(), file1.getKey());
  608. SearchWsResponse responseApplicationBranch = newRequest(application, null, null, "appBranch", null)
  609. .executeProtobuf(SearchWsResponse.class);
  610. assertThat(responseApplicationBranch.getHotspotsList())
  611. .extracting(SearchWsResponse.Hotspot::getKey)
  612. .containsOnly(Arrays.stream(hotspots2).map(IssueDto::getKey).toArray(String[]::new));
  613. assertThat(responseApplicationBranch.getComponentsList())
  614. .extracting(Component::getKey)
  615. .containsOnly(project2.getKey(), file2.getKey());
  616. }
  617. @Test
  618. public void returns_hotspot_of_branch_or_pullRequest() {
  619. ProjectData projectData = dbTester.components().insertPublicProject();
  620. ComponentDto project = projectData.getMainBranchComponent();
  621. userSessionRule.registerProjects(projectData.getProjectDto());
  622. indexPermissions();
  623. ComponentDto branch = dbTester.components().insertProjectBranch(project, b -> b.setKey("branch"));
  624. ComponentDto pullRequest = dbTester.components().insertProjectBranch(project, t -> t.setBranchType(BranchType.PULL_REQUEST).setKey("prKey"));
  625. ComponentDto fileProject = dbTester.components().insertComponent(newFileDto(project));
  626. ComponentDto fileBranch = dbTester.components().insertComponent(newFileDto(branch, project.uuid()));
  627. ComponentDto filePR = dbTester.components().insertComponent(newFileDto(pullRequest, project.uuid()));
  628. IssueDto[] hotspotProject = IntStream.range(0, 1 + RANDOM.nextInt(10))
  629. .mapToObj(i -> {
  630. RuleDto rule = newRule(SECURITY_HOTSPOT);
  631. return insertHotspot(project, fileProject, rule);
  632. })
  633. .toArray(IssueDto[]::new);
  634. IssueDto[] hotspotBranch = IntStream.range(0, 1 + RANDOM.nextInt(10))
  635. .mapToObj(i -> {
  636. RuleDto rule = newRule(SECURITY_HOTSPOT);
  637. return insertHotspot(branch, fileBranch, rule);
  638. })
  639. .toArray(IssueDto[]::new);
  640. IssueDto[] hotspotPR = IntStream.range(0, 1 + RANDOM.nextInt(10))
  641. .mapToObj(i -> {
  642. RuleDto rule = newRule(SECURITY_HOTSPOT);
  643. return insertHotspot(pullRequest, filePR, rule);
  644. })
  645. .toArray(IssueDto[]::new);
  646. indexIssues();
  647. SearchWsResponse responseProject = newRequest(project)
  648. .executeProtobuf(SearchWsResponse.class);
  649. SearchWsResponse responseBranch = newRequest(branch, res -> res.setParam(PARAM_BRANCH, "branch"))
  650. .executeProtobuf(SearchWsResponse.class);
  651. SearchWsResponse responsePR = newRequest(pullRequest, res -> res.setParam(PARAM_PULL_REQUEST, "prKey"))
  652. .executeProtobuf(SearchWsResponse.class);
  653. assertThat(responseProject.getHotspotsList())
  654. .extracting(SearchWsResponse.Hotspot::getKey)
  655. .containsExactlyInAnyOrder(Arrays.stream(hotspotProject).map(IssueDto::getKey).toArray(String[]::new));
  656. assertThat(responseBranch.getHotspotsList())
  657. .extracting(SearchWsResponse.Hotspot::getKey)
  658. .containsExactlyInAnyOrder(Arrays.stream(hotspotBranch).map(IssueDto::getKey).toArray(String[]::new));
  659. assertThat(responsePR.getHotspotsList())
  660. .extracting(SearchWsResponse.Hotspot::getKey)
  661. .containsExactlyInAnyOrder(Arrays.stream(hotspotPR).map(IssueDto::getKey).toArray(String[]::new));
  662. verify(issueIndexSyncProgressChecker, times(3)).checkIfComponentNeedIssueSync(any(), eq(project.getKey()));
  663. }
  664. @Test
  665. @UseDataProvider("onlyMineParamValues")
  666. public void returns_hotspots_of_specified_project_assigned_to_current_user_if_only_mine_is_set(String onlyMineParameter, boolean shouldFilter) {
  667. ProjectData projectData = dbTester.components().insertPublicProject();
  668. ComponentDto project1 = projectData.getMainBranchComponent();
  669. String assigneeUuid = this.userSessionRule.logIn().registerProjects(projectData.getProjectDto()).getUuid();
  670. indexPermissions();
  671. ComponentDto file1 = dbTester.components().insertComponent(newFileDto(project1));
  672. IssueDto[] assigneeHotspots = IntStream.range(0, 1 + RANDOM.nextInt(10))
  673. .mapToObj(i -> {
  674. RuleDto rule = newRule(SECURITY_HOTSPOT);
  675. insertHotspot(rule, project1, file1, randomAlphabetic(5));
  676. return insertHotspot(rule, project1, file1, assigneeUuid);
  677. })
  678. .toArray(IssueDto[]::new);
  679. indexIssues();
  680. SearchWsResponse allHotspots = newRequest(project1)
  681. .executeProtobuf(SearchWsResponse.class);
  682. SearchWsResponse userHotspots = newRequest(project1, r -> r.setParam(PARAM_ONLY_MINE, onlyMineParameter))
  683. .executeProtobuf(SearchWsResponse.class);
  684. assertThat(allHotspots.getHotspotsList())
  685. .extracting(SearchWsResponse.Hotspot::getKey)
  686. .contains(Arrays.stream(assigneeHotspots).map(IssueDto::getKey).toArray(String[]::new))
  687. .hasSizeGreaterThan(assigneeHotspots.length);
  688. if (shouldFilter) {
  689. assertThat(userHotspots.getHotspotsList())
  690. .extracting(SearchWsResponse.Hotspot::getKey)
  691. .containsOnly(Arrays.stream(assigneeHotspots).map(IssueDto::getKey).toArray(String[]::new));
  692. } else {
  693. assertThat(userHotspots.getHotspotsList())
  694. .extracting(SearchWsResponse.Hotspot::getKey)
  695. .containsOnly(allHotspots.getHotspotsList().stream().map(SearchWsResponse.Hotspot::getKey).toArray(String[]::new));
  696. }
  697. }
  698. @DataProvider
  699. public static Object[][] onlyMineParamValues() {
  700. return new Object[][] {
  701. {"yes", true},
  702. {"true", true},
  703. {"no", false},
  704. {"false", false}
  705. };
  706. }
  707. @Test
  708. public void fail_if_hotspots_provided_with_onlyMine_param() {
  709. ProjectData projectData = dbTester.components().insertPrivateProject();
  710. ComponentDto project = projectData.getMainBranchComponent();
  711. userSessionRule.registerProjects(projectData.getProjectDto());
  712. userSessionRule.logIn().addProjectPermission(USER, project);
  713. TestRequest request = actionTester.newRequest()
  714. .setParam(PARAM_HOTSPOTS, IntStream.range(2, 10).mapToObj(String::valueOf).collect(joining(",")))
  715. .setParam(PARAM_ONLY_MINE, "true");
  716. assertThatThrownBy(request::execute)
  717. .isInstanceOf(IllegalArgumentException.class)
  718. .hasMessage("Parameter 'onlyMine' can be used with parameter 'projectKey' only");
  719. }
  720. @Test
  721. public void fail_if_user_not_authenticated_with_onlyMine_param() {
  722. ComponentDto project = dbTester.components().insertPublicProject().getMainBranchComponent();
  723. userSessionRule.anonymous();
  724. TestRequest request = actionTester.newRequest()
  725. .setParam(PARAM_PROJECT_KEY, project.getKey())
  726. .setParam(PARAM_ONLY_MINE, "true");
  727. assertThatThrownBy(request::execute)
  728. .isInstanceOf(IllegalArgumentException.class)
  729. .hasMessage("Parameter 'onlyMine' requires user to be logged in");
  730. }
  731. @Test
  732. public void returns_hotpots_with_any_status_if_no_status_nor_resolution_parameter() {
  733. ProjectData projectData = dbTester.components().insertPublicProject();
  734. ComponentDto project = projectData.getMainBranchComponent();
  735. userSessionRule.registerProjects(projectData.getProjectDto());
  736. indexPermissions();
  737. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  738. List<IssueDto> hotspots = insertRandomNumberOfHotspotsOfAllSupportedStatusesAndResolutions(project, file)
  739. .collect(toList());
  740. indexIssues();
  741. SearchWsResponse response = newRequest(project)
  742. .executeProtobuf(SearchWsResponse.class);
  743. assertThat(response.getHotspotsList())
  744. .extracting(SearchWsResponse.Hotspot::getKey)
  745. .containsExactlyInAnyOrder(hotspots.stream().map(IssueDto::getKey).toArray(String[]::new));
  746. }
  747. @Test
  748. public void returns_hotpots_reviewed_as_safe_and_fixed_if_status_is_REVIEWED_and_resolution_is_not_set() {
  749. ProjectData projectData = dbTester.components().insertPublicProject();
  750. ComponentDto project = projectData.getMainBranchComponent();
  751. userSessionRule.registerProjects(projectData.getProjectDto());
  752. indexPermissions();
  753. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  754. List<IssueDto> reviewedHotspots = insertRandomNumberOfHotspotsOfAllSupportedStatusesAndResolutions(project, file)
  755. .filter(t -> STATUS_REVIEWED.equals(t.getStatus()))
  756. .collect(toList());
  757. indexIssues();
  758. SearchWsResponse response = newRequest(project, STATUS_REVIEWED, null, null, null)
  759. .executeProtobuf(SearchWsResponse.class);
  760. assertThat(response.getHotspotsList())
  761. .extracting(SearchWsResponse.Hotspot::getKey)
  762. .containsExactlyInAnyOrder(reviewedHotspots.stream().map(IssueDto::getKey).toArray(String[]::new));
  763. }
  764. @Test
  765. public void returns_hotpots_reviewed_as_safe_if_status_is_REVIEWED_and_resolution_is_SAFE() {
  766. ProjectData projectData = dbTester.components().insertPublicProject();
  767. ComponentDto project = projectData.getMainBranchComponent();
  768. userSessionRule.registerProjects(projectData.getProjectDto());
  769. indexPermissions();
  770. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  771. List<IssueDto> safeHotspots = insertRandomNumberOfHotspotsOfAllSupportedStatusesAndResolutions(project, file)
  772. .filter(t -> STATUS_REVIEWED.equals(t.getStatus()) && RESOLUTION_SAFE.equals(t.getResolution()))
  773. .collect(toList());
  774. indexIssues();
  775. SearchWsResponse response = newRequest(project, STATUS_REVIEWED, RESOLUTION_SAFE, null, null)
  776. .executeProtobuf(SearchWsResponse.class);
  777. assertThat(response.getHotspotsList())
  778. .extracting(SearchWsResponse.Hotspot::getKey)
  779. .containsExactlyInAnyOrder(safeHotspots.stream().map(IssueDto::getKey).toArray(String[]::new));
  780. }
  781. @Test
  782. public void returns_hotpots_reviewed_as_fixed_if_status_is_REVIEWED_and_resolution_is_FIXED() {
  783. ProjectData projectData = dbTester.components().insertPublicProject();
  784. ComponentDto project = projectData.getMainBranchComponent();
  785. userSessionRule.registerProjects(projectData.getProjectDto());
  786. indexPermissions();
  787. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  788. List<IssueDto> fixedHotspots = insertRandomNumberOfHotspotsOfAllSupportedStatusesAndResolutions(project, file)
  789. .filter(t -> STATUS_REVIEWED.equals(t.getStatus()) && RESOLUTION_FIXED.equals(t.getResolution()))
  790. .collect(toList());
  791. indexIssues();
  792. SearchWsResponse response = newRequest(project, STATUS_REVIEWED, RESOLUTION_FIXED, null, null)
  793. .executeProtobuf(SearchWsResponse.class);
  794. assertThat(response.getHotspotsList())
  795. .extracting(SearchWsResponse.Hotspot::getKey)
  796. .containsExactlyInAnyOrder(fixedHotspots.stream().map(IssueDto::getKey).toArray(String[]::new));
  797. }
  798. @Test
  799. public void returns_only_unresolved_hotspots_when_status_is_TO_REVIEW() {
  800. ProjectData projectData = dbTester.components().insertPublicProject();
  801. ComponentDto project = projectData.getMainBranchComponent();
  802. userSessionRule.registerProjects(projectData.getProjectDto());
  803. indexPermissions();
  804. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  805. RuleDto rule = newRule(SECURITY_HOTSPOT);
  806. IssueDto unresolvedHotspot = insertHotspot(rule, project, file, t -> t.setResolution(null));
  807. // unrealistic case since a resolution must be set, but shows a limit of current implementation (resolution is enough)
  808. IssueDto badlyResolved = insertHotspot(rule, project, file, t -> t.setStatus(STATUS_TO_REVIEW).setResolution(randomAlphabetic(5)));
  809. IssueDto badlyReviewed = insertHotspot(rule, project, file, t -> t.setStatus(STATUS_REVIEWED).setResolution(null));
  810. IssueDto badlyClosedHotspot = insertHotspot(rule, project, file, t -> t.setStatus(STATUS_CLOSED).setResolution(null));
  811. indexIssues();
  812. SearchWsResponse response = newRequest(project, STATUS_TO_REVIEW, null, null, null)
  813. .executeProtobuf(SearchWsResponse.class);
  814. assertThat(response.getHotspotsList())
  815. .extracting(SearchWsResponse.Hotspot::getKey)
  816. .containsOnly(unresolvedHotspot.getKey());
  817. }
  818. private Stream<IssueDto> insertRandomNumberOfHotspotsOfAllSupportedStatusesAndResolutions(ComponentDto project, ComponentDto file) {
  819. RuleDto rule = newRule(SECURITY_HOTSPOT);
  820. List<IssueDto> hotspots = Arrays.stream(validStatusesAndResolutions())
  821. .flatMap(objects -> {
  822. String status = (String) objects[0];
  823. String resolution = (String) objects[1];
  824. return IntStream.range(0, 1 + RANDOM.nextInt(15))
  825. .mapToObj(i -> newIssue(rule, project, file)
  826. .setKee("hotspot_" + status + "_" + resolution + "_" + i)
  827. .setType(SECURITY_HOTSPOT)
  828. .setStatus(status)
  829. .setResolution(resolution));
  830. })
  831. .collect(toList());
  832. Collections.shuffle(hotspots);
  833. hotspots.forEach(t -> dbTester.issues().insertHotspot(t));
  834. return hotspots.stream();
  835. }
  836. @Test
  837. @UseDataProvider("validStatusesAndResolutions")
  838. public void returns_fields_of_hotspot(String status, @Nullable String resolution) {
  839. ProjectData projectData = dbTester.components().insertPublicProject();
  840. ComponentDto project = projectData.getMainBranchComponent();
  841. userSessionRule.registerProjects(projectData.getProjectDto());
  842. indexPermissions();
  843. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  844. RuleDto rule = newRule(SECURITY_HOTSPOT);
  845. IssueDto hotspot = insertHotspot(rule, project, file,
  846. t -> t
  847. .setStatus(randomAlphabetic(11))
  848. .setLine(RANDOM.nextInt(230))
  849. .setMessage(randomAlphabetic(10))
  850. .setAssigneeUuid(randomAlphabetic(9))
  851. .setAuthorLogin(randomAlphabetic(8))
  852. .setStatus(status)
  853. .setResolution(resolution));
  854. indexIssues();
  855. SearchWsResponse response = newRequest(project)
  856. .executeProtobuf(SearchWsResponse.class);
  857. assertThat(response.getHotspotsList()).hasSize(1);
  858. SearchWsResponse.Hotspot actual = response.getHotspots(0);
  859. assertThat(actual.getComponent()).isEqualTo(file.getKey());
  860. assertThat(actual.getProject()).isEqualTo(project.getKey());
  861. assertThat(actual.getStatus()).isEqualTo(status);
  862. if (resolution == null) {
  863. assertThat(actual.hasResolution()).isFalse();
  864. } else {
  865. assertThat(actual.getResolution()).isEqualTo(resolution);
  866. }
  867. assertThat(actual.getLine()).isEqualTo(hotspot.getLine());
  868. assertThat(actual.getMessage()).isEqualTo(hotspot.getMessage());
  869. assertThat(actual.getAssignee()).isEqualTo(hotspot.getAssigneeUuid());
  870. assertThat(actual.getAuthor()).isEqualTo(hotspot.getAuthorLogin());
  871. assertThat(actual.getCreationDate()).isEqualTo(formatDateTime(hotspot.getIssueCreationDate()));
  872. assertThat(actual.getUpdateDate()).isEqualTo(formatDateTime(hotspot.getIssueUpdateDate()));
  873. }
  874. @DataProvider
  875. public static Object[][] validStatusesAndResolutions() {
  876. return new Object[][] {
  877. {STATUS_TO_REVIEW, null},
  878. {STATUS_REVIEWED, RESOLUTION_FIXED},
  879. {STATUS_REVIEWED, RESOLUTION_SAFE},
  880. };
  881. }
  882. @Test
  883. @UseDataProvider("allSQCategories")
  884. public void returns_SQCategory_and_VulnerabilityProbability_of_rule(Set<String> securityStandards, SQCategory expected) {
  885. ProjectData projectData = dbTester.components().insertPublicProject();
  886. ComponentDto project = projectData.getMainBranchComponent();
  887. userSessionRule.registerProjects(projectData.getProjectDto());
  888. indexPermissions();
  889. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  890. RuleDto rule = newRule(SECURITY_HOTSPOT, t -> t.setSecurityStandards(securityStandards));
  891. IssueDto hotspot = insertHotspot(project, file, rule);
  892. indexIssues();
  893. SearchWsResponse response = newRequest(project)
  894. .executeProtobuf(SearchWsResponse.class);
  895. assertThat(response.getHotspotsList()).hasSize(1);
  896. SearchWsResponse.Hotspot actual = response.getHotspots(0);
  897. assertThat(actual.getSecurityCategory()).isEqualTo(expected.getKey());
  898. assertThat(actual.getVulnerabilityProbability()).isEqualTo(expected.getVulnerability().name());
  899. }
  900. @DataProvider
  901. public static Object[][] allSQCategories() {
  902. Stream<Object[]> allCategoriesButOTHERS = SecurityStandards.CWES_BY_SQ_CATEGORY.entrySet()
  903. .stream()
  904. .map(t -> new Object[] {
  905. t.getValue().stream().map(c -> "cwe:" + c).collect(toSet()),
  906. t.getKey()
  907. });
  908. Stream<Object[]> sqCategoryOTHERS = Stream.of(
  909. new Object[] {Collections.emptySet(), SQCategory.OTHERS},
  910. new Object[] {of("foo", "donut", "acme"), SQCategory.OTHERS});
  911. return Stream.concat(allCategoriesButOTHERS, sqCategoryOTHERS).toArray(Object[][]::new);
  912. }
  913. @Test
  914. public void does_not_fail_when_hotspot_has_none_of_the_nullable_fields() {
  915. ProjectData projectData = dbTester.components().insertPublicProject();
  916. ComponentDto project = projectData.getMainBranchComponent();
  917. userSessionRule.registerProjects(projectData.getProjectDto());
  918. indexPermissions();
  919. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  920. RuleDto rule = newRule(SECURITY_HOTSPOT);
  921. insertHotspot(rule, project, file,
  922. t -> t.setResolution(null)
  923. .setLine(null)
  924. .setMessage(null)
  925. .setAssigneeUuid(null)
  926. .setAuthorLogin(null));
  927. indexIssues();
  928. SearchWsResponse response = newRequest(project)
  929. .executeProtobuf(SearchWsResponse.class);
  930. assertThat(response.getHotspotsList())
  931. .hasSize(1);
  932. SearchWsResponse.Hotspot actual = response.getHotspots(0);
  933. assertThat(actual.hasResolution()).isFalse();
  934. assertThat(actual.hasLine()).isFalse();
  935. assertThat(actual.getMessage()).isEmpty();
  936. assertThat(actual.hasAssignee()).isFalse();
  937. assertThat(actual.getAuthor()).isEmpty();
  938. }
  939. @Test
  940. public void returns_details_of_components() {
  941. ProjectData projectData = dbTester.components().insertPublicProject();
  942. ComponentDto project = projectData.getMainBranchComponent();
  943. userSessionRule.registerProjects(projectData.getProjectDto());
  944. indexPermissions();
  945. ComponentDto directory = dbTester.components().insertComponent(newDirectory(project, "donut/acme"));
  946. ComponentDto directory2 = dbTester.components().insertComponent(newDirectory(project, "foo/bar"));
  947. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  948. ComponentDto file2 = dbTester.components().insertComponent(newFileDto(project));
  949. RuleDto rule = newRule(SECURITY_HOTSPOT);
  950. IssueDto fileHotspot = insertHotspot(project, file, rule);
  951. IssueDto dirHotspot = insertHotspot(project, directory, rule);
  952. IssueDto projectHotspot = insertHotspot(project, project, rule);
  953. indexIssues();
  954. SearchWsResponse response = newRequest(project)
  955. .executeProtobuf(SearchWsResponse.class);
  956. assertThat(response.getHotspotsList())
  957. .extracting(SearchWsResponse.Hotspot::getKey)
  958. .containsOnly(fileHotspot.getKey(), dirHotspot.getKey(), projectHotspot.getKey());
  959. assertThat(response.getComponentsList()).hasSize(3);
  960. assertThat(response.getComponentsList())
  961. .extracting(Component::getKey)
  962. .containsOnly(project.getKey(), directory.getKey(), file.getKey());
  963. Map<String, Component> componentByKey = response.getComponentsList().stream().collect(Collectors.toMap(Component::getKey, Function.identity()));
  964. Component actualProject = componentByKey.get(project.getKey());
  965. assertThat(actualProject.getQualifier()).isEqualTo(project.qualifier());
  966. assertThat(actualProject.getName()).isEqualTo(project.name());
  967. assertThat(actualProject.getLongName()).isEqualTo(project.longName());
  968. assertThat(actualProject.hasPath()).isFalse();
  969. assertThat(actualProject.hasBranch()).isFalse();
  970. assertThat(actualProject.hasPullRequest()).isFalse();
  971. Component actualDirectory = componentByKey.get(directory.getKey());
  972. assertThat(actualDirectory.getQualifier()).isEqualTo(directory.qualifier());
  973. assertThat(actualDirectory.getName()).isEqualTo(directory.name());
  974. assertThat(actualDirectory.getLongName()).isEqualTo(directory.longName());
  975. assertThat(actualDirectory.getPath()).isEqualTo(directory.path());
  976. assertThat(actualDirectory.hasBranch()).isFalse();
  977. assertThat(actualDirectory.hasPullRequest()).isFalse();
  978. Component actualFile = componentByKey.get(file.getKey());
  979. assertThat(actualFile.getQualifier()).isEqualTo(file.qualifier());
  980. assertThat(actualFile.getName()).isEqualTo(file.name());
  981. assertThat(actualFile.getLongName()).isEqualTo(file.longName());
  982. assertThat(actualFile.getPath()).isEqualTo(file.path());
  983. assertThat(actualFile.hasBranch()).isFalse();
  984. assertThat(actualFile.hasPullRequest()).isFalse();
  985. }
  986. @Test
  987. public void returns_branch_field_of_components_of_branch() {
  988. ProjectData projectData = dbTester.components().insertPublicProject();
  989. ComponentDto project = projectData.getMainBranchComponent();
  990. String branchName = randomAlphanumeric(248);
  991. ComponentDto branch = dbTester.components().insertProjectBranch(project, b -> b.setKey(branchName));
  992. userSessionRule.registerProjects(projectData.getProjectDto());
  993. indexPermissions();
  994. ComponentDto directory = dbTester.components().insertComponent(newDirectoryOnBranch(branch, "donut/acme", project.uuid()));
  995. ComponentDto file = dbTester.components().insertComponent(newFileDto(branch, project.uuid()));
  996. RuleDto rule = newRule(SECURITY_HOTSPOT);
  997. IssueDto fileHotspot = insertHotspot(branch, file, rule);
  998. IssueDto dirHotspot = insertHotspot(branch, directory, rule);
  999. IssueDto projectHotspot = insertHotspot(branch, branch, rule);
  1000. indexIssues();
  1001. SearchWsResponse response = newRequest(branch, r -> r.setParam(PARAM_BRANCH, branchName))
  1002. .executeProtobuf(SearchWsResponse.class);
  1003. assertThat(response.getHotspotsList())
  1004. .extracting(SearchWsResponse.Hotspot::getKey)
  1005. .containsOnly(fileHotspot.getKey(), dirHotspot.getKey(), projectHotspot.getKey());
  1006. assertThat(response.getComponentsList())
  1007. .extracting(Component::getKey)
  1008. .containsOnly(project.getKey(), directory.getKey(), file.getKey());
  1009. Map<String, Component> componentByKey = response.getComponentsList().stream().collect(Collectors.toMap(Component::getKey, Function.identity()));
  1010. Component actualProject = componentByKey.get(project.getKey());
  1011. assertThat(actualProject.getBranch()).isEqualTo(branchName);
  1012. assertThat(actualProject.hasPullRequest()).isFalse();
  1013. Component actualDirectory = componentByKey.get(directory.getKey());
  1014. assertThat(actualDirectory.getBranch()).isEqualTo(branchName);
  1015. assertThat(actualDirectory.hasPullRequest()).isFalse();
  1016. Component actualFile = componentByKey.get(file.getKey());
  1017. assertThat(actualFile.getBranch()).isEqualTo(branchName);
  1018. assertThat(actualFile.hasPullRequest()).isFalse();
  1019. }
  1020. @Test
  1021. public void returns_pullRequest_field_of_components_of_pullRequest() {
  1022. ProjectData projectData = dbTester.components().insertPublicProject();
  1023. ComponentDto project = projectData.getMainBranchComponent();
  1024. String pullRequestKey = randomAlphanumeric(100);
  1025. ComponentDto pullRequest = dbTester.components().insertProjectBranch(project, t -> t.setBranchType(BranchType.PULL_REQUEST)
  1026. .setKey(pullRequestKey));
  1027. userSessionRule.registerProjects(projectData.getProjectDto());
  1028. indexPermissions();
  1029. ComponentDto directory = dbTester.components().insertComponent(newDirectoryOnBranch(pullRequest, "donut/acme", project.uuid()));
  1030. ComponentDto file = dbTester.components().insertComponent(newFileDto(pullRequest, project.uuid()));
  1031. RuleDto rule = newRule(SECURITY_HOTSPOT);
  1032. IssueDto fileHotspot = insertHotspot(pullRequest, file, rule);
  1033. IssueDto dirHotspot = insertHotspot(pullRequest, directory, rule);
  1034. IssueDto projectHotspot = insertHotspot(pullRequest, pullRequest, rule);
  1035. indexIssues();
  1036. SearchWsResponse response = newRequest(pullRequest, r -> r.setParam(PARAM_PULL_REQUEST, pullRequestKey))
  1037. .executeProtobuf(SearchWsResponse.class);
  1038. assertThat(response.getHotspotsList())
  1039. .extracting(SearchWsResponse.Hotspot::getKey)
  1040. .containsOnly(fileHotspot.getKey(), dirHotspot.getKey(), projectHotspot.getKey());
  1041. assertThat(response.getComponentsList())
  1042. .extracting(Component::getKey)
  1043. .containsOnly(project.getKey(), directory.getKey(), file.getKey());
  1044. Map<String, Component> componentByKey = response.getComponentsList().stream().collect(Collectors.toMap(Component::getKey, Function.identity()));
  1045. Component actualProject = componentByKey.get(project.getKey());
  1046. assertThat(actualProject.hasBranch()).isFalse();
  1047. assertThat(actualProject.getPullRequest()).isEqualTo(pullRequestKey);
  1048. Component actualDirectory = componentByKey.get(directory.getKey());
  1049. assertThat(actualDirectory.hasBranch()).isFalse();
  1050. assertThat(actualDirectory.getPullRequest()).isEqualTo(pullRequestKey);
  1051. Component actualFile = componentByKey.get(file.getKey());
  1052. assertThat(actualFile.hasBranch()).isFalse();
  1053. assertThat(actualFile.getPullRequest()).isEqualTo(pullRequestKey);
  1054. }
  1055. @Test
  1056. public void returns_hotspots_ordered_by_vulnerabilityProbability_score_then_rule_uuid() {
  1057. ProjectData projectData = dbTester.components().insertPublicProject();
  1058. ComponentDto project = projectData.getMainBranchComponent();
  1059. userSessionRule.registerProjects(projectData.getProjectDto());
  1060. indexPermissions();
  1061. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  1062. List<IssueDto> hotspots = Arrays.stream(SQCategory.values())
  1063. .sorted(Ordering.from(Comparator.<SQCategory>comparingInt(t1 -> t1.getVulnerability().getScore()).reversed())
  1064. .thenComparing(SQCategory::getKey))
  1065. .flatMap(sqCategory -> {
  1066. Set<String> cwes = SecurityStandards.CWES_BY_SQ_CATEGORY.get(sqCategory);
  1067. Set<String> securityStandards = singleton("cwe:" + (cwes == null ? "unknown" : cwes.iterator().next()));
  1068. RuleDto rule1 = newRule(
  1069. SECURITY_HOTSPOT,
  1070. t -> t.setUuid(sqCategory.name() + "_a").setName("rule_" + sqCategory.name() + "_a").setSecurityStandards(securityStandards));
  1071. RuleDto rule2 = newRule(
  1072. SECURITY_HOTSPOT,
  1073. t -> t.setUuid(sqCategory.name() + "_b").setName("rule_" + sqCategory.name() + "_b").setSecurityStandards(securityStandards));
  1074. return Stream.of(
  1075. newHotspot(rule1, project, file).setKee(sqCategory + "_a"),
  1076. newHotspot(rule2, project, file).setKee(sqCategory + "_b"));
  1077. })
  1078. .collect(toList());
  1079. String[] expectedHotspotKeys = hotspots.stream().map(IssueDto::getKey).toArray(String[]::new);
  1080. // insert hotspots in random order
  1081. Collections.shuffle(hotspots);
  1082. hotspots.forEach(dbTester.issues()::insertHotspot);
  1083. indexIssues();
  1084. SearchWsResponse response = newRequest(project)
  1085. .executeProtobuf(SearchWsResponse.class);
  1086. assertThat(response.getHotspotsList())
  1087. .extracting(SearchWsResponse.Hotspot::getKey)
  1088. .containsExactly(expectedHotspotKeys);
  1089. }
  1090. @Test
  1091. public void returns_hotspots_ordered_by_file_path_then_line_then_key() {
  1092. ProjectData projectData = dbTester.components().insertPublicProject();
  1093. ComponentDto project = projectData.getMainBranchComponent();
  1094. userSessionRule.registerProjects(projectData.getProjectDto());
  1095. indexPermissions();
  1096. ComponentDto file1 = dbTester.components().insertComponent(newFileDto(project).setPath("b/c/a"));
  1097. ComponentDto file2 = dbTester.components().insertComponent(newFileDto(project).setPath("b/c/b"));
  1098. ComponentDto file3 = dbTester.components().insertComponent(newFileDto(project).setPath("a/a/d"));
  1099. RuleDto rule = newRule(SECURITY_HOTSPOT);
  1100. List<IssueDto> hotspots = Stream.of(
  1101. newHotspot(rule, project, file3).setLine(8),
  1102. newHotspot(rule, project, file3).setLine(10),
  1103. newHotspot(rule, project, file1).setLine(null),
  1104. newHotspot(rule, project, file1).setLine(9),
  1105. newHotspot(rule, project, file1).setLine(11).setKee("a"),
  1106. newHotspot(rule, project, file1).setLine(11).setKee("b"),
  1107. newHotspot(rule, project, file2).setLine(null),
  1108. newHotspot(rule, project, file2).setLine(2))
  1109. .collect(toList());
  1110. String[] expectedHotspotKeys = hotspots.stream().map(IssueDto::getKey).toArray(String[]::new);
  1111. // insert hotspots in random order
  1112. Collections.shuffle(hotspots);
  1113. hotspots.forEach(dbTester.issues()::insertHotspot);
  1114. indexIssues();
  1115. SearchWsResponse response = newRequest(project)
  1116. .executeProtobuf(SearchWsResponse.class);
  1117. assertThat(response.getHotspotsList())
  1118. .extracting(SearchWsResponse.Hotspot::getKey)
  1119. .containsExactly(expectedHotspotKeys);
  1120. }
  1121. @Test
  1122. public void returns_hotspot_with_secondary_locations() {
  1123. ProjectData projectData = dbTester.components().insertPublicProject();
  1124. ComponentDto project = projectData.getMainBranchComponent();
  1125. userSessionRule.registerProjects(projectData.getProjectDto());
  1126. indexPermissions();
  1127. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  1128. ComponentDto anotherFile = dbTester.components().insertComponent(newFileDto(project));
  1129. List<DbIssues.Location> hotspotLocations = Stream.of(
  1130. newHotspotLocation(file.uuid(), "security hotspot flow message 0", 1, 1, 0, 12),
  1131. newHotspotLocation(file.uuid(), "security hotspot flow message 1", 3, 3, 0, 10),
  1132. newHotspotLocation(anotherFile.uuid(), "security hotspot flow message 2", 5, 5, 0, 15),
  1133. newHotspotLocation(anotherFile.uuid(), "security hotspot flow message 3", 7, 7, 0, 18),
  1134. newHotspotLocation(null, "security hotspot flow message 4", 12, 12, 2, 8))
  1135. .collect(toList());
  1136. DbIssues.Locations.Builder locations = DbIssues.Locations.newBuilder().addFlow(DbIssues.Flow.newBuilder().addAllLocation(hotspotLocations));
  1137. RuleDto rule = newRule(SECURITY_HOTSPOT);
  1138. dbTester.issues().insertHotspot(rule, project, file, h -> h.setLocations(locations.build()));
  1139. indexIssues();
  1140. SearchWsResponse response = newRequest(project)
  1141. .executeProtobuf(SearchWsResponse.class);
  1142. assertThat(response.getHotspotsCount()).isOne();
  1143. assertThat(response.getHotspotsList().stream().findFirst().get().getFlowsCount()).isEqualTo(1);
  1144. assertThat(response.getHotspotsList().stream().findFirst().get().getFlowsList().stream().findFirst().get().getLocationsCount()).isEqualTo(5);
  1145. assertThat(response.getHotspotsList().stream().findFirst().get().getFlowsList().stream().findFirst().get().getLocationsList())
  1146. .extracting(
  1147. Common.Location::getComponent,
  1148. Common.Location::getMsg,
  1149. l -> l.getTextRange().getStartLine(),
  1150. l -> l.getTextRange().getEndLine(),
  1151. l -> l.getTextRange().getStartOffset(),
  1152. l -> l.getTextRange().getEndOffset())
  1153. .containsExactlyInAnyOrder(
  1154. tuple(file.getKey(), "security hotspot flow message 0", 1, 1, 0, 12),
  1155. tuple(file.getKey(), "security hotspot flow message 1", 3, 3, 0, 10),
  1156. tuple(anotherFile.getKey(), "security hotspot flow message 2", 5, 5, 0, 15),
  1157. tuple(anotherFile.getKey(), "security hotspot flow message 3", 7, 7, 0, 18),
  1158. tuple(file.getKey(), "security hotspot flow message 4", 12, 12, 2, 8));
  1159. }
  1160. @Test
  1161. public void returns_first_page_with_100_results_by_default() {
  1162. ProjectData projectData = dbTester.components().insertPublicProject();
  1163. ComponentDto project = projectData.getMainBranchComponent();
  1164. userSessionRule.registerProjects(projectData.getProjectDto());
  1165. indexPermissions();
  1166. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  1167. RuleDto rule = newRule(SECURITY_HOTSPOT);
  1168. int total = 436;
  1169. List<IssueDto> hotspots = IntStream.range(0, total)
  1170. .mapToObj(i -> dbTester.issues().insertHotspot(rule, project, file, t -> t.setLine(i)))
  1171. .collect(toList());
  1172. indexIssues();
  1173. TestRequest request = newRequest(project);
  1174. SearchWsResponse response = request.executeProtobuf(SearchWsResponse.class);
  1175. assertThat(response.getHotspotsList())
  1176. .extracting(SearchWsResponse.Hotspot::getKey)
  1177. .containsExactly(hotspots.stream().limit(100).map(IssueDto::getKey).toArray(String[]::new));
  1178. assertThat(response.getPaging().getTotal()).isEqualTo(hotspots.size());
  1179. assertThat(response.getPaging().getPageIndex()).isOne();
  1180. assertThat(response.getPaging().getPageSize()).isEqualTo(100);
  1181. }
  1182. @Test
  1183. public void returns_specified_page_with_100_results_by_default() {
  1184. ProjectData projectData = dbTester.components().insertPublicProject();
  1185. ComponentDto project = projectData.getMainBranchComponent();
  1186. userSessionRule.registerProjects(projectData.getProjectDto());
  1187. indexPermissions();
  1188. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  1189. RuleDto rule = newRule(SECURITY_HOTSPOT);
  1190. verifyPaging(project, file, rule, 336, 100);
  1191. }
  1192. @Test
  1193. public void returns_specified_page_with_specified_number_of_results() {
  1194. ProjectData projectData = dbTester.components().insertPublicProject();
  1195. ComponentDto project = projectData.getMainBranchComponent();
  1196. userSessionRule.registerProjects(projectData.getProjectDto());
  1197. indexPermissions();
  1198. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  1199. RuleDto rule = newRule(SECURITY_HOTSPOT);
  1200. int total = 336;
  1201. int pageSize = 1 + new Random().nextInt(100);
  1202. verifyPaging(project, file, rule, total, pageSize);
  1203. }
  1204. private void verifyPaging(ComponentDto project, ComponentDto file, RuleDto rule, int total, int pageSize) {
  1205. List<IssueDto> hotspots = IntStream.range(0, total)
  1206. .mapToObj(i -> dbTester.issues().insertHotspot(rule, project, file, t -> t.setLine(i).setKee("issue_" + i)))
  1207. .collect(toList());
  1208. indexIssues();
  1209. SearchWsResponse response = newRequest(project)
  1210. .setParam("p", "3")
  1211. .setParam("ps", String.valueOf(pageSize))
  1212. .executeProtobuf(SearchWsResponse.class);
  1213. assertThat(response.getHotspotsList())
  1214. .extracting(SearchWsResponse.Hotspot::getKey)
  1215. .containsExactly(hotspots.stream().skip(2 * pageSize).limit(pageSize).map(IssueDto::getKey).toArray(String[]::new));
  1216. assertThat(response.getPaging().getTotal()).isEqualTo(hotspots.size());
  1217. assertThat(response.getPaging().getPageIndex()).isEqualTo(3);
  1218. assertThat(response.getPaging().getPageSize()).isEqualTo(pageSize);
  1219. response = newRequest(project)
  1220. .setParam("p", "4")
  1221. .setParam("ps", String.valueOf(pageSize))
  1222. .executeProtobuf(SearchWsResponse.class);
  1223. assertThat(response.getHotspotsList())
  1224. .extracting(SearchWsResponse.Hotspot::getKey)
  1225. .containsExactly(hotspots.stream().skip(3 * pageSize).limit(pageSize).map(IssueDto::getKey).toArray(String[]::new));
  1226. assertThat(response.getPaging().getTotal()).isEqualTo(hotspots.size());
  1227. assertThat(response.getPaging().getPageIndex()).isEqualTo(4);
  1228. assertThat(response.getPaging().getPageSize()).isEqualTo(pageSize);
  1229. int emptyPage = (hotspots.size() / pageSize) + 10;
  1230. response = newRequest(project)
  1231. .setParam("p", String.valueOf(emptyPage))
  1232. .setParam("ps", String.valueOf(pageSize))
  1233. .executeProtobuf(SearchWsResponse.class);
  1234. assertThat(response.getHotspotsList())
  1235. .extracting(SearchWsResponse.Hotspot::getKey)
  1236. .isEmpty();
  1237. assertThat(response.getPaging().getTotal()).isEqualTo(hotspots.size());
  1238. assertThat(response.getPaging().getPageIndex()).isEqualTo(emptyPage);
  1239. assertThat(response.getPaging().getPageSize()).isEqualTo(pageSize);
  1240. }
  1241. @Test
  1242. public void returns_empty_if_none_of_hotspot_keys_exist() {
  1243. ProjectData projectData = dbTester.components().insertPublicProject();
  1244. ComponentDto project = projectData.getMainBranchComponent();
  1245. userSessionRule.registerProjects(projectData.getProjectDto());
  1246. indexPermissions();
  1247. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  1248. RuleDto rule = newRule(SECURITY_HOTSPOT);
  1249. List<IssueDto> hotspots = IntStream.range(0, 1 + RANDOM.nextInt(15))
  1250. .mapToObj(i -> dbTester.issues().insertHotspot(rule, project, file, t -> t.setLine(i)))
  1251. .collect(toList());
  1252. indexIssues();
  1253. SearchWsResponse response = newRequest(IntStream.range(0, 1 + RANDOM.nextInt(30)).mapToObj(i -> "key_" + i).collect(toList()))
  1254. .executeProtobuf(SearchWsResponse.class);
  1255. verify(issueIndexSyncProgressChecker).checkIfIssueSyncInProgress(any());
  1256. assertThat(response.getHotspotsList()).isEmpty();
  1257. }
  1258. @Test
  1259. public void returns_specified_hotspots() {
  1260. ProjectData projectData = dbTester.components().insertPublicProject();
  1261. ComponentDto project = projectData.getMainBranchComponent();
  1262. userSessionRule.registerProjects(projectData.getProjectDto());
  1263. indexPermissions();
  1264. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  1265. RuleDto rule = newRule(SECURITY_HOTSPOT);
  1266. int total = 1 + RANDOM.nextInt(20);
  1267. List<IssueDto> hotspots = IntStream.range(0, total)
  1268. .mapToObj(i -> dbTester.issues().insertHotspot(rule, project, file, t -> t.setLine(i)))
  1269. .collect(toList());
  1270. Collections.shuffle(hotspots);
  1271. List<IssueDto> selectedHotspots = hotspots.stream().limit(total == 1 ? 1 : 1 + RANDOM.nextInt(total - 1)).collect(toList());
  1272. indexIssues();
  1273. SearchWsResponse response = newRequest(selectedHotspots.stream().map(IssueDto::getKey).collect(toList()))
  1274. .executeProtobuf(SearchWsResponse.class);
  1275. assertThat(response.getHotspotsList())
  1276. .extracting(SearchWsResponse.Hotspot::getKey)
  1277. .containsExactlyInAnyOrder(selectedHotspots.stream().map(IssueDto::getKey).toArray(String[]::new));
  1278. }
  1279. @Test
  1280. public void returns_hotspots_with_specified_sonarsourceSecurity_category() {
  1281. ProjectData projectData = dbTester.components().insertPublicProject();
  1282. ComponentDto project = projectData.getMainBranchComponent();
  1283. userSessionRule.registerProjects(projectData.getProjectDto());
  1284. indexPermissions();
  1285. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  1286. RuleDto rule1 = newRule(SECURITY_HOTSPOT);
  1287. RuleDto rule2 = newRule(SECURITY_HOTSPOT, r -> r.setSecurityStandards(of("cwe:117", "cwe:190")));
  1288. RuleDto rule3 = newRule(SECURITY_HOTSPOT, r -> r.setSecurityStandards(of("owaspTop10:a1", "cwe:601")));
  1289. insertHotspot(project, file, rule1);
  1290. IssueDto hotspot2 = insertHotspot(project, file, rule2);
  1291. insertHotspot(project, file, rule3);
  1292. indexIssues();
  1293. SearchWsResponse response = newRequest(project).setParam(PARAM_SONARSOURCE_SECURITY, "log-injection")
  1294. .executeProtobuf(SearchWsResponse.class);
  1295. assertThat(response.getHotspotsList())
  1296. .extracting(SearchWsResponse.Hotspot::getKey)
  1297. .containsExactly(hotspot2.getKey());
  1298. }
  1299. @Test
  1300. public void returns_hotspots_with_specified_cwes() {
  1301. ProjectData projectData = dbTester.components().insertPublicProject();
  1302. ComponentDto project = projectData.getMainBranchComponent();
  1303. userSessionRule.registerProjects(projectData.getProjectDto());
  1304. indexPermissions();
  1305. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  1306. RuleDto rule1 = newRule(SECURITY_HOTSPOT);
  1307. RuleDto rule2 = newRule(SECURITY_HOTSPOT, r -> r.setSecurityStandards(of("cwe:117", "cwe:190")));
  1308. RuleDto rule3 = newRule(SECURITY_HOTSPOT, r -> r.setSecurityStandards(of("owaspTop10:a1", "cwe:601")));
  1309. insertHotspot(project, file, rule1);
  1310. IssueDto hotspot2 = insertHotspot(project, file, rule2);
  1311. insertHotspot(project, file, rule3);
  1312. indexIssues();
  1313. SearchWsResponse response = newRequest(project).setParam(PARAM_CWE, "117,190")
  1314. .executeProtobuf(SearchWsResponse.class);
  1315. assertThat(response.getHotspotsList())
  1316. .extracting(SearchWsResponse.Hotspot::getKey)
  1317. .containsExactly(hotspot2.getKey());
  1318. }
  1319. @Test
  1320. public void returns_hotspots_with_specified_owaspTop10_category() {
  1321. ProjectData projectData = dbTester.components().insertPublicProject();
  1322. ComponentDto project = projectData.getMainBranchComponent();
  1323. userSessionRule.registerProjects(projectData.getProjectDto());
  1324. indexPermissions();
  1325. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  1326. RuleDto rule1 = newRule(SECURITY_HOTSPOT);
  1327. RuleDto rule2 = newRule(SECURITY_HOTSPOT, r -> r.setSecurityStandards(of("cwe:117", "cwe:190")));
  1328. RuleDto rule3 = newRule(SECURITY_HOTSPOT, r -> r.setSecurityStandards(of("owaspTop10:a1", "cwe:601")));
  1329. insertHotspot(project, file, rule1);
  1330. insertHotspot(project, file, rule2);
  1331. IssueDto hotspot3 = insertHotspot(project, file, rule3);
  1332. indexIssues();
  1333. SearchWsResponse response = newRequest(project).setParam(PARAM_OWASP_TOP_10_2017, "a1")
  1334. .executeProtobuf(SearchWsResponse.class);
  1335. assertThat(response.getHotspotsList())
  1336. .extracting(SearchWsResponse.Hotspot::getKey)
  1337. .containsExactly(hotspot3.getKey());
  1338. }
  1339. @Test
  1340. public void returns_hotspots_with_specified_pciDss_category() {
  1341. ProjectData projectData = dbTester.components().insertPublicProject();
  1342. ComponentDto project = projectData.getMainBranchComponent();
  1343. userSessionRule.registerProjects(projectData.getProjectDto());
  1344. indexPermissions();
  1345. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  1346. RuleDto rule1 = newRule(SECURITY_HOTSPOT);
  1347. RuleDto rule2 = newRule(SECURITY_HOTSPOT, r -> r.setSecurityStandards(of("cwe:117", "cwe:190")));
  1348. RuleDto rule3 = newRule(SECURITY_HOTSPOT, r -> r.setSecurityStandards(of("pciDss-3.2:1.2.3")));
  1349. RuleDto rule4 = newRule(SECURITY_HOTSPOT, r -> r.setSecurityStandards(of("pciDss-4.0:2.3a")));
  1350. insertHotspot(project, file, rule1);
  1351. insertHotspot(project, file, rule2);
  1352. IssueDto hotspot3 = insertHotspot(project, file, rule3);
  1353. IssueDto hotspot4 = insertHotspot(project, file, rule4);
  1354. indexIssues();
  1355. SearchWsResponse response32 = newRequest(project).setParam(PARAM_PCI_DSS_32, "1")
  1356. .executeProtobuf(SearchWsResponse.class);
  1357. assertThat(response32.getHotspotsList())
  1358. .extracting(SearchWsResponse.Hotspot::getKey)
  1359. .containsExactly(hotspot3.getKey());
  1360. SearchWsResponse response40 = newRequest(project).setParam(PARAM_PCI_DSS_40, "2")
  1361. .executeProtobuf(SearchWsResponse.class);
  1362. assertThat(response40.getHotspotsList())
  1363. .extracting(SearchWsResponse.Hotspot::getKey)
  1364. .containsExactly(hotspot4.getKey());
  1365. }
  1366. @Test
  1367. public void returns_hotspots_with_specified_owaspAsvs_category() {
  1368. ProjectData projectData = dbTester.components().insertPublicProject();
  1369. ComponentDto project = projectData.getMainBranchComponent();
  1370. userSessionRule.registerProjects(projectData.getProjectDto());
  1371. indexPermissions();
  1372. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  1373. RuleDto rule1 = newRule(SECURITY_HOTSPOT);
  1374. RuleDto rule2 = newRule(SECURITY_HOTSPOT, r -> r.setSecurityStandards(of("cwe:117", "cwe:190")));
  1375. RuleDto rule3 = newRule(SECURITY_HOTSPOT, r -> r.setSecurityStandards(of("owaspAsvs-4.0:1.2.3")));
  1376. RuleDto rule4 = newRule(SECURITY_HOTSPOT, r -> r.setSecurityStandards(of("owaspAsvs-4.0:1.2.4")));
  1377. insertHotspot(project, file, rule1);
  1378. insertHotspot(project, file, rule2);
  1379. IssueDto hotspot3 = insertHotspot(project, file, rule3);
  1380. IssueDto hotspot4 = insertHotspot(project, file, rule4);
  1381. indexIssues();
  1382. SearchWsResponse responseFor1 = newRequest(project).setParam(PARAM_OWASP_ASVS_40, "1")
  1383. .executeProtobuf(SearchWsResponse.class);
  1384. assertThat(responseFor1.getHotspotsList())
  1385. .extracting(SearchWsResponse.Hotspot::getKey)
  1386. .containsOnly(hotspot3.getKey(), hotspot4.getKey());
  1387. SearchWsResponse responseFor124 = newRequest(project).setParam(PARAM_OWASP_ASVS_40, "1.2.4")
  1388. .executeProtobuf(SearchWsResponse.class);
  1389. assertThat(responseFor124.getHotspotsList())
  1390. .extracting(SearchWsResponse.Hotspot::getKey)
  1391. .containsExactly(hotspot4.getKey());
  1392. }
  1393. @Test
  1394. public void returns_hotspots_with_specified_owaspAsvs_level() {
  1395. ProjectData projectData = dbTester.components().insertPublicProject();
  1396. ComponentDto project = projectData.getMainBranchComponent();
  1397. userSessionRule.registerProjects(projectData.getProjectDto());
  1398. indexPermissions();
  1399. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  1400. RuleDto rule1 = newRule(SECURITY_HOTSPOT);
  1401. RuleDto rule2 = newRule(SECURITY_HOTSPOT, r -> r.setSecurityStandards(of("cwe:117", "cwe:190")));
  1402. RuleDto rule3 = newRule(SECURITY_HOTSPOT, r -> r.setSecurityStandards(of("owaspAsvs-4.0:2.1.1")));
  1403. RuleDto rule4 = newRule(SECURITY_HOTSPOT, r -> r.setSecurityStandards(of("owaspAsvs-4.0:1.1.1")));
  1404. RuleDto rule5 = newRule(SECURITY_HOTSPOT, r -> r.setSecurityStandards(of("owaspAsvs-4.0:3.6.1")));
  1405. insertHotspot(project, file, rule1);
  1406. insertHotspot(project, file, rule2);
  1407. IssueDto hotspot3 = insertHotspot(project, file, rule3);
  1408. IssueDto hotspot4 = insertHotspot(project, file, rule4);
  1409. IssueDto hotspot5 = insertHotspot(project, file, rule5);
  1410. indexIssues();
  1411. SearchWsResponse responseFor1 = newRequest(project)
  1412. .setParam(PARAM_OWASP_ASVS_40, "1,2,3")
  1413. .setParam(PARAM_OWASP_ASVS_LEVEL, "1")
  1414. .executeProtobuf(SearchWsResponse.class);
  1415. assertThat(responseFor1.getHotspotsList())
  1416. .extracting(SearchWsResponse.Hotspot::getKey)
  1417. .containsExactlyInAnyOrder(hotspot3.getKey());
  1418. SearchWsResponse responseFor2 = newRequest(project)
  1419. .setParam(PARAM_OWASP_ASVS_40, "1,2,3")
  1420. .setParam(PARAM_OWASP_ASVS_LEVEL, "2")
  1421. .executeProtobuf(SearchWsResponse.class);
  1422. assertThat(responseFor2.getHotspotsList())
  1423. .extracting(SearchWsResponse.Hotspot::getKey)
  1424. .containsExactlyInAnyOrder(hotspot3.getKey(), hotspot4.getKey());
  1425. SearchWsResponse responseFor3 = newRequest(project)
  1426. .setParam(PARAM_OWASP_ASVS_40, "1.1.1,2,3")
  1427. .setParam(PARAM_OWASP_ASVS_LEVEL, "3")
  1428. .executeProtobuf(SearchWsResponse.class);
  1429. assertThat(responseFor3.getHotspotsList())
  1430. .extracting(SearchWsResponse.Hotspot::getKey)
  1431. .containsExactlyInAnyOrder(hotspot3.getKey(), hotspot4.getKey(), hotspot5.getKey());
  1432. SearchWsResponse responseFor1111 = newRequest(project)
  1433. .setParam(PARAM_OWASP_ASVS_40, "1.1.1")
  1434. .setParam(PARAM_OWASP_ASVS_LEVEL, "1")
  1435. .executeProtobuf(SearchWsResponse.class);
  1436. assertThat(responseFor1111.getHotspotsList())
  1437. .extracting(SearchWsResponse.Hotspot::getKey)
  1438. .isEmpty();
  1439. }
  1440. @Test
  1441. public void returns_hotspots_with_specified_owasp2021Top10_category() {
  1442. ProjectData projectData = dbTester.components().insertPublicProject();
  1443. ComponentDto project = projectData.getMainBranchComponent();
  1444. userSessionRule.registerProjects(projectData.getProjectDto());
  1445. indexPermissions();
  1446. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  1447. RuleDto rule1 = newRule(SECURITY_HOTSPOT);
  1448. RuleDto rule2 = newRule(SECURITY_HOTSPOT, r -> r.setSecurityStandards(of("cwe:117", "cwe:190")));
  1449. RuleDto rule3 = newRule(SECURITY_HOTSPOT, r -> r.setSecurityStandards(of("owaspTop10-2021:a5", "cwe:489")));
  1450. insertHotspot(project, file, rule1);
  1451. insertHotspot(project, file, rule2);
  1452. IssueDto hotspot3 = insertHotspot(project, file, rule3);
  1453. indexIssues();
  1454. SearchWsResponse response = newRequest(project).setParam(PARAM_OWASP_TOP_10_2021, "a5")
  1455. .executeProtobuf(SearchWsResponse.class);
  1456. assertThat(response.getHotspotsList())
  1457. .extracting(SearchWsResponse.Hotspot::getKey)
  1458. .containsExactly(hotspot3.getKey());
  1459. }
  1460. @Test
  1461. public void returns_hotspots_with_specified_sansTop25_category() {
  1462. ProjectData projectData = dbTester.components().insertPublicProject();
  1463. ComponentDto project = projectData.getMainBranchComponent();
  1464. userSessionRule.registerProjects(projectData.getProjectDto());
  1465. indexPermissions();
  1466. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  1467. RuleDto rule1 = newRule(SECURITY_HOTSPOT);
  1468. RuleDto rule2 = newRule(SECURITY_HOTSPOT, r -> r.setSecurityStandards(of("cwe:117", "cwe:190")));
  1469. RuleDto rule3 = newRule(SECURITY_HOTSPOT, r -> r.setSecurityStandards(of("owaspTop10:a1", "cwe:601")));
  1470. insertHotspot(project, file, rule1);
  1471. insertHotspot(project, file, rule2);
  1472. IssueDto hotspot3 = insertHotspot(project, file, rule3);
  1473. indexIssues();
  1474. SearchWsResponse response = newRequest(project).setParam(PARAM_SANS_TOP_25, "insecure-interaction")
  1475. .executeProtobuf(SearchWsResponse.class);
  1476. assertThat(response.getHotspotsList())
  1477. .extracting(SearchWsResponse.Hotspot::getKey)
  1478. .containsExactly(hotspot3.getKey());
  1479. }
  1480. @Test
  1481. public void returns_hotspots_with_specified_files() {
  1482. ProjectData projectData = dbTester.components().insertPublicProject();
  1483. ComponentDto project = projectData.getMainBranchComponent();
  1484. userSessionRule.registerProjects(projectData.getProjectDto());
  1485. indexPermissions();
  1486. ComponentDto file1 = dbTester.components().insertComponent(newFileDto(project));
  1487. ComponentDto file2 = dbTester.components().insertComponent(newFileDto(project));
  1488. RuleDto rule = newRule(SECURITY_HOTSPOT);
  1489. final IssueDto hotspot = insertHotspot(project, file1, rule);
  1490. insertHotspot(project, file2, rule);
  1491. indexIssues();
  1492. SearchWsResponse response = newRequest(project).setParam(PARAM_FILES, file1.path())
  1493. .executeProtobuf(SearchWsResponse.class);
  1494. assertThat(response.getHotspotsList())
  1495. .extracting(SearchWsResponse.Hotspot::getKey)
  1496. .containsExactly(hotspot.getKey());
  1497. }
  1498. @Test
  1499. public void returns_hotspots_on_the_leak_period_when_inNewCodePeriod_is_true() {
  1500. ProjectData projectData = dbTester.components().insertPublicProject();
  1501. ComponentDto project = projectData.getMainBranchComponent();
  1502. userSessionRule.registerProjects(projectData.getProjectDto());
  1503. userSessionRule.addProjectBranchMapping(projectData.projectUuid(), project);
  1504. indexPermissions();
  1505. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  1506. long periodDate = 800_996_999_332L;
  1507. dbTester.components().insertSnapshot(project, t -> t.setPeriodDate(periodDate).setLast(false));
  1508. dbTester.components().insertSnapshot(project, t -> t.setPeriodDate(periodDate - 1_500).setLast(true));
  1509. RuleDto rule = newRule(SECURITY_HOTSPOT);
  1510. List<IssueDto> hotspotsInLeakPeriod = IntStream.range(0, 1 + RANDOM.nextInt(20))
  1511. .mapToObj(i -> {
  1512. long issueCreationDate = periodDate + ONE_MINUTE + (RANDOM.nextInt(300) * ONE_MINUTE);
  1513. return dbTester.issues().insertHotspot(rule, project, file,
  1514. t -> t.setLine(i).setIssueCreationTime(issueCreationDate));
  1515. })
  1516. .collect(toList());
  1517. // included because
  1518. List<IssueDto> atLeakPeriod = IntStream.range(0, 1 + RANDOM.nextInt(20))
  1519. .mapToObj(i -> dbTester.issues().insertHotspot(rule, project, file,
  1520. t -> t.setType(SECURITY_HOTSPOT).setLine(i).setIssueCreationTime(periodDate)))
  1521. .collect(toList());
  1522. List<IssueDto> hotspotsBefore = IntStream.range(0, 1 + RANDOM.nextInt(20))
  1523. .mapToObj(i -> {
  1524. long issueCreationDate = periodDate - ONE_MINUTE - (RANDOM.nextInt(300) * ONE_MINUTE);
  1525. return dbTester.issues().insertHotspot(rule, project, file,
  1526. t -> t.setLine(i).setIssueCreationTime(issueCreationDate));
  1527. })
  1528. .collect(toList());
  1529. indexIssues();
  1530. SearchWsResponse responseAll = newRequest(project)
  1531. .executeProtobuf(SearchWsResponse.class);
  1532. assertThat(responseAll.getHotspotsList())
  1533. .extracting(SearchWsResponse.Hotspot::getKey)
  1534. .containsExactlyInAnyOrder(Stream.of(
  1535. hotspotsInLeakPeriod.stream(),
  1536. atLeakPeriod.stream(),
  1537. hotspotsBefore.stream())
  1538. .flatMap(t -> t)
  1539. .map(IssueDto::getKey)
  1540. .toArray(String[]::new));
  1541. SearchWsResponse responseOnLeak = newRequest(project,
  1542. t -> t.setParam(PARAM_IN_NEW_CODE_PERIOD, "true"))
  1543. .executeProtobuf(SearchWsResponse.class);
  1544. assertThat(responseOnLeak.getHotspotsList())
  1545. .extracting(SearchWsResponse.Hotspot::getKey)
  1546. .containsExactlyInAnyOrder(Stream.concat(
  1547. hotspotsInLeakPeriod.stream(),
  1548. atLeakPeriod.stream())
  1549. .map(IssueDto::getKey)
  1550. .toArray(String[]::new));
  1551. }
  1552. @Test
  1553. public void returns_hotspots_on_the_leak_period_when_inNewCodePeriod_is_true_and_branch_uses_reference_branch() {
  1554. ProjectData projectData = dbTester.components().insertPublicProject();
  1555. ComponentDto project = projectData.getMainBranchComponent();
  1556. userSessionRule.registerProjects(projectData.getProjectDto());
  1557. indexPermissions();
  1558. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  1559. dbTester.components().insertSnapshot(project, t -> t.setPeriodMode(REFERENCE_BRANCH.name()).setPeriodParam("master"));
  1560. RuleDto rule = newRule(SECURITY_HOTSPOT);
  1561. List<IssueDto> hotspotsInLeakPeriod = IntStream.range(0, 1 + RANDOM.nextInt(20))
  1562. .mapToObj(i -> dbTester.issues().insertHotspot(rule, project, file, t -> t.setLine(i)))
  1563. .collect(toList());
  1564. hotspotsInLeakPeriod.stream().forEach(i -> dbTester.issues().insertNewCodeReferenceIssue(newCodeReferenceIssue(i)));
  1565. List<IssueDto> hotspotsNotInLeakPeriod = IntStream.range(0, 1 + RANDOM.nextInt(20))
  1566. .mapToObj(i -> dbTester.issues().insertHotspot(rule, project, file, t -> t.setLine(i)))
  1567. .collect(toList());
  1568. indexIssues();
  1569. SearchWsResponse responseAll = newRequest(project)
  1570. .executeProtobuf(SearchWsResponse.class);
  1571. assertThat(responseAll.getHotspotsList())
  1572. .extracting(SearchWsResponse.Hotspot::getKey)
  1573. .containsExactlyInAnyOrder(Stream.of(
  1574. hotspotsInLeakPeriod.stream(),
  1575. hotspotsNotInLeakPeriod.stream())
  1576. .flatMap(t -> t)
  1577. .map(IssueDto::getKey)
  1578. .toArray(String[]::new));
  1579. SearchWsResponse responseOnLeak = newRequest(project,
  1580. t -> t.setParam(PARAM_IN_NEW_CODE_PERIOD, "true"))
  1581. .executeProtobuf(SearchWsResponse.class);
  1582. assertThat(responseOnLeak.getHotspotsList())
  1583. .extracting(SearchWsResponse.Hotspot::getKey)
  1584. .containsExactlyInAnyOrder(hotspotsInLeakPeriod
  1585. .stream()
  1586. .map(IssueDto::getKey)
  1587. .toArray(String[]::new));
  1588. }
  1589. @Test
  1590. public void returns_nothing_when_inNewCodePeriod_is_true_and_no_period_exists() {
  1591. long referenceDate = 800_996_999_332L;
  1592. system2.setNow(referenceDate + 10_000);
  1593. ProjectData projectData = dbTester.components().insertPublicProject();
  1594. ComponentDto project = projectData.getMainBranchComponent();
  1595. userSessionRule.registerProjects(projectData.getProjectDto());
  1596. indexPermissions();
  1597. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  1598. dbTester.components().insertSnapshot(project, t -> t.setPeriodDate(referenceDate).setLast(false));
  1599. dbTester.components().insertSnapshot(project, t -> t.setPeriodDate(null).setLast(true));
  1600. RuleDto rule = newRule(SECURITY_HOTSPOT);
  1601. IssueDto afterRef = dbTester.issues().insertHotspot(rule, project, file, t -> t.setIssueCreationTime(referenceDate + 1000));
  1602. IssueDto atRef = dbTester.issues().insertHotspot(rule, project, file, t -> t.setType(SECURITY_HOTSPOT).setIssueCreationTime(referenceDate));
  1603. IssueDto beforeRef = dbTester.issues().insertHotspot(rule, project, file, t -> t.setIssueCreationTime(referenceDate - 1000));
  1604. indexIssues();
  1605. SearchWsResponse responseAll = newRequest(project)
  1606. .executeProtobuf(SearchWsResponse.class);
  1607. assertThat(responseAll.getHotspotsList())
  1608. .extracting(SearchWsResponse.Hotspot::getKey)
  1609. .containsExactlyInAnyOrder(Stream.of(afterRef, atRef, beforeRef)
  1610. .map(IssueDto::getKey)
  1611. .toArray(String[]::new));
  1612. SearchWsResponse responseOnLeak = newRequest(project,
  1613. t -> t.setParam(PARAM_IN_NEW_CODE_PERIOD, "true"))
  1614. .executeProtobuf(SearchWsResponse.class);
  1615. assertThat(responseOnLeak.getHotspotsList()).isEmpty();
  1616. }
  1617. @Test
  1618. public void returns_all_issues_when_inNewCodePeriod_is_true_and_is_pr() {
  1619. long referenceDate = 800_996_999_332L;
  1620. system2.setNow(referenceDate + 10_000);
  1621. ProjectData projectData = dbTester.components().insertPublicProject();
  1622. ComponentDto project = projectData.getMainBranchComponent();
  1623. ComponentDto pr = dbTester.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST).setKey("pr"));
  1624. userSessionRule.registerProjects(projectData.getProjectDto());
  1625. indexPermissions();
  1626. ComponentDto file = dbTester.components().insertComponent(newFileDto(pr, project.uuid()));
  1627. dbTester.components().insertSnapshot(project, t -> t.setPeriodDate(referenceDate).setLast(true));
  1628. dbTester.components().insertSnapshot(pr, t -> t.setPeriodDate(null).setLast(true));
  1629. RuleDto rule = newRule(SECURITY_HOTSPOT);
  1630. IssueDto afterRef = dbTester.issues().insertHotspot(rule, pr, file, t -> t.setIssueCreationTime(referenceDate + 1000));
  1631. IssueDto atRef = dbTester.issues().insertHotspot(rule, pr, file, t -> t.setType(SECURITY_HOTSPOT).setIssueCreationTime(referenceDate));
  1632. IssueDto beforeRef = dbTester.issues().insertHotspot(rule, pr, file, t -> t.setIssueCreationTime(referenceDate - 1000));
  1633. indexIssues();
  1634. SearchWsResponse responseAll = newRequest(project).setParam(PARAM_PULL_REQUEST, "pr")
  1635. .executeProtobuf(SearchWsResponse.class);
  1636. assertThat(responseAll.getHotspotsList())
  1637. .extracting(SearchWsResponse.Hotspot::getKey)
  1638. .containsExactlyInAnyOrder(Stream.of(afterRef, atRef, beforeRef)
  1639. .map(IssueDto::getKey)
  1640. .toArray(String[]::new));
  1641. SearchWsResponse responseOnLeak = newRequest(project,
  1642. t -> t.setParam(PARAM_IN_NEW_CODE_PERIOD, "true").setParam(PARAM_PULL_REQUEST, "pr"))
  1643. .executeProtobuf(SearchWsResponse.class);
  1644. assertThat(responseOnLeak.getHotspotsList()).hasSize(3);
  1645. }
  1646. @Test
  1647. public void returns_issues_when_inNewCodePeriod_is_true_and_is_application_for_main_branch() {
  1648. long referenceDate = 800_996_999_332L;
  1649. system2.setNow(referenceDate + 10_000);
  1650. ProjectData application = dbTester.components().insertPublicApplication();
  1651. ProjectData project = dbTester.components().insertPublicProject();
  1652. ProjectData project2 = dbTester.components().insertPublicProject();
  1653. dbTester.components().addApplicationProject(application, project);
  1654. dbTester.components().addApplicationProject(application, project2);
  1655. dbTester.components().insertComponent(ComponentTesting.newProjectCopy(project, application));
  1656. dbTester.components().insertComponent(ComponentTesting.newProjectCopy(project2, application));
  1657. indexViews();
  1658. userSessionRule.registerApplication(application.getProjectDto(), project.getProjectDto(), project2.getProjectDto());
  1659. indexPermissions();
  1660. ComponentDto file = dbTester.components().insertComponent(newFileDto(project.getMainBranchComponent()));
  1661. dbTester.components().insertSnapshot(project, t -> t.setPeriodDate(referenceDate).setLast(true));
  1662. RuleDto rule = newRule(SECURITY_HOTSPOT);
  1663. IssueDto afterRef = dbTester.issues().insertHotspot(rule, project, file, t -> t.setIssueCreationTime(referenceDate + 1000));
  1664. IssueDto atRef = dbTester.issues().insertHotspot(rule, project, file, t -> t.setType(SECURITY_HOTSPOT).setIssueCreationTime(referenceDate));
  1665. IssueDto beforeRef = dbTester.issues().insertHotspot(rule, project, file, t -> t.setIssueCreationTime(referenceDate - 1000));
  1666. ComponentDto file2 = dbTester.components().insertComponent(newFileDto(project2.getMainBranchComponent()));
  1667. IssueDto project2Issue = dbTester.issues().insertHotspot(rule, project2, file2, t -> t.setIssueCreationTime(referenceDate - 1000));
  1668. indexIssues();
  1669. SearchWsResponse responseAll = newRequest(application.getMainBranchComponent())
  1670. .executeProtobuf(SearchWsResponse.class);
  1671. assertThat(responseAll.getHotspotsList())
  1672. .extracting(SearchWsResponse.Hotspot::getKey)
  1673. .containsExactlyInAnyOrder(afterRef.getKey(), atRef.getKey(), beforeRef.getKey(), project2Issue.getKey());
  1674. SearchWsResponse responseOnLeak = newRequest(application.getMainBranchComponent(),
  1675. t -> t.setParam(PARAM_IN_NEW_CODE_PERIOD, "true"))
  1676. .executeProtobuf(SearchWsResponse.class);
  1677. assertThat(responseOnLeak.getHotspotsList())
  1678. .extracting(SearchWsResponse.Hotspot::getKey)
  1679. .containsExactlyInAnyOrder(afterRef.getKey());
  1680. }
  1681. @Test
  1682. public void returns_issues_when_inNewCodePeriod_is_true_and_is_application_for_branch_other_than_main() {
  1683. long referenceDate = 800_996_999_332L;
  1684. system2.setNow(referenceDate + 10_000);
  1685. ProjectData applicationData = dbTester.components().insertPublicApplication();
  1686. ProjectDto application = applicationData.getProjectDto();
  1687. BranchDto applicationBranch = dbTester.components().insertProjectBranch(application, branchDto -> branchDto.setKey("application_branch_1"));
  1688. ProjectDto project = dbTester.components().insertPublicProject().getProjectDto();
  1689. BranchDto projectBranch = dbTester.components().insertProjectBranch(project, branchDto -> branchDto.setKey("project_1_branch_1"));
  1690. ProjectDto project2 = dbTester.components().insertPublicProject().getProjectDto();
  1691. BranchDto project2Branch = dbTester.components().insertProjectBranch(project2, branchDto -> branchDto.setKey("project_2_branch_1"));
  1692. dbTester.components().addApplicationProject(application, project);
  1693. dbTester.components().addApplicationProject(application, project2);
  1694. dbTester.components().addProjectBranchToApplicationBranch(applicationBranch, projectBranch, project2Branch);
  1695. ComponentDto applicationBranchComponentDto = dbClient.componentDao().selectByUuid(dbTester.getSession(), applicationBranch.getUuid()).get();
  1696. ComponentDto projectBranchComponentDto = dbClient.componentDao().selectByUuid(dbTester.getSession(), projectBranch.getUuid()).get();
  1697. ComponentDto project2BranchComponentDto = dbClient.componentDao().selectByUuid(dbTester.getSession(), project2Branch.getUuid()).get();
  1698. dbTester.components().insertComponent(ComponentTesting.newProjectCopy(projectBranchComponentDto, applicationBranchComponentDto));
  1699. dbTester.components().insertComponent(ComponentTesting.newProjectCopy(project2BranchComponentDto, applicationBranchComponentDto));
  1700. indexViews();
  1701. userSessionRule.registerApplication(application, project, project2);
  1702. indexPermissions();
  1703. ComponentDto file = dbTester.components().insertComponent(newFileDto(projectBranchComponentDto));
  1704. dbTester.components().insertSnapshot(projectBranch, t -> t.setPeriodDate(referenceDate).setLast(true));
  1705. RuleDto rule = newRule(SECURITY_HOTSPOT);
  1706. IssueDto afterRef = dbTester.issues().insertHotspot(rule, projectBranchComponentDto, file, t -> t.setIssueCreationTime(referenceDate + 1000));
  1707. IssueDto atRef = dbTester.issues().insertHotspot(rule, projectBranchComponentDto, file, t -> t.setType(SECURITY_HOTSPOT).setIssueCreationTime(referenceDate));
  1708. IssueDto beforeRef = dbTester.issues().insertHotspot(rule, projectBranchComponentDto, file, t -> t.setIssueCreationTime(referenceDate - 1000));
  1709. ComponentDto file2 = dbTester.components().insertComponent(newFileDto(project2BranchComponentDto));
  1710. IssueDto project2Issue = dbTester.issues().insertHotspot(rule, project2BranchComponentDto, file2, t -> t.setIssueCreationTime(referenceDate - 1000));
  1711. indexIssues();
  1712. ComponentDto applicationComponentDto = applicationData.getMainBranchComponent();
  1713. SearchWsResponse responseAll = newRequest(applicationComponentDto,
  1714. t -> t.setParam(PARAM_BRANCH, applicationBranch.getKey()))
  1715. .executeProtobuf(SearchWsResponse.class);
  1716. assertThat(responseAll.getHotspotsList())
  1717. .extracting(SearchWsResponse.Hotspot::getKey)
  1718. .containsExactlyInAnyOrder(afterRef.getKey(), atRef.getKey(), beforeRef.getKey(), project2Issue.getKey());
  1719. SearchWsResponse responseOnLeak = newRequest(applicationComponentDto,
  1720. t -> t.setParam(PARAM_IN_NEW_CODE_PERIOD, "true").setParam(PARAM_BRANCH, applicationBranch.getKey()))
  1721. .executeProtobuf(SearchWsResponse.class);
  1722. assertThat(responseOnLeak.getHotspotsList())
  1723. .extracting(SearchWsResponse.Hotspot::getKey)
  1724. .containsExactlyInAnyOrder(afterRef.getKey());
  1725. }
  1726. @Test
  1727. public void verify_response_example() {
  1728. ProjectData projectData = dbTester.components().insertPublicProject(componentDto -> componentDto
  1729. .setName("test-project")
  1730. .setLongName("test-project")
  1731. .setKey("com.sonarsource:test-project"));
  1732. ComponentDto project = projectData.getMainBranchComponent();
  1733. userSessionRule.registerProjects(projectData.getProjectDto());
  1734. indexPermissions();
  1735. ComponentDto fileWithHotspot = dbTester.components().insertComponent(newFileDto(project)
  1736. .setKey("com.sonarsource:test-project:src/main/java/com/sonarsource/FourthClass.java")
  1737. .setName("FourthClass.java")
  1738. .setLongName("src/main/java/com/sonarsource/FourthClass.java")
  1739. .setPath("src/main/java/com/sonarsource/FourthClass.java"));
  1740. long time = 1577976190000L;
  1741. IssueDto[] hotspots = IntStream.range(0, 3)
  1742. .mapToObj(i -> {
  1743. RuleKey ruleKey = RuleKey.of("repository-" + i, "rule-" + i);
  1744. RuleDto rule = newRule(SECURITY_HOTSPOT, ruleKey)
  1745. .setSecurityStandards(Sets.newHashSet(SQCategory.WEAK_CRYPTOGRAPHY.getKey()));
  1746. return insertHotspot(rule, project, fileWithHotspot, issueDto -> issueDto.setKee("hotspot-" + i)
  1747. .setAssigneeUuid("assignee-uuid")
  1748. .setAuthorLogin("joe")
  1749. .setMessage("message-" + i)
  1750. .setLine(10 + i)
  1751. .setIssueCreationTime(time)
  1752. .setIssueUpdateTime(time));
  1753. })
  1754. .toArray(IssueDto[]::new);
  1755. indexIssues();
  1756. assertThat(actionTester.getDef().responseExampleAsString()).isNotNull();
  1757. newRequest(project)
  1758. .execute()
  1759. .assertJson(actionTester.getDef().responseExampleAsString());
  1760. }
  1761. @Test
  1762. public void returns_hotspots_with_ruleKey() {
  1763. ProjectData projectData = dbTester.components().insertPublicProject();
  1764. ComponentDto project = projectData.getMainBranchComponent();
  1765. userSessionRule.registerProjects(projectData.getProjectDto());
  1766. indexPermissions();
  1767. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  1768. RuleDto rule1 = newRule(SECURITY_HOTSPOT);
  1769. insertHotspot(project, file, rule1);
  1770. indexIssues();
  1771. SearchWsResponse response = newRequest(project)
  1772. .executeProtobuf(SearchWsResponse.class);
  1773. assertThat(response.getHotspotsList())
  1774. .extracting(SearchWsResponse.Hotspot::getRuleKey)
  1775. .containsExactly(rule1.getKey().toString());
  1776. }
  1777. private IssueDto insertHotspot(ComponentDto project, ComponentDto file, RuleDto rule) {
  1778. return insertHotspot(rule, project, file, t -> {
  1779. });
  1780. }
  1781. private IssueDto insertHotspot(RuleDto rule, ComponentDto project, ComponentDto file, @Nullable String assigneeUuid) {
  1782. return insertHotspot(rule, project, file, t -> t.setAssigneeUuid(assigneeUuid));
  1783. }
  1784. private IssueDto insertHotspot(RuleDto rule, ComponentDto project, ComponentDto file, Consumer<IssueDto> consumer) {
  1785. return dbTester.issues().insertHotspot(rule, project, file, consumer);
  1786. }
  1787. private static IssueDto newHotspot(RuleDto rule, ComponentDto project, ComponentDto component) {
  1788. return newHotspot(rule, project, component, t -> {
  1789. });
  1790. }
  1791. private static IssueDto newHotspot(RuleDto rule, ComponentDto project, ComponentDto component, Consumer<IssueDto> consumer) {
  1792. IssueDto res = newIssue(rule, project, component)
  1793. .setStatus(STATUS_TO_REVIEW);
  1794. consumer.accept(res);
  1795. return res.setType(SECURITY_HOTSPOT);
  1796. }
  1797. private static DbIssues.Location newHotspotLocation(@Nullable String componentUuid, String message, int startLine, int endLine, int startOffset, int endOffset) {
  1798. DbIssues.Location.Builder builder = DbIssues.Location.newBuilder();
  1799. if (componentUuid != null) {
  1800. builder.setComponentId(componentUuid);
  1801. }
  1802. builder
  1803. .setMsg(message)
  1804. .setTextRange(DbCommons.TextRange.newBuilder()
  1805. .setStartLine(startLine)
  1806. .setEndLine(endLine)
  1807. .setStartOffset(startOffset)
  1808. .setEndOffset(endOffset)
  1809. .build());
  1810. return builder.build();
  1811. }
  1812. private TestRequest newRequest(ComponentDto project) {
  1813. return newRequest(project, null, null, null, null);
  1814. }
  1815. private TestRequest newRequest(ComponentDto project, Consumer<TestRequest> consumer) {
  1816. return newRequest(project, null,
  1817. null, null, null, consumer);
  1818. }
  1819. private TestRequest newRequest(ComponentDto project, @Nullable String status, @Nullable String resolution, @Nullable String branch, @Nullable String pullRequest) {
  1820. return newRequest(project, status, resolution, branch, pullRequest, t -> {
  1821. });
  1822. }
  1823. private TestRequest newRequest(ComponentDto project, @Nullable String status, @Nullable String resolution,
  1824. @Nullable String branch, @Nullable String pullRequest, Consumer<TestRequest> consumer) {
  1825. TestRequest res = actionTester.newRequest()
  1826. .setParam(PARAM_PROJECT_KEY, project.getKey());
  1827. if (branch != null) {
  1828. res.setParam(PARAM_BRANCH, branch);
  1829. }
  1830. if (pullRequest != null) {
  1831. res.setParam(PARAM_PULL_REQUEST, pullRequest);
  1832. }
  1833. if (status != null) {
  1834. res.setParam(PARAM_STATUS, status);
  1835. }
  1836. if (resolution != null) {
  1837. res.setParam(PARAM_RESOLUTION, resolution);
  1838. }
  1839. consumer.accept(res);
  1840. return res;
  1841. }
  1842. private TestRequest newRequest(Collection<String> hotspotKeys) {
  1843. return actionTester.newRequest()
  1844. .setParam(PARAM_HOTSPOTS, String.join(",", hotspotKeys));
  1845. }
  1846. private void indexPermissions() {
  1847. permissionIndexer.indexAll(permissionIndexer.getIndexTypes());
  1848. }
  1849. private void indexIssues() {
  1850. issueIndexer.indexAllIssues();
  1851. }
  1852. private void indexViews() {
  1853. viewIndexer.indexAll();
  1854. }
  1855. private RuleDto newRule(RuleType ruleType) {
  1856. return newRule(ruleType, t -> {
  1857. });
  1858. }
  1859. private RuleDto newRule(RuleType ruleType, RuleKey ruleKey) {
  1860. RuleDto ruleDto = RuleTesting.newRule(ruleKey)
  1861. .setType(ruleType);
  1862. dbTester.rules().insert(ruleDto);
  1863. return ruleDto;
  1864. }
  1865. private RuleDto newRule(RuleType ruleType, Consumer<RuleDto> populate) {
  1866. RuleDto ruleDto = RuleTesting.newRule()
  1867. .setType(ruleType);
  1868. populate.accept(ruleDto);
  1869. dbTester.rules().insert(ruleDto);
  1870. return ruleDto;
  1871. }
  1872. }