manage_config_groups_controller_test.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. var c;
  20. describe('App.ManageConfigGroupsController', function() {
  21. var controller = App.ManageConfigGroupsController.create({});
  22. beforeEach(function() {
  23. c = App.ManageConfigGroupsController.create({});
  24. });
  25. var manageConfigGroupsController = App.ManageConfigGroupsController.create({});
  26. describe('#addConfigGroup', function() {
  27. beforeEach(function() {
  28. manageConfigGroupsController.addConfigGroup();
  29. });
  30. describe("#validate", function() {
  31. it("should display no warning if user inputs valid characters into group name", function() {
  32. manageConfigGroupsController.addGroupPopup.set('configGroupName', 'hello');
  33. expect(manageConfigGroupsController.addGroupPopup.warningMessage).to.be.empty;
  34. });
  35. it("should display warning if user inputs invalid characters into group name", function() {
  36. manageConfigGroupsController.addGroupPopup.set('configGroupName', '/{"!@#$%');
  37. expect(manageConfigGroupsController.addGroupPopup.warningMessage).to.equal('Invalid Group Name. Only alphanumerics, hyphens, spaces and underscores are allowed.');
  38. });
  39. });
  40. });
  41. describe('#renameConfigGroup', function() {
  42. beforeEach(function() {
  43. var configGroup = Ember.Object.create ({
  44. name: 'name',
  45. description: 'description'
  46. });
  47. manageConfigGroupsController.set('selectedConfigGroup', configGroup);
  48. manageConfigGroupsController.renameConfigGroup();
  49. });
  50. describe("#validate", function() {
  51. it("should display no warning if user inputs valid characters into group name", function() {
  52. manageConfigGroupsController.renameGroupPopup.set('configGroupName', 'hello');
  53. expect(manageConfigGroupsController.renameGroupPopup.warningMessage).to.be.empty;
  54. });
  55. it("should display warning if user inputs invalid characters into group name", function() {
  56. manageConfigGroupsController.renameGroupPopup.set('configGroupName', '/{"!@#$%');
  57. expect(manageConfigGroupsController.renameGroupPopup.warningMessage).to.equal('Invalid Group Name. Only alphanumerics, hyphens, spaces and underscores are allowed.');
  58. });
  59. });
  60. });
  61. describe('#addHostsCallback', function() {
  62. beforeEach(function() {
  63. c.reopen({
  64. selectedConfigGroup: Em.Object.create({
  65. hosts: ['h1'],
  66. parentConfigGroup: Em.Object.create({
  67. hosts: ['h2', 'h3']
  68. })
  69. })
  70. });
  71. });
  72. it('should set hosts to selectedConfigGroup and remove them form default group', function () {
  73. c.addHostsCallback(['h2', 'h3']);
  74. expect(c.get('selectedConfigGroup.hosts')).to.include.members(['h1','h2','h3']);
  75. expect(c.get('selectedConfigGroup.parentConfigGroup.hosts').toArray()).to.be.empty;
  76. });
  77. });
  78. describe('#isHostsModified', function () {
  79. Em.A([
  80. {
  81. o: {
  82. toClearHosts: [],
  83. toDelete: [],
  84. toSetHosts: [],
  85. toCreate: []
  86. },
  87. e: false
  88. },
  89. {
  90. o: {
  91. toClearHosts: [{}],
  92. toDelete: [],
  93. toSetHosts: [],
  94. toCreate: []
  95. },
  96. e: true
  97. },
  98. {
  99. o: {
  100. toClearHosts: [],
  101. toDelete: [{}],
  102. toSetHosts: [],
  103. toCreate: []
  104. },
  105. e: true
  106. },
  107. {
  108. o: {
  109. toClearHosts: [],
  110. toDelete: [],
  111. toSetHosts: [{}],
  112. toCreate: []
  113. },
  114. e: true
  115. },
  116. {
  117. o: {
  118. toClearHosts: [],
  119. toDelete: [],
  120. toSetHosts: [],
  121. toCreate: [{}]
  122. },
  123. e: true
  124. }
  125. ]).forEach(function (test, index) {
  126. it('test #' + index, function () {
  127. c.reopen({
  128. isLoaded: true,
  129. hostsModifiedConfigGroups: test.o
  130. });
  131. expect(c.get('isHostsModified')).to.equal(test.e);
  132. });
  133. });
  134. });
  135. describe('#deleteConfigGroup', function () {
  136. beforeEach(function() {
  137. var defaultGroup = Em.Object.create({
  138. hosts: ['h2', 'h3'],
  139. isDefault: true
  140. });
  141. var selectedGroup = Em.Object.create({
  142. hosts: ['h1'],
  143. parentConfigGroup: defaultGroup
  144. });
  145. c.reopen({
  146. configGroups: [defaultGroup, selectedGroup],
  147. selectedConfigGroup: selectedGroup
  148. });
  149. });
  150. it('after deleting some config group, Default should be selected', function () {
  151. c.deleteConfigGroup();
  152. expect(c.get('configGroups.length')).to.equal(1);
  153. expect(c.get('selectedConfigGroup.hosts')).to.include.members(['h1','h2','h3']);
  154. expect(c.get('selectedConfigGroup.isDefault')).to.be.true;
  155. });
  156. });
  157. describe("#manageConfigurationGroups", function () {
  158. var service = Em.Object.create({});
  159. manageConfigGroupsController.set('hostsModifiedConfigGroups', {});
  160. describe("#controller passed", function () {
  161. controller = Em.Object.create({
  162. content: Em.Object.create()
  163. });
  164. var popup = manageConfigGroupsController.manageConfigurationGroups(controller, service);
  165. describe("#onPrimary()", function () {
  166. beforeEach(function () {
  167. sinon.stub(popup, 'onPrimaryWizard', Em.K);
  168. });
  169. afterEach(function () {
  170. popup.onPrimaryWizard.restore();
  171. });
  172. it("onPrimaryWizard is called", function () {
  173. popup.onPrimary();
  174. expect(popup.onPrimaryWizard.calledOnce).to.be.true;
  175. });
  176. });
  177. describe("#onPrimaryWizard()", function () {
  178. var ctrl = Em.Object.create({
  179. selectedService: Em.Object.create({
  180. selected: false
  181. }),
  182. selectedServiceObserver: Em.K,
  183. setGroupsToDelete: Em.K
  184. });
  185. beforeEach(function () {
  186. sinon.spy(ctrl, 'selectedServiceObserver');
  187. sinon.spy(ctrl, 'setGroupsToDelete');
  188. sinon.stub(manageConfigGroupsController, 'persistConfigGroups', Em.K);
  189. sinon.stub(popup, 'updateConfigGroupOnServicePage', Em.K);
  190. sinon.stub(popup, 'hide', Em.K);
  191. });
  192. afterEach(function () {
  193. ctrl.setGroupsToDelete.restore();
  194. ctrl.selectedServiceObserver.restore();
  195. manageConfigGroupsController.persistConfigGroups.restore();
  196. popup.updateConfigGroupOnServicePage.restore();
  197. popup.hide.restore();
  198. });
  199. describe("groups deleted on 7th step", function () {
  200. beforeEach(function () {
  201. ctrl.set('name', 'wizardStep7Controller');
  202. popup.onPrimaryWizard(ctrl, {toDelete: [1]});
  203. });
  204. it('selectedServiceObserver is called once', function () {
  205. expect(ctrl.selectedServiceObserver.calledOnce).to.be.true;
  206. });
  207. it('setGroupsToDelete is called with [1]', function () {
  208. expect(ctrl.setGroupsToDelete.calledWith([1])).to.be.true;
  209. });
  210. it('persistConfigGroups is called once', function () {
  211. expect(manageConfigGroupsController.persistConfigGroups.calledOnce).to.be.true;
  212. });
  213. it('updateConfigGroupOnServicePage is called once', function () {
  214. expect(popup.updateConfigGroupOnServicePage.calledOnce).to.be.true;
  215. });
  216. it('hide is called once', function () {
  217. expect(popup.hide.calledOnce).to.be.true;
  218. });
  219. });
  220. describe("wizard not on 7th step", function () {
  221. beforeEach(function () {
  222. ctrl.set('name', '');
  223. popup.onPrimaryWizard(ctrl, {});
  224. });
  225. it('selectedServiceObserver is called once', function () {
  226. expect(ctrl.selectedServiceObserver.calledOnce).to.be.true;
  227. });
  228. it('setGroupsToDelete is not called', function () {
  229. expect(ctrl.setGroupsToDelete.called).to.be.false;
  230. });
  231. it('persistConfigGroups is not called', function () {
  232. expect(manageConfigGroupsController.persistConfigGroups.called).to.be.false;
  233. });
  234. it('updateConfigGroupOnServicePage is not called', function () {
  235. expect(popup.updateConfigGroupOnServicePage.called).to.be.false;
  236. });
  237. it('hide is called once', function () {
  238. expect(popup.hide.calledOnce).to.be.true;
  239. });
  240. });
  241. describe("wizard on 7th step, service selected", function () {
  242. beforeEach(function () {
  243. ctrl.set('name', 'wizardStep7Controller');
  244. ctrl.set('selectedService.selected', true);
  245. popup.onPrimaryWizard(ctrl, {toDelete: [1]});
  246. });
  247. it('selectedServiceObserver is called once', function () {
  248. expect(ctrl.selectedServiceObserver.calledOnce).to.be.true;
  249. });
  250. it('setGroupsToDelete is not called', function () {
  251. expect(ctrl.setGroupsToDelete.called).to.be.false;
  252. });
  253. it('persistConfigGroups is called once', function () {
  254. expect(manageConfigGroupsController.persistConfigGroups.calledOnce).to.be.true;
  255. });
  256. it('updateConfigGroupOnServicePage is called once', function () {
  257. expect(popup.updateConfigGroupOnServicePage.calledOnce).to.be.true;
  258. });
  259. it('hide is called once', function () {
  260. expect(popup.hide.calledOnce).to.be.true;
  261. });
  262. });
  263. describe("wizard on 7th step, no groups to delete", function () {
  264. beforeEach(function () {
  265. ctrl.set('name', 'wizardStep7Controller');
  266. ctrl.set('selectedService.selected', false);
  267. popup.onPrimaryWizard(ctrl, {toDelete: []});
  268. });
  269. it('selectedServiceObserver is called once', function () {
  270. expect(ctrl.selectedServiceObserver.calledOnce).to.be.true;
  271. });
  272. it('setGroupsToDelete is not called', function () {
  273. expect(ctrl.setGroupsToDelete.called).to.be.false;
  274. });
  275. it('persistConfigGroups is called once', function () {
  276. expect(manageConfigGroupsController.persistConfigGroups.calledOnce).to.be.true;
  277. });
  278. it('updateConfigGroupOnServicePage is called once', function () {
  279. expect(popup.updateConfigGroupOnServicePage.calledOnce).to.be.true;
  280. });
  281. it('hide is called once', function () {
  282. expect(popup.hide.calledOnce).to.be.true;
  283. });
  284. });
  285. });
  286. });
  287. describe("#controller not passed", function () {
  288. var popup = manageConfigGroupsController.manageConfigurationGroups(null, service);
  289. describe("#onPrimary()", function () {
  290. beforeEach(function () {
  291. sinon.stub(popup, 'runClearCGQueue').returns({
  292. done: function (callback) {
  293. callback();
  294. }
  295. });
  296. sinon.stub(popup, 'runModifyCGQueue').returns({
  297. done: function (callback) {
  298. callback();
  299. }
  300. });
  301. sinon.stub(popup, 'runCreateCGQueue').returns({
  302. done: function (callback) {
  303. callback();
  304. }
  305. });
  306. sinon.stub(popup, 'updateConfigGroupOnServicePage', Em.K);
  307. sinon.stub(popup, 'hide', Em.K);
  308. manageConfigGroupsController.set('hostsModifiedConfigGroups', {toCreate: []});
  309. popup.onPrimary();
  310. });
  311. afterEach(function () {
  312. popup.runCreateCGQueue.restore();
  313. popup.runModifyCGQueue.restore();
  314. popup.runClearCGQueue.restore();
  315. popup.updateConfigGroupOnServicePage.restore();
  316. popup.hide.restore();
  317. });
  318. it("runClearCGQueue is called", function () {
  319. expect(popup.runClearCGQueue.calledOnce).to.be.true;
  320. });
  321. it("runModifyCGQueue is called", function () {
  322. expect(popup.runModifyCGQueue.calledOnce).to.be.true;
  323. });
  324. it("runCreateCGQueue is called", function () {
  325. expect(popup.runCreateCGQueue.calledOnce).to.be.true;
  326. });
  327. it("updateConfigGroupOnServicePage is called", function () {
  328. expect(popup.updateConfigGroupOnServicePage.calledOnce).to.be.true;
  329. });
  330. it("hide is called", function () {
  331. expect(popup.hide.calledOnce).to.be.true;
  332. });
  333. });
  334. describe("#runClearCGQueue()", function () {
  335. beforeEach(function () {
  336. sinon.stub(manageConfigGroupsController, 'updateConfigurationGroup', Em.K);
  337. sinon.stub(manageConfigGroupsController, 'deleteConfigurationGroup', Em.K);
  338. popup.runClearCGQueue(Em.K, {
  339. initialGroups: [],
  340. toClearHosts: [Em.Object.create()],
  341. toDelete: [1]
  342. });
  343. });
  344. afterEach(function () {
  345. manageConfigGroupsController.updateConfigurationGroup.restore();
  346. manageConfigGroupsController.deleteConfigurationGroup.restore();
  347. });
  348. it("updateConfigurationGroup is called once", function () {
  349. expect(manageConfigGroupsController.updateConfigurationGroup.calledOnce).to.be.true;
  350. });
  351. it("deleteConfigurationGroup is called once", function () {
  352. expect(manageConfigGroupsController.deleteConfigurationGroup.calledOnce).to.be.true;
  353. });
  354. });
  355. describe("#runModifyCGQueue()", function () {
  356. beforeEach(function () {
  357. sinon.stub(manageConfigGroupsController, 'updateConfigurationGroup', Em.K);
  358. });
  359. afterEach(function () {
  360. manageConfigGroupsController.updateConfigurationGroup.restore();
  361. });
  362. it("updateConfigurationGroup is called once", function () {
  363. popup.runModifyCGQueue(Em.K, {toSetHosts: [1]});
  364. expect(manageConfigGroupsController.updateConfigurationGroup.calledOnce).to.be.true;
  365. });
  366. });
  367. describe("#runCreateCGQueue()", function () {
  368. beforeEach(function () {
  369. sinon.stub(manageConfigGroupsController, 'postNewConfigurationGroup', Em.K);
  370. });
  371. afterEach(function () {
  372. manageConfigGroupsController.postNewConfigurationGroup.restore();
  373. });
  374. it("postNewConfigurationGroup is called once", function () {
  375. popup.runCreateCGQueue(Em.K, {toCreate: [1]});
  376. expect(manageConfigGroupsController.postNewConfigurationGroup.calledOnce).to.be.true;
  377. });
  378. });
  379. });
  380. });
  381. describe('#_onLoadPropertiesSuccess', function () {
  382. var data = {
  383. items: [
  384. {
  385. type: 'type1',
  386. tag: 'tag1',
  387. properties: {
  388. prop1: 'val1',
  389. prop2: 'val2'
  390. }
  391. },
  392. {
  393. type: 'type1',
  394. tag: 'tag2',
  395. properties: {
  396. prop3: 'val3'
  397. }
  398. },
  399. {
  400. type: 'type2',
  401. tag: 'tag1',
  402. properties: {
  403. prop4: 'val4'
  404. }
  405. }
  406. ]
  407. };
  408. var params = {
  409. typeTagToGroupMap: {
  410. 'type1///tag1': 'group1',
  411. 'type1///tag2': 'group2',
  412. 'type2///tag1': 'group3'
  413. }
  414. };
  415. var configGroups = [
  416. Em.Object.create({
  417. name: 'group1',
  418. properties: []
  419. }),
  420. Em.Object.create({
  421. name: 'group2',
  422. properties: []
  423. }),
  424. Em.Object.create({
  425. name: 'group3',
  426. properties: []
  427. }),
  428. Em.Object.create({
  429. name: 'group4',
  430. properties: []
  431. })
  432. ];
  433. beforeEach(function () {
  434. sinon.stub(c, 'resortConfigGroup', Em.K);
  435. });
  436. afterEach(function () {
  437. c.resortConfigGroup.restore();
  438. });
  439. it('should set properties to config groups', function () {
  440. c.set('configGroups', configGroups);
  441. c._onLoadPropertiesSuccess(data, null, params);
  442. expect(JSON.stringify(c.get('configGroups'))).to.equal(JSON.stringify([
  443. Em.Object.create({
  444. properties: [
  445. {
  446. name: 'prop1',
  447. value: 'val1',
  448. type: 'type1'
  449. },
  450. {
  451. name: 'prop2',
  452. value: 'val2',
  453. type: 'type1'
  454. }
  455. ],
  456. name: 'group1'
  457. }),
  458. Em.Object.create({
  459. properties: [
  460. {
  461. name: 'prop3',
  462. value: 'val3',
  463. type: 'type1'
  464. }
  465. ],
  466. name: 'group2'
  467. }),
  468. Em.Object.create({
  469. properties: [
  470. {
  471. name: 'prop4',
  472. value: 'val4',
  473. type: 'type2'
  474. }
  475. ],
  476. name: 'group3'
  477. }),
  478. Em.Object.create({
  479. properties: [],
  480. name: 'group4'
  481. })
  482. ]));
  483. });
  484. });
  485. describe('#componentsForFilter', function () {
  486. beforeEach(function () {
  487. sinon.stub(App.StackServiceComponent, 'find', function () {
  488. return [
  489. Em.Object.create({
  490. serviceName: 'HDFS'
  491. }),
  492. Em.Object.create({
  493. serviceName: 'noHDFS'
  494. }),
  495. Em.Object.create({
  496. serviceName: 'HDFS'
  497. })
  498. ];
  499. });
  500. c.set('serviceName', 'HDFS');
  501. });
  502. afterEach(function () {
  503. App.StackServiceComponent.find.restore();
  504. });
  505. it('should map components for current service', function () {
  506. expect(c.get('componentsForFilter')).to.have.property('length').equal(2);
  507. });
  508. it('no one is selected', function () {
  509. expect(c.get('componentsForFilter').mapProperty('selected')).to.be.eql([false, false]);
  510. });
  511. });
  512. });