object_utils_test.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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 objectUtils = require('utils/object_utils');
  19. describe('utils/object_utils', function() {
  20. describe('#recursiveTree()', function() {
  21. var testObj = {
  22. a1: {
  23. a2: 'v1',
  24. a3: {
  25. a4: {
  26. a5: {
  27. a6: 'v2',
  28. a7: 'v3'
  29. }
  30. }
  31. }
  32. }
  33. };
  34. it('should return correct tree of childs', function(){
  35. var result = objectUtils.recursiveTree(testObj);
  36. expect(result).to.be.equal('a2 (/a1)<br/>a5 (/a1/a3/a4)<br/>');
  37. });
  38. it('should return `null` if type missed', function() {
  39. var result = objectUtils.recursiveTree('{ a1: "v1"}');
  40. expect(result).to.be.null;
  41. });
  42. });
  43. describe('#recursiveKeysCount()', function() {
  44. var tests = [
  45. {
  46. m: 'should return 1 child',
  47. e: 3,
  48. obj: {
  49. a1: {
  50. a2: 'v1',
  51. a3: 'v2',
  52. a4: {
  53. a5: 'v3'
  54. }
  55. }
  56. }
  57. },
  58. {
  59. m: 'should return 1 childs',
  60. e: 1,
  61. obj: {
  62. a1: 'c1'
  63. }
  64. },
  65. {
  66. m: 'should return `null`',
  67. e: null,
  68. obj: 'a1'
  69. }
  70. ];
  71. tests.forEach(function(test){
  72. it(test.m, function() {
  73. expect(objectUtils.recursiveKeysCount(test.obj)).to.be.eql(test.e);
  74. });
  75. });
  76. });
  77. describe('#deepEqual', function() {
  78. it('simple values', function() {
  79. expect(objectUtils.deepEqual(true, true)).to.true;
  80. });
  81. it('simple values strict', function() {
  82. expect(objectUtils.deepEqual(true, 1)).to.false;
  83. });
  84. it('simple with complex', function() {
  85. expect(objectUtils.deepEqual(true, {})).to.false;
  86. });
  87. it('complex with simple', function() {
  88. expect(objectUtils.deepEqual({}, 2)).to.false;
  89. });
  90. it('simple objects', function() {
  91. var a = {
  92. value: 1
  93. };
  94. var b = {
  95. value: 1
  96. };
  97. expect(objectUtils.deepEqual(a, b)).to.true;
  98. });
  99. it('simple objects failed', function() {
  100. var a = {
  101. value: 1,
  102. c: 1
  103. };
  104. var b = {
  105. value: 1
  106. };
  107. expect(objectUtils.deepEqual(a, b)).to.false;
  108. });
  109. it('complex objects', function() {
  110. var a = {
  111. value: 1,
  112. c: {
  113. d: {
  114. x: {
  115. val: 1
  116. }
  117. }
  118. }
  119. };
  120. var b = {
  121. value: 1,
  122. c: {
  123. d: {
  124. x: {
  125. val: 1
  126. }
  127. }
  128. }
  129. };
  130. expect(objectUtils.deepEqual(a, b)).to.true;
  131. });
  132. it('complex objects failed', function() {
  133. var a = {
  134. value: 1,
  135. c: {
  136. d: {
  137. x: {
  138. val: 1
  139. }
  140. }
  141. }
  142. };
  143. var b = {
  144. value: 1,
  145. c: {
  146. d: {
  147. x: {
  148. val: 2
  149. }
  150. }
  151. }
  152. };
  153. expect(objectUtils.deepEqual(a, b)).to.false;
  154. });
  155. it('complex array', function() {
  156. var a = [1,2,{a: 2}, 4, {b:{}}];
  157. var b = [1,2,{a: 2}, 4, {b:{}}];
  158. expect(objectUtils.deepEqual(a, b)).to.true;
  159. });
  160. it('complex array failed', function() {
  161. var a = [1,3,{a: 2}, 4, {b:{}}];
  162. var b = [1,2,{a: 2}, 4, {b:{}}];
  163. expect(objectUtils.deepEqual(a, b)).to.false;
  164. });
  165. it('simple array', function() {
  166. var a = [1,3];
  167. var b = [1,3];
  168. expect(objectUtils.deepEqual(a, b)).to.true;
  169. });
  170. it('simple array failed', function() {
  171. var a = [3,1];
  172. var b = [1,3];
  173. expect(objectUtils.deepEqual(a, b)).to.false;
  174. });
  175. });
  176. describe('#deepMerge', function() {
  177. var tests = [
  178. {
  179. target: {
  180. a: [
  181. {
  182. c: 3
  183. }
  184. ]
  185. },
  186. source: {
  187. a: [
  188. {
  189. b: 2
  190. }
  191. ]
  192. },
  193. e: {
  194. a: [
  195. {
  196. c: 3
  197. },
  198. {
  199. b: 2
  200. }
  201. ]
  202. }
  203. },
  204. {
  205. target: {
  206. a: {}
  207. },
  208. source: {
  209. a: {
  210. b: 2,
  211. c: [1,2,3]
  212. }
  213. },
  214. e: {
  215. a: {
  216. b: 2,
  217. c: [1,2,3]
  218. }
  219. }
  220. },
  221. {
  222. target: {
  223. artifact_data: {
  224. services: [
  225. {
  226. name: "HIVE",
  227. configurations: [
  228. {
  229. "hive-site": {
  230. hive_prop1: "hive_val1"
  231. }
  232. }
  233. ]
  234. }
  235. ]
  236. }
  237. },
  238. source: {
  239. artifact_data: {
  240. services: [
  241. {
  242. name: "HDFS",
  243. configurations: [
  244. {
  245. "hdfs-site": {
  246. hdfs_prop1: "hdfs_val1"
  247. }
  248. }
  249. ]
  250. }
  251. ]
  252. }
  253. },
  254. e: {
  255. artifact_data: {
  256. services: [
  257. {
  258. name: "HIVE",
  259. configurations: [
  260. {
  261. "hive-site": {
  262. hive_prop1: "hive_val1"
  263. }
  264. }
  265. ]
  266. },
  267. {
  268. name: "HDFS",
  269. configurations: [
  270. {
  271. "hdfs-site": {
  272. hdfs_prop1: "hdfs_val1"
  273. }
  274. }
  275. ]
  276. }
  277. ]
  278. }
  279. }
  280. },
  281. {
  282. source: {
  283. "artifact_data" : {
  284. "identities" : [
  285. {
  286. "principal" : {
  287. "value" : "HTTP/_HOST@${realm}",
  288. "type" : "service"
  289. },
  290. "name" : "spnego",
  291. "keytab" : {
  292. "file" : "${keytab_dir}/spnego.service.keytab",
  293. "owner" : {
  294. "name" : "root",
  295. "access" : "r"
  296. },
  297. "group" : {
  298. "name" : "${cluster-env/user_group}",
  299. "access" : "r"
  300. }
  301. }
  302. },
  303. {
  304. "principal" : {
  305. "value" : "${cluster-env/smokeuser}-----@${realm}",
  306. "local_username" : "${cluster-env/smokeuser}",
  307. "configuration" : "cluster-env/smokeuser_principal_name",
  308. "type" : "user"
  309. },
  310. "name" : "smokeuser",
  311. "keytab" : {
  312. "file" : "${keytab_dir}/smokeuser.headless.keytab",
  313. "owner" : {
  314. "name" : "${cluster-env/smokeuser}",
  315. "access" : "r"
  316. },
  317. "configuration" : "cluster-env/smokeuser_keytab",
  318. "group" : {
  319. "name" : "${cluster-env/user_group}",
  320. "access" : "r"
  321. }
  322. }
  323. }
  324. ]
  325. }
  326. },
  327. target: {
  328. "artifact_data" : {
  329. "identities" : [
  330. {
  331. "principal" : {
  332. "value" : "${cluster-env/smokeuser}@${realm}",
  333. "local_username" : "${cluster-env/smokeuser}",
  334. "configuration" : "cluster-env/smokeuser_principal_name",
  335. "type" : "user"
  336. },
  337. "name" : "smokeuser",
  338. "keytab" : {
  339. "file" : "${keytab_dir}/smokeuser.headless.keytab",
  340. "owner" : {
  341. "name" : "${cluster-env/smokeuser}",
  342. "access" : "r"
  343. },
  344. "configuration" : "cluster-env/smokeuser_keytab",
  345. "group" : {
  346. "name" : "${cluster-env/user_group}",
  347. "access" : "r"
  348. }
  349. }
  350. },
  351. {
  352. "principal" : {
  353. "value" : "HTTP/_HOST@${realm}",
  354. "local_username" : null,
  355. "configuration" : null,
  356. "type" : "service"
  357. },
  358. "name" : "spnego",
  359. "keytab" : {
  360. "file" : "${keytab_dir}/spnego.service.keytab",
  361. "owner" : {
  362. "name" : "root",
  363. "access" : "r"
  364. },
  365. "configuration" : null,
  366. "group" : {
  367. "name" : "${cluster-env/user_group}",
  368. "access" : "d"
  369. }
  370. }
  371. },
  372. {
  373. "name": "anotherOne"
  374. }
  375. ]
  376. }
  377. },
  378. e: {
  379. "artifact_data" : {
  380. "identities" : [
  381. {
  382. "principal" : {
  383. "value" : "${cluster-env/smokeuser}-----@${realm}",
  384. "local_username" : "${cluster-env/smokeuser}",
  385. "configuration" : "cluster-env/smokeuser_principal_name",
  386. "type" : "user"
  387. },
  388. "name" : "smokeuser",
  389. "keytab" : {
  390. "file" : "${keytab_dir}/smokeuser.headless.keytab",
  391. "owner" : {
  392. "name" : "${cluster-env/smokeuser}",
  393. "access" : "r"
  394. },
  395. "configuration" : "cluster-env/smokeuser_keytab",
  396. "group" : {
  397. "name" : "${cluster-env/user_group}",
  398. "access" : "r"
  399. }
  400. }
  401. },
  402. {
  403. "principal" : {
  404. "value" : "HTTP/_HOST@${realm}",
  405. "local_username" : null,
  406. "configuration" : null,
  407. "type" : "service"
  408. },
  409. "name" : "spnego",
  410. "keytab" : {
  411. "file" : "${keytab_dir}/spnego.service.keytab",
  412. "owner" : {
  413. "name" : "root",
  414. "access" : "r"
  415. },
  416. "configuration" : null,
  417. "group" : {
  418. "name" : "${cluster-env/user_group}",
  419. "access" : "r"
  420. }
  421. }
  422. },
  423. {
  424. "name": "anotherOne"
  425. }
  426. ]
  427. }
  428. }
  429. }
  430. ];
  431. tests.forEach(function(test) {
  432. it("Should merge objects `{0}`, `{1}`".format(JSON.stringify(test.target), JSON.stringify(test.source)), function() {
  433. expect(objectUtils.deepMerge(test.target, test.source, test.handler)).to.be.eql(test.e);
  434. });
  435. });
  436. });
  437. describe('#detectIndexedKey', function() {
  438. var tests = [
  439. {
  440. target: [
  441. {
  442. a: 1,
  443. b: []
  444. },
  445. {
  446. a: 3,
  447. b: 2
  448. },
  449. {
  450. a: 2,
  451. b: {}
  452. }
  453. ],
  454. e: 'a',
  455. m: 'should detect uniq key as `a`'
  456. },
  457. {
  458. target: [
  459. {
  460. "principal" : {
  461. "value" : "HTTP/_HOST@${realm}",
  462. "local_username" : null,
  463. "configuration" : null,
  464. "type" : "service"
  465. },
  466. "name" : "spnego",
  467. "keytab" : {
  468. "file" : "${keytab_dir}/spnego.service.keytab",
  469. "owner" : {
  470. "name" : "root",
  471. "access" : "r"
  472. },
  473. "configuration" : null,
  474. "group" : {
  475. "name" : "${cluster-env/user_group}",
  476. "access" : "r"
  477. }
  478. }
  479. },
  480. {
  481. "principal" : {
  482. "value" : "${cluster-env/smokeuser}-${cluster_name|toLower()}@${realm}",
  483. "local_username" : "${cluster-env/smokeuser}",
  484. "configuration" : "cluster-env/smokeuser_principal_name",
  485. "type" : "user"
  486. },
  487. "name" : "smokeuser",
  488. "keytab" : {
  489. "file" : "${keytab_dir}/smokeuser.headless.keytab",
  490. "owner" : {
  491. "name" : "${cluster-env/smokeuser}",
  492. "access" : "r"
  493. },
  494. "configuration" : "cluster-env/smokeuser_keytab",
  495. "group" : {
  496. "name" : "${cluster-env/user_group}",
  497. "access" : "r"
  498. }
  499. }
  500. }
  501. ],
  502. e: 'name',
  503. m: 'should detect uniq key as `name`'
  504. },
  505. ];
  506. tests.forEach(function(test) {
  507. it(test.m, function() {
  508. expect(objectUtils.detectIndexedKey(test.target)).to.eql(test.e);
  509. });
  510. });
  511. });
  512. describe('#smartArrayObjectMerge', function() {
  513. var tests = [
  514. {
  515. target: [
  516. {
  517. a: 2,
  518. B: 2
  519. }
  520. ],
  521. source: [
  522. {
  523. a: 3,
  524. c: 4
  525. },
  526. ],
  527. m: 'should merge {0} {1}, into {2}',
  528. e: [
  529. {
  530. a: 2,
  531. B: 2
  532. },
  533. {
  534. a: 3,
  535. c: 4
  536. }
  537. ]
  538. },
  539. {
  540. target: [
  541. {
  542. a: 2,
  543. B: 2
  544. }
  545. ],
  546. source: [
  547. {
  548. a: 2,
  549. B: 3,
  550. b: 4
  551. },
  552. {
  553. a: 3,
  554. c: 4
  555. }
  556. ],
  557. m: 'should merge {0} {1}, into {2}',
  558. e: [
  559. {
  560. a: 2,
  561. B: 3,
  562. b: 4
  563. },
  564. {
  565. a: 3,
  566. c: 4
  567. }
  568. ]
  569. },
  570. {
  571. target: [
  572. {
  573. "spark-defaults" : {
  574. "spark.history.kerberos.enabled" : "true",
  575. "spark.history.enabled" : "no"
  576. }
  577. }
  578. ],
  579. source: [
  580. {
  581. "spark-defaults" : {
  582. "spark.history.kerberos.enabled" : "false"
  583. }
  584. },
  585. {
  586. "spark-site" : {
  587. "spark.property" : "false"
  588. }
  589. }
  590. ],
  591. m: 'should merge {0} {1}, into {2}',
  592. e: [
  593. {
  594. "spark-defaults" : {
  595. "spark.history.kerberos.enabled" : "true",
  596. "spark.history.enabled" : "no"
  597. }
  598. },
  599. {
  600. "spark-site" : {
  601. "spark.property" : "false"
  602. }
  603. }
  604. ]
  605. }
  606. ];
  607. tests.forEach(function(test) {
  608. it(test.m.format(JSON.stringify(test.target), JSON.stringify(test.source), JSON.stringify(test.e)), function() {
  609. expect(objectUtils.smartArrayObjectMerge(test.target, test.source).toArray()).to.be.eql(test.e);
  610. });
  611. });
  612. });
  613. });