From 4a52f811b2b3eb39aaa461765a009948ddafd192 Mon Sep 17 00:00:00 2001 From: Irfan Habib Date: Tue, 9 Aug 2016 12:07:02 +0100 Subject: [PATCH 1/2] TEAMFOUR-911 - Add Routes form migrated to Async Task Dialog --- .../add-route/add-route.controller.spec.js | 128 ++++-------- .../summary/add-route/add-route.html | 25 +-- .../summary/add-route/add-route.service.js | 196 ++++++++---------- .../application/summary/summary.module.js | 2 +- 4 files changed, 136 insertions(+), 215 deletions(-) diff --git a/src/plugins/cloud-foundry/view/applications/application/summary/add-route/add-route.controller.spec.js b/src/plugins/cloud-foundry/view/applications/application/summary/add-route/add-route.controller.spec.js index 487dfe67b9..862f9737e1 100644 --- a/src/plugins/cloud-foundry/view/applications/application/summary/add-route/add-route.controller.spec.js +++ b/src/plugins/cloud-foundry/view/applications/application/summary/add-route/add-route.controller.spec.js @@ -1,13 +1,14 @@ (function () { 'use strict'; - describe('AddRouteController', function () { - var addRouteController, $httpBackend, modelManager, $uibModalInstance; + describe('Add-route controller test', function () { + var $httpBackend, addRoutesFactory; var spaceGuid = 'testSpace'; var domainGuid = 'testDomain'; var cnsiGuid = 'testCnsi'; var applicationId = 'testApplicationId'; + var path = 'testpath'; var mockAddRouteResponse = { testCnsi: { metadata: { @@ -15,108 +16,71 @@ } } }; + var data = { + path: null, + port: null, + host: path, + space_guid: spaceGuid, + domain_guid: domainGuid + }; + beforeEach(module('templates')); beforeEach(module('green-box-console')); - beforeEach(module('cloud-foundry.view.applications.application.summary')); + beforeEach(module({ + 'helion.framework.widgets.asyncTaskDialog': function (content, context, actionTask) { + return { + content: content, + context: context, + actionTask: actionTask + }; + } + })); beforeEach(inject(function ($injector) { - - var context = { - data: { - path: null, - port: null, - host: null, - space_guid: spaceGuid, - domain_guid: domainGuid - } - }; - var content = {}; - + addRoutesFactory = $injector.get('cloud-foundry.view.applications.application.summary.addRoutes'); $httpBackend = $injector.get('$httpBackend'); - var $stateParams = $injector.get('$stateParams'); - modelManager = $injector.get('app.model.modelManager'); - $uibModalInstance = { - close: angular.noop, - dismiss: angular.noop - }; - var $controller = $injector.get('$controller'); - addRouteController = $controller('addRouteController', { - $scope: { - $watch: angular.noop - }, - context: context, - content: content, - $stateParams: $stateParams, - modelManager: modelManager, - $uibModalInstance: $uibModalInstance - }); + var modelManager = $injector.get('app.model.modelManager'); - addRouteController.cnsiGuid = cnsiGuid; - addRouteController.applicationId = applicationId; - spyOn($uibModalInstance, 'close').and.callThrough(); - spyOn(addRouteController, 'onAddRouteError').and.callThrough(); + // Initialise model data + var model = modelManager.retrieve('cloud-foundry.model.application'); + var availableDomains = [{name: 'test.com', guid: domainGuid}]; + _.set(model, 'application.summary.available_domains', availableDomains); + _.set(model, 'application.summary.space_guid', spaceGuid); })); it('should be defined', function () { - expect(addRouteController).toBeDefined(); + expect(addRoutesFactory).toBeDefined(); }); - it('should have `host` property initially set to null', function () { - expect(addRouteController.context.data.host).toBe(null); + it('should pass correct content spec to asyncTaskDialog', function () { + var modalObj = addRoutesFactory.add(cnsiGuid, applicationId); + expect(modalObj.content.title).toBeDefined(); + expect(modalObj.content.templateUrl).toBeDefined(); + expect(modalObj.content.buttonTitles.submit).toBeDefined(); }); it('should have `domain_guid`, `space_guid` set to appropriate values', function () { - expect(addRouteController.context.data.space_guid).toEqual(spaceGuid); - expect(addRouteController.context.data.domain_guid).toEqual(domainGuid); + var modalObj = addRoutesFactory.add(cnsiGuid, applicationId); + expect(modalObj.context.data.space_guid).toEqual(spaceGuid); + expect(modalObj.context.data.domain_guid).toEqual(domainGuid); }); it('should successfully add a route', function () { - $httpBackend.when('POST', '/pp/v1/proxy/v2/routes').respond(200, mockAddRouteResponse); - $httpBackend.when('PUT', '/pp/v1/proxy/v2/routes/testGuid/apps/' + applicationId).respond(200, {}); - $httpBackend.when('GET', '/pp/v1/proxy/v2/apps/' + applicationId + '/summary').respond(200, {}); - - addRouteController.addRoute(); - $httpBackend.flush(); - - expect($uibModalInstance.close).toHaveBeenCalled(); - expect(addRouteController.onAddRouteError).not.toHaveBeenCalled(); - }); - - it('should invoke onAddRouteError when failing to associate a route', function () { - $httpBackend.when('POST', '/pp/v1/proxy/v2/routes').respond(200, mockAddRouteResponse); - $httpBackend.when('PUT', '/pp/v1/proxy/v2/routes/testGuid/apps/' + applicationId).respond(500, {}); - - addRouteController.addRoute(); - $httpBackend.flush(); - - expect(addRouteController.onAddRouteError).toHaveBeenCalled(); - expect($uibModalInstance.close).not.toHaveBeenCalled(); - expect(addRouteController.addRouteError).toBe(true); - }); - - it('should invoke onAddRouteError when failing to add a route', function () { - $httpBackend.when('POST', '/pp/v1/proxy/v2/routes').respond(500, mockAddRouteResponse); + var expectedPostReq = { + domain_guid: domainGuid, + host: path, + space_guid: spaceGuid + }; - addRouteController.addRoute(); - $httpBackend.flush(); + var modalObj = addRoutesFactory.add(cnsiGuid, applicationId); - expect(addRouteController.onAddRouteError).toHaveBeenCalled(); - expect($uibModalInstance.close).not.toHaveBeenCalled(); - expect(addRouteController.addRouteError).toBe(true); - }); + $httpBackend.expectPOST('/pp/v1/proxy/v2/routes', expectedPostReq).respond(200, mockAddRouteResponse); + $httpBackend.expectPUT('/pp/v1/proxy/v2/routes/testGuid/apps/testApplicationId').respond(200, {}); + $httpBackend.expectGET('/pp/v1/proxy/v2/apps/' + applicationId + '/summary').respond(200, {}); - it('should invoke onAddRouteError when failing to update AppSummary', function () { - $httpBackend.when('POST', '/pp/v1/proxy/v2/routes').respond(200, mockAddRouteResponse); - $httpBackend.when('PUT', '/pp/v1/proxy/v2/routes/testGuid/apps/' + applicationId).respond(200, {}); - $httpBackend.when('GET', '/pp/v1/proxy/v2/apps/' + applicationId + '/summary').respond(500, {}); - - addRouteController.addRoute(); + modalObj.actionTask(data); $httpBackend.flush(); - - expect(addRouteController.onAddRouteError).toHaveBeenCalled(); - expect($uibModalInstance.close).not.toHaveBeenCalled(); - expect(addRouteController.addRouteError).toBe(true); }); }); diff --git a/src/plugins/cloud-foundry/view/applications/application/summary/add-route/add-route.html b/src/plugins/cloud-foundry/view/applications/application/summary/add-route/add-route.html index 2cf497f95d..dc80f4e052 100644 --- a/src/plugins/cloud-foundry/view/applications/application/summary/add-route/add-route.html +++ b/src/plugins/cloud-foundry/view/applications/application/summary/add-route/add-route.html @@ -1,10 +1,9 @@
-

Add a Route

-
+
A route with this name already exists. Choose a new one - @@ -13,27 +12,11 @@

Add a Route

-
-

- There was a problem adding or associating this route with your app. Please try again. If this error persists, - please contact the administrator. -

-
-
diff --git a/src/plugins/cloud-foundry/view/applications/application/summary/add-route/add-route.service.js b/src/plugins/cloud-foundry/view/applications/application/summary/add-route/add-route.service.js index 97be494c01..10cdedd77f 100644 --- a/src/plugins/cloud-foundry/view/applications/application/summary/add-route/add-route.service.js +++ b/src/plugins/cloud-foundry/view/applications/application/summary/add-route/add-route.service.js @@ -3,20 +3,44 @@ angular .module('cloud-foundry.view.applications.application.summary') - .factory('cloud-foundry.view.applications.application.summary.addRoutes', AddRouteServiceFactory) - .controller('addRouteController', AddRouteController); + .factory('cloud-foundry.view.applications.application.summary.addRoutes', AddRouteServiceFactory); AddRouteServiceFactory.$inject = [ 'app.model.modelManager', - 'helion.framework.widgets.detailView' + 'helion.framework.widgets.asyncTaskDialog' ]; - function AddRouteServiceFactory(modelManager, detailView) { + /** + * @name AddRouteServiceFactory + * @description Factory for getting the Add Route Dialog + * @memberof cloud-foundry.view.applications.application.summary + * @param {app.model.modelManager} modelManager - the Model management service + * @param {object} asyncTaskDialog - async dialog service + * @constructor + */ + function AddRouteServiceFactory(modelManager, asyncTaskDialog) { + + this.routeModel = modelManager.retrieve('cloud-foundry.model.route'); + + var that = this; + return { - add: function () { - var model = modelManager.retrieve('cloud-foundry.model.application'); + + /** + * @name add + * @description Display Add Route Dialog + * @param {String} cnsiGuid - CNSI GUID + * @param {String} applicationId - Application GUID + * @returns {*} asyncTaskDialog + */ + add: function (cnsiGuid, applicationId) { // Create a map of domain names -> domain guids + var model = modelManager.retrieve('cloud-foundry.model.application'); + var domains = []; + var routeExists = false; + var hideAsyncIndicatorContent = false; + model.application.summary.available_domains.forEach(function (domain) { domains.push({ label: domain.name, @@ -31,120 +55,70 @@ space_guid: spaceGuid, domain_guid: domains[0].value }; - return detailView( + + var addRoute = function (contextData) { + + hideAsyncIndicatorContent = false; + routeExists = false; + + var data = { + space_guid: contextData.space_guid, + domain_guid: contextData.domain_guid, + host: contextData.host + }; + + return that.routeModel.createRoute(cnsiGuid, data) + .then(function (response) { + if (!(response.metadata && response.metadata.guid)) { + /* eslint-disable no-throw-literal */ + throw response; + /* eslint-enable no-throw-literal */ + } + var routeId = response.metadata.guid; + return that.routeModel.associateAppWithRoute(cnsiGuid, routeId, applicationId); + }) + + .then(function () { + // Update application summary model + return model.getAppSummary(cnsiGuid, applicationId); + }) + .catch(function (error) { + // check if error is CF-RouteHostTaken indicating that the route has already been created + if (_.isPlainObject(error) && + error.error_code && + error.error_code === 'CF-RouteHostTaken') { + routeExists = true; + hideAsyncIndicatorContent = true; + } + throw error; + }); + }; + + return asyncTaskDialog( { - controller: AddRouteController, - controllerAs: 'addRouteCtrl', - detailViewTemplateUrl: 'plugins/cloud-foundry/view/applications/' + - 'application/summary/add-route/add-route.html' + title: gettext('Add a Route'), + templateUrl: 'plugins/cloud-foundry/view/applications/' + + 'application/summary/add-route/add-route.html', + buttonTitles: { + submit: 'Create route' + } }, { data: data, options: { domains: domains + }, + routeExists: function () { + return routeExists; + }, + hideAsyncIndicatorContent: function () { + return hideAsyncIndicatorContent; } - } - ).result; + }, + addRoute + ); } }; } - AddRouteController.$inject = [ - '$scope', - '$stateParams', - 'app.model.modelManager', - '$uibModalInstance', - 'context' - ]; - - /** - * @name AddRouteController - * @constructor - * @param {Object} $scope - Angular $scope - * @param {Object} $stateParams - the UI router $stateParams service - * @param {app.model.modelManager} modelManager - the Model management service - * @param {Object} $uibModalInstance - the Angular UI Bootstrap $uibModalInstance service - * @param {Object} context - the uibModal context - */ - function AddRouteController($scope, $stateParams, modelManager, $uibModalInstance, context) { - var that = this; - that.addRouteError = false; - that.applicationId = $stateParams.guid; - that.cnsiGuid = $stateParams.cnsiGuid; - that.model = modelManager.retrieve('cloud-foundry.model.application'); - that.routeModel = modelManager.retrieve('cloud-foundry.model.route'); - that.uibModelInstance = $uibModalInstance; - that.context = context; - - that.addRouteError = false; - that.routeExists = false; - - $scope.$watch(function () { - return that.context.data.host; - }, function () { - if (that.routeExists) { - that.routeExists = false; - } - }); - } - - angular.extend(AddRouteController.prototype, { - - addRoute: function () { - var that = this; - var data = { - space_guid: that.context.data.space_guid, - domain_guid: that.context.data.domain_guid, - host: that.context.data.host - }; - - this.routeModel.createRoute(that.cnsiGuid, data) - .then(function (response) { - if (!(response.metadata && response.metadata.guid)) { - /* eslint-disable no-throw-literal */ - throw response; - /* eslint-enable no-throw-literal */ - } - var routeId = response.metadata.guid; - return that.routeModel.associateAppWithRoute(that.cnsiGuid, routeId, that.applicationId); - }) - - .then(function () { - // Update application summary model - return that.model.getAppSummary(that.cnsiGuid, that.applicationId); - }) - - .then(function () { - that.uibModelInstance.close(); - }) - - .catch(function (error) { - // check if error is CF-RouteHostTaken indicating that the route has already been created - if (_.isPlainObject(error) && - error.error_code && - error.error_code === 'CF-RouteHostTaken') { - that.routeExists = true; - return; - } - that.onAddRouteError(); - }); - }, - - /** - * @function cancel - * @description Cancel adding a route. Clear the form and dismiss this form. - */ - cancel: function () { - this.uibModelInstance.dismiss(); - }, - - /** - * @function onAddRouteError - * @description Display error when adding a route - */ - onAddRouteError: function () { - this.addRouteError = true; - } - }); - })(); diff --git a/src/plugins/cloud-foundry/view/applications/application/summary/summary.module.js b/src/plugins/cloud-foundry/view/applications/application/summary/summary.module.js index 5aa24b6056..d01c2b0481 100644 --- a/src/plugins/cloud-foundry/view/applications/application/summary/summary.module.js +++ b/src/plugins/cloud-foundry/view/applications/application/summary/summary.module.js @@ -94,7 +94,7 @@ * @public **/ showAddRouteForm: function () { - this.addRoutesService.add(); + this.addRoutesService.add(this.cnsiGuid, this.id); }, /** From 918f167ca6b3d209f00a9f6a1496ac6cea72fc9b Mon Sep 17 00:00:00 2001 From: Irfan Habib Date: Wed, 10 Aug 2016 09:40:22 +0100 Subject: [PATCH 2/2] Deleted legacy style for add-route --- .../application/summary/add-route/add-route.scss | 8 -------- .../view/applications/application/summary/summary.scss | 1 - 2 files changed, 9 deletions(-) delete mode 100644 src/plugins/cloud-foundry/view/applications/application/summary/add-route/add-route.scss diff --git a/src/plugins/cloud-foundry/view/applications/application/summary/add-route/add-route.scss b/src/plugins/cloud-foundry/view/applications/application/summary/add-route/add-route.scss deleted file mode 100644 index dc6d06b2f4..0000000000 --- a/src/plugins/cloud-foundry/view/applications/application/summary/add-route/add-route.scss +++ /dev/null @@ -1,8 +0,0 @@ -.add-route { - margin-left: $hpe-unit-space * 2; - margin-top: $hpe-unit-space * 2; - - .model-footer { - margin-right: $hpe-unit-space * 5; - } -} diff --git a/src/plugins/cloud-foundry/view/applications/application/summary/summary.scss b/src/plugins/cloud-foundry/view/applications/application/summary/summary.scss index 912364ab84..e6824054a0 100644 --- a/src/plugins/cloud-foundry/view/applications/application/summary/summary.scss +++ b/src/plugins/cloud-foundry/view/applications/application/summary/summary.scss @@ -1,4 +1,3 @@ -@import "add-route/add-route"; @import "edit-app/edit-app"; .application-details .summary .data-body {