manage_alert_notifications_controller_test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. var controller;
  20. describe('App.ManageAlertNotificationsController', function () {
  21. beforeEach(function () {
  22. controller = App.ManageAlertNotificationsController.create({});
  23. sinon.spy(App.ajax, 'send');
  24. });
  25. afterEach(function () {
  26. App.ajax.send.restore();
  27. });
  28. describe('#alertNotifications', function () {
  29. beforeEach(function () {
  30. sinon.stub(App.AlertNotification, 'find', function () {
  31. return [1, 2, 3];
  32. });
  33. });
  34. afterEach(function () {
  35. App.AlertNotification.find.restore();
  36. });
  37. it("should return all alert notifications if controller isLoaded", function () {
  38. controller.set('isLoaded', true);
  39. expect(controller.get('alertNotifications')).to.eql([1, 2, 3]);
  40. });
  41. it("should return [] if controller isLoaded=false", function () {
  42. controller.set('isLoaded', false);
  43. expect(controller.get('alertNotifications')).to.eql([]);
  44. });
  45. });
  46. describe('#loadAlertNotifications()', function () {
  47. it("should send ajax request and set isLoaded to false", function () {
  48. controller.set('isLoaded', true);
  49. controller.loadAlertNotifications();
  50. expect(controller.get('isLoaded')).to.be.false;
  51. expect(App.ajax.send.calledOnce).to.be.true;
  52. });
  53. });
  54. describe('#getAlertNotificationsSuccessCallback()', function () {
  55. beforeEach(function () {
  56. sinon.spy(App.alertNotificationMapper, 'map');
  57. });
  58. afterEach(function () {
  59. App.alertNotificationMapper.map.restore();
  60. });
  61. it("should call mapper and set isLoaded to true", function () {
  62. controller.set('isLoaded', false);
  63. controller.getAlertNotificationsSuccessCallback('test');
  64. expect(controller.get('isLoaded')).to.be.true;
  65. expect(App.alertNotificationMapper.map.calledWith('test')).to.be.true;
  66. });
  67. });
  68. describe('#getAlertNotificationsErrorCallback()', function () {
  69. it("should set isLoaded to true", function () {
  70. controller.set('isLoaded', false);
  71. controller.getAlertNotificationsSuccessCallback('test');
  72. expect(controller.get('isLoaded')).to.be.true;
  73. });
  74. });
  75. describe('#addAlertNotification()', function () {
  76. beforeEach(function () {
  77. sinon.spy(controller, 'showCreateEditPopup');
  78. });
  79. afterEach(function () {
  80. controller.showCreateEditPopup.restore();
  81. });
  82. it("should set value for inputFields and call showCreateEditPopup", function () {
  83. controller.set('inputFields', Em.Object.create({
  84. a: {
  85. value: '',
  86. defaultValue: 'a'
  87. },
  88. b: {
  89. value: '',
  90. defaultValue: 'b'
  91. },
  92. c: {
  93. value: '',
  94. defaultValue: 'c'
  95. },
  96. severityFilter: {
  97. value: [],
  98. defaultValue: [true, true, true, true]
  99. }
  100. }));
  101. controller.addAlertNotification();
  102. Em.keys(controller.get('inputFields')).forEach(function (key) {
  103. expect(controller.get('inputFields.' + key + '.value')).to.equal(controller.get('inputFields.' + key + '.defaultValue'));
  104. });
  105. expect(controller.showCreateEditPopup.calledOnce).to.be.true;
  106. });
  107. });
  108. describe('#editAlertNotification()', function () {
  109. beforeEach(function () {
  110. sinon.stub(controller, 'showCreateEditPopup', Em.K);
  111. sinon.stub(controller, 'fillEditCreateInputs', Em.K);
  112. });
  113. afterEach(function () {
  114. controller.showCreateEditPopup.restore();
  115. controller.fillEditCreateInputs.restore();
  116. });
  117. it("should call fillEditCreateInputs and showCreateEditPopup", function () {
  118. controller.editAlertNotification();
  119. expect(controller.fillEditCreateInputs.calledOnce).to.be.true;
  120. expect(controller.showCreateEditPopup.calledWith(true)).to.be.true;
  121. });
  122. });
  123. describe('#fillEditCreateInputs()', function () {
  124. it("should map properties from selectedAlertNotification to inputFields", function () {
  125. controller.set('selectedAlertNotification', Em.Object.create({
  126. name: 'test_name',
  127. description: 'test_description',
  128. type: 'EMAIL',
  129. alertStates: ['OK', 'UNKNOWN'],
  130. properties: {
  131. 'ambari.dispatch.recipients': [
  132. 'test1@test.test',
  133. 'test2@test.test'
  134. ]
  135. }
  136. }));
  137. controller.set('inputFields', Em.Object.create({
  138. name: {
  139. value: ''
  140. },
  141. groups: {
  142. value: ''
  143. },
  144. method: {
  145. value: ''
  146. },
  147. email: {
  148. value: ''
  149. },
  150. severityFilter: {
  151. value: []
  152. },
  153. description: {
  154. value: ''
  155. }
  156. }));
  157. controller.fillEditCreateInputs();
  158. expect(JSON.stringify(controller.get('inputFields'))).to.equal(JSON.stringify({
  159. name: {
  160. value: 'test_name'
  161. },
  162. groups: {
  163. value: ''
  164. },
  165. method: {
  166. value: 'EMAIL'
  167. },
  168. email: {
  169. value: 'test1@test.test, test2@test.test'
  170. },
  171. severityFilter: {
  172. value: [true, false, false, true]
  173. },
  174. description: {
  175. value: 'test_description'
  176. }
  177. }));
  178. });
  179. });
  180. describe("#showCreateEditPopup()", function () {
  181. before(function () {
  182. sinon.stub(App.ModalPopup, 'show', function () {
  183. return 'popup';
  184. });
  185. });
  186. after(function () {
  187. App.ModalPopup.show.restore();
  188. });
  189. it("should open popup and set popup object to createEditPopup", function () {
  190. controller.set('createEditPopup', null);
  191. controller.showCreateEditPopup();
  192. expect(App.ModalPopup.show.calledOnce).to.be.true;
  193. expect(controller.get('createEditPopup')).to.equal('popup');
  194. });
  195. });
  196. describe("#formatNotificationAPIObject()", function () {
  197. it("should create object with properties from inputFields values", function () {
  198. controller.set('inputFields', Em.Object.create({
  199. name: {
  200. value: 'test_name'
  201. },
  202. groups: {
  203. value: ''
  204. },
  205. method: {
  206. value: 'EMAIL'
  207. },
  208. email: {
  209. value: 'test1@test.test, test2@test.test,test3@test.test , test4@test.test'
  210. },
  211. severityFilter: {
  212. value: [true, false, true, false]
  213. },
  214. description: {
  215. value: 'test_description'
  216. }
  217. }));
  218. var result = controller.formatNotificationAPIObject();
  219. expect(result).to.eql({
  220. AlertTarget: {
  221. name: 'test_name',
  222. description: 'test_description',
  223. notification_type: 'EMAIL',
  224. alert_states: ['OK', 'CRITICAL'],
  225. properties: {
  226. 'ambari.dispatch.recipients': [
  227. 'test1@test.test',
  228. 'test2@test.test',
  229. 'test3@test.test',
  230. 'test4@test.test'
  231. ]
  232. }
  233. }
  234. });
  235. });
  236. });
  237. describe('#createAlertNotification()', function () {
  238. it("should send ajax request", function () {
  239. controller.createAlertNotification();
  240. expect(App.ajax.send.calledOnce).to.be.true;
  241. });
  242. });
  243. describe('#createAlertNotificationSuccessCallback()', function () {
  244. beforeEach(function () {
  245. controller.set('createEditPopup', {
  246. hide: Em.K
  247. });
  248. sinon.stub(controller, 'loadAlertNotifications', Em.K);
  249. sinon.spy(controller.createEditPopup, 'hide');
  250. });
  251. afterEach(function () {
  252. controller.loadAlertNotifications.restore();
  253. controller.createEditPopup.hide.restore();
  254. });
  255. it("should call loadAlertNotifications and createEditPopup.hide", function () {
  256. controller.createAlertNotificationSuccessCallback();
  257. expect(controller.loadAlertNotifications.calledOnce).to.be.true;
  258. expect(controller.createEditPopup.hide.calledOnce).to.be.true;
  259. });
  260. });
  261. describe('#updateAlertNotification()', function () {
  262. it("should send ajax request", function () {
  263. controller.createAlertNotification();
  264. expect(App.ajax.send.calledOnce).to.be.true;
  265. });
  266. });
  267. describe('#updateAlertNotificationSuccessCallback()', function () {
  268. beforeEach(function () {
  269. controller.set('createEditPopup', {
  270. hide: Em.K
  271. });
  272. sinon.stub(controller, 'loadAlertNotifications', Em.K);
  273. sinon.spy(controller.createEditPopup, 'hide');
  274. });
  275. afterEach(function () {
  276. controller.loadAlertNotifications.restore();
  277. controller.createEditPopup.hide.restore();
  278. });
  279. it("should call loadAlertNotifications and createEditPopup.hide", function () {
  280. controller.updateAlertNotificationSuccessCallback();
  281. expect(controller.loadAlertNotifications.calledOnce).to.be.true;
  282. expect(controller.createEditPopup.hide.calledOnce).to.be.true;
  283. });
  284. });
  285. describe('#deleteAlertNotification()', function () {
  286. beforeEach(function () {
  287. sinon.spy(App, 'showConfirmationPopup');
  288. });
  289. afterEach(function () {
  290. App.showConfirmationPopup.restore();
  291. });
  292. it("should show popup and send request on confirmation", function () {
  293. var popup = controller.deleteAlertNotification();
  294. expect(App.showConfirmationPopup.calledOnce).to.be.true;
  295. popup.onPrimary();
  296. expect(App.ajax.send.calledOnce).to.be.true;
  297. });
  298. });
  299. describe('#deleteAlertNotificationSuccessCallback()', function () {
  300. it("should call loadAlertNotifications, selectedAlertNotification.deleteRecord and set null to selectedAlertNotification", function () {
  301. var mockSelectedAlertNotification = {
  302. deleteRecord: Em.K
  303. };
  304. controller.set('selectedAlertNotification', mockSelectedAlertNotification);
  305. sinon.stub(controller, 'loadAlertNotifications', Em.K);
  306. sinon.spy(mockSelectedAlertNotification, 'deleteRecord');
  307. controller.deleteAlertNotificationSuccessCallback();
  308. expect(controller.loadAlertNotifications.calledOnce).to.be.true;
  309. expect(mockSelectedAlertNotification.deleteRecord.calledOnce).to.be.true;
  310. expect(controller.get('selectedAlertNotification')).to.equal(null);
  311. controller.loadAlertNotifications.restore();
  312. mockSelectedAlertNotification.deleteRecord.restore();
  313. });
  314. });
  315. describe('#duplicateAlertNotification()', function () {
  316. beforeEach(function () {
  317. sinon.stub(controller, 'fillEditCreateInputs', Em.K);
  318. sinon.stub(controller, 'showCreateEditPopup', Em.K);
  319. });
  320. afterEach(function () {
  321. controller.fillEditCreateInputs.restore();
  322. controller.showCreateEditPopup.restore();
  323. });
  324. it("should call fillEditCreateInputs and showCreateEditPopup", function () {
  325. controller.duplicateAlertNotification();
  326. expect(controller.fillEditCreateInputs.calledWith(true)).to.be.true;
  327. expect(controller.showCreateEditPopup.calledOnce).to.be.true;
  328. });
  329. });
  330. });