API Docs for: 0.9.3
Show:

File: lib/abstractClient.js

  1. // Copyright 2014, Yahoo! Inc.
  2. // Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.
  3.  
  4. var Base = require('preceptor-core').Base;
  5. var utils = require('preceptor-core').utils;
  6. var ReportManager = require('preceptor-reporter');
  7. var _ = require('underscore');
  8. var Promise = require('promise');
  9.  
  10. var defaultClient = require('./defaults/defaultClient');
  11.  
  12. /**
  13. * @class AbstractClient
  14. * @extends Base
  15. *
  16. * @property {object} _options
  17. * @property {AbstractClientDecorator[]} decorators
  18. * @property {ReportManager} _reportManager
  19. * @property {EventReporter} _eventReporter
  20. */
  21. var AbstractClient = Base.extend(
  22.  
  23. /**
  24. * Abstract client constructor
  25. *
  26. * @param {object[]} decorators
  27. * @param {object} decoratorPlugins
  28. * @param {object} options
  29. * @constructor
  30. */
  31. function (decorators, decoratorPlugins, options) {
  32. this.__super();
  33.  
  34. this._reportManager = new ReportManager({ collect: false });
  35. this._eventReporter = this._reportManager.addReporter('Event');
  36. this._eventReporter.on('message', function (areaType, messageType, data) {
  37. // Make sure not to forward admin messages; this should stay in the client
  38. if (["admin"].indexOf(areaType) === -1) {
  39. this.emit('reportMessage', messageType, data);
  40. }
  41. }.bind(this));
  42.  
  43. this._options = utils.deepExtend({}, [defaultClient, options || {}]);
  44. this.decorators = this._setupDecorators(this._eventReporter, decorators, decoratorPlugins);
  45.  
  46. this.initialize();
  47. },
  48.  
  49. {
  50. /**
  51. * Setup decorator plugins
  52. *
  53. * @method _setupDecorators
  54. * @param {EventReporter} eventReporter
  55. * @param {object[]} decorators
  56. * @param {object} decoratorPlugins
  57. * @return {AbstractClientDecorator[]}
  58. * @private
  59. */
  60. _setupDecorators: function (eventReporter, decorators, decoratorPlugins) {
  61.  
  62. var decoratorList = [];
  63.  
  64. _.each(decorators, function (currentDecorator) {
  65.  
  66. var decoratorPlugin = decoratorPlugins[currentDecorator.type.toLowerCase()],
  67. DecoratorClass,
  68. decoratorInstance;
  69.  
  70. if (!decoratorPlugin) {
  71. throw new Error('Unknown decorator: ' + currentDecorator.type);
  72. }
  73.  
  74. DecoratorClass = require(decoratorPlugin);
  75. decoratorInstance = new DecoratorClass(eventReporter, currentDecorator);
  76. decoratorList.push(decoratorInstance);
  77.  
  78. }, this);
  79.  
  80. return decoratorList;
  81. },
  82.  
  83.  
  84. /**
  85. * Processes the begin of the testing environment
  86. *
  87. * @method processBefore
  88. * @return {Promise}
  89. */
  90. processBefore: function () {
  91.  
  92. var promise = Promise.resolve();
  93.  
  94. _.each(this.decorators, function (decorator) {
  95. promise = promise.then(function () {
  96. return decorator.processBefore();
  97. })
  98. }, this);
  99.  
  100. return promise;
  101. },
  102.  
  103. /**
  104. * Processes the end of the testing environment
  105. *
  106. * @method processAfter
  107. * @return {Promise}
  108. */
  109. processAfter: function () {
  110.  
  111. var promise = Promise.resolve();
  112.  
  113. _.each(this.decorators, function (decorator) {
  114. promise = promise.then(function () {
  115. return decorator.processAfter();
  116. })
  117. }, this);
  118.  
  119. return promise;
  120. },
  121.  
  122. /**
  123. * Processes the beginning of a test
  124. *
  125. * @method processBeforeTest
  126. * @return {Promise}
  127. */
  128. processBeforeTest: function () {
  129.  
  130. var promise = Promise.resolve();
  131.  
  132. _.each(this.decorators, function (decorator) {
  133. promise = promise.then(function () {
  134. return decorator.processBeforeTest();
  135. })
  136. }, this);
  137.  
  138. return promise;
  139. },
  140.  
  141. /**
  142. * Processes the ending of a test
  143. *
  144. * @method processAfterTest
  145. * @return {Promise}
  146. */
  147. processAfterTest: function () {
  148.  
  149. var promise = Promise.resolve();
  150.  
  151. _.each(this.decorators, function (decorator) {
  152. promise = promise.then(function () {
  153. return decorator.processAfterTest();
  154. })
  155. }, this);
  156.  
  157. return promise;
  158. },
  159.  
  160.  
  161. /**
  162. * Gets the options
  163. *
  164. * @method getOptions
  165. * @return {object}
  166. */
  167. getOptions: function () {
  168. return this._options;
  169. },
  170.  
  171. /**
  172. * Gets the decorator
  173. *
  174. * @method getDecorator
  175. * @return {AbstractClientDecorator[]}
  176. */
  177. getDecorator: function () {
  178. return this.decorators;
  179. },
  180.  
  181.  
  182. /**
  183. * Gets the report-manager
  184. *
  185. * @method getReportManager
  186. * @return {ReportManager}
  187. */
  188. getReportManager: function () {
  189. return this._reportManager;
  190. },
  191.  
  192. /**
  193. * Gets the event-reporter
  194. *
  195. * @method getEventReporter
  196. * @return {EventReporter}
  197. */
  198. getEventReporter: function () {
  199. return this._eventReporter;
  200. },
  201.  
  202.  
  203. /**
  204. * Will be called when the client begins
  205. *
  206. * @method run
  207. * @return {Promise}
  208. */
  209. run: function () {
  210. throw new Error('Unimplemented method "run" in the client.');
  211. }
  212. },
  213.  
  214. {
  215. /**
  216. * @property TYPE
  217. * @type {string}
  218. * @static
  219. */
  220. TYPE: 'AbstractClient'
  221. });
  222.  
  223. module.exports = AbstractClient;
  224.