manage_alert_notifications_controller_test.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. });
  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.stub(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: ['OK', 'WARNING', 'CRITICAL', 'UNKNOWN']
  99. },
  100. global: {
  101. value: false
  102. }
  103. }));
  104. controller.addAlertNotification();
  105. Em.keys(controller.get('inputFields')).forEach(function (key) {
  106. expect(controller.get('inputFields.' + key + '.value')).to.equal(controller.get('inputFields.' + key + '.defaultValue'));
  107. });
  108. expect(controller.showCreateEditPopup.calledOnce).to.be.true;
  109. });
  110. });
  111. describe('#editAlertNotification()', function () {
  112. beforeEach(function () {
  113. sinon.stub(controller, 'showCreateEditPopup', Em.K);
  114. sinon.stub(controller, 'fillEditCreateInputs', Em.K);
  115. });
  116. afterEach(function () {
  117. controller.showCreateEditPopup.restore();
  118. controller.fillEditCreateInputs.restore();
  119. });
  120. it("should call fillEditCreateInputs and showCreateEditPopup", function () {
  121. controller.editAlertNotification();
  122. expect(controller.fillEditCreateInputs.calledOnce).to.be.true;
  123. expect(controller.showCreateEditPopup.calledWith(true)).to.be.true;
  124. });
  125. });
  126. describe('#fillEditCreateInputs()', function () {
  127. it("should map properties from selectedAlertNotification to inputFields (ambari.dispatch.recipients ignored)", function () {
  128. controller.set('selectedAlertNotification', Em.Object.create({
  129. name: 'test_name',
  130. global: true,
  131. description: 'test_description',
  132. groups: ['test1', 'test2'],
  133. type: 'EMAIL',
  134. alertStates: ['OK', 'UNKNOWN'],
  135. properties: {
  136. 'ambari.dispatch.recipients': [
  137. 'test1@test.test',
  138. 'test2@test.test'
  139. ],
  140. 'customName': 'customValue'
  141. }
  142. }));
  143. controller.set('inputFields', Em.Object.create({
  144. name: {
  145. value: ''
  146. },
  147. groups: {
  148. value: []
  149. },
  150. global: {
  151. value: false
  152. },
  153. method: {
  154. value: ''
  155. },
  156. email: {
  157. value: ''
  158. },
  159. severityFilter: {
  160. value: []
  161. },
  162. description: {
  163. value: ''
  164. },
  165. SMTPServer: {
  166. value: ''
  167. },
  168. SMTPPort: {
  169. value: ''
  170. },
  171. emailFrom: {
  172. value: ''
  173. },
  174. version: {
  175. value: ''
  176. },
  177. OIDs: {
  178. value: ''
  179. },
  180. community: {
  181. value: ''
  182. },
  183. port: {
  184. value: ''
  185. },
  186. customProperties: [
  187. {name: 'customName', value: 'customValue1', defaultValue: 'customValue1'},
  188. {name: 'customName2', value: 'customValue1', defaultValue: 'customValue1'}
  189. ]
  190. }));
  191. controller.fillEditCreateInputs();
  192. expect(JSON.stringify(controller.get('inputFields'))).to.equal(JSON.stringify({
  193. name: {
  194. value: 'test_name'
  195. },
  196. groups: {
  197. value: ['test1', 'test2']
  198. },
  199. global: {
  200. value: true,
  201. disabled: true
  202. },
  203. method: {
  204. value: 'EMAIL'
  205. },
  206. email: {
  207. value: 'test1@test.test, test2@test.test'
  208. },
  209. severityFilter: {
  210. value: ['OK', 'UNKNOWN']
  211. },
  212. description: {
  213. value: 'test_description'
  214. },
  215. SMTPServer: {},
  216. SMTPPort: {},
  217. emailFrom: {},
  218. version: {},
  219. OIDs: {},
  220. community: {},
  221. port: {},
  222. customProperties: [
  223. {name: 'customName', value: 'customValue', defaultValue: 'customValue'}
  224. ]
  225. }));
  226. });
  227. });
  228. describe("#showCreateEditPopup()", function () {
  229. beforeEach(function () {
  230. sinon.spy(App.ModalPopup, 'show');
  231. });
  232. afterEach(function () {
  233. App.ModalPopup.show.restore();
  234. });
  235. it("should open popup and set popup object to createEditPopup", function () {
  236. controller.showCreateEditPopup();
  237. expect(App.ModalPopup.show.calledOnce).to.be.true;
  238. });
  239. describe('#bodyClass', function () {
  240. var view;
  241. beforeEach(function () {
  242. view = controller.showCreateEditPopup().get('bodyClass').create({
  243. controller: Em.Object.create({
  244. inputFields: {
  245. global: {}
  246. }
  247. }),
  248. groupSelect: Em.Object.create({
  249. selection: [],
  250. content: [{}, {}]
  251. })
  252. });
  253. });
  254. describe('#selectAllGroups', function () {
  255. it('should check inputFields.global.value', function () {
  256. view.set('controller.inputFields.global.value', true);
  257. view.selectAllGroups();
  258. expect(view.get('groupSelect.selection')).to.eql([]);
  259. view.set('controller.inputFields.global.value', false);
  260. view.selectAllGroups();
  261. expect(view.get('groupSelect.selection')).to.eql([{}, {}]);
  262. });
  263. });
  264. describe('#clearAllGroups', function () {
  265. it('should check inputFields.global.value', function () {
  266. view.set('controller.inputFields.global.value', false);
  267. view.selectAllGroups();
  268. view.set('controller.inputFields.global.value', true);
  269. view.clearAllGroups();
  270. expect(view.get('groupSelect.selection')).to.eql([{}, {}]);
  271. view.set('controller.inputFields.global.value', false);
  272. view.clearAllGroups();
  273. expect(view.get('groupSelect.selection')).to.eql([]);
  274. });
  275. });
  276. });
  277. });
  278. describe("#formatNotificationAPIObject()", function () {
  279. var inputFields = Em.Object.create({
  280. name: {
  281. value: 'test_name'
  282. },
  283. groups: {
  284. value: [{id: 1}, {id: 2}, {id: 3}]
  285. },
  286. global: {
  287. value: false
  288. },
  289. method: {
  290. value: 'EMAIL'
  291. },
  292. email: {
  293. value: 'test1@test.test, test2@test.test,test3@test.test , test4@test.test'
  294. },
  295. severityFilter: {
  296. value: ['OK', 'CRITICAL']
  297. },
  298. description: {
  299. value: 'test_description'
  300. },
  301. customProperties: [
  302. {name: 'n1', value: 'v1'},
  303. {name: 'n2', value: 'v2'}
  304. ]
  305. });
  306. it("should create object with properties from inputFields values", function () {
  307. controller.set('inputFields', inputFields);
  308. var result = controller.formatNotificationAPIObject();
  309. expect(JSON.stringify(result)).to.eql(JSON.stringify({
  310. AlertTarget: {
  311. name: 'test_name',
  312. description: 'test_description',
  313. global: false,
  314. notification_type: 'EMAIL',
  315. alert_states: ['OK', 'CRITICAL'],
  316. properties: {
  317. 'ambari.dispatch.recipients': [
  318. 'test1@test.test',
  319. 'test2@test.test',
  320. 'test3@test.test',
  321. 'test4@test.test'
  322. ],
  323. 'n1': 'v1',
  324. 'n2': 'v2'
  325. },
  326. groups: [1,2,3]
  327. }
  328. }));
  329. });
  330. it('should ignore groups if global is true', function () {
  331. controller.set('inputFields', inputFields);
  332. controller.set('inputFields.global.value', true);
  333. var result = controller.formatNotificationAPIObject();
  334. expect(Em.keys(result.AlertTarget)).to.not.contain('groups');
  335. });
  336. });
  337. describe('#createAlertNotification()', function () {
  338. it("should send ajax request", function () {
  339. controller.createAlertNotification();
  340. expect(App.ajax.send.calledOnce).to.be.true;
  341. });
  342. });
  343. describe('#createAlertNotificationSuccessCallback()', function () {
  344. beforeEach(function () {
  345. controller.set('createEditPopup', {
  346. hide: Em.K
  347. });
  348. sinon.stub(controller, 'loadAlertNotifications', Em.K);
  349. sinon.spy(controller.createEditPopup, 'hide');
  350. });
  351. afterEach(function () {
  352. controller.loadAlertNotifications.restore();
  353. controller.createEditPopup.hide.restore();
  354. });
  355. it("should call loadAlertNotifications and createEditPopup.hide", function () {
  356. controller.createAlertNotificationSuccessCallback();
  357. expect(controller.loadAlertNotifications.calledOnce).to.be.true;
  358. expect(controller.createEditPopup.hide.calledOnce).to.be.true;
  359. });
  360. });
  361. describe('#updateAlertNotification()', function () {
  362. it("should send ajax request", function () {
  363. controller.createAlertNotification();
  364. expect(App.ajax.send.calledOnce).to.be.true;
  365. });
  366. });
  367. describe('#updateAlertNotificationSuccessCallback()', function () {
  368. beforeEach(function () {
  369. controller.set('createEditPopup', {
  370. hide: Em.K
  371. });
  372. sinon.stub(controller, 'loadAlertNotifications', Em.K);
  373. sinon.spy(controller.createEditPopup, 'hide');
  374. });
  375. afterEach(function () {
  376. controller.loadAlertNotifications.restore();
  377. controller.createEditPopup.hide.restore();
  378. });
  379. it("should call loadAlertNotifications and createEditPopup.hide", function () {
  380. controller.updateAlertNotificationSuccessCallback();
  381. expect(controller.loadAlertNotifications.calledOnce).to.be.true;
  382. expect(controller.createEditPopup.hide.calledOnce).to.be.true;
  383. });
  384. });
  385. describe('#deleteAlertNotification()', function () {
  386. beforeEach(function () {
  387. sinon.spy(App, 'showConfirmationPopup');
  388. });
  389. afterEach(function () {
  390. App.showConfirmationPopup.restore();
  391. });
  392. it("should show popup and send request on confirmation", function () {
  393. var popup = controller.deleteAlertNotification();
  394. expect(App.showConfirmationPopup.calledOnce).to.be.true;
  395. popup.onPrimary();
  396. expect(App.ajax.send.calledOnce).to.be.true;
  397. });
  398. });
  399. describe('#deleteAlertNotificationSuccessCallback()', function () {
  400. it("should call loadAlertNotifications, selectedAlertNotification.deleteRecord and set null to selectedAlertNotification", function () {
  401. var mockSelectedAlertNotification = {
  402. deleteRecord: Em.K
  403. };
  404. controller.set('selectedAlertNotification', mockSelectedAlertNotification);
  405. sinon.stub(controller, 'loadAlertNotifications', Em.K);
  406. sinon.spy(mockSelectedAlertNotification, 'deleteRecord');
  407. controller.deleteAlertNotificationSuccessCallback();
  408. expect(controller.loadAlertNotifications.calledOnce).to.be.true;
  409. expect(mockSelectedAlertNotification.deleteRecord.calledOnce).to.be.true;
  410. expect(controller.get('selectedAlertNotification')).to.equal(null);
  411. controller.loadAlertNotifications.restore();
  412. mockSelectedAlertNotification.deleteRecord.restore();
  413. });
  414. });
  415. describe('#duplicateAlertNotification()', function () {
  416. beforeEach(function () {
  417. sinon.stub(controller, 'fillEditCreateInputs', Em.K);
  418. sinon.stub(controller, 'showCreateEditPopup', Em.K);
  419. });
  420. afterEach(function () {
  421. controller.fillEditCreateInputs.restore();
  422. controller.showCreateEditPopup.restore();
  423. });
  424. it("should call fillEditCreateInputs and showCreateEditPopup", function () {
  425. controller.duplicateAlertNotification();
  426. expect(controller.fillEditCreateInputs.calledWith(true)).to.be.true;
  427. expect(controller.showCreateEditPopup.calledOnce).to.be.true;
  428. });
  429. });
  430. describe('#addCustomProperty', function () {
  431. beforeEach(function () {
  432. controller.set('inputFields.customProperties', []);
  433. });
  434. it('should add custom Property to customProperties', function () {
  435. controller.set('newCustomProperty', {name: 'n1', value: 'v1'});
  436. controller.addCustomProperty();
  437. helpers.nestedExpect([{name: 'n1', value: 'v1', defaultValue: 'v1'}], controller.get('inputFields.customProperties'));
  438. });
  439. });
  440. describe('#removeCustomPropertyHandler', function () {
  441. var c = {name: 'n2', value: 'v2', defaultValue: 'v2'};
  442. beforeEach(function () {
  443. controller.set('inputFields.customProperties', [
  444. {name: 'n1', value: 'v1', defaultValue: 'v1'},
  445. c,
  446. {name: 'n3', value: 'v3', defaultValue: 'v3'}
  447. ]);
  448. });
  449. it('should remove selected custom property', function () {
  450. controller.removeCustomPropertyHandler({context: c});
  451. helpers.nestedExpect(
  452. [
  453. {name: 'n1', value: 'v1', defaultValue: 'v1'},
  454. {name: 'n3', value: 'v3', defaultValue: 'v3'}
  455. ],
  456. controller.get('inputFields.customProperties')
  457. );
  458. });
  459. });
  460. describe('#addCustomPropertyHandler', function () {
  461. it('should clean up newCustomProperty on primary click', function () {
  462. controller.set('newCustomProperty', {name: 'n1', value: 'v1'});
  463. controller.addCustomPropertyHandler().onPrimary();
  464. expect(controller.get('newCustomProperty')).to.eql({name: '', value: ''});
  465. });
  466. describe('#bodyClass', function () {
  467. var view;
  468. beforeEach(function () {
  469. view = controller.addCustomPropertyHandler().get('bodyClass').create({
  470. parentView: Em.View.create(),
  471. controller: Em.Object.create({
  472. inputFields: Em.Object.create({
  473. customProperties: [
  474. {name: 'n1', value: 'v1', defaultValue: 'v1'}
  475. ]
  476. }),
  477. newCustomProperty: {name: '', value: ''}
  478. })
  479. });
  480. });
  481. describe('#errorHandler', function () {
  482. it('should fire invalid name', function () {
  483. view.set('controller.newCustomProperty.name', '!!');
  484. view.errorsHandler();
  485. expect(view.get('isError')).to.be.true;
  486. expect(view.get('parentView.disablePrimary')).to.be.true;
  487. expect(view.get('errorMessage.length') > 0).to.be.true;
  488. });
  489. it('should fire existing property name', function () {
  490. view.set('controller.newCustomProperty.name', 'n1');
  491. view.errorsHandler();
  492. expect(view.get('isError')).to.be.true;
  493. expect(view.get('parentView.disablePrimary')).to.be.true;
  494. expect(view.get('errorMessage.length') > 0).to.be.true;
  495. });
  496. });
  497. });
  498. });
  499. });