Files
dial-reference/server/tests/js_tests/tests/hideApplicationInRunningState.js
2017-03-08 09:58:27 -08:00

76 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 () {
utils.printTestInfo(__filename.slice(__dirname.length + 1), "Hide " + app + " application when it is running and expect response code 200");
})
.then(dial.getApplicationStatus.bind(null, host, app))
.then(function getCurrentAppState(result) {
if(!result || !result.state) {
return Q.reject(new Error("Could not retrieve 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(new Error("Could not launch " + 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("Could not retrieve current " + app + " application state"));
}
if(result.state !== "running") {
return Q.reject(new Error("Expected " + app + " state to be running but state was " + result.state));
}
})
.then(dial.hideApplication.bind(null, host, app))
.then(function (response) {
if(response.statusCode !== 200) {
return Q.reject(new Error("Error hiding " + 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("Could not retrieve current " + app + " application state"));
}
if(result.state !== "hidden") {
return Q.reject(new Error("Expected " + app + " state to be hidden but state was " + result.state));
}
})
.then(function () {
utils.printSuccess()
})
.fail(function handleError(err) {
utils.printFailure(err);
});
}
module.exports.test = test;
if (require.main === module) {
test()
.done();
}