manage_alert_notifications_controller_test.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. var helpers = require('test/helpers');
  21. describe('App.ManageAlertNotificationsController', function () {
  22. beforeEach(function () {
  23. controller = App.ManageAlertNotificationsController.create({});
  24. sinon.stub(App.ajax, 'send');
  25. });
  26. afterEach(function () {
  27. App.ajax.send.restore();
  28. });
  29. describe('#alertNotifications', function () {
  30. beforeEach(function () {
  31. sinon.stub(App.AlertNotification, 'find', function () {
  32. return [1, 2, 3];
  33. });
  34. });
  35. afterEach(function () {
  36. App.AlertNotification.find.restore();
  37. });
  38. it("should return all alert notifications if controller isLoaded", function () {
  39. controller.set('isLoaded', true);
  40. expect(controller.get('alertNotifications')).to.eql([1, 2, 3]);
  41. });
  42. it("should return [] if controller isLoaded=false", function () {
  43. controller.set('isLoaded', false);
  44. expect(controller.get('alertNotifications')).to.eql([]);
  45. });
  46. });
  47. describe('#loadAlertNotifications()', function () {
  48. it("should send ajax request and set isLoaded to false", function () {
  49. controller.set('isLoaded', true);
  50. controller.loadAlertNotifications();
  51. expect(controller.get('isLoaded')).to.be.false;
  52. expect(App.ajax.send.calledOnce).to.be.true;
  53. });
  54. });
  55. describe('#getAlertNotificationsSuccessCallback()', function () {
  56. beforeEach(function () {
  57. sinon.spy(App.alertNotificationMapper, 'map');
  58. });
  59. afterEach(function () {
  60. App.alertNotificationMapper.map.restore();
  61. });
  62. it("should call mapper and set isLoaded to true", function () {
  63. controller.set('isLoaded', false);
  64. controller.getAlertNotificationsSuccessCallback('test');
  65. expect(controller.get('isLoaded')).to.be.true;
  66. expect(App.alertNotificationMapper.map.calledWith('test')).to.be.true;
  67. });
  68. });
  69. describe('#getAlertNotificationsErrorCallback()', function () {
  70. it("should set isLoaded to true", function () {
  71. controller.set('isLoaded', false);
  72. controller.getAlertNotificationsSuccessCallback('test');
  73. expect(controller.get('isLoaded')).to.be.true;
  74. });
  75. });
  76. describe('#addAlertNotification()', function () {
  77. beforeEach(function () {
  78. sinon.stub(controller, 'showCreateEditPopup');
  79. });
  80. afterEach(function () {
  81. controller.showCreateEditPopup.restore();
  82. });
  83. it("should set value for inputFields and call showCreateEditPopup", function () {
  84. controller.set('inputFields', Em.Object.create({
  85. a: {
  86. value: '',
  87. defaultValue: 'a'
  88. },
  89. b: {
  90. value: '',
  91. defaultValue: 'b'
  92. },
  93. c: {
  94. value: '',
  95. defaultValue: 'c'
  96. },
  97. severityFilter: {
  98. value: [],
  99. defaultValue: ['OK', 'WARNING', 'CRITICAL', 'UNKNOWN']
  100. },
  101. global: {
  102. value: false
  103. }
  104. }));
  105. controller.addAlertNotification();
  106. Em.keys(controller.get('inputFields')).forEach(function (key) {
  107. expect(controller.get('inputFields.' + key + '.value')).to.equal(controller.get('inputFields.' + key + '.defaultValue'));
  108. });
  109. expect(controller.showCreateEditPopup.calledOnce).to.be.true;
  110. });
  111. });
  112. describe('#editAlertNotification()', function () {
  113. beforeEach(function () {
  114. sinon.stub(controller, 'showCreateEditPopup', Em.K);
  115. sinon.stub(controller, 'fillEditCreateInputs', Em.K);
  116. });
  117. afterEach(function () {
  118. controller.showCreateEditPopup.restore();
  119. controller.fillEditCreateInputs.restore();
  120. });
  121. it("should call fillEditCreateInputs and showCreateEditPopup", function () {
  122. controller.editAlertNotification();
  123. expect(controller.fillEditCreateInputs.calledOnce).to.be.true;
  124. expect(controller.showCreateEditPopup.calledWith(true)).to.be.true;
  125. });
  126. });
  127. describe('#fillEditCreateInputs()', function () {
  128. it("should map properties from selectedAlertNotification to inputFields (ambari.dispatch.recipients ignored)", function () {
  129. controller.set('selectedAlertNotification', Em.Object.create({
  130. name: 'test_name',
  131. global: true,
  132. description: 'test_description',
  133. groups: ['test1', 'test2'],
  134. type: 'EMAIL',
  135. alertStates: ['OK', 'UNKNOWN'],
  136. properties: {
  137. 'ambari.dispatch.recipients': [
  138. 'test1@test.test',
  139. 'test2@test.test'
  140. ],
  141. 'customName': 'customValue'
  142. }
  143. }));
  144. controller.set('inputFields', Em.Object.create({
  145. name: {
  146. value: ''
  147. },
  148. groups: {
  149. value: []
  150. },
  151. global: {
  152. value: false
  153. },
  154. method: {
  155. value: ''
  156. },
  157. email: {
  158. value: ''
  159. },
  160. severityFilter: {
  161. value: []
  162. },
  163. description: {
  164. value: ''
  165. },
  166. customProperties: [
  167. {name: 'customName', value: 'customValue1', defaultValue: 'customValue1'},
  168. {name: 'customName2', value: 'customValue1', defaultValue: 'customValue1'}
  169. ]
  170. }));
  171. controller.fillEditCreateInputs();
  172. expect(JSON.stringify(controller.get('inputFields'))).to.equal(JSON.stringify({
  173. name: {
  174. value: 'test_name'
  175. },
  176. groups: {
  177. value: ['test1', 'test2']
  178. },
  179. global: {
  180. value: true,
  181. disabled: true
  182. },
  183. method: {
  184. value: 'EMAIL'
  185. },
  186. email: {
  187. value: 'test1@test.test, test2@test.test'
  188. },
  189. severityFilter: {
  190. value: ['OK', 'UNKNOWN']
  191. },
  192. description: {
  193. value: 'test_description'
  194. },
  195. customProperties: [
  196. {name: 'customName', value: 'customValue', defaultValue: 'customValue'}
  197. ]
  198. }));
  199. });
  200. });
  201. describe("#showCreateEditPopup()", function () {
  202. before(function () {
  203. sinon.stub(App.ModalPopup, 'show', function () {
  204. return 'popup';
  205. });
  206. });
  207. after(function () {
  208. App.ModalPopup.show.restore();
  209. });
  210. it("should open popup and set popup object to createEditPopup", function () {
  211. controller.set('createEditPopup', null);
  212. controller.showCreateEditPopup();
  213. expect(App.ModalPopup.show.calledOnce).to.be.true;
  214. expect(controller.get('createEditPopup')).to.equal('popup');
  215. });
  216. });
  217. describe("#formatNotificationAPIObject()", function () {
  218. beforeEach(function(){
  219. sinon.stub(App.Cluster, 'find', function () {
  220. return {
  221. objectAt: function () {
  222. return Em.Object.create({
  223. id: 1
  224. })
  225. }
  226. }
  227. });
  228. });
  229. afterEach(function() {
  230. App.Cluster.find.restore();
  231. });
  232. it("should create object with properties from inputFields values", function () {
  233. controller.set('inputFields', Em.Object.create({
  234. name: {
  235. value: 'test_name'
  236. },
  237. groups: {
  238. value: []
  239. },
  240. global: {
  241. value: false
  242. },
  243. method: {
  244. value: 'EMAIL'
  245. },
  246. email: {
  247. value: 'test1@test.test, test2@test.test,test3@test.test , test4@test.test'
  248. },
  249. severityFilter: {
  250. value: ['OK', 'CRITICAL']
  251. },
  252. description: {
  253. value: 'test_description'
  254. },
  255. customProperties: [
  256. {name: 'n1', value: 'v1'},
  257. {name: 'n2', value: 'v2'}
  258. ]
  259. }));
  260. var result = controller.formatNotificationAPIObject();
  261. expect(result).to.eql({
  262. AlertTarget: {
  263. name: 'test_name',
  264. description: 'test_description',
  265. global: false,
  266. groups: [],
  267. notification_type: 'EMAIL',
  268. alert_states: ['OK', 'CRITICAL'],
  269. properties: {
  270. 'ambari.dispatch.recipients': [
  271. 'test1@test.test',
  272. 'test2@test.test',
  273. 'test3@test.test',
  274. 'test4@test.test'
  275. ],
  276. 'n1': 'v1',
  277. 'n2': 'v2'
  278. }
  279. }
  280. });
  281. });
  282. });
  283. describe('#createAlertNotification()', function () {
  284. it("should send ajax request", function () {
  285. controller.createAlertNotification();
  286. expect(App.ajax.send.calledOnce).to.be.true;
  287. });
  288. });
  289. describe('#createAlertNotificationSuccessCallback()', function () {
  290. beforeEach(function () {
  291. controller.set('createEditPopup', {
  292. hide: Em.K
  293. });
  294. sinon.stub(controller, 'loadAlertNotifications', Em.K);
  295. sinon.spy(controller.createEditPopup, 'hide');
  296. });
  297. afterEach(function () {
  298. controller.loadAlertNotifications.restore();
  299. controller.createEditPopup.hide.restore();
  300. });
  301. it("should call loadAlertNotifications and createEditPopup.hide", function () {
  302. controller.createAlertNotificationSuccessCallback();
  303. expect(controller.loadAlertNotifications.calledOnce).to.be.true;
  304. expect(controller.createEditPopup.hide.calledOnce).to.be.true;
  305. });
  306. });
  307. describe('#updateAlertNotification()', function () {
  308. it("should send ajax request", function () {
  309. controller.createAlertNotification();
  310. expect(App.ajax.send.calledOnce).to.be.true;
  311. });
  312. });
  313. describe('#updateAlertNotificationSuccessCallback()', function () {
  314. beforeEach(function () {
  315. controller.set('createEditPopup', {
  316. hide: Em.K
  317. });
  318. sinon.stub(controller, 'loadAlertNotifications', Em.K);
  319. sinon.spy(controller.createEditPopup, 'hide');
  320. });
  321. afterEach(function () {
  322. controller.loadAlertNotifications.restore();
  323. controller.createEditPopup.hide.restore();
  324. });
  325. it("should call loadAlertNotifications and createEditPopup.hide", function () {
  326. controller.updateAlertNotificationSuccessCallback();
  327. expect(controller.loadAlertNotifications.calledOnce).to.be.true;
  328. expect(controller.createEditPopup.hide.calledOnce).to.be.true;
  329. });
  330. });
  331. describe('#deleteAlertNotification()', function () {
  332. beforeEach(function () {
  333. sinon.spy(App, 'showConfirmationPopup');
  334. });
  335. afterEach(function () {
  336. App.showConfirmationPopup.restore();
  337. });
  338. it("should show popup and send request on confirmation", function () {
  339. var popup = controller.deleteAlertNotification();
  340. expect(App.showConfirmationPopup.calledOnce).to.be.true;
  341. popup.onPrimary();
  342. expect(App.ajax.send.calledOnce).to.be.true;
  343. });
  344. });
  345. describe('#deleteAlertNotificationSuccessCallback()', function () {
  346. it("should call loadAlertNotifications, selectedAlertNotification.deleteRecord and set null to selectedAlertNotification", function () {
  347. var mockSelectedAlertNotification = {
  348. deleteRecord: Em.K
  349. };
  350. controller.set('selectedAlertNotification', mockSelectedAlertNotification);
  351. sinon.stub(controller, 'loadAlertNotifications', Em.K);
  352. sinon.spy(mockSelectedAlertNotification, 'deleteRecord');
  353. controller.deleteAlertNotificationSuccessCallback();
  354. expect(controller.loadAlertNotifications.calledOnce).to.be.true;
  355. expect(mockSelectedAlertNotification.deleteRecord.calledOnce).to.be.true;
  356. expect(controller.get('selectedAlertNotification')).to.equal(null);
  357. controller.loadAlertNotifications.restore();
  358. mockSelectedAlertNotification.deleteRecord.restore();
  359. });
  360. });
  361. describe('#duplicateAlertNotification()', function () {
  362. beforeEach(function () {
  363. sinon.stub(controller, 'fillEditCreateInputs', Em.K);
  364. sinon.stub(controller, 'showCreateEditPopup', Em.K);
  365. });
  366. afterEach(function () {
  367. controller.fillEditCreateInputs.restore();
  368. controller.showCreateEditPopup.restore();
  369. });
  370. it("should call fillEditCreateInputs and showCreateEditPopup", function () {
  371. controller.duplicateAlertNotification();
  372. expect(controller.fillEditCreateInputs.calledWith(true)).to.be.true;
  373. expect(controller.showCreateEditPopup.calledOnce).to.be.true;
  374. });
  375. });
  376. describe('#addCustomProperty', function () {
  377. beforeEach(function () {
  378. controller.set('inputFields.customProperties', []);
  379. });
  380. it('should add custom Property to customProperties', function () {
  381. controller.set('newCustomProperty', {name: 'n1', value: 'v1'});
  382. controller.addCustomProperty();
  383. helpers.nestedExpect([{name: 'n1', value: 'v1', defaultValue: 'v1'}], controller.get('inputFields.customProperties'));
  384. });
  385. });
  386. describe('#removeCustomPropertyHandler', function () {
  387. var c = {name: 'n2', value: 'v2', defaultValue: 'v2'};
  388. beforeEach(function () {
  389. controller.set('inputFields.customProperties', [
  390. {name: 'n1', value: 'v1', defaultValue: 'v1'},
  391. c,
  392. {name: 'n3', value: 'v3', defaultValue: 'v3'}
  393. ]);
  394. });
  395. it('should remove selected custom property', function () {
  396. controller.removeCustomPropertyHandler({context: c});
  397. helpers.nestedExpect(
  398. [
  399. {name: 'n1', value: 'v1', defaultValue: 'v1'},
  400. {name: 'n3', value: 'v3', defaultValue: 'v3'}
  401. ],
  402. controller.get('inputFields.customProperties')
  403. );
  404. });
  405. });
  406. describe('#addCustomPropertyHandler', function () {
  407. it('should clean up newCustomProperty on primary click', function () {
  408. controller.set('newCustomProperty', {name: 'n1', value: 'v1'});
  409. controller.addCustomPropertyHandler().onPrimary();
  410. expect(controller.get('newCustomProperty')).to.eql({name: '', value: ''});
  411. });
  412. describe('#bodyClass', function () {
  413. var view;
  414. beforeEach(function () {
  415. view = controller.addCustomPropertyHandler().get('bodyClass').create({
  416. parentView: Em.View.create(),
  417. controller: Em.Object.create({
  418. inputFields: Em.Object.create({
  419. customProperties: [
  420. {name: 'n1', value: 'v1', defaultValue: 'v1'}
  421. ]
  422. }),
  423. newCustomProperty: {name: '', value: ''}
  424. })
  425. });
  426. });
  427. describe('#errorHandler', function () {
  428. it('should fire invalid name', function () {
  429. view.set('controller.newCustomProperty.name', '!!');
  430. view.errorsHandler();
  431. expect(view.get('isError')).to.be.true;
  432. expect(view.get('parentView.disablePrimary')).to.be.true;
  433. expect(view.get('errorMessage.length') > 0).to.be.true;
  434. });
  435. it('should fire existing property name', function () {
  436. view.set('controller.newCustomProperty.name', 'n1');
  437. view.errorsHandler();
  438. expect(view.get('isError')).to.be.true;
  439. expect(view.get('parentView.disablePrimary')).to.be.true;
  440. expect(view.get('errorMessage.length') > 0).to.be.true;
  441. });
  442. });
  443. });
  444. });
  445. });