helper_test.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. require('utils/helper');
  20. describe('utils/helper', function() {
  21. describe('String helpers', function() {
  22. describe('#trim()', function(){
  23. it('should replace first space', function() {
  24. expect(' as d f'.trim()).to.eql('as d f');
  25. });
  26. });
  27. describe('#endsWith()', function() {
  28. it('`abcd` ends with `d`', function(){
  29. expect('abcd'.endsWith('d')).to.eql(true);
  30. });
  31. it('`abcd` doesn\'t end with `f`', function(){
  32. expect('abcd'.endsWith('f')).to.eql(false);
  33. });
  34. });
  35. describe('#contains()', function() {
  36. it('`abc` contains b', function(){
  37. expect('abc'.contains('b')).to.eql(true);
  38. });
  39. it('`abc` doesn\'t contain d', function() {
  40. expect('abc'.contains('d')).to.eql(false);
  41. });
  42. });
  43. describe('#capitalize()',function() {
  44. it('`abc d` should start with `A`', function() {
  45. expect('abc d'.capitalize()).to.eql('Abc d');
  46. });
  47. });
  48. describe('#findIn()', function(){
  49. var obj = {
  50. a: {
  51. a1: 'AVal1'
  52. },
  53. b: 'BVal',
  54. c: {
  55. c1: {
  56. c2: 'Cval2'
  57. },
  58. b: 'BVal'
  59. }
  60. };
  61. var testValue = function(key, value) {
  62. it('key `' + key + '` should have `' + JSON.stringify(value) + '` value', function() {
  63. expect(key.findIn(obj)).to.eql(value);
  64. });
  65. };
  66. it('expect return `null` on non-object input', function(){
  67. expect('a'.findIn('b')).to.null;
  68. });
  69. testValue('a', obj.a);
  70. testValue('c2', obj.c.c1.c2);
  71. testValue('b', obj.b);
  72. testValue('d', null);
  73. });
  74. describe('#format()', function(){
  75. it('should replace string correctly', function(){
  76. expect("{0} world{1}".format("Hello","!")).to.eql("Hello world!");
  77. });
  78. });
  79. describe('#highlight()', function() {
  80. var str = "Hello world! I want to highlight this word!";
  81. it('should highlight `word` with default template', function() {
  82. var result = str.highlight(['word']);
  83. expect(result).to.eql("Hello world! I want to highlight this <b>word</b>!");
  84. });
  85. it('should highlight `world` and `word` with template `<span class="yellow">{0}</span>`', function() {
  86. var result = str.highlight(["world", "word"], '<span class="yellow">{0}</span>');
  87. expect(result).to.eql('Hello <span class="yellow">world</span>! I want to highlight this <span class="yellow">word</span>!')
  88. });
  89. var str2 = "First word, second word";
  90. it('should highlight `word` multiply times with default template', function() {
  91. var result = str2.highlight(["word"]);
  92. expect(result).to.eql("First <b>word</b>, second <b>word</b>");
  93. });
  94. });
  95. });
  96. describe('Number helpers', function(){
  97. describe('#toDaysHoursMinutes()', function(){
  98. var time = 1000000000;
  99. var minute = 1000*60;
  100. var hour = 60*minute;
  101. var day = 24*hour;
  102. var result = time.toDaysHoursMinutes();
  103. var testDays = Math.floor(time/day);
  104. it('should correct convert days', function(){
  105. expect(testDays).to.eql(result.d);
  106. });
  107. it('should correct convert hours', function(){
  108. expect(Math.floor((time - testDays * day)/hour)).to.eql(result.h);
  109. });
  110. it('should correct convert minutes', function(){
  111. expect(((time - Math.floor((time - testDays*day)/hour)*hour - testDays*day)/minute).toFixed(2)).to.eql(result.m);
  112. });
  113. });
  114. });
  115. describe('Array helpers', function(){
  116. describe('#sortPropertyLight()', function(){
  117. var testable = [
  118. { a: 2 },
  119. { a: 1 },
  120. { a: 6},
  121. { a: 64},
  122. { a: 3},
  123. { a: 3}
  124. ];
  125. var result = testable.sortPropertyLight('a');
  126. it('should return array with same length', function(){
  127. expect(testable.length).to.eql(result.length);
  128. });
  129. it('should sort array', function() {
  130. result.forEach(function(resultObj, index, resultArr) {
  131. if (index > resultArr.length - 1)
  132. expect(resultObj.a < resultArr[index + 1].a).to.eql(false);
  133. });
  134. });
  135. it('should try to sort without throwing exception', function(){
  136. expect(testable.sortPropertyLight(['a'])).to.ok;
  137. });
  138. });
  139. });
  140. describe('App helpers', function(){
  141. var appendDiv = function() {
  142. $('body').append('<div id="tooltip-test"></div>');
  143. };
  144. var removeDiv = function() {
  145. $('body').remove('#tooltip-test');
  146. };
  147. describe('#isEmptyObject', function(){
  148. it('should return true on empty object', function() {
  149. expect(App.isEmptyObject({})).to.eql(true);
  150. });
  151. it('should return false on non-empty object', function() {
  152. expect(App.isEmptyObject({ a: 1 })).to.eql(false);
  153. });
  154. });
  155. describe('#parseJSON()', function(){
  156. var testable = '{"hello": "world"}';
  157. expect(App.parseJSON(testable).hello).to.eql('world');
  158. });
  159. describe('#tooltip()', function() {
  160. beforeEach(appendDiv);
  161. afterEach(removeDiv);
  162. it('should add tooltip', function() {
  163. var tooltip = App.tooltip($('#tooltip-test'));
  164. expect($('#tooltip-test').data('tooltip').enabled).to.eql(true);
  165. });
  166. });
  167. describe('#popover()', function() {
  168. beforeEach(appendDiv);
  169. afterEach(removeDiv);
  170. it('should add popover', function() {
  171. var tooltip = App.popover($('#tooltip-test'));
  172. expect($('#tooltip-test').data('popover').enabled).to.eql(true);
  173. });
  174. });
  175. describe('#App.format', function(){
  176. describe('#commandDetail()', function() {
  177. var command = "GANGLIA_MONITOR STOP";
  178. var ignored = "DECOMMISSION, NAMENODE";
  179. var removeString = "SERVICE/HDFS STOP";
  180. var nagiosState = "nagios_update_ignore ACTIONEXECUTE";
  181. var installRepo = "install_packages ACTIONEXECUTE";
  182. it('should convert command to readable info', function() {
  183. expect(App.format.commandDetail(command)).to.eql(' Ganglia Monitor Stop');
  184. });
  185. it('should ignore decommission command', function(){
  186. expect(App.format.commandDetail(ignored)).to.eql(' NameNode');
  187. });
  188. it('should remove SERVICE string from command', function(){
  189. expect(App.format.commandDetail(removeString)).to.eql(' HDFS Stop');
  190. });
  191. it('should return maintenance message', function() {
  192. expect(App.format.commandDetail(nagiosState)).to.eql(' Toggle Maintenance Mode');
  193. });
  194. it('should return install repo message', function() {
  195. expect(App.format.commandDetail(installRepo)).to.eql(Em.I18n.t('common.installRepo.task'));
  196. });
  197. });
  198. describe('#taskStatus()', function(){
  199. var testable = [
  200. { status: 'PENDING', expectable: 'pending'},
  201. { status: 'QUEUED', expectable: 'queued'},
  202. { status: 'COMPLETED', expectable: 'completed'}
  203. ];
  204. testable.forEach(function(testObj){
  205. it('should convert `' + testObj.status + '` to `' + testObj.expectable + '`', function(){
  206. expect(App.format.taskStatus(testObj.status)).to.eql(testObj.expectable);
  207. });
  208. });
  209. });
  210. describe('#normalizeName()', function() {
  211. var testMessage = '`{0}` should be converted to `{1}`';
  212. var tests = {
  213. 'APP_TIMELINE_SERVER': 'App Timeline Server',
  214. 'DATANODE': 'DataNode',
  215. 'DECOMMISSION_DATANODE': 'Update Exclude File',
  216. 'DRPC_SERVER': 'DRPC Server',
  217. 'FALCON': 'Falcon',
  218. 'FALCON_CLIENT': 'Falcon Client',
  219. 'FALCON_SERVER': 'Falcon Server',
  220. 'FALCON_SERVICE_CHECK': 'Falcon Service Check',
  221. 'FLUME_HANDLER': 'Flume',
  222. 'FLUME_SERVICE_CHECK': 'Flume Service Check',
  223. 'GANGLIA_MONITOR': 'Ganglia Monitor',
  224. 'GANGLIA_SERVER': 'Ganglia Server',
  225. 'GLUSTERFS_CLIENT': 'GLUSTERFS Client',
  226. 'GLUSTERFS_SERVICE_CHECK': 'GLUSTERFS Service Check',
  227. 'GMETAD_SERVICE_CHECK': 'Gmetad Service Check',
  228. 'GMOND_SERVICE_CHECK': 'Gmond Service Check',
  229. 'HADOOP_CLIENT': 'Hadoop Client',
  230. 'HBASE_CLIENT': 'HBase Client',
  231. 'HBASE_MASTER': 'HBase Master',
  232. 'HBASE_REGIONSERVER': 'RegionServer',
  233. 'HBASE_SERVICE_CHECK': 'HBase Service Check',
  234. 'HCAT': 'HCat Client',
  235. 'HDFS': 'HDFS',
  236. 'HDFS_CLIENT': 'HDFS Client',
  237. 'HDFS_SERVICE_CHECK': 'HDFS Service Check',
  238. 'HISTORYSERVER': 'History Server',
  239. 'HIVE_CLIENT': 'Hive Client',
  240. 'HIVE_METASTORE': 'Hive Metastore',
  241. 'HIVE_SERVER': 'HiveServer2',
  242. 'HIVE_SERVICE_CHECK': 'Hive Service Check',
  243. 'HUE_SERVER': 'Hue Server',
  244. 'JAVA_JCE': 'Java JCE',
  245. 'JOBTRACKER': 'JobTracker',
  246. 'JOBTRACKER_SERVICE_CHECK': 'JobTracker Service Check',
  247. 'JOURNALNODE': 'JournalNode',
  248. 'KERBEROS_ADMIN_CLIENT': 'Kerberos Admin Client',
  249. 'KERBEROS_CLIENT': 'Kerberos Client',
  250. 'KERBEROS_SERVER': 'Kerberos Server',
  251. 'MAPREDUCE2_CLIENT': 'MapReduce2 Client',
  252. 'MAPREDUCE2_SERVICE_CHECK': 'MapReduce2 Service Check',
  253. 'MYSQL_SERVER': 'MySQL Server',
  254. 'NAMENODE': 'NameNode',
  255. 'NAMENODE_SERVICE_CHECK': 'NameNode Service Check',
  256. 'NIMBUS': 'Nimbus',
  257. 'NODEMANAGER': 'NodeManager',
  258. 'OOZIE_CLIENT': 'Oozie Client',
  259. 'OOZIE_SERVER': 'Oozie Server',
  260. 'OOZIE_SERVICE_CHECK': 'Oozie Service Check',
  261. 'PIG': 'Pig',
  262. 'PIG_SERVICE_CHECK': 'Pig Service Check',
  263. 'RESOURCEMANAGER': 'ResourceManager',
  264. 'SECONDARY_NAMENODE': 'SNameNode',
  265. 'SQOOP': 'Sqoop',
  266. 'SQOOP_SERVICE_CHECK': 'Sqoop Service Check',
  267. 'STORM_REST_API': 'Storm REST API Server',
  268. 'STORM_SERVICE_CHECK': 'Storm Service Check',
  269. 'STORM_UI_SERVER': 'Storm UI Server',
  270. 'SUPERVISOR': 'Supervisor',
  271. 'TASKTRACKER': 'TaskTracker',
  272. 'TEZ_CLIENT': 'Tez Client',
  273. 'WEBHCAT_SERVER': 'WebHCat Server',
  274. 'YARN_CLIENT': 'YARN Client',
  275. 'YARN_SERVICE_CHECK': 'YARN Service Check',
  276. 'ZKFC': 'ZKFailoverController',
  277. 'ZOOKEEPER_CLIENT': 'ZooKeeper Client',
  278. 'ZOOKEEPER_QUORUM_SERVICE_CHECK': 'ZK Quorum Service Check',
  279. 'ZOOKEEPER_SERVER': 'ZooKeeper Server',
  280. 'ZOOKEEPER_SERVICE_CHECK': 'ZooKeeper Service Check',
  281. 'CLIENT': 'Client'
  282. };
  283. for (var inputName in tests) {
  284. (function(name) {
  285. it(testMessage.format(name, tests[name]), function() {
  286. expect(App.format.normalizeName(name)).to.eql(tests[name]);
  287. });
  288. })(inputName)
  289. }
  290. });
  291. });
  292. });
  293. describe('#App.permit()', function() {
  294. var obj = {
  295. a1: 'v1',
  296. a2: 'v2',
  297. a3: 'v3'
  298. }
  299. var tests = [
  300. {
  301. keys: 'a1',
  302. e: {
  303. a1: 'v1'
  304. }
  305. },
  306. {
  307. keys: ['a2','a3','a4'],
  308. e: {
  309. a2: 'v2',
  310. a3: 'v3'
  311. }
  312. }
  313. ];
  314. tests.forEach(function(test) {
  315. it('should return object `{0}` permitted keys `{1}`'.format(JSON.stringify(test.e), JSON.stringify(test.keys)), function() {
  316. expect(App.permit(obj, test.keys)).to.deep.eql(test.e);
  317. });
  318. });
  319. });
  320. describe('#App.keysUnderscoreToCamelCase()', function() {
  321. var tests = [
  322. {
  323. object: {
  324. 'key_upper': '2'
  325. },
  326. expected: {
  327. keyUpper: '2'
  328. },
  329. m: 'One level object, key should be camelCased'
  330. },
  331. {
  332. object: {
  333. 'key_upper': '2',
  334. 'key': '1'
  335. },
  336. expected: {
  337. keyUpper: '2',
  338. key: '1'
  339. },
  340. m: 'One level object, one key should be camelCased.'
  341. },
  342. {
  343. object: {
  344. 'key_upper': '2',
  345. 'key': '1'
  346. },
  347. expected: {
  348. keyUpper: '2',
  349. key: '1'
  350. },
  351. m: 'One level object, one key should be camelCased.'
  352. },
  353. {
  354. object: {
  355. 'key_upper': '2',
  356. 'key_upone_uptwo_upthree': '4',
  357. 'key': '1'
  358. },
  359. expected: {
  360. keyUpper: '2',
  361. keyUponeUptwoUpthree: '4',
  362. key: '1'
  363. },
  364. m: 'One level object, two keys should be camelCased, few dots notation.'
  365. }
  366. ];
  367. tests.forEach(function(test) {
  368. it(test.m, function() {
  369. expect(App.keysUnderscoreToCamelCase(test.object)).to.deep.equal(test.expected);
  370. });
  371. });
  372. });
  373. describe('#App.keysDottedToCamelCase()', function() {
  374. var tests = [
  375. {
  376. object: {
  377. 'key.upper': '2'
  378. },
  379. expected: {
  380. keyUpper: '2'
  381. },
  382. m: 'One level object, key should be camelCased'
  383. },
  384. {
  385. object: {
  386. 'key.upper': '2',
  387. 'key': '1'
  388. },
  389. expected: {
  390. keyUpper: '2',
  391. key: '1'
  392. },
  393. m: 'One level object, one key should be camelCased.'
  394. },
  395. {
  396. object: {
  397. 'key.upper': '2',
  398. 'key': '1'
  399. },
  400. expected: {
  401. keyUpper: '2',
  402. key: '1'
  403. },
  404. m: 'One level object, one key should be camelCased.'
  405. },
  406. {
  407. object: {
  408. 'key.upper': '2',
  409. 'key.upone.uptwo.upthree': '4',
  410. 'key': '1'
  411. },
  412. expected: {
  413. keyUpper: '2',
  414. keyUponeUptwoUpthree: '4',
  415. key: '1'
  416. },
  417. m: 'One level object, two keys should be camelCased, few dots notation.'
  418. }
  419. ];
  420. tests.forEach(function(test) {
  421. it(test.m, function() {
  422. expect(App.keysDottedToCamelCase(test.object)).to.deep.equal(test.expected);
  423. });
  424. });
  425. });
  426. });