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.

TimeMachineServiceMock.ts 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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 { chunk, cloneDeep, times } from 'lodash';
  21. import { parseDate } from '../../helpers/dates';
  22. import { mockHistoryItem, mockMeasureHistory } from '../../helpers/mocks/project-activity';
  23. import { BranchParameters } from '../../types/branch-like';
  24. import { MetricKey } from '../../types/metrics';
  25. import { MeasureHistory } from '../../types/project-activity';
  26. import { TimeMachineResponse, getAllTimeMachineData, getTimeMachineData } from '../time-machine';
  27. jest.mock('../time-machine');
  28. const PAGE_SIZE = 10;
  29. const DEFAULT_PAGE = 0;
  30. const HISTORY_COUNT = 10;
  31. const START_DATE = '2016-01-01T00:00:00.000Z';
  32. const defaultMeasureHistory = [
  33. MetricKey.bugs,
  34. MetricKey.code_smells,
  35. MetricKey.confirmed_issues,
  36. MetricKey.vulnerabilities,
  37. MetricKey.blocker_violations,
  38. MetricKey.lines_to_cover,
  39. MetricKey.uncovered_lines,
  40. MetricKey.security_hotspots_reviewed,
  41. MetricKey.coverage,
  42. MetricKey.duplicated_lines_density,
  43. MetricKey.test_success_density,
  44. ].map((metric) => {
  45. return mockMeasureHistory({
  46. metric,
  47. history: times(HISTORY_COUNT, (i) => {
  48. const date = parseDate(START_DATE);
  49. date.setDate(date.getDate() + i);
  50. return mockHistoryItem({ value: i.toString(), date });
  51. }),
  52. });
  53. });
  54. export class TimeMachineServiceMock {
  55. #measureHistory: MeasureHistory[];
  56. constructor() {
  57. this.#measureHistory = cloneDeep(defaultMeasureHistory);
  58. jest.mocked(getTimeMachineData).mockImplementation(this.handleGetTimeMachineData);
  59. jest.mocked(getAllTimeMachineData).mockImplementation(this.handleGetAllTimeMachineData);
  60. }
  61. handleGetTimeMachineData = (
  62. data: {
  63. component: string;
  64. from?: string;
  65. metrics: string;
  66. p?: number;
  67. ps?: number;
  68. to?: string;
  69. } & BranchParameters,
  70. ) => {
  71. const { ps = PAGE_SIZE, p = DEFAULT_PAGE } = data;
  72. const measureHistoryChunked = chunk(this.#measureHistory, ps);
  73. return this.reply({
  74. paging: { pageSize: ps, total: this.#measureHistory.length, pageIndex: p },
  75. measures: measureHistoryChunked[p - 1] ? this.map(measureHistoryChunked[p - 1]) : [],
  76. });
  77. };
  78. handleGetAllTimeMachineData = (
  79. data: {
  80. component: string;
  81. metrics: string;
  82. from?: string;
  83. p?: number;
  84. to?: string;
  85. } & BranchParameters,
  86. _prev?: TimeMachineResponse,
  87. ) => {
  88. const { p = DEFAULT_PAGE } = data;
  89. return this.reply({
  90. paging: { pageSize: PAGE_SIZE, total: this.#measureHistory.length, pageIndex: p },
  91. measures: this.map(this.#measureHistory),
  92. });
  93. };
  94. setMeasureHistory = (list: MeasureHistory[]) => {
  95. this.#measureHistory = list;
  96. };
  97. map = (list: MeasureHistory[]) => {
  98. return list.map((item) => ({
  99. ...item,
  100. history: item.history.map((h) => ({ ...h, date: h.date.toDateString() })),
  101. }));
  102. };
  103. reset = () => {
  104. this.#measureHistory = cloneDeep(defaultMeasureHistory);
  105. };
  106. reply<T>(response: T): Promise<T> {
  107. return Promise.resolve(cloneDeep(response));
  108. }
  109. }