use of org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest in project alfresco-remote-api by Alfresco.
the class BlogServiceTest method testGetAll.
public void testGetAll() throws Exception {
String url = URL_BLOG_POSTS;
Response response = sendRequest(new GetRequest(url), 200);
JSONObject result = new JSONObject(response.getContentAsString());
JSONObject blog;
// We shouldn't have any posts at this point
assertEquals(0, this.posts.size());
assertEquals(0, this.drafts.size());
assertEquals(0, result.getInt("total"));
assertEquals(0, result.getInt("startIndex"));
assertEquals(0, result.getInt("itemCount"));
assertEquals(0, result.getJSONArray("items").length());
// Check that the permissions are correct
JSONObject metadata = result.getJSONObject("metadata");
JSONObject perms = metadata.getJSONObject("blogPermissions");
assertEquals(false, metadata.getBoolean("externalBlogConfig"));
// No container yet
assertEquals(false, perms.getBoolean("delete"));
assertEquals(true, perms.getBoolean("edit"));
assertEquals(true, perms.getBoolean("create"));
// Create a draft and a full post
String TITLE_1 = "Published";
String TITLE_2 = "Draft";
String TITLE_3 = "Another Published";
createPost(TITLE_1, "Stuff", null, false, Status.STATUS_OK);
createPost(TITLE_2, "Draft Stuff", null, true, Status.STATUS_OK);
// Check now
response = sendRequest(new GetRequest(url), 200);
result = new JSONObject(response.getContentAsString());
assertEquals(2, result.getInt("total"));
assertEquals(0, result.getInt("startIndex"));
assertEquals(2, result.getInt("itemCount"));
assertEquals(2, result.getJSONArray("items").length());
// Check the core permissions
metadata = result.getJSONObject("metadata");
perms = metadata.getJSONObject("blogPermissions");
assertEquals(false, metadata.getBoolean("externalBlogConfig"));
// On the container itself
assertEquals(true, perms.getBoolean("delete"));
assertEquals(true, perms.getBoolean("edit"));
assertEquals(true, perms.getBoolean("create"));
// Check each one in detail, they'll come back Published
// then draft (newest first within that)
blog = result.getJSONArray("items").getJSONObject(0);
assertEquals(TITLE_1, blog.get("title"));
assertEquals(false, blog.getBoolean("isDraft"));
perms = blog.getJSONObject("permissions");
assertEquals(true, perms.getBoolean("delete"));
assertEquals(true, perms.getBoolean("edit"));
blog = result.getJSONArray("items").getJSONObject(1);
assertEquals(TITLE_2, blog.get("title"));
assertEquals(true, blog.getBoolean("isDraft"));
perms = blog.getJSONObject("permissions");
assertEquals(true, perms.getBoolean("delete"));
assertEquals(true, perms.getBoolean("edit"));
// Add a third post
createPost(TITLE_3, "Still Stuff", null, false, Status.STATUS_OK);
response = sendRequest(new GetRequest(url), 200);
result = new JSONObject(response.getContentAsString());
assertEquals(3, result.getInt("total"));
assertEquals(0, result.getInt("startIndex"));
assertEquals(3, result.getInt("itemCount"));
assertEquals(3, result.getJSONArray("items").length());
// Published then draft, newest first
blog = result.getJSONArray("items").getJSONObject(0);
assertEquals(TITLE_3, blog.get("title"));
blog = result.getJSONArray("items").getJSONObject(1);
assertEquals(TITLE_1, blog.get("title"));
blog = result.getJSONArray("items").getJSONObject(2);
assertEquals(TITLE_2, blog.get("title"));
// Ensure that paging behaves properly
response = sendRequest(new GetRequest(url + "?pageSize=2&startIndex=0"), 200);
result = new JSONObject(response.getContentAsString());
assertEquals(3, result.getInt("total"));
assertEquals(0, result.getInt("startIndex"));
assertEquals(2, result.getInt("itemCount"));
assertEquals(2, result.getJSONArray("items").length());
assertEquals(TITLE_3, result.getJSONArray("items").getJSONObject(0).get("title"));
assertEquals(TITLE_1, result.getJSONArray("items").getJSONObject(1).get("title"));
response = sendRequest(new GetRequest(url + "?pageSize=2&startIndex=1"), 200);
result = new JSONObject(response.getContentAsString());
assertEquals(3, result.getInt("total"));
assertEquals(1, result.getInt("startIndex"));
assertEquals(2, result.getInt("itemCount"));
assertEquals(2, result.getJSONArray("items").length());
assertEquals(TITLE_1, result.getJSONArray("items").getJSONObject(0).get("title"));
assertEquals(TITLE_2, result.getJSONArray("items").getJSONObject(1).get("title"));
response = sendRequest(new GetRequest(url + "?pageSize=2&startIndex=2"), 200);
result = new JSONObject(response.getContentAsString());
assertEquals(3, result.getInt("total"));
assertEquals(2, result.getInt("startIndex"));
assertEquals(1, result.getInt("itemCount"));
assertEquals(1, result.getJSONArray("items").length());
assertEquals(TITLE_2, result.getJSONArray("items").getJSONObject(0).get("title"));
// Switch user, check that permissions are correct
// (Drafts won't be seen)
this.authenticationComponent.setCurrentUser(USER_TWO);
response = sendRequest(new GetRequest(url), 200);
result = new JSONObject(response.getContentAsString());
assertEquals(2, result.getInt("total"));
assertEquals(0, result.getInt("startIndex"));
assertEquals(2, result.getInt("itemCount"));
assertEquals(2, result.getJSONArray("items").length());
blog = result.getJSONArray("items").getJSONObject(0);
assertEquals(TITLE_3, blog.get("title"));
assertEquals(false, blog.getBoolean("isDraft"));
perms = blog.getJSONObject("permissions");
assertEquals(false, perms.getBoolean("delete"));
assertEquals(true, perms.getBoolean("edit"));
blog = result.getJSONArray("items").getJSONObject(1);
assertEquals(TITLE_1, blog.get("title"));
assertEquals(false, blog.getBoolean("isDraft"));
perms = blog.getJSONObject("permissions");
assertEquals(false, perms.getBoolean("delete"));
assertEquals(true, perms.getBoolean("edit"));
}
use of org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest in project alfresco-remote-api by Alfresco.
the class BlogServiceTest method testCreateDraftPostWithTagsAndComment.
/**
* @since 4.0
*/
@Test
@Category({ LuceneTests.class, RedundantTests.class })
public void testCreateDraftPostWithTagsAndComment() throws Exception {
String[] tags = new String[] { "foo", "bar" };
String title = "test";
String content = "test";
JSONObject item = createPost(title, content, tags, true, 200);
// check that the values
assertEquals(title, item.get("title"));
assertEquals(content, item.get("content"));
assertEquals(true, item.get("isDraft"));
JSONArray reportedTags = (JSONArray) item.get("tags");
assertEquals("Tags size was wrong.", 2, reportedTags.length());
List<String> recoveredTagsList = Arrays.asList(new String[] { reportedTags.getString(0), reportedTags.getString(1) });
assertEquals("Tags were wrong.", Arrays.asList(tags), recoveredTagsList);
// comment on the blog post.
NodeRef blogPostNode = new NodeRef(item.getString("nodeRef"));
// Currently (mid-Swift dev) there is no Java CommentService, so we have to post a comment via the REST API.
String commentsPostUrl = "/api/node/" + blogPostNode.getStoreRef().getProtocol() + "/" + blogPostNode.getStoreRef().getIdentifier() + "/" + blogPostNode.getId() + "/comments";
String jsonToPost = new JSONStringer().object().key("title").value("Commented blog title").key("content").value("Some content.").endObject().toString();
Response response = sendRequest(new PostRequest(commentsPostUrl, jsonToPost, "application/json"), 200);
// check that other user doesn't have access to the draft
this.authenticationComponent.setCurrentUser(USER_TWO);
getPost(item.getString("name"), 404);
this.authenticationComponent.setCurrentUser(USER_ONE);
// Now we'll GET my-drafts to ensure that the post is there.
response = sendRequest(new GetRequest(URL_MY_DRAFT_BLOG_POSTS), 200);
JSONObject result = new JSONObject(response.getContentAsString());
// Ensure it reports the tag correctly on GET.
JSONArray items = result.getJSONArray("items");
JSONArray tagsArray = items.getJSONObject(0).getJSONArray("tags");
assertEquals("Wrong number of tags", 2, tagsArray.length());
assertEquals("Tag wrong", tags[0], tagsArray.getString(0));
assertEquals("Tag wrong", tags[1], tagsArray.getString(1));
// Ensure the comment count is accurate
assertEquals("Wrong comment count", 1, items.getJSONObject(0).getInt("commentCount"));
// and that there is content at the commentsURL.
String commentsUrl = "/api" + items.getJSONObject(0).getString("commentsUrl");
response = sendRequest(new GetRequest(commentsUrl), 200);
// Now get blog-post by tag.
// 1. No such tag
response = sendRequest(new GetRequest(URL_BLOG_POSTS + "?tag=NOSUCHTAG"), 200);
result = new JSONObject(response.getContentAsString());
assertEquals(0, result.getInt("total"));
// tag created above
response = sendRequest(new GetRequest(URL_BLOG_POSTS + "?tag=foo"), 200);
result = new JSONObject(response.getContentAsString());
assertEquals(1, result.getInt("total"));
// TODO More assertions on recovered node.
}
use of org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest in project alfresco-remote-api by Alfresco.
the class BlogServiceTest method testComments.
public void testComments() throws Exception {
JSONObject item = createPost("test", "test", null, false, 200);
String name = item.getString("name");
String nodeRef = item.getString("nodeRef");
JSONObject commentOne = createComment(nodeRef, "comment", "content", 200);
JSONObject commentTwo = createComment(nodeRef, "comment", "content", 200);
// fetch the comments
Response response = sendRequest(new GetRequest(getCommentsUrl(nodeRef)), 200);
JSONObject result = new JSONObject(response.getContentAsString());
assertEquals(2, result.getInt("total"));
// add another one
JSONObject commentThree = createComment(nodeRef, "comment", "content", 200);
response = sendRequest(new GetRequest(getCommentsUrl(nodeRef)), 200);
result = new JSONObject(response.getContentAsString());
assertEquals(3, result.getInt("total"));
// delete the last comment
response = sendRequest(new DeleteRequest(getCommentUrl(commentThree.getString("nodeRef"))), 200);
response = sendRequest(new GetRequest(getCommentsUrl(nodeRef)), 200);
result = new JSONObject(response.getContentAsString());
assertEquals(2, result.getInt("total"));
JSONObject commentTwoUpdated = updateComment(commentTwo.getString("nodeRef"), "new title", "new content", 200);
assertEquals("new title", commentTwoUpdated.getString("title"));
assertEquals("new content", commentTwoUpdated.getString("content"));
}
use of org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest in project alfresco-remote-api by Alfresco.
the class BlogServiceTest method testCreateDraftPost.
// Tests
public void testCreateDraftPost() throws Exception {
String title = "test";
String content = "test";
JSONObject item = createPost(title, content, null, true, 200);
// check that the values
assertEquals(title, item.get("title"));
assertEquals(content, item.get("content"));
assertEquals(true, item.get("isDraft"));
// check that other user doesn't have access to the draft
this.authenticationComponent.setCurrentUser(USER_TWO);
getPost(item.getString("name"), 404);
this.authenticationComponent.setCurrentUser(USER_ONE);
// Now we'll GET my-drafts to ensure that the post is there.
Response response = sendRequest(new GetRequest(URL_MY_DRAFT_BLOG_POSTS), 200);
JSONObject result = new JSONObject(response.getContentAsString());
assertTrue("Wrong number of posts", result.length() > 0);
}
use of org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest in project alfresco-remote-api by Alfresco.
the class BlogServiceTest method testCreatePublishedPost.
public void testCreatePublishedPost() throws Exception {
String title = "published";
String content = "content";
JSONObject item = createPost(title, content, null, false, 200);
final String postName = item.getString("name");
// check the values
assertEquals(title, item.get("title"));
assertEquals(content, item.get("content"));
assertEquals(false, item.get("isDraft"));
// check that user two has access to it as well
this.authenticationComponent.setCurrentUser(USER_TWO);
getPost(item.getString("name"), 200);
this.authenticationComponent.setCurrentUser(USER_ONE);
// Now we'll GET my-published to ensure that the post is there.
Response response = sendRequest(new GetRequest(URL_MY_PUBLISHED_BLOG_POSTS), 200);
JSONObject result = new JSONObject(response.getContentAsString());
// we should have posts.size + drafts.size together
assertEquals(this.posts.size() + this.drafts.size(), result.getInt("total"));
// Finally, we'll delete the blog-post to test the REST DELETE call.
response = sendRequest(new DeleteRequest(URL_BLOG_POST + postName), 200);
}
Aggregations