alert_groups_mapper_test.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. /*eslint-disable mocha-cleanup/asserts-limit */
  97. it('should parse alert groups', function() {
  98. var expected = [
  99. {
  100. id: 3,
  101. name: 'ZOOKEEPER',
  102. default: true,
  103. definitions: [8,9],
  104. targets: [1, 2]
  105. },
  106. {
  107. id: 2,
  108. name: 'YARN',
  109. default: true,
  110. definitions: [1, 2, 3, 4, 5, 6, 7],
  111. targets: [2, 3]
  112. }
  113. ];
  114. App.alertGroupsMapper.map(json);
  115. var mapped = App.alertGroupsMapper.get('model.content');
  116. testHelpers.nestedExpect(expected, mapped);
  117. });
  118. /*eslint-enable mocha-cleanup/asserts-limit */
  119. it('should set App.cache.previousAlertGroupsMap', function () {
  120. var expected = {
  121. 8: [3],
  122. 9: [3],
  123. 1: [2],
  124. 2: [2],
  125. 3: [2],
  126. 4: [2],
  127. 5: [2],
  128. 6: [2],
  129. 7: [2]
  130. };
  131. App.alertGroupsMapper.map(json);
  132. expect(App.cache.previousAlertGroupsMap).to.eql(expected);
  133. });
  134. describe('should delete not existing groups', function () {
  135. var groups = [
  136. {id: 1},
  137. {id: 2},
  138. {id: 3},
  139. {id: 4}
  140. ];
  141. beforeEach(function () {
  142. sinon.stub(App.AlertGroup, 'find', function() {
  143. if (arguments.length) {
  144. return groups.findProperty('id', arguments[0]);
  145. }
  146. return groups;
  147. });
  148. sinon.stub(App.alertGroupsMapper, 'deleteRecord', Em.K);
  149. });
  150. afterEach(function () {
  151. App.AlertGroup.find.restore();
  152. App.alertGroupsMapper.deleteRecord.restore();
  153. });
  154. it('should call deleteRecord with not existing groups', function () {
  155. App.alertGroupsMapper.map(json);
  156. expect(App.alertGroupsMapper.deleteRecord.calledTwice).to.be.true;
  157. // first call
  158. expect(App.alertGroupsMapper.deleteRecord.args[0][0].id).to.equal(1);
  159. // second call
  160. expect(App.alertGroupsMapper.deleteRecord.args[1][0].id).to.equal(4);
  161. });
  162. });
  163. });
  164. });