manage_config_groups_controller_test.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. var 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. it("groups deleted on 7th step", function () {
  200. ctrl.set('name', 'wizardStep7Controller');
  201. popup.onPrimaryWizard(ctrl, {toDelete: [1]});
  202. expect(ctrl.selectedServiceObserver.calledOnce).to.be.true;
  203. expect(ctrl.setGroupsToDelete.calledWith([1])).to.be.true;
  204. expect(manageConfigGroupsController.persistConfigGroups.calledOnce).to.be.true;
  205. expect(popup.updateConfigGroupOnServicePage.calledOnce).to.be.true;
  206. expect(popup.hide.calledOnce).to.be.true;
  207. });
  208. it("wizard not on 7th step", function () {
  209. ctrl.set('name', '');
  210. popup.onPrimaryWizard(ctrl, {});
  211. expect(ctrl.selectedServiceObserver.calledOnce).to.be.true;
  212. expect(ctrl.setGroupsToDelete.called).to.be.false;
  213. expect(manageConfigGroupsController.persistConfigGroups.called).to.be.false;
  214. expect(popup.updateConfigGroupOnServicePage.called).to.be.false;
  215. expect(popup.hide.calledOnce).to.be.true;
  216. });
  217. it("wizard on 7th step, service selected", function () {
  218. ctrl.set('name', 'wizardStep7Controller');
  219. ctrl.set('selectedService.selected', true);
  220. popup.onPrimaryWizard(ctrl, {toDelete: [1]});
  221. expect(ctrl.selectedServiceObserver.calledOnce).to.be.true;
  222. expect(ctrl.setGroupsToDelete.called).to.be.false;
  223. expect(manageConfigGroupsController.persistConfigGroups.calledOnce).to.be.true;
  224. expect(popup.updateConfigGroupOnServicePage.calledOnce).to.be.true;
  225. expect(popup.hide.calledOnce).to.be.true;
  226. });
  227. it("wizard on 7th step, no groups to delete", function () {
  228. ctrl.set('name', 'wizardStep7Controller');
  229. ctrl.set('selectedService.selected', false);
  230. popup.onPrimaryWizard(ctrl, {toDelete: []});
  231. expect(ctrl.selectedServiceObserver.calledOnce).to.be.true;
  232. expect(ctrl.setGroupsToDelete.called).to.be.false;
  233. expect(manageConfigGroupsController.persistConfigGroups.calledOnce).to.be.true;
  234. expect(popup.updateConfigGroupOnServicePage.calledOnce).to.be.true;
  235. expect(popup.hide.calledOnce).to.be.true;
  236. });
  237. });
  238. });
  239. describe("#controller not passed", function () {
  240. var popup = manageConfigGroupsController.manageConfigurationGroups(null, service);
  241. describe("#onPrimary()", function () {
  242. beforeEach(function () {
  243. sinon.stub(popup, 'runClearCGQueue').returns({
  244. done: function (callback) {
  245. callback();
  246. }
  247. });
  248. sinon.stub(popup, 'runModifyCGQueue').returns({
  249. done: function (callback) {
  250. callback();
  251. }
  252. });
  253. sinon.stub(popup, 'runCreateCGQueue').returns({
  254. done: function (callback) {
  255. callback();
  256. }
  257. });
  258. sinon.stub(popup, 'updateConfigGroupOnServicePage', Em.K);
  259. sinon.stub(popup, 'hide', Em.K);
  260. manageConfigGroupsController.set('hostsModifiedConfigGroups', {toCreate: []});
  261. popup.onPrimary();
  262. });
  263. afterEach(function () {
  264. popup.runCreateCGQueue.restore();
  265. popup.runModifyCGQueue.restore();
  266. popup.runClearCGQueue.restore();
  267. popup.updateConfigGroupOnServicePage.restore();
  268. popup.hide.restore();
  269. });
  270. it("runClearCGQueue is called", function () {
  271. expect(popup.runClearCGQueue.calledOnce).to.be.true;
  272. });
  273. it("runModifyCGQueue is called", function () {
  274. expect(popup.runModifyCGQueue.calledOnce).to.be.true;
  275. });
  276. it("runCreateCGQueue is called", function () {
  277. expect(popup.runCreateCGQueue.calledOnce).to.be.true;
  278. });
  279. it("updateConfigGroupOnServicePage is called", function () {
  280. expect(popup.updateConfigGroupOnServicePage.calledOnce).to.be.true;
  281. });
  282. it("hide is called", function () {
  283. expect(popup.hide.calledOnce).to.be.true;
  284. });
  285. });
  286. describe("#runClearCGQueue()", function () {
  287. beforeEach(function () {
  288. sinon.stub(manageConfigGroupsController, 'updateConfigurationGroup', Em.K);
  289. sinon.stub(manageConfigGroupsController, 'deleteConfigurationGroup', Em.K);
  290. popup.runClearCGQueue(Em.K, {
  291. initialGroups: [],
  292. toClearHosts: [Em.Object.create()],
  293. toDelete: [1]
  294. });
  295. });
  296. afterEach(function () {
  297. manageConfigGroupsController.updateConfigurationGroup.restore();
  298. manageConfigGroupsController.deleteConfigurationGroup.restore();
  299. });
  300. it("updateConfigurationGroup is called once", function () {
  301. expect(manageConfigGroupsController.updateConfigurationGroup.calledOnce).to.be.true;
  302. });
  303. it("deleteConfigurationGroup is called once", function () {
  304. expect(manageConfigGroupsController.deleteConfigurationGroup.calledOnce).to.be.true;
  305. });
  306. });
  307. describe("#runModifyCGQueue()", function () {
  308. beforeEach(function () {
  309. sinon.stub(manageConfigGroupsController, 'updateConfigurationGroup', Em.K);
  310. });
  311. afterEach(function () {
  312. manageConfigGroupsController.updateConfigurationGroup.restore();
  313. });
  314. it("updateConfigurationGroup is called once", function () {
  315. popup.runModifyCGQueue(Em.K, {toSetHosts: [1]});
  316. expect(manageConfigGroupsController.updateConfigurationGroup.calledOnce).to.be.true;
  317. });
  318. });
  319. describe("#runCreateCGQueue()", function () {
  320. beforeEach(function () {
  321. sinon.stub(manageConfigGroupsController, 'postNewConfigurationGroup', Em.K);
  322. });
  323. afterEach(function () {
  324. manageConfigGroupsController.postNewConfigurationGroup.restore();
  325. });
  326. it("postNewConfigurationGroup is called once", function () {
  327. popup.runCreateCGQueue(Em.K, {toCreate: [1]});
  328. expect(manageConfigGroupsController.postNewConfigurationGroup.calledOnce).to.be.true;
  329. });
  330. });
  331. });
  332. });
  333. describe('#_onLoadPropertiesSuccess', function () {
  334. beforeEach(function () {
  335. sinon.stub(c, 'resortConfigGroup', Em.K);
  336. });
  337. afterEach(function () {
  338. c.resortConfigGroup.restore();
  339. });
  340. it('should set properties to config groups', function () {
  341. c.set('configGroups', [
  342. Em.Object.create({
  343. name: 'group1',
  344. properties: []
  345. }),
  346. Em.Object.create({
  347. name: 'group2',
  348. properties: []
  349. }),
  350. Em.Object.create({
  351. name: 'group3',
  352. properties: []
  353. }),
  354. Em.Object.create({
  355. name: 'group4',
  356. properties: []
  357. })
  358. ]);
  359. var data = {
  360. items: [
  361. {
  362. type: 'type1',
  363. tag: 'tag1',
  364. properties: {
  365. prop1: 'val1',
  366. prop2: 'val2'
  367. }
  368. },
  369. {
  370. type: 'type1',
  371. tag: 'tag2',
  372. properties: {
  373. prop3: 'val3'
  374. }
  375. },
  376. {
  377. type: 'type2',
  378. tag: 'tag1',
  379. properties: {
  380. prop4: 'val4'
  381. }
  382. }
  383. ]
  384. };
  385. var params = {
  386. typeTagToGroupMap: {
  387. 'type1///tag1': 'group1',
  388. 'type1///tag2': 'group2',
  389. 'type2///tag1': 'group3'
  390. }
  391. };
  392. c._onLoadPropertiesSuccess(data, null, params);
  393. expect(JSON.stringify(c.get('configGroups'))).to.equal(JSON.stringify([
  394. Em.Object.create({
  395. properties: [
  396. {
  397. name: 'prop1',
  398. value: 'val1',
  399. type: 'type1'
  400. },
  401. {
  402. name: 'prop2',
  403. value: 'val2',
  404. type: 'type1'
  405. }
  406. ],
  407. name: 'group1'
  408. }),
  409. Em.Object.create({
  410. properties: [
  411. {
  412. name: 'prop3',
  413. value: 'val3',
  414. type: 'type1'
  415. }
  416. ],
  417. name: 'group2'
  418. }),
  419. Em.Object.create({
  420. properties: [
  421. {
  422. name: 'prop4',
  423. value: 'val4',
  424. type: 'type2'
  425. }
  426. ],
  427. name: 'group3'
  428. }),
  429. Em.Object.create({
  430. properties: [],
  431. name: 'group4'
  432. })
  433. ]));
  434. });
  435. });
  436. });