From 6bf09a3f112ef3f2fe48c79d949fe6067774811a Mon Sep 17 00:00:00 2001 From: Mark Rogers Date: Mon, 8 Dec 2008 15:45:34 +0000 Subject: [PATCH] Merge WCM_SERVICES2 to HEAD 12236 Implementation of REST WCM Revert git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@12308 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../wcm/sandbox/Asset/revert.post.desc.xml | 24 ++ .../wcm/sandbox/Asset/revert.post.json.ftl | 6 + .../wcm/sandbox/Asset/revert.post.json.js | 99 +++++ .../web/scripts/wcm/sandbox/AssetTest.java | 345 +++++++++++++++++- 4 files changed, 471 insertions(+), 3 deletions(-) create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/wcm/sandbox/Asset/revert.post.desc.xml create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/wcm/sandbox/Asset/revert.post.json.ftl create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/wcm/sandbox/Asset/revert.post.json.js diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/wcm/sandbox/Asset/revert.post.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/wcm/sandbox/Asset/revert.post.desc.xml new file mode 100644 index 0000000000..272dd88d12 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/wcm/sandbox/Asset/revert.post.desc.xml @@ -0,0 +1,24 @@ + + Revert modified assets within the specified author sandbox. + + + If the optional webApp argument is specified then reverts the modified assets within that web app. +
+ JSON data fileds +
+
all
boolean is this submit all? (optional)
+
assets
array, of JSON objects containing a path property (optional). If the "all" option is true then this parameter is ignored
+
paths
array, of String paths. If the "all" option is true then this parameter is ignored
+
+ + ]]> +
+ /api/wcm/webprojects/{webprojectref}/sandboxes/{sandboxref}/reverter + /api/wcm/webprojects/{webprojectref}/sandboxes/{sandboxref}/reverter?webApp={webApp?} + + user + required + WCM Service +
\ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/wcm/sandbox/Asset/revert.post.json.ftl b/config/alfresco/templates/webscripts/org/alfresco/repository/wcm/sandbox/Asset/revert.post.json.ftl new file mode 100644 index 0000000000..523c5b1639 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/wcm/sandbox/Asset/revert.post.json.ftl @@ -0,0 +1,6 @@ +<#import "asset.lib.ftl" as assetLib/> +{ + +} + + diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/wcm/sandbox/Asset/revert.post.json.js b/config/alfresco/templates/webscripts/org/alfresco/repository/wcm/sandbox/Asset/revert.post.json.js new file mode 100644 index 0000000000..419eafabbf --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/wcm/sandbox/Asset/revert.post.json.js @@ -0,0 +1,99 @@ +/* + * Revert the modified items within a sandbox (JSON) + */ +function main() +{ + var urlElements = url.extension.split("/"); + var shortName = urlElements[0]; + var boxName = urlElements[2]; + + var webproject = webprojects.getWebProject(shortName); + if (webproject == null) + { + // Web Project cannot be found + status.setCode(status.STATUS_NOT_FOUND, "The webproject, " + shortName + ", does not exist."); + return; + } + + var sandbox = webproject.getSandbox(boxName); + if (sandbox == null) + { + // Site cannot be found + status.setCode(status.STATUS_NOT_FOUND, "The sandbox, " + boxName + ", in webproject, " + shortName + ", does not exist."); + return; + } + + // URL + var webApp = args["webApp"]; + + // Optional + var isAll = false; + if(json.has("all")) + { + isAll = json.getBoolean("all"); + } + var assets = null; + if(json.has("assets")) + { + assets = json.getJSONArray("assets"); + } + var paths = null; + if(json.has("paths")) + { + paths = json.getJSONArray("paths"); + } + + if(paths == null && assets == null && isAll == false ) + { + status.setCode(status.STATUS_BAD_REQUEST, "One of 'all', 'assets' or 'paths' must be specified"); + return; + } + + // Now do the revert + if(isAll) + { + if(webApp != null) + { + sandbox.revertAllWebApp(webApp); + } + else + { + sandbox.revertAll(); + } + } + else + { + var i = 0; + var input = new Array(); + + if(assets != null) + { + for(var x = 0; x < assets.length(); x++) + { + var jsonObj = assets.getJSONObject(x); + input[i++] = jsonObj.get("path"); + } + } + + if(paths != null) + { + for(var k = 0; k < paths.length(); k++) + { + var path = paths.get(k); + input[i++] = path; + } + } + + // submit a list of files and directories + sandbox.revert(input); + } + + // set model properties + model.sandbox = sandbox; + model.webproject = webproject; + + status.setCode(status.STATUS_OK, "Reverted"); +} + +main() + diff --git a/source/java/org/alfresco/repo/web/scripts/wcm/sandbox/AssetTest.java b/source/java/org/alfresco/repo/web/scripts/wcm/sandbox/AssetTest.java index 666ab4758d..daba076ef4 100644 --- a/source/java/org/alfresco/repo/web/scripts/wcm/sandbox/AssetTest.java +++ b/source/java/org/alfresco/repo/web/scripts/wcm/sandbox/AssetTest.java @@ -224,7 +224,7 @@ public class AssetTest extends BaseWebScriptTest { } /** - * test the modified assets methods + * Test the modified assets (Web App) methods * @throws Exception */ public void testModifiedAssetsWebAppTest() throws Exception @@ -646,12 +646,351 @@ public class AssetTest extends BaseWebScriptTest { } /** - * test the modified assets methods + * Test the submit assets (Web App) methods + * @throws Exception + */ + public void testSubmitAssetsWebAppTest() throws Exception + { + this.authenticationComponent.setCurrentUser("admin"); + String webprojref = createWebProject(); + createMembership(webprojref, USER_ONE, ROLE_CONTENT_MANAGER); + String sandboxref = createSandbox(webprojref, USER_ONE); + + //TODO REPLACE THIS IMPLEMENTATION WITH THE REST API ONCE AVAILABLE + String bodgeRootPath = sandboxref + AVM_STORE_SEPARATOR + "/www/avm_webapps/" + WEBAPP_ROOT; + String bodgeYellowPath = sandboxref + AVM_STORE_SEPARATOR + "/www/avm_webapps/" + WEBAPP_YELLOW; + avmLockingAwareService.createDirectory(sandboxref + AVM_STORE_SEPARATOR + "/www/avm_webapps", WEBAPP_YELLOW); + + String submitterURL = URL_WEB_PROJECT + "/" + webprojref + URI_SANDBOXES + "/" + sandboxref + "/submitter"; + JSONObject submitForm = new JSONObject(); + submitForm.put("label", "the label"); + submitForm.put("comment", "the comment"); + submitForm.put("all", true); + sendRequest(new PostRequest(submitterURL, submitForm.toString(), "application/json"), Status.STATUS_OK); + + /** + * Now we can set up our test data + */ + avmLockingAwareService.createFile(bodgeRootPath, "rootFile1"); + avmLockingAwareService.createFile(bodgeYellowPath, "yellowFile1"); + + /** + * Submit YELLOW - Should leave root alone + */ + submitForm.put("label", "yellow submit"); + submitForm.put("comment", "yellow submit"); + submitForm.put("all", true); + sendRequest(new PostRequest(submitterURL + "?webApp=" + WEBAPP_YELLOW, submitForm.toString(), "application/json"), Status.STATUS_OK); + + /** + * Get the modified asset (yellow should have been submitted leaving root + */ + { + String sandboxesURL = URL_WEB_PROJECT + "/" + webprojref + URI_SANDBOXES + "/" + sandboxref + "/modified"; + Response list = sendRequest(new GetRequest(sandboxesURL), Status.STATUS_OK); + JSONObject result = new JSONObject(list.getContentAsString()); + JSONArray lookupResult = result.getJSONArray("data"); + + assertEquals("testListUserSandbox", lookupResult.length(), 1); + + // Now check the contents.. + JSONObject x = lookupResult.getJSONObject(0); + String name = x.getString("name"); + + assertNotNull("name is null", name); + assertEquals("name is wrong", "rootFile1", name); + } + } + + /** + * test the revert assets methods * @throws Exception */ public void testRevertAssetsTest() throws Exception { + this.authenticationComponent.setCurrentUser("admin"); + String webprojref = createWebProject(); + createMembership(webprojref, USER_ONE, ROLE_CONTENT_MANAGER); + String sandboxref = createSandbox(webprojref, USER_ONE); + //TODO REPLACE THIS IMPLEMENTATION WITH THE REST API ONCE AVAILABLE + String bodgePath = sandboxref + AVM_STORE_SEPARATOR + "/www/avm_webapps/ROOT"; + avmLockingAwareService.createFile(bodgePath, "myFile1"); + + String reverterURL = URL_WEB_PROJECT + "/" + webprojref + URI_SANDBOXES + "/" + sandboxref + "/reverter"; + + /** + * Revert all Negative test - invalid project + */ + { + + String crapURL = URL_WEB_PROJECT + "/" + "crap" + URI_SANDBOXES + "/" + sandboxref + "/reverter"; + + JSONObject submitForm = new JSONObject(); + submitForm.put("all", true); + sendRequest(new PostRequest(crapURL, submitForm.toString(), "application/json"), Status.STATUS_NOT_FOUND); + } + + /** + * Submit all Negative test - invalid sandbox + */ + { + + String crapURL = URL_WEB_PROJECT + "/" + webprojref + URI_SANDBOXES + "/" + "crap" + "/reverter"; + + JSONObject submitForm = new JSONObject(); + submitForm.put("all", true); + sendRequest(new PostRequest(crapURL, submitForm.toString(), "application/json"), Status.STATUS_NOT_FOUND); + } + + /** + * Submit all Negative test - none of all, assets or paths. + */ + { + JSONObject submitForm = new JSONObject(); + submitForm.put("all", false); + sendRequest(new PostRequest(reverterURL, submitForm.toString(), "application/json"), Status.STATUS_BAD_REQUEST); + } + + /** + * Positive test - Revert all + */ + { + avmLockingAwareService.createFile(bodgePath, "myFile2"); + + { + String sandboxesURL = URL_WEB_PROJECT + "/" + webprojref + URI_SANDBOXES + "/" + sandboxref + "/modified"; + Response list = sendRequest(new GetRequest(sandboxesURL), Status.STATUS_OK); + JSONObject result = new JSONObject(list.getContentAsString()); + JSONArray lookupResult = result.getJSONArray("data"); + + assertTrue("testListUserSandbox", lookupResult.length() > 0); + } + + JSONObject submitForm = new JSONObject(); + submitForm.put("all", true); + Response response = sendRequest(new PostRequest(reverterURL, submitForm.toString(), "application/json"), Status.STATUS_OK); + //TODO Nothing in the response now. + + checkSandboxEmpty(webprojref, sandboxref); + + } + + /** + * Revert via paths + */ + { + avmLockingAwareService.createFile(bodgePath, "myFile3"); + JSONObject submitForm = new JSONObject(); + + JSONArray paths = new JSONArray(); + paths.put("/www/avm_webapps/ROOT/myFile3"); + submitForm.put("paths", paths); + Response response = sendRequest(new PostRequest(reverterURL, submitForm.toString(), "application/json"), Status.STATUS_OK); + checkSandboxEmpty(webprojref, sandboxref); + } + + /** + * Revert assets - get a list of modified assets and revert them back + */ + { + avmLockingAwareService.createFile(bodgePath, "myFile4"); + + { + String sandboxesURL = URL_WEB_PROJECT + "/" + webprojref + URI_SANDBOXES + "/" + sandboxref + "/modified"; + Response list = sendRequest(new GetRequest(sandboxesURL), Status.STATUS_OK); + JSONObject result = new JSONObject(list.getContentAsString()); + JSONArray lookupResult = result.getJSONArray("data"); + + assertTrue("testListUserSandbox", lookupResult.length() > 0); + + JSONObject submitForm = new JSONObject(); + submitForm.put("assets", lookupResult); + Response response = sendRequest(new PostRequest(reverterURL, submitForm.toString(), "application/json"), Status.STATUS_OK); + + } + checkSandboxEmpty(webprojref, sandboxref); + + } + + /** + * Revert assets more complex example - get a list of modified assets and submit them back + * Also has a delete to revert + */ + { + avmLockingAwareService.createFile(bodgePath, "buffy.jpg"); + avmLockingAwareService.createDirectory(bodgePath, "vampires"); + avmLockingAwareService.createFile(bodgePath + "/vampires", "master"); + avmLockingAwareService.createFile(bodgePath + "/vampires", "drusilla"); + avmLockingAwareService.createDirectory(bodgePath, "humans"); + avmLockingAwareService.createFile(bodgePath + "/humans", "willow"); + avmLockingAwareService.createFile(bodgePath + "/humans", "xander"); + + { + String sandboxesURL = URL_WEB_PROJECT + "/" + webprojref + URI_SANDBOXES + "/" + sandboxref + "/modified"; + Response list = sendRequest(new GetRequest(sandboxesURL), Status.STATUS_OK); + JSONObject result = new JSONObject(list.getContentAsString()); + JSONArray lookupResult = result.getJSONArray("data"); + + assertTrue("testListUserSandbox", lookupResult.length() > 0); + + JSONObject submitForm = new JSONObject(); + submitForm.put("assets", lookupResult); + Response response = sendRequest(new PostRequest(reverterURL, submitForm.toString(), "application/json"), Status.STATUS_OK); + + } + checkSandboxEmpty(webprojref, sandboxref); + + } + + + /** + * Now finally, a big complicated reversion of assets and paths. + */ + { + // First submit a chunk of data + avmLockingAwareService.createFile(bodgePath, "buffy.jpg"); + avmLockingAwareService.createDirectory(bodgePath, "vampires"); + avmLockingAwareService.createFile(bodgePath + "/vampires", "master"); + avmLockingAwareService.createFile(bodgePath + "/vampires", "drusilla"); + avmLockingAwareService.createDirectory(bodgePath, "humans"); + avmLockingAwareService.createFile(bodgePath + "/humans", "willow"); + avmLockingAwareService.createFile(bodgePath + "/humans", "xander"); + + JSONObject submitForm = new JSONObject(); + submitForm.put("label", "the label"); + submitForm.put("comment", "the comment"); + submitForm.put("all", true); + String submitterURL = URL_WEB_PROJECT + "/" + webprojref + URI_SANDBOXES + "/" + sandboxref + "/submitter"; + sendRequest(new PostRequest(submitterURL, submitForm.toString(), "application/json"), Status.STATUS_OK); + + // Now we can set up the data that will get reverted + + // single file in existing dir + avmLockingAwareService.createFile(bodgePath + "/vampires", "angel"); + + //delete from an existing dir + avmLockingAwareService.removeNode(sandboxref + AVM_STORE_SEPARATOR + "/www/avm_webapps/ROOT/vampires/drusilla"); + // multiple file in existing dir + avmLockingAwareService.createFile(bodgePath + "/humans", "giles"); + avmLockingAwareService.createFile(bodgePath + "/humans", "dawn"); + avmLockingAwareService.createFile(bodgePath + "/humans", "anya"); + // new directory + avmLockingAwareService.createDirectory(bodgePath, "cast"); + avmLockingAwareService.createFile(bodgePath + "/cast", "Anthony Head"); + avmLockingAwareService.createFile(bodgePath + "/cast", "James Marsters"); + + + { + String sandboxesURL = URL_WEB_PROJECT + "/" + webprojref + URI_SANDBOXES + "/" + sandboxref + "/modified"; + Response list = sendRequest(new GetRequest(sandboxesURL), Status.STATUS_OK); + JSONObject result = new JSONObject(list.getContentAsString()); + JSONArray lookupResult = result.getJSONArray("data"); + JSONArray assets = new JSONArray(); + JSONArray paths = new JSONArray(); + JSONArray omitted = new JSONArray(); + assertTrue("testListUserSandbox", lookupResult.length() > 4); + + /** + * chop off 3 items from the modified list. First 2 go into path which should leave 1 unsubmitted. + */ + for(int i = 0; i < lookupResult.length(); i++) + { + if (i < 2) + { + // do nothing + omitted.put(lookupResult.getJSONObject(i).get("path")); + } + else if ( i < 4) + { + // copy into paths + paths.put(lookupResult.getJSONObject(i).get("path")); + } + else + { + // copy into assets + assets.put(lookupResult.getJSONObject(i)); + } + } + + JSONObject revertForm = new JSONObject(); + revertForm.put("assets", assets); + revertForm.put("paths", paths); + + sendRequest(new PostRequest(reverterURL, revertForm.toString(), "application/json"), Status.STATUS_OK); + + Response listTwo = sendRequest(new GetRequest(sandboxesURL), Status.STATUS_OK); + JSONObject resultTwo = new JSONObject(listTwo.getContentAsString()); + JSONArray lookupResultTwo = resultTwo.getJSONArray("data"); + assertTrue("testListUserSandbox", lookupResultTwo.length() == 2); + + /** + * Now revert the omitted two files + */ + JSONObject submitOmitted = new JSONObject(); + submitOmitted.put("paths", omitted); + sendRequest(new PostRequest(reverterURL, submitOmitted.toString(), "application/json"), Status.STATUS_OK); + } + checkSandboxEmpty(webprojref, sandboxref); + + } } -} + /** + * Test the revert assets (Web App) methods + * @throws Exception + */ + public void testRevertAssetsWebAppTest() throws Exception + { + this.authenticationComponent.setCurrentUser("admin"); + String webprojref = createWebProject(); + createMembership(webprojref, USER_ONE, ROLE_CONTENT_MANAGER); + String sandboxref = createSandbox(webprojref, USER_ONE); + + //TODO REPLACE THIS IMPLEMENTATION WITH THE REST API ONCE AVAILABLE + String bodgeRootPath = sandboxref + AVM_STORE_SEPARATOR + "/www/avm_webapps/" + WEBAPP_ROOT; + String bodgeYellowPath = sandboxref + AVM_STORE_SEPARATOR + "/www/avm_webapps/" + WEBAPP_YELLOW; + avmLockingAwareService.createDirectory(sandboxref + AVM_STORE_SEPARATOR + "/www/avm_webapps", WEBAPP_YELLOW); + + String submitterURL = URL_WEB_PROJECT + "/" + webprojref + URI_SANDBOXES + "/" + sandboxref + "/submitter"; + JSONObject submitForm = new JSONObject(); + submitForm.put("label", "the label"); + submitForm.put("comment", "the comment"); + submitForm.put("all", true); + sendRequest(new PostRequest(submitterURL, submitForm.toString(), "application/json"), Status.STATUS_OK); + + /** + * Now we can set up our test data + */ + avmLockingAwareService.createFile(bodgeRootPath, "rootFile1"); + avmLockingAwareService.createFile(bodgeYellowPath, "yellowFile1"); + + /** + * Revert YELLOW - Should leave root alone + */ + + String reverterURL = URL_WEB_PROJECT + "/" + webprojref + URI_SANDBOXES + "/" + sandboxref + "/reverter?webApp=" + WEBAPP_YELLOW; + JSONObject revertForm = new JSONObject(); + revertForm.put("all", true); + sendRequest(new PostRequest(reverterURL, revertForm.toString(), "application/json"), Status.STATUS_OK); + + /** + * Get the modified asset (yellow should have been reverted leaving root + */ + { + String sandboxesURL = URL_WEB_PROJECT + "/" + webprojref + URI_SANDBOXES + "/" + sandboxref + "/modified"; + Response list = sendRequest(new GetRequest(sandboxesURL), Status.STATUS_OK); + JSONObject result = new JSONObject(list.getContentAsString()); + JSONArray lookupResult = result.getJSONArray("data"); + + assertTrue("testListUserSandbox", lookupResult.length() == 1); + + // Now check the contents.. + JSONObject x = lookupResult.getJSONObject(0); + String name = x.getString("name"); + + assertNotNull("name is null", name); + assertEquals("name is wrong", "rootFile1", name); + } + } // End of testRevertAssetsWebAppTest +} // End of AssetTest