Move listFunction and add javascript tests for cluster services

This patch move listFunction and urlFunction  in module.js into service.js
It is same as horizon's implementation and it will be able to add tests.

This patch adds javascript tests for the following modules.
 - horizon.dashboard.container-infra.clusters.service
 - horizon.dashboard.container-infra.clusterTemplates.service

Change-Id: I4388e2e45dfa39e177ccb92df714118f0008f982
This commit is contained in:
Kenji Ishii 2016-09-27 10:45:52 +09:00
parent 00f4a0def2
commit 0ef248a864
6 changed files with 230 additions and 42 deletions

View File

@ -54,12 +54,12 @@
run.$inject = [
'horizon.framework.conf.resource-type-registry.service',
'horizon.app.core.openstack-service-api.magnum',
'horizon.dashboard.container-infra.cluster-templates.service',
'horizon.dashboard.container-infra.cluster-templates.basePath',
'horizon.dashboard.container-infra.cluster-templates.resourceType'
];
function run(registry, magnum, basePath, resourceType) {
function run(registry, clusterTemplatesService, basePath, resourceType) {
registry.getResourceType(resourceType)
.setNames(gettext('Cluster Template'), gettext('Cluster Templates'))
@ -78,14 +78,14 @@
.setProperty('network_driver', {
label: gettext('Network Driver')
})
.setListFunction(listFunction)
.setListFunction(clusterTemplatesService.getClusterTemplatesPromise)
.tableColumns
.append({
id: 'name',
priority: 1,
sortDefault: true,
filters: ['noName'],
urlFunction: urlFunction
urlFunction: clusterTemplatesService.urlFunction
})
.append({
id: 'id',
@ -122,23 +122,6 @@
{label: gettext('Mesos'), key: 'mesos'}
]
});
function listFunction(params) {
return magnum.getClusterTemplates(params).then(modifyResponse);
function modifyResponse(response) {
return {data: {items: response.data.items.map(addTrackBy)}};
function addTrackBy(clusterTemplate) {
clusterTemplate.trackBy = clusterTemplate.id;
return clusterTemplate;
}
}
}
function urlFunction(item) {
return 'project/ngdetails/OS::Magnum::ClusterTemplate/' + item.id;
}
}
config.$inject = [

View File

@ -0,0 +1,60 @@
/**
* Licensed 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.
*/
(function() {
"use strict";
angular.module('horizon.dashboard.container-infra.cluster-templates')
.factory('horizon.dashboard.container-infra.cluster-templates.service',
clusterTemplatesService);
clusterTemplatesService.$inject = [
'$filter',
'horizon.app.core.openstack-service-api.magnum'
];
/*
* @ngdoc factory
* @name horizon.dashboard.container-infra.cluster-templates.service
*
* @description
* This service provides functions that are used through the Cluster Templates
* features. These are primarily used in the module registrations
* but do not need to be restricted to such use. Each exposed function
* is documented below.
*/
function clusterTemplatesService($filter, magnum) {
return {
getClusterTemplatesPromise: getClusterTemplatesPromise,
urlFunction: urlFunction
};
function getClusterTemplatesPromise(params) {
return magnum.getClusterTemplates(params).then(modifyResponse);
function modifyResponse(response) {
return {data: {items: response.data.items.map(addTrackBy)}};
function addTrackBy(clusterTemplate) {
clusterTemplate.trackBy = clusterTemplate.id;
return clusterTemplate;
}
}
}
function urlFunction(item) {
return 'project/ngdetails/OS::Magnum::ClusterTemplate/' + item.id;
}
}
})();

View File

@ -0,0 +1,51 @@
/*
*
* Licensed 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.
*/
(function() {
"use strict";
describe('cluster templates service', function() {
var service;
beforeEach(module('horizon.dashboard.container-infra.cluster-templates'));
beforeEach(inject(function($injector) {
service = $injector.get('horizon.dashboard.container-infra.cluster-templates.service');
}));
describe('getClusterTemplatesPromise', function() {
it("provides a promise", inject(function($q, $injector, $timeout) {
var magnum = $injector.get('horizon.app.core.openstack-service-api.magnum');
var deferred = $q.defer();
spyOn(magnum, 'getClusterTemplates').and.returnValue(deferred.promise);
var result = service.getClusterTemplatesPromise({});
deferred.resolve({
data:{
items: [{id: 123, name: 'template1'}]
}
});
$timeout.flush();
expect(magnum.getClusterTemplates).toHaveBeenCalled();
expect(result.$$state.value.data.items[0].name).toBe('template1');
}));
});
describe('urlFunction', function() {
it("get url", inject(function() {
var result = service.urlFunction({id:"123abc"});
expect(result).toBe("project/ngdetails/OS::Magnum::ClusterTemplate/123abc");
}));
});
});
})();

View File

@ -52,12 +52,12 @@
run.$inject = [
'horizon.framework.conf.resource-type-registry.service',
'horizon.app.core.openstack-service-api.magnum',
'horizon.dashboard.container-infra.clusters.service',
'horizon.dashboard.container-infra.clusters.basePath',
'horizon.dashboard.container-infra.clusters.resourceType'
];
function run(registry, magnum, basePath, resourceType) {
function run(registry, clustersService, basePath, resourceType) {
registry.getResourceType(resourceType)
.setNames(gettext('Cluster'), gettext('Clusters'))
@ -79,14 +79,14 @@
.setProperty('node_count', {
label: gettext('Node Count')
})
.setListFunction(listFunction)
.setListFunction(clustersService.getClustersPromise)
.tableColumns
.append({
id: 'name',
priority: 1,
sortDefault: true,
filters: ['noName'],
urlFunction: urlFunction
urlFunction: clustersService.urlFunction
})
.append({
id: 'id',
@ -149,23 +149,6 @@
'name': 'node_count',
'singleton': true
});
function listFunction(params) {
return magnum.getClusters(params).then(modifyResponse);
function modifyResponse(response) {
return {data: {items: response.data.items.map(addTrackBy)}};
function addTrackBy(cluster) {
cluster.trackBy = cluster.id;
return cluster;
}
}
}
function urlFunction(item) {
return 'project/ngdetails/OS::Magnum::Cluster/' + item.id;
}
}
config.$inject = [

View File

@ -0,0 +1,60 @@
/**
* Licensed 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.
*/
(function() {
"use strict";
angular.module('horizon.dashboard.container-infra.clusters')
.factory('horizon.dashboard.container-infra.clusters.service',
clustersService);
clustersService.$inject = [
'$filter',
'horizon.app.core.openstack-service-api.magnum'
];
/*
* @ngdoc factory
* @name horizon.dashboard.container-infra.clusters.service
*
* @description
* This service provides functions that are used through the Clusters
* features. These are primarily used in the module registrations
* but do not need to be restricted to such use. Each exposed function
* is documented below.
*/
function clustersService($filter, magnum) {
return {
getClustersPromise: getClustersPromise,
urlFunction: urlFunction
};
function getClustersPromise(params) {
return magnum.getClusters(params).then(modifyResponse);
function modifyResponse(response) {
return {data: {items: response.data.items.map(addTrackBy)}};
function addTrackBy(cluster) {
cluster.trackBy = cluster.id;
return cluster;
}
}
}
function urlFunction(item) {
return 'project/ngdetails/OS::Magnum::Cluster/' + item.id;
}
}
})();

View File

@ -0,0 +1,51 @@
/*
*
* Licensed 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.
*/
(function() {
"use strict";
describe('cluster templates service', function() {
var service;
beforeEach(module('horizon.dashboard.container-infra.cluster-templates'));
beforeEach(inject(function($injector) {
service = $injector.get('horizon.dashboard.container-infra.clusters.service');
}));
describe('getClustersPromise', function() {
it("provides a promise", inject(function($q, $injector, $timeout) {
var magnum = $injector.get('horizon.app.core.openstack-service-api.magnum');
var deferred = $q.defer();
spyOn(magnum, 'getClusters').and.returnValue(deferred.promise);
var result = service.getClustersPromise({});
deferred.resolve({
data:{
items: [{id: 123, name: 'cluster1'}]
}
});
$timeout.flush();
expect(magnum.getClusters).toHaveBeenCalled();
expect(result.$$state.value.data.items[0].name).toBe('cluster1');
}));
});
describe('urlFunction', function() {
it("get url", inject(function() {
var result = service.urlFunction({id:"123abc"});
expect(result).toBe("project/ngdetails/OS::Magnum::Cluster/123abc");
}));
});
});
})();