Skip to content

Commit

Permalink
integrate Config into Service for private serviceConfig that is creat…
Browse files Browse the repository at this point in the history
…ed upon service configuration

see issue #9
  • Loading branch information
alrik committed Oct 13, 2015
1 parent 9265795 commit bcfcb7f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
28 changes: 26 additions & 2 deletions Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"use strict";

const EventEmitter = require('events').EventEmitter;

const Config = require('./Config');

/**
* Abstract class that defines the srvoa service interface.
Expand All @@ -20,6 +20,12 @@ class Service extends EventEmitter {
* @property serviceManager {ServiceManager}
*/

/**
* The config passed to configure as config service..
*
* @property serviceConfig {Config}
*/

/**
* Create a new service instance.
*
Expand All @@ -30,6 +36,7 @@ class Service extends EventEmitter {
super();

this.serviceManager = serviceManager || null;
this.serviceConfig = null;
}

/**
Expand Down Expand Up @@ -60,7 +67,7 @@ class Service extends EventEmitter {
* @return {Service}
*/
configure(config) {
return this;
return this._applyConfig(config);
}

/**
Expand All @@ -80,6 +87,23 @@ class Service extends EventEmitter {
launch() {
return this;
}

/**
* Take a config hash and turn it into a config instance that is stored as `serviceConfig` property.
*
* @param config
* @returns {Service}
* @private
*/
_applyConfig(config) {
this.serviceConfig = new Config;

if (config) {
this.serviceConfig.setConfig(config);
}

return this;
}
}

module.exports = Service;
27 changes: 27 additions & 0 deletions test/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const
assert = require('assert'),
Config = require('../Config'),
Service = require('../Service'),
ServiceManager = require('../ServiceManager'),
EventEmitter = require('events').EventEmitter;
Expand Down Expand Up @@ -69,4 +70,30 @@ describe('srvoa::service', function() {

assert(srv.getServiceManager() === serviceManager);
});

it('creates internal serviceConfig during configuration.', function() {
var srv = new Service();

assert(srv.serviceConfig === null, 'Expected `serviceConfig` to be null before configure is called.');

srv.configure();

assert(srv.serviceConfig instanceof Config, 'Expected `serviceConfig` to be instance of `Config` after configure is called.');
});

it('should pass the right config hash to serviceConfig.', function() {
var srv = new Service();

srv.configure({
test: {
test2: 1
}
});

assert(srv.serviceConfig.get('test.test2') === 1, 'Expected config key `test.test2` to equal 1.');

srv.configure({});

assert(srv.serviceConfig.get('test.test2') === undefined, 'Expected config key `test.test2` to equal undefined after resetting configuration.');
});
});

0 comments on commit bcfcb7f

Please sign in to comment.