Skip to content

Commit

Permalink
Merge pull request #9 from /issues/8
Browse files Browse the repository at this point in the history
Added get label/displayName methods, closes #8
  • Loading branch information
Craig Lanzen authored Aug 25, 2016
2 parents 374bc58 + 9c71255 commit c95ec77
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
26 changes: 26 additions & 0 deletions cf.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,30 @@ function getServiceGuid(serviceName) {
}
}

/**
* Get the display_name of a service, given its label. Return label if not found.
*/
function getServiceDisplayName(serviceLabel) {
for (let i = 0; i < this.serviceCache.length; i++) {
if (this.serviceCache[i].label === serviceLabel) {
return this.serviceCache[i].display_name;
}
}
return serviceLabel;
}

/**
* Get the label of a service, given its display_name. Return display_name if not found.
*/
function getServiceLabel(serviceName) {
for (let i = 0; i < this.serviceCache.length; i++) {
if (this.serviceCache[i].display_name === serviceName) {
return this.serviceCache[i].label;
}
}
return serviceName;
}

/**
* Keep the hubot connection to bluemix alive.
* @param {UsersUAA} uaa Cloud Foundry UAA manager
Expand All @@ -247,3 +271,5 @@ function stayAlive(uaa) {

module.exports.promise = promise;
module.exports.getServiceGuid = getServiceGuid;
module.exports.getServiceDisplayName = getServiceDisplayName;
module.exports.getServiceLabel = getServiceLabel;
44 changes: 40 additions & 4 deletions test/cf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,42 @@ describe('Test CF Convenience', function() {
return;
});

context('Get service label', function() {
it('should return valid label if display name exists', function(done) {
let testDone = done;
cf.promise.then(() => {
expect(cf.getServiceLabel('A ValidService2 Display Name')).to.eql('validService2');
testDone();
});
});

it('should return same display name if it does not exist', function(done) {
let testDone = done;
cf.promise.then(() => {
expect(cf.getServiceLabel('invalidDisplayName')).to.eql('invalidDisplayName');
testDone();
});
});
});

context('Get service display name', function() {
it('should return valid display name if label exists', function(done) {
let testDone = done;
cf.promise.then(() => {
expect(cf.getServiceDisplayName('validService2')).to.eql('A ValidService2 Display Name');
testDone();
});
});

it('should return same label if it does not exist', function(done) {
let testDone = done;
cf.promise.then(() => {
expect(cf.getServiceDisplayName('invalidLabel')).to.eql('invalidLabel');
testDone();
});
});
});

context('initialize cf with promise', function() {
it('should complete and have initialized objects', function(done) {
let testDone = done;
Expand Down Expand Up @@ -79,7 +115,7 @@ describe('Test CF Convenience', function() {
id: '12345'
}
}
}
};
let robot = {
brain: {
get: function(id) {
Expand All @@ -102,10 +138,10 @@ describe('Test CF Convenience', function() {
cf.promise.then((result) => {
let service1, service2;
result.serviceCache.forEach((service) => {
if(service.guid === 'validService1Guid') {
if (service.guid === 'validService1Guid') {
service1 = service;
}
else if(service.guid === 'validService2Guid') {
else if (service.guid === 'validService2Guid') {
service2 = service;
}
});
Expand All @@ -122,7 +158,7 @@ describe('Test CF Convenience', function() {
it('should execute getApp', function(done) {
let testDone = done;
cf.promise.then(() => {
let result = cfRewire.__get__('getApp')('testApp2Name','space');
let result = cfRewire.__get__('getApp')('testApp2Name', 'space');
expect(result).to.be.a('promise');
testDone();
});
Expand Down

0 comments on commit c95ec77

Please sign in to comment.