From 7d32941e9c2b065f19b82b6d7848829419ba8987 Mon Sep 17 00:00:00 2001 From: Roy Wetherall Date: Thu, 1 May 2008 16:48:30 +0000 Subject: [PATCH] Site Service CRUD API's (Java, JavaScript and REST), unit test methods added to JS API to help unit test JS API's git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@8986 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../repository/site/site.delete.desc.xml | 8 + .../alfresco/repository/site/site.delete.js | 16 ++ .../repository/site/site.delete.json.ftl | 0 .../repository/site/site.get.desc.xml | 8 + .../org/alfresco/repository/site/site.get.js | 17 +++ .../repository/site/site.get.json.ftl | 2 + .../org/alfresco/repository/site/site.lib.ftl | 10 ++ .../repository/site/site.put.desc.xml | 8 + .../org/alfresco/repository/site/site.put.js | 21 +++ .../repository/site/site.put.json.ftl | 2 + .../repository/site/sites.get.json.ftl | 14 +- .../repository/site/sites.post.json.ftl | 10 +- .../repo/web/scripts/BaseWebScriptTest.java | 32 ++++ .../web/scripts/site/SiteServiceTest.java | 140 ++++++++++++++++++ .../web/scripts/site/TestSiteService.java | 84 ----------- 15 files changed, 270 insertions(+), 102 deletions(-) create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/site/site.delete.desc.xml create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/site/site.delete.js create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/site/site.delete.json.ftl create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/site/site.get.desc.xml create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/site/site.get.js create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/site/site.get.json.ftl create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/site/site.lib.ftl create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/site/site.put.desc.xml create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/site/site.put.js create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/site/site.put.json.ftl create mode 100644 source/java/org/alfresco/repo/web/scripts/site/SiteServiceTest.java delete mode 100644 source/java/org/alfresco/repo/web/scripts/site/TestSiteService.java diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.delete.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.delete.desc.xml new file mode 100644 index 0000000000..e6bcc68e8b --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.delete.desc.xml @@ -0,0 +1,8 @@ + + Site + Delete the details of a site. + /api/site/{shortname} + + guest + required + \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.delete.js b/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.delete.js new file mode 100644 index 0000000000..96977b8cda --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.delete.js @@ -0,0 +1,16 @@ +// Get the shortname +var shortName = url.extension; + +// Get the site +var site = siteService.getSite(shortName); +if (site != null) +{ + // Delete the site + site.deleteSite(); +} +else +{ + // Return 404 + status.code = 404; + status.redirect = true; +} \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.delete.json.ftl b/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.delete.json.ftl new file mode 100644 index 0000000000..e69de29bb2 diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.get.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.get.desc.xml new file mode 100644 index 0000000000..e040cafc55 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.get.desc.xml @@ -0,0 +1,8 @@ + + Site + Get the details of a site. + /api/site/{shortname} + + guest + required + \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.get.js b/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.get.js new file mode 100644 index 0000000000..a487f4c622 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.get.js @@ -0,0 +1,17 @@ +// Get the shortname +var shortName = url.extension; + +// Get the site +var site = siteService.getSite(shortName); + +if (site != null) +{ + // Pass the site to the template + model.site = site; +} +else +{ + // Return 404 + status.code = 404; + status.redirect = true; +} \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.get.json.ftl b/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.get.json.ftl new file mode 100644 index 0000000000..c385c7431a --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.get.json.ftl @@ -0,0 +1,2 @@ +<#import "site.lib.ftl" as siteLib/> +<@siteLib.siteJSON site=site/> \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.lib.ftl b/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.lib.ftl new file mode 100644 index 0000000000..d965eeba0c --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.lib.ftl @@ -0,0 +1,10 @@ +<#macro siteJSON site> +{ + "url" : "${url.serviceContext}/api/sites/${site.shortName}", + "sitePreset" : "${site.sitePreset}", + "shortName" : "${site.shortName}", + "title" : "${site.title}", + "description" : "${site.description}", + "isPublic" : ${site.isPublic?string("true", "false")} +} + \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.put.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.put.desc.xml new file mode 100644 index 0000000000..a0b0bf9d18 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.put.desc.xml @@ -0,0 +1,8 @@ + + Site + Update the details of a site. + /api/site/{shortname} + + guest + required + \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.put.js b/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.put.js new file mode 100644 index 0000000000..27cbc0b125 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.put.js @@ -0,0 +1,21 @@ +// Get the site +var shortName = url.extension; +var site = siteService.getSite(shortName); + +if (site != null) +{ + // Update the sites details + site.title = json.get("title"); + site.description = json.get("description"); + site.isPublic = json.getBoolean("isPublic"); + site.save(); + + // Pass the model to the template + model.site = site; +} +else +{ + // Return 404 + status.code = 404; + status.redirect = true; +} \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.put.json.ftl b/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.put.json.ftl new file mode 100644 index 0000000000..c385c7431a --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/site/site.put.json.ftl @@ -0,0 +1,2 @@ +<#import "site.lib.ftl" as siteLib/> +<@siteLib.siteJSON site=site/> \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/site/sites.get.json.ftl b/config/alfresco/templates/webscripts/org/alfresco/repository/site/sites.get.json.ftl index 77a270c2ad..0748caf69e 100644 --- a/config/alfresco/templates/webscripts/org/alfresco/repository/site/sites.get.json.ftl +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/site/sites.get.json.ftl @@ -1,14 +1,8 @@ -<#assign first = true/> +<#import "site.lib.ftl" as siteLib/> + [ <#list sites as site> - <#if first == true><#assign first = false/><#else>, - { - "url" : "${url.serviceContext}/api/sites/${site.shortName}", - "sitePreset" : "${site.sitePreset}", - "shortName" : "${site.shortName}", - "title" : "${site.title}", - "description" : "${site.description}", - "isPublic" : ${site.isPublic?string("true", "false")} - } + <@siteLib.siteJSON site=site/> + <#if site_has_next>, ] \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/site/sites.post.json.ftl b/config/alfresco/templates/webscripts/org/alfresco/repository/site/sites.post.json.ftl index 428223ce33..c385c7431a 100644 --- a/config/alfresco/templates/webscripts/org/alfresco/repository/site/sites.post.json.ftl +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/site/sites.post.json.ftl @@ -1,8 +1,2 @@ -{ - "url" : "${url.serviceContext}/api/sites/${site.shortName}", - "sitePreset" : "${site.sitePreset}", - "shortName" : "${site.shortName}", - "title" : "${site.title}", - "description" : "${site.description}", - "isPublic" : ${site.isPublic?string("true", "false")} -} \ No newline at end of file +<#import "site.lib.ftl" as siteLib/> +<@siteLib.siteJSON site=site/> \ No newline at end of file diff --git a/source/java/org/alfresco/repo/web/scripts/BaseWebScriptTest.java b/source/java/org/alfresco/repo/web/scripts/BaseWebScriptTest.java index 7984e3a61c..4039edbe91 100644 --- a/source/java/org/alfresco/repo/web/scripts/BaseWebScriptTest.java +++ b/source/java/org/alfresco/repo/web/scripts/BaseWebScriptTest.java @@ -71,11 +71,27 @@ public abstract class BaseWebScriptTest extends TestCase return sendRequest(METHOD_GET, url, expectedStatus, null, null); } + /** + * "DELETE" the url and check for the expected status code + * + * @param url + * @param expectedStatus + * @return + * @throws IOException + */ + protected MockHttpServletResponse deleteRequest(String url, int expectedStatus) + throws IOException + { + return sendRequest(METHOD_DELETE, url, expectedStatus, null, null); + } + /** * "POST" the url and check for the expected status code * * @param url * @param expectedStatus + * @param body + * @param contentType * @return * @throws IOException */ @@ -85,6 +101,22 @@ public abstract class BaseWebScriptTest extends TestCase return sendRequest(METHOD_POST, url, expectedStatus, body, contentType); } + /** + * "PUT" the url and check for the expected status code + * + * @param url + * @param expectedStatus + * @param body + * @param contentType + * @return + * @throws IOException + */ + protected MockHttpServletResponse putRequest(String url, int expectedStatus, String body, String contentType) + throws IOException + { + return sendRequest(METHOD_PUT, url, expectedStatus, body, contentType); + } + /** * * @param method diff --git a/source/java/org/alfresco/repo/web/scripts/site/SiteServiceTest.java b/source/java/org/alfresco/repo/web/scripts/site/SiteServiceTest.java new file mode 100644 index 0000000000..e99bf62886 --- /dev/null +++ b/source/java/org/alfresco/repo/web/scripts/site/SiteServiceTest.java @@ -0,0 +1,140 @@ +/* + * Copyright (C) 2005-2007 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have recieved a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.repo.web.scripts.site; + +import org.alfresco.repo.web.scripts.BaseWebScriptTest; +import org.alfresco.util.GUID; +import org.json.JSONArray; +import org.json.JSONObject; +import org.springframework.mock.web.MockHttpServletResponse; + +/** + * Unit test to test site Web Script API + * + * @author Roy Wetherall + */ +public class SiteServiceTest extends BaseWebScriptTest +{ + private static final String URL_SITES = "/api/sites"; + private static final String URL_SITE = "/api/site/"; + + public void testCreateSite() throws Exception + { + String shortName = GUID.generate(); + + // Create a new site + JSONObject result = createSite("myPreset", shortName, "myTitle", "myDescription", true, 200); + assertEquals("myPreset", result.get("sitePreset")); + assertEquals(shortName, result.get("shortName")); + assertEquals("myTitle", result.get("title")); + assertEquals("myDescription", result.get("description")); + assertTrue(result.getBoolean("isPublic")); + + // Check for duplicate names + createSite("myPreset", shortName, "myTitle", "myDescription", true, 500); + } + + private JSONObject createSite(String sitePreset, String shortName, String title, String description, boolean isPublic, int expectedStatus) + throws Exception + { + JSONObject site = new JSONObject(); + site.put("sitePreset", sitePreset); + site.put("shortName", shortName); + site.put("title", title); + site.put("description", description); + site.put("isPublic", isPublic); + MockHttpServletResponse response = postRequest(URL_SITES, expectedStatus, site.toString(), "application/json"); + return new JSONObject(response.getContentAsString()); + } + + public void testGetSites() throws Exception + { + // == Test basic GET with no filters == + + MockHttpServletResponse response = getRequest(URL_SITES, 200); + JSONArray result = new JSONArray(response.getContentAsString()); + + // TODO formalise this test once i can be sure that i know what's already in the site store + // ie: .. i need to clean up after myself in this test + + System.out.println(response.getContentAsString()); + } + + public void testGetSite() throws Exception + { + // Get a site that doesn't exist + MockHttpServletResponse response = getRequest(URL_SITE + "somerandomshortname", 404); + + // Create a site and get it + String shortName = GUID.generate(); + JSONObject result = createSite("myPreset", shortName, "myTitle", "myDescription", true, 200); + response = getRequest(URL_SITE + shortName, 200); + + } + + public void testUpdateSite() throws Exception + { + // Create a site + String shortName = GUID.generate(); + JSONObject result = createSite("myPreset", shortName, "myTitle", "myDescription", true, 200); + + // Update the site + result.put("title", "abs123abc"); + result.put("description", "123abc123"); + result.put("isPublic", false); + MockHttpServletResponse response = putRequest(URL_SITE + shortName, 200, result.toString(), "application/json"); + result = new JSONObject(response.getContentAsString()); + assertEquals("abs123abc", result.get("title")); + assertEquals("123abc123", result.get("description")); + assertFalse(result.getBoolean("isPublic")); + + // Try and get the site and double check it's changed + response = getRequest(URL_SITE + shortName, 200); + result = new JSONObject(response.getContentAsString()); + assertEquals("abs123abc", result.get("title")); + assertEquals("123abc123", result.get("description")); + assertFalse(result.getBoolean("isPublic")); + } + + public void testDeleteSite() throws Exception + { + // Delete non-existant site + MockHttpServletResponse response = deleteRequest(URL_SITE + "somerandomshortname", 404); + + // Create a site + String shortName = GUID.generate(); + JSONObject result = createSite("myPreset", shortName, "myTitle", "myDescription", true, 200); + + // Get the site + response = getRequest(URL_SITE + shortName, 200); + + // Delete the site + response = deleteRequest(URL_SITE + shortName, 200); + + // Get the site + response = getRequest(URL_SITE + shortName, 404); + } + +} diff --git a/source/java/org/alfresco/repo/web/scripts/site/TestSiteService.java b/source/java/org/alfresco/repo/web/scripts/site/TestSiteService.java deleted file mode 100644 index 1957f4b9df..0000000000 --- a/source/java/org/alfresco/repo/web/scripts/site/TestSiteService.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright (C) 2005-2007 Alfresco Software Limited. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - - * As a special exception to the terms and conditions of version 2.0 of - * the GPL, you may redistribute this Program in connection with Free/Libre - * and Open Source Software ("FLOSS") applications as described in Alfresco's - * FLOSS exception. You should have recieved a copy of the text describing - * the FLOSS exception, and it is also available here: - * http://www.alfresco.com/legal/licensing" - */ -package org.alfresco.repo.web.scripts.site; - -import org.alfresco.repo.web.scripts.BaseWebScriptTest; -import org.alfresco.util.GUID; -import org.json.JSONArray; -import org.json.JSONObject; -import org.springframework.mock.web.MockHttpServletResponse; - -/** - * Unit test to test site Web Script API - * - * @author Roy Wetherall - */ -public class TestSiteService extends BaseWebScriptTest -{ - private static final String URL_SITES = "/api/sites"; - - public void testCreateSite() throws Exception - { - String shortName = GUID.generate(); - - // == Create a new site == - - JSONObject site = new JSONObject(); - site.put("sitePreset", "myPreset"); - site.put("shortName", shortName); - site.put("title", "myTitle"); - site.put("description", "myDescription"); - site.put("isPublic", true); - - MockHttpServletResponse response = postRequest(URL_SITES, 200, site.toString(), "application/json"); - - JSONObject result = new JSONObject(response.getContentAsString()); - - assertEquals("myPreset", result.get("sitePreset")); - assertEquals(shortName, result.get("shortName")); - assertEquals("myTitle", result.get("title")); - assertEquals("myDescription", result.get("description")); - assertTrue(result.getBoolean("isPublic")); - - // == Create a site with a duplicate shortName == - - response = postRequest(URL_SITES, 500, site.toString(), "application/json"); - result = new JSONObject(response.getContentAsString()); - } - - public void testGetSites() throws Exception - { - // == Test basic GET with no filters == - - MockHttpServletResponse response = getRequest(URL_SITES, 200); - JSONArray result = new JSONArray(response.getContentAsString()); - - // TODO formalise this test once i can be sure that i know what's already in the site store - // ie: .. i need to clean up after myself in this test - - System.out.println(response.getContentAsString()); - } - -}