alert_groups_mapper_test.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with this
  4. * work for additional information regarding copyright ownership. The ASF
  5. * licenses this file to you under the Apache License, Version 2.0 (the
  6. * "License"); you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. var App = require('app');
  18. require('mappers/alert_groups_mapper');
  19. var testHelpers = require('test/helpers');
  20. describe('App.alertGroupsMapper', function () {
  21. describe('#map', function () {
  22. var json = {
  23. items: [
  24. {
  25. "AlertGroup" : {
  26. "default" : true,
  27. "definitions" : [
  28. {
  29. "id" : 8,
  30. "source_type" : "PORT"
  31. },
  32. {
  33. "id" : 9,
  34. "source_type" : "AGGREGATE"
  35. }
  36. ],
  37. "id" : 3,
  38. "name" : "ZOOKEEPER",
  39. "targets": [{id: 1}, {id: 2}]
  40. }
  41. },
  42. {
  43. "AlertGroup" : {
  44. "default" : true,
  45. "definitions" : [
  46. {
  47. "id" : 1,
  48. "source_type" : "METRIC"
  49. },
  50. {
  51. "id" : 2,
  52. "source_type" : "WEB"
  53. },
  54. {
  55. "id" : 3,
  56. "source_type" : "WEB"
  57. },
  58. {
  59. "id" : 4,
  60. "source_type" : "AGGREGATE"
  61. },
  62. {
  63. "id" : 5,
  64. "source_type" : "METRIC"
  65. },
  66. {
  67. "id" : 6,
  68. "source_type" : "SCRIPT"
  69. },
  70. {
  71. "id" : 7,
  72. "source_type" : "WEB"
  73. }
  74. ],
  75. "id" : 2,
  76. "name" : "YARN",
  77. "targets": [{id: 2}, {id: 3}]
  78. }
  79. }
  80. ]
  81. };
  82. beforeEach(function () {
  83. sinon.stub(App.store, 'commit', Em.K);
  84. sinon.stub(App.store, 'loadMany', function (type, content) {
  85. type.content = content;
  86. });
  87. App.alertGroupsMapper.set('model', {});
  88. App.cache['previousAlertGroupsMap'] = {};
  89. });
  90. afterEach(function () {
  91. App.store.commit.restore();
  92. App.store.loadMany.restore();
  93. App.alertGroupsMapper.set('model', App.AlertGroup);
  94. App.cache['previousAlertGroupsMap'] = {};
  95. });
  96. it('should parse alert groups', function() {
  97. var expected = [
  98. {
  99. id: 3,
  100. name: 'ZOOKEEPER',
  101. default: true,
  102. port_alert_definitions: [8],
  103. metrics_alert_definitions: [],
  104. web_alert_definitions: [],
  105. aggregate_alert_definitions: [9],
  106. script_alert_definitions: [],
  107. targets: [1, 2]
  108. },
  109. {
  110. id: 2,
  111. name: 'YARN',
  112. default: true,
  113. port_alert_definitions: [],
  114. metrics_alert_definitions: [1, 5],
  115. web_alert_definitions: [2, 3, 7],
  116. aggregate_alert_definitions: [4],
  117. script_alert_definitions: [6],
  118. targets: [2, 3]
  119. }
  120. ];
  121. App.alertGroupsMapper.map(json);
  122. var mapped = App.alertGroupsMapper.get('model.content');
  123. testHelpers.nestedExpect(expected, mapped);
  124. });
  125. it('should set App.cache.previousAlertGroupsMap', function () {
  126. var expected = {
  127. 8: [3],
  128. 9: [3],
  129. 1: [2],
  130. 2: [2],
  131. 3: [2],
  132. 4: [2],
  133. 5: [2],
  134. 6: [2],
  135. 7: [2]
  136. };
  137. App.alertGroupsMapper.map(json);
  138. expect(App.cache['previousAlertGroupsMap']).to.eql(expected);
  139. });
  140. describe('should delete not existing groups', function () {
  141. var groups = [
  142. {id: 1},
  143. {id: 2},
  144. {id: 3},
  145. {id: 4}
  146. ];
  147. beforeEach(function () {
  148. sinon.stub(App.AlertGroup, 'find', function() {
  149. if (arguments.length) {
  150. return groups.findProperty('id', arguments[0]);
  151. }
  152. return groups;
  153. });
  154. sinon.stub(App.alertGroupsMapper, 'deleteRecord', Em.K);
  155. });
  156. afterEach(function () {
  157. App.AlertGroup.find.restore();
  158. App.alertGroupsMapper.deleteRecord.restore();
  159. });
  160. it('should call deleteRecord with not existing groups', function () {
  161. App.alertGroupsMapper.map(json);
  162. expect(App.alertGroupsMapper.deleteRecord.calledTwice).to.be.true;
  163. // first call
  164. expect(App.alertGroupsMapper.deleteRecord.args[0][0].id).to.equal(1);
  165. // second call
  166. expect(App.alertGroupsMapper.deleteRecord.args[1][0].id).to.equal(4);
  167. });
  168. });
  169. });
  170. });