manage_alert_notifications_controller_test.js 15 KB

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