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
This commit is contained in:
Roy Wetherall
2008-05-01 16:48:30 +00:00
parent 060c5eb8a1
commit 7d32941e9c
15 changed files with 270 additions and 102 deletions

View File

@@ -0,0 +1,8 @@
<webscript>
<shortname>Site</shortname>
<description>Delete the details of a site.</description>
<url>/api/site/{shortname}</url>
<format default="json"/>
<authentication>guest</authentication>
<transaction>required</transaction>
</webscript>

View File

@@ -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;
}

View File

@@ -0,0 +1,8 @@
<webscript>
<shortname>Site</shortname>
<description>Get the details of a site.</description>
<url>/api/site/{shortname}</url>
<format default="json"/>
<authentication>guest</authentication>
<transaction>required</transaction>
</webscript>

View File

@@ -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;
}

View File

@@ -0,0 +1,2 @@
<#import "site.lib.ftl" as siteLib/>
<@siteLib.siteJSON site=site/>

View File

@@ -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")}
}
</#macro>

View File

@@ -0,0 +1,8 @@
<webscript>
<shortname>Site</shortname>
<description>Update the details of a site.</description>
<url>/api/site/{shortname}</url>
<format default="json"/>
<authentication>guest</authentication>
<transaction>required</transaction>
</webscript>

View File

@@ -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;
}

View File

@@ -0,0 +1,2 @@
<#import "site.lib.ftl" as siteLib/>
<@siteLib.siteJSON site=site/>

View File

@@ -1,14 +1,8 @@
<#assign first = true/> <#import "site.lib.ftl" as siteLib/>
[ [
<#list sites as site> <#list sites as site>
<#if first == true><#assign first = false/><#else>,</#if> <@siteLib.siteJSON site=site/>
{ <#if site_has_next>,</#if>
"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")}
}
</#list> </#list>
] ]

View File

@@ -1,8 +1,2 @@
{ <#import "site.lib.ftl" as siteLib/>
"url" : "${url.serviceContext}/api/sites/${site.shortName}", <@siteLib.siteJSON site=site/>
"sitePreset" : "${site.sitePreset}",
"shortName" : "${site.shortName}",
"title" : "${site.title}",
"description" : "${site.description}",
"isPublic" : ${site.isPublic?string("true", "false")}
}

View File

@@ -71,11 +71,27 @@ public abstract class BaseWebScriptTest extends TestCase
return sendRequest(METHOD_GET, url, expectedStatus, null, null); 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 * "POST" the url and check for the expected status code
* *
* @param url * @param url
* @param expectedStatus * @param expectedStatus
* @param body
* @param contentType
* @return * @return
* @throws IOException * @throws IOException
*/ */
@@ -85,6 +101,22 @@ public abstract class BaseWebScriptTest extends TestCase
return sendRequest(METHOD_POST, url, expectedStatus, body, contentType); 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 * @param method

View File

@@ -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);
}
}

View File

@@ -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());
}
}