diff --git a/source/java/org/alfresco/repo/web/scripts/forms/AbstractTestFormRestApi.java b/source/java/org/alfresco/repo/web/scripts/forms/AbstractTestFormRestApi.java index 3b5dced5f8..9e3c1c824e 100644 --- a/source/java/org/alfresco/repo/web/scripts/forms/AbstractTestFormRestApi.java +++ b/source/java/org/alfresco/repo/web/scripts/forms/AbstractTestFormRestApi.java @@ -52,6 +52,7 @@ 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 refNodePropertiesAfterCreation; protected NodeRef associatedDoc_A; @@ -59,12 +60,18 @@ public abstract class AbstractTestFormRestApi extends BaseWebScriptTest protected NodeRef associatedDoc_C; protected NodeRef associatedDoc_D; protected NodeRef associatedDoc_E; - protected NodeRef associatedFolderNodeRef; + 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 @@ -104,8 +111,9 @@ public abstract class AbstractTestFormRestApi extends BaseWebScriptTest // Create a folder - will use this for child-association testing FileInfo associatedDocsFolder = - this.fileFolderService.create(companyHomeNodeRef, "associatedDocsFolder" + guid, ContentModel.TYPE_FOLDER); - associatedFolderNodeRef = associatedDocsFolder.getNodeRef(); + 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); @@ -118,15 +126,40 @@ public abstract class AbstractTestFormRestApi extends BaseWebScriptTest 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 third and 4th nodes without associations as they may be created in + // Leave the 3rd, 4th and 5th nodes without associations as they may be created in // other test code. - // Create and store the path + // Create a container for the children. + HashMap containerProps = new HashMap(); + 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); @@ -142,7 +175,13 @@ public abstract class AbstractTestFormRestApi extends BaseWebScriptTest nodeService.deleteNode(this.associatedDoc_C); nodeService.deleteNode(this.associatedDoc_D); nodeService.deleteNode(this.associatedDoc_E); - nodeService.deleteNode(this.associatedFolderNodeRef); + 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) @@ -157,7 +196,7 @@ public abstract class AbstractTestFormRestApi extends BaseWebScriptTest Map docProps = new HashMap(1); docProps.put(ContentModel.PROP_NAME, associatedDocName + ".txt"); return this.nodeService.createNode( - associatedFolderNodeRef, + testFolderNodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, associatedDocName + ".txt"), ContentModel.TYPE_CONTENT, diff --git a/source/java/org/alfresco/repo/web/scripts/forms/FormRestApiJsonPost_Test.java b/source/java/org/alfresco/repo/web/scripts/forms/FormRestApiJsonPost_Test.java index 0d28269e37..bf91e99dd4 100644 --- a/source/java/org/alfresco/repo/web/scripts/forms/FormRestApiJsonPost_Test.java +++ b/source/java/org/alfresco/repo/web/scripts/forms/FormRestApiJsonPost_Test.java @@ -32,6 +32,7 @@ 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; @@ -246,6 +247,62 @@ public class FormRestApiJsonPost_Test extends AbstractTestFormRestApi 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 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 modifiedAssocs = nodeService.getChildAssocs(containerNodeRef); + assertEquals(5, modifiedAssocs.size()); + + // Extract the target nodeRefs to make them easier to examine + associatedNodes = new ArrayList(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)); + } + } + private void checkOriginalAssocsBeforeChanges() { List originalAssocs = nodeService.getTargetAssocs(referencingDocNodeRef, RegexQNamePattern.MATCH_ALL); @@ -258,4 +315,17 @@ public class FormRestApiJsonPost_Test extends AbstractTestFormRestApi assertTrue(associatedNodes.contains(associatedDoc_A)); assertTrue(associatedNodes.contains(associatedDoc_B)); } + + private void checkOriginalChildAssocsBeforeChanges() + { + List originalChildAssocs = nodeService.getChildAssocs(containerNodeRef); + assertEquals(2, originalChildAssocs.size()); + + List associatedNodes = new ArrayList(2); + associatedNodes.add(originalChildAssocs.get(0).getChildRef()); + associatedNodes.add(originalChildAssocs.get(1).getChildRef()); + + assertTrue(associatedNodes.contains(childDoc_A)); + assertTrue(associatedNodes.contains(childDoc_B)); + } }