helper_test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. it('should convert command to readable info', function() {
  182. expect(App.format.commandDetail(command)).to.eql(' Ganglia Monitor Stop');
  183. });
  184. it('should ignore decommission command', function(){
  185. expect(App.format.commandDetail(ignored)).to.eql(' NameNode');
  186. });
  187. it('should remove SERVICE string from command', function(){
  188. expect(App.format.commandDetail(removeString)).to.eql(' HDFS Stop');
  189. });
  190. it('should return maintenance message', function() {
  191. expect(App.format.commandDetail(nagiosState)).to.eql(' Toggle Maintenance Mode');
  192. });
  193. });
  194. describe('#taskStatus()', function(){
  195. var testable = [
  196. { status: 'PENDING', expectable: 'pending'},
  197. { status: 'QUEUED', expectable: 'queued'},
  198. { status: 'COMPLETED', expectable: 'completed'}
  199. ];
  200. testable.forEach(function(testObj){
  201. it('should convert `' + testObj.status + '` to `' + testObj.expectable + '`', function(){
  202. expect(App.format.taskStatus(testObj.status)).to.eql(testObj.expectable);
  203. });
  204. });
  205. });
  206. describe('#normalizeName()', function() {
  207. var testMessage = '`{0}` should be converted to `{1}`';
  208. var tests = {
  209. 'APP_TIMELINE_SERVER': 'App Timeline Server',
  210. 'DATANODE': 'DataNode',
  211. 'DECOMMISSION_DATANODE': 'Update Exclude File',
  212. 'DRPC_SERVER': 'DRPC Server',
  213. 'FALCON': 'Falcon',
  214. 'FALCON_CLIENT': 'Falcon Client',
  215. 'FALCON_SERVER': 'Falcon Server',
  216. 'FALCON_SERVICE_CHECK': 'Falcon Service Check',
  217. 'FLUME_HANDLER': 'Flume',
  218. 'FLUME_SERVICE_CHECK': 'Flume Service Check',
  219. 'GANGLIA_MONITOR': 'Ganglia Monitor',
  220. 'GANGLIA_SERVER': 'Ganglia Server',
  221. 'GLUSTERFS_CLIENT': 'GLUSTERFS Client',
  222. 'GLUSTERFS_SERVICE_CHECK': 'GLUSTERFS Service Check',
  223. 'GMETAD_SERVICE_CHECK': 'Gmetad Service Check',
  224. 'GMOND_SERVICE_CHECK': 'Gmond Service Check',
  225. 'HADOOP_CLIENT': 'Hadoop Client',
  226. 'HBASE_CLIENT': 'HBase Client',
  227. 'HBASE_MASTER': 'HBase Master',
  228. 'HBASE_REGIONSERVER': 'RegionServer',
  229. 'HBASE_SERVICE_CHECK': 'HBase Service Check',
  230. 'HCAT': 'HCat',
  231. 'HCATALOG': 'HCatalog',
  232. 'HCAT_SERVICE_CHECK': 'HCat Service Check',
  233. 'HDFS': 'HDFS',
  234. 'HDFS_CLIENT': 'HDFS Client',
  235. 'HDFS_SERVICE_CHECK': 'HDFS Service Check',
  236. 'HISTORYSERVER': 'History Server',
  237. 'HIVE_CLIENT': 'Hive Client',
  238. 'HIVE_METASTORE': 'Hive Metastore',
  239. 'HIVE_SERVER': 'HiveServer2',
  240. 'HIVE_SERVICE_CHECK': 'Hive Service Check',
  241. 'HUE_SERVER': 'Hue Server',
  242. 'JAVA_JCE': 'Java JCE',
  243. 'JOBTRACKER': 'JobTracker',
  244. 'JOBTRACKER_SERVICE_CHECK': 'JobTracker Service Check',
  245. 'JOURNALNODE': 'JournalNode',
  246. 'KERBEROS_ADMIN_CLIENT': 'Kerberos Admin Client',
  247. 'KERBEROS_CLIENT': 'Kerberos Client',
  248. 'KERBEROS_SERVER': 'Kerberos Server',
  249. 'MAPREDUCE2_CLIENT': 'MapReduce2 Client',
  250. 'MAPREDUCE2_SERVICE_CHECK': 'MapReduce2 Service Check',
  251. 'MAPREDUCE_CLIENT': 'MapReduce Client',
  252. 'MAPREDUCE_SERVICE_CHECK': 'MapReduce Service Check',
  253. 'MYSQL_SERVER': 'MySQL Server',
  254. 'NAGIOS_SERVER': 'Nagios Server',
  255. 'NAMENODE': 'NameNode',
  256. 'NAMENODE_SERVICE_CHECK': 'NameNode Service Check',
  257. 'NIMBUS': 'Nimbus',
  258. 'NODEMANAGER': 'NodeManager',
  259. 'OOZIE_CLIENT': 'Oozie Client',
  260. 'OOZIE_SERVER': 'Oozie Server',
  261. 'OOZIE_SERVICE_CHECK': 'Oozie Service Check',
  262. 'PIG': 'Pig',
  263. 'PIG_SERVICE_CHECK': 'Pig Service Check',
  264. 'RESOURCEMANAGER': 'ResourceManager',
  265. 'SECONDARY_NAMENODE': 'SNameNode',
  266. 'SQOOP': 'Sqoop',
  267. 'SQOOP_SERVICE_CHECK': 'Sqoop Service Check',
  268. 'STORM_REST_API': 'Storm REST API Server',
  269. 'STORM_SERVICE_CHECK': 'Storm Service Check',
  270. 'STORM_UI_SERVER': 'Storm UI Server',
  271. 'SUPERVISOR': 'Supervisor',
  272. 'TASKTRACKER': 'TaskTracker',
  273. 'TEZ_CLIENT': 'Tez Client',
  274. 'WEBHCAT_SERVER': 'WebHCat Server',
  275. 'WEBHCAT_SERVICE_CHECK': 'WebHCat Service Check',
  276. 'YARN_CLIENT': 'YARN Client',
  277. 'YARN_SERVICE_CHECK': 'YARN Service Check',
  278. 'ZKFC': 'ZKFailoverController',
  279. 'ZOOKEEPER_CLIENT': 'ZooKeeper Client',
  280. 'ZOOKEEPER_QUORUM_SERVICE_CHECK': 'ZK Quorum Service Check',
  281. 'ZOOKEEPER_SERVER': 'ZooKeeper Server',
  282. 'ZOOKEEPER_SERVICE_CHECK': 'ZooKeeper Service Check',
  283. 'CLIENT': 'Client'
  284. };
  285. for (var inputName in tests) {
  286. (function(name) {
  287. it(testMessage.format(name, tests[name]), function() {
  288. expect(App.format.normalizeName(name)).to.eql(tests[name]);
  289. });
  290. })(inputName)
  291. }
  292. });
  293. });
  294. });
  295. describe('#App.permit()', function() {
  296. var obj = {
  297. a1: 'v1',
  298. a2: 'v2',
  299. a3: 'v3'
  300. }
  301. var tests = [
  302. {
  303. keys: 'a1',
  304. e: {
  305. a1: 'v1'
  306. }
  307. },
  308. {
  309. keys: ['a2','a3','a4'],
  310. e: {
  311. a2: 'v2',
  312. a3: 'v3'
  313. }
  314. }
  315. ];
  316. tests.forEach(function(test) {
  317. it('should return object `{0}` permitted keys `{1}`'.format(JSON.stringify(test.e), JSON.stringify(test.keys)), function() {
  318. expect(App.permit(obj, test.keys)).to.deep.eql(test.e);
  319. });
  320. });
  321. });
  322. });