manage_alert_notifications_controller_test.js 15 KB

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