Person Service - now all Web Script Unit Tests pass

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@9558 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Glen Johnson
2008-06-24 23:20:13 +00:00
parent 6234679ccb
commit 57f8a813c1
6 changed files with 64 additions and 45 deletions

View File

@@ -1,5 +1,5 @@
<webscript> <webscript>
<shortname>People</shortname> <shortname>Get People</shortname>
<description> <description>
Get collection of people stored in the repository. This can optionally be Get collection of people stored in the repository. This can optionally be
filtered according to some given filter query string filtered according to some given filter query string

View File

@@ -1,8 +1,8 @@
<webscript> <webscript>
<shortname>People</shortname> <shortname>Add Person</shortname>
<description>Adds a new person based on the details provided.</description> <description>Adds a new person based on the details provided.</description>
<url>/api/people</url> <url>/api/people</url>
<format default="json" /> <format default="json" />
<authentication>guest</authentication> <authentication>user</authentication>
<transaction>required</transaction> <transaction>required</transaction>
</webscript> </webscript>

View File

@@ -8,25 +8,23 @@ function main()
return; return;
} }
var title = json.get("title"); // Create the person with the supplied user name
var firstName = json.get("firstName");
var lastName = json.get("lastName");
var organisation = json.get("organisation");
var jobTitle = json.get("jobTitle");
var email = json.get("email");
var bio = json.get("bio");
var avatarUrl = json.get("avatarUrl");
// Create the person
var person = people.createPerson(userName); var person = people.createPerson(userName);
person.properties.title = title;
person.properties.firstName = firstName; // return error message if a person with that user name could not be created
person.properties.lastName = lastName; if (person === null)
person.properties.organisation = organisation; {
person.properties.jobTitle = jobTitle; status.setCode(status.STATUS_INTERNAL_SERVER_ERROR, "Person could not be created with user name: " + userName);
person.properties.email = email; return;
person.properties.bio = bio; }
person.properties.avatarUrl = avatarUrl;
// assign values to the person's properties
person.properties.title = json.get("title");
person.properties.firstName = json.get("firstName");
person.properties.lastName = json.get("lastName");
person.properties.organization = json.get("organisation");
person.properties.jobTitle = json.get("jobTitle");
person.properties.email = json.get("email");
person.save(); person.save();
// Put the created person into the model // Put the created person into the model

View File

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

View File

@@ -1,23 +1,37 @@
<#macro personJSON person> <#macro personJSON person>
{ {
"url" : "${url.serviceContext}/api/person/${person.userName}", "url" : "${url.serviceContext}/api/person/${person.properties.userName}",
"userName" : "${person.userName}", "userName" : "${person.properties.userName}",
"title" : "${person.title}", <#if person.properties.title??>
"firstName" : "${person.firstName}", "title" : "${person.properties.title}",
"lastName" : "${person.lastName}", <#else>
"organisation" : "${person.organisation}", "title" : undefined,
"jobTitle" : "${person.jobTitle}", </#if>
"email" : "${person.email}", "firstName" : "${person.properties.firstName}",
"bio" : "${person.bio}", "lastName" : "${person.properties.lastName}",
"avatarUrl" : "${person.avatarUrl}", <#if person.properties.organization??>
"organisation" : "${person.properties.organization}",
<#else>
"organisation" : undefined,
</#if>
<#if person.properties.jobtitle??>
"jobTitle" : "${person.properties.jobtitle}",
<#else>
"jobTitle" : undefined,
</#if>
<#if person.properties.email??>
"email" : "${person.properties.email}"
<#else>
"email" : undefined
</#if>
} }
</#macro> </#macro>
<#macro personSummaryJSON person> <#macro personSummaryJSON person>
{ {
"url" : "${url.serviceContext}/api/person/${person.userName}", "url" : "${url.serviceContext}/api/person/${person.properties.userName}",
"userName" : "${person.userName}", "userName" : "${person.properties.userName}",
"firstName" : "${person.firstName}", "firstName" : "${person.properties.firstName}",
"lastName" : "${person.lastName}" "lastName" : "${person.properties.lastName}"
} }
</#macro> </#macro>

View File

@@ -101,18 +101,20 @@ public class PersonServiceTest extends BaseWebScriptTest
protected void tearDown() throws Exception protected void tearDown() throws Exception
{ {
super.tearDown(); super.tearDown();
this.authenticationComponent.setCurrentUser("admin"); String adminUser = this.authenticationComponent.getSystemUserName();
this.authenticationComponent.setCurrentUser(adminUser);
/* /*
* TODO: glen.johnson at alfresco dot com - * TODO: glen.johnson at alfresco dot com -
* When DELETE /people/{userid} becomes a requirement and is subsequently implemented, * When DELETE /people/{userid} becomes a requirement and is subsequently implemented,
* include this section to tidy-up people's resources created during the execution of the test * include this section to tidy-up people's resources created during the execution of the test
* *
*/
for (String userName : this.createdPeople) for (String userName : this.createdPeople)
{ {
deleteRequest(URL_PEOPLE + "/" + userName, 0); // deleteRequest(URL_PEOPLE + "/" + userName, 0);
personService.deletePerson(userName);
} }
*/
// Clear the list // Clear the list
this.createdPeople.clear(); this.createdPeople.clear();
@@ -123,7 +125,7 @@ public class PersonServiceTest extends BaseWebScriptTest
String userName = RandomStringUtils.randomNumeric(6); String userName = RandomStringUtils.randomNumeric(6);
// Create a new site // Create a new site
JSONObject result = createPerson(userName, "myTitle", "myFirstName", "mylastName", "myOrganisation", JSONObject result = createPerson(userName, "myTitle", "myFirstName", "myLastName", "myOrganisation",
"myJobTitle", "firstName.lastName@email.com", "myBio", "images/avatar.jpg", 200); "myJobTitle", "firstName.lastName@email.com", "myBio", "images/avatar.jpg", 200);
assertEquals(userName, result.get("userName")); assertEquals(userName, result.get("userName"));
assertEquals("myTitle", result.get("title")); assertEquals("myTitle", result.get("title"));
@@ -132,8 +134,6 @@ public class PersonServiceTest extends BaseWebScriptTest
assertEquals("myOrganisation", result.get("organisation")); assertEquals("myOrganisation", result.get("organisation"));
assertEquals("myJobTitle", result.get("jobTitle")); assertEquals("myJobTitle", result.get("jobTitle"));
assertEquals("firstName.lastName@email.com", result.get("email")); assertEquals("firstName.lastName@email.com", result.get("email"));
assertEquals("myBio", result.get("bio"));
assertEquals("images/avatar.jpg", result.get("avatarUrl"));
// Check for duplicate names // Check for duplicate names
createPerson(userName, "myTitle", "myFirstName", "mylastName", "myOrganisation", createPerson(userName, "myTitle", "myFirstName", "mylastName", "myOrganisation",
@@ -144,6 +144,11 @@ public class PersonServiceTest extends BaseWebScriptTest
String organisation, String jobTitle, String email, String bio, String avatarUrl, int expectedStatus) String organisation, String jobTitle, String email, String bio, String avatarUrl, int expectedStatus)
throws Exception throws Exception
{ {
// switch to admin user to create a person
String currentUser = this.authenticationComponent.getCurrentUserName();
String adminUser = this.authenticationComponent.getSystemUserName();
this.authenticationComponent.setCurrentUser(adminUser);
JSONObject person = new JSONObject(); JSONObject person = new JSONObject();
person.put("userName", userName); person.put("userName", userName);
person.put("title", title); person.put("title", title);
@@ -152,11 +157,13 @@ public class PersonServiceTest extends BaseWebScriptTest
person.put("organisation", organisation); person.put("organisation", organisation);
person.put("jobTitle", jobTitle); person.put("jobTitle", jobTitle);
person.put("email", email); person.put("email", email);
person.put("bio", bio);
person.put("avatarUrl", avatarUrl);
MockHttpServletResponse response = postRequest(URL_PEOPLE, expectedStatus, person.toString(), "application/json"); MockHttpServletResponse response = postRequest(URL_PEOPLE, expectedStatus, person.toString(), "application/json");
this.createdPeople.add(userName); this.createdPeople.add(userName);
// switch back to non-admin user
this.authenticationComponent.setCurrentUser(currentUser);
return new JSONObject(response.getContentAsString()); return new JSONObject(response.getContentAsString());
} }