123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623 |
- /**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var objectUtils = require('utils/object_utils');
- describe('utils/object_utils', function() {
- describe('#recursiveTree()', function() {
- var testObj = {
- a1: {
- a2: 'v1',
- a3: {
- a4: {
- a5: {
- a6: 'v2',
- a7: 'v3'
- }
- }
- }
- }
- };
- it('should return correct tree of childs', function(){
- var result = objectUtils.recursiveTree(testObj);
- expect(result).to.be.equal('a2 (/a1)<br/>a5 (/a1/a3/a4)<br/>');
- });
- it('should return `null` if type missed', function() {
- var result = objectUtils.recursiveTree('{ a1: "v1"}');
- expect(result).to.be.null;
- });
- });
- describe('#recursiveKeysCount()', function() {
- var tests = [
- {
- m: 'should return 1 child',
- e: 3,
- obj: {
- a1: {
- a2: 'v1',
- a3: 'v2',
- a4: {
- a5: 'v3'
- }
- }
- }
- },
- {
- m: 'should return 1 childs',
- e: 1,
- obj: {
- a1: 'c1'
- }
- },
- {
- m: 'should return `null`',
- e: null,
- obj: 'a1'
- }
- ];
- tests.forEach(function(test){
- it(test.m, function() {
- expect(objectUtils.recursiveKeysCount(test.obj)).to.be.eql(test.e);
- });
- });
- });
- describe('#deepEqual', function() {
- it('simple values', function() {
- expect(objectUtils.deepEqual(true, true)).to.true;
- });
- it('simple values strict', function() {
- expect(objectUtils.deepEqual(true, 1)).to.false;
- });
- it('simple with complex', function() {
- expect(objectUtils.deepEqual(true, {})).to.false;
- });
- it('complex with simple', function() {
- expect(objectUtils.deepEqual({}, 2)).to.false;
- });
- it('simple objects', function() {
- var a = {
- value: 1
- };
- var b = {
- value: 1
- };
- expect(objectUtils.deepEqual(a, b)).to.true;
- });
- it('simple objects failed', function() {
- var a = {
- value: 1,
- c: 1
- };
- var b = {
- value: 1
- };
- expect(objectUtils.deepEqual(a, b)).to.false;
- });
- it('complex objects', function() {
- var a = {
- value: 1,
- c: {
- d: {
- x: {
- val: 1
- }
- }
- }
- };
- var b = {
- value: 1,
- c: {
- d: {
- x: {
- val: 1
- }
- }
- }
- };
- expect(objectUtils.deepEqual(a, b)).to.true;
- });
- it('complex objects failed', function() {
- var a = {
- value: 1,
- c: {
- d: {
- x: {
- val: 1
- }
- }
- }
- };
- var b = {
- value: 1,
- c: {
- d: {
- x: {
- val: 2
- }
- }
- }
- };
- expect(objectUtils.deepEqual(a, b)).to.false;
- });
- it('complex array', function() {
- var a = [1,2,{a: 2}, 4, {b:{}}];
- var b = [1,2,{a: 2}, 4, {b:{}}];
- expect(objectUtils.deepEqual(a, b)).to.true;
- });
- it('complex array failed', function() {
- var a = [1,3,{a: 2}, 4, {b:{}}];
- var b = [1,2,{a: 2}, 4, {b:{}}];
- expect(objectUtils.deepEqual(a, b)).to.false;
- });
- it('simple array', function() {
- var a = [1,3];
- var b = [1,3];
- expect(objectUtils.deepEqual(a, b)).to.true;
- });
- it('simple array failed', function() {
- var a = [3,1];
- var b = [1,3];
- expect(objectUtils.deepEqual(a, b)).to.false;
- });
- });
- describe('#deepMerge', function() {
- var tests = [
- {
- target: {
- a: [
- {
- c: 3
- }
- ]
- },
- source: {
- a: [
- {
- b: 2
- }
- ]
- },
- e: {
- a: [
- {
- c: 3
- },
- {
- b: 2
- }
- ]
- }
- },
- {
- target: {
- a: {}
- },
- source: {
- a: {
- b: 2,
- c: [1,2,3]
- }
- },
- e: {
- a: {
- b: 2,
- c: [1,2,3]
- }
- }
- },
- {
- target: {
- artifact_data: {
- services: [
- {
- name: "HIVE",
- configurations: [
- {
- "hive-site": {
- hive_prop1: "hive_val1"
- }
- }
- ]
- }
- ]
- }
- },
- source: {
- artifact_data: {
- services: [
- {
- name: "HDFS",
- configurations: [
- {
- "hdfs-site": {
- hdfs_prop1: "hdfs_val1"
- }
- }
- ]
- }
- ]
- }
- },
- e: {
- artifact_data: {
- services: [
- {
- name: "HIVE",
- configurations: [
- {
- "hive-site": {
- hive_prop1: "hive_val1"
- }
- }
- ]
- },
- {
- name: "HDFS",
- configurations: [
- {
- "hdfs-site": {
- hdfs_prop1: "hdfs_val1"
- }
- }
- ]
- }
- ]
- }
- }
- },
- {
- source: {
- "artifact_data" : {
- "identities" : [
- {
- "principal" : {
- "value" : "HTTP/_HOST@${realm}",
- "type" : "service"
- },
- "name" : "spnego",
- "keytab" : {
- "file" : "${keytab_dir}/spnego.service.keytab",
- "owner" : {
- "name" : "root",
- "access" : "r"
- },
- "group" : {
- "name" : "${cluster-env/user_group}",
- "access" : "r"
- }
- }
- },
- {
- "principal" : {
- "value" : "${cluster-env/smokeuser}-----@${realm}",
- "local_username" : "${cluster-env/smokeuser}",
- "configuration" : "cluster-env/smokeuser_principal_name",
- "type" : "user"
- },
- "name" : "smokeuser",
- "keytab" : {
- "file" : "${keytab_dir}/smokeuser.headless.keytab",
- "owner" : {
- "name" : "${cluster-env/smokeuser}",
- "access" : "r"
- },
- "configuration" : "cluster-env/smokeuser_keytab",
- "group" : {
- "name" : "${cluster-env/user_group}",
- "access" : "r"
- }
- }
- }
- ]
- }
- },
- target: {
- "artifact_data" : {
- "identities" : [
- {
- "principal" : {
- "value" : "${cluster-env/smokeuser}@${realm}",
- "local_username" : "${cluster-env/smokeuser}",
- "configuration" : "cluster-env/smokeuser_principal_name",
- "type" : "user"
- },
- "name" : "smokeuser",
- "keytab" : {
- "file" : "${keytab_dir}/smokeuser.headless.keytab",
- "owner" : {
- "name" : "${cluster-env/smokeuser}",
- "access" : "r"
- },
- "configuration" : "cluster-env/smokeuser_keytab",
- "group" : {
- "name" : "${cluster-env/user_group}",
- "access" : "r"
- }
- }
- },
- {
- "principal" : {
- "value" : "HTTP/_HOST@${realm}",
- "local_username" : null,
- "configuration" : null,
- "type" : "service"
- },
- "name" : "spnego",
- "keytab" : {
- "file" : "${keytab_dir}/spnego.service.keytab",
- "owner" : {
- "name" : "root",
- "access" : "r"
- },
- "configuration" : null,
- "group" : {
- "name" : "${cluster-env/user_group}",
- "access" : "d"
- }
- }
- },
- {
- "name": "anotherOne"
- }
- ]
- }
- },
- e: {
- "artifact_data" : {
- "identities" : [
- {
- "principal" : {
- "value" : "${cluster-env/smokeuser}-----@${realm}",
- "local_username" : "${cluster-env/smokeuser}",
- "configuration" : "cluster-env/smokeuser_principal_name",
- "type" : "user"
- },
- "name" : "smokeuser",
- "keytab" : {
- "file" : "${keytab_dir}/smokeuser.headless.keytab",
- "owner" : {
- "name" : "${cluster-env/smokeuser}",
- "access" : "r"
- },
- "configuration" : "cluster-env/smokeuser_keytab",
- "group" : {
- "name" : "${cluster-env/user_group}",
- "access" : "r"
- }
- }
- },
- {
- "principal" : {
- "value" : "HTTP/_HOST@${realm}",
- "local_username" : null,
- "configuration" : null,
- "type" : "service"
- },
- "name" : "spnego",
- "keytab" : {
- "file" : "${keytab_dir}/spnego.service.keytab",
- "owner" : {
- "name" : "root",
- "access" : "r"
- },
- "configuration" : null,
- "group" : {
- "name" : "${cluster-env/user_group}",
- "access" : "r"
- }
- }
- },
- {
- "name": "anotherOne"
- }
- ]
- }
- }
- }
- ];
- tests.forEach(function(test) {
- it("Should merge objects `{0}`, `{1}`".format(JSON.stringify(test.target), JSON.stringify(test.source)), function() {
- expect(objectUtils.deepMerge(test.target, test.source, test.handler)).to.be.eql(test.e);
- });
- });
- });
- describe('#detectIndexedKey', function() {
- var tests = [
- {
- target: [
- {
- a: 1,
- b: []
- },
- {
- a: 3,
- b: 2
- },
- {
- a: 2,
- b: {}
- }
- ],
- e: 'a',
- m: 'should detect uniq key as `a`'
- },
- {
- target: [
- {
- "principal" : {
- "value" : "HTTP/_HOST@${realm}",
- "local_username" : null,
- "configuration" : null,
- "type" : "service"
- },
- "name" : "spnego",
- "keytab" : {
- "file" : "${keytab_dir}/spnego.service.keytab",
- "owner" : {
- "name" : "root",
- "access" : "r"
- },
- "configuration" : null,
- "group" : {
- "name" : "${cluster-env/user_group}",
- "access" : "r"
- }
- }
- },
- {
- "principal" : {
- "value" : "${cluster-env/smokeuser}-${cluster_name|toLower()}@${realm}",
- "local_username" : "${cluster-env/smokeuser}",
- "configuration" : "cluster-env/smokeuser_principal_name",
- "type" : "user"
- },
- "name" : "smokeuser",
- "keytab" : {
- "file" : "${keytab_dir}/smokeuser.headless.keytab",
- "owner" : {
- "name" : "${cluster-env/smokeuser}",
- "access" : "r"
- },
- "configuration" : "cluster-env/smokeuser_keytab",
- "group" : {
- "name" : "${cluster-env/user_group}",
- "access" : "r"
- }
- }
- }
- ],
- e: 'name',
- m: 'should detect uniq key as `name`'
- },
- ];
- tests.forEach(function(test) {
- it(test.m, function() {
- expect(objectUtils.detectIndexedKey(test.target)).to.eql(test.e);
- });
- });
- });
- describe('#smartArrayObjectMerge', function() {
- var tests = [
- {
- target: [
- {
- a: 2,
- B: 2
- }
- ],
- source: [
- {
- a: 3,
- c: 4
- },
- ],
- m: 'should merge {0} {1}, into {2}',
- e: [
- {
- a: 2,
- B: 2
- },
- {
- a: 3,
- c: 4
- }
- ]
- },
- {
- target: [
- {
- a: 2,
- B: 2
- }
- ],
- source: [
- {
- a: 2,
- B: 3,
- b: 4
- },
- {
- a: 3,
- c: 4
- }
- ],
- m: 'should merge {0} {1}, into {2}',
- e: [
- {
- a: 2,
- B: 3,
- b: 4
- },
- {
- a: 3,
- c: 4
- }
- ]
- },
- {
- target: [
- {
- "spark-defaults" : {
- "spark.history.kerberos.enabled" : "true",
- "spark.history.enabled" : "no"
- }
- }
- ],
- source: [
- {
- "spark-defaults" : {
- "spark.history.kerberos.enabled" : "false"
- }
- },
- {
- "spark-site" : {
- "spark.property" : "false"
- }
- }
- ],
- m: 'should merge {0} {1}, into {2}',
- e: [
- {
- "spark-defaults" : {
- "spark.history.kerberos.enabled" : "true",
- "spark.history.enabled" : "no"
- }
- },
- {
- "spark-site" : {
- "spark.property" : "false"
- }
- }
- ]
- }
- ];
- tests.forEach(function(test) {
- it(test.m.format(JSON.stringify(test.target), JSON.stringify(test.source), JSON.stringify(test.e)), function() {
- expect(objectUtils.smartArrayObjectMerge(test.target, test.source).toArray()).to.be.eql(test.e);
- });
- });
- });
- });
|