definitions_configs_controller_test.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. describe('App.MainAlertDefinitionConfigsController', function () {
  21. beforeEach(function () {
  22. controller = App.MainAlertDefinitionConfigsController.create({
  23. allServices: ['service1', 'service2', 'service3'],
  24. allComponents: ['component1', 'component2', 'component3'],
  25. aggregateAlertNames: ['alertDefinitionName', 'alertDefinitionName2', 'alertDefinitionName3']
  26. });
  27. });
  28. describe('#renderConfigs()', function () {
  29. beforeEach(function () {
  30. controller.set('content', Em.Object.create({}));
  31. sinon.stub(controller, 'renderPortConfigs', function () {
  32. return [];
  33. });
  34. sinon.stub(controller, 'renderMetricConfigs', function () {
  35. return [];
  36. });
  37. sinon.stub(controller, 'renderWebConfigs', function () {
  38. return [];
  39. });
  40. sinon.stub(controller, 'renderScriptConfigs', function () {
  41. return [];
  42. });
  43. sinon.stub(controller, 'renderAggregateConfigs', function () {
  44. return [];
  45. });
  46. });
  47. afterEach(function () {
  48. controller.renderPortConfigs.restore();
  49. controller.renderMetricConfigs.restore();
  50. controller.renderWebConfigs.restore();
  51. controller.renderScriptConfigs.restore();
  52. controller.renderAggregateConfigs.restore();
  53. });
  54. it('should call renderPortConfigs method', function () {
  55. controller.set('alertDefinitionType', 'PORT');
  56. controller.renderConfigs();
  57. expect(controller.renderPortConfigs.calledOnce).to.be.true;
  58. });
  59. it('should call renderMetricConfigs method', function () {
  60. controller.set('alertDefinitionType', 'METRIC');
  61. controller.renderConfigs();
  62. expect(controller.renderMetricConfigs.calledOnce).to.be.true;
  63. });
  64. it('should call renderWebConfigs method', function () {
  65. controller.set('alertDefinitionType', 'WEB');
  66. controller.renderConfigs();
  67. expect(controller.renderWebConfigs.calledOnce).to.be.true;
  68. });
  69. it('should call renderScriptConfigs method', function () {
  70. controller.set('alertDefinitionType', 'SCRIPT');
  71. controller.renderConfigs();
  72. expect(controller.renderScriptConfigs.calledOnce).to.be.true;
  73. });
  74. it('should call renderAggregateConfigs method', function () {
  75. controller.set('alertDefinitionType', 'AGGREGATE');
  76. controller.renderConfigs();
  77. expect(controller.renderAggregateConfigs.calledOnce).to.be.true;
  78. });
  79. });
  80. describe('#renderPortConfigs()', function () {
  81. it('should render array of configs with correct values', function () {
  82. controller.set('content', Em.Object.create({
  83. name: 'alertDefinitionName',
  84. service: {displayName: 'alertDefinitionService'},
  85. componentName: 'component1',
  86. scope: 'HOST',
  87. description: 'alertDefinitionDescription',
  88. interval: 60,
  89. reporting: [
  90. Em.Object.create({
  91. type: 'warning',
  92. value: 10
  93. }),
  94. Em.Object.create({
  95. type: 'critical',
  96. value: 20
  97. }),
  98. Em.Object.create({
  99. type: 'ok',
  100. value: 30
  101. })
  102. ],
  103. uri: 'alertDefinitionUri',
  104. defaultPort: '777'
  105. }));
  106. controller.set('isWizard', true);
  107. var result = controller.renderPortConfigs();
  108. expect(result.length).to.equal(11);
  109. controller.set('isWizard', false);
  110. result = controller.renderPortConfigs();
  111. expect(result.length).to.equal(5);
  112. });
  113. });
  114. describe('#renderMetricConfigs()', function () {
  115. it('should render array of configs with correct values', function () {
  116. controller.set('content', Em.Object.create({
  117. name: 'alertDefinitionName',
  118. service: {displayName: 'alertDefinitionService'},
  119. componentName: 'component1',
  120. scope: 'HOST',
  121. description: 'alertDefinitionDescription',
  122. interval: 60,
  123. reporting: [
  124. Em.Object.create({
  125. type: 'warning',
  126. value: 10
  127. }),
  128. Em.Object.create({
  129. type: 'critical',
  130. value: 20
  131. }),
  132. Em.Object.create({
  133. type: 'ok',
  134. value: 30
  135. })
  136. ],
  137. uri: {
  138. "http": "{{mapred-site/mapreduce.jobhistory.webapp.address}}",
  139. "https": "{{mapred-site/mapreduce.jobhistory.webapp.https.address}}",
  140. "https_property": "{{mapred-site/mapreduce.jobhistory.http.policy}}",
  141. "https_property_value": "HTTPS_ONLY",
  142. "default_port": 0.0
  143. },
  144. jmx: {
  145. propertyList: ['property1', 'property2'],
  146. value: 'jmxValue'
  147. },
  148. ganglia: {
  149. propertyList: null,
  150. value: null
  151. }
  152. }));
  153. controller.set('isWizard', true);
  154. var result = controller.renderMetricConfigs();
  155. expect(result.length).to.equal(11);
  156. controller.set('isWizard', false);
  157. result = controller.renderMetricConfigs();
  158. expect(result.length).to.equal(5);
  159. });
  160. });
  161. describe('#renderWebConfigs()', function () {
  162. it('should render array of configs with correct values', function () {
  163. controller.set('content', Em.Object.create({
  164. name: 'alertDefinitionName',
  165. service: {displayName: 'alertDefinitionService'},
  166. componentName: 'component1',
  167. scope: 'HOST',
  168. description: 'alertDefinitionDescription',
  169. interval: 60,
  170. reporting: [
  171. Em.Object.create({
  172. type: 'warning',
  173. value: 10
  174. }),
  175. Em.Object.create({
  176. type: 'critical',
  177. value: 20
  178. }),
  179. Em.Object.create({
  180. type: 'ok',
  181. value: 30
  182. })
  183. ],
  184. uri: {
  185. "http": "{{mapred-site/mapreduce.jobhistory.webapp.address}}",
  186. "https": "{{mapred-site/mapreduce.jobhistory.webapp.https.address}}",
  187. "https_property": "{{mapred-site/mapreduce.jobhistory.http.policy}}",
  188. "https_property_value": "HTTPS_ONLY",
  189. "default_port": 0.0
  190. }
  191. }));
  192. controller.set('isWizard', true);
  193. var result = controller.renderWebConfigs();
  194. expect(result.length).to.equal(11);
  195. controller.set('isWizard', false);
  196. result = controller.renderWebConfigs();
  197. expect(result.length).to.equal(5);
  198. });
  199. });
  200. describe('#renderScriptConfigs()', function () {
  201. it('should render array of configs with correct values', function () {
  202. controller.set('content', Em.Object.create({
  203. name: 'alertDefinitionName',
  204. service: {displayName: 'alertDefinitionService'},
  205. componentName: 'component1',
  206. scope: 'HOST',
  207. description: 'alertDefinitionDescription',
  208. interval: 60,
  209. reporting: [
  210. Em.Object.create({
  211. type: 'warning',
  212. value: 10
  213. }),
  214. Em.Object.create({
  215. type: 'critical',
  216. value: 20
  217. }),
  218. Em.Object.create({
  219. type: 'ok',
  220. value: 30
  221. })
  222. ],
  223. location: 'path to script'
  224. }));
  225. controller.set('isWizard', true);
  226. var result = controller.renderScriptConfigs();
  227. expect(result.length).to.equal(11);
  228. controller.set('isWizard', false);
  229. result = controller.renderScriptConfigs();
  230. expect(result.length).to.equal(5);
  231. });
  232. });
  233. describe('#renderAggregateConfigs()', function () {
  234. it('should render array of configs with correct values', function () {
  235. controller.set('content', Em.Object.create({
  236. name: 'alertDefinitionName',
  237. description: 'alertDefinitionDescription',
  238. reporting: [
  239. Em.Object.create({
  240. type: 'warning',
  241. value: 10
  242. }),
  243. Em.Object.create({
  244. type: 'critical',
  245. value: 20
  246. }),
  247. Em.Object.create({
  248. type: 'ok',
  249. value: 30
  250. })
  251. ]
  252. }));
  253. var result = controller.renderAggregateConfigs();
  254. expect(result.length).to.equal(4);
  255. });
  256. });
  257. describe('#editConfigs()', function () {
  258. it('should set previousValue, isDisabled for each config and change canEdit flag', function () {
  259. controller.set('configs', [
  260. Em.Object.create({value: 'value1', previousValue: '', isDisabled: true}),
  261. Em.Object.create({value: 'value2', previousValue: '', isDisabled: true}),
  262. Em.Object.create({value: 'value3', previousValue: '', isDisabled: true})
  263. ]);
  264. controller.set('canEdit', false);
  265. controller.editConfigs();
  266. expect(controller.get('configs').mapProperty('previousValue')).to.eql(['value1', 'value2', 'value3']);
  267. expect(controller.get('configs').someProperty('isDisabled', true)).to.be.false;
  268. expect(controller.get('canEdit')).to.be.true;
  269. });
  270. });
  271. describe('#cancelEditConfigs()', function () {
  272. it('should set previousValue, isDisabled for each config and change canEdit flag', function () {
  273. controller.set('configs', [
  274. Em.Object.create({value: '', previousValue: 'value1', isDisabled: false}),
  275. Em.Object.create({value: '', previousValue: 'value2', isDisabled: false}),
  276. Em.Object.create({value: '', previousValue: 'value3', isDisabled: false})
  277. ]);
  278. controller.set('canEdit', true);
  279. controller.cancelEditConfigs();
  280. expect(controller.get('configs').mapProperty('value')).to.eql(['value1', 'value2', 'value3']);
  281. expect(controller.get('configs').someProperty('isDisabled', false)).to.be.false;
  282. expect(controller.get('canEdit')).to.be.false;
  283. });
  284. });
  285. describe('#saveConfigs()', function () {
  286. beforeEach(function () {
  287. sinon.spy(App.ajax, 'send');
  288. });
  289. afterEach(function () {
  290. App.ajax.send.restore();
  291. });
  292. it('should set previousValue, isDisabled for each config and change canEdit flag', function () {
  293. controller.set('configs', [
  294. Em.Object.create({isDisabled: true}),
  295. Em.Object.create({isDisabled: true}),
  296. Em.Object.create({isDisabled: true})
  297. ]);
  298. controller.set('canEdit', true);
  299. controller.saveConfigs();
  300. expect(controller.get('configs').someProperty('isDisabled', false)).to.be.false;
  301. expect(controller.get('canEdit')).to.be.false;
  302. expect(App.ajax.send.calledOnce).to.be.true;
  303. });
  304. });
  305. describe('#getPropertiesToUpdate()', function () {
  306. beforeEach(function () {
  307. controller.set('content', {
  308. rawSourceData: {
  309. path1: 'value',
  310. path2: {
  311. path3: 'value'
  312. }
  313. }
  314. });
  315. });
  316. var testCases = [
  317. {
  318. m: 'should ignore configs with wasChanged false',
  319. configs: [
  320. Em.Object.create({
  321. wasChanged: false,
  322. apiProperty: 'name1',
  323. apiFormattedValue: 'test1'
  324. }),
  325. Em.Object.create({
  326. wasChanged: true,
  327. apiProperty: 'name2',
  328. apiFormattedValue: 'test2'
  329. }),
  330. Em.Object.create({
  331. wasChanged: false,
  332. apiProperty: 'name3',
  333. apiFormattedValue: 'test3'
  334. })
  335. ],
  336. result: {
  337. 'AlertDefinition/name2': 'test2'
  338. }
  339. },
  340. {
  341. m: 'should correctly map deep source properties',
  342. configs: [
  343. Em.Object.create({
  344. wasChanged: true,
  345. apiProperty: 'name1',
  346. apiFormattedValue: 'test1'
  347. }),
  348. Em.Object.create({
  349. wasChanged: true,
  350. apiProperty: 'source.path1',
  351. apiFormattedValue: 'value1'
  352. }),
  353. Em.Object.create({
  354. wasChanged: true,
  355. apiProperty: 'source.path2.path3',
  356. apiFormattedValue: 'value2'
  357. })
  358. ],
  359. result: {
  360. 'AlertDefinition/name1': 'test1',
  361. 'AlertDefinition/source': {
  362. path1: 'value1',
  363. path2: {
  364. path3: 'value2'
  365. }
  366. }
  367. }
  368. },
  369. {
  370. m: 'should correctly multiple apiProperties',
  371. configs: [
  372. Em.Object.create({
  373. wasChanged: true,
  374. apiProperty: ['name1', 'name2'],
  375. apiFormattedValue: ['value1', 'value2']
  376. })
  377. ],
  378. result: {
  379. 'AlertDefinition/name1': 'value1',
  380. 'AlertDefinition/name2': 'value2'
  381. }
  382. }
  383. ];
  384. testCases.forEach(function (testCase) {
  385. it(testCase.m, function () {
  386. controller.set('configs', testCase.configs);
  387. var result = controller.getPropertiesToUpdate(true);
  388. expect(result).to.eql(testCase.result);
  389. });
  390. });
  391. });
  392. describe('#changeType()', function () {
  393. it('should disable and enable appropriate configs', function () {
  394. controller.set('allServices', ['service1', 'service2']);
  395. controller.set('allScopes', ['scope1', 'scope2']);
  396. controller.set('configs', [
  397. Em.Object.create({name: 'service', isDisabled: false}),
  398. Em.Object.create({name: 'component', isDisabled: false}),
  399. Em.Object.create({name: 'scope', isDisabled: false})
  400. ]);
  401. controller.changeType('Host Alert Definition');
  402. expect(controller.get('configs').everyProperty('isDisabled', true)).to.be.true;
  403. expect(controller.get('configs').findProperty('name', 'service').get('options')).to.eql(['Ambari']);
  404. expect(controller.get('configs').findProperty('name', 'service').get('value')).to.equal('Ambari');
  405. expect(controller.get('configs').findProperty('name', 'component').get('value')).to.equal('Ambari Agent');
  406. expect(controller.get('configs').findProperty('name', 'scope').get('options')).to.eql(['Host']);
  407. expect(controller.get('configs').findProperty('name', 'scope').get('value')).to.equal('Host');
  408. controller.changeType('alert_type_service');
  409. expect(controller.get('configs').everyProperty('isDisabled', false)).to.be.true;
  410. expect(controller.get('configs').findProperty('name', 'service').get('options')).to.eql(['service1', 'service2']);
  411. expect(controller.get('configs').findProperty('name', 'service').get('value')).to.equal('service1');
  412. expect(controller.get('configs').findProperty('name', 'component').get('value')).to.equal('No component');
  413. expect(controller.get('configs').findProperty('name', 'scope').get('options')).to.eql(['scope1', 'scope2']);
  414. expect(controller.get('configs').findProperty('name', 'scope').get('value')).to.equal('scope1');
  415. });
  416. });
  417. describe('#renderCommonWizardConfigs()', function () {
  418. it('should return correct number of configs', function () {
  419. var result = controller.renderCommonWizardConfigs();
  420. expect(result.length).to.equal(6);
  421. });
  422. });
  423. describe('#getConfigsValues()', function () {
  424. it('should create key-value map from configs', function () {
  425. controller.set('configs', [
  426. Em.Object.create({name: 'name1', value: 'value1'}),
  427. Em.Object.create({name: 'name2', value: 'value2'}),
  428. Em.Object.create({name: 'name3', value: 'value3'})
  429. ]);
  430. var result = controller.getConfigsValues();
  431. expect(result).to.eql([
  432. {name: 'name1', value: 'value1'},
  433. {name: 'name2', value: 'value2'},
  434. {name: 'name3', value: 'value3'}
  435. ]);
  436. });
  437. });
  438. });