diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/applications/common-test/common-test.js b/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/applications/common-test/common-test.js index 46836a7d5ef554671fb175364029b6f673cc9a6c..3cdf49a5a65c722326470a25cecf05a7d8b1ce61 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/applications/common-test/common-test.js +++ b/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/applications/common-test/common-test.js @@ -11,7 +11,7 @@ var createFacade = function(action, timeoutOrNull){ action(facade); - var timeout = timeoutOrNull ? timeoutOrNull : 10000; + var timeout = timeoutOrNull ? timeoutOrNull : 30000; var checkInterval = 100; var intervalTotal = 0; diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/applications/openbis-test/openbis-test.js b/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/applications/openbis-test/openbis-test.js index b94f3dac446fbc05f7cfb75e574029842cdd882f..bfb90026acc77ed5da7464bcb519b4cd8f87123a 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/applications/openbis-test/openbis-test.js +++ b/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/applications/openbis-test/openbis-test.js @@ -134,6 +134,30 @@ var downloadFile = function(url, action){ }); } +var uploadFileToSessionWorkspace = function(facade, fileName, fileContent, dataStoreCode, action){ + var dataStoreUrl = facade._internal.getDataStoreUrlForDataStoreCode(dataStoreCode); + var uploadUrl = dataStoreUrl + "/session_workspace_file_upload" + + "?filename=" + fileName + + "&id=0" + + "&startByte=0" + + "&endByte=0" + + "&sessionID=" + facade.getSession(); + + $.ajax({ + url : uploadUrl, + type : "POST", + data : fileContent, + contentType : "multipart/form-data" + }).done(function(response){ + action(response); + }); +} + +var generateRandomString = function(){ + return Math.random().toString(); +} + + test("logout", function(){ createFacade(function(facade){ facade.logout(function(){ @@ -693,7 +717,7 @@ test("createMetaproject(), updateMetaproject()", function(){ createNewMetaproject(facade, metaprojectIdentifier, function(response){ var metaproject = response.result; var metaprojectId = createMetaprojectIdentifierId(metaprojectIdentifier); - var description = new Date().getTime().toString(); + var description = generateRandomString(); ok(!metaproject.description, "Metaproject description was empty"); @@ -815,7 +839,7 @@ test("updateSampleProperties(), searchForSamples()", function(){ facade.searchForSamples(searchCriteria, function(response){ var sample = response.result[0]; - var description = new Date().getTime().toString(); + var description = generateRandomString(); var properties = { "DESCRIPTION" : description }; @@ -834,7 +858,7 @@ test("updateSampleProperties(), searchForSamples()", function(){ test("addUnofficialVocabularyTerm(), listVocabularies()", function(){ createFacadeAndLogin(function(facade){ var vocabularyCode = "MICROSCOPE"; - var termCode = new Date().getTime().toString(); + var termCode = generateRandomString(); facade.listVocabularies(function(response){ var originalVocabulary = findVocabulary(response.result, vocabularyCode); @@ -861,7 +885,7 @@ test("addUnofficialVocabularyTerm(), listVocabularies()", function(){ test("setWebAppSettings(), getWebAppSettings()", function(){ createFacadeAndLogin(function(facade){ - var webAppId = new Date().getTime().toString(); + var webAppId = generateRandomString(); facade.getWebAppSettings(webAppId, function(response){ deepEqual(response.result.settings, {}, 'Web app settings are empty'); @@ -1064,3 +1088,218 @@ test("getDownloadUrlForFileForDataSetWithTimeout()", function(){ }); }); }); + +test("createSessionWorkspaceDownloadUrl()", function(){ + createFacadeAndLogin(function(facade){ + var fileName = generateRandomString(); + var fileContent = generateRandomString(); + var dataStoreCode = null; + + uploadFileToSessionWorkspace(facade, fileName, fileContent, dataStoreCode, function(){ + var downloadUrl = facade.createSessionWorkspaceDownloadUrl(fileName); + downloadFile(downloadUrl, function(response){ + equal(response, fileContent, "Download url is correct"); + facade.close(); + }); + }); + }); +}); + +test("createSessionWorkspaceDownloadUrlForDataStore()", function(){ + createFacadeAndLogin(function(facade){ + var fileName = generateRandomString(); + var fileContent = generateRandomString(); + var dataStoreCode = "DSS-SCREENING"; + + uploadFileToSessionWorkspace(facade, fileName, fileContent, dataStoreCode, function(){ + var downloadUrl = facade.createSessionWorkspaceDownloadUrlForDataStore(fileName, dataStoreCode); + downloadFile(downloadUrl, function(response){ + equal(response, fileContent, "Download url is correct"); + facade.close(); + }); + }); + }); +}); + +test("createSessionWorkspaceDownloadLink()", function(){ + createFacadeAndLogin(function(facade){ + var fileName = generateRandomString(); + var linkText = generateRandomString(); + + var link = facade.createSessionWorkspaceDownloadLink(fileName, linkText); + + equal($(link).attr("href"), facade.createSessionWorkspaceDownloadUrl(fileName), "Link has correct url"); + equal($(link).text(), linkText, "Link has correct text"); + facade.close(); + }); +}); + +test("createSessionWorkspaceDownloadLinkForDataStore()", function(){ + createFacadeAndLogin(function(facade){ + var fileName = generateRandomString(); + var linkText = generateRandomString(); + var dataStoreCode = "DSS-SCREENING"; + + var link = facade.createSessionWorkspaceDownloadLinkForDataStore(fileName, linkText, dataStoreCode); + + equal($(link).attr("href"), facade.createSessionWorkspaceDownloadUrlForDataStore(fileName, dataStoreCode), "Link has correct url"); + equal($(link).text(), linkText, "Link has correct text"); + facade.close(); + }); +}); + +test("downloadSessionWorkspaceFile()", function(){ + createFacadeAndLogin(function(facade){ + var fileName = generateRandomString(); + var fileContent = generateRandomString(); + var dataStoreCode = null; + + uploadFileToSessionWorkspace(facade, fileName, fileContent, dataStoreCode, function(){ + facade.downloadSessionWorkspaceFile(fileName, function(response){ + equal(response, fileContent, "File has been downloaded"); + facade.close(); + }); + }); + }); +}); + +test("downloadSessionWorkspaceFileForDataStore()", function(){ + createFacadeAndLogin(function(facade){ + var fileName = generateRandomString(); + var fileContent = generateRandomString(); + var dataStoreCode = "DSS-SCREENING"; + + uploadFileToSessionWorkspace(facade, fileName, fileContent, dataStoreCode, function(){ + facade.downloadSessionWorkspaceFileForDataStore(fileName, dataStoreCode, function(response){ + equal(response, fileContent, "File has been downloaded"); + facade.close(); + }); + }); + }); +}); + +test("deleteSessionWorkspaceFile()", function(){ + createFacadeAndLogin(function(facade){ + var fileName = generateRandomString(); + var fileContent = generateRandomString(); + var dataStoreCode = null; + + uploadFileToSessionWorkspace(facade, fileName, fileContent, dataStoreCode, function(){ + facade.deleteSessionWorkspaceFile(fileName, function(){ + facade.downloadSessionWorkspaceFile(fileName, function(response){ + ok(response.error.indexOf("No such file or directory"), "File has been deleted"); + facade.close(); + }); + }); + }); + }); +}); + +test("deleteSessionWorkspaceFileForDataStore()", function(){ + createFacadeAndLogin(function(facade){ + var fileName = generateRandomString(); + var fileContent = generateRandomString(); + var dataStoreCode = "DSS-SCREENING"; + + uploadFileToSessionWorkspace(facade, fileName, fileContent, dataStoreCode, function(){ + facade.deleteSessionWorkspaceFileForDataStore(fileName, dataStoreCode, function(){ + facade.downloadSessionWorkspaceFileForDataStore(fileName, dataStoreCode, function(response){ + ok(response.error.indexOf("No such file or directory"), "File has been deleted"); + facade.close(); + }); + }); + }); + }); +}); + +test("getPathToDataSet()", function(){ + createFacadeAndLogin(function(facade){ + var dataSetCode = "20110913114645299-83009"; + var overrideStoreRootPathOrNull = ""; + + facade.getPathToDataSet(dataSetCode, overrideStoreRootPathOrNull, function(response){ + equal(response.result, "/1/678243C3-BD97-42E4-B04B-34DA0C43564D/04/b7/53/20110913114645299-83009", "Data set path is correct"); + facade.close(); + }); + }); +}); + +test("listAllShares()", function(){ + createFacadeAndLogin(function(facade){ + facade.listAllShares(function(response){ + assertObjectsCount(response.result, 2); + assertObjectsWithValues(response.result, "shareId", ["1", "2"]); + facade.close(); + }); + }); +}); + +test("listAllSharesForDataStore()", function(){ + createFacadeAndLogin(function(facade){ + var dataStoreCode = "DSS-SCREENING"; + + facade.listAllSharesForDataStore(dataStoreCode, function(response){ + assertObjectsCount(response.result, 2); + assertObjectsWithValues(response.result, "shareId", ["1", "2"]); + facade.close(); + }); + }); +}); + +test("shuffleDataSet()", function(){ + createFacadeAndLogin(function(facade){ + var dataSetCode = "20110817134351959-81695"; + var share1Id = "1"; + var share2Id = "2"; + var overrideStoreRootPathOrNull = ""; + var isInShare = function(dataSetPath, shareId){ + return dataSetPath.indexOf("/" + shareId + "/") == 0 + } + + facade.getPathToDataSet(dataSetCode, overrideStoreRootPathOrNull, function(response){ + var toShareId = null; + + if(isInShare(response.result, share1Id)){ + toShareId = share2Id; + }else{ + toShareId = share1Id; + } + + facade.shuffleDataSet(dataSetCode, toShareId, function(response){ + facade.getPathToDataSet(dataSetCode, overrideStoreRootPathOrNull, function(response){ + ok(isInShare(response.result, toShareId), "Data set has been moved to a different share"); + facade.close(); + }); + }); + }); + }); +}); + +/* + +TODO add validation scripts + +test("getValidationScript()", function(){ + createFacadeAndLogin(function(facade){ + var dataSetTypeOrNull = "HCS_IMAGE_RAW"; + + facade.getValidationScript(dataSetTypeOrNull, function(response){ + ok(response.result, "Got a validation script"); + facade.close(); + }); + }); +}); + +test("getValidationScriptForDataStore()", function(){ + createFacadeAndLogin(function(facade){ + var dataSetTypeOrNull = "HCS_IMAGE_RAW"; + var dataStoreCode = "DSS-SCREENING"; + + facade.getValidationScriptForDataStore(dataSetTypeOrNull, dataStoreCode, function(response){ + ok(response.result, "Got a validation script"); + facade.close(); + }); + }); +}); + +*/ \ No newline at end of file diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/js-new/openbis.js b/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/js-new/openbis.js index c703b2a3da0e0085e85945eaf2a661db9a54b255..1bedd586926cf49b80cbe199eb07aead239f5383 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/js-new/openbis.js +++ b/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/js-new/openbis.js @@ -37,23 +37,30 @@ _openbisInternal.prototype.ajaxRequest = function(settings) { settings.processData = false; settings.dataType = "json"; settings.data = this.jsonRequestData(settings.data); - + settings.success = this.ajaxRequestSuccess(settings.success); + // we call the same method on error for backward compatibility + settings.error = this.ajaxRequestSuccess(settings.success); + $.ajax(settings) +} + +_openbisInternal.prototype.ajaxRequestSuccess = function(action){ var openbisObj = this; - var originalOnSuccess = settings.success; - - settings.success = function(response){ + return function(response){ if(response.error){ openbisObj.log("Request failed: " + JSON.stringify(response.error)); - }else{ - originalOnSuccess(response); } + action(response); }; - - settings.error = function(xhr, status, error){ +} + +_openbisInternal.prototype.ajaxRequestError = function(action){ + var openbisObj = this; + return function(xhr, status, error){ openbisObj.log("Request failed: " + error); + action({ + "error" : error + }); }; - - $.ajax(settings) } // Functions for working with cookies (see http://www.quirksmode.org/js/cookies.html) @@ -85,15 +92,20 @@ _openbisInternal.prototype.eraseCookie = function(name) { _openbisInternal.prototype.getDataStoreUrlForDataStoreCode = function(dataStoreCodeOrNull) { if(this.dataStores.length == 0){ - throw "Couldn't get a data store url as there are no data stores configured." + throw "Couldn't get a data store url as there are no data stores configured."; }else{ if(dataStoreCodeOrNull){ + var dataStoreUrl = null; $.each(this.dataStores, function(index, dataStore){ if(dataStore.code == dataStoreCodeOrNull){ - return dataStore.downloadUrl; + dataStoreUrl = dataStore.downloadUrl; } }); - return null; + if(dataStoreUrl){ + return dataStoreUrl; + }else{ + throw "Couldn't get a data store url because data store with " + dataStoreCodeOrNull + " code does not exist."; + } }else{ if(this.dataStores.length == 1){ return this.dataStores[0].downloadUrl; @@ -106,7 +118,7 @@ _openbisInternal.prototype.getDataStoreUrlForDataStoreCode = function(dataStoreC _openbisInternal.prototype.getDataStoreUrlForDataSetCode = function(dataSetCode, action) { if(this.dataStores.length == 0){ - throw "Couldn't get a data store url as there are no data stores configured." + throw "Couldn't get a data store url as there are no data stores configured."; }else if(this.dataStores.length == 1){ action(this.dataStores[0].downloadUrl); }else{ @@ -128,7 +140,7 @@ _openbisInternal.prototype.getDataStoreUrlForDataSetCode = function(dataSetCode, } } -_openbisInternal.prototype.getDataStoreApiUrlForDataStoreCode = function(dataStoreCodeOrNull, action) { +_openbisInternal.prototype.getDataStoreApiUrlForDataStoreCode = function(dataStoreCodeOrNull) { var dataStoreUrl = this.getDataStoreUrlForDataStoreCode(dataStoreCodeOrNull); if(dataStoreUrl){ return dataStoreUrl + "/rmi-dss-api-v1.json"; @@ -189,16 +201,17 @@ openbis.prototype.login = function(userId, userPassword, action) { }, success: function(loginResponse) { - openbisObj._internal.sessionToken = loginResponse.result; - openbisObj.rememberSession(); - openbisObj.listDataStores(function(storesResponse){ - openbisObj._internal.dataStores = storesResponse.result; - action(loginResponse) - }); - }, - error: function() { - alert("Login failed") - } + if(loginResponse.error){ + alert("Login failed") + }else{ + openbisObj._internal.sessionToken = loginResponse.result; + openbisObj.rememberSession(); + openbisObj.listDataStores(function(storesResponse){ + openbisObj._internal.dataStores = storesResponse.result; + action(loginResponse) + }); + } + } }); } @@ -1300,7 +1313,8 @@ openbis.prototype.downloadSessionWorkspaceFileForDataStore = function(filePath, $.ajax({ type: "GET", url: this.createSessionWorkspaceDownloadUrlForDataStore(filePath, dataStoreCodeOrNull), - success: action + success: this._internal.ajaxRequestSuccess(action), + error: this._internal.ajaxRequestError(action) }); } @@ -1317,15 +1331,13 @@ openbis.prototype.deleteSessionWorkspaceFile = function(filePath, action) { * @method */ openbis.prototype.deleteSessionWorkspaceFileForDataStore = function(filePath, dataStoreCodeOrNull, action) { - var openbisObj = this; - this._internal.getDataStoreApiUrlForDataStoreCode(dataStoreCodeOrNull, function(dataStoreApiUrl){ - openbisObj._internal.ajaxRequest({ - url: dataStoreApiUrl, - data: { "method" : "deleteSessionWorkspaceFile", - "params" : [ openbisObj.getSession(), filePath ] - }, - success: action - }); + this._internal.ajaxRequest({ + url: this._internal.getDataStoreApiUrlForDataStoreCode(dataStoreCodeOrNull), + data: { + "method" : "deleteSessionWorkspaceFile", + "params" : [ this.getSession(), filePath ] + }, + success: action }); } @@ -1352,7 +1364,7 @@ openbis.prototype.getPathToDataSet = function(dataSetCode, overrideStoreRootPath * @method */ openbis.prototype.listAllShares = function(action) { - this.listAllShares(null, action); + this.listAllSharesForDataStore(null, action); } /** @@ -1362,15 +1374,13 @@ openbis.prototype.listAllShares = function(action) { * @method */ openbis.prototype.listAllSharesForDataStore = function(dataStoreCodeOrNull, action) { - var openbisObj = this; - this._internal.getDataStoreApiUrlForDataStoreCode(dataStoreCodeOrNull, function(dataStoreApiUrl){ - openbisObj._internal.ajaxRequest({ - url: dataStoreApiUrl, - data: { "method" : "listAllShares", - "params" : [ openbisObj.getSession() ] - }, - success: action - }); + this._internal.ajaxRequest({ + url: this._internal.getDataStoreApiUrlForDataStoreCode(dataStoreCodeOrNull), + data: { + "method" : "listAllShares", + "params" : [ this.getSession() ] + }, + success: action }); } @@ -1407,15 +1417,13 @@ openbis.prototype.getValidationScript = function(dataSetTypeOrNull, action) { * @method */ openbis.prototype.getValidationScriptForDataStore = function(dataSetTypeOrNull, dataStoreCodeOrNull, action) { - var openbisObj = this; - this._internal.getDataStoreApiUrlForDataStoreCode(dataStoreCodeOrNull, function(dataStoreApiUrl){ - openbisObj._internal.ajaxRequest({ - url: dataStoreApiUrl, - data: { "method" : "getValidationScript", - "params" : [ openbisObj.getSession(), dataSetTypeOrNull ] - }, - success: action - }); + this._internal.ajaxRequest({ + url: this._internal.getDataStoreApiUrlForDataStoreCode(dataStoreCodeOrNull), + data: { + "method" : "getValidationScript", + "params" : [ this.getSession(), dataSetTypeOrNull ] + }, + success: action }); }