Merged DEV/FORMS-REFACTOR branch to HEAD

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@14000 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Gavin Cornwell
2009-04-18 22:42:15 +00:00
parent 695f606034
commit c379051889
14 changed files with 1044 additions and 879 deletions

View File

@@ -1,205 +1,219 @@
/*
* Copyright (C) 2005-2009 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 received 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.forms;
import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.model.Repository;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.web.scripts.BaseWebScriptTest;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.model.FileInfo;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.GUID;
import org.alfresco.web.scripts.TestWebScriptServer.GetRequest;
import org.alfresco.web.scripts.TestWebScriptServer.Response;
public abstract class AbstractTestFormRestApi extends BaseWebScriptTest
{
protected static final String TEST_FORM_DESCRIPTION = "Test form description";
protected static final String TEST_FORM_TITLE = "Test form title";
protected String referencingNodeUrl;
protected String containingNodeUrl;
protected NodeRef referencingDocNodeRef;
protected Map<QName, Serializable> refNodePropertiesAfterCreation;
protected NodeRef associatedDoc_A;
protected NodeRef associatedDoc_B;
protected NodeRef associatedDoc_C;
protected NodeRef associatedDoc_D;
protected NodeRef associatedDoc_E;
protected NodeRef childDoc_A;
protected NodeRef childDoc_B;
protected NodeRef childDoc_C;
protected NodeRef childDoc_D;
protected NodeRef childDoc_E;
protected NodeRef testFolderNodeRef;
protected NodeService nodeService;
private FileFolderService fileFolderService;
private ContentService contentService;
private Repository repositoryHelper;
protected NodeRef containerNodeRef;
@Override
protected void setUp() throws Exception
{
super.setUp();
this.fileFolderService = (FileFolderService) getServer().getApplicationContext().getBean(
"FileFolderService");
this.contentService = (ContentService) getServer().getApplicationContext().getBean(
"ContentService");
this.repositoryHelper = (Repository) getServer().getApplicationContext().getBean(
"repositoryHelper");
this.nodeService = (NodeService) getServer().getApplicationContext().getBean("NodeService");
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getSystemUserName());
NodeRef companyHomeNodeRef = this.repositoryHelper.getCompanyHome();
String guid = GUID.generate();
// Create a test file (not a folder)
FileInfo referencingDoc = this.fileFolderService.create(companyHomeNodeRef,
"referencingDoc" + guid + ".txt", ContentModel.TYPE_CONTENT);
referencingDocNodeRef = referencingDoc.getNodeRef();
// Add an aspect.
Map<QName, Serializable> aspectProps = new HashMap<QName, Serializable>(2);
aspectProps.put(ContentModel.PROP_TITLE, TEST_FORM_TITLE);
aspectProps.put(ContentModel.PROP_DESCRIPTION, TEST_FORM_DESCRIPTION);
nodeService.addAspect(referencingDocNodeRef, ContentModel.ASPECT_TITLED, aspectProps);
// Write some content into the node.
ContentWriter contentWriter = this.contentService.getWriter(referencingDoc.getNodeRef(),
ContentModel.PROP_CONTENT, true);
contentWriter.setEncoding("UTF-8");
contentWriter.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
contentWriter.putContent("The quick brown fox jumped over the lazy dog.");
// Create a folder - will use this for child-association testing
FileInfo associatedDocsFolder =
this.fileFolderService.create(companyHomeNodeRef, "testFolder" + guid, ContentModel.TYPE_FOLDER);
testFolderNodeRef = associatedDocsFolder.getNodeRef();
this.associatedDoc_A = createTestNode("associatedDoc_A" + guid);
this.associatedDoc_B = createTestNode("associatedDoc_B" + guid);
this.associatedDoc_C = createTestNode("associatedDoc_C" + guid);
this.associatedDoc_D = createTestNode("associatedDoc_D" + guid);
this.associatedDoc_E = createTestNode("associatedDoc_E" + guid);
// Now create associations between the referencing and the two node refs.
aspectProps.clear();
this.nodeService.addAspect(this.referencingDocNodeRef, ContentModel.ASPECT_REFERENCING, aspectProps);
this.nodeService.createAssociation(this.referencingDocNodeRef, associatedDoc_A, ContentModel.ASSOC_REFERENCES);
this.nodeService.createAssociation(this.referencingDocNodeRef, associatedDoc_B, ContentModel.ASSOC_REFERENCES);
// Leave the 3rd, 4th and 5th nodes without associations as they may be created in
// other test code.
// Create a container for the children.
HashMap<QName, Serializable> containerProps = new HashMap<QName, Serializable>();
this.containerNodeRef = nodeService.createNode(companyHomeNodeRef, ContentModel.ASSOC_CONTAINS,
QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "testContainer" + guid),
ContentModel.TYPE_CONTAINER,
containerProps).getChildRef();
this.childDoc_A = createTestNode("childDoc_A" + guid);
this.childDoc_B = createTestNode("childDoc_B" + guid);
this.childDoc_C = createTestNode("childDoc_C" + guid);
this.childDoc_D = createTestNode("childDoc_D" + guid);
this.childDoc_E = createTestNode("childDoc_E" + guid);
// Now create the pre-test child-associations.
this.nodeService.addChild(containerNodeRef, childDoc_A, ContentModel.ASSOC_CHILDREN, QName.createQName("childA"));
this.nodeService.addChild(containerNodeRef, childDoc_B, ContentModel.ASSOC_CHILDREN, QName.createQName("childB"));
// The other childDoc nodes will be added as children over the REST API as part
// of later test code.
// Create and store the url to the referencingNode
StringBuilder builder = new StringBuilder();
builder.append("/api/forms/node/workspace/").append(referencingDocNodeRef.getStoreRef().getIdentifier())
.append("/").append(referencingDocNodeRef.getId());
this.referencingNodeUrl = builder.toString();
// Create and store the url to the containing node
builder = new StringBuilder();
builder.append("/api/forms/node/workspace/").append(containerNodeRef.getStoreRef().getIdentifier())
.append("/").append(containerNodeRef.getId());
this.containingNodeUrl = builder.toString();
// Store the original properties of this node
this.refNodePropertiesAfterCreation = nodeService.getProperties(referencingDocNodeRef);
refNodePropertiesAfterCreation.toString();
}
@Override
public void tearDown()
{
nodeService.deleteNode(this.referencingDocNodeRef);
nodeService.deleteNode(this.associatedDoc_A);
nodeService.deleteNode(this.associatedDoc_B);
nodeService.deleteNode(this.associatedDoc_C);
nodeService.deleteNode(this.associatedDoc_D);
nodeService.deleteNode(this.associatedDoc_E);
nodeService.deleteNode(this.childDoc_A);
nodeService.deleteNode(this.childDoc_B);
nodeService.deleteNode(this.childDoc_C);
nodeService.deleteNode(this.childDoc_D);
nodeService.deleteNode(this.childDoc_E);
nodeService.deleteNode(this.testFolderNodeRef);
nodeService.deleteNode(this.containerNodeRef);
}
protected Response sendGetReq(String url, int expectedStatusCode)
throws IOException, UnsupportedEncodingException
{
Response result = sendRequest(new GetRequest(url), expectedStatusCode);
return result;
}
protected NodeRef createTestNode(String associatedDocName)
{
Map<QName, Serializable> docProps = new HashMap<QName, Serializable>(1);
docProps.put(ContentModel.PROP_NAME, associatedDocName + ".txt");
return this.nodeService.createNode(
testFolderNodeRef,
ContentModel.ASSOC_CONTAINS,
QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, associatedDocName + ".txt"),
ContentModel.TYPE_CONTENT,
docProps).getChildRef();
}
/*
* Copyright (C) 2005-2009 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 received 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.forms;
import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.model.Repository;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.web.scripts.BaseWebScriptTest;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.model.FileInfo;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.GUID;
import org.alfresco.web.scripts.TestWebScriptServer.GetRequest;
import org.alfresco.web.scripts.TestWebScriptServer.Response;
public abstract class AbstractTestFormRestApi extends BaseWebScriptTest
{
protected static final String APPLICATION_JSON = "application/json";
protected static final String TEST_FORM_DESCRIPTION = "Test form description";
protected static final String TEST_FORM_TITLE = "Test form title";
protected String referencingNodeDefUrl;
protected String referencingNodeUpdateUrl;
protected String containingNodeDefUrl;
protected String containingNodeUpdateUrl;
protected String containingNodeUrl;
protected NodeRef referencingDocNodeRef;
protected Map<QName, Serializable> refNodePropertiesAfterCreation;
protected NodeRef associatedDoc_A;
protected NodeRef associatedDoc_B;
protected NodeRef associatedDoc_C;
protected NodeRef associatedDoc_D;
protected NodeRef associatedDoc_E;
protected NodeRef childDoc_A;
protected NodeRef childDoc_B;
protected NodeRef childDoc_C;
protected NodeRef childDoc_D;
protected NodeRef childDoc_E;
protected NodeRef testFolderNodeRef;
protected NodeService nodeService;
private FileFolderService fileFolderService;
private ContentService contentService;
private Repository repositoryHelper;
protected NodeRef containerNodeRef;
@Override
protected void setUp() throws Exception
{
super.setUp();
this.fileFolderService = (FileFolderService) getServer().getApplicationContext().getBean(
"FileFolderService");
this.contentService = (ContentService) getServer().getApplicationContext().getBean(
"ContentService");
this.repositoryHelper = (Repository) getServer().getApplicationContext().getBean(
"repositoryHelper");
this.nodeService = (NodeService) getServer().getApplicationContext().getBean("NodeService");
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getSystemUserName());
NodeRef companyHomeNodeRef = this.repositoryHelper.getCompanyHome();
String guid = GUID.generate();
// Create a test file (not a folder)
FileInfo referencingDoc = this.fileFolderService.create(companyHomeNodeRef,
"referencingDoc" + guid + ".txt", ContentModel.TYPE_CONTENT);
referencingDocNodeRef = referencingDoc.getNodeRef();
// Add an aspect.
Map<QName, Serializable> aspectProps = new HashMap<QName, Serializable>(2);
aspectProps.put(ContentModel.PROP_TITLE, TEST_FORM_TITLE);
aspectProps.put(ContentModel.PROP_DESCRIPTION, TEST_FORM_DESCRIPTION);
nodeService.addAspect(referencingDocNodeRef, ContentModel.ASPECT_TITLED, aspectProps);
// Write some content into the node.
ContentWriter contentWriter = this.contentService.getWriter(referencingDoc.getNodeRef(),
ContentModel.PROP_CONTENT, true);
contentWriter.setEncoding("UTF-8");
contentWriter.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
contentWriter.putContent("The quick brown fox jumped over the lazy dog.");
// Create a folder - will use this for child-association testing
FileInfo associatedDocsFolder =
this.fileFolderService.create(companyHomeNodeRef, "testFolder" + guid, ContentModel.TYPE_FOLDER);
testFolderNodeRef = associatedDocsFolder.getNodeRef();
this.associatedDoc_A = createTestNode("associatedDoc_A" + guid);
this.associatedDoc_B = createTestNode("associatedDoc_B" + guid);
this.associatedDoc_C = createTestNode("associatedDoc_C" + guid);
this.associatedDoc_D = createTestNode("associatedDoc_D" + guid);
this.associatedDoc_E = createTestNode("associatedDoc_E" + guid);
// Now create associations between the referencing and the two node refs.
aspectProps.clear();
this.nodeService.addAspect(this.referencingDocNodeRef, ContentModel.ASPECT_REFERENCING, aspectProps);
this.nodeService.createAssociation(this.referencingDocNodeRef, associatedDoc_A, ContentModel.ASSOC_REFERENCES);
this.nodeService.createAssociation(this.referencingDocNodeRef, associatedDoc_B, ContentModel.ASSOC_REFERENCES);
// Leave the 3rd, 4th and 5th nodes without associations as they may be created in
// other test code.
// Create a container for the children.
HashMap<QName, Serializable> containerProps = new HashMap<QName, Serializable>();
this.containerNodeRef = nodeService.createNode(companyHomeNodeRef, ContentModel.ASSOC_CONTAINS,
QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "testContainer" + guid),
ContentModel.TYPE_CONTAINER,
containerProps).getChildRef();
this.childDoc_A = createTestNode("childDoc_A" + guid);
this.childDoc_B = createTestNode("childDoc_B" + guid);
this.childDoc_C = createTestNode("childDoc_C" + guid);
this.childDoc_D = createTestNode("childDoc_D" + guid);
this.childDoc_E = createTestNode("childDoc_E" + guid);
// Now create the pre-test child-associations.
this.nodeService.addChild(containerNodeRef, childDoc_A, ContentModel.ASSOC_CHILDREN, QName.createQName("childA"));
this.nodeService.addChild(containerNodeRef, childDoc_B, ContentModel.ASSOC_CHILDREN, QName.createQName("childB"));
// The other childDoc nodes will be added as children over the REST API as part
// of later test code.
// Create and store the urls to the referencingNode
StringBuilder builder = new StringBuilder();
builder.append("/api/form/definition/node/workspace/").append(referencingDocNodeRef.getStoreRef().getIdentifier())
.append("/").append(referencingDocNodeRef.getId());
this.referencingNodeDefUrl = builder.toString();
builder = new StringBuilder();
builder.append("/api/form/node/workspace/").append(referencingDocNodeRef.getStoreRef().getIdentifier())
.append("/").append(referencingDocNodeRef.getId());
this.referencingNodeUpdateUrl = builder.toString();
// Create and store the urls to the containing node
builder = new StringBuilder();
builder.append("/api/form/definition/node/workspace/").append(containerNodeRef.getStoreRef().getIdentifier())
.append("/").append(containerNodeRef.getId());
this.containingNodeDefUrl = builder.toString();
builder = new StringBuilder();
builder.append("/api/form/node/workspace/").append(containerNodeRef.getStoreRef().getIdentifier())
.append("/").append(containerNodeRef.getId());
this.containingNodeUpdateUrl = builder.toString();
// Store the original properties of this node
this.refNodePropertiesAfterCreation = nodeService.getProperties(referencingDocNodeRef);
refNodePropertiesAfterCreation.toString();
}
@Override
public void tearDown()
{
nodeService.deleteNode(this.referencingDocNodeRef);
nodeService.deleteNode(this.associatedDoc_A);
nodeService.deleteNode(this.associatedDoc_B);
nodeService.deleteNode(this.associatedDoc_C);
nodeService.deleteNode(this.associatedDoc_D);
nodeService.deleteNode(this.associatedDoc_E);
nodeService.deleteNode(this.childDoc_A);
nodeService.deleteNode(this.childDoc_B);
nodeService.deleteNode(this.childDoc_C);
nodeService.deleteNode(this.childDoc_D);
nodeService.deleteNode(this.childDoc_E);
nodeService.deleteNode(this.testFolderNodeRef);
nodeService.deleteNode(this.containerNodeRef);
}
protected Response sendGetReq(String url, int expectedStatusCode)
throws IOException, UnsupportedEncodingException
{
Response result = sendRequest(new GetRequest(url), expectedStatusCode);
return result;
}
protected NodeRef createTestNode(String associatedDocName)
{
Map<QName, Serializable> docProps = new HashMap<QName, Serializable>(1);
docProps.put(ContentModel.PROP_NAME, associatedDocName + ".txt");
return this.nodeService.createNode(
testFolderNodeRef,
ContentModel.ASSOC_CONTAINS,
QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, associatedDocName + ".txt"),
ContentModel.TYPE_CONTENT,
docProps).getChildRef();
}
}

View File

@@ -28,30 +28,41 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.web.scripts.TestWebScriptServer.PostRequest;
import org.alfresco.web.scripts.TestWebScriptServer.Response;
import org.alfresco.web.scripts.json.JSONUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
public class FormRestApiGet_Test extends AbstractTestFormRestApi {
public class FormRestApiGet_Test extends AbstractTestFormRestApi
{
public void testResponseContentType() throws Exception
{
Response rsp = sendGetReq(referencingNodeUrl, 200);
JSONObject jsonPostData = new JSONObject();
String jsonPostString = jsonPostData.toString();
Response rsp = sendRequest(new PostRequest(referencingNodeDefUrl,
jsonPostString, APPLICATION_JSON), 200);
assertEquals("application/json;charset=UTF-8", rsp.getContentType());
}
public void testGetFormForNonExistentNode() throws Exception
{
// Replace all digits with an 'x' char - this should make for a non-existent node.
Response rsp = sendGetReq(referencingNodeUrl.replaceAll("\\d", "x"), 404);
JSONObject jsonPostData = new JSONObject();
String jsonPostString = jsonPostData.toString();
Response rsp = sendRequest(new PostRequest(referencingNodeDefUrl.replaceAll("\\d", "x"),
jsonPostString, APPLICATION_JSON), 404);
assertEquals("application/json;charset=UTF-8", rsp.getContentType());
}
public void testJsonContentParsesCorrectly() throws Exception
{
Response rsp = sendGetReq(referencingNodeUrl, 200);
JSONObject jsonPostData = new JSONObject();
String jsonPostString = jsonPostData.toString();
Response rsp = sendRequest(new PostRequest(referencingNodeDefUrl,
jsonPostString, APPLICATION_JSON), 200);
String jsonResponseString = rsp.getContentAsString();
Object jsonObject = new JSONUtils().toObject(jsonResponseString);
@@ -60,7 +71,10 @@ public class FormRestApiGet_Test extends AbstractTestFormRestApi {
public void testJsonUpperStructure() throws Exception
{
Response rsp = sendGetReq(referencingNodeUrl, 200);
JSONObject jsonPostData = new JSONObject();
String jsonPostString = jsonPostData.toString();
Response rsp = sendRequest(new PostRequest(referencingNodeDefUrl,
jsonPostString, APPLICATION_JSON), 200);
String jsonResponseString = rsp.getContentAsString();
JSONObject jsonParsedObject = new JSONObject(new JSONTokener(jsonResponseString));
@@ -87,7 +101,10 @@ public class FormRestApiGet_Test extends AbstractTestFormRestApi {
@SuppressWarnings("unchecked")
public void testJsonFormData() throws Exception
{
Response rsp = sendGetReq(referencingNodeUrl, 200);
JSONObject jsonPostData = new JSONObject();
String jsonPostString = jsonPostData.toString();
Response rsp = sendRequest(new PostRequest(referencingNodeDefUrl,
jsonPostString, APPLICATION_JSON), 200);
String jsonResponseString = rsp.getContentAsString();
// At this point the formData names have underscores
@@ -118,7 +135,10 @@ public class FormRestApiGet_Test extends AbstractTestFormRestApi {
@SuppressWarnings("unchecked")
public void testJsonDefinitionFields() throws Exception
{
Response rsp = sendGetReq(referencingNodeUrl, 200);
JSONObject jsonPostData = new JSONObject();
String jsonPostString = jsonPostData.toString();
Response rsp = sendRequest(new PostRequest(referencingNodeDefUrl,
jsonPostString, APPLICATION_JSON), 200);
String jsonResponseString = rsp.getContentAsString();
JSONObject jsonParsedObject = new JSONObject(new JSONTokener(jsonResponseString));
@@ -149,4 +169,71 @@ public class FormRestApiGet_Test extends AbstractTestFormRestApi {
}
}
}
public void testJsonSelectedFields() throws Exception
{
JSONObject jsonPostData = new JSONObject();
JSONArray jsonFields = new JSONArray();
jsonFields.put("cm:name");
jsonFields.put("cm:title");
jsonFields.put("cm:publisher");
jsonPostData.put("fields", jsonFields);
// Submit the JSON request.
String jsonPostString = jsonPostData.toString();
Response rsp = sendRequest(new PostRequest(referencingNodeDefUrl, jsonPostString,
APPLICATION_JSON), 200);
String jsonResponseString = rsp.getContentAsString();
JSONObject jsonParsedObject = new JSONObject(new JSONTokener(jsonResponseString));
assertNotNull(jsonParsedObject);
JSONObject rootDataObject = (JSONObject)jsonParsedObject.get("data");
JSONObject definitionObject = (JSONObject)rootDataObject.get("definition");
JSONArray fieldsArray = (JSONArray)definitionObject.get("fields");
assertEquals("Expected 2 fields", 2, fieldsArray.length());
// get the data and check it
JSONObject formDataObject = (JSONObject)rootDataObject.get("formData");
assertNotNull("Expected to find cm:name data", formDataObject.get("prop_cm_name"));
assertNotNull("Expected to find cm:title data", formDataObject.get("prop_cm_title"));
assertEquals(TEST_FORM_TITLE, formDataObject.get("prop_cm_title"));
}
public void testJsonForcedFields() throws Exception
{
JSONObject jsonPostData = new JSONObject();
JSONArray jsonFields = new JSONArray();
jsonFields.put("cm:name");
jsonFields.put("cm:title");
jsonFields.put("cm:publisher");
jsonFields.put("cm:wrong");
jsonPostData.put("fields", jsonFields);
JSONArray jsonForcedFields = new JSONArray();
jsonForcedFields.put("cm:publisher");
jsonForcedFields.put("cm:wrong");
jsonPostData.put("force", jsonForcedFields);
// Submit the JSON request.
String jsonPostString = jsonPostData.toString();
Response rsp = sendRequest(new PostRequest(referencingNodeDefUrl, jsonPostString,
APPLICATION_JSON), 200);
String jsonResponseString = rsp.getContentAsString();
JSONObject jsonParsedObject = new JSONObject(new JSONTokener(jsonResponseString));
assertNotNull(jsonParsedObject);
JSONObject rootDataObject = (JSONObject)jsonParsedObject.get("data");
JSONObject definitionObject = (JSONObject)rootDataObject.get("definition");
JSONArray fieldsArray = (JSONArray)definitionObject.get("fields");
assertEquals("Expected 3 fields", 3, fieldsArray.length());
// get the data and check it
JSONObject formDataObject = (JSONObject)rootDataObject.get("formData");
assertNotNull("Expected to find cm:name data", formDataObject.get("prop_cm_name"));
assertNotNull("Expected to find cm:title data", formDataObject.get("prop_cm_title"));
assertEquals(TEST_FORM_TITLE, formDataObject.get("prop_cm_title"));
}
}

View File

@@ -1,424 +1,423 @@
/*
* Copyright (C) 2005-2009 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 received 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.forms;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.service.cmr.repository.AssociationRef;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.namespace.RegexQNamePattern;
import org.alfresco.web.scripts.TestWebScriptServer.GetRequest;
import org.alfresco.web.scripts.TestWebScriptServer.PostRequest;
import org.alfresco.web.scripts.TestWebScriptServer.Response;
import org.json.JSONException;
import org.json.JSONObject;
public class FormRestApiJsonPost_Test extends AbstractTestFormRestApi
{
private static final String PROP_CM_DESCRIPTION = "prop_cm_description";
private static final String PROP_MIMETYPE = "prop_mimetype";
private static final String APPLICATION_JSON = "application/json";
private static final String ASSOC_CM_REFERENCES = "assoc_cm_references";
private static final String ASSOC_CM_REFERENCES_ADDED = "assoc_cm_references_added";
private static final String ASSOC_CM_REFERENCES_REMOVED = "assoc_cm_references_removed";
private static final String ASSOC_SYS_CHILDREN = "assoc_sys_children";
private static final String ASSOC_SYS_CHILDREN_ADDED = "assoc_sys_children_added";
private static final String ASSOC_SYS_CHILDREN_REMOVED = "assoc_sys_children_removed";
public void testSimpleJsonPostRequest() throws IOException, JSONException
{
// Retrieve and store the original property value.
Serializable originalDescription =
nodeService.getProperty(referencingDocNodeRef, ContentModel.PROP_DESCRIPTION);
assertEquals(TEST_FORM_DESCRIPTION, originalDescription);
// get the original mimetype
String originalMimetype = null;
ContentData content = (ContentData)this.nodeService.getProperty(referencingDocNodeRef, ContentModel.PROP_CONTENT);
if (content != null)
{
originalMimetype = content.getMimetype();
}
// Construct some JSON to represent a new value.
JSONObject jsonPostData = new JSONObject();
final String proposedNewDescription = "Modified Description";
jsonPostData.put(PROP_CM_DESCRIPTION, proposedNewDescription);
jsonPostData.put(PROP_MIMETYPE, MimetypeMap.MIMETYPE_HTML);
// Submit the JSON request.
String jsonPostString = jsonPostData.toString();
Response ignoredRsp = sendRequest(new PostRequest(referencingNodeUrl, jsonPostString,
APPLICATION_JSON), 200);
// The nodeService should give us the modified property.
Serializable modifiedDescription =
nodeService.getProperty(referencingDocNodeRef, ContentModel.PROP_DESCRIPTION);
assertEquals(proposedNewDescription, modifiedDescription);
// get the original mimetype
String modifiedMimetype = null;
content = (ContentData)this.nodeService.getProperty(referencingDocNodeRef, ContentModel.PROP_CONTENT);
if (content != null)
{
modifiedMimetype = content.getMimetype();
}
assertEquals(MimetypeMap.MIMETYPE_HTML, modifiedMimetype);
// The Rest API should also give us the modified property.
Response response = sendRequest(new GetRequest(referencingNodeUrl), 200);
JSONObject jsonGetResponse = new JSONObject(response.getContentAsString());
JSONObject jsonDataObj = (JSONObject)jsonGetResponse.get("data");
assertNotNull(jsonDataObj);
JSONObject formData = (JSONObject)jsonDataObj.get("formData");
assertNotNull(formData);
String retrievedValue = (String)formData.get(PROP_CM_DESCRIPTION);
assertEquals(modifiedDescription, retrievedValue);
String retrievedMimetype = (String)formData.get(PROP_MIMETYPE);
assertEquals(MimetypeMap.MIMETYPE_HTML, modifiedMimetype);
}
/**
* This test method attempts to add new associations between existing nodes.
*/
public void testAddNewAssociationsToNode() throws Exception
{
List<NodeRef> associatedNodes;
checkOriginalAssocsBeforeChanges();
// Add three additional associations
JSONObject jsonPostData = new JSONObject();
String assocsToAdd = associatedDoc_C + "," + associatedDoc_D + "," + associatedDoc_E;
jsonPostData.put(ASSOC_CM_REFERENCES_ADDED, assocsToAdd);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(referencingNodeUrl, jsonPostString, APPLICATION_JSON), 200);
// Check the now updated associations via the node service
List<AssociationRef> modifiedAssocs = nodeService.getTargetAssocs(referencingDocNodeRef, RegexQNamePattern.MATCH_ALL);
assertEquals(5, modifiedAssocs.size());
// Extract the target nodeRefs to make them easier to examine
associatedNodes = new ArrayList<NodeRef>(5);
for (AssociationRef assocRef : modifiedAssocs)
{
associatedNodes.add(assocRef.getTargetRef());
}
assertTrue(associatedNodes.contains(associatedDoc_A));
assertTrue(associatedNodes.contains(associatedDoc_B));
assertTrue(associatedNodes.contains(associatedDoc_C));
assertTrue(associatedNodes.contains(associatedDoc_D));
assertTrue(associatedNodes.contains(associatedDoc_E));
// The Rest API should also give us the modified assocs.
Response response = sendRequest(new GetRequest(referencingNodeUrl), 200);
String jsonRspString = response.getContentAsString();
JSONObject jsonGetResponse = new JSONObject(jsonRspString);
JSONObject jsonData = (JSONObject)jsonGetResponse.get("data");
assertNotNull(jsonData);
JSONObject jsonFormData = (JSONObject)jsonData.get("formData");
assertNotNull(jsonFormData);
String jsonAssocs = (String)jsonFormData.get(ASSOC_CM_REFERENCES);
// We expect exactly 5 assocs on the test node
assertEquals(5, jsonAssocs.split(",").length);
for (AssociationRef assocRef : modifiedAssocs)
{
assertTrue(jsonAssocs.contains(assocRef.getTargetRef().toString()));
}
}
/**
* This test method attempts to remove an existing association between two existing
* nodes.
*/
public void testRemoveAssociationsFromNode() throws Exception
{
List<NodeRef> associatedNodes;
checkOriginalAssocsBeforeChanges();
// Remove an association
JSONObject jsonPostData = new JSONObject();
String assocsToRemove = associatedDoc_B.toString();
jsonPostData.put(ASSOC_CM_REFERENCES_REMOVED, assocsToRemove);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(referencingNodeUrl, jsonPostString, APPLICATION_JSON), 200);
// Check the now updated associations via the node service
List<AssociationRef> modifiedAssocs = nodeService.getTargetAssocs(referencingDocNodeRef, RegexQNamePattern.MATCH_ALL);
assertEquals(1, modifiedAssocs.size());
// Extract the target nodeRefs to make them easier to examine
associatedNodes = new ArrayList<NodeRef>(5);
for (AssociationRef assocRef : modifiedAssocs)
{
associatedNodes.add(assocRef.getTargetRef());
}
assertTrue(associatedNodes.contains(associatedDoc_A));
// The Rest API should also give us the modified assocs.
Response response = sendRequest(new GetRequest(referencingNodeUrl), 200);
String jsonRspString = response.getContentAsString();
JSONObject jsonGetResponse = new JSONObject(jsonRspString);
JSONObject jsonData = (JSONObject)jsonGetResponse.get("data");
assertNotNull(jsonData);
JSONObject jsonFormData = (JSONObject)jsonData.get("formData");
assertNotNull(jsonFormData);
String jsonAssocs = (String)jsonFormData.get(ASSOC_CM_REFERENCES);
// We expect exactly 1 assoc on the test node
assertEquals(1, jsonAssocs.split(",").length);
for (AssociationRef assocRef : modifiedAssocs)
{
assertTrue(jsonAssocs.contains(assocRef.getTargetRef().toString()));
}
}
/**
* This test method attempts to add the same association twice. This attempt will
* not succeed, but the test case is to confirm that there is no exception thrown
* back across the REST API.
*/
public void testAddAssocThatAlreadyExists() throws Exception
{
checkOriginalAssocsBeforeChanges();
// Add an association
JSONObject jsonPostData = new JSONObject();
String assocsToAdd = associatedDoc_C.toString();
jsonPostData.put(ASSOC_CM_REFERENCES_ADDED, assocsToAdd);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(referencingNodeUrl, jsonPostString, APPLICATION_JSON), 200);
// Try to add the same association again
sendRequest(new PostRequest(referencingNodeUrl, jsonPostString, APPLICATION_JSON), 200);
}
/**
* This test method attempts to remove an association that does not exist. This
* attempt will not succeed, but the test case is to confirm that there is no
* exception thrown back across the REST API.
*/
public void testRemoveAssocThatDoesNotExist() throws Exception
{
checkOriginalAssocsBeforeChanges();
// Remove a non-existent association
JSONObject jsonPostData = new JSONObject();
String assocsToRemove = associatedDoc_E.toString();
jsonPostData.put(ASSOC_CM_REFERENCES_REMOVED, assocsToRemove);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(referencingNodeUrl, jsonPostString, APPLICATION_JSON), 200);
}
/**
* This test method attempts to add new associations between existing nodes.
*/
public void testAddNewChildAssociationsToNode() throws Exception
{
List<NodeRef> associatedNodes;
checkOriginalChildAssocsBeforeChanges();
// Add three additional associations
JSONObject jsonPostData = new JSONObject();
String assocsToAdd = childDoc_C + "," + childDoc_D + "," + childDoc_E;
jsonPostData.put(ASSOC_SYS_CHILDREN_ADDED, assocsToAdd);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(containingNodeUrl.toString(), jsonPostString, APPLICATION_JSON), 200);
// Check the now updated child associations via the node service
List<ChildAssociationRef> modifiedAssocs = nodeService.getChildAssocs(containerNodeRef);
assertEquals(5, modifiedAssocs.size());
// Extract the target nodeRefs to make them easier to examine
associatedNodes = new ArrayList<NodeRef>(5);
for (ChildAssociationRef assocRef : modifiedAssocs)
{
associatedNodes.add(assocRef.getChildRef());
}
assertTrue(associatedNodes.contains(childDoc_A));
assertTrue(associatedNodes.contains(childDoc_B));
assertTrue(associatedNodes.contains(childDoc_C));
assertTrue(associatedNodes.contains(childDoc_D));
assertTrue(associatedNodes.contains(childDoc_E));
// The Rest API should also give us the modified assocs.
Response response = sendRequest(new GetRequest(containingNodeUrl), 200);
String jsonRspString = response.getContentAsString();
JSONObject jsonGetResponse = new JSONObject(jsonRspString);
JSONObject jsonData = (JSONObject)jsonGetResponse.get("data");
assertNotNull(jsonData);
JSONObject jsonFormData = (JSONObject)jsonData.get("formData");
assertNotNull(jsonFormData);
String jsonAssocs = (String)jsonFormData.get(ASSOC_SYS_CHILDREN);
// We expect exactly 5 assocs on the test node
assertEquals(5, jsonAssocs.split(",").length);
for (ChildAssociationRef assocRef : modifiedAssocs)
{
String childNodeRef = assocRef.getChildRef().toString();
assertTrue(jsonAssocs.contains(childNodeRef));
assertTrue(NodeRef.isNodeRef(childNodeRef));
}
}
/**
* This test method attempts to remove an existing child association between two
* existing nodes.
*/
public void testRemoveChildAssociationsFromNode() throws Exception
{
List<NodeRef> associatedNodes;
checkOriginalChildAssocsBeforeChanges();
// Remove an association
JSONObject jsonPostData = new JSONObject();
String assocsToRemove = childDoc_B.toString();
jsonPostData.put(ASSOC_SYS_CHILDREN_REMOVED, assocsToRemove);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(containingNodeUrl, jsonPostString, APPLICATION_JSON), 200);
// Check the now updated child associations via the node service
List<ChildAssociationRef> modifiedAssocs = nodeService.getChildAssocs(containerNodeRef);
assertEquals(1, modifiedAssocs.size());
// Extract the target nodeRefs to make them easier to examine
associatedNodes = new ArrayList<NodeRef>(5);
for (ChildAssociationRef assocRef : modifiedAssocs)
{
associatedNodes.add(assocRef.getChildRef());
}
assertTrue(associatedNodes.contains(childDoc_A));
// The Rest API should also give us the modified assocs.
Response response = sendRequest(new GetRequest(containingNodeUrl), 200);
String jsonRspString = response.getContentAsString();
JSONObject jsonGetResponse = new JSONObject(jsonRspString);
JSONObject jsonData = (JSONObject)jsonGetResponse.get("data");
assertNotNull(jsonData);
JSONObject jsonFormData = (JSONObject)jsonData.get("formData");
assertNotNull(jsonFormData);
String jsonAssocs = (String)jsonFormData.get(ASSOC_SYS_CHILDREN);
// We expect exactly 1 assoc on the test node
assertEquals(1, jsonAssocs.split(",").length);
for (ChildAssociationRef assocRef : modifiedAssocs)
{
assertTrue(jsonAssocs.contains(assocRef.getChildRef().toString()));
}
}
/**
* This test method attempts to add the same child association twice. This attempt
* will not succeed, but the test case is to confirm that there is no exception thrown
* back across the REST API.
*/
public void testAddChildAssocThatAlreadyExists() throws Exception
{
checkOriginalChildAssocsBeforeChanges();
// Add an association
JSONObject jsonPostData = new JSONObject();
String assocsToAdd = this.childDoc_C.toString();
jsonPostData.put(ASSOC_SYS_CHILDREN_ADDED, assocsToAdd);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(referencingNodeUrl, jsonPostString, APPLICATION_JSON), 200);
// Try to add the same child association again
sendRequest(new PostRequest(referencingNodeUrl, jsonPostString, APPLICATION_JSON), 200);
}
/**
* This test method attempts to remove a child association that does not exist. This
* attempt will not succeed, but the test case is to confirm that there is no
* exception thrown back across the REST API.
*/
public void testRemoveChildAssocThatDoesNotExist() throws Exception
{
checkOriginalChildAssocsBeforeChanges();
// Remove a non-existent child association
JSONObject jsonPostData = new JSONObject();
String assocsToRemove = childDoc_E.toString();
jsonPostData.put(ASSOC_SYS_CHILDREN_REMOVED, assocsToRemove);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(referencingNodeUrl, jsonPostString, APPLICATION_JSON), 200);
}
private void checkOriginalAssocsBeforeChanges()
{
List<AssociationRef> originalAssocs = nodeService.getTargetAssocs(referencingDocNodeRef, RegexQNamePattern.MATCH_ALL);
assertEquals(2, originalAssocs.size());
List<NodeRef> associatedNodes = new ArrayList<NodeRef>(2);
associatedNodes.add(originalAssocs.get(0).getTargetRef());
associatedNodes.add(originalAssocs.get(1).getTargetRef());
assertTrue(associatedNodes.contains(associatedDoc_A));
assertTrue(associatedNodes.contains(associatedDoc_B));
}
private void checkOriginalChildAssocsBeforeChanges()
{
List<ChildAssociationRef> originalChildAssocs = nodeService.getChildAssocs(containerNodeRef);
assertEquals(2, originalChildAssocs.size());
List<NodeRef> associatedNodes = new ArrayList<NodeRef>(2);
associatedNodes.add(originalChildAssocs.get(0).getChildRef());
associatedNodes.add(originalChildAssocs.get(1).getChildRef());
assertTrue(associatedNodes.contains(childDoc_A));
assertTrue(associatedNodes.contains(childDoc_B));
}
}
/*
* Copyright (C) 2005-2009 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 received 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.forms;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.service.cmr.repository.AssociationRef;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.namespace.RegexQNamePattern;
import org.alfresco.web.scripts.TestWebScriptServer.PostRequest;
import org.alfresco.web.scripts.TestWebScriptServer.Response;
import org.json.JSONException;
import org.json.JSONObject;
public class FormRestApiJsonPost_Test extends AbstractTestFormRestApi
{
private static final String PROP_CM_DESCRIPTION = "prop_cm_description";
private static final String PROP_MIMETYPE = "prop_mimetype";
private static final String ASSOC_CM_REFERENCES = "assoc_cm_references";
private static final String ASSOC_CM_REFERENCES_ADDED = "assoc_cm_references_added";
private static final String ASSOC_CM_REFERENCES_REMOVED = "assoc_cm_references_removed";
private static final String ASSOC_SYS_CHILDREN = "assoc_sys_children";
private static final String ASSOC_SYS_CHILDREN_ADDED = "assoc_sys_children_added";
private static final String ASSOC_SYS_CHILDREN_REMOVED = "assoc_sys_children_removed";
public void testSimpleJsonPostRequest() throws IOException, JSONException
{
// Retrieve and store the original property value.
Serializable originalDescription =
nodeService.getProperty(referencingDocNodeRef, ContentModel.PROP_DESCRIPTION);
assertEquals(TEST_FORM_DESCRIPTION, originalDescription);
// get the original mimetype
String originalMimetype = null;
ContentData content = (ContentData)this.nodeService.getProperty(referencingDocNodeRef, ContentModel.PROP_CONTENT);
if (content != null)
{
originalMimetype = content.getMimetype();
}
// Construct some JSON to represent a new value.
JSONObject jsonPostData = new JSONObject();
final String proposedNewDescription = "Modified Description";
jsonPostData.put(PROP_CM_DESCRIPTION, proposedNewDescription);
jsonPostData.put(PROP_MIMETYPE, MimetypeMap.MIMETYPE_HTML);
// Submit the JSON request.
String jsonPostString = jsonPostData.toString();
Response ignoredRsp = sendRequest(new PostRequest(referencingNodeUpdateUrl, jsonPostString,
APPLICATION_JSON), 200);
// The nodeService should give us the modified property.
Serializable modifiedDescription =
nodeService.getProperty(referencingDocNodeRef, ContentModel.PROP_DESCRIPTION);
assertEquals(proposedNewDescription, modifiedDescription);
// get the modified mimetype
String modifiedMimetype = null;
content = (ContentData)this.nodeService.getProperty(referencingDocNodeRef, ContentModel.PROP_CONTENT);
if (content != null)
{
modifiedMimetype = content.getMimetype();
}
assertEquals(MimetypeMap.MIMETYPE_HTML, modifiedMimetype);
// The Rest API should also give us the modified property.
/*
Response response = sendRequest(new GetRequest(referencingNodeUpdateUrl), 200);
JSONObject jsonGetResponse = new JSONObject(response.getContentAsString());
JSONObject jsonDataObj = (JSONObject)jsonGetResponse.get("data");
assertNotNull(jsonDataObj);
JSONObject formData = (JSONObject)jsonDataObj.get("formData");
assertNotNull(formData);
String retrievedValue = (String)formData.get(PROP_CM_DESCRIPTION);
assertEquals(modifiedDescription, retrievedValue);
String retrievedMimetype = (String)formData.get(PROP_MIMETYPE);
assertEquals(MimetypeMap.MIMETYPE_HTML, modifiedMimetype);*/
}
/**
* This test method attempts to add new associations between existing nodes.
*/
public void testAddNewAssociationsToNode() throws Exception
{
List<NodeRef> associatedNodes;
checkOriginalAssocsBeforeChanges();
// Add three additional associations
JSONObject jsonPostData = new JSONObject();
String assocsToAdd = associatedDoc_C + "," + associatedDoc_D + "," + associatedDoc_E;
jsonPostData.put(ASSOC_CM_REFERENCES_ADDED, assocsToAdd);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(referencingNodeUpdateUrl, jsonPostString, APPLICATION_JSON), 200);
// Check the now updated associations via the node service
List<AssociationRef> modifiedAssocs = nodeService.getTargetAssocs(referencingDocNodeRef, RegexQNamePattern.MATCH_ALL);
assertEquals(5, modifiedAssocs.size());
// Extract the target nodeRefs to make them easier to examine
associatedNodes = new ArrayList<NodeRef>(5);
for (AssociationRef assocRef : modifiedAssocs)
{
associatedNodes.add(assocRef.getTargetRef());
}
assertTrue(associatedNodes.contains(associatedDoc_A));
assertTrue(associatedNodes.contains(associatedDoc_B));
assertTrue(associatedNodes.contains(associatedDoc_C));
assertTrue(associatedNodes.contains(associatedDoc_D));
assertTrue(associatedNodes.contains(associatedDoc_E));
// The Rest API should also give us the modified assocs.
/*Response response = sendRequest(new GetRequest(referencingNodeUpdateUrl), 200);
String jsonRspString = response.getContentAsString();
JSONObject jsonGetResponse = new JSONObject(jsonRspString);
JSONObject jsonData = (JSONObject)jsonGetResponse.get("data");
assertNotNull(jsonData);
JSONObject jsonFormData = (JSONObject)jsonData.get("formData");
assertNotNull(jsonFormData);
String jsonAssocs = (String)jsonFormData.get(ASSOC_CM_REFERENCES);
// We expect exactly 5 assocs on the test node
assertEquals(5, jsonAssocs.split(",").length);
for (AssociationRef assocRef : modifiedAssocs)
{
assertTrue(jsonAssocs.contains(assocRef.getTargetRef().toString()));
}*/
}
/**
* This test method attempts to remove an existing association between two existing
* nodes.
*/
public void testRemoveAssociationsFromNode() throws Exception
{
List<NodeRef> associatedNodes;
checkOriginalAssocsBeforeChanges();
// Remove an association
JSONObject jsonPostData = new JSONObject();
String assocsToRemove = associatedDoc_B.toString();
jsonPostData.put(ASSOC_CM_REFERENCES_REMOVED, assocsToRemove);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(referencingNodeUpdateUrl, jsonPostString, APPLICATION_JSON), 200);
// Check the now updated associations via the node service
List<AssociationRef> modifiedAssocs = nodeService.getTargetAssocs(referencingDocNodeRef, RegexQNamePattern.MATCH_ALL);
assertEquals(1, modifiedAssocs.size());
// Extract the target nodeRefs to make them easier to examine
associatedNodes = new ArrayList<NodeRef>(5);
for (AssociationRef assocRef : modifiedAssocs)
{
associatedNodes.add(assocRef.getTargetRef());
}
assertTrue(associatedNodes.contains(associatedDoc_A));
// The Rest API should also give us the modified assocs.
/*Response response = sendRequest(new GetRequest(referencingNodeUpdateUrl), 200);
String jsonRspString = response.getContentAsString();
JSONObject jsonGetResponse = new JSONObject(jsonRspString);
JSONObject jsonData = (JSONObject)jsonGetResponse.get("data");
assertNotNull(jsonData);
JSONObject jsonFormData = (JSONObject)jsonData.get("formData");
assertNotNull(jsonFormData);
String jsonAssocs = (String)jsonFormData.get(ASSOC_CM_REFERENCES);
// We expect exactly 1 assoc on the test node
assertEquals(1, jsonAssocs.split(",").length);
for (AssociationRef assocRef : modifiedAssocs)
{
assertTrue(jsonAssocs.contains(assocRef.getTargetRef().toString()));
}*/
}
/**
* This test method attempts to add the same association twice. This attempt will
* not succeed, but the test case is to confirm that there is no exception thrown
* back across the REST API.
*/
public void testAddAssocThatAlreadyExists() throws Exception
{
checkOriginalAssocsBeforeChanges();
// Add an association
JSONObject jsonPostData = new JSONObject();
String assocsToAdd = associatedDoc_C.toString();
jsonPostData.put(ASSOC_CM_REFERENCES_ADDED, assocsToAdd);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(referencingNodeUpdateUrl, jsonPostString, APPLICATION_JSON), 200);
// Try to add the same association again
sendRequest(new PostRequest(referencingNodeUpdateUrl, jsonPostString, APPLICATION_JSON), 200);
}
/**
* This test method attempts to remove an association that does not exist. This
* attempt will not succeed, but the test case is to confirm that there is no
* exception thrown back across the REST API.
*/
public void testRemoveAssocThatDoesNotExist() throws Exception
{
checkOriginalAssocsBeforeChanges();
// Remove a non-existent association
JSONObject jsonPostData = new JSONObject();
String assocsToRemove = associatedDoc_E.toString();
jsonPostData.put(ASSOC_CM_REFERENCES_REMOVED, assocsToRemove);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(referencingNodeUpdateUrl, jsonPostString, APPLICATION_JSON), 200);
}
/**
* This test method attempts to add new associations between existing nodes.
*/
public void testAddNewChildAssociationsToNode() throws Exception
{
List<NodeRef> associatedNodes;
checkOriginalChildAssocsBeforeChanges();
// Add three additional associations
JSONObject jsonPostData = new JSONObject();
String assocsToAdd = childDoc_C + "," + childDoc_D + "," + childDoc_E;
jsonPostData.put(ASSOC_SYS_CHILDREN_ADDED, assocsToAdd);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(containingNodeUpdateUrl.toString(), jsonPostString, APPLICATION_JSON), 200);
// Check the now updated child associations via the node service
List<ChildAssociationRef> modifiedAssocs = nodeService.getChildAssocs(containerNodeRef);
assertEquals(5, modifiedAssocs.size());
// Extract the target nodeRefs to make them easier to examine
associatedNodes = new ArrayList<NodeRef>(5);
for (ChildAssociationRef assocRef : modifiedAssocs)
{
associatedNodes.add(assocRef.getChildRef());
}
assertTrue(associatedNodes.contains(childDoc_A));
assertTrue(associatedNodes.contains(childDoc_B));
assertTrue(associatedNodes.contains(childDoc_C));
assertTrue(associatedNodes.contains(childDoc_D));
assertTrue(associatedNodes.contains(childDoc_E));
// The Rest API should also give us the modified assocs.
/*Response response = sendRequest(new GetRequest(containingNodeUpdateUrl), 200);
String jsonRspString = response.getContentAsString();
JSONObject jsonGetResponse = new JSONObject(jsonRspString);
JSONObject jsonData = (JSONObject)jsonGetResponse.get("data");
assertNotNull(jsonData);
JSONObject jsonFormData = (JSONObject)jsonData.get("formData");
assertNotNull(jsonFormData);
String jsonAssocs = (String)jsonFormData.get(ASSOC_SYS_CHILDREN);
// We expect exactly 5 assocs on the test node
assertEquals(5, jsonAssocs.split(",").length);
for (ChildAssociationRef assocRef : modifiedAssocs)
{
String childNodeRef = assocRef.getChildRef().toString();
assertTrue(jsonAssocs.contains(childNodeRef));
assertTrue(NodeRef.isNodeRef(childNodeRef));
}*/
}
/**
* This test method attempts to remove an existing child association between two
* existing nodes.
*/
public void testRemoveChildAssociationsFromNode() throws Exception
{
List<NodeRef> associatedNodes;
checkOriginalChildAssocsBeforeChanges();
// Remove an association
JSONObject jsonPostData = new JSONObject();
String assocsToRemove = childDoc_B.toString();
jsonPostData.put(ASSOC_SYS_CHILDREN_REMOVED, assocsToRemove);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(containingNodeUpdateUrl, jsonPostString, APPLICATION_JSON), 200);
// Check the now updated child associations via the node service
List<ChildAssociationRef> modifiedAssocs = nodeService.getChildAssocs(containerNodeRef);
assertEquals(1, modifiedAssocs.size());
// Extract the target nodeRefs to make them easier to examine
associatedNodes = new ArrayList<NodeRef>(5);
for (ChildAssociationRef assocRef : modifiedAssocs)
{
associatedNodes.add(assocRef.getChildRef());
}
assertTrue(associatedNodes.contains(childDoc_A));
// The Rest API should also give us the modified assocs.
/*Response response = sendRequest(new GetRequest(containingNodeUpdateUrl), 200);
String jsonRspString = response.getContentAsString();
JSONObject jsonGetResponse = new JSONObject(jsonRspString);
JSONObject jsonData = (JSONObject)jsonGetResponse.get("data");
assertNotNull(jsonData);
JSONObject jsonFormData = (JSONObject)jsonData.get("formData");
assertNotNull(jsonFormData);
String jsonAssocs = (String)jsonFormData.get(ASSOC_SYS_CHILDREN);
// We expect exactly 1 assoc on the test node
assertEquals(1, jsonAssocs.split(",").length);
for (ChildAssociationRef assocRef : modifiedAssocs)
{
assertTrue(jsonAssocs.contains(assocRef.getChildRef().toString()));
}*/
}
/**
* This test method attempts to add the same child association twice. This attempt
* will not succeed, but the test case is to confirm that there is no exception thrown
* back across the REST API.
*/
public void testAddChildAssocThatAlreadyExists() throws Exception
{
checkOriginalChildAssocsBeforeChanges();
// Add an association
JSONObject jsonPostData = new JSONObject();
String assocsToAdd = this.childDoc_C.toString();
jsonPostData.put(ASSOC_SYS_CHILDREN_ADDED, assocsToAdd);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(referencingNodeUpdateUrl, jsonPostString, APPLICATION_JSON), 200);
// Try to add the same child association again
sendRequest(new PostRequest(referencingNodeUpdateUrl, jsonPostString, APPLICATION_JSON), 200);
}
/**
* This test method attempts to remove a child association that does not exist. This
* attempt will not succeed, but the test case is to confirm that there is no
* exception thrown back across the REST API.
*/
public void testRemoveChildAssocThatDoesNotExist() throws Exception
{
checkOriginalChildAssocsBeforeChanges();
// Remove a non-existent child association
JSONObject jsonPostData = new JSONObject();
String assocsToRemove = childDoc_E.toString();
jsonPostData.put(ASSOC_SYS_CHILDREN_REMOVED, assocsToRemove);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(referencingNodeUpdateUrl, jsonPostString, APPLICATION_JSON), 200);
}
private void checkOriginalAssocsBeforeChanges()
{
List<AssociationRef> originalAssocs = nodeService.getTargetAssocs(referencingDocNodeRef, RegexQNamePattern.MATCH_ALL);
assertEquals(2, originalAssocs.size());
List<NodeRef> associatedNodes = new ArrayList<NodeRef>(2);
associatedNodes.add(originalAssocs.get(0).getTargetRef());
associatedNodes.add(originalAssocs.get(1).getTargetRef());
assertTrue(associatedNodes.contains(associatedDoc_A));
assertTrue(associatedNodes.contains(associatedDoc_B));
}
private void checkOriginalChildAssocsBeforeChanges()
{
List<ChildAssociationRef> originalChildAssocs = nodeService.getChildAssocs(containerNodeRef);
assertEquals(2, originalChildAssocs.size());
List<NodeRef> associatedNodes = new ArrayList<NodeRef>(2);
associatedNodes.add(originalChildAssocs.get(0).getChildRef());
associatedNodes.add(originalChildAssocs.get(1).getChildRef());
assertTrue(associatedNodes.contains(childDoc_A));
assertTrue(associatedNodes.contains(childDoc_B));
}
}