Files
dial-reference/server/tests/js_tests/tests/stopApplicationInRunningState.js
Shruti Ranganathan Jothi fd58106e7f ability to run tests standalone
2017-02-28 17:29:57 -08:00

75 lines
2.8 KiB
JavaScript

"use strict";
var dial = require("../libs/dialClient.js"),
utils = require("../libs/utils.js"),
Q = require("q");
function test() {
var host = utils.getParam("host");
var app = utils.getParam("app");
var timeToWaitForStateChange = utils.getParam("timeToWaitForStateChange") || 5000;
return new Q()
.then(function () {
console.log("TEST " + __filename + ": Stop " + app + " application when it is running and check for response code 200 from DIAL server ");
})
.then(dial.getApplicationStatus.bind(null, host, app))
.then(function getCurrentAppState(result) {
if(!result || !result.state) {
return Q.reject(new Error("Error retrieving current " + app + " application state"));
}
return result.state;
})
.then(function startAppIfNotRunning(state) {
if(state !== "running") {
return dial.launchApplication(host, app)
.then(function (response) {
if(response.statusCode !== 201) {
return Q.reject("Error launching " + app + " application. Expected status code 201 but got " + response.statusCode);
}
});
}
})
.delay(timeToWaitForStateChange)
.then(dial.getApplicationStatus.bind(null, host, app))
.then(function getCurrentAppState(result) {
if(!result || !result.state) {
return Q.reject(new Error("Error retrieving current " + app + " application state"));
}
if(result.state !== "running") {
return Q.reject(new Error("Expected " + app + " state to be running but querying application state returned " + result.state));
}
})
.then(dial.stopApplication.bind(null, host, app))
.then(function (response) {
if(response.statusCode !== 200) {
return Q.reject("Error stopping " + app + " application. Expected status code 200 but got " + response.statusCode);
}
})
.delay(timeToWaitForStateChange)
.then(dial.getApplicationStatus.bind(null, host, app))
.then(function getCurrentAppState(result) {
if(!result || !result.state) {
return Q.reject(new Error("Error retrieving current " + app + " application state"));
}
if(result.state !== "stopped") {
return Q.reject(new Error("Expected " + app + " state to be stopped but querying application state returned " + result.state));
}
})
.then(function () {
console.log("TEST PASSED");
})
.fail(function handleError(err) {
console.error("TEST FAILED " + err);
});
}
module.exports.test = test;
if (require.main === module) {
test()
.done();
}