Skip to content

Commit

Permalink
Added get label/displayName methods
Browse files Browse the repository at this point in the history
  • Loading branch information
reicruz committed Aug 25, 2016
1 parent 1a95753 commit 9c71255
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 @@ -223,6 +223,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 @@ -239,3 +263,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 @@ -77,7 +113,7 @@ describe('Test CF Convenience', function() {
id: '12345'
}
}
}
};
let robot = {
brain: {
get: function(id) {
Expand All @@ -100,10 +136,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 @@ -120,7 +156,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 9c71255

Please sign in to comment.