Repository: ambari Updated Branches: refs/heads/trunk 662a0af63 -> f5f40f534
AMBARI-5524. Unit tests for number_utils, string_utils, validator and misc files. (onechiporenko) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/f5f40f53 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/f5f40f53 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/f5f40f53 Branch: refs/heads/trunk Commit: f5f40f53446b1da96634223bcd4b3a81c3e25f5c Parents: 662a0af Author: Oleg Nechiporenko <[email protected]> Authored: Tue Apr 22 16:21:28 2014 +0300 Committer: Oleg Nechiporenko <[email protected]> Committed: Tue Apr 22 16:39:38 2014 +0300 ---------------------------------------------------------------------- .../app/controllers/wizard/step10_controller.js | 4 +- ambari-web/app/messages.js | 12 +--- ambari-web/app/utils/number_utils.js | 5 ++ ambari-web/app/utils/validator.js | 3 +- ambari-web/test/utils/misc_test.js | 23 ++++++ ambari-web/test/utils/number_utils_test.js | 76 ++++++++++++++++++++ ambari-web/test/utils/string_utils_test.js | 10 ++- ambari-web/test/utils/validator_test.js | 64 +++++++++++++---- 8 files changed, 168 insertions(+), 29 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/f5f40f53/ambari-web/app/controllers/wizard/step10_controller.js ---------------------------------------------------------------------- diff --git a/ambari-web/app/controllers/wizard/step10_controller.js b/ambari-web/app/controllers/wizard/step10_controller.js index 2e6860e..c73f403 100644 --- a/ambari-web/app/controllers/wizard/step10_controller.js +++ b/ambari-web/app/controllers/wizard/step10_controller.js @@ -100,7 +100,7 @@ App.WizardStep10Controller = Em.Controller.extend({ } var succeededHosts = hostsInfo.filterProperty('status', 'success'); var warnedHosts = hostsInfo.filter(function(host) { - return ['warning', 'failed'].contains(host.get('status')); + return ['warning', 'failed'].contains(host.status); }); if (succeededHosts.length) { var successStatement = Em.I18n.t('installer.step10.servicesSummary').format(succeededHosts.length) + ((succeededHosts.length > 1) ? Em.I18n.t('installer.step8.hosts') : Em.I18n.t('installer.step8.host')); @@ -270,4 +270,4 @@ App.WizardStep10Controller = Em.Controller.extend({ } } -}); \ No newline at end of file +}); http://git-wip-us.apache.org/repos/asf/ambari/blob/f5f40f53/ambari-web/app/messages.js ---------------------------------------------------------------------- diff --git a/ambari-web/app/messages.js b/ambari-web/app/messages.js index cae4da6..27f80d6 100644 --- a/ambari-web/app/messages.js +++ b/ambari-web/app/messages.js @@ -681,17 +681,7 @@ Em.I18n.translations = { 'installer.step10.taskStatus.timedOut':' timed out on ', 'installer.step10.installStatus.failed':'Installing master services failed', 'installer.step10.installStatus.installed':'Master services installed', - 'installer.step10.master.nameNode':'NameNode installed on ', - 'installer.step10.master.secondaryNameNode':'SecondaryNameNode installed on ', - 'installer.step10.master.jobTracker':'JobTracker installed on ', - 'installer.step10.master.historyServer':'History Server installed on ', - 'installer.step10.master.resourceManager':'ResourceManager installed on ', - 'installer.step10.master.zooKeeper':'ZooKeeper installed on ', - 'installer.step10.master.hbase':'HBase Master installed on ', - 'installer.step10.master.hiveMetastore':'Hive Metastore installed on ', - 'installer.step10.master.oozie':'Oozie Server installed on ', - 'installer.step10.master.ganglia':'Ganglia Server installed on ', - 'installer.step10.master.nagios':'Nagios Server installed on ', + 'installer.step10.master.installedOn':'{0} installed on {1}', 'installer.step10.startStatus.failed':'Starting services failed', 'installer.step10.startStatus.passed':'All tests passed', 'installer.step10.startStatus.started':'All services started', http://git-wip-us.apache.org/repos/asf/ambari/blob/f5f40f53/ambari-web/app/utils/number_utils.js ---------------------------------------------------------------------- diff --git a/ambari-web/app/utils/number_utils.js b/ambari-web/app/utils/number_utils.js index bc6e577..d118977 100644 --- a/ambari-web/app/utils/number_utils.js +++ b/ambari-web/app/utils/number_utils.js @@ -57,6 +57,11 @@ module.exports = { * Validates if the given string or number is an integer between the * values of min and max (inclusive). The minimum and maximum * checks are ignored if their valid is NaN. + * + * @method validateInteger + * @param {string|number} str - input string + * @param {string|number} [min] + * @param {string|number} [max] */ validateInteger : function(str, min, max) { if (str==null || str==undefined || (str + "").trim().length < 1) { http://git-wip-us.apache.org/repos/asf/ambari/blob/f5f40f53/ambari-web/app/utils/validator.js ---------------------------------------------------------------------- diff --git a/ambari-web/app/utils/validator.js b/ambari-web/app/utils/validator.js index 9978e77..e59532f 100644 --- a/ambari-web/app/utils/validator.js +++ b/ambari-web/app/utils/validator.js @@ -77,7 +77,7 @@ module.exports = { * @return {Boolean} */ isIpAddress: function(value) { - var ipRegex = /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(\:[0-9]{1,5})?$/; + var ipRegex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)($|\:[0-9]{1,5})$/; return ipRegex.test(value); }, @@ -137,6 +137,7 @@ module.exports = { case "0": case null: case false: + case undefined: case typeof this == "undefined": return true; default : http://git-wip-us.apache.org/repos/asf/ambari/blob/f5f40f53/ambari-web/test/utils/misc_test.js ---------------------------------------------------------------------- diff --git a/ambari-web/test/utils/misc_test.js b/ambari-web/test/utils/misc_test.js index fd5696d..3ce91b8 100644 --- a/ambari-web/test/utils/misc_test.js +++ b/ambari-web/test/utils/misc_test.js @@ -84,4 +84,27 @@ describe('misc', function () { }); }); + describe('#xmlToObject()', function(){ + var xml = '<!-- Edited by XMLSpy -->'+ + '<stacks name="HDP-2.1">'+ + '<service>'+ + '<name>NAGIOS</name>'+ + '<name>OOZIE</name>'+ + '<name>HDFS</name>'+ + '<component>NAGIOS_SERVER</component>'+ + '</service>'+ + '</stacks>'; + xml = new DOMParser().parseFromString(xml,"text/xml"); + var converted = misc.xmlToObject(xml); + it('should be an object', function(){ + expect(converted).to.a('object'); + }); + it('`attribute` name should be present', function(){ + expect(converted.stacks).to.ok; + }); + it('`stacks.service.name` should be an array', function() { + expect(converted.stacks.service.name).to.a('array'); + }); + }); + }); http://git-wip-us.apache.org/repos/asf/ambari/blob/f5f40f53/ambari-web/test/utils/number_utils_test.js ---------------------------------------------------------------------- diff --git a/ambari-web/test/utils/number_utils_test.js b/ambari-web/test/utils/number_utils_test.js index 42fcb1f..0ad6167 100644 --- a/ambari-web/test/utils/number_utils_test.js +++ b/ambari-web/test/utils/number_utils_test.js @@ -39,6 +39,30 @@ describe('', function() { multiplyBy: null, e: 'n/a', m: '"n/a" if bytes is undefined' + }, + { + bytes: 200, + precision: null, + parseType: undefined, + multiplyBy: null, + e: '0 Bytes', + m: '0 if multiply is `null`' + }, + { + bytes: 200, + precision: null, + parseType: undefined, + multiplyBy: undefined, + e: '200 Bytes', + m: '"200 Bytes" if `multiplyBy` and `parseType` are `undefined`' + }, + { + bytes: 200, + precision: null, + parseType: undefined, + multiplyBy: 1, + e: '200 Bytes', + m: '`200 Bytes` if `parsetype` is `undefined`' } ]); @@ -168,5 +192,57 @@ describe('', function() { }); }); + describe('#validateInteger()', function() { + var tests = [ + { + str: null, + min: null, + max: null, + m: 'all params null to' + Em.I18n.t('number.validate.empty'), + e: Em.I18n.t('number.validate.empty') + }, + { + str: "string", + min: null, + max: null, + m: 'try to validate `string` should return ' + Em.I18n.t('number.validate.empty'), + e: Em.I18n.t('number.validate.notValidNumber') + }, + { + str: "string", + min: null, + max: null, + m: 'try to validate `string` should return ' + Em.I18n.t('number.validate.notValidNumber'), + e: Em.I18n.t('number.validate.notValidNumber') + }, + { + str: "1abc", + min: null, + max: null, + m: 'try to validate `1abc` should return ' + Em.I18n.t('number.validate.notValidNumber'), + e: Em.I18n.t('number.validate.notValidNumber') + }, + { + str: "1", + min: null, + max: null, + m: 'try to validate `1` should return ' + Em.I18n.t('number.validate.moreThanMaximum').format(null), + e: Em.I18n.t('number.validate.moreThanMaximum').format(null) + }, + { + str: "1", + min: 2, + max: 0, + m: 'try to validate `1` with max = 0 and min = 2 should return ' + Em.I18n.t('number.validate.lessThanMinumum').format(2), + e: Em.I18n.t('number.validate.lessThanMinumum').format(2) + } + ]; + + tests.forEach(function(test) { + it(test.m, function(){ + expect(numberUtils.validateInteger(test.str, test.min, test.max)).to.eql(test.e); + }); + }); + }); }); \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/f5f40f53/ambari-web/test/utils/string_utils_test.js ---------------------------------------------------------------------- diff --git a/ambari-web/test/utils/string_utils_test.js b/ambari-web/test/utils/string_utils_test.js index 9fb5095..4dadcd0 100644 --- a/ambari-web/test/utils/string_utils_test.js +++ b/ambari-web/test/utils/string_utils_test.js @@ -45,7 +45,8 @@ describe('string_utils', function () { {m: '"name" to "name"', i: 'name', l: 4, a: 1, f: ' ', e: 'name'}, {m: '"name" to "||||||||name"', i: 'name', l: 8, a:1, f: '||', e: '||||||||name'}, {m: '"name" to "||||name||||"', i: 'name', l: 8, a:3, f: '||', e: '||||name||||'}, - {m: '"name" to "name||||||||"', i: 'name', l: 8, a:2, f: '||', e: 'name||||||||'} + {m: '"name" to "name||||||||"', i: 'name', l: 8, a:2, f: '||', e: 'name||||||||'}, + {m: '"name" to "name" `str` param passed only', i: 'name', e: 'name'} ]; tests.forEach(function(test) { it(test.m + ' ', function () { @@ -61,7 +62,8 @@ describe('string_utils', function () { {m: '1.3 higher than 1.2', v1:'1.3', v2:'1.2', e: 1}, {m: '1.2.1 higher than 1.2', v1:'1.2.1', v2:'1.2', e: 1}, {m: '11.2 higher than 2.2', v1:'11.2', v2:'2.2', e: 1}, - {m: '0.9 higher than 0.8', v1:'0.9', v2:'0.8', e: 1} + {m: '0.9 higher than 0.8', v1:'0.9', v2:'0.8', e: 1}, + {m: 'return false if no string passed', v1: '0.9', e: false} ]; tests.forEach(function(test) { it(test.m + ' ', function () { @@ -145,7 +147,9 @@ describe('string_utils', function () { {i:'aAA. bBB',e:'Aaa. Bbb'}, {i:'STARTING',e:'Starting'}, {i:'starting',e:'Starting'}, - {i:'starting,ending',e:'Starting,Ending'} + {i:'starting,ending',e:'Starting,Ending'}, + {i: null, e: null}, + {i: undefined, e: undefined} ]; tests.forEach(function(test) { it(test.i + ' to ' + test.e + ' ', function () { http://git-wip-us.apache.org/repos/asf/ambari/blob/f5f40f53/ambari-web/test/utils/validator_test.js ---------------------------------------------------------------------- diff --git a/ambari-web/test/utils/validator_test.js b/ambari-web/test/utils/validator_test.js index 1561dc3..262c068 100644 --- a/ambari-web/test/utils/validator_test.js +++ b/ambari-web/test/utils/validator_test.js @@ -137,7 +137,7 @@ describe('validator', function () { }) }); - /*describe('#isIpAddress(value)', function () { + describe('#isIpAddress(value)', function () { it('"127.0.0.1" - valid IP', function () { expect(validator.isIpAddress('127.0.0.1')).to.equal(true); }) @@ -171,16 +171,8 @@ describe('validator', function () { it('"327.0.0.0:45555" - invalid IP', function () { expect(validator.isIpAddress('327.0.0.0:45555')).to.equal(false); }) - it('"0.0.0.0" - invalid IP', function () { - expect(validator.isIpAddress('0.0.0.0')).to.equal(false); - }) - it('"0.0.0.0:12" - invalid IP', function () { - expect(validator.isIpAddress('0.0.0.0:12')).to.equal(false); - }) - it('"1.0.0.0:0" - invalid IP', function () { - expect(validator.isIpAddress('1.0.0.0:0')).to.equal(false); - }) - })*/ + }); + describe('#isDomainName(value)', function () { it('"google.com" - valid Domain Name', function () { expect(validator.isDomainName('google.com')).to.equal(true); @@ -198,6 +190,54 @@ describe('validator', function () { expect(validator.isDomainName('55454')).to.equal(false); }) }); + + describe('#hasSpaces()', function(){ + var testable = [ + { str: ' hello', detect: true }, + { str: 'hello world', detect: true }, + { str: 'hello ', detect: true }, + { str: 'hello', detect: false } + ]; + testable.forEach(function(value){ + it('should ' + (value.detect ? '' : 'not') + ' detects spaces in `' + value.str + '`', function(){ + expect(validator.hasSpaces(value.str)).to.eql(value.detect); + }); + }); + }); + describe('#isNotTrimmed', function(){ + var testable = [ + { str: ' hello world', detect: true }, + { str: ' hello world ', detect: true }, + { str: 'hello world ', detect: true }, + { str: 'hello world', detect: false }, + { str: 'hello world !', detect: false } + ]; + testable.forEach(function(value){ + it('should ' + (value.detect ? '' : 'not') + 'trimmed string', function() { + expect(validator.isNotTrimmed(value.str)).to.eql(value.detect); + }); + }); + }); + describe('#empty()', function(){ + var testable = [ + { obj: "", detect: true }, + { obj: 0, detect: true }, + { obj: "0", detect: true }, + { obj: null, detect: true }, + { obj: undefined, detect: true }, + { obj: 'hello', detect: false }, + { obj: {}, detect: false }, + { obj: [], detect: false }, + { obj: ['a'], detect: false }, + { obj: 1, detect: false }, + { obj: true, detect: false } + ]; + testable.forEach(function(value){ + it('should ' + (value.detect ? '' : 'not') + ' detect empty value in `' + new String(value.obj) + '`', function(){ + expect(validator.empty(value.obj)).to.eql(value.detect); + }); + }); + }); describe('#isValidUserName(value)', function() { var tests = [ {m:'"" - invalid',i:'',e:false}, @@ -292,4 +332,4 @@ describe('validator', function () { }) }); }) -}); \ No newline at end of file +});
