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.

testMocks.ts 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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. import { To } from 'react-router-dom';
  21. import { CompareResponse } from '../api/quality-profiles';
  22. import { RuleDescriptionSections } from '../apps/coding-rules/rule';
  23. import { Exporter, Profile, ProfileChangelogEvent } from '../apps/quality-profiles/types';
  24. import { LogsLevels } from '../apps/system/utils';
  25. import { Location, Router } from '../components/hoc/withRouter';
  26. import { AppState } from '../types/appstate';
  27. import {
  28. CleanCodeAttribute,
  29. CleanCodeAttributeCategory,
  30. SoftwareImpactSeverity,
  31. SoftwareQuality,
  32. } from '../types/clean-code-taxonomy';
  33. import { RuleRepository } from '../types/coding-rules';
  34. import { EditionKey } from '../types/editions';
  35. import {
  36. IssueDeprecatedStatus,
  37. IssueScope,
  38. IssueSeverity,
  39. IssueStatus,
  40. IssueType,
  41. RawIssue,
  42. } from '../types/issues';
  43. import { Language } from '../types/languages';
  44. import { MetricKey, MetricType } from '../types/metrics';
  45. import { Notification } from '../types/notifications';
  46. import { DumpStatus, DumpTask } from '../types/project-dump';
  47. import { TaskStatuses } from '../types/tasks';
  48. import {
  49. AlmApplication,
  50. Condition,
  51. FlowLocation,
  52. Group,
  53. HealthTypes,
  54. IdentityProvider,
  55. Issue,
  56. Measure,
  57. MeasureEnhanced,
  58. Metric,
  59. Paging,
  60. Period,
  61. Rule,
  62. RuleActivation,
  63. RuleDetails,
  64. RuleParameter,
  65. SysInfoBase,
  66. SysInfoCluster,
  67. SysInfoLogging,
  68. SysInfoStandalone,
  69. UserGroupMember,
  70. UserSelected,
  71. } from '../types/types';
  72. import { CurrentUser, LoggedInUser, RestUserDetailed, User } from '../types/users';
  73. export function mockAlmApplication(overrides: Partial<AlmApplication> = {}): AlmApplication {
  74. return {
  75. backgroundColor: '#444444',
  76. iconPath: '/images/sonarcloud/github-white.svg',
  77. installationUrl: 'https://github.com/apps/greg-sonarcloud/installations/new',
  78. key: 'github',
  79. name: 'GitHub',
  80. ...overrides,
  81. };
  82. }
  83. export function mockAppState(overrides: Partial<AppState> = {}): AppState {
  84. return {
  85. edition: EditionKey.community,
  86. productionDatabase: true,
  87. qualifiers: ['TRK'],
  88. settings: {},
  89. version: '1.0',
  90. documentationUrl: 'https://docs.sonarsource.com/sonarqube/10.0',
  91. ...overrides,
  92. };
  93. }
  94. export function mockBaseSysInfo(overrides: Partial<any> = {}): SysInfoBase {
  95. return {
  96. Health: HealthTypes.GREEN,
  97. 'Health Causes': [],
  98. System: {
  99. Version: '7.8',
  100. },
  101. Database: {
  102. Database: 'PostgreSQL',
  103. 'Database Version': '10.3',
  104. Username: 'sonar',
  105. URL: 'jdbc:postgresql://localhost/sonar',
  106. Driver: 'PostgreSQL JDBC Driver',
  107. 'Driver Version': '42.2.5',
  108. },
  109. 'Compute Engine Tasks': {
  110. 'Total Pending': 0,
  111. 'Total In Progress': 0,
  112. },
  113. 'Search State': { State: 'GREEN', Nodes: 3 },
  114. 'Search Indexes': {
  115. 'Index components - Docs': 30445,
  116. 'Index components - Shards': 10,
  117. },
  118. ...overrides,
  119. };
  120. }
  121. export function mockClusterSysInfo(overrides: Partial<any> = {}): SysInfoCluster {
  122. const baseInfo = mockBaseSysInfo(overrides);
  123. return {
  124. ...baseInfo,
  125. System: {
  126. ...baseInfo.System,
  127. 'High Availability': true,
  128. 'Server ID': 'asd564-asd54a-5dsfg45',
  129. },
  130. Settings: {
  131. 'sonar.cluster.enabled': 'true',
  132. 'sonar.cluster.node.name': 'server9.example.com',
  133. },
  134. 'Application Nodes': [
  135. {
  136. Name: 'server1.example.com',
  137. Host: '10.0.0.0',
  138. Health: HealthTypes.RED,
  139. 'Health Causes': ['Something is wrong'],
  140. System: {
  141. Version: '7.8',
  142. },
  143. Plugins: {
  144. java: '5.13.0.17924 [SonarJava]',
  145. },
  146. 'Web JVM State': {
  147. 'Max Memory (MB)': 1024,
  148. 'Free Memory (MB)': 122,
  149. },
  150. 'Web Database Connection': {
  151. 'Pool Active Connections': 1,
  152. },
  153. 'Web Logging': { 'Logs Level': 'DEBUG' },
  154. 'Web JVM Properties': {
  155. 'file.encoding': 'UTF-8',
  156. 'file.separator': '/',
  157. },
  158. 'Compute Engine Tasks': {
  159. Pending: 0,
  160. 'In Progress': 0,
  161. },
  162. 'Compute Engine JVM State': {
  163. 'Max Memory (MB)': 1024,
  164. 'Free Memory (MB)': 78,
  165. },
  166. 'Compute Engine Database Connection': {
  167. 'Pool Initial Size': 0,
  168. 'Pool Active Connections': 0,
  169. },
  170. 'Compute Engine Logging': {
  171. 'Logs Level': 'INFO',
  172. },
  173. 'Compute Engine JVM Properties': {
  174. 'file.encoding': 'UTF-8',
  175. 'file.separator': '/',
  176. },
  177. },
  178. {
  179. Name: 'server2.example.com',
  180. Host: '10.0.0.0',
  181. Health: HealthTypes.YELLOW,
  182. 'Health Causes': ['Friendly warning'],
  183. System: {
  184. Version: '7.8',
  185. },
  186. Plugins: {
  187. java: '5.13.0.17924 [SonarJava]',
  188. },
  189. 'Web JVM State': {
  190. 'Max Memory (MB)': 1024,
  191. 'Free Memory (MB)': 111,
  192. },
  193. 'Web Database Connection': {
  194. 'Pool Active Connections': 0,
  195. 'Pool Max Connections': 60,
  196. },
  197. 'Web Logging': { 'Logs Level': 'INFO' },
  198. 'Web JVM Properties': {
  199. 'file.encoding': 'UTF-8',
  200. 'file.separator': '/',
  201. },
  202. 'Compute Engine Tasks': {
  203. Pending: 0,
  204. 'In Progress': 0,
  205. },
  206. 'Compute Engine JVM State': {
  207. 'Max Memory (MB)': 1024,
  208. 'Free Memory (MB)': 89,
  209. },
  210. 'Compute Engine Database Connection': {
  211. 'Pool Initial Size': 0,
  212. 'Pool Active Connections': 0,
  213. },
  214. 'Compute Engine Logging': {
  215. 'Logs Level': 'INFO',
  216. },
  217. 'Compute Engine JVM Properties': {
  218. 'file.encoding': 'UTF-8',
  219. 'file.separator': '/',
  220. },
  221. },
  222. ],
  223. 'Search Nodes': [
  224. {
  225. Name: 'server1.example.com',
  226. Host: '10.0.0.0',
  227. 'Search State': {
  228. 'CPU Usage (%)': 0,
  229. 'Disk Available': '93 GB',
  230. },
  231. },
  232. {
  233. Name: 'server2.example.com',
  234. Host: '10.0.0.0',
  235. 'Search State': {
  236. 'CPU Usage (%)': 0,
  237. 'Disk Available': '93 GB',
  238. },
  239. },
  240. {
  241. Name: 'server3.example.com',
  242. Host: '10.0.0.0',
  243. 'Search State': {
  244. 'CPU Usage (%)': 0,
  245. 'Disk Available': '93 GB',
  246. },
  247. },
  248. ],
  249. Statistics: {
  250. ncloc: 989880,
  251. },
  252. ...overrides,
  253. };
  254. }
  255. export function mockCondition(overrides: Partial<Condition> = {}): Condition {
  256. return {
  257. error: '10',
  258. id: '1',
  259. metric: 'coverage',
  260. op: 'LT',
  261. ...overrides,
  262. };
  263. }
  264. export function mockCurrentUser(overrides: Partial<CurrentUser> = {}): CurrentUser {
  265. return {
  266. isLoggedIn: false,
  267. dismissedNotices: {
  268. educationPrinciples: false,
  269. },
  270. ...overrides,
  271. };
  272. }
  273. export function mockLoggedInUser(overrides: Partial<LoggedInUser> = {}): LoggedInUser {
  274. return {
  275. groups: [],
  276. isLoggedIn: true,
  277. login: 'luke',
  278. name: 'Skywalker',
  279. scmAccounts: [],
  280. dismissedNotices: {
  281. educationPrinciples: false,
  282. },
  283. ...overrides,
  284. };
  285. }
  286. export function mockGroup(overrides: Partial<Group> = {}): Group {
  287. return {
  288. membersCount: 1,
  289. name: 'Foo',
  290. managed: false,
  291. ...overrides,
  292. };
  293. }
  294. export function mockRawIssue(withLocations = false, overrides: Partial<RawIssue> = {}): RawIssue {
  295. const rawIssue: RawIssue = {
  296. actions: [],
  297. component: 'main.js',
  298. key: 'AVsae-CQS-9G3txfbFN2',
  299. creationDate: '2023-01-15T09:36:01+0100',
  300. line: 25,
  301. project: 'myproject',
  302. rule: 'javascript:S1067',
  303. severity: IssueSeverity.Major,
  304. textRange: { startLine: 25, endLine: 26, startOffset: 0, endOffset: 15 },
  305. type: IssueType.CodeSmell,
  306. status: IssueDeprecatedStatus.Open,
  307. issueStatus: IssueStatus.Open,
  308. transitions: [],
  309. scope: IssueScope.Main,
  310. cleanCodeAttributeCategory: CleanCodeAttributeCategory.Responsible,
  311. cleanCodeAttribute: CleanCodeAttribute.Respectful,
  312. impacts: [
  313. { softwareQuality: SoftwareQuality.Maintainability, severity: SoftwareImpactSeverity.Medium },
  314. ],
  315. ...overrides,
  316. };
  317. if (withLocations) {
  318. const loc = mockFlowLocation;
  319. rawIssue.flows = [
  320. {
  321. locations: [
  322. loc({ component: overrides.component }),
  323. loc({ component: overrides.component }),
  324. ],
  325. },
  326. ];
  327. }
  328. return {
  329. ...rawIssue,
  330. ...overrides,
  331. };
  332. }
  333. export function mockIssue(withLocations = false, overrides: Partial<Issue> = {}): Issue {
  334. const issue: Issue = {
  335. actions: [],
  336. component: 'main.js',
  337. componentEnabled: true,
  338. componentLongName: 'main.js',
  339. componentQualifier: 'FIL',
  340. componentUuid: 'foo1234',
  341. creationDate: '2017-03-01T09:36:01+0100',
  342. flows: [],
  343. flowsWithType: [],
  344. key: 'AVsae-CQS-9G3txfbFN2',
  345. line: 25,
  346. message: 'Reduce the number of conditional operators (4) used in the expression',
  347. project: 'myproject',
  348. projectKey: 'foo',
  349. projectName: 'Foo',
  350. rule: 'javascript:S1067',
  351. ruleName: 'foo',
  352. scope: IssueScope.Main,
  353. secondaryLocations: [],
  354. severity: IssueSeverity.Major,
  355. status: IssueDeprecatedStatus.Open,
  356. issueStatus: IssueStatus.Open,
  357. textRange: { startLine: 25, endLine: 26, startOffset: 0, endOffset: 15 },
  358. transitions: [],
  359. type: 'BUG',
  360. cleanCodeAttributeCategory: CleanCodeAttributeCategory.Responsible,
  361. cleanCodeAttribute: CleanCodeAttribute.Respectful,
  362. impacts: [
  363. { softwareQuality: SoftwareQuality.Maintainability, severity: SoftwareImpactSeverity.Medium },
  364. ],
  365. };
  366. const loc = mockFlowLocation;
  367. if (withLocations) {
  368. issue.flows = [
  369. [loc(), loc(), loc()],
  370. [loc(), loc()],
  371. ];
  372. issue.secondaryLocations = [loc(), loc()];
  373. }
  374. return {
  375. ...issue,
  376. ...overrides,
  377. };
  378. }
  379. export function mockLocation(overrides: Partial<Location> = {}): Location {
  380. return {
  381. hash: '',
  382. key: 'key',
  383. pathname: '/path',
  384. query: {},
  385. search: '',
  386. state: {},
  387. ...overrides,
  388. };
  389. }
  390. export function mockMetric(
  391. overrides: Partial<Pick<Metric, 'key' | 'name' | 'type' | 'domain'>> = {},
  392. ): Metric {
  393. const key = overrides.key || MetricKey.coverage;
  394. const name = overrides.name || key;
  395. const type = overrides.type || MetricType.Percent;
  396. return {
  397. ...overrides,
  398. id: key,
  399. key,
  400. name,
  401. type,
  402. };
  403. }
  404. export function mockMeasure(overrides: Partial<Measure> = {}): Measure {
  405. return {
  406. bestValue: true,
  407. metric: 'bugs',
  408. period: {
  409. bestValue: true,
  410. index: 1,
  411. value: '1.0',
  412. },
  413. value: '1.0',
  414. ...overrides,
  415. };
  416. }
  417. export function mockMeasureEnhanced(overrides: Partial<MeasureEnhanced> = {}): MeasureEnhanced {
  418. return {
  419. bestValue: true,
  420. leak: '1',
  421. metric: mockMetric({ ...(overrides.metric || {}) }),
  422. period: {
  423. bestValue: true,
  424. index: 1,
  425. value: '1.0',
  426. },
  427. value: '1.0',
  428. ...overrides,
  429. };
  430. }
  431. export function mockNotification(overrides: Partial<Notification> = {}): Notification {
  432. return {
  433. channel: 'channel1',
  434. type: 'type-global',
  435. project: 'foo',
  436. projectName: 'Foo',
  437. ...overrides,
  438. };
  439. }
  440. export function mockPeriod(overrides: Partial<Period> = {}): Period {
  441. return {
  442. date: '2019-04-23T02:12:32+0100',
  443. index: 0,
  444. mode: 'previous_version',
  445. ...overrides,
  446. };
  447. }
  448. export function mockQualityProfile(overrides: Partial<Profile> = {}): Profile {
  449. return {
  450. activeDeprecatedRuleCount: 2,
  451. activeRuleCount: 10,
  452. childrenCount: 0,
  453. depth: 1,
  454. isBuiltIn: false,
  455. isDefault: false,
  456. isInherited: false,
  457. key: 'key',
  458. language: 'js',
  459. languageName: 'JavaScript',
  460. name: 'name',
  461. projectCount: 3,
  462. ...overrides,
  463. };
  464. }
  465. export function mockCompareResult(overrides: Partial<CompareResponse> = {}): CompareResponse {
  466. return {
  467. left: { name: 'Profile A' },
  468. right: { name: 'Profile B' },
  469. inLeft: [
  470. {
  471. key: 'java:S4604',
  472. name: 'Rule in left',
  473. cleanCodeAttributeCategory: CleanCodeAttributeCategory.Adaptable,
  474. impacts: [
  475. {
  476. softwareQuality: SoftwareQuality.Maintainability,
  477. severity: SoftwareImpactSeverity.Medium,
  478. },
  479. ],
  480. },
  481. ],
  482. inRight: [
  483. {
  484. key: 'java:S5128',
  485. name: 'Rule in right',
  486. cleanCodeAttributeCategory: CleanCodeAttributeCategory.Responsible,
  487. impacts: [
  488. {
  489. softwareQuality: SoftwareQuality.Security,
  490. severity: SoftwareImpactSeverity.Medium,
  491. },
  492. ],
  493. },
  494. ],
  495. modified: [
  496. {
  497. impacts: [],
  498. key: 'java:S1698',
  499. name: '== and != should not be used when equals is overridden',
  500. left: { params: {}, severity: 'MINOR' },
  501. right: { params: {}, severity: 'CRITICAL' },
  502. },
  503. ],
  504. ...overrides,
  505. };
  506. }
  507. export function mockQualityProfileChangelogEvent(
  508. eventOverride?: Partial<ProfileChangelogEvent>,
  509. ): ProfileChangelogEvent {
  510. return {
  511. action: 'ACTIVATED',
  512. date: '2019-04-23T02:12:32+0100',
  513. params: {
  514. severity: IssueSeverity.Major,
  515. },
  516. cleanCodeAttributeCategory: CleanCodeAttributeCategory.Responsible,
  517. impacts: [
  518. {
  519. softwareQuality: SoftwareQuality.Maintainability,
  520. severity: SoftwareImpactSeverity.Low,
  521. },
  522. {
  523. softwareQuality: SoftwareQuality.Security,
  524. severity: SoftwareImpactSeverity.High,
  525. },
  526. ],
  527. ruleKey: 'rule-key',
  528. ruleName: 'rule-name',
  529. sonarQubeVersion: '10.3',
  530. ...eventOverride,
  531. };
  532. }
  533. export function mockQualityProfileExporter(override?: Partial<Exporter>): Exporter {
  534. return {
  535. key: 'exporter-key',
  536. name: 'exporter-name',
  537. languages: ['first-lang', 'second-lang'],
  538. ...override,
  539. };
  540. }
  541. export function mockRouter(
  542. overrides: {
  543. push?: (loc: To) => void;
  544. replace?: (loc: To) => void;
  545. } = {},
  546. ) {
  547. return {
  548. createHref: jest.fn(),
  549. createPath: jest.fn(),
  550. go: jest.fn(),
  551. goBack: jest.fn(),
  552. goForward: jest.fn(),
  553. isActive: jest.fn(),
  554. push: jest.fn(),
  555. replace: jest.fn(),
  556. setRouteLeaveHook: jest.fn(),
  557. ...overrides,
  558. } as Router;
  559. }
  560. export function mockRule(overrides: Partial<Rule> = {}): Rule {
  561. return {
  562. key: 'javascript:S1067',
  563. lang: 'js',
  564. langName: 'JavaScript',
  565. name: 'Use foo',
  566. severity: 'MAJOR',
  567. status: 'READY',
  568. sysTags: ['a', 'b'],
  569. tags: ['x'],
  570. type: 'CODE_SMELL',
  571. ...overrides,
  572. } as Rule;
  573. }
  574. export function mockRuleActivation(overrides: Partial<RuleActivation> = {}): RuleActivation {
  575. return {
  576. createdAt: '2020-02-01',
  577. inherit: 'NONE',
  578. params: [{ key: 'foo', value: 'Bar' }],
  579. qProfile: 'baz',
  580. severity: 'MAJOR',
  581. ...overrides,
  582. };
  583. }
  584. export function mockRuleDetails(overrides: Partial<RuleDetails> = {}): RuleDetails {
  585. return {
  586. cleanCodeAttributeCategory: CleanCodeAttributeCategory.Adaptable,
  587. cleanCodeAttribute: CleanCodeAttribute.Clear,
  588. key: 'squid:S1337',
  589. repo: 'squid',
  590. name: '".equals()" should not be used to test the values of "Atomic" classes',
  591. createdAt: '2014-12-16T17:26:54+0100',
  592. descriptionSections: [
  593. {
  594. key: RuleDescriptionSections.DEFAULT,
  595. content: '<b>Why</b> Because',
  596. },
  597. ],
  598. htmlDesc: '',
  599. mdDesc: '',
  600. severity: 'MAJOR',
  601. status: 'READY',
  602. isTemplate: false,
  603. impacts: [
  604. { softwareQuality: SoftwareQuality.Maintainability, severity: SoftwareImpactSeverity.High },
  605. ],
  606. tags: [],
  607. sysTags: ['multi-threading'],
  608. lang: 'java',
  609. langName: 'Java',
  610. params: [],
  611. defaultRemFnType: 'CONSTANT_ISSUE',
  612. defaultRemFnBaseEffort: '5min',
  613. remFnType: 'CONSTANT_ISSUE',
  614. remFnBaseEffort: '5min',
  615. remFnOverloaded: false,
  616. scope: 'MAIN',
  617. isExternal: false,
  618. type: 'BUG',
  619. ...overrides,
  620. };
  621. }
  622. export function mockRuleDetailsParameter(overrides: Partial<RuleParameter> = {}): RuleParameter {
  623. return {
  624. defaultValue: '1',
  625. htmlDesc: 'description',
  626. key: '1',
  627. type: 'number',
  628. ...overrides,
  629. };
  630. }
  631. export function mockLogs(logsLevel: LogsLevels = LogsLevels.INFO): SysInfoLogging {
  632. return { 'Logs Level': logsLevel, 'Logs Dir': '/logs' };
  633. }
  634. export function mockStandaloneSysInfo(overrides: Partial<any> = {}): SysInfoStandalone {
  635. const baseInfo = mockBaseSysInfo(overrides);
  636. return {
  637. ...baseInfo,
  638. System: {
  639. ...baseInfo.System,
  640. 'High Availability': false,
  641. 'Server ID': 'asd564-asd54a-5dsfg45',
  642. },
  643. Settings: {
  644. 'sonar.cluster.enabled': 'true',
  645. 'sonar.cluster.node.name': 'server9.example.com',
  646. },
  647. 'Web JVM State': {
  648. 'Max Memory (MB)': 1024,
  649. 'Free Memory (MB)': 111,
  650. },
  651. 'Web Database Connection': {
  652. 'Pool Active Connections': 0,
  653. 'Pool Max Connections': 60,
  654. },
  655. 'Web Logging': mockLogs(),
  656. 'Web JVM Properties': {
  657. 'file.encoding': 'UTF-8',
  658. 'file.separator': '/',
  659. },
  660. 'Compute Engine Tasks': {
  661. Pending: 0,
  662. 'In Progress': 0,
  663. },
  664. 'Compute Engine JVM State': {
  665. 'Max Memory (MB)': 1024,
  666. 'Free Memory (MB)': 89,
  667. },
  668. 'Compute Engine Database Connection': {
  669. 'Pool Initial Size': 0,
  670. 'Pool Active Connections': 0,
  671. },
  672. 'Compute Engine Logging': mockLogs(),
  673. 'Compute Engine JVM Properties': {
  674. 'file.encoding': 'UTF-8',
  675. 'file.separator': '/',
  676. },
  677. ALMs: {},
  678. Bundled: {},
  679. Plugins: {},
  680. ...overrides,
  681. };
  682. }
  683. export function mockUser(overrides: Partial<User> = {}): User {
  684. return {
  685. active: true,
  686. local: true,
  687. login: 'john.doe',
  688. name: 'John Doe',
  689. managed: false,
  690. ...overrides,
  691. };
  692. }
  693. export function mockRestUser(overrides: Partial<RestUserDetailed> = {}): RestUserDetailed {
  694. return {
  695. id: Math.random().toString(),
  696. login: 'buzz.aldrin',
  697. name: 'Buzz Aldrin',
  698. email: 'buzz.aldrin@nasa.com',
  699. active: true,
  700. local: true,
  701. managed: false,
  702. externalProvider: '',
  703. externalLogin: '',
  704. sonarQubeLastConnectionDate: null,
  705. sonarLintLastConnectionDate: null,
  706. scmAccounts: [],
  707. avatar: 'buzzonthemoon',
  708. ...overrides,
  709. };
  710. }
  711. export function mockUserSelected(overrides: Partial<UserSelected> = {}): UserSelected {
  712. return {
  713. active: true,
  714. login: 'john.doe',
  715. name: 'John Doe',
  716. selected: true,
  717. ...overrides,
  718. };
  719. }
  720. export function mockUserGroupMember(overrides: Partial<UserGroupMember> = {}): UserGroupMember {
  721. return {
  722. login: 'john.doe',
  723. name: 'John Doe',
  724. managed: false,
  725. selected: true,
  726. ...overrides,
  727. };
  728. }
  729. export function mockDocumentationMarkdown(
  730. overrides: Partial<{ content: string; title: string; key: string }> = {},
  731. ): string {
  732. const content =
  733. overrides.content ||
  734. `
  735. ## Lorem Ipsum
  736. Donec at est elit. In finibus justo ut augue rhoncus, vitae consequat mauris mattis.
  737. Nunc ante est, volutpat ac volutpat ac, pharetra in libero.
  738. `;
  739. const frontMatter = `
  740. ---
  741. ${overrides.title ? 'title: ' + overrides.title : ''}
  742. ${overrides.key ? 'key: ' + overrides.key : ''}
  743. ---`;
  744. return `${frontMatter}
  745. ${content}`;
  746. }
  747. export function mockLanguage(overrides: Partial<Language> = {}): Language {
  748. return {
  749. key: 'css',
  750. name: 'CSS',
  751. ...overrides,
  752. };
  753. }
  754. export function mockFlowLocation(overrides: Partial<FlowLocation> = {}): FlowLocation {
  755. return {
  756. component: 'main.js',
  757. textRange: {
  758. startLine: 1,
  759. startOffset: 1,
  760. endLine: 2,
  761. endOffset: 2,
  762. },
  763. ...overrides,
  764. };
  765. }
  766. export function mockIdentityProvider(overrides: Partial<IdentityProvider> = {}): IdentityProvider {
  767. return {
  768. backgroundColor: '#000000',
  769. iconPath: '/path/icon.svg',
  770. key: 'github',
  771. name: 'Github',
  772. ...overrides,
  773. };
  774. }
  775. export function mockRef(
  776. overrides: Partial<React.RefObject<Partial<HTMLElement>>> = {},
  777. ): React.RefObject<HTMLElement> {
  778. return {
  779. current: {
  780. getBoundingClientRect: jest.fn(),
  781. ...overrides.current,
  782. },
  783. } as React.RefObject<HTMLElement>;
  784. }
  785. export function mockPaging(overrides: Partial<Paging> = {}): Paging {
  786. return {
  787. pageIndex: 1,
  788. pageSize: 100,
  789. total: 1000,
  790. ...overrides,
  791. };
  792. }
  793. export function mockDumpTask(props: Partial<DumpTask> = {}): DumpTask {
  794. return {
  795. status: TaskStatuses.Success,
  796. startedAt: '2020-03-12T12:20:20Z',
  797. submittedAt: '2020-03-12T12:15:20Z',
  798. executedAt: '2020-03-12T12:22:20Z',
  799. ...props,
  800. };
  801. }
  802. export function mockDumpStatus(props: Partial<DumpStatus> = {}): DumpStatus {
  803. return {
  804. canBeExported: true,
  805. canBeImported: true,
  806. dumpToImport: '',
  807. exportedDump: '',
  808. ...props,
  809. };
  810. }
  811. export function mockRuleRepository(override: Partial<RuleRepository> = {}) {
  812. return { key: 'css', language: 'css', name: 'SonarQube', ...override };
  813. }