-
Piotr Gawron authored
clicking on map access appropriate elements vi api
Piotr Gawron authoredclicking on map access appropriate elements vi api
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
ServerConnector-mock.js 5.41 KiB
'use strict';
var logger = require('./logger');
var OriginalServerConnector = require('../../main/js/ServerConnector');
var fs = require('fs');
var request = require('request');
var ServerConnectorMock = OriginalServerConnector;
ServerConnectorMock.init = function() {
this._customMap = null;
this._sessionData = undefined;
ServerConnectorMock.listeners = [];
ServerConnectorMock.selectedPolygon = null;
ServerConnectorMock.exportModelId = null;
// add listener types
ServerConnectorMock.listeners.push("onActualizeSessionData");
ServerConnectorMock.listeners.push("onActualizeParams");
ServerConnectorMock.listeners.push("onSendClearRequest");
ServerConnectorMock.listeners.push("onSendOverlayDetailDataRequest");
ServerConnectorMock.listeners.push("onRequestUpdateCommentList");
ServerConnectorMock.listeners.push("onSearchByCoord");
};
ServerConnectorMock.init();
ServerConnectorMock.addListener = function(type, fun) {
if (ServerConnectorMock.listeners.indexOf(type) < 0) {
throw new Error("Unknown listener type: " + type);
}
if (typeof fun !== "function") {
throw new Error("Second parameter must be a function but \"" + typeof (fun) + "\" found.");
}
var listenerList = this.listeners[type];
if (listenerList === null || listenerList === undefined) {
listenerList = [];
this.listeners[type] = listenerList;
}
listenerList.push(fun);
};
ServerConnectorMock.removeListener = function(type, fun) {
if (ServerConnectorMock.listeners.indexOf(type) < 0) {
throw new Error("Unknown listener type: " + type);
}
if (typeof fun !== "function") {
throw new Error("Second parameter must be a function but \"" + typeof (fun) + "\" found.");
}
var listenerList = this.listeners[type];
if (listenerList !== null && listenerList !== undefined) {
var index = listenerList.indexOf(fun);
if (index > -1) {
listenerList.splice(index, 1);
}
}
};
ServerConnectorMock.callListeners = function(type, param) {
if (ServerConnectorMock.listeners.indexOf(type) < 0) {
throw new Error("Unknown listener type: " + type);
}
var listenerList = this.listeners[type];
if (listenerList !== null && listenerList !== undefined) {
logger.debug("ServerConnector is calling: " + type + ". " + listenerList.length + " listeners found.");
for ( var i in listenerList) {
var e = {};
e.type = type;
e.object = this;
e.param = param;
listenerList[i](e);
}
} else {
logger.debug("ServerConnector is calling: " + type + ". No listeners found.");
}
};
ServerConnectorMock.setSelectedPolygon = function(value) {
this.selectedPolygon = value;
};
ServerConnectorMock.getSelectedPolygon = function() {
return this.selectedPolygon;
};
ServerConnectorMock.setExportModelId = function(value) {
this.exportModelId = value;
};
ServerConnectorMock.getExportModelId = function() {
return this.exportModelId;
};
ServerConnectorMock.sendClearRequest = function(overlayName) {
ServerConnectorMock.callListeners("onSendClearRequest", overlayName);
};
ServerConnectorMock.sendOverlayDetailDataRequest = function(overlayName, identifiedElement) {
ServerConnectorMock.callListeners("onSendOverlayDetailDataRequest", overlayName, identifiedElement);
};
ServerConnectorMock.addOverlayCollection = function(overlay) {
logger.debug("Adding overlay: " + overlay);
};
ServerConnectorMock.sendRefreshRequest = function(overlayName) {
logger.debug("Send refresh request: " + overlayName);
};
ServerConnectorMock.getMaxOverlayColorInt = function() {
return 0xFF0000;
};
ServerConnectorMock.getMinOverlayColorInt = function() {
return 0x00FF00;
};
ServerConnectorMock.readFile = function(url) {
return new Promise(function(resolve, reject) {
if (url.indexOf("http") === 0) {
request.get(url, function(error, response, body) {
if (error) {
reject(error);
} else if (response.statusCode !== 200) {
reject(response);
} else {
resolve(body);
}
});
} else {
fs.readFile(url, 'utf8', function(err, content) {
if (err) {
reject(err);
} else {
resolve(content);
}
});
}
});
};
ServerConnectorMock.sendPostRequest = function(url, params) {
var self = this;
return new Promise(function(resolve, reject) {
if (url.indexOf("http") === 0) {
request.post({url:url, form:params}, function(error, response, body) {
if (error) {
reject(error);
} else if (response.statusCode !== 200) {
reject(response);
} else {
resolve(body);
}
});
} else {
var mockUrl = url+"/"+self.createGetParams(params);
fs.readFile(mockUrl, 'utf8', function(err, content) {
if (err) {
reject(err);
} else {
resolve(content);
}
});
}
});
};
ServerConnectorMock.getApiBaseUrl = function() {
return "./testFiles/apiCalls/";
};
var originalGetApiUrl = OriginalServerConnector.getApiUrl;
ServerConnectorMock.getApiUrl = function(paramObj) {
//replace '?' with '/'
//the call is done on ServerConnectorObject (so 'this' is set properly)
return originalGetApiUrl.call(this, paramObj).replace(/\?/g, '/');
};
module.exports = ServerConnectorMock;