Site Service: GET and POST methods implemented on Site Membership REST API

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@9042 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Roy Wetherall
2008-05-08 17:00:35 +00:00
parent 7f192494de
commit 04024e0e8b
14 changed files with 216 additions and 27 deletions

View File

@@ -0,0 +1,13 @@
<#macro membershipJSON site role person>
{
"role" : "${role}",
"person":
{
"userName" : "${person.properties.userName}",
"url" : "${url.serviceContext}/api/people/${person.properties.userName}",
"firstName" : "${person.properties.firstName}",
"lastName" : "${person.properties.lastName}"
},
"url" : "${url.serviceContext}/api/sites/${site.shortName}/memberships/${person.properties.userName}"
}
</#macro>

View File

@@ -0,0 +1,8 @@
<webscript>
<shortname>Memberships</shortname>
<description>Get a colleciton of a sites memberships.</description>
<url>/api/sites/{shortname}/memberships?nf={namefilter?}&amp;rf={rolefilter?}&amp;size={pagesize?}&amp;pos={position?}</url>
<format default="json"/>
<authentication>guest</authentication>
<transaction>required</transaction>
</webscript>

View File

@@ -0,0 +1,21 @@
// Get the site id
var shortName = url.extension.split("/")[0];
var site = siteService.getSite(shortName);
// TODO get the filters
// Get all the memeberships
var memberships = site.listMembers(null, null);
// Get a list of all the users resolved to person nodes
var peopleList = Array();
for (userName in memberships)
{
var person = people.getPerson(userName);
peopleList[userName] = person;
}
// Pass the information to the template
model.site = site;
model.memberships = memberships;
model.people = peopleList;

View File

@@ -0,0 +1,8 @@
<#import "membership.lib.ftl" as membershipLib/>
[
<#assign userNames = memberships?keys />
<#list userNames as userName>
<@membershipLib.membershipJSON site=site role=memberships[userName] person=people[userName]/>
<#if userName_has_next>,</#if>
</#list>
]

View File

@@ -0,0 +1,8 @@
<webscript>
<shortname>Memberships</shortname>
<description>Adds a new membership to the site</description>
<url>/api/sites/{shortname}/memberships</url>
<format default="json"/>
<authentication>guest</authentication>
<transaction>required</transaction>
</webscript>

View File

@@ -0,0 +1,15 @@
// Get the site
var shortName = url.extension.split("/")[0];
var site = siteService.getSite(shortName);
// Get the role
var role = json.get("role");
var userName = json.getJSONObject("person").get("userName");
// Set the membership details
site.setMembership(userName, role);
// Pass the details to the template
model.site = site;
model.role = role;
model.person = people.getPerson(userName);

View File

@@ -0,0 +1,2 @@
<#import "membership.lib.ftl" as membershipLib/>
<@membershipLib.membershipJSON site=site role=role person=person/>

View File

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

View File

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

View File

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

View File

@@ -1,7 +1,7 @@
<webscript>
<shortname>Sites</shortname>
<description>Get a colleciton of the sites in the repository. The collection can be filtered by name and/or site preset.</description>
<url>/api/sites?nf={namefilter?}&amp;spf={sitepresetfilter?}</url>
<url>/api/sites?nf={namefilter?}&amp;spf={sitepresetfilter?}&amp;size={pagesize?}&amp;pos={position?}</url>
<format default="json"/>
<authentication>guest</authentication>
<transaction>required</transaction>

View File

@@ -1,12 +1,33 @@
// Get the details of the site
var shortName = json.get("shortName");
var sitePreset = json.get("sitePreset");
var title = json.get("title");
var description = json.get("description");
var isPublic = json.getBoolean("isPublic");
function main()
{
// Get the details of the site
var shortName = json.get("shortName");
if (shortName == null || shortName.length == 0)
{
status.code = 400;
status.message = "Short name missing when creating site.";
status.redirect = true;
return;
}
// Create the site
var site = siteService.createSite(sitePreset, shortName, title, description, isPublic);
var sitePreset = json.get("sitePreset");
if (shortName == null || shortName.length == 0)
{
status.code = 400;
status.message = "Site preset missing when creating site.";
status.redirect = true;
return;
}
// Put the created site into the model
model["site"] = site;
var title = json.get("title");
var description = json.get("description");
var isPublic = json.getBoolean("isPublic");
// Create the site
var site = siteService.createSite(sitePreset, shortName, title, description, isPublic);
// Put the created site into the model
model.site = site;
}
main();

View File

@@ -135,7 +135,7 @@ public abstract class BaseWebScriptTest extends TestCase
{
System.out.println(response.getContentAsString());
}
fail("The expected status code (" + expectedStatus + ") was not returned.");
fail("Expected status code " + expectedStatus + " , " + response.getStatus() + " was returned.");
}
return response;
}

View File

@@ -27,8 +27,14 @@ package org.alfresco.repo.web.scripts.site;
import java.util.ArrayList;
import java.util.List;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.repo.site.SiteModel;
import org.alfresco.repo.web.scripts.BaseWebScriptTest;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.util.GUID;
import org.alfresco.util.PropertyMap;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.mock.web.MockHttpServletResponse;
@@ -40,20 +46,64 @@ import org.springframework.mock.web.MockHttpServletResponse;
*/
public class SiteServiceTest extends BaseWebScriptTest
{
private AuthenticationService authenticationService;
private AuthenticationComponent authenticationComponent;
private PersonService personService;
private static final String USER_ONE = "SiteTestOne";
private static final String USER_TWO = "SiteTestTwo";
private static final String USER_THREE = "SiteTestThree";
private static final String URL_SITES = "/api/sites";
private static final String URL_SITE = "/api/site/";
private static final String URL_MEMBERSHIPS = "/memberships";
private List<String> createdSites = new ArrayList<String>(5);
@Override
protected void setUp() throws Exception
{
super.setUp();
this.authenticationService = (AuthenticationService)getServer().getApplicationContext().getBean("AuthenticationService");
this.authenticationComponent = (AuthenticationComponent)getServer().getApplicationContext().getBean("authenticationComponent");
this.personService = (PersonService)getServer().getApplicationContext().getBean("PersonService");
// Create users
createUser(USER_ONE);
createUser(USER_TWO);
createUser(USER_THREE);
// Do tests as user one
this.authenticationComponent.setCurrentUser(USER_ONE);
}
private void createUser(String userName)
{
if (this.authenticationService.authenticationExists(userName) == false)
{
this.authenticationService.createAuthentication(userName, "PWD".toCharArray());
PropertyMap ppOne = new PropertyMap(4);
ppOne.put(ContentModel.PROP_USERNAME, userName);
ppOne.put(ContentModel.PROP_FIRSTNAME, "firstName");
ppOne.put(ContentModel.PROP_LASTNAME, "lastName");
ppOne.put(ContentModel.PROP_EMAIL, "email@email.com");
ppOne.put(ContentModel.PROP_JOBTITLE, "jobTitle");
this.personService.createPerson(ppOne);
}
}
@Override
protected void tearDown() throws Exception
{
super.tearDown();
this.authenticationComponent.setCurrentUser("admin");
// Tidy-up any site's create during the execution of the test
for (String shortName : this.createdSites)
{
deleteRequest(URL_SITE + shortName, 0);
deleteRequest(URL_SITES + "/" + shortName, 0);
}
// Clear the list
@@ -106,12 +156,12 @@ public class SiteServiceTest extends BaseWebScriptTest
public void testGetSite() throws Exception
{
// Get a site that doesn't exist
MockHttpServletResponse response = getRequest(URL_SITE + "somerandomshortname", 404);
MockHttpServletResponse response = getRequest(URL_SITES + "/" + "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);
response = getRequest(URL_SITES + "/" + shortName, 200);
}
@@ -125,14 +175,14 @@ public class SiteServiceTest extends BaseWebScriptTest
result.put("title", "abs123abc");
result.put("description", "123abc123");
result.put("isPublic", false);
MockHttpServletResponse response = putRequest(URL_SITE + shortName, 200, result.toString(), "application/json");
MockHttpServletResponse response = putRequest(URL_SITES + "/" + 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);
response = getRequest(URL_SITES + "/" + shortName, 200);
result = new JSONObject(response.getContentAsString());
assertEquals("abs123abc", result.get("title"));
assertEquals("123abc123", result.get("description"));
@@ -142,20 +192,63 @@ public class SiteServiceTest extends BaseWebScriptTest
public void testDeleteSite() throws Exception
{
// Delete non-existant site
MockHttpServletResponse response = deleteRequest(URL_SITE + "somerandomshortname", 404);
MockHttpServletResponse response = deleteRequest(URL_SITES + "/" + "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);
response = getRequest(URL_SITES + "/" + shortName, 200);
// Delete the site
response = deleteRequest(URL_SITE + shortName, 200);
response = deleteRequest(URL_SITES + "/" + shortName, 200);
// Get the site
response = getRequest(URL_SITE + shortName, 404);
response = getRequest(URL_SITES + "/" + shortName, 404);
}
public void testGetMemeberships() throws Exception
{
// Create a site
String shortName = GUID.generate();
createSite("myPreset", shortName, "myTitle", "myDescription", true, 200);
// Check the memberships
MockHttpServletResponse response = getRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS, 200);
JSONArray result = new JSONArray(response.getContentAsString());
assertNotNull(result);
assertEquals(1, result.length());
JSONObject membership = result.getJSONObject(0);
assertEquals(SiteModel.SITE_MANAGER, membership.get("role"));
assertEquals(USER_ONE, membership.getJSONObject("person").get("userName"));
}
public void testPostMemberships() throws Exception
{
// Create a site
String shortName = GUID.generate();
createSite("myPreset", shortName, "myTitle", "myDescription", true, 200);
// Build the JSON membership object
JSONObject membership = new JSONObject();
membership.put("role", SiteModel.SITE_CONSUMER);
JSONObject person = new JSONObject();
person.put("userName", USER_TWO);
membership.put("person", person);
// Post the memebership
MockHttpServletResponse response = postRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS, 200, membership.toString(), "application/json");
JSONObject result = new JSONObject(response.getContentAsString());
// Check the result
assertEquals(SiteModel.SITE_CONSUMER, membership.get("role"));
assertEquals(USER_TWO, membership.getJSONObject("person").get("userName"));
// Get the membership list
response = getRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS, 200);
JSONArray result2 = new JSONArray(response.getContentAsString());
assertNotNull(result2);
assertEquals(2, result2.length());
}
}