ALF-9155 Links webscript unit tests for CRUD cases

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@29457 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Nick Burch
2011-07-29 12:40:57 +00:00
parent ee59e70a4b
commit 454cfe0a3c

View File

@@ -59,6 +59,8 @@ public class LinksRestApiTest extends BaseWebScriptTest
private static final String USER_ONE = "UserOneSecondToo"; private static final String USER_ONE = "UserOneSecondToo";
private static final String USER_TWO = "UserTwoSecondToo"; private static final String USER_TWO = "UserTwoSecondToo";
private static final String USERDETAILS_FIRSTNAME = "FirstName123";
private static final String USERDETAILS_LASTNAME = "LastName123";
private static final String SITE_SHORT_NAME_LINKS = "LinkSiteShortNameTest"; private static final String SITE_SHORT_NAME_LINKS = "LinkSiteShortNameTest";
private static final String LINK_TITLE_ONE = "TestLinkOne"; private static final String LINK_TITLE_ONE = "TestLinkOne";
@@ -68,11 +70,12 @@ public class LinksRestApiTest extends BaseWebScriptTest
private static final String LINK_URL_TWO = "http://alfresco.com/"; private static final String LINK_URL_TWO = "http://alfresco.com/";
private static final String LINK_URL_THREE = "http://share.alfresco.com/"; private static final String LINK_URL_THREE = "http://share.alfresco.com/";
private static final String URL_LINKS_BASE = "/api/links/site/" + SITE_SHORT_NAME_LINKS; private static final String URL_LINKS_BASE = "/api/links/site/" + SITE_SHORT_NAME_LINKS + "/links";
private static final String URL_LINKS_LIST = URL_LINKS_BASE + "/links"; private static final String URL_LINKS_LIST = URL_LINKS_BASE;
private static final String URL_LINKS_CREATE = URL_LINKS_BASE + "/posts"; private static final String URL_LINKS_CREATE = URL_LINKS_BASE + "/posts";
private static final String URL_LINKS_UPDATE = URL_LINKS_BASE + "/"; // plus path private static final String URL_LINKS_UPDATE = URL_LINKS_BASE + "/"; // plus path
private static final String URL_LINKS_DELETE = "/api/links/delete/site/" + SITE_SHORT_NAME_LINKS + "/links"; private static final String URL_LINKS_DELETE = "/api/links/delete/site/" + SITE_SHORT_NAME_LINKS + "/links";
private static final String URL_LINKS_FETCH = "/api/links/link/site/" + SITE_SHORT_NAME_LINKS + "/links/"; // plus path
// General methods // General methods
@@ -149,8 +152,8 @@ public class LinksRestApiTest extends BaseWebScriptTest
// create person properties // create person properties
PropertyMap personProps = new PropertyMap(); PropertyMap personProps = new PropertyMap();
personProps.put(ContentModel.PROP_USERNAME, userName); personProps.put(ContentModel.PROP_USERNAME, userName);
personProps.put(ContentModel.PROP_FIRSTNAME, "FirstName123"); personProps.put(ContentModel.PROP_FIRSTNAME, USERDETAILS_FIRSTNAME);
personProps.put(ContentModel.PROP_LASTNAME, "LastName123"); personProps.put(ContentModel.PROP_LASTNAME, USERDETAILS_LASTNAME);
personProps.put(ContentModel.PROP_EMAIL, "FirstName123.LastName123@email.com"); personProps.put(ContentModel.PROP_EMAIL, "FirstName123.LastName123@email.com");
personProps.put(ContentModel.PROP_JOBTITLE, "JobTitle123"); personProps.put(ContentModel.PROP_JOBTITLE, "JobTitle123");
personProps.put(ContentModel.PROP_JOBTITLE, "Organisation123"); personProps.put(ContentModel.PROP_JOBTITLE, "Organisation123");
@@ -191,11 +194,14 @@ public class LinksRestApiTest extends BaseWebScriptTest
private JSONObject getLink(String name, int expectedStatus) throws Exception private JSONObject getLink(String name, int expectedStatus) throws Exception
{ {
// TODO Response response = sendRequest(new GetRequest(URL_LINKS_FETCH + name), expectedStatus);
Response response = sendRequest(new GetRequest(URL_LINKS_LIST + name), expectedStatus);
if (expectedStatus == Status.STATUS_OK) if (expectedStatus == Status.STATUS_OK)
{ {
JSONObject result = new JSONObject(response.getContentAsString()); JSONObject result = new JSONObject(response.getContentAsString());
if(result.has("item"))
{
return result.getJSONObject("item");
}
return result; return result;
} }
else else
@@ -215,13 +221,13 @@ public class LinksRestApiTest extends BaseWebScriptTest
json.put("site", SITE_SHORT_NAME_LINKS); json.put("site", SITE_SHORT_NAME_LINKS);
json.put("title", title); json.put("title", title);
json.put("description", description); json.put("description", description);
json.put("url", "url"); json.put("url", url);
json.put("tags", ""); json.put("tags", "");
if(internal) if(internal)
{ {
json.put("internal", "true"); json.put("internal", "true");
} }
json.put("page", "links-view"); json.put("page", "links-view"); // TODO Is this really needed?
Response response = sendRequest(new PostRequest(URL_LINKS_CREATE, json.toString(), "application/json"), expectedStatus); Response response = sendRequest(new PostRequest(URL_LINKS_CREATE, json.toString(), "application/json"), expectedStatus);
if (expectedStatus == Status.STATUS_OK) if (expectedStatus == Status.STATUS_OK)
@@ -241,26 +247,18 @@ public class LinksRestApiTest extends BaseWebScriptTest
/** /**
* Updates the link with the new details * Updates the link with the new details
* TODO
*/ */
private JSONObject updateLink(String name, String title, String description, String url, private JSONObject updateLink(String name, String title, String description, String url,
boolean internal, int expectedStatus) throws Exception boolean internal, int expectedStatus) throws Exception
{ {
String date = "2011/06/28"; // A Tuesday
String start = "11:30";
String end = "13:30";
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("desc", description); json.put("site", SITE_SHORT_NAME_LINKS);
json.put("from", date); json.put("title", title);
json.put("to", date); json.put("description", description);
// json.put("fromdate", "Tuesday, 30 June 2011"); // Not needed json.put("url", url);
// json.put("todate", "Tuesday, 30 June 2011"); // Not needed
json.put("start", start);
json.put("end", end);
json.put("tags", ""); json.put("tags", "");
json.put("docfolder", ""); json.put("internal", Boolean.toString(internal).toLowerCase());
json.put("page", "calendar"); json.put("page", "links-view"); // TODO Is this really needed?
Response response = sendRequest(new PutRequest(URL_LINKS_UPDATE + name, json.toString(), "application/json"), expectedStatus); Response response = sendRequest(new PutRequest(URL_LINKS_UPDATE + name, json.toString(), "application/json"), expectedStatus);
if (expectedStatus == Status.STATUS_OK) if (expectedStatus == Status.STATUS_OK)
@@ -278,6 +276,29 @@ public class LinksRestApiTest extends BaseWebScriptTest
} }
} }
/**
* Deletes the link
*/
private JSONObject deleteLink(String name, int expectedStatus) throws Exception
{
JSONArray items = new JSONArray();
items.put(name);
JSONObject json = new JSONObject();
json.put("items", items);
Response response = sendRequest(new PostRequest(URL_LINKS_DELETE, json.toString(), "application/json"), expectedStatus);
if (expectedStatus == Status.STATUS_OK)
{
JSONObject result = new JSONObject(response.getContentAsString());
return result;
}
else
{
return null;
}
}
/** /**
* Monkeys with the created date on a link * Monkeys with the created date on a link
*/ */
@@ -326,100 +347,134 @@ public class LinksRestApiTest extends BaseWebScriptTest
public void testCreateEditDeleteEntry() throws Exception public void testCreateEditDeleteEntry() throws Exception
{ {
JSONObject link; JSONObject link;
JSONObject author;
JSONObject permissions;
String name; String name;
if(1!=0) { return; } // TODO Finish
// None to start with
link = getLinks(null, null, null);
assertEquals("Incorrect JSON: " + link.toString(), true, link.has("total"));
assertEquals(0, link.getInt("total"));
// Won't be there to start with // Won't be there to start with
link = getLink(LINK_TITLE_ONE, Status.STATUS_OK); link = getLink(LINK_TITLE_ONE, Status.STATUS_NOT_FOUND);
assertEquals(true, link.has("error"));
// Create // Create
// (We don't get much info back)
link = createLink(LINK_TITLE_ONE, "Thing 1", LINK_URL_ONE, false, Status.STATUS_OK); link = createLink(LINK_TITLE_ONE, "Thing 1", LINK_URL_ONE, false, Status.STATUS_OK);
assertEquals("Incorrect JSON: " + link.toString(), true, link.has("name"));
name = getNameFromLink(link); name = getNameFromLink(link);
assertEquals(LINK_TITLE_ONE, link.getString("name")); assertEquals(name, link.getString("name"));
assertEquals("Where", link.getString("where")); assertEquals(name, link.getString("message"));
assertEquals("Thing", link.getString("desc"));
assertEquals("2011-06-29", link.getString("from")); // Different format!
assertEquals("2011-06-29", link.getString("to")); // Different format!
assertEquals("12:00", link.getString("start"));
assertEquals("13:00", link.getString("end"));
assertEquals("false", link.getString("allday"));
// Fetch // Fetch
link = getLink(name, Status.STATUS_OK); link = getLink(name, Status.STATUS_OK);
assertEquals("Error found " + link.toString(), false, link.has("error")); assertEquals("Error found " + link.toString(), false, link.has("error"));
assertEquals(LINK_TITLE_ONE, link.getString("what")); assertEquals(LINK_TITLE_ONE, link.getString("title"));
assertEquals(name, link.getString("name")); assertEquals("Thing 1", link.getString("description"));
assertEquals("Where", link.getString("location")); // Not where... assertEquals(LINK_URL_ONE, link.getString("url"));
assertEquals("Thing", link.getString("description")); // Not desc... assertEquals(false, link.getBoolean("internal"));
assertEquals(0, link.getJSONArray("tags").length());
assertEquals("false", link.getString("isoutlook")); assertEquals(true, link.has("author"));
assertEquals("6/29/2011", link.getString("from")); author = link.getJSONObject("author");
assertEquals("6/29/2011", link.getString("to")); assertEquals(USER_ONE, author.getString("username"));
assertEquals("12:00", link.getString("start")); assertEquals(USERDETAILS_FIRSTNAME, author.getString("firstName"));
assertEquals("13:00", link.getString("end")); assertEquals(USERDETAILS_LASTNAME, author.getString("lastName"));
assertEquals("false", link.getString("allday"));
// Check the new style dates too // Check the permissions
// assertEquals("2011-06-29T12:00:00Z", entry.getJSONObject("startAt").get("iso8601")); // TODO Needs TZ going in assertEquals(true, link.has("permissions"));
assertEquals("6/29/2011", link.getJSONObject("startAt").get("legacyDate")); permissions = link.getJSONObject("permissions");
assertEquals("12:00", link.getJSONObject("startAt").get("legacyTime")); assertEquals(true, permissions.getBoolean("edit"));
// assertEquals("2011-06-29T13:00:00Z", entry.getJSONObject("endAt").get("iso8601")); // TODO Needs TZ going in assertEquals(true, permissions.getBoolean("delete"));
assertEquals("6/29/2011", link.getJSONObject("endAt").get("legacyDate"));
assertEquals("13:00", link.getJSONObject("endAt").get("legacyTime")); // Check the noderef, comments url, created on
// TODO
// "commentsUrl": "/node/workspace\/SpacesStore\/7a8ea18e-8ff0-4337-b5af-b732d9e8d6e9/comments",
// "nodeRef": "workspace://SpacesStore/7a8ea18e-8ff0-4337-b5af-b732d9e8d6e9",
// "createdOn": "Jul 28 2011 17:23:20 GMT+0100 (BST)",
// Edit // Edit
link = updateLink(name, LINK_TITLE_ONE, "More Where 1", LINK_URL_ONE, false, Status.STATUS_OK); // We should get a simple message
assertEquals("Error found " + link.toString(), false, link.has("error")); link = updateLink(name, LINK_TITLE_ONE, "More Thing 1", LINK_URL_ONE, true, Status.STATUS_OK);
assertEquals(LINK_TITLE_ONE, link.getString("summary")); assertEquals(
assertEquals("More Where", link.getString("location")); "Incorrect JSON: " + link.toString(),
assertEquals("More Thing", link.getString("description")); true, link.has("message")
);
assertEquals(
"Incorrect JSON: " + link.toString(),
true, link.getString("message").contains("updated")
);
// No from/to/start/end, does dtstart and dtend instead
assertEquals("2011-06-28T11:30", link.getString("dtstart"));
assertEquals("2011-06-28T13:30", link.getString("dtend"));
assertEquals("false", link.getString("allday"));
// No isoutlook on create/edit
// Fetch // Fetch
link = getLink(name, Status.STATUS_OK); link = getLink(name, Status.STATUS_OK);
assertEquals("Error found " + link.toString(), false, link.has("error")); assertEquals("Error found " + link.toString(), false, link.has("error"));
assertEquals(LINK_TITLE_ONE, link.getString("what")); assertEquals(LINK_TITLE_ONE, link.getString("title"));
assertEquals(name, link.getString("name")); assertEquals("More Thing 1", link.getString("description"));
assertEquals("More Where", link.getString("location")); // Not where... assertEquals(LINK_URL_ONE, link.getString("url"));
assertEquals("More Thing", link.getString("description")); assertEquals(true, link.getBoolean("internal"));
assertEquals(0, link.getJSONArray("tags").length());
assertEquals("false", link.getString("isoutlook")); assertEquals(true, link.has("author"));
assertEquals("6/28/2011", link.getString("from")); author = link.getJSONObject("author");
assertEquals("6/28/2011", link.getString("to")); assertEquals(USER_ONE, author.getString("username"));
assertEquals("11:30", link.getString("start")); assertEquals(USERDETAILS_FIRSTNAME, author.getString("firstName"));
assertEquals("13:30", link.getString("end")); assertEquals(USERDETAILS_LASTNAME, author.getString("lastName"));
assertEquals("false", link.getString("allday"));
// Fetch as a different user, permissions different
this.authenticationComponent.setCurrentUser(USER_TWO);
link = getLink(name, Status.STATUS_OK);
// Check the basics
assertEquals(LINK_TITLE_ONE, link.getString("title"));
assertEquals("More Thing 1", link.getString("description"));
assertEquals(LINK_URL_ONE, link.getString("url"));
assertEquals(true, link.getBoolean("internal"));
assertEquals(0, link.getJSONArray("tags").length());
// Different user in the site, can edit but not delete
assertEquals(true, link.has("permissions"));
permissions = link.getJSONObject("permissions");
assertEquals(true, permissions.getBoolean("edit"));
assertEquals(false, permissions.getBoolean("delete"));
this.authenticationComponent.setCurrentUser(USER_ONE);
// Delete // Delete
sendRequest(new DeleteRequest(URL_LINKS_DELETE + name), Status.STATUS_NO_CONTENT); link = deleteLink(name, Status.STATUS_OK);
assertEquals(
"Incorrect JSON: " + link.toString(),
true, link.has("message")
);
assertEquals(
"Incorrect JSON: " + link.toString(),
true, link.getString("message").contains("deleted")
);
// Fetch, will have gone // Fetch, will have gone
link = getLink(LINK_TITLE_ONE, Status.STATUS_OK); link = getLink(name, Status.STATUS_NOT_FOUND);
assertEquals(true, link.has("error"));
// Can't delete again // Can't delete again
sendRequest(new DeleteRequest(URL_LINKS_DELETE + name), Status.STATUS_NOT_FOUND); deleteLink(name, Status.STATUS_NOT_FOUND);
// Can't edit it when it's deleted // Can't edit it when it's deleted
sendRequest(new PutRequest(URL_LINKS_UPDATE + name, "{}", "application/json"), Status.STATUS_OK); sendRequest(new PutRequest(URL_LINKS_UPDATE + name, "{}", "application/json"), Status.STATUS_NOT_FOUND);
assertEquals(true, link.has("error"));
} }
/** /**