Skip to content

Commit

Permalink
WIP - Refactoring dependency injection in Javascript code
Browse files Browse the repository at this point in the history
  • Loading branch information
jerome-netguardians committed May 1, 2020
1 parent facbe65 commit 6eaff12
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 30 deletions.
7 changes: 3 additions & 4 deletions src/main/webapp/html/eskimoMarathonServicesConfig.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,17 @@ <h3>Marathon Services configuration</h3>
</button>
</div>
<div class="btn-group">
<button id="reinstall-marathon-servicesbtn" name="reinstall-marathon-servicesbtn" class="btn btn-primary"
onclick="javascript:eskimoMain.getMarathonServicesConfig().showReinstallSelection(); event.preventDefault(); return false;">
<button id="reinstall-marathon-servicesbtn" name="reinstall-marathon-servicesbtn" class="btn btn-primary">
Force Reinstall
</button>
</div>
<div class="btn-group">
<button id="select-all-marathon-servicesconfig" name="select-all-marathon-servicesconfig" onclick="javascript:eskimoMain.getMarathonServicesConfig().selectAll(); event.preventDefault(); return false;" class="btn btn-default">
<button id="select-all-marathon-servicesconfig" name="select-all-marathon-servicesconfig" class="btn btn-default">
Select All
</button>
</div>
<div class="btn-group">
<button id="reset-marathon-servicesconfig" name="reset-marathon-servicesconfig" onclick="javascript:eskimoMain.getMarathonServicesConfig().showMarathonServicesConfig(); event.preventDefault(); return false;" class="btn btn-default">
<button id="reset-marathon-servicesconfig" name="reset-marathon-servicesconfig" class="btn btn-default">
Reset
</button>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/scripts/eskimoMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ eskimo.Main = function() {
eskimoServicesConfig = new eskimo.ServicesConfig();
// loadServicesConfig -> load-services-config

eskimoMarathonServicesConfig = new eskimo.MarathonServicesConfig();
eskimoMarathonServicesConfig = new eskimo.MarathonServicesConfig({eskimoMain: this});
// loadMarathonServices -> get-marathon-services

eskimoOperationsCommand = new eskimo.OperationsCommand();
Expand Down
67 changes: 47 additions & 20 deletions src/main/webapp/scripts/eskimoMarathonServicesConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ Software.
if (typeof eskimo === "undefined" || eskimo == null) {
window.eskimo = {}
}
eskimo.MarathonServicesConfig = function() {
eskimo.MarathonServicesConfig = function(constructorObject) {

// will be injected eventually from constructorObject
this.eskimoMain = null;

var that = this;

Expand All @@ -56,19 +59,37 @@ eskimo.MarathonServicesConfig = function() {
console.log(setupConfig);

try {
checkMarathonSetup(setupConfig, eskimoMain.getNodesConfig().getServicesDependencies(),
checkMarathonSetup(setupConfig, that.eskimoMain.getNodesConfig().getServicesDependencies(),
function () {
// callback if setup is OK
proceedWithMarathonInstallation(setupConfig);
});
} catch (error) {
alert ("error");
alert ("error : " + error);
}

e.preventDefault();
return false;
});

$("#reinstall-marathon-servicesbtn").click(function (e) {
showReinstallSelection();
e.preventDefault();
return false;
});

$("#select-all-marathon-servicesconfig").click(function (e) {
selectAll();
e.preventDefault();
return false;
});

$("#reset-marathon-servicesconfig").click(function (e) {
showMarathonServicesConfig();
e.preventDefault();
return false;
});

loadMarathonServices();


Expand Down Expand Up @@ -112,7 +133,7 @@ eskimo.MarathonServicesConfig = function() {
return MARATHON_SERVICES;
};

this.selectAll = function() {
function selectAll(){

var allSelected = true;

Expand All @@ -127,7 +148,8 @@ eskimo.MarathonServicesConfig = function() {
for (var i = 0; i < MARATHON_SERVICES.length; i++) {
$('#' + MARATHON_SERVICES[i] + "_install").get(0).checked = !allSelected;
}
};
}
this.selectAll = selectAll;

this.renderMarathonConfig = function (marathonConfig) {

Expand All @@ -139,7 +161,7 @@ eskimo.MarathonServicesConfig = function() {

marathonServiceRow += ''+
'<td>' +
'<img class="nodes-config-logo" src="' + eskimoMain.getNodesConfig().getServiceLogoPath(MARATHON_SERVICES[i]) + '" />' +
'<img class="nodes-config-logo" src="' + that.eskimoMain.getNodesConfig().getServiceLogoPath(MARATHON_SERVICES[i]) + '" />' +
'</td>'+
'<td>'+
MARATHON_SERVICES[i]+
Expand Down Expand Up @@ -174,9 +196,9 @@ eskimo.MarathonServicesConfig = function() {

};

this.showReinstallSelection = function() {
function showReinstallSelection() {

eskimoMain.getMarathonServicesSelection().showMarathonServiceSelection();
that.eskimoMain.getMarathonServicesSelection().showMarathonServiceSelection();

var marathonServicesSelectionHTML = $('#marathon-services-container-table').html();
marathonServicesSelectionHTML = marathonServicesSelectionHTML.replace(/marathon\-services/g, "marathon-services-selection");
Expand All @@ -186,18 +208,18 @@ eskimo.MarathonServicesConfig = function() {
'<form id="marathon-servicesreinstall">' +
marathonServicesSelectionHTML +
'</form>');

};
}
this.showReinstallSelection = showReinstallSelection;

function showMarathonServicesConfig () {

if (!eskimoMain.isSetupDone()) {
eskimoMain.showSetupNotDone("Cannot configure marathon services as long as initial setup is not completed");
if (!that.eskimoMain.isSetupDone()) {
that.eskimoMain.showSetupNotDone("Cannot configure marathon services as long as initial setup is not completed");
return;
}

if (eskimoMain.isOperationInProgress()) {
eskimoMain.showProgressbar();
if (that.eskimoMain.isOperationInProgress()) {
that.eskimoMain.showProgressbar();
}

$.ajax({
Expand All @@ -222,7 +244,7 @@ eskimo.MarathonServicesConfig = function() {

} else if (data.clear == "setup"){

eskimoMain.handleSetupNotCompleted();
that.eskimoMain.handleSetupNotCompleted();

}

Expand All @@ -231,7 +253,7 @@ eskimo.MarathonServicesConfig = function() {
error: errorHandler
});

eskimoMain.showOnlyContent("marathon-services-config");
that.eskimoMain.showOnlyContent("marathon-services-config");
}
this.showMarathonServicesConfig = showMarathonServicesConfig;

Expand All @@ -253,7 +275,7 @@ eskimo.MarathonServicesConfig = function() {

function proceedWithMarathonInstallation(model, reinstall) {

eskimoMain.showProgressbar();
that.eskimoMain.showProgressbar();

// 1 hour timeout
$.ajax({
Expand All @@ -265,7 +287,7 @@ eskimo.MarathonServicesConfig = function() {
data: JSON.stringify(model),
success: function (data, status, jqXHR) {

eskimoMain.hideProgressbar();
that.eskimoMain.hideProgressbar();

// OK
console.log(data);
Expand All @@ -278,18 +300,23 @@ eskimo.MarathonServicesConfig = function() {
if (!data.command) {
alert ("Expected pending operations command but got none !");
} else {
eskimoMain.getMarathonOperationsCommand().showCommand (data.command);
that.eskimoMain.getMarathonOperationsCommand().showCommand (data.command);
}
}
},

error: function (jqXHR, status) {
eskimoMain.hideProgressbar();
that.eskimoMain.hideProgressbar();
errorHandler (jqXHR, status);
}
});
}

// inject constructor object in the end
if (constructorObject != null) {
$.extend(this, constructorObject);
}

// call constructor
this.initialize();
};
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ public void init() throws Exception {
page.executeJavaScript("var nodesConfig = {};");
page.executeJavaScript("nodesConfig.getServiceLogoPath = function (serviceName){ return serviceName + '-logo.png'; };");
page.executeJavaScript("nodesConfig.getServiceIconPath = function (serviceName){ return serviceName + '-icon.png'; };");
page.executeJavaScript("nodesConfig.getServicesDependencies = function () { return {}; };");
page.executeJavaScript("nodesConfig.isServiceUnique = function (serviceName){ " +
"return (serviceName == 'mesos-master' " +
" || serviceName == 'zookeeper' " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@

import ch.niceideas.common.utils.ResourceUtils;
import ch.niceideas.common.utils.StreamUtils;
import ch.niceideas.eskimo.model.NodesConfigWrapper;
import ch.niceideas.eskimo.services.StandardSetupHelpers;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
import static junit.framework.TestCase.fail;
import static org.junit.Assert.assertNotNull;

public class EskimoMarathonServicesConfigTest extends AbstractWebTest {

Expand All @@ -69,7 +68,7 @@ public void setUp() throws Exception {
*/

// instantiate test object
page.executeJavaScript("eskimoMarathonServicesConfig = new eskimo.MarathonServicesConfig();");
page.executeJavaScript("eskimoMarathonServicesConfig = new eskimo.MarathonServicesConfig({eskimoMain: eskimoMain});");

waitForElementIdInDOM("reset-marathon-servicesconfig");

Expand All @@ -82,11 +81,34 @@ public void setUp() throws Exception {
" \"spark-history-server\",\n" +
" \"zeppelin\"\n" +
" ]);");

page.executeJavaScript("$('#inner-content-marathon-services-config').css('display', 'inherit')");
page.executeJavaScript("$('#inner-content-marathon-services-config').css('visibility', 'visible')");
}

@Test
public void testSaveMarathonServicesButtonClick() throws Exception {
fail ("To Be Implemented");

testRenderMarathonConfig();

page.executeJavaScript("function checkMarathonSetup (marathonConfig) {" +
" console.log (marathonConfig);" +
" window.savedMarathonConfig = JSON.stringify (marathonConfig);" +
"};");

page.getElementById("save-marathon-servicesbtn").click();

JSONObject expectedConfig = new JSONObject("" +
"{\"cerebro_install\":\"on\"," +
"\"gdash_install\":\"on\"," +
"\"grafana_install\":\"on\"," +
"\"kafka-manager_install\":\"on\"," +
"\"kibana_install\":\"on\"," +
"\"spark-history-server_install\":\"on\"," +
"\"zeppelin_install\":\"on\"}");

JSONObject actualConfig = new JSONObject((String)page.executeJavaScript("window.savedMarathonConfig").getJavaScriptResult());
assertTrue(expectedConfig.similar(actualConfig));
}

@Test
Expand Down

0 comments on commit 6eaff12

Please sign in to comment.