manage_alert_notifications_controller_test.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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.spy(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.spy(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: [true, true, true, true]
  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. 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. customProperties: [
  166. {name: 'customName', value: 'customValue1', defaultValue: 'customValue1'},
  167. {name: 'customName2', value: 'customValue1', defaultValue: 'customValue1'}
  168. ]
  169. }));
  170. controller.fillEditCreateInputs();
  171. expect(JSON.stringify(controller.get('inputFields'))).to.equal(JSON.stringify({
  172. name: {
  173. value: 'test_name'
  174. },
  175. groups: {
  176. value: ''
  177. },
  178. global: {
  179. value: true,
  180. disabled: true
  181. },
  182. method: {
  183. value: 'EMAIL'
  184. },
  185. email: {
  186. value: 'test1@test.test, test2@test.test'
  187. },
  188. severityFilter: {
  189. value: [true, false, false, true]
  190. },
  191. description: {
  192. value: 'test_description'
  193. },
  194. customProperties: [
  195. {name: 'customName', value: 'customValue', defaultValue: 'customValue'}
  196. ]
  197. }));
  198. });
  199. });
  200. describe("#showCreateEditPopup()", function () {
  201. before(function () {
  202. sinon.stub(App.ModalPopup, 'show', function () {
  203. return 'popup';
  204. });
  205. });
  206. after(function () {
  207. App.ModalPopup.show.restore();
  208. });
  209. it("should open popup and set popup object to createEditPopup", function () {
  210. controller.set('createEditPopup', null);
  211. controller.showCreateEditPopup();
  212. expect(App.ModalPopup.show.calledOnce).to.be.true;
  213. expect(controller.get('createEditPopup')).to.equal('popup');
  214. });
  215. });
  216. describe("#formatNotificationAPIObject()", function () {
  217. it("should create object with properties from inputFields values", function () {
  218. controller.set('inputFields', Em.Object.create({
  219. name: {
  220. value: 'test_name'
  221. },
  222. groups: {
  223. value: ''
  224. },
  225. global: {
  226. value: false
  227. },
  228. method: {
  229. value: 'EMAIL'
  230. },
  231. email: {
  232. value: 'test1@test.test, test2@test.test,test3@test.test , test4@test.test'
  233. },
  234. severityFilter: {
  235. value: [true, false, true, false]
  236. },
  237. description: {
  238. value: 'test_description'
  239. },
  240. customProperties: [
  241. {name: 'n1', value: 'v1'},
  242. {name: 'n2', value: 'v2'}
  243. ]
  244. }));
  245. var result = controller.formatNotificationAPIObject();
  246. expect(result).to.eql({
  247. AlertTarget: {
  248. name: 'test_name',
  249. global: false,
  250. description: 'test_description',
  251. notification_type: 'EMAIL',
  252. alert_states: ['OK', 'CRITICAL'],
  253. properties: {
  254. 'ambari.dispatch.recipients': [
  255. 'test1@test.test',
  256. 'test2@test.test',
  257. 'test3@test.test',
  258. 'test4@test.test'
  259. ],
  260. 'n1': 'v1',
  261. 'n2': 'v2'
  262. }
  263. }
  264. });
  265. });
  266. });
  267. describe('#createAlertNotification()', function () {
  268. it("should send ajax request", function () {
  269. controller.createAlertNotification();
  270. expect(App.ajax.send.calledOnce).to.be.true;
  271. });
  272. });
  273. describe('#createAlertNotificationSuccessCallback()', function () {
  274. beforeEach(function () {
  275. controller.set('createEditPopup', {
  276. hide: Em.K
  277. });
  278. sinon.stub(controller, 'loadAlertNotifications', Em.K);
  279. sinon.spy(controller.createEditPopup, 'hide');
  280. });
  281. afterEach(function () {
  282. controller.loadAlertNotifications.restore();
  283. controller.createEditPopup.hide.restore();
  284. });
  285. it("should call loadAlertNotifications and createEditPopup.hide", function () {
  286. controller.createAlertNotificationSuccessCallback();
  287. expect(controller.loadAlertNotifications.calledOnce).to.be.true;
  288. expect(controller.createEditPopup.hide.calledOnce).to.be.true;
  289. });
  290. });
  291. describe('#updateAlertNotification()', function () {
  292. it("should send ajax request", function () {
  293. controller.createAlertNotification();
  294. expect(App.ajax.send.calledOnce).to.be.true;
  295. });
  296. });
  297. describe('#updateAlertNotificationSuccessCallback()', function () {
  298. beforeEach(function () {
  299. controller.set('createEditPopup', {
  300. hide: Em.K
  301. });
  302. sinon.stub(controller, 'loadAlertNotifications', Em.K);
  303. sinon.spy(controller.createEditPopup, 'hide');
  304. });
  305. afterEach(function () {
  306. controller.loadAlertNotifications.restore();
  307. controller.createEditPopup.hide.restore();
  308. });
  309. it("should call loadAlertNotifications and createEditPopup.hide", function () {
  310. controller.updateAlertNotificationSuccessCallback();
  311. expect(controller.loadAlertNotifications.calledOnce).to.be.true;
  312. expect(controller.createEditPopup.hide.calledOnce).to.be.true;
  313. });
  314. });
  315. describe('#deleteAlertNotification()', function () {
  316. beforeEach(function () {
  317. sinon.spy(App, 'showConfirmationPopup');
  318. });
  319. afterEach(function () {
  320. App.showConfirmationPopup.restore();
  321. });
  322. it("should show popup and send request on confirmation", function () {
  323. var popup = controller.deleteAlertNotification();
  324. expect(App.showConfirmationPopup.calledOnce).to.be.true;
  325. popup.onPrimary();
  326. expect(App.ajax.send.calledOnce).to.be.true;
  327. });
  328. });
  329. describe('#deleteAlertNotificationSuccessCallback()', function () {
  330. it("should call loadAlertNotifications, selectedAlertNotification.deleteRecord and set null to selectedAlertNotification", function () {
  331. var mockSelectedAlertNotification = {
  332. deleteRecord: Em.K
  333. };
  334. controller.set('selectedAlertNotification', mockSelectedAlertNotification);
  335. sinon.stub(controller, 'loadAlertNotifications', Em.K);
  336. sinon.spy(mockSelectedAlertNotification, 'deleteRecord');
  337. controller.deleteAlertNotificationSuccessCallback();
  338. expect(controller.loadAlertNotifications.calledOnce).to.be.true;
  339. expect(mockSelectedAlertNotification.deleteRecord.calledOnce).to.be.true;
  340. expect(controller.get('selectedAlertNotification')).to.equal(null);
  341. controller.loadAlertNotifications.restore();
  342. mockSelectedAlertNotification.deleteRecord.restore();
  343. });
  344. });
  345. describe('#duplicateAlertNotification()', function () {
  346. beforeEach(function () {
  347. sinon.stub(controller, 'fillEditCreateInputs', Em.K);
  348. sinon.stub(controller, 'showCreateEditPopup', Em.K);
  349. });
  350. afterEach(function () {
  351. controller.fillEditCreateInputs.restore();
  352. controller.showCreateEditPopup.restore();
  353. });
  354. it("should call fillEditCreateInputs and showCreateEditPopup", function () {
  355. controller.duplicateAlertNotification();
  356. expect(controller.fillEditCreateInputs.calledWith(true)).to.be.true;
  357. expect(controller.showCreateEditPopup.calledOnce).to.be.true;
  358. });
  359. });
  360. describe('#addCustomProperty', function () {
  361. beforeEach(function () {
  362. controller.set('inputFields.customProperties', []);
  363. });
  364. it('should add custom Property to customProperties', function () {
  365. controller.set('newCustomProperty', {name: 'n1', value: 'v1'});
  366. controller.addCustomProperty();
  367. helpers.nestedExpect([{name: 'n1', value: 'v1', defaultValue: 'v1'}], controller.get('inputFields.customProperties'));
  368. });
  369. });
  370. describe('#removeCustomPropertyHandler', function () {
  371. var c = {name: 'n2', value: 'v2', defaultValue: 'v2'};
  372. beforeEach(function () {
  373. controller.set('inputFields.customProperties', [
  374. {name: 'n1', value: 'v1', defaultValue: 'v1'},
  375. c,
  376. {name: 'n3', value: 'v3', defaultValue: 'v3'}
  377. ]);
  378. });
  379. it('should remove selected custom property', function () {
  380. controller.removeCustomPropertyHandler({context: c});
  381. helpers.nestedExpect(
  382. [
  383. {name: 'n1', value: 'v1', defaultValue: 'v1'},
  384. {name: 'n3', value: 'v3', defaultValue: 'v3'}
  385. ],
  386. controller.get('inputFields.customProperties')
  387. );
  388. });
  389. });
  390. describe('#addCustomPropertyHandler', function () {
  391. it('should clean up newCustomProperty on primary click', function () {
  392. controller.set('newCustomProperty', {name: 'n1', value: 'v1'});
  393. controller.addCustomPropertyHandler().onPrimary();
  394. expect(controller.get('newCustomProperty')).to.eql({name: '', value: ''});
  395. });
  396. describe('#bodyClass', function () {
  397. var view;
  398. beforeEach(function () {
  399. view = controller.addCustomPropertyHandler().get('bodyClass').create({
  400. parentView: Em.View.create(),
  401. controller: Em.Object.create({
  402. inputFields: Em.Object.create({
  403. customProperties: [
  404. {name: 'n1', value: 'v1', defaultValue: 'v1'}
  405. ]
  406. }),
  407. newCustomProperty: {name: '', value: ''}
  408. })
  409. });
  410. });
  411. describe('#errorHandler', function () {
  412. it('should fire invalid name', function () {
  413. view.set('controller.newCustomProperty.name', '!!');
  414. view.errorsHandler();
  415. expect(view.get('isError')).to.be.true;
  416. expect(view.get('parentView.disablePrimary')).to.be.true;
  417. expect(view.get('errorMessage.length') > 0).to.be.true;
  418. });
  419. it('should fire existing property name', function () {
  420. view.set('controller.newCustomProperty.name', 'n1');
  421. view.errorsHandler();
  422. expect(view.get('isError')).to.be.true;
  423. expect(view.get('parentView.disablePrimary')).to.be.true;
  424. expect(view.get('errorMessage.length') > 0).to.be.true;
  425. });
  426. });
  427. });
  428. });
  429. });