AmRoutesTable.test.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { MatcherOperator } from 'app/plugins/datasource/alertmanager/types';
  2. import { FormAmRoute } from '../../types/amroutes';
  3. import { MatcherFieldValue } from '../../types/silence-form';
  4. import { deleteRoute, getFilteredRoutes, updatedRoute } from './AmRoutesTable';
  5. const defaultAmRoute: FormAmRoute = {
  6. id: '',
  7. object_matchers: [],
  8. continue: false,
  9. receiver: '',
  10. overrideGrouping: false,
  11. groupBy: [],
  12. overrideTimings: false,
  13. groupWaitValue: '',
  14. groupWaitValueType: '',
  15. groupIntervalValue: '',
  16. groupIntervalValueType: '',
  17. repeatIntervalValue: '',
  18. repeatIntervalValueType: '',
  19. muteTimeIntervals: [],
  20. routes: [],
  21. };
  22. const buildAmRoute = (override: Partial<FormAmRoute> = {}): FormAmRoute => {
  23. return { ...defaultAmRoute, ...override };
  24. };
  25. const buildMatcher = (name: string, value: string, operator: MatcherOperator): MatcherFieldValue => {
  26. return { name, value, operator };
  27. };
  28. describe('getFilteredRoutes', () => {
  29. it('Shoult return all entries when filters are empty', () => {
  30. // Arrange
  31. const routes: FormAmRoute[] = [buildAmRoute({ id: '1' }), buildAmRoute({ id: '2' }), buildAmRoute({ id: '3' })];
  32. // Act
  33. const filteredRoutes = getFilteredRoutes(routes, undefined, undefined);
  34. // Assert
  35. expect(filteredRoutes).toHaveLength(3);
  36. expect(filteredRoutes).toContain(routes[0]);
  37. expect(filteredRoutes).toContain(routes[1]);
  38. expect(filteredRoutes).toContain(routes[2]);
  39. });
  40. it('Should only return entries matching provided label query', () => {
  41. // Arrange
  42. const routes: FormAmRoute[] = [
  43. buildAmRoute({ id: '1' }),
  44. buildAmRoute({ id: '2', object_matchers: [buildMatcher('severity', 'critical', MatcherOperator.equal)] }),
  45. buildAmRoute({ id: '3' }),
  46. ];
  47. // Act
  48. const filteredRoutes = getFilteredRoutes(routes, 'severity=critical', undefined);
  49. // Assert
  50. expect(filteredRoutes).toHaveLength(1);
  51. expect(filteredRoutes).toContain(routes[1]);
  52. });
  53. it('Should only return entries matching provided contact query', () => {
  54. // Arrange
  55. const routes: FormAmRoute[] = [
  56. buildAmRoute({ id: '1' }),
  57. buildAmRoute({ id: '2', receiver: 'TestContactPoint' }),
  58. buildAmRoute({ id: '3' }),
  59. ];
  60. // Act
  61. const filteredRoutes = getFilteredRoutes(routes, undefined, 'contact');
  62. // Assert
  63. expect(filteredRoutes).toHaveLength(1);
  64. expect(filteredRoutes).toContain(routes[1]);
  65. });
  66. it('Should only return entries matching provided label and contact query', () => {
  67. // Arrange
  68. const routes: FormAmRoute[] = [
  69. buildAmRoute({ id: '1' }),
  70. buildAmRoute({
  71. id: '2',
  72. receiver: 'TestContactPoint',
  73. object_matchers: [buildMatcher('severity', 'critical', MatcherOperator.equal)],
  74. }),
  75. buildAmRoute({ id: '3' }),
  76. ];
  77. // Act
  78. const filteredRoutes = getFilteredRoutes(routes, 'severity=critical', 'contact');
  79. // Assert
  80. expect(filteredRoutes).toHaveLength(1);
  81. expect(filteredRoutes).toContain(routes[1]);
  82. });
  83. it('Should return entries matching regex matcher configuration without regex evaluation', () => {
  84. // Arrange
  85. const routes: FormAmRoute[] = [
  86. buildAmRoute({ id: '1' }),
  87. buildAmRoute({ id: '2', object_matchers: [buildMatcher('severity', 'critical', MatcherOperator.equal)] }),
  88. buildAmRoute({ id: '3', object_matchers: [buildMatcher('severity', 'crit', MatcherOperator.regex)] }),
  89. ];
  90. // Act
  91. const filteredRoutes = getFilteredRoutes(routes, 'severity=~crit', undefined);
  92. // Assert
  93. expect(filteredRoutes).toHaveLength(1);
  94. expect(filteredRoutes).toContain(routes[2]);
  95. });
  96. });
  97. describe('updatedRoute', () => {
  98. it('Should update an item of the same id', () => {
  99. // Arrange
  100. const routes: FormAmRoute[] = [buildAmRoute({ id: '1' }), buildAmRoute({ id: '2' }), buildAmRoute({ id: '3' })];
  101. const routeUpdate: FormAmRoute = {
  102. ...routes[1],
  103. object_matchers: [buildMatcher('severity', 'critical', MatcherOperator.equal)],
  104. };
  105. // Act
  106. const updatedRoutes = updatedRoute(routes, routeUpdate);
  107. // Assert
  108. expect(updatedRoutes).toHaveLength(3);
  109. const changedRoute = updatedRoutes[1];
  110. expect(changedRoute.object_matchers).toHaveLength(1);
  111. expect(changedRoute.object_matchers[0].name).toBe('severity');
  112. expect(changedRoute.object_matchers[0].value).toBe('critical');
  113. expect(changedRoute.object_matchers[0].operator).toBe(MatcherOperator.equal);
  114. });
  115. it('Should not update any element when an element of matching id not found', () => {
  116. // Arrange
  117. const routes: FormAmRoute[] = [buildAmRoute({ id: '1' }), buildAmRoute({ id: '2' }), buildAmRoute({ id: '3' })];
  118. const routeUpdate: FormAmRoute = {
  119. ...routes[1],
  120. id: '-1',
  121. object_matchers: [buildMatcher('severity', 'critical', MatcherOperator.equal)],
  122. };
  123. // Act
  124. const updatedRoutes = updatedRoute(routes, routeUpdate);
  125. // Assert
  126. expect(updatedRoutes).toHaveLength(3);
  127. updatedRoutes.forEach((route) => {
  128. expect(route.object_matchers).toHaveLength(0);
  129. });
  130. });
  131. });
  132. describe('deleteRoute', () => {
  133. it('Should delete an element of the same id', () => {
  134. // Arrange
  135. const routes: FormAmRoute[] = [buildAmRoute({ id: '1' }), buildAmRoute({ id: '2' }), buildAmRoute({ id: '3' })];
  136. const routeToDelete = routes[1];
  137. // Act
  138. const updatedRoutes = deleteRoute(routes, routeToDelete.id);
  139. // Assert
  140. expect(updatedRoutes).toHaveLength(2);
  141. expect(updatedRoutes[0].id).toBe('1');
  142. expect(updatedRoutes[1].id).toBe('3');
  143. });
  144. it('Should not delete anything when an element of matching id not found', () => {
  145. // Arrange
  146. const routes: FormAmRoute[] = [buildAmRoute({ id: '1' }), buildAmRoute({ id: '2' }), buildAmRoute({ id: '3' })];
  147. // Act
  148. const updatedRoutes = deleteRoute(routes, '-1');
  149. // Assert
  150. expect(updatedRoutes).toHaveLength(3);
  151. expect(updatedRoutes[0].id).toBe('1');
  152. expect(updatedRoutes[1].id).toBe('2');
  153. expect(updatedRoutes[2].id).toBe('3');
  154. });
  155. });