widget_mixin_test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. describe('App.WidgetMixin', function() {
  20. var mixinClass = Em.Object.extend(App.WidgetMixin, {metrics: [], content: {}});
  21. describe('#loadMetrics()', function () {
  22. var mixinObject = mixinClass.create();
  23. beforeEach(function () {
  24. this.mock = sinon.stub(mixinObject, 'getRequestData');
  25. sinon.stub(mixinObject, 'getHostComponentMetrics').returns({always: function(callback){
  26. callback();
  27. }});
  28. sinon.stub(mixinObject, 'getServiceComponentMetrics').returns({complete: function(callback){
  29. callback();
  30. }});
  31. sinon.stub(mixinObject, 'onMetricsLoaded');
  32. });
  33. afterEach(function () {
  34. this.mock.restore();
  35. mixinObject.getHostComponentMetrics.restore();
  36. mixinObject.getServiceComponentMetrics.restore();
  37. mixinObject.onMetricsLoaded.restore();
  38. });
  39. it('has host_component_criteria', function () {
  40. this.mock.returns({'key1': {host_component_criteria: 'criteria'}});
  41. mixinObject.set('isLoaded', false);
  42. mixinObject.loadMetrics();
  43. expect(mixinObject.getHostComponentMetrics.calledWith({host_component_criteria: 'criteria'})).to.be.true;
  44. expect(mixinObject.onMetricsLoaded.calledOnce).to.be.true;
  45. });
  46. it('host_component_criteria is absent', function () {
  47. this.mock.returns({'key1': {}});
  48. mixinObject.set('isLoaded', false);
  49. mixinObject.loadMetrics();
  50. expect(mixinObject.getServiceComponentMetrics.calledWith({})).to.be.true;
  51. expect(mixinObject.onMetricsLoaded.calledOnce).to.be.true;
  52. });
  53. });
  54. describe("#extractExpressions()", function() {
  55. var mixinObject = mixinClass.create();
  56. var testCases = [
  57. {
  58. data: '',
  59. result: []
  60. },
  61. {
  62. data: 'text',
  63. result: []
  64. },
  65. {
  66. data: 'text${a}',
  67. result: ['a']
  68. },
  69. {
  70. data: 'text${a} - ${a.b}',
  71. result: ['a', 'a.b']
  72. },
  73. {
  74. data: '${o.a-(b+4)/cc*tt}',
  75. result: ['o.a-(b+4)/cc*tt']
  76. }
  77. ];
  78. testCases.forEach(function (test) {
  79. it('input: ' + test.data, function () {
  80. var input = {value: test.data};
  81. expect(mixinObject.extractExpressions(input)).to.eql(test.result);
  82. });
  83. });
  84. });
  85. describe("#getRequestData()", function() {
  86. var mixinObject = mixinClass.create();
  87. it("", function() {
  88. var data = [
  89. {
  90. "name": "regionserver.Server.percentFilesLocal",
  91. "metric_path": "metrics/hbase/regionserver/percentFilesLocal",
  92. "service_name": "HBASE",
  93. "component_name": "HBASE_REGIONSERVER"
  94. },
  95. {
  96. "name": "regionserver.Server.percentFilesLocal2",
  97. "metric_path": "w2",
  98. "service_name": "HBASE",
  99. "component_name": "HBASE_REGIONSERVER"
  100. },
  101. {
  102. "name": "regionserver.Server.percentFilesLocal",
  103. "metric_path": "metrics/hbase/regionserver/percentFilesLocal",
  104. "service_name": "HBASE",
  105. "component_name": "HBASE_REGIONSERVER",
  106. "host_component_criteria": 'c1'
  107. },
  108. {
  109. "name": "regionserver.Server.percentFilesLocal",
  110. "metric_path": "metrics/hbase/regionserver/percentFilesLocal",
  111. "service_name": "HDFS",
  112. "component_name": "DATANODE",
  113. "host_component_criteria": 'c1'
  114. }
  115. ];
  116. expect(mixinObject.getRequestData(data)).to.eql({
  117. "HBASE_HBASE_REGIONSERVER": {
  118. "name": "regionserver.Server.percentFilesLocal",
  119. "service_name": "HBASE",
  120. "component_name": "HBASE_REGIONSERVER",
  121. "metric_paths": [
  122. "metrics/hbase/regionserver/percentFilesLocal",
  123. "w2"
  124. ]
  125. },
  126. "HBASE_HBASE_REGIONSERVER_c1": {
  127. "name": "regionserver.Server.percentFilesLocal",
  128. "service_name": "HBASE",
  129. "component_name": "HBASE_REGIONSERVER",
  130. "host_component_criteria": "c1",
  131. "metric_paths": [
  132. "metrics/hbase/regionserver/percentFilesLocal"
  133. ]
  134. },
  135. "HDFS_DATANODE_c1": {
  136. "name": "regionserver.Server.percentFilesLocal",
  137. "service_name": "HDFS",
  138. "component_name": "DATANODE",
  139. "host_component_criteria": "c1",
  140. "metric_paths": [
  141. "metrics/hbase/regionserver/percentFilesLocal"
  142. ]
  143. }
  144. });
  145. });
  146. });
  147. describe("#getServiceComponentMetrics()", function () {
  148. var mixinObject = mixinClass.create();
  149. before(function () {
  150. sinon.stub(App.ajax, 'send');
  151. });
  152. after(function () {
  153. App.ajax.send.restore();
  154. });
  155. it("", function () {
  156. var request = {
  157. service_name: 'S1',
  158. component_name: 'C1',
  159. metric_paths: ['w1', 'w2']
  160. };
  161. mixinObject.getServiceComponentMetrics(request);
  162. expect(App.ajax.send.getCall(0).args[0]).to.eql({
  163. name: 'widgets.serviceComponent.metrics.get',
  164. sender: mixinObject,
  165. data: {
  166. serviceName: 'S1',
  167. componentName: 'C1',
  168. metricPaths: 'w1,w2'
  169. },
  170. success: 'getMetricsSuccessCallback'
  171. })
  172. });
  173. });
  174. describe("#getMetricsSuccessCallback()", function () {
  175. var mixinObject = mixinClass.create();
  176. it("", function () {
  177. var data = {
  178. metrics: {
  179. "hbase": {
  180. "ipc": {
  181. "IPC": {
  182. "numOpenConnections": 11.5
  183. }
  184. }
  185. }
  186. }
  187. };
  188. mixinObject.set('content.metrics', [
  189. {
  190. metric_path: 'metrics/hbase/ipc/IPC/numOpenConnections'
  191. }
  192. ]);
  193. mixinObject.getMetricsSuccessCallback(data);
  194. expect(mixinObject.get('metrics').findProperty('metric_path', 'metrics/hbase/ipc/IPC/numOpenConnections').data).to.equal(11.5);
  195. });
  196. });
  197. describe("#getHostComponentMetrics()", function () {
  198. var mixinObject = mixinClass.create();
  199. before(function () {
  200. sinon.stub(App.ajax, 'send').returns({done: function(callback){
  201. callback();
  202. return this;
  203. },fail: function(callback){
  204. callback();
  205. return this;
  206. }});
  207. sinon.stub(mixinObject, 'getHostComponentName').returns({done: function(callback){
  208. var data = {host_components: [{HostRoles:{host_name:"c6401"}}]};
  209. callback(data);
  210. return this;
  211. },fail: function(callback){
  212. callback();
  213. return this;
  214. }});
  215. sinon.stub(mixinObject, 'getMetricsSuccessCallback')
  216. });
  217. after(function () {
  218. App.ajax.send.restore();
  219. mixinObject.getHostComponentName.restore();
  220. mixinObject.getMetricsSuccessCallback.restore();
  221. });
  222. it("", function () {
  223. var request = {
  224. component_name: 'C1',
  225. metric_paths: ['w1', 'w2'],
  226. host_component_criteria: 'c1'
  227. };
  228. mixinObject.getHostComponentMetrics(request);
  229. expect(App.ajax.send.getCall(0).args[0]).to.eql({
  230. name: 'widgets.hostComponent.metrics.get',
  231. sender: mixinObject,
  232. data: {
  233. componentName: 'C1',
  234. hostName: "c6401",
  235. metricPaths: 'w1,w2'
  236. }
  237. })
  238. });
  239. });
  240. describe("#calculateValues()", function() {
  241. var mixinObject = mixinClass.create();
  242. beforeEach(function () {
  243. sinon.stub(mixinObject, 'extractExpressions');
  244. this.mock = sinon.stub(mixinObject, 'computeExpression');
  245. });
  246. afterEach(function () {
  247. mixinObject.extractExpressions.restore();
  248. this.mock.restore();
  249. });
  250. it("value compute correctly", function() {
  251. this.mock.returns({'${a}': 1});
  252. mixinObject.set('content.values', [{
  253. value: '${a}'
  254. }]);
  255. mixinObject.calculateValues();
  256. expect(mixinObject.get('content.values')[0].computedValue).to.equal('1');
  257. });
  258. it("value not available", function() {
  259. this.mock.returns({});
  260. mixinObject.set('content.values', [{
  261. value: '${a}'
  262. }]);
  263. mixinObject.calculateValues();
  264. expect(mixinObject.get('content.values')[0].computedValue).to.be.empty;
  265. });
  266. it("value is null", function() {
  267. this.mock.returns({'${a}': null});
  268. mixinObject.set('content.values', [{
  269. value: '${a}'
  270. }]);
  271. mixinObject.calculateValues();
  272. expect(mixinObject.get('content.values')[0].computedValue).to.be.empty;
  273. });
  274. });
  275. describe("#computeExpression()", function() {
  276. var mixinObject = mixinClass.create();
  277. it("expression missing metrics", function() {
  278. var expressions = ['e.m1'];
  279. var metrics = [];
  280. expect(mixinObject.computeExpression(expressions, metrics)).to.eql({
  281. "${e.m1}": ""
  282. });
  283. });
  284. it("Value is not correct mathematical expression", function() {
  285. var expressions = ['e.m1'];
  286. var metrics = [{
  287. name: 'e.m1',
  288. data: 'a+1'
  289. }];
  290. expect(mixinObject.computeExpression(expressions, metrics)).to.eql({
  291. "${e.m1}": ""
  292. });
  293. });
  294. it("correct expression", function() {
  295. var expressions = ['e.m1+e.m1'];
  296. var metrics = [{
  297. name: 'e.m1',
  298. data: 1
  299. }];
  300. expect(mixinObject.computeExpression(expressions, metrics)).to.eql({
  301. "${e.m1+e.m1}": "2"
  302. });
  303. });
  304. });
  305. describe("#cloneWidget()", function() {
  306. var mixinObject = mixinClass.create();
  307. before(function () {
  308. sinon.spy(App, 'showConfirmationPopup');
  309. sinon.stub(mixinObject, 'postWidgetDefinition', Em.K);
  310. });
  311. after(function () {
  312. App.showConfirmationPopup.restore();
  313. mixinObject.postWidgetDefinition.restore();
  314. });
  315. it("", function() {
  316. var popup = mixinObject.cloneWidget();
  317. expect(App.showConfirmationPopup.calledOnce).to.be.true;
  318. popup.onPrimary();
  319. expect(mixinObject.postWidgetDefinition.calledOnce).to.be.true;
  320. });
  321. });
  322. describe("#postWidgetDefinition()", function() {
  323. var mixinObject = mixinClass.create();
  324. before(function () {
  325. sinon.spy(App.ajax, 'send');
  326. sinon.stub(mixinObject, 'collectWidgetData').returns({});
  327. });
  328. after(function () {
  329. App.ajax.send.restore();
  330. mixinObject.collectWidgetData.restore();
  331. });
  332. it("", function() {
  333. mixinObject.postWidgetDefinition();
  334. expect(App.ajax.send.getCall(0).args[0]).to.eql({
  335. name: 'widgets.wizard.add',
  336. sender: mixinObject,
  337. data: {
  338. data: {}
  339. },
  340. success: 'postWidgetDefinitionSuccessCallback'
  341. });
  342. });
  343. });
  344. });