-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
248 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright 2017 Dell Inc. or its subsidiaries. All Rights Reserved. | ||
|
||
'use strict'; | ||
|
||
var di = require('di'); | ||
|
||
module.exports = ucsBaseJobFactory; | ||
di.annotate(ucsBaseJobFactory, new di.Provide('Job.Ucs.Base')); | ||
di.annotate(ucsBaseJobFactory, | ||
new di.Inject( | ||
'Job.Base', | ||
'Logger', | ||
'Util', | ||
'Promise', | ||
'JobUtils.UcsTool', | ||
'_', | ||
'Assert')); | ||
|
||
function ucsBaseJobFactory( | ||
BaseJob, | ||
Logger, | ||
util, | ||
Promise, | ||
UcsTool, | ||
_, | ||
assert | ||
) { | ||
function UcsBaseJob(logger, options, context, taskId) { | ||
UcsBaseJob.super_.call(this, logger, options, context, taskId); | ||
|
||
var _ucstoolInstance = null; | ||
this._getUcsToolInstance = function() { | ||
if (!_ucstoolInstance) { | ||
_ucstoolInstance = new UcsTool(); | ||
} | ||
return _ucstoolInstance; | ||
}; | ||
} | ||
util.inherits(UcsBaseJob, BaseJob); | ||
|
||
UcsBaseJob.prototype._run = function() {}; | ||
|
||
UcsBaseJob.prototype._ucsRequest = function(url, settings) { | ||
var self = this; | ||
var ucsTool = self._getUcsToolInstance(); | ||
ucsTool.settings = settings; | ||
return ucsTool.clientRequest(url); | ||
}; | ||
|
||
UcsBaseJob.prototype._ucsRequestAsync = function(url, settings, callbackId) { | ||
var self = this; | ||
assert.ok(callbackId); | ||
return Promise.try(function() { | ||
return self._ucsRequest(url, settings); | ||
}) | ||
.then(function(res) { | ||
if (res && res.body && res.body.toUpperCase() !== "ACCEPTED") { | ||
throw new Error( | ||
"Request was not ACCEPTED. Please check input parameters."); | ||
} | ||
return self._subscribeHttpResponseUuidByPromisify(callbackId); | ||
}); | ||
}; | ||
|
||
UcsBaseJob.prototype._subscribeHttpResponseUuidByPromisify = function(id) { | ||
var self = this; | ||
var nodeCallback = function(id, callback) { | ||
self._subscribeHttpResponseUuid(function(data) { | ||
return callback.call(self, null, data); | ||
}, id); | ||
}; | ||
|
||
var promisify = Promise.promisify(nodeCallback); | ||
return promisify(id); | ||
}; | ||
|
||
return UcsBaseJob; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2017 Dell Inc. or its subsidiaries. All Rights Reserved. | ||
|
||
'use strict'; | ||
|
||
var uuid = require('node-uuid'), | ||
sandbox = sinon.sandbox.create(), | ||
taskId = uuid.v4(), | ||
UcsTool, | ||
ucsJobBase, | ||
UcsTool = function() { | ||
return { | ||
clientRequest: sandbox.stub().resolves({ | ||
"body": "ACCEPTED" | ||
}) | ||
}; | ||
}; | ||
|
||
describe('Job.Ucs.Base', function() { | ||
var base = require('./base-spec'); | ||
|
||
base.before(function(context) { | ||
helper.setupInjector([ | ||
helper.require('/spec/mocks/logger.js'), | ||
helper.requireGlob('/lib/services/*.js'), | ||
helper.require('/lib/jobs/base-job.js'), | ||
helper.require('/lib/jobs/ucs-base-job.js'), | ||
helper.di.simpleWrapper(UcsTool, 'JobUtils.UcsTool') | ||
]); | ||
context.Jobclass = helper.injector.get('Job.Ucs.Base'); | ||
UcsTool = helper.injector.get('JobUtils.UcsTool'); | ||
}); | ||
|
||
describe('Base', function() { | ||
base.examples(); | ||
}); | ||
|
||
describe('ucs-job-base', function() { | ||
beforeEach(function() { | ||
|
||
var graphId = uuid.v4(); | ||
ucsJobBase = new this.Jobclass({}, {}, { | ||
graphId: graphId | ||
}, taskId); | ||
}); | ||
|
||
afterEach(function() { | ||
sandbox.restore(); | ||
}); | ||
|
||
|
||
|
||
it("should run ucs request", function() { | ||
|
||
return ucsJobBase._ucsRequest("http://localhost:12345", {}) | ||
.then(function() { | ||
expect(ucsJobBase._getUcsToolInstance().clientRequest).to.be.calledOnce; | ||
}); | ||
}); | ||
|
||
it("should run ucs request asynchronously", function() { | ||
|
||
ucsJobBase._subscribeHttpResponseUuidByPromisify = sandbox.stub().resolves("123"); | ||
sandbox.spy(ucsJobBase, '_ucsRequest'); | ||
|
||
return ucsJobBase._ucsRequestAsync("http://localhost:12345", {}, taskId) | ||
.then(function() { | ||
expect(ucsJobBase._ucsRequest).to.be.calledOnce; | ||
expect(ucsJobBase._ucsRequest).to.be.calledWith("http://localhost:12345", {}); | ||
expect(ucsJobBase._subscribeHttpResponseUuidByPromisify).to.be.calledOnce; | ||
expect(ucsJobBase._subscribeHttpResponseUuidByPromisify) | ||
.to.be.calledWith(taskId); | ||
}); | ||
}); | ||
|
||
it("should get bad request error to run ucs request asynchronously", function() { | ||
ucsJobBase._subscribeHttpResponseUuidByPromisify = sandbox.stub(); | ||
ucsJobBase._ucsRequest = sandbox.stub().resolves({ | ||
"body": "ERROR" | ||
}); | ||
return ucsJobBase._ucsRequestAsync("http://localhost:12345", {}, taskId) | ||
.then(function() { | ||
throw new Error("Test should fail"); | ||
}, function(err) { | ||
expect(ucsJobBase._ucsRequest).to.be.calledOnce; | ||
expect(ucsJobBase._ucsRequest).to.be.calledWith("http://localhost:12345", {}); | ||
expect(ucsJobBase._subscribeHttpResponseUuidByPromisify).not.to.be.calledOnce; | ||
expect(err.message).to.deep.equal( | ||
"Request was not ACCEPTED. Please check input parameters."); | ||
}); | ||
}); | ||
|
||
it("should promisify subscribeHttpResponseUuid", function() { | ||
var mock = function(mockSpy) { | ||
mockSpy("abc"); | ||
}; | ||
ucsJobBase._subscribeHttpResponseUuid = mock; | ||
return ucsJobBase._subscribeHttpResponseUuidByPromisify(taskId) | ||
.then(function(data) { | ||
expect(data).to.equal("abc"); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.