step2_controller_test.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. App = require('app');
  19. require('controllers/main/service/widgets/create/step2_controller');
  20. describe('App.WidgetWizardStep2Controller', function () {
  21. var controller = App.WidgetWizardStep2Controller.create({
  22. content: Em.Object.create()
  23. });
  24. describe("#isEditWidget", function () {
  25. it("empty name", function () {
  26. controller.set('content.controllerName', '');
  27. controller.propertyDidChange('isEditWidget');
  28. expect(controller.get('isEditWidget')).to.be.false;
  29. });
  30. it("correct name", function () {
  31. controller.set('content.controllerName', 'widgetEditController');
  32. controller.propertyDidChange('isEditWidget');
  33. expect(controller.get('isEditWidget')).to.be.true;
  34. });
  35. });
  36. describe("#filteredMetrics", function () {
  37. var testCases = [
  38. {
  39. metric: {
  40. point_in_time: false
  41. },
  42. type: null,
  43. result: []
  44. },
  45. {
  46. metric: {
  47. point_in_time: true
  48. },
  49. type: null,
  50. result: [
  51. {
  52. point_in_time: true
  53. }
  54. ]
  55. },
  56. {
  57. metric: {
  58. temporal: false
  59. },
  60. type: 'GRAPH',
  61. result: []
  62. },
  63. {
  64. metric: {
  65. temporal: true
  66. },
  67. type: 'GRAPH',
  68. result: [
  69. {
  70. temporal: true
  71. }
  72. ]
  73. }
  74. ];
  75. testCases.forEach(function (test) {
  76. it("type=" + test.type + "; temporal=" + test.metric.temporal + "; point_in_time=" + test.metric.point_in_time, function () {
  77. controller.get('content').setProperties({
  78. widgetType: test.type,
  79. allMetrics: [test.metric]
  80. });
  81. controller.propertyDidChange('filteredMetrics');
  82. expect(controller.get('filteredMetrics')).to.eql(test.result);
  83. });
  84. });
  85. });
  86. describe("#isSubmitDisabled", function () {
  87. beforeEach(function () {
  88. this.expressionFunc = sinon.stub(controller, 'isExpressionComplete');
  89. this.graphFunc = sinon.stub(controller, 'isGraphDataComplete');
  90. this.templateFunc = sinon.stub(controller, 'isTemplateDataComplete');
  91. controller.set('expressions', ['']);
  92. });
  93. afterEach(function () {
  94. this.expressionFunc.restore();
  95. this.graphFunc.restore();
  96. this.templateFunc.restore();
  97. controller.get('expressions').clear();
  98. });
  99. it("invalid property", function () {
  100. controller.set('widgetPropertiesViews', [Em.Object.create({isValid: false})]);
  101. controller.propertyDidChange('isSubmitDisabled');
  102. expect(controller.get('isSubmitDisabled')).to.be.true;
  103. });
  104. it("valid number widget", function () {
  105. controller.set('widgetPropertiesViews', []);
  106. controller.set('content.widgetType', 'NUMBER');
  107. this.expressionFunc.returns(true);
  108. controller.propertyDidChange('isSubmitDisabled');
  109. expect(controller.get('isSubmitDisabled')).to.be.false;
  110. });
  111. it("invalid number widget", function () {
  112. controller.set('widgetPropertiesViews', []);
  113. controller.set('content.widgetType', 'NUMBER');
  114. this.expressionFunc.returns(false);
  115. controller.propertyDidChange('isSubmitDisabled');
  116. expect(controller.get('isSubmitDisabled')).to.be.true;
  117. });
  118. it("valid graph widget", function () {
  119. controller.set('widgetPropertiesViews', []);
  120. controller.set('content.widgetType', 'GRAPH');
  121. this.graphFunc.returns(true);
  122. controller.propertyDidChange('isSubmitDisabled');
  123. expect(controller.get('isSubmitDisabled')).to.be.false;
  124. });
  125. it("invalid graph widget", function () {
  126. controller.set('widgetPropertiesViews', []);
  127. controller.set('content.widgetType', 'GRAPH');
  128. this.graphFunc.returns(false);
  129. controller.propertyDidChange('isSubmitDisabled');
  130. expect(controller.get('isSubmitDisabled')).to.be.true;
  131. });
  132. it("valid template widget", function () {
  133. controller.set('widgetPropertiesViews', []);
  134. controller.set('content.widgetType', 'TEMPLATE');
  135. this.templateFunc.returns(true);
  136. controller.propertyDidChange('isSubmitDisabled');
  137. expect(controller.get('isSubmitDisabled')).to.be.false;
  138. });
  139. it("invalid template widget", function () {
  140. controller.set('widgetPropertiesViews', []);
  141. controller.set('content.widgetType', 'TEMPLATE');
  142. this.templateFunc.returns(false);
  143. controller.propertyDidChange('isSubmitDisabled');
  144. expect(controller.get('isSubmitDisabled')).to.be.true;
  145. });
  146. it("unknown widget type", function () {
  147. controller.set('widgetPropertiesViews', []);
  148. controller.set('content.widgetType', '');
  149. controller.propertyDidChange('isSubmitDisabled');
  150. expect(controller.get('isSubmitDisabled')).to.be.false;
  151. });
  152. });
  153. describe("#isExpressionComplete()", function () {
  154. var testCases = [
  155. {
  156. expression: null,
  157. result: false
  158. },
  159. {
  160. expression: Em.Object.create({isInvalid: true}),
  161. result: false
  162. },
  163. {
  164. expression: Em.Object.create({isInvalid: false, isEmpty: false}),
  165. result: true
  166. },
  167. {
  168. expression: Em.Object.create({isInvalid: false, isEmpty: true}),
  169. result: false
  170. }
  171. ];
  172. testCases.forEach(function (test) {
  173. it("expression = " + test.expression, function () {
  174. expect(controller.isExpressionComplete(test.expression)).to.equal(test.result);
  175. });
  176. });
  177. });
  178. describe("#isGraphDataComplete()", function () {
  179. beforeEach(function () {
  180. this.mock = sinon.stub(controller, 'isExpressionComplete');
  181. });
  182. afterEach(function () {
  183. this.mock.restore();
  184. });
  185. it("dataSets is empty", function () {
  186. expect(controller.isGraphDataComplete([])).to.be.false;
  187. });
  188. it("label is empty", function () {
  189. expect(controller.isGraphDataComplete([Em.Object.create({label: ''})])).to.be.false;
  190. });
  191. it("expression is not complete", function () {
  192. this.mock.returns(false);
  193. expect(controller.isGraphDataComplete([Em.Object.create({label: 'abc'})])).to.be.false;
  194. });
  195. it("expression is complete", function () {
  196. this.mock.returns(true);
  197. expect(controller.isGraphDataComplete([Em.Object.create({label: 'abc'})])).to.be.true;
  198. });
  199. });
  200. describe("#isTemplateDataComplete()", function () {
  201. beforeEach(function () {
  202. this.mock = sinon.stub(controller, 'isExpressionComplete');
  203. });
  204. afterEach(function () {
  205. this.mock.restore();
  206. });
  207. it("expressions is empty", function () {
  208. expect(controller.isTemplateDataComplete([])).to.be.false;
  209. });
  210. it("templateValue is empty", function () {
  211. expect(controller.isTemplateDataComplete([{}], '')).to.be.false;
  212. });
  213. it("expression is not complete", function () {
  214. this.mock.returns(false);
  215. expect(controller.isTemplateDataComplete([{}], 'abc')).to.be.false;
  216. });
  217. it("expression is complete", function () {
  218. this.mock.returns(true);
  219. expect(controller.isTemplateDataComplete([{}], 'abc')).to.be.true;
  220. });
  221. });
  222. describe("#addDataSet()", function () {
  223. it("", function () {
  224. controller.get('dataSets').clear();
  225. controller.addDataSet(null, true);
  226. expect(controller.get('dataSets').objectAt(0).get('id')).to.equal(1);
  227. expect(controller.get('dataSets').objectAt(0).get('isRemovable')).to.equal(false);
  228. controller.addDataSet(null);
  229. expect(controller.get('dataSets').objectAt(1).get('id')).to.equal(2);
  230. expect(controller.get('dataSets').objectAt(1).get('isRemovable')).to.equal(true);
  231. controller.get('dataSets').clear();
  232. });
  233. });
  234. describe("#removeDataSet()", function () {
  235. it("", function () {
  236. var dataSet = Em.Object.create();
  237. controller.get('dataSets').pushObject(dataSet);
  238. controller.removeDataSet({context: dataSet});
  239. expect(controller.get('dataSets')).to.be.empty;
  240. });
  241. });
  242. describe("#addExpression()", function () {
  243. it("", function () {
  244. controller.get('expressions').clear();
  245. controller.addExpression(null, true);
  246. expect(controller.get('expressions').objectAt(0).get('id')).to.equal(1);
  247. expect(controller.get('expressions').objectAt(0).get('isRemovable')).to.equal(false);
  248. controller.addExpression(null);
  249. expect(controller.get('expressions').objectAt(1).get('id')).to.equal(2);
  250. expect(controller.get('expressions').objectAt(1).get('isRemovable')).to.equal(true);
  251. controller.get('expressions').clear();
  252. });
  253. });
  254. describe("#removeExpression()", function () {
  255. it("", function () {
  256. var expression = Em.Object.create();
  257. controller.get('expressions').pushObject(expression);
  258. controller.removeExpression({context: expression});
  259. expect(controller.get('expressions')).to.be.empty;
  260. });
  261. });
  262. describe("#initWidgetData()", function () {
  263. it("new data", function () {
  264. controller.set('expressions', []);
  265. controller.set('dataSets', []);
  266. controller.get('content').setProperties({
  267. widgetProperties: {a: 1},
  268. widgetValues: [1],
  269. widgetMetrics: [2]
  270. });
  271. controller.initWidgetData();
  272. expect(controller.get('widgetProperties')).to.eql({a: 1});
  273. expect(controller.get('widgetValues')).to.eql([]);
  274. expect(controller.get('widgetMetrics')).to.eql([]);
  275. expect(controller.get('expressions')).to.not.be.empty;
  276. expect(controller.get('dataSets')).to.not.be.empty;
  277. });
  278. it("previously edited", function () {
  279. controller.set('expressions', [{}]);
  280. controller.set('dataSets', [{}]);
  281. controller.get('content').setProperties({
  282. widgetProperties: {a: 1},
  283. widgetValues: [1],
  284. widgetMetrics: [2]
  285. });
  286. controller.initWidgetData();
  287. expect(controller.get('widgetProperties')).to.eql({a: 1});
  288. expect(controller.get('widgetValues')).to.eql([1]);
  289. expect(controller.get('widgetMetrics')).to.eql([2]);
  290. expect(controller.get('expressions')).to.not.be.empty;
  291. expect(controller.get('dataSets')).to.not.be.empty;
  292. });
  293. });
  294. describe("#updateExpressions()", function () {
  295. beforeEach(function () {
  296. sinon.stub(controller, 'parseExpression').returns({values: [1], metrics: [1]});
  297. sinon.stub(controller, 'parseTemplateExpression').returns({values: [1], metrics: [1]});
  298. sinon.stub(controller, 'parseGraphDataset').returns({values: [1], metrics: [1]});
  299. });
  300. afterEach(function () {
  301. controller.parseExpression.restore();
  302. controller.parseTemplateExpression.restore();
  303. controller.parseGraphDataset.restore();
  304. });
  305. it("empty expressions", function () {
  306. controller.set('expressions', []);
  307. controller.updateExpressions();
  308. expect(controller.parseExpression.called).to.be.false;
  309. expect(controller.parseTemplateExpression.called).to.be.false;
  310. expect(controller.parseGraphDataset.called).to.be.false;
  311. expect(controller.get('widgetValues')).to.be.empty;
  312. expect(controller.get('widgetMetrics')).to.be.empty;
  313. });
  314. it("empty dataSets", function () {
  315. controller.set('dataSets', []);
  316. controller.updateExpressions();
  317. expect(controller.parseExpression.called).to.be.false;
  318. expect(controller.parseTemplateExpression.called).to.be.false;
  319. expect(controller.parseGraphDataset.called).to.be.false;
  320. expect(controller.get('widgetValues')).to.be.empty;
  321. expect(controller.get('widgetMetrics')).to.be.empty;
  322. });
  323. it("GAUGE widget", function () {
  324. controller.set('expressions', [{}]);
  325. controller.set('content.widgetType', 'GAUGE');
  326. controller.set('dataSets', [{}]);
  327. //controller.updateExpressions();
  328. expect(controller.parseExpression.calledOnce).to.be.true;
  329. expect(controller.parseTemplateExpression.called).to.be.false;
  330. expect(controller.parseGraphDataset.called).to.be.false;
  331. expect(controller.get('widgetValues')).to.not.be.empty;
  332. expect(controller.get('widgetMetrics')).to.not.be.empty;
  333. });
  334. it("NUMBER widget", function () {
  335. controller.set('expressions', [{}]);
  336. controller.set('content.widgetType', 'NUMBER');
  337. controller.set('dataSets', [{}]);
  338. //controller.updateExpressions();
  339. expect(controller.parseExpression.calledOnce).to.be.true;
  340. expect(controller.parseTemplateExpression.called).to.be.false;
  341. expect(controller.parseGraphDataset.called).to.be.false;
  342. expect(controller.get('widgetValues')).to.not.be.empty;
  343. expect(controller.get('widgetMetrics')).to.not.be.empty;
  344. });
  345. it("TEMPLATE widget", function () {
  346. controller.set('expressions', [{}]);
  347. controller.set('content.widgetType', 'TEMPLATE');
  348. controller.set('dataSets', [{}]);
  349. //controller.updateExpressions();
  350. expect(controller.parseExpression.called).to.be.false;
  351. expect(controller.parseTemplateExpression.calledOnce).to.be.true;
  352. expect(controller.parseGraphDataset.called).to.be.false;
  353. expect(controller.get('widgetValues')).to.not.be.empty;
  354. expect(controller.get('widgetMetrics')).to.not.be.empty;
  355. });
  356. it("GRAPH widget", function () {
  357. controller.set('expressions', [{}]);
  358. controller.set('content.widgetType', 'GRAPH');
  359. controller.set('dataSets', [{}]);
  360. //controller.updateExpressions();
  361. expect(controller.parseExpression.called).to.be.false;
  362. expect(controller.parseTemplateExpression.called).to.be.false;
  363. expect(controller.parseGraphDataset.calledOnce).to.be.true;
  364. expect(controller.get('widgetValues')).to.not.be.empty;
  365. expect(controller.get('widgetMetrics')).to.not.be.empty;
  366. });
  367. });
  368. describe("#parseGraphDataset()", function () {
  369. beforeEach(function () {
  370. sinon.stub(controller, 'parseExpression').returns({value: 'value'});
  371. });
  372. afterEach(function () {
  373. controller.parseExpression.restore();
  374. });
  375. it("empty dataSets", function () {
  376. expect(controller.parseGraphDataset([])).to.be.eql({
  377. metrics: [],
  378. values: []
  379. });
  380. });
  381. it("correct dataSets", function () {
  382. var result = controller.parseGraphDataset([Em.Object.create({label: 'label'})]);
  383. expect(result.values).to.be.eql([
  384. {
  385. "name": "label",
  386. "value": "value"
  387. }
  388. ]);
  389. expect(result.metrics).to.be.empty;
  390. });
  391. });
  392. describe("#parseTemplateExpression()", function () {
  393. beforeEach(function () {
  394. sinon.stub(controller, 'parseExpression').returns({value: 'value'});
  395. });
  396. afterEach(function () {
  397. controller.parseExpression.restore();
  398. });
  399. it("empty expressions", function () {
  400. expect(controller.parseTemplateExpression("{{Expression1}}", [])).to.be.eql({
  401. metrics: [],
  402. values: [
  403. {
  404. value: "{{Expression1}}"
  405. }
  406. ]
  407. });
  408. });
  409. it("correct expressions", function () {
  410. var result = controller.parseTemplateExpression("{{Expression1}}", [Em.Object.create({alias: '{{Expression1}}'})]);
  411. expect(result.values).to.be.eql([
  412. {
  413. value: "value"
  414. }
  415. ]);
  416. expect(result.metrics).to.be.empty;
  417. });
  418. });
  419. describe("#parseExpression()", function () {
  420. it("expression is empty", function () {
  421. expect(controller.parseExpression({data: []})).to.eql({
  422. metrics: [],
  423. value: ''
  424. });
  425. });
  426. it("expression is correct", function () {
  427. var data = [
  428. {
  429. name: 'm1',
  430. serviceName: 'S1',
  431. componentName: 'C1',
  432. metricPath: 'mp',
  433. hostComponentCriteria: 'hcc',
  434. isMetric: true
  435. },
  436. {
  437. name: '+'
  438. },
  439. {
  440. name: '10'
  441. }
  442. ];
  443. expect(controller.parseExpression({data: data})).to.eql({
  444. metrics: [
  445. {
  446. "name": "m1",
  447. "service_name": "S1",
  448. "component_name": "C1",
  449. "metric_path": "mp",
  450. "host_component_criteria": "hcc"
  451. }
  452. ],
  453. value: '${m1+10}'
  454. });
  455. });
  456. });
  457. describe("#updateProperties()", function () {
  458. it("widgetPropertiesViews is empty", function () {
  459. controller.set('widgetPropertiesViews', []);
  460. expect(controller.get('widgetProperties')).to.be.empty;
  461. });
  462. it("widgetPropertiesViews is correct", function () {
  463. controller.set('widgetPropertiesViews', [
  464. Em.Object.create({
  465. valueMap: {
  466. "key1": 'alias1'
  467. },
  468. key1: 1
  469. })
  470. ]);
  471. expect(controller.get('widgetProperties')).to.eql({
  472. "alias1": 1
  473. });
  474. });
  475. });
  476. describe("#renderProperties()", function () {
  477. beforeEach(function () {
  478. this.mock = sinon.stub(App.WidgetType, 'find');
  479. sinon.stub(App.WidgetPropertyTypes, 'findProperty').returns({valueMap: {}});
  480. sinon.stub(App.WidgetProperty, 'create').returns({});
  481. });
  482. afterEach(function () {
  483. this.mock.restore();
  484. App.WidgetPropertyTypes.findProperty.restore();
  485. App.WidgetProperty.create.restore();
  486. });
  487. it("no properties", function () {
  488. this.mock.returns(Em.Object.create({properties: []}));
  489. controller.renderProperties();
  490. expect(controller.get('widgetPropertiesViews')).to.be.empty;
  491. });
  492. it("correct properties", function () {
  493. this.mock.returns(Em.Object.create({properties: [{}]}));
  494. controller.renderProperties();
  495. expect(App.WidgetProperty.create.calledWith({valueMap: {}})).to.be.true;
  496. expect(controller.get('widgetPropertiesViews')).to.not.be.empty;
  497. });
  498. });
  499. describe("#parseValue()", function () {
  500. beforeEach(function () {
  501. sinon.stub(controller, 'getExpressionData').returns({});
  502. });
  503. afterEach(function () {
  504. controller.getExpressionData.restore();
  505. });
  506. it("empty value", function () {
  507. expect(controller.parseValue("", [])).to.be.empty;
  508. });
  509. it("correct value", function () {
  510. expect(controller.parseValue("${m1}", [])).to.not.be.empty;
  511. expect(controller.getExpressionData.calledWith("m1", [])).to.be.true;
  512. });
  513. });
  514. describe("#getExpressionData()", function () {
  515. beforeEach(function () {
  516. sinon.stub(controller, 'getExpressionVariable').returns({});
  517. });
  518. afterEach(function () {
  519. controller.getExpressionVariable.restore();
  520. });
  521. it("empty expression", function () {
  522. expect(controller.getExpressionData("", [])).to.be.empty;
  523. });
  524. it("correct expression", function () {
  525. expect(controller.getExpressionData("m1+10", [])).to.not.be.empty;
  526. expect(controller.getExpressionVariable.getCall(0).args).to.eql(["m1", 1, []]);
  527. expect(controller.getExpressionVariable.getCall(1).args).to.eql(["10", 3, []]);
  528. });
  529. });
  530. describe("#getExpressionVariable()", function () {
  531. it("get metric definition", function () {
  532. var metrics = [
  533. {
  534. name: 'm1',
  535. component_name: 'C1',
  536. service_name: 'S1',
  537. metric_path: 'mp',
  538. host_component_criteria: 'hcc'
  539. }
  540. ];
  541. expect(controller.getExpressionVariable("m1", 1, metrics)).to.be.eql(Em.Object.create({
  542. id: 1,
  543. name: 'm1',
  544. isMetric: true,
  545. componentName: 'C1',
  546. serviceName: 'S1',
  547. metricPath: 'mp',
  548. hostComponentCriteria: 'hcc'
  549. }));
  550. });
  551. it("get number definition", function () {
  552. expect(controller.getExpressionVariable("10", 2, [])).to.be.eql(Em.Object.create({
  553. id: 2,
  554. name: "10",
  555. isNumber: true
  556. }));
  557. });
  558. });
  559. describe("#next()", function () {
  560. beforeEach(function () {
  561. sinon.stub(App.router, 'send');
  562. });
  563. afterEach(function () {
  564. App.router.send.restore();
  565. });
  566. it("", function () {
  567. controller.next();
  568. expect(App.router.send.calledWith('next'));
  569. });
  570. });
  571. });